Merge branch 'handle-api-errors' into 'develop'
Handle error from API on Group Save See merge request soapbox-pub/soapbox!2440
This commit is contained in:
commit
18127b7cdb
|
@ -1,3 +1,4 @@
|
||||||
|
import { AxiosError } from 'axios';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
import { useAppDispatch, useLoading } from 'soapbox/hooks';
|
import { useAppDispatch, useLoading } from 'soapbox/hooks';
|
||||||
|
@ -23,7 +24,7 @@ function useCreateEntity<TEntity extends Entity = Entity, Data = unknown>(
|
||||||
const [isSubmitting, setPromise] = useLoading();
|
const [isSubmitting, setPromise] = useLoading();
|
||||||
const { entityType, listKey } = parseEntitiesPath(expandedPath);
|
const { entityType, listKey } = parseEntitiesPath(expandedPath);
|
||||||
|
|
||||||
async function createEntity(data: Data, callbacks: EntityCallbacks<TEntity> = {}): Promise<void> {
|
async function createEntity(data: Data, callbacks: EntityCallbacks<TEntity, AxiosError> = {}): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const result = await setPromise(entityFn(data));
|
const result = await setPromise(entityFn(data));
|
||||||
const schema = opts.schema || z.custom<TEntity>();
|
const schema = opts.schema || z.custom<TEntity>();
|
||||||
|
@ -36,8 +37,12 @@ function useCreateEntity<TEntity extends Entity = Entity, Data = unknown>(
|
||||||
callbacks.onSuccess(entity);
|
callbacks.onSuccess(entity);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (callbacks.onError) {
|
if (error instanceof AxiosError) {
|
||||||
callbacks.onError(error);
|
if (callbacks.onError) {
|
||||||
|
callbacks.onError(error);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,10 +62,20 @@ const EditGroup: React.FC<IEditGroup> = ({ params: { id: groupId } }) => {
|
||||||
avatar: avatar.file,
|
avatar: avatar.file,
|
||||||
header: header.file,
|
header: header.file,
|
||||||
tags,
|
tags,
|
||||||
|
}, {
|
||||||
|
onSuccess() {
|
||||||
|
toast.success(intl.formatMessage(messages.groupSaved));
|
||||||
|
},
|
||||||
|
onError(error) {
|
||||||
|
const message = (error.response?.data as any)?.error;
|
||||||
|
|
||||||
|
if (error.response?.status === 422 && typeof message !== 'undefined') {
|
||||||
|
toast.error(message);
|
||||||
|
}
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
setIsSubmitting(false);
|
setIsSubmitting(false);
|
||||||
toast.success(intl.formatMessage(messages.groupSaved));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleAddTag = () => {
|
const handleAddTag = () => {
|
||||||
|
|
Loading…
Reference in New Issue