Projekt

Obecné

Profil

« Předchozí | Další » 

Revize e9103a47

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

Persist navigation state

re #9630

Zobrazit rozdíly:

frontend/src/features/Catalog/AddNewLocationDialog.tsx
1
import { Button } from "@mui/material"
2
import { Fragment } from "react"
3
import AddIcon from '@mui/icons-material/Add'
4

  
5
const AddNewLocationDialog = () => {
6

  
7
    return (
8
        <Fragment>
9
            <Button startIcon={<AddIcon />} variant="outlined">Add Location</Button>
10
        </Fragment>
11
    )
12
}
13

  
14
export default AddNewLocationDialog
frontend/src/features/Catalog/Catalog.tsx
1
import { Container, Paper, Typography } from '@mui/material'
1
import { Container, Paper, Stack, Typography } from '@mui/material'
2 2
import CatalogTable from './CatalogTable'
3 3
import { Fragment } from 'react'
4 4
import CatalogFilter from './CatalogFilter'
......
15 15
            // variant="outlined"
16 16
            style={{ minHeight: '50vh' }}
17 17
        >
18
            <Container sx={{ mt: 4 }}>
19
                <CatalogFilter />
18
            <Stack sx={{ mx: 12, my: 2 }}>
19
                <Stack alignItems="start">
20
                    <CatalogFilter />
21
                </Stack>
20 22
                <CatalogTable />
21
            </Container>
23
            </Stack>
22 24
        </Paper>
23 25
    </Fragment>
24 26
)
frontend/src/features/Catalog/CatalogFilter.tsx
14 14
import FilterListOffIcon from '@mui/icons-material/FilterListOff'
15 15
import ManageSearchIcon from '@mui/icons-material/ManageSearch'
16 16
import { RootState } from '../redux/store'
17
import AddNewLocationDialog from './AddNewLocationDialog'
17 18

  
18 19
const CatalogFilter = () => {
19 20
    const dispatch = useDispatch()
......
33 34

  
34 35
    return (
35 36
        <Fragment>
36
            <Button
37
                startIcon={
38
                    filterOpen ? <FilterListOffIcon /> : <FilterListIcon />
39
                }
40
                variant="outlined"
41
                color="primary"
42
                onClick={toggleFilter}
43
                sx={{ mb: 2 }}
44
            >
45
                Filter
46
            </Button>
37
            <Stack direction="row" spacing={2} sx={{ mb: 2 }}>
38
                <Button
39
                    startIcon={
40
                        filterOpen ? <FilterListOffIcon /> : <FilterListIcon />
41
                    }
42
                    variant="outlined"
43
                    color="primary"
44
                    onClick={toggleFilter}
45
                >
46
                    Filter
47
                </Button>
48
                <AddNewLocationDialog />
49
            </Stack>
47 50
            <Collapse in={filterOpen} timeout="auto" unmountOnExit>
48 51
                <Stack direction="row" spacing={2} sx={{ mt: 2 }}>
49 52
                    <TextField
frontend/src/features/Navigation/Navigation.tsx
17 17

  
18 18
import DarkModeIcon from '@mui/icons-material/DarkMode'
19 19
import LightModeIcon from '@mui/icons-material/LightMode'
20
import { setOpen } from './navigationSlice'
20 21

  
21 22
const drawerWidth = 240
22 23

  
......
74 75
}
75 76

  
76 77
const PersistentDrawerLeft: FunctionComponent<DrawerProps> = ({ children }) => {
77
    const [open, setOpen] = React.useState(false)
78
    const open = useSelector((state: RootState) => state.navigation.open)
78 79

  
79 80
    const onOpenDrawer = () => {
80
        setOpen(true)
81
        dispatch(setOpen(true))
81 82
    }
82 83

  
83 84
    const colorThemeMode = useSelector(
......
92 93

  
93 94
    return (
94 95
        <Fragment>
95
            <Paper style={{ minHeight: '100vh', borderRadius: 0 }}>
96
            <Paper style={{ minHeight: '100vh', borderRadius: 0}}>
96 97
                <Box sx={{ display: 'flex' }}>
97 98
                    {/* <CssBaseline /> */}
98 99
                    <AppBar position="fixed" open={open}>
......
125 126
                    <NavigationMenu
126 127
                        open={open}
127 128
                        drawerWidth={drawerWidth}
128
                        setOpen={setOpen}
129
                        setOpen={(open: boolean) => dispatch(setOpen(open))}
129 130
                    />
130 131
                    <Main open={open} sx={{ mt: 2 }}>
131 132
                        <DrawerHeader />
frontend/src/features/Navigation/navigationSlice.tsx
1
import { createSlice } from '@reduxjs/toolkit'
2
import persistReducer from 'redux-persist/es/persistReducer'
3
import storage from 'redux-persist/lib/storage'
4

  
5
export interface NavigationState {
6
    selectedMenuItem?: string,
7
    open: boolean,
8
}
9

  
10
const persistConfig = {
11
    key: 'navigation',
12
    storage
13
}
14

  
15
const initialState = {
16
    selectedMenuItem: '',
17
    open: false
18
}
19

  
20
export const navigationSlice = createSlice({
21
    name: 'navigation',
22
    initialState,
23
    reducers: {
24
        setSelectedMenuItem: (state, action) => ({...state, selectedMenuItem: action.payload}),
25
        setOpen: (state, action) => ({...state, open: action.payload}),
26
    }
27
})
28

  
29
const navigationReducer = persistReducer(persistConfig, navigationSlice.reducer)
30

  
31
export const { setSelectedMenuItem, setOpen } = navigationSlice.actions
32

  
33
export default navigationReducer
frontend/src/features/Reusables/ButtonOpenableDialog.tsx
12 12
    maxWidth?: 'xs' | 'sm' | 'md' | 'lg' // the max width of the dialog
13 13
}
14 14

  
15

  
15 16
// Generic dialog that can be opened by a button and closed by clicking on the backdrop.
16 17
const ButtonOpenableDialog: FunctionComponent<ButtonOpenableDialogProps> = ({
17 18
    onOpenCallback,
......
52 53
                {buttonText}
53 54
            </Button>
54 55
            <Dialog
55
                fullWidth={true}
56
                fullWidth
56 57
                open={open}
57 58
                onClose={onClose}
58 59
                maxWidth={maxWidth}
frontend/src/features/redux/store.ts
8 8
import notificationReducer from '../Notification/notificationSlice'
9 9
import trackingToolReducer from '../TrackingTool/trackingToolSlice'
10 10
import { enableMapSet } from 'immer'
11
import navigationReducer from '../Navigation/navigationSlice'
11 12

  
12 13
enableMapSet()
13 14

  
......
20 21
        theme: themeReducer,
21 22
        catalog: catalogReducer,
22 23
        notification: notificationReducer,
23
        trackingTool: trackingToolReducer
24
        trackingTool: trackingToolReducer,
25
        navigation: navigationReducer,
24 26
    }),
25 27
    process.env.REACT_APP_DEV_ENV === 'true'
26
        ? composeEnhancers( // ComposeEnhancers will inject redux-devtools-extension
28
        ? composeEnhancers(
29
              // ComposeEnhancers will inject redux-devtools-extension
27 30
              applyMiddleware(thunk) // Thunk middleware so we can async fetch data from the api
28 31
          )
29 32
        : applyMiddleware(thunk)

Také k dispozici: Unified diff