2021-02-02 16:08:16 +01:00
|
|
|
import React from 'react';
|
2021-02-08 13:47:43 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2021-02-11 10:35:22 +01:00
|
|
|
import {
|
|
|
|
Modal,
|
|
|
|
ModalHeader,
|
|
|
|
HeaderModal,
|
|
|
|
HeaderModalTitle,
|
|
|
|
ModalFooter,
|
|
|
|
ModalForm,
|
|
|
|
Tabs,
|
|
|
|
TabsNav,
|
|
|
|
Tab,
|
|
|
|
TabsPanel,
|
|
|
|
TabPanel,
|
|
|
|
} 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';
|
|
|
|
import { object, string } from 'yup';
|
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-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-02-02 16:08:16 +01:00
|
|
|
const { formatMessage } = useIntl();
|
2021-02-08 13:47:43 +01:00
|
|
|
const isOpened = Boolean(localeToEdit);
|
|
|
|
|
2021-02-09 16:55:57 +01:00
|
|
|
const handleSubmit = ({ displayName }) => {
|
|
|
|
const id = localeToEdit.id;
|
|
|
|
const name = displayName || localeToEdit.code;
|
|
|
|
|
|
|
|
return editLocale(id, name).then(onClose);
|
|
|
|
};
|
|
|
|
|
|
|
|
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-02-09 16:55:57 +01:00
|
|
|
<Modal isOpen={isOpened} onToggle={onClose}>
|
2021-02-11 10:35:22 +01:00
|
|
|
<HeaderModal>
|
|
|
|
<ModalHeader
|
|
|
|
headerBreadcrumbs={[formatMessage({ id: getTrad('Settings.list.actions.edit') })]}
|
|
|
|
/>
|
|
|
|
</HeaderModal>
|
|
|
|
|
2021-02-09 16:55:57 +01:00
|
|
|
<Formik
|
|
|
|
initialValues={{ displayName: localeToEdit ? localeToEdit.name : '' }}
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
validationSchema={object().shape({
|
|
|
|
displayName: string().max(50, 'Settings.locales.modal.edit.locales.displayName.error'),
|
|
|
|
})}
|
|
|
|
>
|
2021-02-11 10:35:22 +01:00
|
|
|
{({ handleSubmit, errors }) => (
|
2021-02-09 16:55:57 +01:00
|
|
|
<form onSubmit={handleSubmit}>
|
2021-02-11 10:35:22 +01:00
|
|
|
<div className="container-fluid">
|
|
|
|
<div className="container-fluid">
|
|
|
|
<HeaderModalTitle
|
|
|
|
style={{
|
|
|
|
fontSize: '1.8rem',
|
|
|
|
height: '65px',
|
|
|
|
fontWeight: 'bold',
|
|
|
|
alignItems: 'center',
|
|
|
|
marginBottom: '-39px',
|
|
|
|
paddingTop: '16px',
|
|
|
|
}}
|
|
|
|
>
|
2021-02-09 16:55:57 +01:00
|
|
|
{formatMessage({
|
2021-02-11 10:35:22 +01:00
|
|
|
id: getTrad('Settings.locales.modal.title'),
|
2021-02-09 16:55:57 +01:00
|
|
|
})}
|
2021-02-11 10:35:22 +01:00
|
|
|
</HeaderModalTitle>
|
2021-02-09 16:55:57 +01:00
|
|
|
|
2021-02-11 10:35:22 +01:00
|
|
|
<ModalForm>
|
|
|
|
<TabsNav
|
|
|
|
defaultSelection={0}
|
|
|
|
label={formatMessage({
|
|
|
|
id: getTrad('Settings.locales.modal.edit.tab.label'),
|
2021-02-09 16:55:57 +01:00
|
|
|
})}
|
2021-02-11 10:35:22 +01:00
|
|
|
id="i18n-settings-tabs"
|
|
|
|
>
|
|
|
|
<Tabs position="right">
|
|
|
|
<Tab>{formatMessage({ id: getTrad('Settings.locales.modal.base') })}</Tab>
|
|
|
|
<Tab>{formatMessage({ id: getTrad('Settings.locales.modal.advanced') })}</Tab>
|
|
|
|
</Tabs>
|
|
|
|
|
|
|
|
<TabsPanel>
|
|
|
|
<TabPanel>
|
|
|
|
<BaseForm options={options} defaultOption={defaultOption} />
|
|
|
|
</TabPanel>
|
|
|
|
<TabPanel>advanced</TabPanel>
|
|
|
|
</TabsPanel>
|
|
|
|
</TabsNav>
|
|
|
|
</ModalForm>
|
2021-02-09 16:55:57 +01:00
|
|
|
</div>
|
2021-02-11 10:35:22 +01:00
|
|
|
</div>
|
|
|
|
|
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-08 13:47:43 +01:00
|
|
|
onClose: PropTypes.func.isRequired,
|
2021-02-09 16:55:57 +01:00
|
|
|
locales: PropTypes.arrayOf(
|
|
|
|
PropTypes.shape({
|
|
|
|
id: PropTypes.number.isRequired,
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
code: PropTypes.string.isRequired,
|
|
|
|
})
|
|
|
|
),
|
2021-02-02 16:08:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default ModalEdit;
|