feat(medusa-js,medusa-react,medusa): Add missing Currency endpoints (#2185)

This commit is contained in:
Kasper Fabricius Kristensen authored and GitHub committed 2022-09-12 19:28:43 +02:00
1 parent b6161d2404
commit 64949dc721
13 files changed
+233 -17

No files matched your search

+18 -9
View File
@@ -1,5 +1,12 @@
{
"resources": {
"currency": {
"symbol": "$",
"name": "US Dollar",
"symbol_native": "$",
"code": "USD",
"includes_tax": false
},
"region": {
"id": "reg_01F0YES4R67TXXC1QBQ8P54A8Y",
"name": "Test Region",
@@ -1271,14 +1278,16 @@
"created_at": "2022-07-05T15:16:01.959Z",
"deleted_at": null
},
"sales_channels": [{
"id": "sc_01F0YES4R67TXXC1QBQ8P54A8Y",
"name": "sales channel 1 name",
"description": "sales channel 1 description",
"is_disabled": false,
"updated_at": "2022-07-05T15:16:01.959Z",
"created_at": "2022-07-05T15:16:01.959Z",
"deleted_at": null
}]
"sales_channels": [
{
"id": "sc_01F0YES4R67TXXC1QBQ8P54A8Y",
"name": "sales channel 1 name",
"description": "sales channel 1 description",
"is_disabled": false,
"updated_at": "2022-07-05T15:16:01.959Z",
"created_at": "2022-07-05T15:16:01.959Z",
"deleted_at": null
}
]
}
}
+25 -5
View File
@@ -1,5 +1,4 @@
import { rest } from "msw"
import { body } from "msw/lib/types/context"
import { fixtures } from "../data"
export const adminHandlers = [
@@ -114,8 +113,8 @@ export const adminHandlers = [
ctx.json({
collection: {
...fixtures.get("product_collection"),
products: [fixtures.get("product")]
}
products: [fixtures.get("product")],
},
})
)
}),
@@ -126,7 +125,7 @@ export const adminHandlers = [
ctx.json({
id: req.params.id,
object: "product-collection",
removed_products: [fixtures.get("product").id]
removed_products: [fixtures.get("product").id],
})
)
}),
@@ -892,7 +891,7 @@ export const adminHandlers = [
discount_condition: {
...fixtures
.get("discount")
.rule.conditions.find(c => c.id === req.params.conditionId),
.rule.conditions.find((c) => c.id === req.params.conditionId),
},
})
)
@@ -1767,4 +1766,25 @@ export const adminHandlers = [
})
)
}),
rest.get("/admin/currencies", (req, res, ctx) => {
return res(
ctx.status(200),
ctx.json({
count: 1,
limit: 20,
offset: 20,
currencies: fixtures.list("currency", 1),
})
)
}),
rest.post("/admin/currencies/:code", (req, res, ctx) => {
return res(
ctx.status(200),
ctx.json({
currency: { ...fixtures.get("currency"), ...(req.body as any) },
})
)
}),
]
@@ -0,0 +1,2 @@
export * from "./mutations"
export * from "./queries"
@@ -0,0 +1,30 @@
import {
AdminCurrenciesRes,
AdminPostCurrenciesCurrencyReq,
} from "@medusajs/medusa"
import { Response } from "@medusajs/medusa-js"
import { useMutation, UseMutationOptions, useQueryClient } from "react-query"
import { adminCurrenciesKeys } from "."
import { useMedusa } from "../../../contexts/medusa"
import { buildOptions } from "../../utils/buildOptions"
export const useAdminUpdateCurrency = (
code: string,
options?: UseMutationOptions<
Response<AdminCurrenciesRes>,
Error,
AdminPostCurrenciesCurrencyReq
>
) => {
const { client } = useMedusa()
const queryClient = useQueryClient()
return useMutation(
(payload: AdminPostCurrenciesCurrencyReq) =>
client.admin.currencies.update(code, payload),
buildOptions(
queryClient,
[adminCurrenciesKeys.lists(), adminCurrenciesKeys.detail(code)],
options
)
)
}
@@ -0,0 +1,32 @@
import {
AdminCurrenciesListRes,
AdminGetCurrenciesParams,
} from "@medusajs/medusa"
import { Response } from "@medusajs/medusa-js"
import { useQuery } from "react-query"
import { useMedusa } from "../../../contexts"
import { UseQueryOptionsWrapper } from "../../../types"
import { queryKeysFactory } from "../../utils/index"
const ADMIN_CURRENCIES_QUERY_KEY = `admin_currencies` as const
export const adminCurrenciesKeys = queryKeysFactory(ADMIN_CURRENCIES_QUERY_KEY)
type CurrenciesQueryKey = typeof adminCurrenciesKeys
export const useAdminCurrencies = (
query?: AdminGetCurrenciesParams,
options?: UseQueryOptionsWrapper<
Response<AdminCurrenciesListRes>,
Error,
ReturnType<CurrenciesQueryKey["list"]>
>
) => {
const { client } = useMedusa()
const { data, ...rest } = useQuery(
adminCurrenciesKeys.list(query),
() => client.admin.currencies.list(query),
options
)
return { ...data, ...rest } as const
}
@@ -2,6 +2,7 @@ export * from "./auth"
export * from "./batch-jobs"
export * from "./claims"
export * from "./collections"
export * from "./currencies"
export * from "./customer-groups"
export * from "./customers"
export * from "./discounts"
@@ -24,6 +25,6 @@ export * from "./shipping-profiles"
export * from "./store"
export * from "./swaps"
export * from "./tax-rates"
export * from "./uploads"
export * from "./users"
export * from "./variants"
export * from "./uploads"
@@ -0,0 +1,31 @@
import { renderHook } from "@testing-library/react-hooks"
import { fixtures } from "../../../../mocks/data"
import { useAdminUpdateCurrency } from "../../../../src/"
import { createWrapper } from "../../../utils"
describe("useAdminUpdateCurrency hook", () => {
test("updates a currency and returns it", async () => {
const update = {
includes_tax: true,
}
const { result, waitFor } = renderHook(
() => useAdminUpdateCurrency(fixtures.get("currency").code),
{
wrapper: createWrapper(),
}
)
result.current.mutate(update)
await waitFor(() => result.current.isSuccess)
expect(result.current.data?.response.status).toEqual(200)
expect(result.current.data?.currency).toEqual(
expect.objectContaining({
...fixtures.get("currency"),
...update,
})
)
})
})
@@ -0,0 +1,18 @@
import { renderHook } from "@testing-library/react-hooks"
import { fixtures } from "../../../../mocks/data"
import { useAdminCurrencies } from "../../../../src"
import { createWrapper } from "../../../utils"
describe("useAdminCurrencies hook", () => {
test("returns a list of currencies", async () => {
const currencies = fixtures.list("currency", 1)
const { result, waitFor } = renderHook(() => useAdminCurrencies(), {
wrapper: createWrapper(),
})
await waitFor(() => result.current.isSuccess)
expect(result.current.response?.status).toEqual(200)
expect(result.current.currencies).toEqual(currencies)
})
})