({
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) => { |
| 44 | logEvent({ |
| 45 | event_name: LogEvent.CompleteSquadCreation, |
| 46 | }); |
| 47 | |
| 48 | addSquad(squad); |
| 49 | completeAction(ActionType.CreateSquad); |
| 50 | |
| 51 | if (onSuccess) { |
| 52 | onSuccess(squad); |
| 53 | } else { |
| 54 | router.replace(squad.permalink); |
| 55 | } |
| 56 | }, |
| 57 | onError: (error: ApiErrorResult, variables) => { |
| 58 | const result = parseOrDefault<Record<string, string>>( |
| 59 | error?.response?.errors?.[0]?.message, |
| 60 | ); |
| 61 | |
| 62 | if ( |
| 63 | retryWithRandomizedHandle && |
| 64 | typeof result === 'object' && |
| 65 | result.handle === HANDLE_ERROR |
| 66 | ) { |
| 67 | onCreateSquad({ |
| 68 | ...variables, |
| 69 | handle: `${variables.handle}${getRandom4Digits()}`, |
| 70 | }); |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | displayToast(typeof result === 'object' ? result.handle : DEFAULT_ERROR); |
| 75 | }, |
| 76 | }); |
| 77 | |
| 78 | return { onCreateSquad, isLoading }; |
| 79 | }; |
no test coverage detected