diff --git a/src/routes/products/product-detail/components/product-media-section/product-media-section.tsx b/src/routes/products/product-detail/components/product-media-section/product-media-section.tsx index 29a6a5e9..1a090b60 100644 --- a/src/routes/products/product-detail/components/product-media-section/product-media-section.tsx +++ b/src/routes/products/product-detail/components/product-media-section/product-media-section.tsx @@ -1,119 +1,117 @@ -import { PencilSquare, ThumbnailBadge } from "@medusajs/icons" +import { useState } from 'react'; + +import { PencilSquare, ThumbnailBadge } from '@medusajs/icons'; import { Button, Checkbox, + clx, CommandBar, Container, Heading, Text, Tooltip, - clx, - usePrompt, -} from "@medusajs/ui" -import { useState } from "react" -import { useTranslation } from "react-i18next" -import { Link } from "react-router-dom" -import { ActionMenu } from "../../../../../components/common/action-menu" -import { useUpdateProduct } from "../../../../../hooks/api/products" -import { ExtendedAdminProduct } from "../../../../../types/products" + usePrompt +} from '@medusajs/ui'; +import { useTranslation } from 'react-i18next'; +import { Link } from 'react-router-dom'; + +import { ActionMenu } from '../../../../../components/common/action-menu'; +import { useUpdateProduct } from '../../../../../hooks/api/products'; +import { ExtendedAdminProduct } from '../../../../../types/products'; type ProductMedisaSectionProps = { - product: ExtendedAdminProduct -} + product: ExtendedAdminProduct; +}; export const ProductMediaSection = ({ product }: ProductMedisaSectionProps) => { - const { t } = useTranslation() - const prompt = usePrompt() - const [selection, setSelection] = useState>({}) + const { t } = useTranslation(); + const prompt = usePrompt(); + const [selection, setSelection] = useState>({}); - const media = getMedia(product) + const media = getMedia(product); const handleCheckedChange = (id: string) => { - setSelection((prev) => { + setSelection(prev => { if (prev[id]) { - const { [id]: _, ...rest } = prev - return rest + const { [id]: _, ...rest } = prev; + return rest; } else { - return { ...prev, [id]: true } + return { ...prev, [id]: true }; } - }) - } + }); + }; - const { mutateAsync } = useUpdateProduct(product.id) + const { mutateAsync } = useUpdateProduct(product.id); const handleDelete = async () => { - const ids = Object.keys(selection) - const includingThumbnail = ids.some( - (id) => media.find((m) => m.id === id)?.isThumbnail - ) + const ids = Object.keys(selection); + const includingThumbnail = ids.some(id => media.find(m => m.id === id)?.isThumbnail); const res = await prompt({ - title: t("general.areYouSure"), + title: t('general.areYouSure'), description: includingThumbnail - ? t("products.media.deleteWarningWithThumbnail", { - count: ids.length, + ? t('products.media.deleteWarningWithThumbnail', { + count: ids.length }) - : t("products.media.deleteWarning", { - count: ids.length, + : t('products.media.deleteWarning', { + count: ids.length }), - confirmText: t("actions.delete"), - cancelText: t("actions.cancel"), - }) + confirmText: t('actions.delete'), + cancelText: t('actions.cancel') + }); if (!res) { - return + return; } - const mediaToKeep = product.images - ?.filter((i) => ids.includes(i.id)) - .map((i) => ({ url: i.url })) + const mediaToKeep = product.images?.filter(i => !ids.includes(i.id)).map(i => ({ url: i.url })); await mutateAsync( { images: mediaToKeep, - thumbnail: includingThumbnail ? "" : undefined, + thumbnail: includingThumbnail ? '' : undefined }, { onSuccess: () => { - setSelection({}) - }, + setSelection({}); + } } - ) - } + ); + }; return (
- {t("products.media.label")} + {t('products.media.label')} , - }, - ], - }, + label: t('actions.edit'), + to: 'media?view=edit', + icon: + } + ] + } ]} />
{media.length > 0 ? (
{media.map((i, index) => { - const isSelected = selection[i.id] + const isSelected = selection[i.id]; return (
@@ -124,12 +122,15 @@ export const ProductMediaSection = ({ product }: ProductMedisaSectionProps) => {
{i.isThumbnail && (
- +
)} - + {`${product.title} { />
- ) + ); })}
) : ( @@ -149,60 +150,65 @@ export const ProductMediaSection = ({ product }: ProductMedisaSectionProps) => { weight="plus" className="text-ui-fg-subtle" > - {t("products.media.emptyState.header")} + {t('products.media.emptyState.header')} - - {t("products.media.emptyState.description")} + + {t('products.media.emptyState.description')} - )} - {t("general.countSelected", { - count: Object.keys(selection).length, + {t('general.countSelected', { + count: Object.keys(selection).length })}
- ) -} + ); +}; type Media = { - id: string - url: string - isThumbnail: boolean -} + id: string; + url: string; + isThumbnail: boolean; +}; const getMedia = (product: ExtendedAdminProduct) => { - const { images = [], thumbnail } = product + const { images = [], thumbnail } = product; - const media: Media[] = (images || []).map((image) => ({ + const media: Media[] = (images || []).map(image => ({ id: image.id, url: image.url, - isThumbnail: image.url === thumbnail, - })) + isThumbnail: image.url === thumbnail + })); - if (thumbnail && !media.some((mediaItem) => mediaItem.url === thumbnail)) { + if (thumbnail && !media.some(mediaItem => mediaItem.url === thumbnail)) { media.unshift({ - id: "img_thumbnail", + id: 'img_thumbnail', url: thumbnail, - isThumbnail: true, - }) + isThumbnail: true + }); } - return media -} + return media; +};