fix(dashboard): display location details address (#7491)

**What**
- display the address of the location on the details page
- use address formatting utils
- fix caching keys for shipping options

---

CLOSES CORE-2127
This commit is contained in:
Frane Polić authored and GitHub committed 2024-05-29 06:49:30 +00:00
1 parent 01703c155f
commit 7d6a87241d
10 files changed
+39 -21

No files matched your search

@@ -17,6 +17,12 @@ import {
ShippingOptionRes, ShippingOptionRes,
} from "../../types/api-responses" } from "../../types/api-responses"
import { stockLocationsQueryKeys } from "./stock-locations" import { stockLocationsQueryKeys } from "./stock-locations"
import { queryKeysFactory } from "../../lib/query-key-factory"
const SHIPPING_OPTIONS_QUERY_KEY = "shipping_options" as const
export const shippingOptionsQueryKeys = queryKeysFactory(
SHIPPING_OPTIONS_QUERY_KEY
)
export const useShippingOptions = ( export const useShippingOptions = (
query?: Record<string, any>, query?: Record<string, any>,
@@ -27,7 +33,7 @@ export const useShippingOptions = (
) => { ) => {
const { data, ...rest } = useQuery({ const { data, ...rest } = useQuery({
queryFn: () => client.shippingOptions.list(query), queryFn: () => client.shippingOptions.list(query),
queryKey: stockLocationsQueryKeys.all, queryKey: shippingOptionsQueryKeys.list(query),
...options, ...options,
}) })
@@ -47,6 +53,9 @@ export const useCreateShippingOptions = (
queryClient.invalidateQueries({ queryClient.invalidateQueries({
queryKey: stockLocationsQueryKeys.all, queryKey: stockLocationsQueryKeys.all,
}) })
queryClient.invalidateQueries({
queryKey: shippingOptionsQueryKeys.all,
})
options?.onSuccess?.(data, variables, context) options?.onSuccess?.(data, variables, context)
}, },
...options, ...options,
@@ -67,6 +76,9 @@ export const useUpdateShippingOptions = (
queryClient.invalidateQueries({ queryClient.invalidateQueries({
queryKey: stockLocationsQueryKeys.all, queryKey: stockLocationsQueryKeys.all,
}) })
queryClient.invalidateQueries({
queryKey: shippingOptionsQueryKeys.all,
})
options?.onSuccess?.(data, variables, context) options?.onSuccess?.(data, variables, context)
}, },
...options, ...options,
@@ -83,6 +95,9 @@ export const useDeleteShippingOption = (
queryClient.invalidateQueries({ queryClient.invalidateQueries({
queryKey: stockLocationsQueryKeys.all, queryKey: stockLocationsQueryKeys.all,
}) })
queryClient.invalidateQueries({
queryKey: shippingOptionsQueryKeys.all,
})
options?.onSuccess?.(data, variables, context) options?.onSuccess?.(data, variables, context)
}, },
@@ -40,7 +40,7 @@ export const useStockLocation = (
) => { ) => {
const { data, ...rest } = useQuery({ const { data, ...rest } = useQuery({
queryFn: () => client.stockLocations.retrieve(id, query), queryFn: () => client.stockLocations.retrieve(id, query),
queryKey: stockLocationsQueryKeys.details(), queryKey: stockLocationsQueryKeys.detail(id, query),
...options, ...options,
}) })
@@ -1,7 +1,8 @@
import { Address } from "@medusajs/medusa" import { AddressDTO } from "@medusajs/types"
import { countries } from "./countries" import { countries } from "./countries"
export const isSameAddress = (a: Address | null, b: Address | null) => { export const isSameAddress = (a: AddressDTO | null, b: AddressDTO | null) => {
if (!a || !b) { if (!a || !b) {
return false return false
} }
@@ -21,7 +22,7 @@ export const isSameAddress = (a: Address | null, b: Address | null) => {
export const getFormattedAddress = ({ export const getFormattedAddress = ({
address, address,
}: { }: {
address?: Partial<Address> | null address?: Partial<AddressDTO> | null
}) => { }) => {
if (!address) { if (!address) {
return [] return []
@@ -42,6 +42,7 @@ import {
isOptionEnabledInStore, isOptionEnabledInStore,
isReturnOption, isReturnOption,
} from "../../../../../lib/shipping-options" } from "../../../../../lib/shipping-options"
import { getFormattedAddress } from "../../../../../lib/addresses"
type LocationGeneralSectionProps = { type LocationGeneralSectionProps = {
location: StockLocationDTO location: StockLocationDTO
@@ -54,7 +55,12 @@ export const LocationGeneralSection = ({
<> <>
<Container className="p-0"> <Container className="p-0">
<div className="flex items-center justify-between px-6 py-4"> <div className="flex items-center justify-between px-6 py-4">
<Heading>{location.name}</Heading> <div>
<Heading>{location.name}</Heading>
<Text className="text-ui-fg-subtle txt-small">
{getFormattedAddress({ address: location.address }).join(", ")}
</Text>
</div>
<Actions location={location} /> <Actions location={location} />
</div> </div>
</Container> </Container>
@@ -0,0 +1,2 @@
export const detailsFields =
"name,*sales_channels,*address,fulfillment_sets.type,fulfillment_sets.name,*fulfillment_sets.service_zones.geo_zones,*fulfillment_sets.service_zones,*fulfillment_sets.service_zones.shipping_options,*fulfillment_sets.service_zones.shipping_options.rules,*fulfillment_sets.service_zones.shipping_options.shipping_profile"
@@ -4,13 +4,13 @@ import { LoaderFunctionArgs } from "react-router-dom"
import { stockLocationsQueryKeys } from "../../../hooks/api/stock-locations" import { stockLocationsQueryKeys } from "../../../hooks/api/stock-locations"
import { client } from "../../../lib/client" import { client } from "../../../lib/client"
import { queryClient } from "../../../lib/query-client" import { queryClient } from "../../../lib/query-client"
import { detailsFields } from "./const"
const locationQuery = (id: string) => ({ const locationQuery = (id: string) => ({
queryKey: stockLocationsQueryKeys.detail(id), queryKey: stockLocationsQueryKeys.detail(id),
queryFn: async () => queryFn: async () =>
client.stockLocations.retrieve(id, { client.stockLocations.retrieve(id, {
fields: fields: detailsFields,
"name,*sales_channels,address.city,address.country_code,fulfillment_sets.type,fulfillment_sets.name,*fulfillment_sets.service_zones.geo_zones,*fulfillment_sets.service_zones,*fulfillment_sets.service_zones.shipping_options,*fulfillment_sets.service_zones.shipping_options.rules,*fulfillment_sets.service_zones.shipping_options.shipping_profile",
}), }),
}) })
@@ -10,6 +10,7 @@ import after from "virtual:medusa/widgets/location/details/after"
import before from "virtual:medusa/widgets/location/details/before" import before from "virtual:medusa/widgets/location/details/before"
import sideAfter from "virtual:medusa/widgets/location/details/side/after" import sideAfter from "virtual:medusa/widgets/location/details/side/after"
import sideBefore from "virtual:medusa/widgets/location/details/side/before" import sideBefore from "virtual:medusa/widgets/location/details/side/before"
import { detailsFields } from "./const"
export const LocationDetails = () => { export const LocationDetails = () => {
const initialData = useLoaderData() as Awaited< const initialData = useLoaderData() as Awaited<
@@ -25,8 +26,7 @@ export const LocationDetails = () => {
} = useStockLocation( } = useStockLocation(
location_id!, location_id!,
{ {
fields: fields: detailsFields,
"name,*sales_channels,address.city,address.country_code,fulfillment_sets.type,fulfillment_sets.name,*fulfillment_sets.service_zones.geo_zones,*fulfillment_sets.service_zones,*fulfillment_sets.service_zones.shipping_options,*fulfillment_sets.service_zones.shipping_options.rules,*fulfillment_sets.service_zones.shipping_options.shipping_profile",
}, },
{ {
initialData, initialData,
@@ -18,7 +18,7 @@ import { useNavigate } from "react-router-dom"
import { ActionMenu } from "../../../../../components/common/action-menu" import { ActionMenu } from "../../../../../components/common/action-menu"
import { BadgeListSummary } from "../../../../../components/common/badge-list-summary" import { BadgeListSummary } from "../../../../../components/common/badge-list-summary"
import { useDeleteStockLocation } from "../../../../../hooks/api/stock-locations" import { useDeleteStockLocation } from "../../../../../hooks/api/stock-locations"
import { countries } from "../../../../../lib/countries" import { getFormattedAddress } from "../../../../../lib/addresses"
type SalesChannelsProps = { type SalesChannelsProps = {
salesChannels?: SalesChannelDTO[] salesChannels?: SalesChannelDTO[]
@@ -150,13 +150,7 @@ function Location(props: LocationProps) {
<div className="grow-1 flex flex-1 flex-col"> <div className="grow-1 flex flex-1 flex-col">
<Text weight="plus">{location.name}</Text> <Text weight="plus">{location.name}</Text>
<Text className="text-ui-fg-subtle txt-small"> <Text className="text-ui-fg-subtle txt-small">
{location.address?.city},{" "} {getFormattedAddress({ address: location.address }).join(", ")}
{
countries.find(
(c) =>
location.address?.country_code.toLowerCase() === c.iso_2
)?.display_name
}
</Text> </Text>
</div> </div>
@@ -70,7 +70,7 @@ export const EditShippingOptionForm = ({
const storeRule = rules.find((r) => r.attribute === "enabled_in_store") const storeRule = rules.find((r) => r.attribute === "enabled_in_store")
if (!storeRule) { if (!storeRule) {
// NOTE: should always exist sice we always create this rule when we create a shipping option // NOTE: should always exist since we always create this rule when we create a shipping option
rules.push({ rules.push({
value: values.enabled_in_store ? "true" : "false", value: values.enabled_in_store ? "true" : "false",
attribute: "enabled_in_store", attribute: "enabled_in_store",
@@ -248,7 +248,7 @@ export const EditShippingOptionForm = ({
</div> </div>
<Form.Hint className="!mt-1"> <Form.Hint className="!mt-1">
{t( {t(
"shipping.shippingOptions.create.enableDescription" "location.shippingOptions.create.enableDescription"
)} )}
</Form.Hint> </Form.Hint>
<Form.ErrorMessage /> <Form.ErrorMessage />
@@ -204,7 +204,7 @@ export function EditShippingOptionsPricingForm({
await mutateAsync({ await mutateAsync({
prices: [...currencyPrices, ...regionPrices], prices: [...currencyPrices, ...regionPrices],
}) })
toast.error(t("general.success"), { toast.success(t("general.success"), {
dismissLabel: t("general.close"), dismissLabel: t("general.close"),
}) })
handleSuccess() handleSuccess()