Filters.jsx 16.4 KB
Newer Older
1
2
import React from 'react';
import {
3
    Checkbox, FormControlLabel, FormGroup, FormLabel, Grid, IconButton, Radio, RadioGroup, Switch, TextField, Tooltip
4
5
6
} from '@material-ui/core';
import Autocomplete from '@material-ui/lab/Autocomplete';
import ClearIcon from "@material-ui/icons/Clear";
7
8
import { useStyles } from '../../styles';
import { useTranslation } from "react-i18next";
9
10
11


export const Filters = (props) => {
12
    const [input, dispatch] = props.reducer;
13
    const { chronOntologyTerms, regions } = props;
14
15
16
17

    const { t, i18n } = useTranslation();

    const classes = useStyles();
18

19
20
21
22
23
24
25
26
27
28
29
    const updateBBoxValue = (event) => {
        dispatch({type: "UPDATE_INPUT", payload: {field: "sitesMode", value: "bbox"}});
        // only create bbox if entered values have valid format (floats with at least one decimal place)
        if(/-?\d{1,2}\.\d+,-?\d{1,3}\.\d+/.test(event.currentTarget.value)) {
            dispatch({type: "MANUAL_BBOX", payload: {field: event.currentTarget.name, valueString: event.currentTarget.value}})
        }
        else {
            dispatch({type: "UPDATE_INPUT", payload: {field: event.currentTarget.name, value: event.currentTarget.value}})
        }
    }

30
31

    return (
32
33
        <>
            {<Grid /*className={classes.dashboardTileContent}*/ item container direction="column" spacing={2}>
34
                <Grid item container direction="row" spacing={2}>
35
                    {/*radio buttons for selecting mode*/
36
                        <Grid item>
37
38
39
40
41
42
43
44
45
46
                            <FormGroup>
                                <FormLabel component="legend">Search mode</FormLabel>
                                <RadioGroup
                                    name="mapMode"
                                    value={input.mode}
                                    onChange={(event, newValue) => {
                                        dispatch({type: "UPDATE_INPUT", payload: {field: "mode", value: newValue}})
                                        dispatch({type: "TOGGLE_STATE", payload: {toggledField: "showArchaeoSites"}})
                                        dispatch({type: "TOGGLE_STATE", payload: {toggledField: "showSearchResults"}})
                                        dispatch({type: "UPDATE_INPUT", payload: {field: "selectedMarker", value: undefined}}) // Is this a good place to clear selectedMarker?
47
48
                                    }}
                                >
49
50
51
52
53
                                    <FormControlLabel value="archaeoSites" control={<Radio />} label="Archaeological Sites"/>
                                    <FormControlLabel value="objects" control={<Radio />} label="Objects"/>
                                </RadioGroup>
                            </FormGroup>
                        </Grid>}
54

55
                    {/*input field for string query*/
56
                        <Grid item>
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
                            <FormGroup>
                                <FormLabel component="legend">Filter by search term</FormLabel>
                                <TextField
                                    type="text"
                                    variant="outlined"
                                    size="small"
                                    name="searchStr"
                                    value={input.searchStr}
                                    placeholder="*"
                                    onChange={event => dispatch({type: "UPDATE_INPUT", payload: {field: event.currentTarget.name, value: event.currentTarget.value}})}
                                    InputProps={{
                                        endAdornment: (
                                            input.searchStr!==""
                                            &&<IconButton
                                                onClick={() => {
                                                    dispatch({type: "UPDATE_INPUT", payload: {field: "searchStr", value: ""}});
                                                }}
                                            >
                                                <ClearIcon />
                                            </IconButton>
                                        )
                                    }}
                                />
                            </FormGroup>
                        </Grid>}
82

83
                    {/*dropdown for filter by period; only active in object search mode*/
84
                        !input.showArchaeoSites && <Grid item>
85
86
87
88
89
90
91
92
                            <FormGroup>
                                <FormLabel component="legend" disabled={input.showArchaeoSites}>Filter by time</FormLabel>
                                <Autocomplete
                                    name="chronOntologyTerm"
                                    value={input.chronOntologyTerm}
                                    options={chronOntologyTerms}
                                    onChange={(event, newValue) =>
                                        dispatch({type: "UPDATE_INPUT", payload: {field: "chronOntologyTerm", value: newValue}})
93
                                    }
94
95
96
97
98
99
100
101
102
103
104
                                    renderInput={(params) =>
                                        <TextField {...params} label="iDAI.chronontology term" variant="outlined" />
                                    }
                                    autoSelect={true}
                                    disabled={input.showArchaeoSites}
                                    size="small"
                                />
                            </FormGroup>
                        </Grid>}

                    {/*dropdown for filter by region; only active in site search mode*/
105
                        input.showArchaeoSites && <Grid item>
106
107
108
109
110
111
112
113
114
                            <FormGroup>
                                <FormLabel component="legend">Filter by region</FormLabel>
                                <Autocomplete
                                    name="regionId"
                                    //value={input.regionTitle}
                                    options={regions}
                                    getOptionLabel={(option) => option.title}
                                    getOptionSelected={(option, value) => {
                                        return (option.id === value.id)
115
                                    }}
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
                                    onChange={(event, newValue) => {
                                        dispatch({type: "UPDATE_INPUT", payload: {field: "sitesMode", value: "region"}});
                                        newValue === null
                                            ? (dispatch({type: "UPDATE_INPUT", payload: {field: "sitesMode", value: ""}}),
                                                dispatch({type: "UPDATE_INPUT", payload: {field: "regionId", value: 0}}),
                                                dispatch({type: "UPDATE_INPUT", payload: {field: "regionTitle", value: null}}))
                                            : (dispatch({type: "UPDATE_INPUT", payload: {field: "regionId", value: newValue.id}}),
                                                dispatch({type: "UPDATE_INPUT", payload: {field: "regionTitle", value: newValue.title}}));
                                    }}
                                    renderInput={(params) => <TextField {...params} label="Filter by region" variant="outlined" />}
                                    autoSelect={true}
                                    disabled={input.sitesMode==="bbox"}
                                    size="small"
                                />
                            </FormGroup>
                        </Grid>}
132

133
                    {/*switch and input fields for filter by coordinates*/
134
                        <Grid item>
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
                            <FormGroup>
                                <FormLabel>Filter by coordinates (bounding box)
                                    <Tooltip title="Activate the switch to select a bounding box directly on the map. Click the map in two places to select first the north-east corner, then the south-west corner." arrow placement="right-start">
                                        <Switch
                                            name="drawBBox"
                                            checked={input.drawBBox}
                                            color="primary"
                                            onChange={() => dispatch({type: "TOGGLE_STATE", payload: {toggledField: "drawBBox"}})}
                                            disabled={input.sitesMode==="region"}
                                        />
                                    </Tooltip>
                                </FormLabel>
                                <TextField
                                    type="text"
                                    variant="outlined"
                                    size="small"
                                    name="boundingBoxCorner1"
                                    value={input.boundingBoxCorner1}
                                    placeholder="North, East decimal degrees"
                                    label="North, East decimal degrees"
                                    /*onChange={(event) => {
                                        dispatch({type: "UPDATE_INPUT", payload: {field: "sitesMode", value: "bbox"}});
                                        // only create bbox if entered values have valid format (floats with at least one decimal place)
                                        if(/-?\d{1,2}\.\d+,-?\d{1,3}\.\d+/.test(event.currentTarget.value)) {
                                            dispatch({type: "MANUAL_BBOX", payload: {field: event.currentTarget.name, valueString: event.currentTarget.value}})
                                        }
                                        else {
                                            dispatch({type: "UPDATE_INPUT", payload: {field: event.currentTarget.name, value: event.currentTarget.value}})
                                        }
                                    }}*/
                                    onChange={updateBBoxValue}
                                    InputProps={{
                                        endAdornment: (
                                            input.boundingBoxCorner1.length!==0
                                            &&<IconButton
                                                onClick={() => {
                                                    dispatch({type: "UPDATE_INPUT", payload: {field: "sitesMode", value: ""}});
                                                    dispatch({type: "UPDATE_INPUT", payload: {field: "boundingBoxCorner1", value: []}})}
173
                                                }
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
                                            >
                                                <ClearIcon />
                                            </IconButton>
                                        )
                                    }}
                                    disabled={input.sitesMode==="region"}
                                />
                                <TextField
                                    type="text"
                                    variant="outlined"
                                    size="small"
                                    name="boundingBoxCorner2"
                                    value={input.boundingBoxCorner2}
                                    placeholder="South, West decimal degrees"
                                    label="South, West decimal degrees"
                                    /*onChange={(event) => {
                                        dispatch({type: "UPDATE_INPUT", payload: {field: "sitesMode", value: "bbox"}});
                                        // only create bbox if entered values have valid format (floats with at least one decimal place)
                                        if(/-?\d{1,2}\.\d+,-?\d{1,3}\.\d+/.test(event.currentTarget.value)) {
                                            dispatch({type: "MANUAL_BBOX", payload: {field: event.currentTarget.name, valueString: event.currentTarget.value}})
                                        }
                                        else {
                                            dispatch({type: "UPDATE_INPUT", payload: {field: event.currentTarget.name, value: event.currentTarget.value}})
                                        }
                                    }}*/
                                    onChange={updateBBoxValue}
                                    InputProps={{
                                        endAdornment: (
                                            input.boundingBoxCorner2.length!==0
                                            &&<IconButton
                                                onClick={() => {
                                                    dispatch({type: "UPDATE_INPUT", payload: {field: "sitesMode", value: ""}});
                                                    dispatch({type: "UPDATE_INPUT", payload: {field: "boundingBoxCorner2", value: []}})
                                                }}
                                            >
                                                <ClearIcon />
                                            </IconButton>
                                        )
                                    }}
                                    disabled={input.sitesMode==="region"}
                                />
                            </FormGroup>
                        </Grid>}
217

218
                    {/*checkboxes for filter by catalogs; only active in object search mode*/
219
                        !input.showArchaeoSites && <Grid item>
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
                            <FormGroup>
                                <FormLabel component="legend" disabled={input.showArchaeoSites}>Filter by catalogs</FormLabel>
                                {input.catalogIdsList && input.catalogIdsList.map(project => {
                                    return (project
                                        && <FormControlLabel
                                            key={project.catalogId}
                                            control={
                                                <Checkbox
                                                    checked={input.checkedCatalogIds.includes(project.catalogId)}
                                                    onChange={() => {
                                                        dispatch({
                                                            type: input.checkedCatalogIds.includes(project.catalogId)
                                                                ? "UNCHECK_ITEM"
                                                                : "CHECK_ITEM",
                                                            payload: {field: "checkedCatalogIds", toggledItem: project.catalogId}
                                                        });
                                                        dispatch({
                                                            type: "UPDATE_INPUT",
                                                            payload: {
                                                                field: "checkedCatalogLabels",
                                                                value: input.checkedCatalogLabels.includes(project.catalogLabel)
                                                                    ? [...input.checkedCatalogLabels.filter(catalogLabel => catalogLabel !== project.catalogLabel)]
                                                                    : [...input.checkedCatalogLabels, project.catalogLabel]
                                                            }
                                                        })
                                                    }}
                                                    name={String(project.catalogId)}
                                                    key={project.catalogId}
                                                    disabled={input.showArchaeoSites}
                                                />
                                            }
                                            label={project.catalogLabel}
                                        />
                                    )
                                })}
                            </FormGroup>
                        </Grid>}
                </Grid>
            </Grid>}
259
260
        </>
    );
261
};