gitscope
https://dev.kronoz.win/r/marzneshin.git
app/dashboard/src/utils/toastHandler.ts · v0.2.0
Source
1 bf7c131 Mardin 2y ago import { CreateToastFnReturn } from '@chakra-ui/react';
2 bf7c131 Mardin 2y ago import { UseFormReturn } from 'react-hook-form';
3 a47eb4c Dawsh 2y ago
4 a47eb4c Dawsh 2y ago export const generateErrorMessage = (
5 a47eb4c Dawsh 2y ago e: any,
6 a47eb4c Dawsh 2y ago toast: CreateToastFnReturn,
7 a47eb4c Dawsh 2y ago form?: UseFormReturn<any>
8 a47eb4c Dawsh 2y ago ) => {
9 a47eb4c Dawsh 2y ago if (e.response && e.response._data) {
10 bf7c131 Mardin 2y ago if (typeof e.response._data.detail === 'string')
11 a47eb4c Dawsh 2y ago return toast({
12 a47eb4c Dawsh 2y ago title: e.response._data.detail,
13 bf7c131 Mardin 2y ago status: 'error',
14 a47eb4c Dawsh 2y ago isClosable: true,
15 bf7c131 Mardin 2y ago position: 'top',
16 a47eb4c Dawsh 2y ago duration: 3000,
17 a47eb4c Dawsh 2y ago });
18 bf7c131 Mardin 2y ago if (typeof e.response._data.detail === 'object')
19 a47eb4c Dawsh 2y ago if (form) {
20 a47eb4c Dawsh 2y ago Object.keys(e.response._data.detail).forEach((errorKey) =>
21 a47eb4c Dawsh 2y ago form.setError(errorKey, {
22 a47eb4c Dawsh 2y ago message: e.response._data.detail[errorKey],
23 a47eb4c Dawsh 2y ago })
24 a47eb4c Dawsh 2y ago );
25 a47eb4c Dawsh 2y ago return;
26 a47eb4c Dawsh 2y ago }
27 a47eb4c Dawsh 2y ago }
28 a47eb4c Dawsh 2y ago return toast({
29 bf7c131 Mardin 2y ago title: 'Something went wrong!',
30 bf7c131 Mardin 2y ago status: 'error',
31 a47eb4c Dawsh 2y ago isClosable: true,
32 bf7c131 Mardin 2y ago position: 'top',
33 a47eb4c Dawsh 2y ago duration: 3000,
34 a47eb4c Dawsh 2y ago });
35 a47eb4c Dawsh 2y ago };
36 a47eb4c Dawsh 2y ago
37 a47eb4c Dawsh 2y ago export const generateSuccessMessage = (
38 a47eb4c Dawsh 2y ago message: string,
39 a47eb4c Dawsh 2y ago toast: CreateToastFnReturn
40 a47eb4c Dawsh 2y ago ) => {
41 a47eb4c Dawsh 2y ago return toast({
42 a47eb4c Dawsh 2y ago title: message,
43 bf7c131 Mardin 2y ago status: 'success',
44 a47eb4c Dawsh 2y ago isClosable: true,
45 bf7c131 Mardin 2y ago position: 'top',
46 a47eb4c Dawsh 2y ago duration: 3000,
47 a47eb4c Dawsh 2y ago });
48 a47eb4c Dawsh 2y ago };