traintrape-moi/client/hooks/mutations/useGameMutation.ts

160 lines
4.4 KiB
TypeScript
Raw Normal View History

2024-12-11 18:35:36 +00:00
import { AuthState } from "@/utils/features/auth/authSlice"
import { GamePayload, GameState } from "@/utils/features/game/gameSlice"
2024-12-11 18:35:36 +00:00
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" }
2024-12-11 18:35:36 +00:00
auth: AuthState
onPostSuccess?: onPostSuccessFunc
onError?: onErrorFunc
}
export const useGameStartMutation = ({ auth, updateGameState, onPostSuccess, onError }: GameProps) => {
2024-12-11 18:35:36 +00:00
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) {
2024-12-11 18:35:36 +00:00
if (onError)
onError({ response: data })
return
}
updateGameState(data)
2024-12-11 18:35:36 +00:00
if (onPostSuccess)
onPostSuccess()
},
onError: async (error: Error) => {
if (onError)
onError({ error: error })
}
})
}
export const useGameStopMutation = ({ auth, updateGameState, onPostSuccess, onError }: GameProps) => {
2024-12-11 18:35:36 +00:00
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) {
2024-12-11 18:35:36 +00:00
if (onError)
onError({ response: data })
return
}
updateGameState(data)
2024-12-11 18:35:36 +00:00
if (onPostSuccess)
onPostSuccess()
},
onError: async (error: Error) => {
if (onError)
onError({ error: error })
}
})
}
export const useGameSwitchPlayerMutation = ({ auth, updateGameState, onPostSuccess, onError }: GameProps) => {
2024-12-11 18:35:36 +00:00
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) {
2024-12-11 18:35:36 +00:00
if (onError)
onError({ response: data })
return
}
updateGameState(data)
2024-12-11 18:35:36 +00:00
if (onPostSuccess)
onPostSuccess()
},
onError: async (error: Error) => {
if (onError)
onError({ error: error })
}
})
}
2024-12-14 14:57:14 +00:00
export const useGameRepairMutation = ({ auth, onPostSuccess, onError }: Omit<GameProps, 'updateGameState'>) => {
2024-12-11 18:35:36 +00:00
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) {
2024-12-11 18:35:36 +00:00
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) => {
2024-12-11 18:35:36 +00:00
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) {
2024-12-11 18:35:36 +00:00
if (onError)
onError({ response: data })
return
}
updateGameState(data)
2024-12-11 18:35:36 +00:00
if (onPostSuccess)
onPostSuccess()
},
onError: async (error: Error) => {
if (onError)
onError({ error: error })
}
})
}