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,7 +6,6 @@ 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: {
@ -22,6 +21,18 @@ const addLocale = async ({ code, name, isDefault }) => {
});
return data;
};
const useAddLocale = () => {
const [isLoading, setLoading] = useState(false);
const dispatch = useDispatch();
const persistLocale = async locale => {
setLoading(true);
try {
const newLocale = await addLocale(locale);
dispatch({ type: ADD_LOCALE, newLocale });
} catch (e) {
const message = get(e, 'response.payload.message', null);
@ -38,20 +49,9 @@ const addLocale = async ({ code, name, isDefault }) => {
}
throw e;
}
};
const useAddLocale = () => {
const [isLoading, setLoading] = useState(false);
const dispatch = useDispatch();
const persistLocale = async locale => {
setLoading(true);
const newLocale = await addLocale(locale);
dispatch({ type: ADD_LOCALE, newLocale });
} finally {
setLoading(false);
}
};
return { isAdding: isLoading, addLocale: persistLocale };