/ frontend / containers / Alerts / useCreateTripAlert.tsx
useCreateTripAlert.tsx
 1  import {useCallback} from 'react';
 2  import {
 3    SetTripAlertMutationVariables,
 4    useSetTripAlertMutation,
 5  } from '../../generated/graphql';
 6  import {t} from 'i18next';
 7  import useToastStore from '../../stores/useToastStore';
 8  
 9  const useCreateTripAlert = () => {
10    const addToast = useToastStore(s => s.addToast);
11    const [setTripAlertMutation] = useSetTripAlertMutation();
12  
13    const handleCreateTripAlert = useCallback(
14      async (variables: SetTripAlertMutationVariables) => {
15        try {
16          await setTripAlertMutation({
17            variables,
18          });
19          addToast(t('alert.create'));
20        } catch (error) {
21          addToast(t('alert.errors.cant_create'));
22          console.error(error);
23        }
24      },
25      [addToast, setTripAlertMutation]
26    );
27  
28    return handleCreateTripAlert;
29  };
30  
31  export default useCreateTripAlert;