mirror of
https://github.com/strapi/strapi.git
synced 2025-11-06 21:29:24 +00:00
Fix loading information on error when adding a locale that already exists (#9875)
This commit is contained in:
parent
f3fe056367
commit
f30895b588
@ -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 };
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user