Revize 812b9f90
Přidáno uživatelem Václav Honzík před asi 2 roky(ů)
frontend/src/features/TrackingTool/trackingToolSlice.ts | ||
---|---|---|
2 | 2 |
import { LatLngTuple } from "leaflet" |
3 | 3 |
import mapConfig from "../../config/mapConfig" |
4 | 4 |
import { PathDto } from "../../swagger/data-contracts" |
5 |
import buildPathVariants, { isMapPointDisplayable, MapPoint, PathVariant } from "./pathUtils" |
|
5 |
import buildPathVariants, { isMapPointDisplayable, MapPoint, PathVariant } from "./Map/pathUtils"
|
|
6 | 6 |
import { sendTextForProcessing } from "./trackingToolThunks" |
7 | 7 |
import storage from "redux-persist/lib/storage" |
8 | 8 |
import TrackingToolState from './TrackingToolState' |
... | ... | |
60 | 60 |
...state, |
61 | 61 |
currentPage: action.payload, |
62 | 62 |
}), |
63 |
updateMapMarker: (state: TrackingToolState, action: { payload: { idx: number, item: MapPoint } }) => { |
|
64 |
const { idx, item } = action.payload |
|
63 |
// Updates map marker while ignoring its idx property |
|
64 |
updateMapMarkerWithId: (state: TrackingToolState, action: { payload: { id: string, item: MapPoint } }) => { |
|
65 |
const { item } = action.payload |
|
66 |
const idx = state.primaryPathIdx |
|
67 |
if (!state.pathVariants || state.pathVariants.length <= idx) { |
|
68 |
return state |
|
69 |
} |
|
70 |
|
|
71 |
const mapMarkerIdx = state.pathVariants[idx].findIndex((item) => item.id === action.payload.id) |
|
72 |
if (mapMarkerIdx === -1) { |
|
73 |
return state |
|
74 |
} |
|
75 |
|
|
76 |
const newPathVariant = [...state.pathVariants[idx]] |
|
77 |
newPathVariant[mapMarkerIdx] = item |
|
78 |
return { |
|
79 |
...state, |
|
80 |
pathVariants: [...state.pathVariants.slice(0, idx), newPathVariant, ...state.pathVariants.slice(idx + 1)], |
|
81 |
} |
|
82 |
}, |
|
83 |
// Updates map marker based on its idx property |
|
84 |
updateMapMarker: (state: TrackingToolState, action: { payload: { item: MapPoint } }) => { |
|
85 |
const { item } = action.payload |
|
86 |
const idx = state.primaryPathIdx |
|
65 | 87 |
if (!state.pathVariants || state.pathVariants.length <= idx) { |
66 | 88 |
return state |
67 | 89 |
} |
... | ... | |
81 | 103 |
}) |
82 | 104 |
} |
83 | 105 |
}, |
106 |
swapMapMarkerIndices: (state: TrackingToolState, action: { payload: { destination: number, source: number } }) => { |
|
107 |
const { destination, source } = action.payload |
|
108 |
if (!state.pathVariants || state.pathVariants.length === 0) { |
|
109 |
return state |
|
110 |
} |
|
111 |
|
|
112 |
return { |
|
113 |
...state, |
|
114 |
pathVariants: state.pathVariants.map((pathVariant, i) => { |
|
115 |
if (state.primaryPathIdx !== i) { |
|
116 |
return [...pathVariant] |
|
117 |
} |
|
118 |
|
|
119 |
if (pathVariant.length <= destination || pathVariant.length <= source) { |
|
120 |
return [...pathVariant] |
|
121 |
} |
|
122 |
|
|
123 |
// JS dark magic splice |
|
124 |
const result = [...pathVariant] |
|
125 |
const [removed] = result.splice(source, 1) |
|
126 |
result.splice(destination, 0, removed) |
|
127 |
return result |
|
128 |
}) |
|
129 |
} |
|
130 |
}, |
|
84 | 131 |
clear: () => ({ ...initialState }), |
85 | 132 |
mergeWithCurrentPath: (state: TrackingToolState, action: { payload: PathVariant }) => { |
86 | 133 |
const { payload: jsonPath } = action |
... | ... | |
104 | 151 |
const pathMap = new Map(path.map((item) => [item.catalogItem.id as string, item])) |
105 | 152 |
|
106 | 153 |
// Create an array of items to be replaced and items to be added to the end |
107 |
const itemsToReplace: MapPoint[] = [] |
|
154 |
// const itemsToReplace: MapPoint[] = []
|
|
108 | 155 |
const itemsToAdd: MapPoint[] = [] |
109 | 156 |
jsonPath.forEach((item) => { |
110 | 157 |
if (!pathMap.has(item.catalogItem.id as string)) { |
111 | 158 |
itemsToAdd.push(item) |
112 | 159 |
return |
113 | 160 |
} |
114 |
|
|
115 |
const idx = pathMap.get(item.catalogItem.id as string)!.idx |
|
116 |
item.idx = idx |
|
117 |
itemsToReplace.push(item) |
|
161 |
|
|
162 |
// const idx = pathMap.get(item.catalogItem.id as string)!.idx
|
|
163 |
// item.idx = idx
|
|
164 |
// itemsToReplace.push(item)
|
|
118 | 165 |
}) |
119 | 166 |
|
120 | 167 |
// Iterate over items to replace and replace them |
121 | 168 |
const newPath = [...path] |
122 |
itemsToReplace.forEach((item) => { |
|
123 |
newPath[item.idx] = item |
|
124 |
}) |
|
169 |
// itemsToReplace.forEach((item) => {
|
|
170 |
// newPath[item.idx] = item
|
|
171 |
// })
|
|
125 | 172 |
|
126 | 173 |
// Add items to the end |
127 | 174 |
itemsToAdd.forEach((item) => { |
128 |
item.active = false
|
|
175 |
item.active = !state.pathVariants || state.pathVariants.length === 0
|
|
129 | 176 |
item.idx = newPath.length |
130 | 177 |
newPath.push(item) |
131 | 178 |
}) |
... | ... | |
176 | 223 |
}, |
177 | 224 |
}) |
178 | 225 |
|
179 |
export const { consumeErr, setPrimaryIdx, resetDialogApiCallSuccess, clear, updateMapMarker, mergeWithCurrentPath } = |
|
180 |
trackingToolSlice.actions |
|
226 |
export const { |
|
227 |
consumeErr, |
|
228 |
setPrimaryIdx, |
|
229 |
resetDialogApiCallSuccess, |
|
230 |
clear, |
|
231 |
updateMapMarker, |
|
232 |
mergeWithCurrentPath, |
|
233 |
swapMapMarkerIndices, |
|
234 |
updateMapMarkerWithId |
|
235 |
} = trackingToolSlice.actions |
|
181 | 236 |
const trackingToolReducer = trackingToolSlice.reducer |
182 | 237 |
export default trackingToolReducer |
Také k dispozici: Unified diff
draggable list start
re #9741