2024-12-12 21:55:59 +00:00
|
|
|
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
|
|
|
|
import { PaginationMeta } from '../common'
|
|
|
|
import { ChallengeAction } from '../challengeActions/challengeActionsSlice'
|
2024-12-11 16:26:36 +00:00
|
|
|
|
|
|
|
export interface Challenge {
|
|
|
|
id: number
|
|
|
|
title: string,
|
|
|
|
description: string,
|
|
|
|
reward: number,
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ChallengesState {
|
|
|
|
challenges: Challenge[]
|
|
|
|
}
|
|
|
|
|
|
|
|
const initialState: ChallengesState = {
|
|
|
|
challenges: []
|
|
|
|
}
|
|
|
|
|
2024-12-12 21:55:59 +00:00
|
|
|
export interface ChallengesPayload {
|
|
|
|
data: (Challenge & { action: ChallengeAction | null })[]
|
|
|
|
meta: PaginationMeta
|
|
|
|
}
|
|
|
|
|
2024-12-11 16:26:36 +00:00
|
|
|
export const challengesSlice = createSlice({
|
|
|
|
name: 'challenges',
|
|
|
|
initialState: initialState,
|
|
|
|
reducers: {
|
2024-12-12 21:55:59 +00:00
|
|
|
downloadChallenges(state, action: PayloadAction<ChallengesPayload>) {
|
|
|
|
if (state.challenges)
|
|
|
|
state.challenges = state.challenges.filter(challenge => action.payload.data.filter(dlChallenge => dlChallenge.id === challenge.id) === null)
|
|
|
|
for (const dlChallenge of action.payload.data) {
|
|
|
|
state.challenges.push({
|
|
|
|
id: dlChallenge.id,
|
|
|
|
title: dlChallenge.title,
|
|
|
|
description: dlChallenge.description,
|
|
|
|
reward: dlChallenge.reward,
|
|
|
|
})
|
|
|
|
}
|
2024-12-14 16:18:41 +00:00
|
|
|
state.challenges.sort((c1, c2) => c1.title.localeCompare(c2.title))
|
2024-12-12 21:55:59 +00:00
|
|
|
},
|
2024-12-11 16:26:36 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2024-12-12 21:55:59 +00:00
|
|
|
export const { downloadChallenges } = challengesSlice.actions
|
2024-12-11 16:26:36 +00:00
|
|
|
|
|
|
|
export default challengesSlice.reducer
|