Récupération de défis et tirage d'un nouveau défi via des boutons

This commit is contained in:
2024-12-12 22:55:59 +01:00
parent 9d0b5cb254
commit 04f30e3ac2
12 changed files with 193 additions and 34 deletions

View 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 })
}
})
}

View File

@ -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))
}

View File

@ -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))
}

View File

@ -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))