Fix loading information on error when adding a locale that already exists (#9875)

This commit is contained in:
Marvin Frachet 2021-03-30 11:05:09 +02:00 committed by GitHub
parent f3fe056367
commit f30895b588
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,39 +6,21 @@ import { getTrad } from '../../utils';
import { ADD_LOCALE } from '../constants'; import { ADD_LOCALE } from '../constants';
const addLocale = async ({ code, name, isDefault }) => { const addLocale = async ({ code, name, isDefault }) => {
try { const data = await request(`/i18n/locales`, {
const data = await request(`/i18n/locales`, { method: 'POST',
method: 'POST', body: {
body: { name,
name, code,
code, isDefault,
isDefault, },
}, });
});
strapi.notification.toggle({ strapi.notification.toggle({
type: 'success', type: 'success',
message: { id: getTrad('Settings.locales.modal.create.success') }, message: { id: getTrad('Settings.locales.modal.create.success') },
}); });
return data; return data;
} catch (e) {
const message = get(e, 'response.payload.message', null);
if (message && message.includes('already exists')) {
strapi.notification.toggle({
type: 'warning',
message: { id: getTrad('Settings.locales.modal.create.alreadyExist') },
});
} else {
strapi.notification.toggle({
type: 'warning',
message: { id: 'notification.error' },
});
}
throw e;
}
}; };
const useAddLocale = () => { const useAddLocale = () => {
@ -48,10 +30,28 @@ const useAddLocale = () => {
const persistLocale = async locale => { const persistLocale = async locale => {
setLoading(true); setLoading(true);
const newLocale = await addLocale(locale); try {
const newLocale = await addLocale(locale);
dispatch({ type: ADD_LOCALE, newLocale });
} catch (e) {
const message = get(e, 'response.payload.message', null);
dispatch({ type: ADD_LOCALE, newLocale }); if (message && message.includes('already exists')) {
setLoading(false); strapi.notification.toggle({
type: 'warning',
message: { id: getTrad('Settings.locales.modal.create.alreadyExist') },
});
} else {
strapi.notification.toggle({
type: 'warning',
message: { id: 'notification.error' },
});
}
throw e;
} finally {
setLoading(false);
}
}; };
return { isAdding: isLoading, addLocale: persistLocale }; return { isAdding: isLoading, addLocale: persistLocale };