Projekt

Obecné

Profil

Stáhnout (4.43 KB) Statistiky
| Větev: | Tag: | Revize:
1
import {
2
    Button,
3
    Collapse,
4
    Divider,
5
    Grid,
6
    Stack,
7
    TextField,
8
} from '@mui/material'
9
import { Fragment } from 'react'
10
import { useDispatch, useSelector } from 'react-redux'
11
import { setFilter, setFilterOpen } from './catalogSlice'
12
import { fetchItems } from './catalogThunks'
13
import FilterListIcon from '@mui/icons-material/FilterList'
14
import FilterListOffIcon from '@mui/icons-material/FilterListOff'
15
import ManageSearchIcon from '@mui/icons-material/ManageSearch'
16
import { RootState } from '../redux/store'
17

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

    
21
    const filterOpen = useSelector((state: RootState) => state.catalog.filterOpen)
22
    const toggleFilter = () => {
23
        dispatch(setFilterOpen(!filterOpen))
24
    }
25

    
26
    // current filter object
27
    const filter = useSelector((state: RootState) => state.catalog.filter)
28
    const applyFilter = () => {
29
        dispatch(fetchItems())
30
    }
31

    
32
    return (
33
        <Fragment>
34
            <Button
35
                startIcon={
36
                    filterOpen ? <FilterListOffIcon /> : <FilterListIcon />
37
                }
38
                // variant="outlined"
39
                color="primary"
40
                onClick={toggleFilter}
41
            >
42
                Filter
43
            </Button>
44
            <Collapse in={filterOpen} timeout="auto" unmountOnExit>
45
                <Grid container spacing={1} alignItems="stretch">
46
                    <Grid item xs={6}>
47
                        <Stack direction="column" spacing={1} sx={{ mt: 2 }}>
48
                            <Stack direction="row" spacing={2}>
49
                                <TextField
50
                                    size="small"
51
                                    id="name"
52
                                    label="Name"
53
                                    onChange={(e: any) => {
54
                                        filter.name = e.target.value
55
                                        dispatch(setFilter(filter))
56
                                    }}
57
                                    value={filter.name}
58
                                />
59
                                <TextField
60
                                    size="small"
61
                                    id="type"
62
                                    label="Type"
63
                                    onChange={(e: any) => {
64
                                        filter.type = e.target.value
65
                                        dispatch(setFilter(filter))
66
                                    }}
67
                                    value={filter.type}
68
                                />
69
                            </Stack>
70
                            <Stack direction="row" spacing={2}>
71
                                <TextField
72
                                    size="small"
73
                                    id="writtenForm"
74
                                    label="Written form"
75
                                />
76
                                <TextField
77
                                    size="small"
78
                                    id="stateOrTerritory"
79
                                    label="State or territory"
80
                                    onChange={(e: any) => {
81
                                        filter.country = e.target.value
82
                                        dispatch(setFilter(filter))
83
                                    }}
84
                                    value={filter.country}
85
                                />
86
                                <TextField
87
                                    size="small"
88
                                    id="groupBy"
89
                                    label="Group by"
90
                                />
91
                            </Stack>
92
                        </Stack>
93
                    </Grid>
94
                    <Grid item xs sx={{ mt: 'auto', ml: 1, mb: 1 }}>
95
                        <Stack
96
                            direction="row"
97
                            justifyContent="flex-start"
98
                            alignItems="flex-end"
99
                        >
100
                            <Button startIcon={<ManageSearchIcon/>} variant="contained" onClick={applyFilter}>
101
                                Search
102
                            </Button>
103
                        </Stack>
104
                    </Grid>
105
                </Grid>
106
            </Collapse>
107
            {filterOpen ? <Divider sx={{ mb: 5, mt: 1 }} /> : null}
108
        </Fragment>
109
    )
110
}
111

    
112
export default CatalogFilter
(2-2/7)