From bca731a14880ec6f4e54f6c352f05a8eae9d1559 Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Tue, 7 Mar 2023 10:04:25 +0100 Subject: [PATCH] chore: Product page shows list of categories associated with it (#3400) --- .../molecules/delimited-list/index.tsx | 44 +++++++++++++++++ .../sales-channels-display/index.tsx | 8 ++-- .../product-table/use-product-column.tsx | 42 ++++------------ .../products/edit/sections/general/index.tsx | 48 +++++++++++-------- .../src/providers/feature-flag-provider.tsx | 4 ++ 5 files changed, 90 insertions(+), 56 deletions(-) create mode 100644 packages/admin-ui/ui/src/components/molecules/delimited-list/index.tsx diff --git a/packages/admin-ui/ui/src/components/molecules/delimited-list/index.tsx b/packages/admin-ui/ui/src/components/molecules/delimited-list/index.tsx new file mode 100644 index 0000000000..b025128f3f --- /dev/null +++ b/packages/admin-ui/ui/src/components/molecules/delimited-list/index.tsx @@ -0,0 +1,44 @@ +import React from "react" +import Tooltip from "../../atoms/tooltip" + +type DelimitedListProps = { + list: string[] + delimit?: number +} + +const DelimitedList: React.FC = ({ list, delimit = 1 }) => { + if (!list.length) { + return <> + } + + const itemsToDisplay = list.slice(0, delimit).join(", ") + const showExtraItemsInTooltip = list.length > delimit + const extraItemsInToolTipCount = list.length - delimit + + const ToolTipContent = () => { + return ( +
+ {list.slice(delimit).map((listItem) => ( + {listItem} + ))} +
+ ) + } + + return ( + + {itemsToDisplay} + + {showExtraItemsInTooltip && ( + }> + + {" "} + + {extraItemsInToolTipCount} more + + + )} + + ) +} + +export default DelimitedList diff --git a/packages/admin-ui/ui/src/components/molecules/sales-channels-display/index.tsx b/packages/admin-ui/ui/src/components/molecules/sales-channels-display/index.tsx index 7aa4d6d88c..e889d92b11 100644 --- a/packages/admin-ui/ui/src/components/molecules/sales-channels-display/index.tsx +++ b/packages/admin-ui/ui/src/components/molecules/sales-channels-display/index.tsx @@ -13,12 +13,12 @@ const SalesChannelsDisplay = ({ channels = [] }: Props) => { const remainder = Math.max(channels.length - 3, 0) return ( -
+
{channels.length > 0 && (
-
+
{channels.slice(0, 3).map((sc) => ( - + ))}
{remainder > 0 && ( @@ -32,7 +32,7 @@ const SalesChannelsDisplay = ({ channels = [] }: Props) => { } > -
+
+ {remainder} more
diff --git a/packages/admin-ui/ui/src/components/templates/product-table/use-product-column.tsx b/packages/admin-ui/ui/src/components/templates/product-table/use-product-column.tsx index 5f162db562..9e6db4bc67 100644 --- a/packages/admin-ui/ui/src/components/templates/product-table/use-product-column.tsx +++ b/packages/admin-ui/ui/src/components/templates/product-table/use-product-column.tsx @@ -1,8 +1,8 @@ import clsx from "clsx" import { useAdminStore } from "medusa-react" -import React, { useMemo } from "react" +import { useMemo } from "react" import { defaultChannelsSorter } from "../../../utils/sales-channel-compare-operator" -import Tooltip from "../../atoms/tooltip" +import DelimitedList from "../../molecules/delimited-list" import ListIcon from "../../fundamentals/icons/list-icon" import TileIcon from "../../fundamentals/icons/tile-icon" import ImagePlaceholder from "../../fundamentals/image-placeholder" @@ -27,33 +27,11 @@ const useProductTableColumn = ({ setTileView, setListView, showList }) => { const { store } = useAdminStore() const getProductSalesChannels = (salesChannels) => { - if (salesChannels?.length) { - salesChannels.sort( - defaultChannelsSorter(store?.default_sales_channel_id || "") - ) - return ( - - {salesChannels[0].name} - {salesChannels.length > 1 && ( - - {salesChannels.slice(1).map((sc) => ( - {sc.name} - ))} -
- } - > - - {" "} - + {salesChannels.length - 1} more - - - )} - - ) - } - return <> + ;(salesChannels || []).sort( + defaultChannelsSorter(store?.default_sales_channel_id || "") + ) + + return sc.name)} /> } const columns = useMemo( @@ -64,11 +42,11 @@ const useProductTableColumn = ({ setTileView, setListView, showList }) => { Cell: ({ row: { original } }) => { return (
-
+
{original.thumbnail ? ( ) : ( @@ -110,7 +88,7 @@ const useProductTableColumn = ({ setTileView, setListView, showList }) => { { accessor: "col-3", Header: ( -
+
{ type DetailProps = { title: string - value?: string | null + value?: string[] | string | null } const Detail = ({ title, value }: DetailProps) => { + const DetailValue = () => { + if (!Array.isArray(value)) { + return

{value ? value : "–"}

+ } + + if (value.length) { + return + } + + return

+ } + return (

{title}

-

{value ? value : "–"}

+
) } const ProductDetails = ({ product }: Props) => { + const { isFeatureEnabled } = useFeatureFlag() + return (

Details

@@ -115,6 +131,12 @@ const ProductDetails = ({ product }: Props) => { + {isFeatureEnabled(FeatureFlag.PRODUCT_CATEGORIES) && ( + c.name)} + /> + )} { ) } -type SalesChannelBadgeProps = { - channel: SalesChannel -} - -const SalesChannelBadge: React.FC = ({ channel }) => { - return ( - -
- {channel.name} -
-
- ) -} - const ProductSalesChannels = ({ product }: Props) => { return ( diff --git a/packages/admin-ui/ui/src/providers/feature-flag-provider.tsx b/packages/admin-ui/ui/src/providers/feature-flag-provider.tsx index a48c7fa1fb..9b92389081 100644 --- a/packages/admin-ui/ui/src/providers/feature-flag-provider.tsx +++ b/packages/admin-ui/ui/src/providers/feature-flag-provider.tsx @@ -6,6 +6,10 @@ import React, { useState, } from "react" +export enum FeatureFlag { + PRODUCT_CATEGORIES = "product_categories", +} + const defaultFeatureFlagContext: { featureToggleList: Record isFeatureEnabled: (flag: string) => boolean