2021-03-31 10:04:07 +02:00
|
|
|
import React, { useState } from 'react';
|
2021-02-08 13:47:43 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2021-03-31 10:04:07 +02:00
|
|
|
import { Modal, ModalFooter, TabPanel, useGlobalContext } from 'strapi-helper-plugin';
|
2021-02-02 16:08:16 +01:00
|
|
|
import { useIntl } from 'react-intl';
|
2021-02-11 10:35:22 +01:00
|
|
|
import { Button } from '@buffetjs/core';
|
2021-02-09 16:55:57 +01:00
|
|
|
import { Formik } from 'formik';
|
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-12 15:34:20 +01:00
|
|
|
import SettingsModal from '../SettingsModal';
|
2021-02-02 16:08:16 +01:00
|
|
|
|
2021-02-09 16:55:57 +01:00
|
|
|
const ModalEdit = ({ localeToEdit, onClose, locales }) => {
|
2021-02-08 13:47:43 +01:00
|
|
|
const { isEditing, editLocale } = useEditLocale();
|
2021-03-31 10:04:07 +02:00
|
|
|
const [shouldUpdateMenu, setShouldUpdateMenu] = useState(false);
|
|
|
|
const { updateMenu } = useGlobalContext();
|
2021-02-02 16:08:16 +01:00
|
|
|
const { formatMessage } = useIntl();
|
2021-02-08 13:47:43 +01:00
|
|
|
const isOpened = Boolean(localeToEdit);
|
|
|
|
|
2021-02-25 14:33:25 +01:00
|
|
|
const handleSubmit = ({ displayName, isDefault }) => {
|
2021-02-09 16:55:57 +01:00
|
|
|
const id = localeToEdit.id;
|
|
|
|
const name = displayName || localeToEdit.code;
|
|
|
|
|
2021-03-31 10:04:07 +02:00
|
|
|
return editLocale(id, { name, isDefault })
|
|
|
|
.then(() => {
|
|
|
|
setShouldUpdateMenu(true);
|
|
|
|
})
|
|
|
|
.then(onClose);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleClose = () => {
|
|
|
|
if (shouldUpdateMenu) {
|
|
|
|
updateMenu();
|
|
|
|
}
|
|
|
|
|
|
|
|
setShouldUpdateMenu(false);
|
2021-02-09 16:55:57 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let options = [];
|
|
|
|
let defaultOption;
|
|
|
|
|
|
|
|
if (localeToEdit) {
|
|
|
|
options = locales.map(locale => ({ label: locale.code, value: locale.id }));
|
|
|
|
defaultOption = options.find(option => option.value === localeToEdit.id);
|
|
|
|
}
|
2021-02-02 16:08:16 +01:00
|
|
|
|
|
|
|
return (
|
2021-03-31 10:04:07 +02:00
|
|
|
<Modal isOpen={isOpened} onToggle={onClose} onClosed={handleClose}>
|
2021-02-09 16:55:57 +01:00
|
|
|
<Formik
|
2021-02-25 14:33:25 +01:00
|
|
|
initialValues={{
|
|
|
|
displayName: localeToEdit ? localeToEdit.name : '',
|
|
|
|
isDefault: localeToEdit ? localeToEdit.isDefault : false,
|
|
|
|
}}
|
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-02-11 10:35:22 +01:00
|
|
|
{({ handleSubmit, errors }) => (
|
2021-02-09 16:55:57 +01:00
|
|
|
<form onSubmit={handleSubmit}>
|
2021-02-12 15:34:20 +01:00
|
|
|
<SettingsModal
|
|
|
|
title={formatMessage({
|
|
|
|
id: getTrad('Settings.locales.modal.title'),
|
|
|
|
})}
|
|
|
|
breadCrumb={[formatMessage({ id: getTrad('Settings.list.actions.edit') })]}
|
|
|
|
tabsAriaLabel={formatMessage({
|
|
|
|
id: getTrad('Settings.locales.modal.edit.tab.label'),
|
|
|
|
})}
|
|
|
|
tabsId="i18n-settings-tabs-edit"
|
|
|
|
>
|
|
|
|
<TabPanel>
|
|
|
|
<BaseForm options={options} defaultOption={defaultOption} />
|
|
|
|
</TabPanel>
|
2021-02-25 14:33:25 +01:00
|
|
|
<TabPanel>
|
2021-02-25 15:48:46 +01:00
|
|
|
<AdvancedForm isDefaultLocale={Boolean(localeToEdit && localeToEdit.isDefault)} />
|
2021-02-25 14:33:25 +01:00
|
|
|
</TabPanel>
|
2021-02-12 15:34:20 +01:00
|
|
|
</SettingsModal>
|
2021-02-11 10:35:22 +01:00
|
|
|
|
2021-02-09 16:55:57 +01:00
|
|
|
<ModalFooter>
|
|
|
|
<section>
|
|
|
|
<Button type="button" color="cancel" onClick={onClose}>
|
|
|
|
{formatMessage({ id: 'app.components.Button.cancel' })}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
color="success"
|
|
|
|
type="submit"
|
|
|
|
isLoading={isEditing}
|
|
|
|
disabled={Object.keys(errors).length > 0}
|
|
|
|
>
|
|
|
|
{formatMessage({ id: getTrad('Settings.locales.modal.edit.confirmation') })}
|
|
|
|
</Button>
|
|
|
|
</section>
|
|
|
|
</ModalFooter>
|
|
|
|
</form>
|
|
|
|
)}
|
|
|
|
</Formik>
|
2021-02-02 16:08:16 +01:00
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-02-08 13:47:43 +01:00
|
|
|
ModalEdit.defaultProps = {
|
|
|
|
localeToEdit: undefined,
|
2021-02-09 16:55:57 +01:00
|
|
|
locales: [],
|
2021-02-08 13:47:43 +01:00
|
|
|
};
|
|
|
|
|
2021-02-02 16:08:16 +01:00
|
|
|
ModalEdit.propTypes = {
|
2021-02-09 16:55:57 +01:00
|
|
|
localeToEdit: PropTypes.shape({
|
|
|
|
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-09 16:55:57 +01:00
|
|
|
locales: PropTypes.arrayOf(
|
|
|
|
PropTypes.shape({
|
2021-02-25 14:33:25 +01:00
|
|
|
id: PropTypes.number,
|
|
|
|
name: PropTypes.string,
|
|
|
|
code: PropTypes.string,
|
2021-02-09 16:55:57 +01:00
|
|
|
})
|
|
|
|
),
|
2021-02-02 16:08:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default ModalEdit;
|