67 lines
2.1 KiB
TypeScript
67 lines
2.1 KiB
TypeScript
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
|
|
import { Challenge } from '../challenges/challengesSlice'
|
|
|
|
export interface ChallengeAction {
|
|
id: number
|
|
challengeId: number
|
|
success: boolean,
|
|
start: number, // date
|
|
end: number | null, // date
|
|
penaltyStart: number | null, // date
|
|
penaltyEnd: number | null, // date
|
|
runId: number,
|
|
}
|
|
|
|
export interface ActionsState {
|
|
challengeActions: ChallengeAction[]
|
|
}
|
|
|
|
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,
|
|
runId: number,
|
|
}
|
|
|
|
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,
|
|
runId: dlChallenge.action.runId,
|
|
})
|
|
}
|
|
state.challengeActions.sort((c1, c2) => c2.id - c1.id)
|
|
},
|
|
},
|
|
})
|
|
|
|
export const { downloadChallengeActions } = challengeActionsSlice.actions
|
|
|
|
export default challengeActionsSlice.reducer
|