Projekt

Obecné

Profil

« Předchozí | Další » 

Revize fc8a530f

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

Pagination

re #9741

Zobrazit rozdíly:

frontend/src/features/TrackingTool/Controls/PathPagination.tsx
1
import { Pagination } from '@mui/material'
2
import { ChangeEvent, Fragment } from 'react'
3
import { useDispatch, useSelector } from 'react-redux'
4
import { RootState } from '../../redux/store'
5
import { setDisplayedPathIdx } from '../trackingToolSlice'
6

  
7
const PathPagination = () => {
8
    const dispatch = useDispatch()
9
    const pathVariants = useSelector(
10
        (state: RootState) => state.trackingTool.pathVariants
11
    )
12
    const currentPathIdx = useSelector(
13
        (state: RootState) => state.trackingTool.displayedPathIdx
14
    )
15

  
16
    const onPageChange = (event: ChangeEvent<unknown>, value: number) => {
17
        dispatch(setDisplayedPathIdx(value))
18
    }
19

  
20
    return (
21
        <Fragment>
22
            {pathVariants && (
23
                <Pagination
24
                    count={pathVariants.length}
25
                    page={currentPathIdx + 1}
26
                    onChange={onPageChange}
27
                    color="primary"
28
                />
29
            )}
30
        </Fragment>
31
    )
32
}
33

  
34
export default PathPagination
frontend/src/features/TrackingTool/Map/Map.tsx
7 7
import { buildTheme } from '../../Theme/ThemeWrapper'
8 8
import RightClickPopupMenu from '../Import/ImportContextMenu'
9 9
import MapPath from './MapPath'
10
import { Map as LeafletMap } from 'leaflet'
10
import L, { Map as LeafletMap } from 'leaflet'
11
import {  } from 'leaflet.fullscreen'
11 12

  
12 13
const mapTheme = buildTheme('light')
13 14

  
......
18 19
    const mapRef = useRef<LeafletMap | undefined>(undefined)
19 20
    useEffect(() => {
20 21
        if (!mapRef || !mapRef.current) {
21
            console.log('No map ref')
22 22
            return
23 23
        }
24 24

  
frontend/src/features/TrackingTool/TrackingTool.tsx
12 12
import MapPointToggleables from './Controls/MapPointToggleables'
13 13
import GeoJsonImportDialog from './Import/GeoJsonImportDialog'
14 14
import Map from './Map/Map'
15
import MapPointDraggableList from './Controls/MapPointDraggableList'
16
import PathPagination from './Controls/PathPagination'
15 17

  
16 18
// Page with tracking tool
17 19
const TrackingTool = () => {
......
25 27
        (state: RootState) => state.trackingTool.displayedPath
26 28
    )
27 29

  
30
    const pathVariants = useSelector(
31
        (state: RootState) => state.trackingTool.pathVariants
32
    )
33

  
28 34
    // Consume any error
29 35
    const dispatch = useDispatch()
30 36
    const err = useSelector((state: RootState) => state.trackingTool.lastError)
......
61 67
                            Looks like no path / catalog items match this query.
62 68
                        </Typography>
63 69
                    )}
70
                    {pathVariants && pathVariants.length > 0 && (
71
                        <PathPagination />
72
                    )}
64 73
                    <Stack
65 74
                        direction="row"
66 75
                        alignItems="flex-start"
frontend/src/features/TrackingTool/trackingToolSlice.ts
93 93
                displayedPath: newPath,
94 94
            }
95 95
        },
96
        // Updates currently used path - payload must be a number indexed from 1 since this is default behavior
97
        // for MUI Pagination
98
        // This function should only ever be called if there is some displayed path
99
        setDisplayedPathIdx: (state: TrackingToolState, action: { payload: number }) => {
100
            const newIdx = action.payload - 1 // subtract one to make index start from 0
101
            const pathVariants = [...state.pathVariants ?? []]
102
            const previousIdx = state.displayedPathIdx
103
            if (pathVariants.length < newIdx || newIdx < 0) {
104
                return state
105
            }
106

  
107
            // We will always have displayedPath defined if this function is called
108
            pathVariants.splice(previousIdx, 0, [...state.displayedPath ?? []])
109
            const newPath = pathVariants.splice(newIdx, 1)[0]
110
            return {
111
                ...state,
112
                displayedPath: newPath,
113
                displayedPathIdx: newIdx,
114
                pathVariants,
115
            }
116
        },
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
        }
96 167
    },
97 168
    extraReducers: (builder) => {
98 169
        builder.addCase(sendTextForProcessing.fulfilled, (state, action) => {
99 170
            const pathDto: PathDto = action.payload
100 171
            const pathVariants = buildPathVariants(pathDto)
101 172
            const selectedPath = pathVariants.length > 0 ? pathVariants[0] : undefined
173
            pathVariants.splice(0, 1) // remove the selected path so that we do not have to update it here
102 174
            if (!selectedPath) {
103 175
                return {
104 176
                    ...state,
......
153 225
    moveMarkerToDestination,
154 226
    clear,
155 227
    mergeWithCurrentPath,
228
    setDisplayedPathIdx,
156 229
} = trackingToolSlice.actions
157 230

  
158 231
const trackingToolReducer = trackingToolSlice.reducer

Také k dispozici: Unified diff