Chore: Cleanup advanced settings and email templates

This commit is contained in:
Gustav Hansen 2023-08-11 15:54:08 +02:00
parent c696619b98
commit 86a45d30ed
4 changed files with 102 additions and 102 deletions

View File

@ -20,6 +20,8 @@ import {
GenericInput, GenericInput,
LoadingIndicatorPage, LoadingIndicatorPage,
SettingsPageTitle, SettingsPageTitle,
useAPIErrorHandler,
useFetchClient,
useFocusWhenNavigate, useFocusWhenNavigate,
useNotification, useNotification,
useOverlayBlocker, useOverlayBlocker,
@ -33,7 +35,6 @@ import { useMutation, useQuery, useQueryClient } from 'react-query';
import { PERMISSIONS } from '../../constants'; import { PERMISSIONS } from '../../constants';
import { getTrad } from '../../utils'; import { getTrad } from '../../utils';
import { fetchData, putAdvancedSettings } from './utils/api';
import layout from './utils/layout'; import layout from './utils/layout';
import schema from './utils/schema'; import schema from './utils/schema';
@ -49,6 +50,9 @@ const AdvancedSettingsPage = () => {
const { lockApp, unlockApp } = useOverlayBlocker(); const { lockApp, unlockApp } = useOverlayBlocker();
const { notifyStatus } = useNotifyAT(); const { notifyStatus } = useNotifyAT();
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const { get, put } = useFetchClient();
const { formatAPIError } = useAPIErrorHandler();
useFocusWhenNavigate(); useFocusWhenNavigate();
const { const {
@ -56,7 +60,14 @@ const AdvancedSettingsPage = () => {
allowedActions: { canUpdate }, allowedActions: { canUpdate },
} = useRBAC({ update: PERMISSIONS.updateAdvancedSettings }); } = useRBAC({ update: PERMISSIONS.updateAdvancedSettings });
const { status: isLoadingData, data } = useQuery('advanced', () => fetchData(), { const { isLoading: isLoadingData, data } = useQuery(
['users-permissions', 'advanced'],
async () => {
const { data } = await get('/users-permissions/advanced');
return data;
},
{
onSuccess() { onSuccess() {
notifyStatus( notifyStatus(
formatMessage({ formatMessage({
@ -71,13 +82,15 @@ const AdvancedSettingsPage = () => {
message: { id: getTrad('notification.error'), defaultMessage: 'An error occured' }, message: { id: getTrad('notification.error'), defaultMessage: 'An error occured' },
}); });
}, },
}); }
);
const isLoading = isLoadingForPermissions || isLoadingData !== 'success'; const isLoading = isLoadingForPermissions || isLoadingData;
const submitMutation = useMutation((body) => putAdvancedSettings(body), { const submitMutation = useMutation((body) => put('/users-permissions/advanced', body), {
async onSuccess() { async onSuccess() {
await queryClient.invalidateQueries('advanced'); await queryClient.invalidateQueries(['users-permissions', 'advanced']);
toggleNotification({ toggleNotification({
type: 'success', type: 'success',
message: { id: getTrad('notification.success.saved'), defaultMessage: 'Saved' }, message: { id: getTrad('notification.success.saved'), defaultMessage: 'Saved' },
@ -85,11 +98,12 @@ const AdvancedSettingsPage = () => {
unlockApp(); unlockApp();
}, },
onError() { onError(error) {
toggleNotification({ toggleNotification({
type: 'warning', type: 'warning',
message: { id: getTrad('notification.error'), defaultMessage: 'An error occured' }, message: formatAPIError(error),
}); });
unlockApp(); unlockApp();
}, },
refetchActive: true, refetchActive: true,
@ -100,9 +114,12 @@ const AdvancedSettingsPage = () => {
const handleSubmit = async (body) => { const handleSubmit = async (body) => {
lockApp(); lockApp();
const urlConfirmation = body.email_confirmation ? body.email_confirmation_redirection : ''; submitMutation.mutate({
...body,
await submitMutation.mutateAsync({ ...body, email_confirmation_redirection: urlConfirmation }); email_confirmation_redirection: body.email_confirmation
? body.email_confirmation_redirection
: '',
});
}; };
if (isLoading) { if (isLoading) {

View File

@ -1,16 +0,0 @@
import { getFetchClient } from '@strapi/helper-plugin';
const fetchData = async () => {
const { get } = getFetchClient();
const { data } = await get('/users-permissions/advanced');
return data;
};
const putAdvancedSettings = (body) => {
const { put } = getFetchClient();
return put('/users-permissions/advanced', body);
};
export { fetchData, putAdvancedSettings };

View File

@ -1,10 +1,12 @@
import React, { useRef, useState } from 'react'; import * as React from 'react';
import { ContentLayout, HeaderLayout, Main, useNotifyAT } from '@strapi/design-system'; import { ContentLayout, HeaderLayout, Main, useNotifyAT } from '@strapi/design-system';
import { import {
CheckPagePermissions, CheckPagePermissions,
LoadingIndicatorPage, LoadingIndicatorPage,
SettingsPageTitle, SettingsPageTitle,
useAPIErrorHandler,
useFetchClient,
useFocusWhenNavigate, useFocusWhenNavigate,
useNotification, useNotification,
useOverlayBlocker, useOverlayBlocker,
@ -19,7 +21,6 @@ import { getTrad } from '../../utils';
import EmailForm from './components/EmailForm'; import EmailForm from './components/EmailForm';
import EmailTable from './components/EmailTable'; import EmailTable from './components/EmailTable';
import { fetchData, putEmailTemplate } from './utils/api';
const ProtectedEmailTemplatesPage = () => ( const ProtectedEmailTemplatesPage = () => (
<CheckPagePermissions permissions={PERMISSIONS.readEmailTemplates}> <CheckPagePermissions permissions={PERMISSIONS.readEmailTemplates}>
@ -33,19 +34,28 @@ const EmailTemplatesPage = () => {
const { notifyStatus } = useNotifyAT(); const { notifyStatus } = useNotifyAT();
const toggleNotification = useNotification(); const toggleNotification = useNotification();
const { lockApp, unlockApp } = useOverlayBlocker(); const { lockApp, unlockApp } = useOverlayBlocker();
const trackUsageRef = useRef(trackUsage);
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const { get, put } = useFetchClient();
const { formatAPIError } = useAPIErrorHandler();
useFocusWhenNavigate(); useFocusWhenNavigate();
const [isModalOpen, setIsModalOpen] = useState(false); const [isModalOpen, setIsModalOpen] = React.useState(false);
const [templateToEdit, setTemplateToEdit] = useState(null); const [templateToEdit, setTemplateToEdit] = React.useState(null);
const { const {
isLoading: isLoadingForPermissions, isLoading: isLoadingForPermissions,
allowedActions: { canUpdate }, allowedActions: { canUpdate },
} = useRBAC({ update: PERMISSIONS.updateEmailTemplates }); } = useRBAC({ update: PERMISSIONS.updateEmailTemplates });
const { status: isLoadingData, data } = useQuery('email-templates', () => fetchData(), { const { isLoading: isLoadingData, data } = useQuery(
['users-permissions', 'email-templates'],
async () => {
const { data } = await get('/users-permissions/email-templates');
return data;
},
{
onSuccess() { onSuccess() {
notifyStatus( notifyStatus(
formatMessage({ formatMessage({
@ -54,15 +64,16 @@ const EmailTemplatesPage = () => {
}) })
); );
}, },
onError() { onError(error) {
toggleNotification({ toggleNotification({
type: 'warning', type: 'warning',
message: { id: 'notification.error', defaultMessage: 'An error occured' }, message: formatAPIError(error),
}); });
}, },
}); }
);
const isLoading = isLoadingForPermissions || isLoadingData !== 'success'; const isLoading = isLoadingForPermissions || isLoadingData;
const handleToggle = () => { const handleToggle = () => {
setIsModalOpen((prev) => !prev); setIsModalOpen((prev) => !prev);
@ -73,34 +84,38 @@ const EmailTemplatesPage = () => {
handleToggle(); handleToggle();
}; };
const submitMutation = useMutation((body) => putEmailTemplate({ 'email-templates': body }), { const submitMutation = useMutation(
(body) => put('/users-permissions/email-templates', { 'email-templates': body }),
{
async onSuccess() { async onSuccess() {
await queryClient.invalidateQueries('email-templates'); await queryClient.invalidateQueries(['users-permissions', 'email-templates']);
toggleNotification({ toggleNotification({
type: 'success', type: 'success',
message: { id: 'notification.success.saved', defaultMessage: 'Saved' }, message: { id: 'notification.success.saved', defaultMessage: 'Saved' },
}); });
trackUsageRef.current('didEditEmailTemplates'); trackUsage('didEditEmailTemplates');
unlockApp(); unlockApp();
handleToggle(); handleToggle();
}, },
onError() { onError(error) {
toggleNotification({ toggleNotification({
type: 'warning', type: 'warning',
message: { id: 'notification.error', defaultMessage: 'An error occured' }, message: formatAPIError(error),
}); });
unlockApp(); unlockApp();
}, },
refetchActive: true, refetchActive: true,
}); }
const { isLoading: isSubmittingForm } = submitMutation; );
const handleSubmit = (body) => { const handleSubmit = (body) => {
lockApp(); lockApp();
trackUsageRef.current('willEditEmailTemplates');
trackUsage('willEditEmailTemplates');
const editedTemplates = { ...data, [templateToEdit]: body }; const editedTemplates = { ...data, [templateToEdit]: body };
submitMutation.mutate(editedTemplates); submitMutation.mutate(editedTemplates);
@ -129,7 +144,7 @@ const EmailTemplatesPage = () => {
} }
return ( return (
<Main aria-busy={isSubmittingForm}> <Main aria-busy={submitMutation.isLoading}>
<SettingsPageTitle <SettingsPageTitle
name={formatMessage({ name={formatMessage({
id: getTrad('HeaderNav.link.emailTemplates'), id: getTrad('HeaderNav.link.emailTemplates'),

View File

@ -1,16 +0,0 @@
import { getFetchClient } from '@strapi/helper-plugin';
const fetchData = async () => {
const { get } = getFetchClient();
const { data } = await get('/users-permissions/email-templates');
return data;
};
const putEmailTemplate = (body) => {
const { put } = getFetchClient();
return put('/users-permissions/email-templates', body);
};
export { fetchData, putEmailTemplate };