2021-03-31 11:18:00 +02:00
|
|
|
import React, { useRef } from 'react';
|
2021-02-08 13:47:43 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2021-09-08 17:02:26 +02:00
|
|
|
import { Form } from '@strapi/helper-plugin';
|
2021-02-02 16:08:16 +01:00
|
|
|
import { useIntl } from 'react-intl';
|
2021-02-09 16:55:57 +01:00
|
|
|
import { Formik } from 'formik';
|
2021-09-08 17:02:26 +02:00
|
|
|
import CheckIcon from '@strapi/icons/CheckIcon';
|
|
|
|
import { ModalLayout, ModalHeader, ModalBody, ModalFooter } from '@strapi/parts/ModalLayout';
|
|
|
|
import { TabGroup, Tabs, Tab, TabPanels, TabPanel } from '@strapi/parts/Tabs';
|
|
|
|
import { Row } from '@strapi/parts/Row';
|
|
|
|
import { Box } from '@strapi/parts/Box';
|
|
|
|
import { Button } from '@strapi/parts/Button';
|
|
|
|
import { Divider } from '@strapi/parts/Divider';
|
|
|
|
import { ButtonText, H2 } from '@strapi/parts/Text';
|
2021-02-12 15:34:20 +01:00
|
|
|
import localeFormSchema from '../../schemas';
|
2021-02-08 13:47:43 +01:00
|
|
|
import useEditLocale from '../../hooks/useEditLocale';
|
2021-02-02 16:08:16 +01:00
|
|
|
import { getTrad } from '../../utils';
|
2021-02-11 10:35:22 +01:00
|
|
|
import BaseForm from './BaseForm';
|
2021-02-25 14:33:25 +01:00
|
|
|
import AdvancedForm from './AdvancedForm';
|
2021-02-02 16:08:16 +01:00
|
|
|
|
2021-09-08 17:02:26 +02:00
|
|
|
const ModalEdit = ({ locale, onClose }) => {
|
2021-02-08 13:47:43 +01:00
|
|
|
const { isEditing, editLocale } = useEditLocale();
|
2021-03-31 11:18:00 +02:00
|
|
|
const shouldUpdateMenu = useRef(false);
|
2021-02-02 16:08:16 +01:00
|
|
|
const { formatMessage } = useIntl();
|
2021-02-08 13:47:43 +01:00
|
|
|
|
2021-02-25 14:33:25 +01:00
|
|
|
const handleSubmit = ({ displayName, isDefault }) => {
|
2021-09-08 17:02:26 +02:00
|
|
|
const id = locale.id;
|
|
|
|
const name = displayName || locale.code;
|
2021-02-09 16:55:57 +01:00
|
|
|
|
2021-03-31 10:04:07 +02:00
|
|
|
return editLocale(id, { name, isDefault })
|
|
|
|
.then(() => {
|
2021-03-31 11:18:00 +02:00
|
|
|
shouldUpdateMenu.current = true;
|
2021-03-31 10:04:07 +02:00
|
|
|
})
|
|
|
|
.then(onClose);
|
|
|
|
};
|
|
|
|
|
2021-02-02 16:08:16 +01:00
|
|
|
return (
|
2021-09-08 17:02:26 +02:00
|
|
|
<ModalLayout onClose={onClose} labelledBy="edit-locale-title">
|
2021-02-09 16:55:57 +01:00
|
|
|
<Formik
|
2021-02-25 14:33:25 +01:00
|
|
|
initialValues={{
|
2021-09-08 17:02:26 +02:00
|
|
|
code: locale?.code,
|
|
|
|
displayName: locale?.name || '',
|
|
|
|
isDefault: Boolean(locale?.isDefault),
|
2021-02-25 14:33:25 +01:00
|
|
|
}}
|
2021-02-09 16:55:57 +01:00
|
|
|
onSubmit={handleSubmit}
|
2021-02-12 15:34:20 +01:00
|
|
|
validationSchema={localeFormSchema}
|
2021-02-09 16:55:57 +01:00
|
|
|
>
|
2021-09-08 17:02:26 +02:00
|
|
|
{({ handleSubmit }) => (
|
|
|
|
<Form onSubmit={handleSubmit}>
|
|
|
|
<ModalHeader>
|
|
|
|
<ButtonText textColor="neutral800" as="h2" id="edit-locale-title">
|
|
|
|
{formatMessage({ id: getTrad('Settings.list.actions.edit') })}
|
|
|
|
</ButtonText>
|
|
|
|
</ModalHeader>
|
|
|
|
<ModalBody
|
2021-02-12 15:34:20 +01:00
|
|
|
title={formatMessage({
|
|
|
|
id: getTrad('Settings.locales.modal.title'),
|
|
|
|
})}
|
2021-08-04 22:23:00 +08:00
|
|
|
breadCrumb={[getTrad('Settings.list.actions.edit')]}
|
2021-02-12 15:34:20 +01:00
|
|
|
tabsAriaLabel={formatMessage({
|
|
|
|
id: getTrad('Settings.locales.modal.edit.tab.label'),
|
|
|
|
})}
|
|
|
|
tabsId="i18n-settings-tabs-edit"
|
|
|
|
>
|
2021-09-08 17:02:26 +02:00
|
|
|
<TabGroup
|
|
|
|
label={formatMessage({
|
|
|
|
id: getTrad('Settings.locales.modal.title'),
|
|
|
|
})}
|
|
|
|
id="tabs"
|
|
|
|
variant="simple"
|
|
|
|
>
|
|
|
|
<Row justifyContent="space-between">
|
|
|
|
<H2>
|
|
|
|
{formatMessage({
|
|
|
|
id: getTrad('Settings.locales.modal.title'),
|
|
|
|
})}
|
|
|
|
</H2>
|
|
|
|
|
|
|
|
<Tabs>
|
|
|
|
<Tab>
|
|
|
|
{formatMessage({
|
|
|
|
id: getTrad('Settings.locales.modal.base'),
|
|
|
|
})}
|
|
|
|
</Tab>
|
|
|
|
<Tab>
|
|
|
|
{formatMessage({
|
|
|
|
id: getTrad('Settings.locales.modal.advanced'),
|
|
|
|
})}
|
|
|
|
</Tab>
|
|
|
|
</Tabs>
|
|
|
|
</Row>
|
|
|
|
|
|
|
|
<Divider />
|
|
|
|
|
|
|
|
<Box paddingTop={7} paddingBottom={7}>
|
|
|
|
<TabPanels>
|
|
|
|
<TabPanel>
|
|
|
|
<BaseForm locale={locale} />
|
|
|
|
</TabPanel>
|
|
|
|
<TabPanel>
|
|
|
|
<AdvancedForm isDefaultLocale={Boolean(locale && locale.isDefault)} />
|
|
|
|
</TabPanel>
|
|
|
|
</TabPanels>
|
|
|
|
</Box>
|
|
|
|
</TabGroup>
|
|
|
|
</ModalBody>
|
2021-02-11 10:35:22 +01:00
|
|
|
|
2021-09-08 17:02:26 +02:00
|
|
|
<ModalFooter
|
|
|
|
startActions={
|
|
|
|
<Button variant="tertiary" onClick={onClose}>
|
2021-02-09 16:55:57 +01:00
|
|
|
{formatMessage({ id: 'app.components.Button.cancel' })}
|
|
|
|
</Button>
|
2021-09-08 17:02:26 +02:00
|
|
|
}
|
|
|
|
endActions={
|
|
|
|
<Button type="submit" startIcon={<CheckIcon />} disabled={isEditing}>
|
|
|
|
{formatMessage({ id: 'app.components.Button.save' })}
|
2021-02-09 16:55:57 +01:00
|
|
|
</Button>
|
2021-09-08 17:02:26 +02:00
|
|
|
}
|
|
|
|
/>
|
|
|
|
</Form>
|
2021-02-09 16:55:57 +01:00
|
|
|
)}
|
|
|
|
</Formik>
|
2021-09-08 17:02:26 +02:00
|
|
|
</ModalLayout>
|
2021-02-02 16:08:16 +01:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-02-08 13:47:43 +01:00
|
|
|
ModalEdit.defaultProps = {
|
2021-09-08 17:02:26 +02:00
|
|
|
locale: undefined,
|
2021-02-08 13:47:43 +01:00
|
|
|
};
|
|
|
|
|
2021-02-02 16:08:16 +01:00
|
|
|
ModalEdit.propTypes = {
|
2021-09-08 17:02:26 +02:00
|
|
|
locale: PropTypes.shape({
|
2021-02-09 16:55:57 +01:00
|
|
|
id: PropTypes.number.isRequired,
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
code: PropTypes.string.isRequired,
|
2021-02-25 14:33:25 +01:00
|
|
|
isDefault: PropTypes.bool.isRequired,
|
2021-02-09 16:55:57 +01:00
|
|
|
}),
|
2021-02-08 13:47:43 +01:00
|
|
|
onClose: PropTypes.func.isRequired,
|
2021-02-02 16:08:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default ModalEdit;
|