2024-12-12 13:56:17 +00:00
|
|
|
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
|
2024-12-12 21:55:59 +00:00
|
|
|
import { PaginationMeta } from '../common'
|
2024-12-11 16:26:36 +00:00
|
|
|
|
|
|
|
export interface InterrailTime {
|
|
|
|
hours: number
|
|
|
|
minutes: number
|
|
|
|
offset: number
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface InterrailDate {
|
|
|
|
day: number
|
|
|
|
month: number
|
|
|
|
year: number
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface InterrailTravelInfo {
|
|
|
|
arrivalTime: InterrailTime
|
|
|
|
date: InterrailDate
|
|
|
|
departureTime: InterrailTime
|
|
|
|
haconVersion: number
|
|
|
|
dataSource: number
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface InterrailStopExtraInfo {
|
|
|
|
departureTime: InterrailTime
|
|
|
|
index: number
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface InterrailStopCoordinates {
|
|
|
|
latitude: number
|
|
|
|
longitude: number
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface InterrailStopStation {
|
|
|
|
coordinates: InterrailStopCoordinates
|
|
|
|
country: string
|
|
|
|
name: string
|
|
|
|
stationId: number
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface InterrailLegInfo {
|
|
|
|
attributeCodes: string[]
|
|
|
|
attributes: object
|
|
|
|
duration: InterrailTime
|
|
|
|
directionStation: string
|
|
|
|
endTime: InterrailTime
|
|
|
|
isSeparateTicket: boolean
|
|
|
|
operationDays: string
|
|
|
|
operator: object
|
|
|
|
dataSource: number
|
|
|
|
startTime: InterrailTime
|
|
|
|
stopExtraInfo: InterrailStopExtraInfo[]
|
|
|
|
trainName: string
|
|
|
|
trainStopStations: InterrailStopStation[]
|
|
|
|
trainType: number
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface TrainTrip {
|
|
|
|
id: string
|
|
|
|
distance: number,
|
|
|
|
from: string,
|
|
|
|
to: string,
|
2024-12-11 20:33:51 +00:00
|
|
|
departureTime: number,
|
|
|
|
arrivalTime: number,
|
2024-12-12 13:56:17 +00:00
|
|
|
infoJson?: string,
|
|
|
|
info?: InterrailLegInfo,
|
2024-12-11 16:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface TrainsState {
|
|
|
|
trains: TrainTrip[]
|
|
|
|
}
|
|
|
|
|
|
|
|
const initialState: TrainsState = {
|
|
|
|
trains: []
|
|
|
|
}
|
|
|
|
|
2024-12-12 13:56:17 +00:00
|
|
|
export interface TrainsPayload {
|
|
|
|
data: TrainTrip[]
|
|
|
|
meta: PaginationMeta
|
|
|
|
}
|
|
|
|
|
2024-12-11 16:26:36 +00:00
|
|
|
export const trainSlice = createSlice({
|
|
|
|
name: 'train',
|
|
|
|
initialState: initialState,
|
|
|
|
reducers: {
|
2024-12-12 13:56:17 +00:00
|
|
|
downloadTrains(state, action: PayloadAction<TrainsPayload>) {
|
2024-12-12 14:21:58 +00:00
|
|
|
if (state.trains)
|
|
|
|
state.trains = state.trains.filter(train => action.payload.data.filter(dlTrain => dlTrain.id === train.id) === null)
|
2024-12-12 13:56:17 +00:00
|
|
|
for (const dlTrain of action.payload.data) {
|
|
|
|
const info = dlTrain.infoJson ? JSON.parse(dlTrain.infoJson) : undefined
|
|
|
|
state.trains.push({
|
|
|
|
id: dlTrain.id,
|
|
|
|
distance: dlTrain.distance,
|
|
|
|
from: dlTrain.from,
|
|
|
|
to: dlTrain.to,
|
|
|
|
departureTime: dlTrain.departureTime,
|
|
|
|
arrivalTime: dlTrain.arrivalTime,
|
|
|
|
info: info,
|
|
|
|
})
|
|
|
|
}
|
2024-12-12 21:55:59 +00:00
|
|
|
state.trains.sort((t1, t2) => t1.departureTime > t2.departureTime ? -1 : t1.departureTime == t2.arrivalTime ? 0 : 1)
|
2024-12-12 13:56:17 +00:00
|
|
|
}
|
2024-12-11 16:26:36 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2024-12-12 13:56:17 +00:00
|
|
|
export const {downloadTrains } = trainSlice.actions
|
2024-12-11 16:26:36 +00:00
|
|
|
|
|
|
|
export default trainSlice.reducer
|