2021-09-08 09:14:31 +02:00
|
|
|
import React from 'react';
|
2021-02-12 15:34:20 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2021-09-08 09:14:31 +02:00
|
|
|
import { useRBACProvider, Form } from '@strapi/helper-plugin';
|
|
|
|
|
import { ModalLayout, ModalHeader, ModalBody, ModalFooter } from '@strapi/parts/ModalLayout';
|
|
|
|
|
import { TabGroup, Tabs, Tab, TabPanels, TabPanel } from '@strapi/parts/Tabs';
|
|
|
|
|
import { Button } from '@strapi/parts/Button';
|
|
|
|
|
import { ButtonText, H2 } from '@strapi/parts/Text';
|
|
|
|
|
import { Divider } from '@strapi/parts/Divider';
|
|
|
|
|
import { Box } from '@strapi/parts/Box';
|
|
|
|
|
import { Row } from '@strapi/parts/Row';
|
|
|
|
|
import CheckIcon from '@strapi/icons/CheckIcon';
|
2021-02-12 15:34:20 +01:00
|
|
|
import { useIntl } from 'react-intl';
|
|
|
|
|
import { Formik } from 'formik';
|
|
|
|
|
import localeFormSchema from '../../schemas';
|
|
|
|
|
import { getTrad } from '../../utils';
|
2021-02-15 14:55:08 +01:00
|
|
|
import useAddLocale from '../../hooks/useAddLocale';
|
|
|
|
|
import BaseForm from './BaseForm';
|
2021-02-25 15:29:15 +01:00
|
|
|
import AdvancedForm from './AdvancedForm';
|
2021-02-12 15:34:20 +01:00
|
|
|
|
2021-09-08 09:14:31 +02:00
|
|
|
const initialFormValues = {
|
|
|
|
|
code: '',
|
|
|
|
|
displayName: '',
|
|
|
|
|
isDefault: false,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const ModalCreate = ({ onClose }) => {
|
2021-02-15 14:55:08 +01:00
|
|
|
const { isAdding, addLocale } = useAddLocale();
|
2021-02-12 15:34:20 +01:00
|
|
|
const { formatMessage } = useIntl();
|
2021-05-24 14:46:22 +02:00
|
|
|
const { refetchPermissions } = useRBACProvider();
|
2021-03-31 11:18:00 +02:00
|
|
|
|
2021-09-08 09:14:31 +02:00
|
|
|
/**
|
|
|
|
|
* No need to explicitly call the onClose prop here
|
|
|
|
|
* since the all tree (from the root of the page) is destroyed and re-mounted
|
|
|
|
|
* because of the RBAC refreshing and the potential move of the default locale
|
|
|
|
|
*/
|
|
|
|
|
const handleLocaleAdd = async values => {
|
|
|
|
|
await addLocale({
|
|
|
|
|
code: values.code,
|
|
|
|
|
name: values.displayName,
|
|
|
|
|
isDefault: values.isDefault,
|
2021-03-31 13:14:06 +02:00
|
|
|
});
|
2021-02-15 14:55:08 +01:00
|
|
|
|
2021-09-08 09:14:31 +02:00
|
|
|
await refetchPermissions();
|
|
|
|
|
};
|
2021-03-31 11:03:15 +02:00
|
|
|
|
2021-02-12 15:34:20 +01:00
|
|
|
return (
|
2021-09-08 09:14:31 +02:00
|
|
|
<ModalLayout onClose={onClose} labelledBy="add-locale-title">
|
2021-02-12 15:34:20 +01:00
|
|
|
<Formik
|
2021-09-08 09:14:31 +02:00
|
|
|
initialValues={initialFormValues}
|
|
|
|
|
onSubmit={handleLocaleAdd}
|
2021-02-12 15:34:20 +01:00
|
|
|
validationSchema={localeFormSchema}
|
2021-09-08 09:14:31 +02:00
|
|
|
validateOnChange={false}
|
2021-02-12 15:34:20 +01:00
|
|
|
>
|
2021-09-09 10:26:41 +02:00
|
|
|
<Form>
|
|
|
|
|
<ModalHeader>
|
|
|
|
|
<ButtonText textColor="neutral800" as="h2" id="add-locale-title">
|
|
|
|
|
{formatMessage({ id: getTrad('Settings.list.actions.add') })}
|
|
|
|
|
</ButtonText>
|
|
|
|
|
</ModalHeader>
|
|
|
|
|
<ModalBody>
|
|
|
|
|
<TabGroup
|
|
|
|
|
label={formatMessage({
|
|
|
|
|
id: getTrad('Settings.locales.modal.title'),
|
2021-09-09 11:14:14 +02:00
|
|
|
defaultMessage: 'Configurations',
|
2021-09-09 10:26:41 +02:00
|
|
|
})}
|
|
|
|
|
id="tabs"
|
|
|
|
|
variant="simple"
|
|
|
|
|
>
|
|
|
|
|
<Row justifyContent="space-between">
|
|
|
|
|
<H2>
|
|
|
|
|
{formatMessage({
|
|
|
|
|
id: getTrad('Settings.locales.modal.title'),
|
2021-09-09 11:14:14 +02:00
|
|
|
defaultMessage: 'Configurations',
|
2021-09-09 10:26:41 +02:00
|
|
|
})}
|
|
|
|
|
</H2>
|
|
|
|
|
<Tabs>
|
|
|
|
|
<Tab>
|
2021-09-08 09:14:31 +02:00
|
|
|
{formatMessage({
|
2021-09-09 10:26:41 +02:00
|
|
|
id: getTrad('Settings.locales.modal.base'),
|
2021-09-09 11:14:14 +02:00
|
|
|
defaultMessage: 'Base settings',
|
2021-09-08 09:14:31 +02:00
|
|
|
})}
|
2021-09-09 10:26:41 +02:00
|
|
|
</Tab>
|
|
|
|
|
<Tab>
|
|
|
|
|
{formatMessage({
|
|
|
|
|
id: getTrad('Settings.locales.modal.advanced'),
|
2021-09-09 11:14:14 +02:00
|
|
|
defaultMessage: 'Advanced settings',
|
2021-09-09 10:26:41 +02:00
|
|
|
})}
|
|
|
|
|
</Tab>
|
|
|
|
|
</Tabs>
|
|
|
|
|
</Row>
|
2021-02-12 15:34:20 +01:00
|
|
|
|
2021-09-09 10:26:41 +02:00
|
|
|
<Divider />
|
2021-09-08 09:14:31 +02:00
|
|
|
|
2021-09-09 10:26:41 +02:00
|
|
|
<Box paddingTop={7} paddingBottom={7}>
|
|
|
|
|
<TabPanels>
|
|
|
|
|
<TabPanel>
|
|
|
|
|
<BaseForm />
|
|
|
|
|
</TabPanel>
|
|
|
|
|
<TabPanel>
|
|
|
|
|
<AdvancedForm />
|
|
|
|
|
</TabPanel>
|
|
|
|
|
</TabPanels>
|
|
|
|
|
</Box>
|
|
|
|
|
</TabGroup>
|
|
|
|
|
</ModalBody>
|
|
|
|
|
<ModalFooter
|
|
|
|
|
startActions={
|
|
|
|
|
<Button variant="tertiary" onClick={onClose}>
|
2021-09-09 11:14:14 +02:00
|
|
|
{formatMessage({ id: 'app.components.Button.cancel', defaultMessage: 'Cancel' })}
|
2021-09-09 10:26:41 +02:00
|
|
|
</Button>
|
|
|
|
|
}
|
|
|
|
|
endActions={
|
|
|
|
|
<Button type="submit" startIcon={<CheckIcon />} disabled={isAdding}>
|
2021-09-09 11:14:14 +02:00
|
|
|
{formatMessage({ id: 'app.components.Button.save', defaultMessage: 'Save' })}
|
2021-09-09 10:26:41 +02:00
|
|
|
</Button>
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
</Form>
|
2021-02-12 15:34:20 +01:00
|
|
|
</Formik>
|
2021-09-08 09:14:31 +02:00
|
|
|
</ModalLayout>
|
2021-02-12 15:34:20 +01:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ModalCreate.propTypes = {
|
|
|
|
|
onClose: PropTypes.func.isRequired,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default ModalCreate;
|