traintrape-moi/client/utils/features/moneyUpdates/moneyUpdatesSlice.ts

44 lines
1.1 KiB
TypeScript
Raw Normal View History

import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { PaginationMeta } from '../common'
export interface MoneyUpdate {
id: number
playerId: number
reason: 'START' | 'NEW_RUN' | 'BUY_TRAIN' | 'WIN_CHALLENGE'
timestamp: number
runId: number | null
actionId: number | null
tripId: number | null
}
export interface MoneyUpdatesState {
moneyUpdates: MoneyUpdate[]
}
const initialState: MoneyUpdatesState = {
moneyUpdates: []
}
export interface MoneyUpdatesPayload {
data: MoneyUpdate[]
meta: PaginationMeta
}
export const moneyUpdatesSlice = createSlice({
name: 'moneyUpdates',
initialState: initialState,
reducers: {
downloadMoneyUpdates(state, action: PayloadAction<MoneyUpdatesPayload>) {
state.moneyUpdates = state.moneyUpdates.filter(moneyUpdate => action.payload.data.filter(dlMU => dlMU.id === moneyUpdate.id) === null)
for (const dlMU of action.payload.data) {
state.moneyUpdates.push(dlMU)
}
state.moneyUpdates.sort((mu1, mu2) => mu2.id - mu1.id)
},
},
})
export const { downloadMoneyUpdates } = moneyUpdatesSlice.actions
export default moneyUpdatesSlice.reducer