({
onSuccess,
retryWithRandomizedHandle,
} = {})
| 30 | const HANDLE_ERROR = 'handle is already used'; |
| 31 | |
| 32 | export const useSquadCreate: CustomHook = ({ |
| 33 | onSuccess, |
| 34 | retryWithRandomizedHandle, |
| 35 | } = {}) => { |
| 36 | const { addSquad } = useBoot(); |
| 37 | const { logEvent } = useLogContext(); |
| 38 | const { displayToast } = useToastNotification(); |
| 39 | const { completeAction } = useActions(); |
| 40 | |
| 41 | const { mutateAsync: onCreateSquad, isPending: isLoading } = useMutation({ |
| 42 | mutationFn: createSquad, |
| 43 | onSuccess: (squad, variables) => { |
| 44 | logEvent({ |
| 45 | event_name: LogEvent.CompleteSquadCreation, |
| 46 | ...(variables.company && { |
| 47 | extra: JSON.stringify({ company: true }), |
| 48 | }), |
| 49 | }); |
| 50 | |
| 51 | addSquad(squad); |
| 52 | completeAction(ActionType.CreateSquad); |
| 53 | |
| 54 | if (onSuccess) { |
| 55 | onSuccess(squad); |
| 56 | } else { |
| 57 | router.replace(squad.permalink); |
| 58 | } |
| 59 | }, |
| 60 | onError: (error: ApiErrorResult, variables) => { |
| 61 | const result = parseOrDefault<Record<string, string>>( |
| 62 | error?.response?.errors?.[0]?.message, |
| 63 | ); |
| 64 | |
| 65 | if ( |
| 66 | retryWithRandomizedHandle && |
| 67 | typeof result === 'object' && |
| 68 | result.handle === HANDLE_ERROR |
| 69 | ) { |
| 70 | onCreateSquad({ |
| 71 | ...variables, |
| 72 | handle: `${variables.handle}${getRandom4Digits()}`, |
| 73 | }); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | displayToast( |
| 78 | typeof result === 'object' && result.handle |
| 79 | ? result.handle |
| 80 | : DEFAULT_ERROR, |
| 81 | ); |
| 82 | }, |
| 83 | }); |
| 84 | |
| 85 | return { onCreateSquad, isLoading }; |
| 86 | }; |
no test coverage detected