Revize de12c6be
Přidáno uživatelem Václav Honzík před asi 2 roky(ů)
frontend/src/features/TrackingTool/trackingToolSlice.ts | ||
---|---|---|
1 |
import { createSlice } from '@reduxjs/toolkit'
|
|
2 |
import { LatLngTuple } from 'leaflet'
|
|
3 |
import { persistReducer } from 'redux-persist'
|
|
4 |
import mapConfig from '../../config/mapConfig'
|
|
5 |
import { PathDto } from '../../swagger/data-contracts'
|
|
6 |
import buildPathVariants, { PathVariant } from './buildPathVariants'
|
|
7 |
import { sendTextForProcessing } from './trackingToolThunks'
|
|
8 |
import storage from 'redux-persist/lib/storage'
|
|
1 |
import { createSlice } from "@reduxjs/toolkit"
|
|
2 |
import { LatLngTuple } from "leaflet"
|
|
3 |
import { persistReducer } from "redux-persist"
|
|
4 |
import mapConfig from "../../config/mapConfig"
|
|
5 |
import { PathDto } from "../../swagger/data-contracts"
|
|
6 |
import buildPathVariants, { PathVariant } from "./buildPathVariants"
|
|
7 |
import { sendTextForProcessing } from "./trackingToolThunks"
|
|
8 |
import storage from "redux-persist/lib/storage"
|
|
9 | 9 |
|
10 | 10 |
export interface TrackingToolState { |
11 | 11 |
isLoading: boolean // whether the data is being loaded |
... | ... | |
14 | 14 |
lastError?: string // consumable for errors during thunks |
15 | 15 |
mapCenter: LatLngTuple // pair of latitude and longitude |
16 | 16 |
primaryPathIdx: number // index of the primary path |
17 |
activePaths: Set<number> // indices of the active paths |
|
18 | 17 |
// trigger to close the dialog when API call is finished |
19 | 18 |
dialogApiCallSuccess: boolean |
20 | 19 |
pathsPerPage: number // max number of paths to show on the map at once |
... | ... | |
25 | 24 |
|
26 | 25 |
const initialState: TrackingToolState = { |
27 | 26 |
isLoading: false, |
28 |
mapCenter: [ |
|
29 |
mapConfig.defaultCoordinates[0], |
|
30 |
mapConfig.defaultCoordinates[1], |
|
31 |
], |
|
27 |
mapCenter: [mapConfig.defaultCoordinates[0], mapConfig.defaultCoordinates[1]], |
|
32 | 28 |
primaryPathIdx: 0, |
33 |
activePaths: new Set(), |
|
34 | 29 |
dialogApiCallSuccess: true, |
35 | 30 |
pathsPerPage: defaultPathsPerPage, |
36 | 31 |
currentPage: 0, |
... | ... | |
38 | 33 |
|
39 | 34 |
// Returns tuple of average latitude and longitude |
40 | 35 |
const calculateMapCenter = (pathVariant: PathVariant): LatLngTuple => [ |
41 |
pathVariant.map((item) => item.latitude ?? 0).reduce((a, b) => a + b, 0) / |
|
42 |
pathVariant.length, |
|
43 |
pathVariant.map((item) => item.longitude ?? 0).reduce((a, b) => a + b, 0) / |
|
44 |
pathVariant.length, |
|
36 |
pathVariant |
|
37 |
.map((item) => item.catalogItem.latitude ?? 0) |
|
38 |
.reduce((a, b) => a + b, 0) / pathVariant.length, |
|
39 |
pathVariant |
|
40 |
.map((item) => item.catalogItem.longitude ?? 0) |
|
41 |
.reduce((a, b) => a + b, 0) / pathVariant.length, |
|
45 | 42 |
] |
46 | 43 |
|
47 | 44 |
const persistConfig = { |
48 |
key: 'auth',
|
|
45 |
key: "auth",
|
|
49 | 46 |
storage, // localStorage for browsers |
50 | 47 |
} |
51 | 48 |
|
52 | 49 |
export const trackingToolSlice = createSlice({ |
53 |
name: 'trackingTool',
|
|
50 |
name: "trackingTool",
|
|
54 | 51 |
initialState, |
55 | 52 |
reducers: { |
56 |
consumeErr: (state: TrackingToolState) => ({ ...state, lastErr: undefined }), |
|
53 |
consumeErr: (state: TrackingToolState) => ({ |
|
54 |
...state, |
|
55 |
lastErr: undefined, |
|
56 |
}), |
|
57 | 57 |
setPrimaryIdx: (state: TrackingToolState, action: any) => ({ |
58 | 58 |
...state, |
59 | 59 |
primaryPathIdx: action.payload, |
... | ... | |
62 | 62 |
...state, |
63 | 63 |
dialogApiCallSuccess: false, |
64 | 64 |
}), |
65 |
setPage: (state: TrackingToolState, action: any) => ({
|
|
65 |
setPage: (state: TrackingToolState, action: { payload: number }) => ({
|
|
66 | 66 |
...state, |
67 | 67 |
currentPage: action.payload, |
68 | 68 |
}), |
69 |
clear: () => ({...initialState}) |
|
69 |
updateMapMarkerPosition: (state: TrackingToolState, action: any) => { |
|
70 |
const { idx, mapPointIdx, position } = action.payload |
|
71 |
if (!state.pathVariants || state.pathVariants.length <= idx) { |
|
72 |
return state |
|
73 |
} |
|
74 |
|
|
75 |
return { |
|
76 |
...state, |
|
77 |
pathVariants: state.pathVariants.map((pathVariant, i) => { |
|
78 |
if (i !== idx) { |
|
79 |
return [...pathVariant] |
|
80 |
} |
|
81 |
|
|
82 |
const mapPoint = pathVariant[mapPointIdx] |
|
83 |
mapPoint.catalogItem = { |
|
84 |
...mapPoint.catalogItem, |
|
85 |
latitude: position[0], |
|
86 |
longitude: position[1] |
|
87 |
} |
|
88 |
|
|
89 |
return [ |
|
90 |
...pathVariant.slice(0, mapPointIdx), |
|
91 |
mapPoint, |
|
92 |
...pathVariant.slice(mapPointIdx + 1), |
|
93 |
] |
|
94 |
}) |
|
95 |
} |
|
96 |
}, |
|
97 |
clear: () => ({ ...initialState }), |
|
70 | 98 |
}, |
71 | 99 |
extraReducers: (builder) => { |
72 | 100 |
builder.addCase(sendTextForProcessing.fulfilled, (state, action) => { |
... | ... | |
76 | 104 |
...state, |
77 | 105 |
pathVariants, |
78 | 106 |
pathDto, |
79 |
// TODO map this correctly |
|
80 |
activePaths: new Set(pathVariants.map((_, idx) => idx)), |
|
81 | 107 |
// TODO calculate correctly |
82 | 108 |
mapCenter: |
83 | 109 |
pathVariants.length > 0 |
... | ... | |
105 | 131 |
}, |
106 | 132 |
}) |
107 | 133 |
|
108 |
export const { consumeErr, setPrimaryIdx, resetDialogApiCallSuccess, clear } = trackingToolSlice.actions |
|
134 |
export const { consumeErr, setPrimaryIdx, resetDialogApiCallSuccess, clear, updateMapMarkerPosition } = |
|
135 |
trackingToolSlice.actions |
|
109 | 136 |
const trackingToolReducer = trackingToolSlice.reducer |
110 | 137 |
export default trackingToolReducer |
Také k dispozici: Unified diff
draggable
re #9741