From e5db81379862754fee002d0988e204dd9a36f043 Mon Sep 17 00:00:00 2001 From: Fernando Chavez Date: Mon, 9 Sep 2024 18:06:24 +0200 Subject: [PATCH] fix: change status on locale picker --- .../src/pages/EditView/components/Header.tsx | 2 + .../admin/src/components/CMHeaderActions.tsx | 92 ++++++++++++++----- .../i18n/admin/src/translations/en.json | 1 + 3 files changed, 71 insertions(+), 24 deletions(-) diff --git a/packages/core/content-manager/admin/src/pages/EditView/components/Header.tsx b/packages/core/content-manager/admin/src/pages/EditView/components/Header.tsx index adba2248fd..868d8d4239 100644 --- a/packages/core/content-manager/admin/src/pages/EditView/components/Header.tsx +++ b/packages/core/content-manager/admin/src/pages/EditView/components/Header.tsx @@ -108,6 +108,7 @@ interface HeaderActionDescription { }>; onSelect?: (value: string) => void; value?: string; + customizeContent?: (value: string) => React.ReactNode; } /** @@ -369,6 +370,7 @@ const HeaderActions = ({ actions }: HeaderActionsProps) => { // @ts-expect-error – the DS will handle numbers, but we're not allowing the API. onChange={action.onSelect} value={action.value} + customizeContent={action.customizeContent} > {action.options.map(({ label, ...option }) => ( diff --git a/packages/plugins/i18n/admin/src/components/CMHeaderActions.tsx b/packages/plugins/i18n/admin/src/components/CMHeaderActions.tsx index 8eacc95ad9..ba15ac808e 100644 --- a/packages/plugins/i18n/admin/src/components/CMHeaderActions.tsx +++ b/packages/plugins/i18n/admin/src/components/CMHeaderActions.tsx @@ -28,7 +28,7 @@ import { SingleSelectOption, Dialog, } from '@strapi/design-system'; -import { WarningCircle, ListPlus, Trash, Download, Cross } from '@strapi/icons'; +import { WarningCircle, ListPlus, Trash, Download, Cross, Plus } from '@strapi/icons'; import { Modules } from '@strapi/types'; import { useIntl } from 'react-intl'; import { useNavigate } from 'react-router-dom'; @@ -50,6 +50,59 @@ import type { I18nBaseQuery } from '../types'; * LocalePickerAction * -----------------------------------------------------------------------------------------------*/ +interface LocaleOptionProps { + isDraftAndPublishEnabled: boolean; + locale: Locale; + status: 'draft' | 'published' | 'modified'; + entryExists: boolean; +} + +const LocaleOption = ({ + isDraftAndPublishEnabled, + locale, + status, + entryExists, +}: LocaleOptionProps) => { + const { formatMessage } = useIntl(); + + if (!entryExists) { + return formatMessage( + { + id: getTranslation('CMEditViewLocalePicker.locale.create'), + defaultMessage: 'Create {locale} locale', + }, + { + bold: (locale: React.ReactNode) => {locale}, + locale: locale.name, + } + ); + } + + return ( + + {locale.name} + {isDraftAndPublishEnabled ? ( + + + {capitalize(status)} + + + ) : null} + + ); +}; + const LocalePickerAction = ({ document, meta, @@ -99,11 +152,11 @@ const LocalePickerAction = ({ }, [handleSelect, hasI18n, locales, currentDesiredLocale]); const currentLocale = Array.isArray(locales) - ? locales.find((locale) => locale.code === currentDesiredLocale)?.code + ? locales.find((locale) => locale.code === currentDesiredLocale) : undefined; const allCurrentLocales = [ - { status: getDocumentStatus(document, meta), locale: currentLocale }, + { status: getDocumentStatus(document, meta), locale: currentLocale?.code }, ...(meta?.availableLocales ?? []), ]; @@ -117,38 +170,29 @@ const LocalePickerAction = ({ defaultMessage: 'Locales', }), options: locales.map((locale) => { + const entryWithLocaleExists = allCurrentLocales.some((doc) => doc.locale === locale.code); + const currentLocaleDoc = allCurrentLocales.find((doc) => 'locale' in doc ? doc.locale === locale.code : false ); - const status = currentLocaleDoc?.status ?? 'draft'; const permissionsToCheck = currentLocaleDoc ? canCreate : canRead; - const statusVariant = - status === 'draft' ? 'secondary' : status === 'published' ? 'success' : 'alternative'; - return { disabled: !permissionsToCheck.includes(locale.code), value: locale.code, - label: locale.name, - startIcon: schema?.options?.draftAndPublish ? ( - - - {capitalize(status)} - - - ) : null, + label: ( + + ), + startIcon: !entryWithLocaleExists ? : null, }; }), + customizeContent: () => currentLocale?.name, onSelect: handleSelect, value: currentLocale, }; diff --git a/packages/plugins/i18n/admin/src/translations/en.json b/packages/plugins/i18n/admin/src/translations/en.json index f9f67afe7d..b057b98bd4 100644 --- a/packages/plugins/i18n/admin/src/translations/en.json +++ b/packages/plugins/i18n/admin/src/translations/en.json @@ -18,6 +18,7 @@ "CMEditViewBulkLocale.publication-status": "Publication Status", "CMEditViewBulkLocale.draft-relation-warning": "Some locales are related to draft entries. Publishing them could leave broken links in your app.", "CMEditViewBulkLocale.continue-confirmation": "Are you sure you want to continue?", + "CMEditViewLocalePicker.locale.create": "Create {locale} locale", "CMListView.popover.display-locales.label": "Display translated locales", "CheckboxConfirmation.Modal.body": "Do you want to disable it?", "CheckboxConfirmation.Modal.button-confirm": "Yes, disable",