156 lines
4.1 KiB
TypeScript
156 lines
4.1 KiB
TypeScript
|
import { AuthState } from "@/utils/features/auth/authSlice"
|
||
|
import { 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 = {
|
||
|
game: GameState
|
||
|
auth: AuthState
|
||
|
onPostSuccess?: onPostSuccessFunc
|
||
|
onError?: onErrorFunc
|
||
|
}
|
||
|
|
||
|
export const useGameStartMutation = ({ game, auth, 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.error) {
|
||
|
if (onError)
|
||
|
onError({ response: data })
|
||
|
return
|
||
|
}
|
||
|
if (onPostSuccess)
|
||
|
onPostSuccess()
|
||
|
},
|
||
|
onError: async (error: Error) => {
|
||
|
if (onError)
|
||
|
onError({ error: error })
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
export const useGameStopMutation = ({ auth, game, 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.error) {
|
||
|
if (onError)
|
||
|
onError({ response: data })
|
||
|
return
|
||
|
}
|
||
|
if (onPostSuccess)
|
||
|
onPostSuccess()
|
||
|
},
|
||
|
onError: async (error: Error) => {
|
||
|
if (onError)
|
||
|
onError({ error: error })
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
export const useGameSwitchPlayerMutation = ({ auth, game, 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.error) {
|
||
|
if (onError)
|
||
|
onError({ response: data })
|
||
|
return
|
||
|
}
|
||
|
if (onPostSuccess)
|
||
|
onPostSuccess()
|
||
|
},
|
||
|
onError: async (error: Error) => {
|
||
|
if (onError)
|
||
|
onError({ error: error })
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
export const useGameRepairMutation = ({ auth, game, onPostSuccess, onError }: GameProps) => {
|
||
|
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.error) {
|
||
|
if (onError)
|
||
|
onError({ response: data })
|
||
|
return
|
||
|
}
|
||
|
if (onPostSuccess)
|
||
|
onPostSuccess()
|
||
|
},
|
||
|
onError: async (error: Error) => {
|
||
|
if (onError)
|
||
|
onError({ error: error })
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
export const useGameResetMutation = ({ auth, game, 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.error) {
|
||
|
if (onError)
|
||
|
onError({ response: data })
|
||
|
return
|
||
|
}
|
||
|
if (onPostSuccess)
|
||
|
onPostSuccess()
|
||
|
},
|
||
|
onError: async (error: Error) => {
|
||
|
if (onError)
|
||
|
onError({ error: error })
|
||
|
}
|
||
|
})
|
||
|
}
|