[i18n] delete a locale (#9404)

This commit is contained in:
Marvin Frachet 2021-02-16 12:00:05 +01:00 committed by GitHub
parent f72f8ec2b9
commit e5edd0a1c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 95 additions and 22 deletions

View File

@ -11,7 +11,7 @@ const ModalDelete = ({ localeToDelete, onClose }) => {
const { formatMessage } = useIntl(); const { formatMessage } = useIntl();
const isOpened = Boolean(localeToDelete); const isOpened = Boolean(localeToDelete);
const handleDelete = () => deleteLocale(localeToDelete).then(onClose); const handleDelete = () => deleteLocale(localeToDelete.id).then(onClose);
return ( return (
<ModalConfirm <ModalConfirm
@ -40,7 +40,9 @@ ModalDelete.defaultProps = {
}; };
ModalDelete.propTypes = { ModalDelete.propTypes = {
localeToDelete: PropTypes.shape({}), localeToDelete: PropTypes.shape({
id: PropTypes.number.isRequired,
}),
onClose: PropTypes.func.isRequired, onClose: PropTypes.func.isRequired,
}; };

View File

@ -130,6 +130,25 @@ describe('i18n settings page', () => {
describe('delete', () => { describe('delete', () => {
it('removes the locale when clicking the confirmation button', async () => { it('removes the locale when clicking the confirmation button', async () => {
request.mockImplementation((_, opts) =>
opts.method === 'DELETE'
? Promise.resolve({ id: 1 })
: Promise.resolve([
{
id: 1,
name: 'French',
code: 'fr-FR',
isDefault: false,
},
{
id: 2,
name: 'English',
code: 'en-US',
isDefault: true,
},
])
);
render( render(
<TestWrapper> <TestWrapper>
<LocaleSettingsPage /> <LocaleSettingsPage />
@ -149,6 +168,46 @@ describe('i18n settings page', () => {
}) })
); );
}); });
it('shows an error when something went wrong when deleting', async () => {
request.mockImplementation((_, opts) =>
opts.method === 'DELETE'
? Promise.reject(new Error('An error'))
: Promise.resolve([
{
id: 1,
name: 'French',
code: 'fr-FR',
isDefault: false,
},
{
id: 2,
name: 'English',
code: 'en-US',
isDefault: true,
},
])
);
render(
<TestWrapper>
<LocaleSettingsPage />
</TestWrapper>
);
const row = await waitFor(() => screen.getByText('French').closest('tr'));
const rowUtils = within(row);
fireEvent.click(rowUtils.getByLabelText('Settings.list.actions.delete'));
fireEvent.click(screen.getByText('Confirm'));
await waitFor(() =>
expect(strapi.notification.toggle).toBeCalledWith({
type: 'warning',
message: { id: 'notification.error' },
})
);
});
}); });
describe('edit', () => { describe('edit', () => {

View File

@ -1,28 +1,40 @@
import { useState } from 'react'; import { request } from 'strapi-helper-plugin';
import { useMutation, useQueryClient } from 'react-query';
import { getTrad } from '../../utils'; import { getTrad } from '../../utils';
const useDeleteLocale = () => { const deleteLocale = async id => {
const [isDeleting, setIsDeleting] = useState(false); try {
const data = await request(`/i18n/locales/${id}`, {
const deleteLocale = localeToDelete => { method: 'DELETE',
console.log(`About to delete`, localeToDelete); });
setIsDeleting(true);
return new Promise(resolve =>
setTimeout(() => {
setIsDeleting(false);
strapi.notification.toggle({ strapi.notification.toggle({
type: 'success', type: 'success',
message: { id: getTrad('Settings.locales.modal.delete.success') }, message: { id: getTrad('Settings.locales.modal.delete.success') },
}); });
resolve(); return data;
}, 1000) } catch (e) {
); strapi.notification.toggle({
}; type: 'warning',
message: { id: 'notification.error' },
});
return { isDeleting, deleteLocale }; return e;
}
};
const useDeleteLocale = () => {
const queryClient = useQueryClient();
const { isLoading, mutateAsync } = useMutation(deleteLocale, {
onSuccess: (_, id) =>
queryClient.setQueryData('locales', oldLocales =>
oldLocales.filter(locale => locale.id !== id)
),
});
return { isDeleting: isLoading, deleteLocale: mutateAsync };
}; };
export default useDeleteLocale; export default useDeleteLocale;