Récupération de défis et tirage d'un nouveau défi via des boutons
This commit is contained in:
46
client/hooks/mutations/useChallengeMutation.ts
Normal file
46
client/hooks/mutations/useChallengeMutation.ts
Normal file
@ -0,0 +1,46 @@
|
||||
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
|
||||
}
|
||||
console.log(data)
|
||||
if (onPostSuccess)
|
||||
onPostSuccess()
|
||||
},
|
||||
onError: async (error: Error) => {
|
||||
if (onError)
|
||||
onError({ error: error })
|
||||
}
|
||||
})
|
||||
}
|
@ -1,3 +1,8 @@
|
||||
import { useAppSelector } from "./useStore"
|
||||
import { ChallengeActionsPayload, downloadChallengeActions } from "@/utils/features/challengeActions/challengeActionsSlice"
|
||||
import { useAppDispatch, useAppSelector } from "./useStore"
|
||||
|
||||
export const useChallengeActions = () => useAppSelector((state) => state.challengeActions)
|
||||
export const useDownloadChallengeActions = () => {
|
||||
const dispath = useAppDispatch()
|
||||
return (challengesData: ChallengeActionsPayload) => dispath(downloadChallengeActions(challengesData))
|
||||
}
|
||||
|
@ -1,3 +1,8 @@
|
||||
import { useAppSelector } from "./useStore"
|
||||
import { ChallengesPayload, downloadChallenges } from "@/utils/features/challenges/challengesSlice"
|
||||
import { useAppDispatch, useAppSelector } from "./useStore"
|
||||
|
||||
export const useChallenges = () => useAppSelector((state) => state.challenges)
|
||||
export const useDownloadChallenges = () => {
|
||||
const dispath = useAppDispatch()
|
||||
return (challengesData: ChallengesPayload) => dispath(downloadChallenges(challengesData))
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { useAppDispatch, useAppSelector } from "./useStore"
|
||||
import { GamePayload, setPlayerId, updateGameState, updateMoney } from "@/utils/features/game/gameSlice"
|
||||
import { GamePayload, setPlayerId, updateActiveChallengeId, updateGameState, updateMoney } from "@/utils/features/game/gameSlice"
|
||||
|
||||
export const useGame = () => useAppSelector((state) => state.game)
|
||||
export const useSetPlayerId = () => {
|
||||
@ -10,6 +10,10 @@ export const useUpdateMoney = () => {
|
||||
const dispatch = useAppDispatch()
|
||||
return (money: number) => dispatch(updateMoney(money))
|
||||
}
|
||||
export const useUpdateActiveChallengeId = () => {
|
||||
const dispatch = useAppDispatch()
|
||||
return (challengeActionId: number) => dispatch(updateActiveChallengeId(challengeActionId))
|
||||
}
|
||||
export const useUpdateGameState = () => {
|
||||
const dispatch = useAppDispatch()
|
||||
return (game: GamePayload) => dispatch(updateGameState(game))
|
||||
|
Reference in New Issue
Block a user