import { AuthState } from "@/utils/features/auth/authSlice" import { useMutation } from "@tanstack/react-query" type ErrorResponse = { error: string message: string statusCode: number } type onPostSuccessFunc = () => void type ErrorFuncProps = { response?: ErrorResponse, error?: Error } type onErrorFunc = (props: ErrorFuncProps) => void type ChallengeActionProps = { auth: AuthState onPostSuccess?: onPostSuccessFunc onError?: onErrorFunc } export const useDrawRandomChallengeMutation = ({ auth, onPostSuccess, onError }: ChallengeActionProps) => { return useMutation({ mutationFn: async () => { return fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/challenges/draw-random/`, { method: "POST", headers: { "Authorization": `Bearer ${auth.token}`, "Content-Type": "application/json", }, }).then(resp => resp.json()) }, onSuccess: async (data) => { if (data.statusCode) { if (onError) onError({ response: data }) return } if (onPostSuccess) onPostSuccess() }, onError: async (error: Error) => { if (onError) onError({ error: error }) } }) }