import { jsx, jsxs, Fragment } from 'react/jsx-runtime'; import { Page, useAPIErrorHandler, useNotification, useRBAC, Layouts, Form, useField, isFetchError } from '@strapi/admin/strapi-admin'; import { Button, Flex, Typography, Grid, Field, Combobox, ComboboxOption } from '@strapi/design-system'; import { Check } from '@strapi/icons'; import { useIntl } from 'react-intl'; import { useTypedSelector } from '../modules/hooks.mjs'; import { useGetReleaseSettingsQuery, useUpdateReleaseSettingsMutation } from '../services/release.mjs'; import { getTimezones } from '../utils/time.mjs'; import { SETTINGS_SCHEMA } from '../validation/schemas.mjs'; const ReleasesSettingsPage = ()=>{ const { formatMessage } = useIntl(); const { formatAPIError } = useAPIErrorHandler(); const { toggleNotification } = useNotification(); const { data, isLoading: isLoadingSettings } = useGetReleaseSettingsQuery(); const [updateReleaseSettings, { isLoading: isSubmittingForm }] = useUpdateReleaseSettingsMutation(); const permissions = useTypedSelector((state)=>state.admin_app.permissions['settings']?.['releases']); const { allowedActions: { canUpdate } } = useRBAC(permissions); const { timezoneList } = getTimezones(new Date()); const handleSubmit = async (body)=>{ const { defaultTimezone } = body; const isBodyTimezoneValid = timezoneList.some((timezone)=>timezone.value === defaultTimezone); const newBody = !defaultTimezone || !isBodyTimezoneValid ? { defaultTimezone: null } : { ...body }; try { const response = await updateReleaseSettings(newBody); if ('data' in response) { toggleNotification({ type: 'success', message: formatMessage({ id: 'content-releases.pages.Settings.releases.setting.default-timezone-notification-success', defaultMessage: 'Default timezone updated.' }) }); } else if (isFetchError(response.error)) { toggleNotification({ type: 'danger', message: formatAPIError(response.error) }); } else { toggleNotification({ type: 'danger', message: formatMessage({ id: 'notification.error', defaultMessage: 'An error occurred' }) }); } } catch (error) { toggleNotification({ type: 'danger', message: formatMessage({ id: 'notification.error', defaultMessage: 'An error occurred' }) }); } }; if (isLoadingSettings) { return /*#__PURE__*/ jsx(Page.Loading, {}); } return /*#__PURE__*/ jsxs(Layouts.Root, { children: [ /*#__PURE__*/ jsx(Page.Title, { children: formatMessage({ id: 'Settings.PageTitle', defaultMessage: 'Settings - {name}' }, { name: 'Releases' }) }), /*#__PURE__*/ jsx(Page.Main, { "aria-busy": isLoadingSettings, tabIndex: -1, children: /*#__PURE__*/ jsx(Form, { method: "PUT", initialValues: { defaultTimezone: data?.data.defaultTimezone }, onSubmit: handleSubmit, validationSchema: SETTINGS_SCHEMA, children: ({ modified, isSubmitting })=>{ return /*#__PURE__*/ jsxs(Fragment, { children: [ /*#__PURE__*/ jsx(Layouts.Header, { primaryAction: canUpdate ? /*#__PURE__*/ jsx(Button, { disabled: !modified || isSubmittingForm, loading: isSubmitting, startIcon: /*#__PURE__*/ jsx(Check, {}), type: "submit", children: formatMessage({ id: 'global.save', defaultMessage: 'Save' }) }) : null, title: formatMessage({ id: 'content-releases.pages.Settings.releases.title', defaultMessage: 'Releases' }), subtitle: formatMessage({ id: 'content-releases.pages.Settings.releases.description', defaultMessage: 'Create and manage content updates' }) }), /*#__PURE__*/ jsx(Layouts.Content, { children: /*#__PURE__*/ jsxs(Flex, { direction: "column", background: "neutral0", alignItems: "stretch", padding: 6, gap: 6, shadow: "filterShadow", hasRadius: true, children: [ /*#__PURE__*/ jsx(Typography, { variant: "delta", tag: "h2", children: formatMessage({ id: 'content-releases.pages.Settings.releases.preferences.title', defaultMessage: 'Preferences' }) }), /*#__PURE__*/ jsx(Grid.Root, { children: /*#__PURE__*/ jsx(Grid.Item, { col: 6, s: 12, direction: "column", alignItems: "stretch", children: /*#__PURE__*/ jsx(TimezoneDropdown, {}) }) }) ] }) }) ] }); } }) }) ] }); }; const TimezoneDropdown = ()=>{ const permissions = useTypedSelector((state)=>state.admin_app.permissions['settings']?.['releases']); const { allowedActions: { canUpdate } } = useRBAC(permissions); const { formatMessage } = useIntl(); const { timezoneList } = getTimezones(new Date()); const field = useField('defaultTimezone'); return /*#__PURE__*/ jsxs(Field.Root, { name: "defaultTimezone", hint: formatMessage({ id: 'content-releases.pages.Settings.releases.timezone.hint', defaultMessage: 'The timezone of every release can still be changed individually. ' }), error: field.error, children: [ /*#__PURE__*/ jsx(Field.Label, { children: formatMessage({ id: 'content-releases.pages.Settings.releases.timezone.label', defaultMessage: 'Default timezone' }) }), /*#__PURE__*/ jsx(Combobox, { autocomplete: { type: 'list', filter: 'contains' }, onChange: (value)=>field.onChange('defaultTimezone', value), onTextValueChange: (value)=>field.onChange('defaultTimezone', value), onClear: ()=>field.onChange('defaultTimezone', ''), value: field.value, disabled: !canUpdate, children: timezoneList.map((timezone)=>/*#__PURE__*/ jsx(ComboboxOption, { value: timezone.value, children: timezone.value.replace(/&/, ' ') }, timezone.value)) }), /*#__PURE__*/ jsx(Field.Hint, {}), /*#__PURE__*/ jsx(Field.Error, {}) ] }); }; /* ------------------------------------------------------------------------------------------------- * ProtectedSettingsPage * -----------------------------------------------------------------------------------------------*/ const ProtectedReleasesSettingsPage = ()=>{ const permissions = useTypedSelector((state)=>state.admin_app.permissions['settings']?.['releases']?.read); return /*#__PURE__*/ jsx(Page.Protect, { permissions: permissions, children: /*#__PURE__*/ jsx(ReleasesSettingsPage, {}) }); }; export { ProtectedReleasesSettingsPage }; //# sourceMappingURL=ReleasesSettingsPage.mjs.map