Projekt

Obecné

Profil

« Předchozí | Další » 

Revize a7ae217f

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

import export part 1

re #9741

Zobrazit rozdíly:

frontend/src/features/TrackingTool/trackingToolSlice.ts
1 1
import { createSlice } from "@reduxjs/toolkit"
2 2
import { LatLngTuple } from "leaflet"
3
import { persistReducer } from "redux-persist"
4 3
import mapConfig from "../../config/mapConfig"
5 4
import { PathDto } from "../../swagger/data-contracts"
6
import buildPathVariants, { MapPoint, PathVariant } from "./buildPathVariants"
5
import buildPathVariants, { isMapPointDisplayable, MapPoint, PathVariant } from "./pathUtils"
7 6
import { sendTextForProcessing } from "./trackingToolThunks"
8 7
import storage from "redux-persist/lib/storage"
8
import TrackingToolState from './TrackingToolState'
9 9

  
10
export interface TrackingToolState {
11
    isLoading: boolean // whether the data is being loaded
12
    pathDto?: PathDto // the data
13
    pathVariants?: PathVariant[] // undefined signals that no path variants were yet fetched from the API
14
    lastError?: string // consumable for errors during thunks
15
    mapCenter: LatLngTuple // pair of latitude and longitude
16
    primaryPathIdx: number // index of the primary path
17
    // trigger to close the dialog when API call is finished
18
    dialogApiCallSuccess: boolean
19
    pathsPerPage: number // max number of paths to show on the map at once
20
    currentPage: number // current page of paths - starts from 0
21
}
22 10

  
23 11
const defaultPathsPerPage = 5
24 12

  
......
32 20
}
33 21

  
34 22
const calculateMapCenter = (pathVariant: PathVariant): LatLngTuple | undefined => {
35
    const displayableItems = pathVariant.filter((item) => item.displayable)
23
    const displayableItems = pathVariant.filter((item) => isMapPointDisplayable(item))
36 24
    if (displayableItems.length === 0) {
37 25
        return undefined
38 26
    }
......
94 82
            }
95 83
        },
96 84
        clear: () => ({ ...initialState }),
85
        mergeWithCurrentPath: (state: TrackingToolState, action: { payload: PathVariant }) => {
86
            const { payload: jsonPath } = action
87
            if (!jsonPath) {
88
                return { ...state }
89
            }
90

  
91
            const pathVariants = [...state.pathVariants ?? []]
92
            let primaryPathIdx = state.primaryPathIdx
93
            let currentPage = state.currentPage
94

  
95
            // If there are no path append a new array to the pathVariants array and set primaryIdx to 0
96
            if (pathVariants.length === 0) {
97
                primaryPathIdx = 0
98
                currentPage = 0
99
                pathVariants.push([])
100
            }
101

  
102
            // Get the path and create a map to check whether some point with the same id already exists
103
            const path = pathVariants[primaryPathIdx]
104
            const pathMap = new Map(path.map((item) => [item.catalogItem.id as string, item]))
105

  
106
            // Create an array of items to be replaced and items to be added to the end
107
            const itemsToReplace: MapPoint[] = []
108
            const itemsToAdd: MapPoint[] = []
109
            jsonPath.forEach((item) => {
110
                if (!pathMap.has(item.catalogItem.id as string)) {
111
                    itemsToAdd.push(item)
112
                    return
113
                }
114
                
115
                const idx = pathMap.get(item.catalogItem.id as string)!.idx
116
                item.idx = idx
117
                itemsToReplace.push(item)
118
            })
119

  
120
            // Iterate over items to replace and replace them
121
            const newPath = [...path]
122
            itemsToReplace.forEach((item) => {
123
                newPath[item.idx] = item
124
            })
125

  
126
            // Add items to the end
127
            itemsToAdd.forEach((item) => {
128
                item.active = false
129
                item.idx = newPath.length
130
                newPath.push(item)
131
            })
132

  
133
            // Return the new path
134
            return {
135
                ...state,
136
                pathVariants: [
137
                    ...pathVariants.slice(0, primaryPathIdx),
138
                    newPath,
139
                    ...pathVariants.slice(primaryPathIdx + 1),
140
                ],
141
                primaryPathIdx, // in case the list is empty
142
                currentPage, // in case the list is empty
143
            }
144
        }
97 145
    },
98 146
    extraReducers: (builder) => {
99 147
        builder.addCase(sendTextForProcessing.fulfilled, (state, action) => {
......
128 176
    },
129 177
})
130 178

  
131
export const { consumeErr, setPrimaryIdx, resetDialogApiCallSuccess, clear, updateMapMarker } =
179
export const { consumeErr, setPrimaryIdx, resetDialogApiCallSuccess, clear, updateMapMarker, mergeWithCurrentPath } =
132 180
    trackingToolSlice.actions
133 181
const trackingToolReducer = trackingToolSlice.reducer
134 182
export default trackingToolReducer

Také k dispozici: Unified diff