From f30895b588bd13e7348e1597f3fa026af61eeb91 Mon Sep 17 00:00:00 2001 From: Marvin Frachet Date: Tue, 30 Mar 2021 11:05:09 +0200 Subject: [PATCH] Fix loading information on error when adding a locale that already exists (#9875) --- .../admin/src/hooks/useAddLocale/index.js | 68 +++++++++---------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/packages/strapi-plugin-i18n/admin/src/hooks/useAddLocale/index.js b/packages/strapi-plugin-i18n/admin/src/hooks/useAddLocale/index.js index 906eccb086..5b22b51d31 100644 --- a/packages/strapi-plugin-i18n/admin/src/hooks/useAddLocale/index.js +++ b/packages/strapi-plugin-i18n/admin/src/hooks/useAddLocale/index.js @@ -6,39 +6,21 @@ import { getTrad } from '../../utils'; import { ADD_LOCALE } from '../constants'; const addLocale = async ({ code, name, isDefault }) => { - try { - const data = await request(`/i18n/locales`, { - method: 'POST', - body: { - name, - code, - isDefault, - }, - }); + const data = await request(`/i18n/locales`, { + method: 'POST', + body: { + name, + code, + isDefault, + }, + }); - strapi.notification.toggle({ - type: 'success', - message: { id: getTrad('Settings.locales.modal.create.success') }, - }); + strapi.notification.toggle({ + type: 'success', + message: { id: getTrad('Settings.locales.modal.create.success') }, + }); - 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; - } + return data; }; const useAddLocale = () => { @@ -48,10 +30,28 @@ const useAddLocale = () => { const persistLocale = async locale => { 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 }); - setLoading(false); + 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; + } finally { + setLoading(false); + } }; return { isAdding: isLoading, addLocale: persistLocale };