2021-02-12 15:34:20 +01:00
|
|
|
import React from 'react';
|
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
import { Modal, ModalFooter, TabPanel } from 'strapi-helper-plugin';
|
|
|
|
|
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-12 15:34:20 +01:00
|
|
|
|
|
|
|
|
const ModalCreate = ({ 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-02-15 14:55:08 +01:00
|
|
|
if (!isOpened) return null;
|
|
|
|
|
|
|
|
|
|
if (isLoading) {
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<p>
|
|
|
|
|
{formatMessage({ id: getTrad('Settings.locales.modal.create.defaultLocales.loading') })}
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const options = (defaultLocales || []).map(locale => ({
|
|
|
|
|
label: locale.code,
|
|
|
|
|
value: locale.name,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
const defaultOption = options[0];
|
|
|
|
|
|
2021-02-12 15:34:20 +01:00
|
|
|
return (
|
|
|
|
|
<Modal isOpen={isOpened} onToggle={onClose}>
|
|
|
|
|
<Formik
|
2021-02-15 14:55:08 +01:00
|
|
|
initialValues={{ code: defaultOption.label, displayName: defaultOption.value }}
|
|
|
|
|
onSubmit={values =>
|
2021-02-16 09:54:31 +01:00
|
|
|
addLocale({ code: values.code, name: values.displayName, isDefault: false }).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>
|
|
|
|
|
<BaseForm options={options} defaultOption={defaultOption} />
|
|
|
|
|
</TabPanel>
|
2021-02-12 15:34:20 +01:00
|
|
|
<TabPanel>advanced</TabPanel>
|
|
|
|
|
</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>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ModalCreate.propTypes = {
|
|
|
|
|
onClose: PropTypes.func.isRequired,
|
|
|
|
|
isOpened: PropTypes.bool.isRequired,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default ModalCreate;
|