mirror of
https://github.com/strapi/strapi.git
synced 2025-09-25 16:29:34 +00:00
[i18n] delete a locale (#9404)
This commit is contained in:
parent
f72f8ec2b9
commit
e5edd0a1c9
@ -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,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -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', () => {
|
||||||
|
@ -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 e;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return { isDeleting, deleteLocale };
|
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;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user