Skip to content

Commit

Permalink
feat(sdk): Replace region calls with the SDK in admin, apply typings … (
Browse files Browse the repository at this point in the history
  • Loading branch information
sradevski authored May 20, 2024
1 parent 521b4e7 commit 025536e
Show file tree
Hide file tree
Showing 30 changed files with 315 additions and 185 deletions.
7 changes: 7 additions & 0 deletions .changeset/silver-coats-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@medusajs/dashboard": patch
"@medusajs/js-sdk": patch
"@medusajs/types": patch
---

Replace region calls with the SDK in admin, apply typings to sdk
2 changes: 1 addition & 1 deletion packages/admin-next/dashboard/src/hooks/api/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const useEmailPassLogin = (
options?: UseMutationOptions<void, Error, EmailPassReq>
) => {
return useMutation({
mutationFn: (payload) => sdk.auth.login(payload),
mutationFn: (payload) => sdk.auth.login("admin", "emailpass", payload),
onSuccess: async (data, variables, context) => {
options?.onSuccess?.(data, variables, context)
},
Expand Down
47 changes: 30 additions & 17 deletions packages/admin-next/dashboard/src/hooks/api/regions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@ import {
useMutation,
useQuery,
} from "@tanstack/react-query"
import { client } from "../../lib/client"
import { sdk } from "../../lib/client"
import { queryClient } from "../../lib/medusa"
import { queryKeysFactory } from "../../lib/query-key-factory"
import { CreateRegionReq, UpdateRegionReq } from "../../types/api-payloads"
import {
RegionDeleteRes,
RegionListRes,
RegionRes,
} from "../../types/api-responses"
import { DeleteResponse, HttpTypes, PaginatedResponse } from "@medusajs/types"

const REGIONS_QUERY_KEY = "regions" as const
const regionsQueryKeys = queryKeysFactory(REGIONS_QUERY_KEY)
Expand All @@ -22,13 +17,18 @@ export const useRegion = (
id: string,
query?: Record<string, any>,
options?: Omit<
UseQueryOptions<RegionRes, Error, RegionRes, QueryKey>,
UseQueryOptions<
{ region: HttpTypes.AdminRegion },
Error,
{ region: HttpTypes.AdminRegion },
QueryKey
>,
"queryFn" | "queryKey"
>
) => {
const { data, ...rest } = useQuery({
queryKey: regionsQueryKeys.detail(id),
queryFn: async () => client.regions.retrieve(id, query),
queryFn: async () => sdk.admin.region.retrieve(id, query),
...options,
})

Expand All @@ -38,12 +38,17 @@ export const useRegion = (
export const useRegions = (
query?: Record<string, any>,
options?: Omit<
UseQueryOptions<RegionListRes, Error, RegionListRes, QueryKey>,
UseQueryOptions<
PaginatedResponse<{ regions: HttpTypes.AdminRegion[] }>,
Error,
PaginatedResponse<{ regions: HttpTypes.AdminRegion[] }>,
QueryKey
>,
"queryFn" | "queryKey"
>
) => {
const { data, ...rest } = useQuery({
queryFn: () => client.regions.list(query),
queryFn: () => sdk.admin.region.list(query),
queryKey: regionsQueryKeys.list(query),
...options,
})
Expand All @@ -52,10 +57,14 @@ export const useRegions = (
}

export const useCreateRegion = (
options?: UseMutationOptions<RegionRes, Error, CreateRegionReq>
options?: UseMutationOptions<
{ region: HttpTypes.AdminRegion },
Error,
HttpTypes.AdminCreateRegion
>
) => {
return useMutation({
mutationFn: (payload) => client.regions.create(payload),
mutationFn: (payload) => sdk.admin.region.create(payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({ queryKey: regionsQueryKeys.lists() })
options?.onSuccess?.(data, variables, context)
Expand All @@ -66,10 +75,14 @@ export const useCreateRegion = (

export const useUpdateRegion = (
id: string,
options?: UseMutationOptions<RegionRes, Error, UpdateRegionReq>
options?: UseMutationOptions<
{ region: HttpTypes.AdminRegion },
Error,
HttpTypes.AdminUpdateRegion
>
) => {
return useMutation({
mutationFn: (payload) => client.regions.update(id, payload),
mutationFn: (payload) => sdk.admin.region.update(id, payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({ queryKey: regionsQueryKeys.lists() })
queryClient.invalidateQueries({ queryKey: regionsQueryKeys.detail(id) })
Expand All @@ -82,10 +95,10 @@ export const useUpdateRegion = (

export const useDeleteRegion = (
id: string,
options?: UseMutationOptions<RegionDeleteRes, Error, void>
options?: UseMutationOptions<DeleteResponse<"region">, Error, void>
) => {
return useMutation({
mutationFn: () => client.regions.delete(id),
mutationFn: () => sdk.admin.region.delete(id),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({ queryKey: regionsQueryKeys.lists() })
queryClient.invalidateQueries({ queryKey: regionsQueryKeys.detail(id) })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createColumnHelper } from "@tanstack/react-table"
import { useMemo } from "react"
import { RegionDTO } from "@medusajs/types"

import {
CountriesCell,
Expand All @@ -14,8 +13,9 @@ import {
RegionCell,
RegionHeader,
} from "../../../components/table/table-cells/region/region-cell"
import { HttpTypes } from "@medusajs/types"

const columnHelper = createColumnHelper<RegionDTO>()
const columnHelper = createColumnHelper<HttpTypes.AdminRegion>()

export const useRegionTableColumns = () => {
return useMemo(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AdminGetRegionsParams } from "@medusajs/medusa"
import { FindParams, HttpTypes } from "@medusajs/types"
import { useQueryParams } from "../../use-query-params"

type UseRegionTableQueryProps = {
Expand All @@ -17,7 +17,7 @@ export const useRegionTableQuery = ({

const { offset, q, order, created_at, updated_at } = queryObject

const searchParams: AdminGetRegionsParams = {
const searchParams: FindParams & HttpTypes.AdminRegionFilters = {
limit: pageSize,
offset: offset ? Number(offset) : 0,
order,
Expand Down
2 changes: 0 additions & 2 deletions packages/admin-next/dashboard/src/lib/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { priceLists } from "./price-lists"
import { productTypes } from "./product-types"
import { products } from "./products"
import { promotions } from "./promotions"
import { regions } from "./regions"
import { orders } from "./orders"
import { fulfillments } from "./fulfillments"
import { reservations } from "./reservations"
Expand Down Expand Up @@ -45,7 +44,6 @@ export const client = {
tags: tags,
users: users,
orders: orders,
regions: regions,
taxes: taxes,
invites: invites,
inventoryItems: inventoryItems,
Expand Down
35 changes: 0 additions & 35 deletions packages/admin-next/dashboard/src/lib/client/regions.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
AdminCollectionsRes,
AdminProductsRes,
AdminRegionsRes,
} from "@medusajs/medusa"
import { AdminCollectionsRes, AdminProductsRes } from "@medusajs/medusa"
import {
AdminApiKeyResponse,
AdminCustomerGroupResponse,
Expand All @@ -11,6 +7,7 @@ import {
AdminTaxRegionResponse,
SalesChannelDTO,
UserDTO,
HttpTypes,
} from "@medusajs/types"
import { Outlet, RouteObject } from "react-router-dom"

Expand Down Expand Up @@ -593,7 +590,8 @@ export const RouteMap: RouteObject[] = [
path: ":id",
lazy: () => import("../../v2-routes/regions/region-detail"),
handle: {
crumb: (data: AdminRegionsRes) => data.region.name,
crumb: (data: { region: HttpTypes.AdminRegion }) =>
data.region.name,
},
children: [
{
Expand Down
6 changes: 0 additions & 6 deletions packages/admin-next/dashboard/src/types/api-payloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
CreateProductCollectionDTO,
CreatePromotionDTO,
CreatePromotionRuleDTO,
CreateRegionDTO,
CreateSalesChannelDTO,
CreateServiceZoneDTO,
CreateShippingOptionDTO,
Expand All @@ -26,7 +25,6 @@ import {
UpdateProductCollectionDTO,
UpdatePromotionDTO,
UpdatePromotionRuleDTO,
UpdateRegionDTO,
UpdateSalesChannelDTO,
UpdateServiceZoneDTO,
UpdateShippingOptionDTO,
Expand All @@ -38,10 +36,6 @@ import {
// Auth
export type EmailPassReq = { email: string; password: string }

// Regions
export type CreateRegionReq = CreateRegionDTO
export type UpdateRegionReq = UpdateRegionDTO

// Stores
export type UpdateStoreReq = UpdateStoreDTO

Expand Down
6 changes: 0 additions & 6 deletions packages/admin-next/dashboard/src/types/api-responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
ProductTypeDTO,
ProductVariantDTO,
PromotionDTO,
RegionDTO,
SalesChannelDTO,
ShippingOptionDTO,
ShippingProfileDTO,
Expand Down Expand Up @@ -69,11 +68,6 @@ export type ExtendedStoreDTO = StoreDTO & {
export type StoreRes = { store: ExtendedStoreDTO }
export type StoreListRes = { stores: ExtendedStoreDTO[] } & ListRes

// Regions
export type RegionRes = { region: RegionDTO }
export type RegionListRes = { regions: RegionDTO[] } & ListRes
export type RegionDeleteRes = DeleteRes

// Fulfillments
export type FulfillmentRes = { fulfillment: FulfillmentDTO }
export type FulfillmentListRes = { fulfillments: FulfillmentDTO[] } & ListRes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { useTranslation } from "react-i18next"
import * as zod from "zod"

import { Button, Checkbox, toast } from "@medusajs/ui"
import { RegionCountryDTO, RegionDTO } from "@medusajs/types"
import {
RouteFocusModal,
useRouteModal,
Expand All @@ -22,9 +21,10 @@ import { useCountries } from "../../../common/hooks/use-countries"
import { useCountryTableColumns } from "../../../common/hooks/use-country-table-columns"
import { useCountryTableQuery } from "../../../common/hooks/use-country-table-query"
import { useUpdateRegion } from "../../../../../hooks/api/regions"
import { HttpTypes } from "@medusajs/types"

type AddCountriesFormProps = {
region: RegionDTO
region: HttpTypes.AdminRegion
}

const AddCountriesSchema = zod.object({
Expand Down Expand Up @@ -71,7 +71,7 @@ export const AddCountriesForm = ({ region }: AddCountriesFormProps) => {
iso_3: c.iso_3,
num_code: c.num_code,
region_id: null,
region: {} as RegionDTO,
region: {} as HttpTypes.AdminRegion,
})),
...searchParams,
})
Expand Down Expand Up @@ -102,7 +102,7 @@ export const AddCountriesForm = ({ region }: AddCountriesFormProps) => {

const handleSubmit = form.handleSubmit(async (values) => {
const payload = [
...region.countries.map((c) => c.iso_2),
...(region.countries?.map((c) => c.iso_2) ?? []),
...values.countries,
]

Expand Down Expand Up @@ -162,7 +162,7 @@ export const AddCountriesForm = ({ region }: AddCountriesFormProps) => {
)
}

const columnHelper = createColumnHelper<RegionCountryDTO>()
const columnHelper = createColumnHelper<HttpTypes.AdminRegionCountry>()

const useColumns = () => {
const base = useCountryTableColumns()
Expand Down Expand Up @@ -203,5 +203,5 @@ const useColumns = () => {
...base,
],
[base]
) as ColumnDef<RegionCountryDTO>[]
) as ColumnDef<HttpTypes.AdminRegionCountry>[]
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { PlusMini, Trash } from "@medusajs/icons"
import { Checkbox, Container, Heading, toast, usePrompt } from "@medusajs/ui"
import { RegionCountryDTO, RegionDTO } from "@medusajs/types"
import {
ColumnDef,
RowSelectionState,
Expand All @@ -15,9 +14,10 @@ import { useCountries } from "../../../common/hooks/use-countries"
import { useCountryTableColumns } from "../../../common/hooks/use-country-table-columns"
import { useCountryTableQuery } from "../../../common/hooks/use-country-table-query"
import { useUpdateRegion } from "../../../../../hooks/api/regions"
import { HttpTypes } from "@medusajs/types"

type RegionCountrySectionProps = {
region: RegionDTO
region: HttpTypes.AdminRegion
}

const PREFIX = "c"
Expand Down Expand Up @@ -143,8 +143,8 @@ const CountryActions = ({
country,
region,
}: {
country: RegionCountryDTO
region: RegionDTO
country: HttpTypes.AdminRegionCountry
region: HttpTypes.AdminRegion
}) => {
const { t } = useTranslation()
const prompt = usePrompt()
Expand Down Expand Up @@ -204,7 +204,7 @@ const CountryActions = ({
)
}

const columnHelper = createColumnHelper<RegionCountryDTO>()
const columnHelper = createColumnHelper<HttpTypes.AdminRegionCountry>()

const useColumns = () => {
const base = useCountryTableColumns()
Expand Down Expand Up @@ -243,12 +243,14 @@ const useColumns = () => {
columnHelper.display({
id: "actions",
cell: ({ row, table }) => {
const { region } = table.options.meta as { region: RegionDTO }
const { region } = table.options.meta as {
region: HttpTypes.AdminRegion
}

return <CountryActions country={row.original} region={region} />
},
}),
],
[base]
) as ColumnDef<RegionCountryDTO>[]
) as ColumnDef<HttpTypes.AdminRegionCountry>[]
}
Loading

0 comments on commit 025536e

Please sign in to comment.