2021-03-31 11:18:00 +02:00
|
|
|
import React, { useRef } from 'react';
|
2021-02-12 15:34:20 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2021-05-24 14:46:22 +02:00
|
|
|
import { Modal, ModalFooter, TabPanel, useRBACProvider } from '@strapi/helper-plugin';
|
2021-02-12 15:34:20 +01:00
|
|
|
import { useIntl } from 'react-intl';
|
|
|
|
|
import { Button } from '@buffetjs/core';
|
|
|
|
|
import { Formik } from 'formik';
|
|
|
|
|
import localeFormSchema from '../../schemas';
|
|
|
|
|
import { getTrad } from '../../utils';
|
|
|
|
|
import SettingsModal from '../SettingsModal';
|
2021-02-15 14:55:08 +01:00
|
|
|
import useDefaultLocales from '../../hooks/useDefaultLocales';
|
|
|
|
|
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-03-31 13:14:06 +02:00
|
|
|
const ModalCreate = ({ alreadyUsedLocales, onClose, isOpened }) => {
|
2021-02-15 14:55:08 +01:00
|
|
|
const { defaultLocales, isLoading } = useDefaultLocales();
|
|
|
|
|
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
|
|
|
|
|
|
|
|
const shouldUpdatePermissions = useRef(false);
|
2021-02-15 14:55:08 +01:00
|
|
|
|
|
|
|
|
if (isLoading) {
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<p>
|
|
|
|
|
{formatMessage({ id: getTrad('Settings.locales.modal.create.defaultLocales.loading') })}
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-31 10:16:07 +02:00
|
|
|
const handleClosed = async () => {
|
2021-03-31 11:18:00 +02:00
|
|
|
if (shouldUpdatePermissions.current) {
|
2021-05-24 14:46:22 +02:00
|
|
|
await refetchPermissions();
|
2021-03-31 10:16:07 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-24 15:49:15 +02:00
|
|
|
shouldUpdatePermissions.current = false;
|
2021-03-31 10:16:07 +02:00
|
|
|
};
|
|
|
|
|
|
2021-03-31 13:14:06 +02:00
|
|
|
const options = (defaultLocales || [])
|
|
|
|
|
.map(locale => ({
|
|
|
|
|
label: locale.code,
|
|
|
|
|
value: locale.name,
|
|
|
|
|
}))
|
|
|
|
|
.filter(({ label }) => {
|
|
|
|
|
const foundLocale = alreadyUsedLocales.find(({ code }) => code === label);
|
|
|
|
|
|
|
|
|
|
return !foundLocale;
|
|
|
|
|
});
|
2021-02-15 14:55:08 +01:00
|
|
|
|
|
|
|
|
const defaultOption = options[0];
|
|
|
|
|
|
2021-03-31 11:03:15 +02:00
|
|
|
if (!defaultOption) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-12 15:34:20 +01:00
|
|
|
return (
|
2021-03-31 10:16:07 +02:00
|
|
|
<Modal isOpen={isOpened} onToggle={onClose} withoverflow="true" onClosed={handleClosed}>
|
2021-02-12 15:34:20 +01:00
|
|
|
<Formik
|
2021-02-25 15:29:15 +01:00
|
|
|
initialValues={{
|
|
|
|
|
code: defaultOption.label,
|
|
|
|
|
displayName: defaultOption.value,
|
|
|
|
|
isDefault: false,
|
|
|
|
|
}}
|
2021-02-15 14:55:08 +01:00
|
|
|
onSubmit={values =>
|
2021-02-25 15:29:15 +01:00
|
|
|
addLocale({
|
|
|
|
|
code: values.code,
|
|
|
|
|
name: values.displayName,
|
|
|
|
|
isDefault: values.isDefault,
|
2021-03-31 10:16:07 +02:00
|
|
|
})
|
2021-03-31 11:18:00 +02:00
|
|
|
.then(() => {
|
|
|
|
|
shouldUpdatePermissions.current = true;
|
|
|
|
|
})
|
2021-03-31 10:16:07 +02:00
|
|
|
.then(() => {
|
|
|
|
|
onClose();
|
|
|
|
|
})}
|
2021-02-12 15:34:20 +01:00
|
|
|
validationSchema={localeFormSchema}
|
|
|
|
|
>
|
|
|
|
|
{({ handleSubmit, errors }) => (
|
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
|
<SettingsModal
|
|
|
|
|
title={formatMessage({
|
|
|
|
|
id: getTrad('Settings.locales.modal.title'),
|
|
|
|
|
})}
|
|
|
|
|
breadCrumb={[formatMessage({ id: getTrad('Settings.list.actions.add') })]}
|
|
|
|
|
tabsAriaLabel={formatMessage({
|
|
|
|
|
id: getTrad('Settings.locales.modal.create.tab.label'),
|
|
|
|
|
})}
|
|
|
|
|
tabsId="i18n-settings-tabs-create"
|
|
|
|
|
>
|
2021-02-15 14:55:08 +01:00
|
|
|
<TabPanel>
|
2021-03-31 13:14:06 +02:00
|
|
|
<BaseForm
|
|
|
|
|
options={options}
|
|
|
|
|
defaultOption={defaultOption}
|
|
|
|
|
alreadyUsedLocales={alreadyUsedLocales}
|
|
|
|
|
/>
|
2021-02-15 14:55:08 +01:00
|
|
|
</TabPanel>
|
2021-02-25 15:29:15 +01:00
|
|
|
<TabPanel>
|
|
|
|
|
<AdvancedForm />
|
|
|
|
|
</TabPanel>
|
2021-02-12 15:34:20 +01:00
|
|
|
</SettingsModal>
|
|
|
|
|
|
|
|
|
|
<ModalFooter>
|
|
|
|
|
<section>
|
|
|
|
|
<Button type="button" color="cancel" onClick={onClose}>
|
|
|
|
|
{formatMessage({ id: 'app.components.Button.cancel' })}
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
color="success"
|
|
|
|
|
type="submit"
|
2021-02-15 14:55:08 +01:00
|
|
|
isLoading={isAdding}
|
2021-02-12 15:34:20 +01:00
|
|
|
disabled={Object.keys(errors).length > 0}
|
|
|
|
|
>
|
|
|
|
|
{formatMessage({ id: getTrad('Settings.locales.modal.create.confirmation') })}
|
|
|
|
|
</Button>
|
|
|
|
|
</section>
|
|
|
|
|
</ModalFooter>
|
|
|
|
|
</form>
|
|
|
|
|
)}
|
|
|
|
|
</Formik>
|
|
|
|
|
</Modal>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-31 13:14:06 +02:00
|
|
|
ModalCreate.defaultProps = {
|
|
|
|
|
alreadyUsedLocales: [],
|
|
|
|
|
};
|
|
|
|
|
|
2021-02-12 15:34:20 +01:00
|
|
|
ModalCreate.propTypes = {
|
2021-03-31 13:14:06 +02:00
|
|
|
alreadyUsedLocales: PropTypes.array,
|
2021-02-12 15:34:20 +01:00
|
|
|
onClose: PropTypes.func.isRequired,
|
|
|
|
|
isOpened: PropTypes.bool.isRequired,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default ModalCreate;
|