Skip to content

Commit

Permalink
fix(dashboard): Add Breadcrumb components (#10079)
Browse files Browse the repository at this point in the history
**What**
- Adds Breadcrumb component to all routes that needs breadcrumbs.
- The Breadcrumb components use a combination of loader data and useQuery to ensure that the displayed value is kept up to date if the underlying data is changed via a mutation.
- Also fixes a couple of places where the breadcrumb was not setup correctly.

Resolves CMRC-688
  • Loading branch information
kasperkristensen authored Nov 15, 2024
1 parent 8ed3d87 commit 493d242
Show file tree
Hide file tree
Showing 86 changed files with 1,123 additions and 344 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-horses-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/dashboard": patch
---

fix(dashboard): Ensure Breadcrumbs don't display stale data
14 changes: 8 additions & 6 deletions packages/admin/dashboard/src/components/layout/shell/shell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as Dialog from "@radix-ui/react-dialog"

import { SidebarLeft, TriangleRightMini, XMark } from "@medusajs/icons"
import { IconButton, clx } from "@medusajs/ui"
import { PropsWithChildren } from "react"
import { PropsWithChildren, ReactNode } from "react"
import { useTranslation } from "react-i18next"
import { Link, Outlet, UIMatch, useMatches } from "react-router-dom"

Expand Down Expand Up @@ -45,18 +45,20 @@ const Gutter = ({ children }: PropsWithChildren) => {
const Breadcrumbs = () => {
const matches = useMatches() as unknown as UIMatch<
unknown,
{ crumb?: (data?: unknown) => string }
{
breadcrumb?: (match?: UIMatch) => string | ReactNode
}
>[]

const crumbs = matches
.filter((match) => Boolean(match.handle?.crumb))
.filter((match) => match.handle?.breadcrumb)
.map((match) => {
const handle = match.handle

let label: string | null = null
let label: string | ReactNode | undefined = undefined

try {
label = handle.crumb!(match.data)
label = handle.breadcrumb?.(match)
} catch (error) {
// noop
}
Expand All @@ -70,7 +72,7 @@ const Breadcrumbs = () => {
path: match.pathname,
}
})
.filter(Boolean) as { label: string; path: string }[]
.filter(Boolean) as { label: string | ReactNode; path: string }[]

return (
<ol
Expand Down
17 changes: 13 additions & 4 deletions packages/admin/dashboard/src/hooks/api/products.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,12 @@ export const useProduct = (
id: string,
query?: Record<string, any>,
options?: Omit<
UseQueryOptions<any, FetchError, any, QueryKey>,
UseQueryOptions<
HttpTypes.AdminProductResponse,
FetchError,
HttpTypes.AdminProductResponse,
QueryKey
>,
"queryFn" | "queryKey"
>
) => {
Expand Down Expand Up @@ -302,9 +307,13 @@ export const useUpdateProduct = (
) => {
return useMutation({
mutationFn: (payload) => sdk.admin.product.update(id, payload),
onSuccess: (data, variables, context) => {
queryClient.invalidateQueries({ queryKey: productsQueryKeys.lists() })
queryClient.invalidateQueries({ queryKey: productsQueryKeys.detail(id) })
onSuccess: async (data, variables, context) => {
await queryClient.invalidateQueries({
queryKey: productsQueryKeys.lists(),
})
await queryClient.invalidateQueries({
queryKey: productsQueryKeys.detail(id),
})

options?.onSuccess?.(data, variables, context)
},
Expand Down
13 changes: 8 additions & 5 deletions packages/admin/dashboard/src/hooks/api/shipping-profiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ export const useCreateShippingProfile = (
export const useShippingProfile = (
id: string,
query?: Record<string, any>,
options?: UseQueryOptions<
HttpTypes.AdminShippingProfileResponse,
FetchError,
HttpTypes.AdminShippingProfileResponse,
QueryKey
options?: Omit<
UseQueryOptions<
HttpTypes.AdminShippingProfileResponse,
FetchError,
HttpTypes.AdminShippingProfileResponse,
QueryKey
>,
"queryFn" | "queryKey"
>
) => {
const { data, ...rest } = useQuery({
Expand Down
1 change: 0 additions & 1 deletion packages/admin/dashboard/src/providers/providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Toaster, TooltipProvider } from "@medusajs/ui"
import { QueryClientProvider } from "@tanstack/react-query"
import type { PropsWithChildren } from "react"
import { HelmetProvider } from "react-helmet-async"

import { I18n } from "../components/utilities/i18n"
import {
DashboardExtensionManager,
Expand Down
Loading

0 comments on commit 493d242

Please sign in to comment.