1 |
8c57f958
|
Vaclav Honzik
|
import { createSlice } from '@reduxjs/toolkit'
|
2 |
|
|
import { LatLngTuple } from 'leaflet'
|
3 |
|
|
import mapConfig from '../../config/mapConfig'
|
4 |
|
|
import { PathDto } from '../../swagger/data-contracts'
|
5 |
|
|
import buildPathVariants, { PathVariant } from './buildPathVariants'
|
6 |
|
|
import { sendTextForProcessing } from './trackingToolThunks'
|
7 |
|
|
|
8 |
|
|
export interface TrackingToolState {
|
9 |
|
|
isLoading: boolean // whether the data is being loaded
|
10 |
|
|
pathDto?: PathDto // the data
|
11 |
|
|
pathVariants?: PathVariant[] // undefined signals that no path variants were yet fetched from the API
|
12 |
dd270a41
|
Vaclav Honzik
|
lastError?: string // consumable for errors during thunks
|
13 |
8c57f958
|
Vaclav Honzik
|
mapCenter: LatLngTuple // pair of latitude and longitude
|
14 |
|
|
primaryPathIdx: number // index of the primary path
|
15 |
|
|
activePaths: Set<number> // indices of the active paths
|
16 |
|
|
// trigger to close the dialog when API call is finished
|
17 |
|
|
dialogApiCallSuccess: boolean
|
18 |
|
|
}
|
19 |
|
|
|
20 |
|
|
const initialState: TrackingToolState = {
|
21 |
|
|
isLoading: false,
|
22 |
|
|
mapCenter: [
|
23 |
|
|
mapConfig.defaultCoordinates[0],
|
24 |
|
|
mapConfig.defaultCoordinates[1],
|
25 |
|
|
],
|
26 |
|
|
primaryPathIdx: 0,
|
27 |
|
|
activePaths: new Set(),
|
28 |
|
|
dialogApiCallSuccess: true,
|
29 |
|
|
}
|
30 |
|
|
|
31 |
|
|
// Returns tuple of average latitude and longitude
|
32 |
|
|
const calculateMapCenter = (pathVariant: PathVariant): LatLngTuple => [
|
33 |
|
|
pathVariant.map((item) => item.latitude ?? 0).reduce((a, b) => a + b, 0) /
|
34 |
|
|
pathVariant.length,
|
35 |
|
|
pathVariant.map((item) => item.longitude ?? 0).reduce((a, b) => a + b, 0) /
|
36 |
|
|
pathVariant.length,
|
37 |
|
|
]
|
38 |
|
|
|
39 |
|
|
export const trackingToolSlice = createSlice({
|
40 |
|
|
name: 'trackingTool',
|
41 |
|
|
initialState,
|
42 |
|
|
reducers: {
|
43 |
|
|
consumeErr: (state) => ({ ...state, lastErr: undefined }),
|
44 |
|
|
setPrimaryIdx: (state, action) => ({
|
45 |
|
|
...state,
|
46 |
|
|
primaryPathIdx: action.payload,
|
47 |
|
|
}),
|
48 |
|
|
resetDialogApiCallSuccess: (state) => ({
|
49 |
|
|
...state,
|
50 |
|
|
dialogApiCallSuccess: false,
|
51 |
|
|
}),
|
52 |
|
|
},
|
53 |
|
|
extraReducers: (builder) => {
|
54 |
|
|
builder.addCase(sendTextForProcessing.fulfilled, (state, action) => {
|
55 |
|
|
const dto: PathDto = action.payload
|
56 |
|
|
const pathVariants = buildPathVariants(dto)
|
57 |
|
|
return {
|
58 |
|
|
...state,
|
59 |
|
|
pathVariants,
|
60 |
|
|
// TODO map this correctly
|
61 |
|
|
activePaths: new Set(pathVariants.map((_, idx) => idx)),
|
62 |
|
|
// TODO calculate correctly
|
63 |
|
|
mapCenter:
|
64 |
|
|
pathVariants.length > 0
|
65 |
|
|
? calculateMapCenter(pathVariants[0])
|
66 |
|
|
: (state.mapCenter as LatLngTuple),
|
67 |
|
|
isLoading: false,
|
68 |
|
|
dialogApiCallSuccess: true,
|
69 |
|
|
}
|
70 |
|
|
})
|
71 |
|
|
builder.addCase(sendTextForProcessing.rejected, (state, action) => ({
|
72 |
|
|
...initialState,
|
73 |
dd270a41
|
Vaclav Honzik
|
lastError: action.error.message,
|
74 |
8c57f958
|
Vaclav Honzik
|
isLoading: false,
|
75 |
|
|
dialogApiCallSuccess: false,
|
76 |
|
|
}))
|
77 |
|
|
builder.addCase(sendTextForProcessing.pending, (state) => {
|
78 |
|
|
return {
|
79 |
|
|
...state,
|
80 |
|
|
isLoading: true,
|
81 |
|
|
dialogApiCallSuccess: false,
|
82 |
|
|
}
|
83 |
|
|
})
|
84 |
|
|
},
|
85 |
|
|
})
|
86 |
|
|
|
87 |
|
|
export const { consumeErr, setPrimaryIdx, resetDialogApiCallSuccess } = trackingToolSlice.actions
|
88 |
|
|
const trackingToolReducer = trackingToolSlice.reducer
|
89 |
|
|
export default trackingToolReducer
|