2020-08-07 09:28:29 +02:00
|
|
|
import React, { useCallback, useMemo, useRef, useState } from 'react';
|
2020-08-04 14:33:43 +02:00
|
|
|
import { useIntl } from 'react-intl';
|
2020-08-04 16:35:16 +02:00
|
|
|
import { Header, List } from '@buffetjs/custom';
|
|
|
|
import { Pencil } from '@buffetjs/icons';
|
2020-08-06 13:29:00 +02:00
|
|
|
import { get } from 'lodash';
|
2020-08-07 09:48:28 +02:00
|
|
|
import {
|
|
|
|
SettingsPageTitle,
|
|
|
|
SizedInput,
|
|
|
|
useGlobalContext,
|
|
|
|
request,
|
|
|
|
getYupInnerErrors,
|
|
|
|
} from 'strapi-helper-plugin';
|
2020-08-06 13:29:00 +02:00
|
|
|
import { Row } from 'reactstrap';
|
2020-08-04 16:35:16 +02:00
|
|
|
import pluginPermissions from '../../permissions';
|
2020-08-07 09:28:29 +02:00
|
|
|
import { useForm } from '../../hooks';
|
2020-08-04 16:35:16 +02:00
|
|
|
import ListBaselineAlignment from '../../components/ListBaselineAlignment';
|
|
|
|
import ListRow from '../../components/ListRow';
|
2020-08-06 18:40:22 +02:00
|
|
|
import ModalForm from '../../components/ModalForm';
|
2020-08-04 16:35:16 +02:00
|
|
|
import { getRequestURL, getTrad } from '../../utils';
|
2020-08-06 13:29:00 +02:00
|
|
|
import forms from './utils/forms';
|
2020-08-06 16:41:10 +02:00
|
|
|
import schema from './utils/schema';
|
2020-08-03 16:49:40 +02:00
|
|
|
|
|
|
|
const EmailTemplatesPage = () => {
|
2020-08-04 14:33:43 +02:00
|
|
|
const { formatMessage } = useIntl();
|
2020-08-07 09:48:28 +02:00
|
|
|
const { emitEvent } = useGlobalContext();
|
|
|
|
const emitEventRef = useRef(emitEvent);
|
2020-08-06 13:29:00 +02:00
|
|
|
const buttonSubmitRef = useRef(null);
|
2020-08-04 14:33:43 +02:00
|
|
|
const pageTitle = formatMessage({ id: getTrad('HeaderNav.link.emailTemplates') });
|
2020-08-04 16:35:16 +02:00
|
|
|
const updatePermissions = useMemo(() => {
|
2020-08-04 18:10:35 +02:00
|
|
|
return { update: pluginPermissions.updateEmailTemplates };
|
2020-08-04 16:35:16 +02:00
|
|
|
}, []);
|
2020-08-06 13:29:00 +02:00
|
|
|
const [isOpen, setIsOpen] = useState(false);
|
2020-08-06 16:41:10 +02:00
|
|
|
const [isSubmiting, setIsSubmiting] = useState(false);
|
2020-08-06 13:29:00 +02:00
|
|
|
const [showForm, setShowForm] = useState(false);
|
|
|
|
const [templateToEdit, setTemplateToEdit] = useState(null);
|
2020-08-07 09:28:29 +02:00
|
|
|
|
|
|
|
const {
|
|
|
|
allowedActions: { canUpdate },
|
|
|
|
dispatchResetForm,
|
|
|
|
dispatchSetFormErrors,
|
|
|
|
dispatchSubmitSucceeded,
|
|
|
|
formErrors,
|
|
|
|
handleChange,
|
|
|
|
isLoading,
|
|
|
|
isLoadingForPermissions,
|
|
|
|
modifiedData,
|
|
|
|
} = useForm('email-templates', updatePermissions);
|
|
|
|
|
2020-08-04 16:35:16 +02:00
|
|
|
const emailTemplates = useMemo(() => {
|
|
|
|
return Object.keys(modifiedData).reduce((acc, current) => {
|
|
|
|
const { display, icon } = modifiedData[current];
|
|
|
|
|
2020-08-06 13:29:00 +02:00
|
|
|
acc.push({
|
|
|
|
id: current,
|
|
|
|
name: formatMessage({ id: getTrad(display) }),
|
|
|
|
icon: ['fas', icon],
|
|
|
|
});
|
2020-08-04 16:35:16 +02:00
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, []);
|
|
|
|
}, [modifiedData, formatMessage]);
|
|
|
|
|
|
|
|
const listTitle = useMemo(() => {
|
|
|
|
const count = emailTemplates.length;
|
|
|
|
|
|
|
|
return formatMessage(
|
|
|
|
{
|
|
|
|
id: getTrad(`List.title.emailTemplates.${count > 1 ? 'plural' : 'singular'}`),
|
|
|
|
},
|
|
|
|
{ number: count }
|
|
|
|
);
|
|
|
|
}, [emailTemplates.length, formatMessage]);
|
|
|
|
|
2020-08-06 13:29:00 +02:00
|
|
|
const handleClosed = useCallback(() => {
|
|
|
|
setTemplateToEdit(null);
|
|
|
|
setShowForm(false);
|
2020-08-07 09:28:29 +02:00
|
|
|
dispatchResetForm();
|
|
|
|
}, [dispatchResetForm]);
|
2020-08-06 13:29:00 +02:00
|
|
|
|
|
|
|
const handleToggle = useCallback(() => {
|
|
|
|
setIsOpen(prev => !prev);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const handleClickEdit = useCallback(
|
|
|
|
template => {
|
|
|
|
setTemplateToEdit(template);
|
|
|
|
handleToggle();
|
|
|
|
},
|
|
|
|
[handleToggle]
|
|
|
|
);
|
|
|
|
|
2020-08-06 16:41:10 +02:00
|
|
|
const handleSubmit = useCallback(
|
|
|
|
async e => {
|
|
|
|
e.preventDefault();
|
2020-08-06 13:29:00 +02:00
|
|
|
|
2020-08-06 16:41:10 +02:00
|
|
|
let errors = {};
|
|
|
|
|
|
|
|
try {
|
|
|
|
setIsSubmiting(true);
|
|
|
|
await schema.validate(modifiedData[templateToEdit.id], { abortEarly: false });
|
|
|
|
|
2020-08-07 09:28:29 +02:00
|
|
|
strapi.lockAppWithOverlay();
|
|
|
|
|
2020-08-06 16:41:10 +02:00
|
|
|
try {
|
2020-08-07 09:48:28 +02:00
|
|
|
emitEventRef.current('willEditEmailTemplates');
|
|
|
|
|
2020-08-06 16:41:10 +02:00
|
|
|
await request(getRequestURL('email-templates'), {
|
|
|
|
method: 'PUT',
|
|
|
|
body: { 'email-templates': modifiedData },
|
|
|
|
});
|
|
|
|
|
2020-08-07 09:48:28 +02:00
|
|
|
emitEventRef.current('didEditEmailTemplates');
|
|
|
|
|
2020-10-02 18:58:15 +02:00
|
|
|
strapi.notification.toggle({
|
|
|
|
type: 'success',
|
|
|
|
message: { id: getTrad('notification.success.submit') },
|
|
|
|
});
|
2020-08-06 16:41:10 +02:00
|
|
|
|
2020-08-07 09:28:29 +02:00
|
|
|
dispatchSubmitSucceeded();
|
2020-08-06 16:41:10 +02:00
|
|
|
|
|
|
|
handleToggle();
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
|
2020-10-02 18:58:15 +02:00
|
|
|
strapi.notification.toggle({
|
|
|
|
type: 'warning',
|
|
|
|
message: { id: 'notification.error' },
|
|
|
|
});
|
2020-08-06 16:41:10 +02:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
errors = getYupInnerErrors(err);
|
|
|
|
} finally {
|
|
|
|
setIsSubmiting(false);
|
2020-08-07 09:28:29 +02:00
|
|
|
strapi.unlockApp();
|
2020-08-06 16:41:10 +02:00
|
|
|
}
|
|
|
|
|
2020-08-07 09:28:29 +02:00
|
|
|
dispatchSetFormErrors(errors);
|
2020-08-06 16:41:10 +02:00
|
|
|
},
|
2020-08-07 09:28:29 +02:00
|
|
|
[dispatchSetFormErrors, dispatchSubmitSucceeded, modifiedData, templateToEdit, handleToggle]
|
2020-08-06 16:41:10 +02:00
|
|
|
);
|
2020-08-06 13:29:00 +02:00
|
|
|
|
|
|
|
const handleClick = useCallback(() => {
|
|
|
|
buttonSubmitRef.current.click();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const handleOpened = useCallback(() => {
|
|
|
|
setShowForm(true);
|
|
|
|
}, []);
|
|
|
|
|
2020-08-04 14:33:43 +02:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<SettingsPageTitle name={pageTitle} />
|
2020-08-04 15:59:19 +02:00
|
|
|
<div>
|
2020-08-04 17:48:23 +02:00
|
|
|
<Header title={{ label: pageTitle }} isLoading={isLoadingForPermissions || isLoading} />
|
2020-08-04 16:35:16 +02:00
|
|
|
<ListBaselineAlignment />
|
|
|
|
<List
|
|
|
|
title={listTitle}
|
|
|
|
items={emailTemplates}
|
2020-08-04 17:48:23 +02:00
|
|
|
isLoading={isLoadingForPermissions || isLoading}
|
2020-08-06 13:29:00 +02:00
|
|
|
customRowComponent={template => (
|
2020-08-04 16:35:16 +02:00
|
|
|
<ListRow
|
2020-08-06 13:29:00 +02:00
|
|
|
{...template}
|
2020-08-04 16:35:16 +02:00
|
|
|
onClick={() => {
|
2020-08-06 13:29:00 +02:00
|
|
|
if (canUpdate) {
|
|
|
|
handleClickEdit(template);
|
|
|
|
}
|
2020-08-04 16:35:16 +02:00
|
|
|
}}
|
|
|
|
links={[
|
|
|
|
{
|
|
|
|
icon: canUpdate ? <Pencil fill="#0e1622" /> : null,
|
2020-08-06 16:27:51 +02:00
|
|
|
onClick: e => {
|
|
|
|
e.stopPropagation();
|
2020-08-06 13:29:00 +02:00
|
|
|
handleClickEdit(template);
|
2020-08-04 16:35:16 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
2020-08-04 15:59:19 +02:00
|
|
|
</div>
|
2020-08-06 18:40:22 +02:00
|
|
|
<ModalForm
|
2020-08-06 13:29:00 +02:00
|
|
|
isOpen={isOpen}
|
|
|
|
onOpened={handleOpened}
|
|
|
|
onToggle={handleToggle}
|
|
|
|
onClosed={handleClosed}
|
2020-08-06 18:40:22 +02:00
|
|
|
headerBreadcrumbs={[
|
|
|
|
getTrad('PopUpForm.header.edit.email-templates'),
|
|
|
|
get(templateToEdit, 'name', ''),
|
|
|
|
]}
|
|
|
|
onClick={handleClick}
|
|
|
|
onCancel={handleToggle}
|
|
|
|
isLoading={isSubmiting}
|
2020-08-06 13:29:00 +02:00
|
|
|
>
|
2020-08-06 18:40:22 +02:00
|
|
|
{showForm && (
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
<Row>
|
|
|
|
{forms.map(input => {
|
|
|
|
const id = get(templateToEdit, 'id');
|
|
|
|
|
|
|
|
return (
|
|
|
|
<SizedInput
|
|
|
|
key={input.name}
|
|
|
|
{...input}
|
|
|
|
error={formErrors[input.name]}
|
|
|
|
name={`${id}.${input.name}`}
|
|
|
|
onChange={handleChange}
|
|
|
|
value={get(modifiedData, [id, ...input.name.split('.')], '')}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</Row>
|
|
|
|
<button type="submit" style={{ display: 'none' }} ref={buttonSubmitRef}>
|
|
|
|
hidden button to use the native form event
|
|
|
|
</button>
|
|
|
|
</form>
|
|
|
|
)}
|
|
|
|
</ModalForm>
|
2020-08-04 14:33:43 +02:00
|
|
|
</>
|
|
|
|
);
|
2020-08-03 16:49:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default EmailTemplatesPage;
|