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

@ -1,11 +1,9 @@
import { createSlice } from '@reduxjs/toolkit'
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { Challenge } from '../challenges/challengesSlice'
export interface ChallengeAction {
id: number
challengeId: number
title: string,
description: string,
reward: number,
success: boolean,
start: number, // date
end: number | null, // date
@ -21,13 +19,45 @@ const initialState: ActionsState = {
challengeActions: []
}
export interface ChallengeActionPayload {
id: number
playerId: number
challengeId: number
success: boolean,
start: string,
end: string | null,
penaltyStart: string | null,
penaltyEnd: string | null,
}
export interface ChallengeActionsPayload {
data: (Challenge & { action: ChallengeActionPayload })[]
}
export const challengeActionsSlice = createSlice({
name: 'challengeActions',
initialState: initialState,
reducers: {
downloadChallengeActions(state, action: PayloadAction<ChallengeActionsPayload>) {
if (state.challengeActions)
state.challengeActions = state.challengeActions.filter(challengeAction => action.payload.data.filter(dlChallenge => dlChallenge.action.id === challengeAction.id) === null)
for (const dlChallenge of action.payload.data) {
state.challengeActions.push({
id: dlChallenge.action.id,
challengeId: dlChallenge.id,
success: dlChallenge.action.success,
start: new Date(dlChallenge.action.start).getTime(),
end: dlChallenge.action.end ? new Date(dlChallenge.action.end).getTime() : null,
penaltyStart: dlChallenge.action.penaltyStart ? new Date(dlChallenge.action.penaltyStart).getTime() : null,
penaltyEnd: dlChallenge.action.penaltyEnd ? new Date(dlChallenge.action.penaltyEnd).getTime() : null,
})
}
state.challengeActions.sort((c1, c2) => c2.id - c1.id)
},
},
})
export const { } = challengeActionsSlice.actions
export const { downloadChallengeActions } = challengeActionsSlice.actions
export default challengeActionsSlice.reducer