import { AuthState } from "@/utils/features/auth/authSlice" import { GamePayload, GameState } from "@/utils/features/game/gameSlice" 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 GameProps = { updateGameState: (payload: GamePayload) => { payload: GamePayload, type: "game/updateGameState" } auth: AuthState onPostSuccess?: onPostSuccessFunc onError?: onErrorFunc } export const useGameStartMutation = ({ auth, updateGameState, onPostSuccess, onError }: GameProps) => { return useMutation({ mutationFn: async () => { return fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/game/start/`, { 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 } updateGameState(data) if (onPostSuccess) onPostSuccess() }, onError: async (error: Error) => { if (onError) onError({ error: error }) } }) } export const useGameStopMutation = ({ auth, updateGameState, onPostSuccess, onError }: GameProps) => { return useMutation({ mutationFn: async () => { return fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/game/stop/`, { 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 } updateGameState(data) if (onPostSuccess) onPostSuccess() }, onError: async (error: Error) => { if (onError) onError({ error: error }) } }) } export const useGameSwitchPlayerMutation = ({ auth, updateGameState, onPostSuccess, onError }: GameProps) => { return useMutation({ mutationFn: async () => { return fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/game/switch-running-player/`, { 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 } updateGameState(data) if (onPostSuccess) onPostSuccess() }, onError: async (error: Error) => { if (onError) onError({ error: error }) } }) } export const useGameRepairMutation = ({ auth, onPostSuccess, onError }: Omit) => { return useMutation({ mutationFn: async () => { return fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/game/repair/`, { method: "PUT", 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 }) } }) } export const useGameResetMutation = ({ auth, updateGameState, onPostSuccess, onError }: GameProps) => { return useMutation({ mutationFn: async () => { return fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/game/reset/`, { method: "DELETE", 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 } updateGameState(data) if (onPostSuccess) onPostSuccess() }, onError: async (error: Error) => { if (onError) onError({ error: error }) } }) }