Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 14588cb6

Přidáno uživatelem Václav Honzík před asi 2 roky(ů)

  • ID 14588cb653e6b7358e0e641cc2b8d45dc27b5cf8
  • Rodič fc8a530f

fixed keys and indices

re #9741

Zobrazit rozdíly:

frontend/src/features/TrackingTool/trackingToolSlice.ts
1 1
import { createSlice } from '@reduxjs/toolkit'
2 2
import mapConfig from '../../config/mapConfig'
3 3
import { PathDto } from '../../swagger/data-contracts'
4
import generateUuid from '../../utils/id/uuidGenerator'
4 5
import buildPathVariants from './Map/mapUtils'
5 6
import TrackingToolState from './trackingToolState'
6 7
import { sendTextForProcessing } from './trackingToolThunks'
7
import { calculateMapCenter, MapPoint, PathVariant } from './trackingToolUtils'
8
import { calculateMapCenter, MapPoint, PathVariant, setMapPointIds } from './trackingToolUtils'
8 9

  
9 10
const initialState: TrackingToolState = {
10 11
    isLoading: false,
......
18 19
    initialState,
19 20
    reducers: {
20 21
        consumeErr: (state: TrackingToolState) => ({ ...state, lastErr: undefined, }),
21
        setSelectedPathIdx: (state: TrackingToolState, action: { payload: number }) => ({ ...state, selectedPathIdx: action.payload, }),
22
        resetDialogApiCallSuccess: (state: TrackingToolState) => ({ ...state, dialogApiCallSuccess: false, }),
22
        setSelectedPathIdx: (state: TrackingToolState, action: { payload: number }) =>
23
            ({ ...state, selectedPathIdx: action.payload, }),
24
        resetDialogApiCallSuccess: (state: TrackingToolState) =>
25
            ({ ...state, dialogApiCallSuccess: false, }),
23 26
        // Updates currently displayed path
24
        updateDisplayedPath: (state: TrackingToolState, action: { payload: PathVariant }) => ({ ...state, displayedPath: action.payload, }),
25
        // Updates map marker
26
        updateMapMarker: (state: TrackingToolState, action: { payload: MapPoint }) => {
27
        updateDisplayedPath: (state: TrackingToolState, action: { payload: PathVariant }) =>
28
            ({ ...state, displayedPath: action.payload, }),
29
        updateMapPointAtIndex: (state: TrackingToolState, action: { payload: MapPoint }) => {
27 30
            const newPath = [...state.displayedPath ?? []]
28 31
            if (newPath.length <= action.payload.idx) {
29 32
                return state // do nothing if there is no path
......
35 38
                displayedPath: newPath,
36 39
            }
37 40
        },
41
        // Updates map marker
42
        updateMapPoint: (state: TrackingToolState, action: { payload: MapPoint }) => {
43
            const newPath = [...state.displayedPath ?? []]
44
            return {
45
                ...state,
46
                displayedPath: newPath.map((point, _) => point.id === action.payload.id ? {...action.payload, idx: point.idx} : point)
47
            }
48
        },
38 49
        // Removes map marker from currently used path
39 50
        removeMapMarker: (state: TrackingToolState, action: { payload: { id: string, idx: number } }) => {
40 51
            const newPath = [...state.displayedPath ?? []]
......
47 58
                displayedPath: [...newPath.slice(0, action.payload.idx), ...newPath.slice(action.payload.idx + 1)],
48 59
            }
49 60
        },
50
        // Moves map marker in the path array
51
        moveMarkerToDestination: (state: TrackingToolState, action: { payload: { destination: number, source: number } }) => {
61
        // Changes marker index
62
        changeMarkerIdx: (state: TrackingToolState, action: { payload: { destination: number, source: number } }) => {
52 63
            const { destination, source } = action.payload
53 64
            const newPath = [...state.displayedPath ?? []]
54 65
            if (newPath.length <= source
......
74 85

  
75 86
            const newPath = [...state.displayedPath ?? []]
76 87
            const existingIds = new Set(newPath.map(item => item.id ?? ''))
77

  
78 88
            const newItems: MapPoint[] = []
79 89
            jsonPath.forEach(item => {
80 90
                if (!existingIds.has(item.id ?? '')) {
......
85 95
            newItems.forEach(item => {
86 96
                item.addToPath = !state.pathVariants || state.pathVariants.length === 0
87 97
                item.idx = newPath.length
98
                const uuid = generateUuid()
99
                item.reactId = uuid
100
                item.id = item.catalogItem.id ? item.catalogItem.id : uuid
88 101
                newPath.push(item)
89 102
            })
90 103

  
......
106 119

  
107 120
            // We will always have displayedPath defined if this function is called
108 121
            pathVariants.splice(previousIdx, 0, [...state.displayedPath ?? []])
109
            const newPath = pathVariants.splice(newIdx, 1)[0]
122
            const newPath = setMapPointIds(pathVariants.splice(newIdx, 1)[0])
110 123
            return {
111 124
                ...state,
112 125
                displayedPath: newPath,
......
114 127
                pathVariants,
115 128
            }
116 129
        },
117
        moveToNextPathVariant: (state: TrackingToolState) => {
118
            const currentPathIdx = state.displayedPathIdx
119
            const pathVariants = [...state.pathVariants ?? []]
120

  
121
            // Since we take out the selected path index the displayedPathIdx will serve as
122
            // index of the next element
123
            if (currentPathIdx >= pathVariants.length) {
124
                return state // do nothing if the displayed path is also the last one
125
            }
126

  
127
            // Else get the next path variant
128
            const newPath = [...pathVariants[currentPathIdx]]
129
            const previousPath = [...state.displayedPath ?? []]
130

  
131
            // And replace the displayed path with the new one
132
            return {
133
                ...state,
134
                displayedPath: newPath,
135
                displayedPathIdx: currentPathIdx + 1,
136
                pathVariants: [
137
                    ...pathVariants.slice(0, currentPathIdx),
138
                    previousPath,
139
                    ...pathVariants.slice(currentPathIdx + 1),
140
                ]
141
            }
142
        },
143
        moveToPreviousPathVariant: (state: TrackingToolState) => {
144
            const currentPathIdx = state.displayedPathIdx
145
            const pathVariants = [...state.pathVariants ?? []]
146

  
147
            const previousPathIdx = currentPathIdx - 1
148
            if (previousPathIdx < 0) {
149
                return state // do nothing if the displayed path is the first one
150
            }
151

  
152
            // Else get the previous path variant
153
            const newPath = [...pathVariants[previousPathIdx]]
154
            const previousPath = [...state.displayedPath ?? []]
155

  
156
            return {
157
                ...state,
158
                displayedPath: newPath,
159
                displayedPathIdx: previousPathIdx,
160
                pathVariants: [
161
                    ...pathVariants.slice(0, previousPathIdx),
162
                    previousPath,
163
                    ...pathVariants.slice(previousPathIdx + 1),
164
                ]
165
            }
166
        }
167 130
    },
168 131
    extraReducers: (builder) => {
169 132
        builder.addCase(sendTextForProcessing.fulfilled, (state, action) => {
170 133
            const pathDto: PathDto = action.payload
171 134
            const pathVariants = buildPathVariants(pathDto)
172
            const selectedPath = pathVariants.length > 0 ? pathVariants[0] : undefined
135
            const selectedPath = pathVariants.length > 0 ? setMapPointIds(pathVariants[0]) : undefined
173 136
            pathVariants.splice(0, 1) // remove the selected path so that we do not have to update it here
174 137
            if (!selectedPath) {
175 138
                return {
......
220 183
    setSelectedPathIdx,
221 184
    resetDialogApiCallSuccess,
222 185
    updateDisplayedPath,
223
    updateMapMarker,
186
    updateMapPoint,
224 187
    removeMapMarker,
225
    moveMarkerToDestination,
188
    changeMarkerIdx,
226 189
    clear,
227 190
    mergeWithCurrentPath,
228 191
    setDisplayedPathIdx,
192
    updateMapPointAtIndex
229 193
} = trackingToolSlice.actions
230 194

  
231 195
const trackingToolReducer = trackingToolSlice.reducer

Také k dispozici: Unified diff