65 lines
1.7 KiB
TypeScript
65 lines
1.7 KiB
TypeScript
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
|
|
import { PaginationMeta } from '../common'
|
|
|
|
export interface MoneyUpdate {
|
|
id: number
|
|
playerId: number
|
|
amount: number
|
|
reason: 'START' | 'NEW_RUN' | 'BUY_TRAIN' | 'WIN_CHALLENGE'
|
|
timestamp: number
|
|
runId: number | null
|
|
actionId: number | null
|
|
tripId: string | null
|
|
}
|
|
|
|
export interface MoneyUpdatesState {
|
|
moneyUpdates: MoneyUpdate[]
|
|
}
|
|
|
|
const initialState: MoneyUpdatesState = {
|
|
moneyUpdates: []
|
|
}
|
|
|
|
export interface MoneyUpdatePayload {
|
|
id: number
|
|
playerId: number
|
|
amount: number
|
|
reason: 'START' | 'NEW_RUN' | 'BUY_TRAIN' | 'WIN_CHALLENGE'
|
|
timestamp: string
|
|
runId: number | null
|
|
actionId: number | null
|
|
tripId: string | null
|
|
}
|
|
|
|
export interface MoneyUpdatesPayload {
|
|
data: MoneyUpdatePayload[]
|
|
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({
|
|
id: dlMU.id,
|
|
playerId: dlMU.playerId,
|
|
amount: dlMU.amount,
|
|
reason: dlMU.reason,
|
|
timestamp: new Date(dlMU.timestamp).getTime(),
|
|
runId: dlMU.runId,
|
|
actionId: dlMU.actionId,
|
|
tripId: dlMU.tripId,
|
|
})
|
|
}
|
|
state.moneyUpdates.sort((mu1, mu2) => mu2.id - mu1.id)
|
|
},
|
|
},
|
|
})
|
|
|
|
export const { downloadMoneyUpdates } = moneyUpdatesSlice.actions
|
|
|
|
export default moneyUpdatesSlice.reducer
|