Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dashboard): regions v2 #6943

Merged
merged 21 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@
"removeCountriesWarning_other": "You are about to remove {{count}} countries from the region. This action cannot be undone.",
"removeCountryWarning": "You are about to remove the country {{name}} from the region. This action cannot be undone.",
"taxInclusiveHint": "When enabled prices in the region will be tax inclusive.",
"providersHint": " Add which fulfillment and payment providers should be available in this region.",
"providersHint": " Add which payment providers should be available in this region.",
"shippingOptions": "Shipping Options",
"deleteShippingOptionWarning": "You are about to delete the shipping option {{name}}. This action cannot be undone.",
"return": "Return",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function generateCountries() {
const dest = path.join(__dirname, "../src/lib/countries.ts")
const destDir = path.dirname(dest)

const fileContent = `/** This file is auto-generated. Do not modify it manually. */\nimport type { Country } from "@medusajs/medusa"\n\nexport const countries: Omit<Country, "region" | "region_id" | "id">[] = ${json}`
const fileContent = `/** This file is auto-generated. Do not modify it manually. */\nimport type { RegionCountryDTO } from "@medusajs/types"\n\nexport const countries: Omit<RegionCountryDTO, "id">[] = ${json}`

if (!fs.existsSync(destDir)) {
fs.mkdirSync(destDir, { recursive: true })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Country } from "@medusajs/medusa"
import { useTranslation } from "react-i18next"
import { RegionCountryDTO } from "@medusajs/types"

import { PlaceholderCell } from "../../common/placeholder-cell"
import { ListSummary } from "../../../../common/list-summary"
import { countries as COUNTRIES } from "../../../../../lib/countries"

type CountriesCellProps = {
countries?: Country[] | null
countries?: RegionCountryDTO[] | null
}

export const CountriesCell = ({ countries }: CountriesCellProps) => {
Expand All @@ -13,26 +16,14 @@ export const CountriesCell = ({ countries }: CountriesCellProps) => {
return <PlaceholderCell />
}

const displayValue = countries
.slice(0, 2)
.map((c) => c.display_name)
.join(", ")

const additionalCountries = countries
.slice(2)
.map((c) => c.display_name).length

const text = `${displayValue}${
additionalCountries > 0
? ` ${t("general.plusCountMore", {
count: additionalCountries,
})}`
: ""
}`

return (
<div className="flex size-full items-center overflow-hidden">
<span className="truncate">{text}</span>
<ListSummary
list={countries.map(
(country) =>
COUNTRIES.find((c) => c.iso_2 === country.iso_2)!.display_name
)}
/>
</div>
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { Region } from "@medusajs/medusa"
import { createColumnHelper } from "@tanstack/react-table"
import { useMemo } from "react"
import { RegionDTO } from "@medusajs/types"

import {
CountriesCell,
CountriesHeader,
} from "../../../components/table/table-cells/region/countries-cell"
import {
FulfillmentProvidersCell,
FulfillmentProvidersHeader,
} from "../../../components/table/table-cells/region/fulfillment-providers-cell"
import {
PaymentProvidersCell,
PaymentProvidersHeader,
Expand All @@ -19,7 +15,7 @@ import {
RegionHeader,
} from "../../../components/table/table-cells/region/region-cell"

const columnHelper = createColumnHelper<Region>()
const columnHelper = createColumnHelper<RegionDTO>()

export const useRegionTableColumns = () => {
return useMemo(
Expand All @@ -38,12 +34,6 @@ export const useRegionTableColumns = () => {
<PaymentProvidersCell paymentProviders={getValue()} />
),
}),
columnHelper.accessor("fulfillment_providers", {
header: () => <FulfillmentProvidersHeader />,
cell: ({ getValue }) => (
<FulfillmentProvidersCell fulfillmentProviders={getValue()} />
),
}),
],
[]
)
Expand Down
41 changes: 39 additions & 2 deletions packages/admin-next/dashboard/src/lib/api-v2/region.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { RegionDTO } from "@medusajs/types"
import { adminRegionKeys, useAdminCustomQuery } from "medusa-react"
import { CreateRegionDTO, RegionDTO, UpdateRegionDTO } from "@medusajs/types"
import {
adminRegionKeys,
useAdminCustomDelete,
useAdminCustomPost,
useAdminCustomQuery,
} from "medusa-react"

import { V2ListRes } from "./types/common"

export const useV2Regions = (query?: any, options?: any) => {
Expand All @@ -21,3 +27,34 @@ export const useV2Regions = (query?: any, options?: any) => {

return { ...typedData, ...rest }
}

export const useV2Region = (id: string, options?: any) => {
const { data, ...rest } = useAdminCustomQuery(
`/regions/${id}`,
adminRegionKeys.detail(id),
undefined,
options
)

const region: RegionDTO | undefined = data?.region

return { region, ...rest }
}

export const useV2CreateRegion = () => {
return useAdminCustomPost<CreateRegionDTO, { region: RegionDTO }>(
`/regions`,
adminRegionKeys.list()
)
}

export const useV2UpdateRegion = (id: string) => {
return useAdminCustomPost<UpdateRegionDTO, { region: RegionDTO }>(
`/regions/${id}`,
adminRegionKeys.detail(id)
)
}

export const useV2DeleteRegion = (id: string) => {
return useAdminCustomDelete(`/regions/${id}`, adminRegionKeys.list())
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { CurrencyDTO, StoreDTO } from "@medusajs/types"
import { CurrencyDTO, PaymentProviderDTO, StoreDTO } from "@medusajs/types"

export type Store = StoreDTO & {
default_currency: CurrencyDTO | null
currencies?: CurrencyDTO[]
payment_providers?: PaymentProviderDTO[]
}
8 changes: 4 additions & 4 deletions packages/admin-next/dashboard/src/lib/countries.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** This file is auto-generated. Do not modify it manually. */
import type { Country } from "@medusajs/medusa"
import type { RegionCountryDTO } from "@medusajs/types"

export const countries: Omit<Country, "region" | "region_id" | "id">[] = [
export const countries: Omit<RegionCountryDTO, "id">[] = [
{
iso_2: "af",
iso_3: "afg",
Expand Down Expand Up @@ -888,8 +888,8 @@ export const countries: Omit<Country, "region" | "region_id" | "id">[] = [
iso_2: "ly",
iso_3: "lby",
num_code: 434,
name: "LIBYAN ARAB JAMAHIRIYA",
display_name: "Libyan Arab Jamahiriya",
name: "LIBYA",
display_name: "Libya",
},
{
iso_2: "li",
Expand Down
51 changes: 0 additions & 51 deletions packages/admin-next/dashboard/src/providers/router-provider/v1.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -595,57 +595,6 @@ export const v1Routes: RouteObject[] = [
},
],
},
{
path: "regions",
element: <Outlet />,
handle: {
crumb: () => "Regions",
},
children: [
{
path: "",
lazy: () => import("../../routes/regions/region-list"),
children: [
{
path: "create",
lazy: () => import("../../routes/regions/region-create"),
},
],
},
{
path: ":id",
lazy: () => import("../../routes/regions/region-detail"),
handle: {
crumb: (data: AdminRegionsRes) => data.region.name,
},
children: [
{
path: "edit",
lazy: () => import("../../routes/regions/region-edit"),
},
{
path: "countries/add",
lazy: () =>
import("../../routes/regions/region-add-countries"),
},
{
path: "shipping-options/:so_id/edit",
lazy: () =>
import(
"../../routes/regions/region-edit-shipping-option"
),
},
{
path: "shipping-options/create",
lazy: () =>
import(
"../../routes/regions/region-create-shipping-option"
),
},
],
},
],
},
{
path: "users",
element: <Outlet />,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Navigate, RouteObject, useLocation } from "react-router-dom"
import { AdminCollectionsRes, type AdminRegionsRes } from "@medusajs/medusa"
import { SalesChannelDTO, UserDTO } from "@medusajs/types"
import { Spinner } from "@medusajs/icons"

import { AdminCollectionsRes } from "@medusajs/medusa"
import { ErrorBoundary } from "../../components/error/error-boundary"
import { MainLayout } from "../../components/layout-v2/main-layout"
import { Outlet } from "react-router-dom"
import { SearchProvider } from "../search-provider"
import { SettingsLayout } from "../../components/layout/settings-layout"
import { SidebarProvider } from "../sidebar-provider"
import { Spinner } from "@medusajs/icons"
import { useV2Session } from "../../lib/api-v2"

export const ProtectedRoute = () => {
Expand Down Expand Up @@ -164,6 +164,43 @@ export const v2Routes: RouteObject[] = [
},
],
},
{
path: "regions",
element: <Outlet />,
handle: {
crumb: () => "Regions",
},
children: [
{
path: "",
lazy: () => import("../../v2-routes/regions/region-list"),
children: [
{
path: "create",
lazy: () => import("../../v2-routes/regions/region-create"),
},
],
},
{
path: ":id",
lazy: () => import("../../v2-routes/regions/region-detail"),
handle: {
crumb: (data: AdminRegionsRes) => data.region.name,
},
children: [
{
path: "edit",
lazy: () => import("../../v2-routes/regions/region-edit"),
},
{
path: "countries/add",
lazy: () =>
import("../../v2-routes/regions/region-add-countries"),
},
],
},
],
},
{
path: "store",
lazy: () => import("../../v2-routes/store/store-detail"),
Expand Down
Loading
Loading