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-02 16:08:16 +01:00
|
|
|
import { Modal, ModalHeader, ModalSection, ModalFooter } from 'strapi-helper-plugin';
|
|
|
|
import { useIntl } from 'react-intl';
|
2021-02-09 16:55:57 +01:00
|
|
|
import { Button, Label, InputText } from '@buffetjs/core';
|
|
|
|
import Select from 'react-select';
|
|
|
|
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-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}>
|
|
|
|
<Formik
|
|
|
|
initialValues={{ displayName: localeToEdit ? localeToEdit.name : '' }}
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
validationSchema={object().shape({
|
|
|
|
displayName: string().max(50, 'Settings.locales.modal.edit.locales.displayName.error'),
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{({ values, handleSubmit, handleChange, errors }) => (
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
<ModalHeader
|
|
|
|
headerBreadcrumbs={[formatMessage({ id: getTrad('Settings.list.actions.edit') })]}
|
|
|
|
/>
|
|
|
|
<ModalSection>
|
|
|
|
<div>
|
|
|
|
<span id="locale-code">
|
|
|
|
<Label htmlFor="">
|
|
|
|
{formatMessage({ id: getTrad('Settings.locales.modal.edit.locales.label') })}
|
|
|
|
</Label>
|
|
|
|
</span>
|
|
|
|
|
|
|
|
<Select
|
|
|
|
aria-labelledby="locale-code"
|
|
|
|
options={options}
|
|
|
|
defaultValue={defaultOption}
|
|
|
|
isDisabled
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
<Label htmlFor="displayName">
|
|
|
|
{formatMessage({
|
|
|
|
id: getTrad('Settings.locales.modal.edit.locales.displayName'),
|
|
|
|
})}
|
|
|
|
</Label>
|
|
|
|
<InputText name="displayName" value={values.displayName} onChange={handleChange} />
|
|
|
|
|
|
|
|
{errors.displayName && (
|
|
|
|
<small>
|
|
|
|
{formatMessage({
|
|
|
|
id: getTrad(' Settings.locales.modal.edit.locales.displayName.error'),
|
|
|
|
})}
|
|
|
|
</small>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</ModalSection>
|
|
|
|
<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;
|