Boutons pour démarrer le jeu
This commit is contained in:
155
client/hooks/mutations/useGameMutation.ts
Normal file
155
client/hooks/mutations/useGameMutation.ts
Normal file
@ -0,0 +1,155 @@
|
||||
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 })
|
||||
}
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user