feat(dashboard): Update v2 store domain to follow conventions, and abstract some logic (#6927)

**What**
- Updates the V2 store domain.
- Abstracts some of the API requests logic into re-usable hooks

**Note**
- Partial PR as we need to add support for setting a default currency, region and location. Currently the `q` param is missing on all V2 endpoints, so we can't use our combobox component to list these options. Will be added later when that has been fixed in core.
- The PR includes a generic hook for fetching combobox data, that I added before realising that `q` is not currently supported. But keeping it as it will be useful once support is added.
This commit is contained in:
Kasper Fabricius Kristensen
2024-04-04 07:15:17 +00:00
committed by GitHub
parent 3044ecaf61
commit 7385a67dc7
17 changed files with 399 additions and 491 deletions
@@ -0,0 +1,37 @@
import { CurrencyDTO } from "@medusajs/types"
import { adminCurrenciesKeys, useAdminCustomQuery } from "medusa-react"
import { V2ListRes } from "./types/common"
// TODO: Add types once we export V2 API types
export const useV2Currencies = (query?: any, options?: any) => {
const { data, ...rest } = useAdminCustomQuery(
`/admin/currencies`,
adminCurrenciesKeys.list(query),
query,
options
)
const typedData: {
currencies: CurrencyDTO[] | undefined
} & V2ListRes = {
currencies: data?.currencies,
count: data?.count,
offset: data?.offset,
limit: data?.limit,
}
return { ...typedData, ...rest }
}
export const useV2Currency = (id: string, options?: any) => {
const { data, ...rest } = useAdminCustomQuery(
`/admin/currencies/${id}`,
adminCurrenciesKeys.detail(id),
undefined,
options
)
const currency: CurrencyDTO | undefined = data?.currency
return { currency, ...rest }
}
@@ -0,0 +1,23 @@
import { RegionDTO } from "@medusajs/types"
import { adminRegionKeys, useAdminCustomQuery } from "medusa-react"
import { V2ListRes } from "./types/common"
export const useV2Regions = (query?: any, options?: any) => {
const { data, ...rest } = useAdminCustomQuery(
"/regions",
adminRegionKeys.list(query),
query,
options
)
const typedData: {
regions: RegionDTO[] | undefined
} & V2ListRes = {
regions: data?.regions,
count: data?.count,
offset: data?.offset,
limit: data?.limit,
}
return { ...typedData, ...rest }
}
@@ -1,18 +1,32 @@
import { adminStoreKeys, useAdminCustomQuery } from "medusa-react"
import {
adminStoreKeys,
useAdminCustomPost,
useAdminCustomQuery,
} from "medusa-react"
import { Store } from "./types/store"
export const useV2Store = ({ initialData }: { initialData?: any }) => {
const { data, isLoading, isError, error } = useAdminCustomQuery(
// TODO: Add types once we export V2 API types
export const useV2Store = (options?: any) => {
const { data, isLoading, isError, error, ...rest } = useAdminCustomQuery(
"/admin/stores",
adminStoreKeys.details(),
{},
{ initialData }
undefined,
options
)
const store = data?.stores[0]
const store = data?.stores[0] as Store | undefined
let hasError = isError
let err: Error | null = error
if (!isLoading && !isError && typeof store === "undefined") {
throw new Error("Store does not exist")
hasError = true
err = new Error("Store not found")
}
return { store, isLoading, isError, error }
return { store, isLoading, isError: hasError, error: err, ...rest }
}
export const useV2UpdateStore = (id: string) => {
return useAdminCustomPost(`/admin/stores/${id}`, adminStoreKeys.detail(id))
}
@@ -0,0 +1,5 @@
export type V2ListRes = {
count: number | undefined
offset: number | undefined
limit: number | undefined
}
@@ -0,0 +1,5 @@
import { CurrencyDTO, StoreDTO } from "@medusajs/types"
export type Store = StoreDTO & {
default_currency: CurrencyDTO | null
}