Projekt

Obecné

Profil

Stáhnout (12.9 KB) Statistiky
| Větev: | Tag: | Revize:
1 e49b1f44 Schwobik
import {
2
    Box,
3 4da7b143 Schwobik
    Button,
4
    ChevronDownIcon,
5
    ChevronUpIcon,
6
    CloseIcon,
7
    HStack,
8
    Input,
9
    Text,
10 e49b1f44 Schwobik
    VStack
11
} from "native-base"
12 9c55d3bb Schwobik
import { useDispatch, useSelector } from "react-redux"
13 4da7b143 Schwobik
import { useCallback, useEffect, useState } from "react"
14 9c55d3bb Schwobik
import { AppDispatch, RootState } from "../../stores/store"
15
import { Inventory } from "../../types/searchFormTypes"
16
import { search } from "../../stores/actions/listViewThunks"
17 e49b1f44 Schwobik
import MultiSelect from "./MultiSelect"
18
import SwitchWithLabel from "./SwitchWithLabel"
19
import { log } from "../../logging/logger"
20 ab632fe5 Michal Schwob
import {resetInventories, setFilterState} from "../../stores/reducers/listViewSlice"
21 ca44ce3d Schwobik
import { SearchParams } from "../../api/searchService"
22 9c55d3bb Schwobik
23 e49b1f44 Schwobik
interface SearchFormProps {
24 4da7b143 Schwobik
    inventoryId: string | null
25 e49b1f44 Schwobik
}
26 9c55d3bb Schwobik
27 e49b1f44 Schwobik
28
const SearchForm = (props: SearchFormProps) => {
29 4da7b143 Schwobik
    const [isFilterOpen, setIsFilterOpen] = useState<boolean>(true)
30 ab632fe5 Michal Schwob
    const loading = useSelector((state: RootState) => state.listView.loading)
31 4da7b143 Schwobik
32 9c55d3bb Schwobik
    const inventories: Inventory[] = useSelector((state: RootState) => state.searchForm.inventories)
33
    const nationalities = useSelector((state: RootState) => state.searchForm.nationalities)
34
    const artists = useSelector((state: RootState) => state.searchForm.artists)
35
    const subjects = useSelector((state: RootState) => state.searchForm.subjects)
36
    const rooms = useSelector((state: RootState) => state.searchForm.rooms)
37 e49b1f44 Schwobik
    const techniques = useSelector((state: RootState) => state.searchForm.techniques)
38 9c55d3bb Schwobik
    const countries = useSelector((state: RootState) => state.searchForm.countries)
39
    const cities = useSelector((state: RootState) => state.searchForm.cities)
40
    const institutions = useSelector((state: RootState) => state.searchForm.institutions)
41
42 e49b1f44 Schwobik
    const [selectedInventories, setSelectedInventories] = useState<{ label: string, value: string }[]>([])
43
    const [selectedNationalities, setSelectedNationalities] = useState<{ label: string, value: string }[]>([])
44
    const [selectedArtists, setSelectedArtists] = useState<{ label: string, value: string }[]>([])
45
    const [selectedSubjects, setSelectedSubjects] = useState<{ label: string, value: string }[]>([])
46
    const [selectedRooms, setSelectedRooms] = useState<{ label: string, value: string }[]>([])
47
    const [selectedTechniques, setSelectedTechniques] = useState<{ label: string, value: string }[]>([])
48
    const [selectedCountries, setSelectedCountries] = useState<{ label: string, value: string }[]>([])
49
    const [selectedCities, setSelectedCities] = useState<{ label: string, value: string }[]>([])
50
    const [selectedInstitutions, setSelectedInstitutions] = useState<{ label: string, value: string }[]>([])
51 1980ed09 Schwobik
    const [searchQuery, setSearchQuery] = useState<string>("")
52 9c55d3bb Schwobik
53 e49b1f44 Schwobik
    const [isSchoolOfPrague, setIsSchoolOfPrague] = useState(false)
54
    const [isOriginal, setIsOriginal] = useState(false)
55
    const [isCopy, setIsCopy] = useState(false)
56
    const [isHighQuality, setIsHighQuality] = useState(false)
57
    const [isLowQuality, setIsLowQuality] = useState(false)
58 9c55d3bb Schwobik
59
    const dispatch = useDispatch<AppDispatch>()
60
61 e49b1f44 Schwobik
    const searchSubmit = () => {
62 ca44ce3d Schwobik
        const filterState: SearchParams = {
63 e49b1f44 Schwobik
            inventories: selectedInventories.map(i => i.value),
64
            nationalities: selectedNationalities.map(n => n.value),
65
            artists: selectedArtists.map(a => a.value),
66
            subjects: selectedSubjects.map(s => s.value),
67
            rooms: selectedRooms.map(r => r.value),
68
            techniques: selectedTechniques.map(t => t.value),
69
            countries: selectedCountries.map(c => c.value),
70
            cities: selectedCities.map(c => c.value),
71
            institutions: selectedInstitutions.map(i => i.value),
72
            isSchoolOfPrague,
73
            isOriginal,
74
            isCopy,
75
            isHighQuality,
76 1980ed09 Schwobik
            isLowQuality,
77
            searchQuery: searchQuery,
78 ca44ce3d Schwobik
        }
79 ab632fe5 Michal Schwob
        dispatch(resetInventories())
80 ca44ce3d Schwobik
        dispatch(setFilterState(filterState))
81
        dispatch(search(filterState))
82 4da7b143 Schwobik
        setIsFilterOpen(false)
83 e49b1f44 Schwobik
    }
84 9c55d3bb Schwobik
85 e49b1f44 Schwobik
    const clearForm = () => {
86
        log.debug("SearchForm", "clearForm")
87 9c55d3bb Schwobik
88 e49b1f44 Schwobik
        setSelectedInventories([])
89
        setSelectedNationalities([])
90
        setSelectedArtists([])
91
        setSelectedSubjects([])
92
        setSelectedRooms([])
93
        setSelectedTechniques([])
94
        setSelectedCountries([])
95
        setSelectedCities([])
96
        setSelectedInstitutions([])
97 1980ed09 Schwobik
        setSearchQuery("")
98 5f3c7202 Michal Schwob
99
        // reset switches
100
        setIsSchoolOfPrague(false)
101
        setIsOriginal(false)
102
        setIsCopy(false)
103
        setIsHighQuality(false)
104
        setIsLowQuality(false)
105 1980ed09 Schwobik
    }
106
107 4da7b143 Schwobik
    const getSearchHeader = useCallback(() => {
108
        if (selectedInventories && selectedInventories.length === 1) {
109
            return `Search: ${ selectedInventories[0].label }`
110
        } else if (selectedRooms && selectedRooms.length === 1) {
111
            return `Search: ${ selectedRooms[0].label }`
112 1980ed09 Schwobik
        } else {
113
            return "Search:"
114
        }
115 cbf81c55 Schwobik
    }, [selectedInventories, selectedRooms])
116 4da7b143 Schwobik
117
    useEffect(() => {
118
        log.debug("SearchForm", "useEffect", "props.inventoryId", props.inventoryId)
119
        if (props.inventoryId) {
120
            clearForm()
121
            setIsFilterOpen(false)
122
            setSelectedInventories([{ label: props.inventoryId, value: props.inventoryId }])
123
            dispatch(setFilterState({ inventories: [props.inventoryId] }))
124
            dispatch(search({ inventories: [props.inventoryId] }))
125
        }
126
    }, [props.inventoryId])
127 9c55d3bb Schwobik
128
    return (
129 e49b1f44 Schwobik
        <>
130 1980ed09 Schwobik
            <Text
131
                fontSize={ "2xl" }
132
                fontWeight={ "bold" }
133
                color={ "primary.500" }
134
            >
135
                { getSearchHeader() }
136
            </Text>
137
            <Input
138
                placeholder="Search"
139
                size={ "md" }
140
                variant="underlined"
141
                value={searchQuery}
142
                onChangeText={ setSearchQuery }
143
            />
144
            <HStack justifyContent={"space-between"}>
145
                <Button
146 4da7b143 Schwobik
                    onPress={ () => setIsFilterOpen(!isFilterOpen) }
147 1980ed09 Schwobik
                    variant="outline"
148
                    endIcon={
149
                        <>
150 4da7b143 Schwobik
                            { isFilterOpen ?
151 1980ed09 Schwobik
                                (<ChevronUpIcon size={ 4 } color={ "primary.500" }/>)
152
                                : (<ChevronDownIcon size={ 4 } color={ "primary.500" }/>) }
153
                        </>
154
                    }
155
                    flexWrap={ "nowrap" }
156
                    borderColor={ "primary.500" }
157
                    size={ "sm" }
158
                    mt={ 2 }
159
                    p={ 2 }
160
161
                >
162
                    Filter
163
                </Button>
164 4da7b143 Schwobik
                { !isFilterOpen && (
165 1980ed09 Schwobik
                    <Button
166 ab632fe5 Michal Schwob
                        isLoading={ loading }
167 1980ed09 Schwobik
                        borderRadius={ 10 }
168
                        onPress={ () => searchSubmit() }
169
                        colorScheme="primary"
170
                        background={ "primary.500" }
171
                        variant="solid"
172
                        mt={2}
173
                        p={2}
174
                        size={ "md" }
175
                    >
176
                        Search
177
                    </Button>
178
                )}
179
            </HStack>
180 4da7b143 Schwobik
            { isFilterOpen && (
181 e49b1f44 Schwobik
                <>
182
                    <MultiSelect
183
                        data={ inventories.map((inventory, index) => {
184
                            return {label: inventory.label, value: inventory.name, index}
185
                        }) }
186
                        label="Inventory (OR)"
187
                        selectedItems={ selectedInventories }
188
                        onSelectedItemsChange={ setSelectedInventories }
189
                    />
190
                    <MultiSelect
191
                        data={ rooms.map((room, index) => {
192
                            return {label: `${ room.id } - ${ room.label }`, value: room.id.toString(), index}
193
                        }) }
194
                        label="Rooms (OR)"
195
                        selectedItems={ selectedRooms }
196
                        onSelectedItemsChange={ setSelectedRooms }
197
                    />
198
                    <MultiSelect
199
                        data={ artists.map((art, index) => {
200
                            return {label: art.display_name, value: art.getty_id, index}
201
                        }) }
202
                        label="Artists/Copyists (OR)"
203
                        selectedItems={ selectedArtists }
204
                        onSelectedItemsChange={ setSelectedArtists }
205
                    />
206
                    <MultiSelect
207
                        data={ nationalities.map((nat, index) => {
208
                            return {label: nat, value: nat, index}
209
                        }) }
210
                        label="Artist`s Origin (OR)"
211
                        selectedItems={ selectedNationalities }
212
                        onSelectedItemsChange={ setSelectedNationalities }
213
                    />
214
                    <MultiSelect
215
                        data={ subjects.map((subj, index) => {
216
                            return {label: subj, value: subj, index}
217
                        }) }
218
                        label="Subject (AND)"
219
                        selectedItems={ selectedSubjects }
220
                        onSelectedItemsChange={ setSelectedSubjects }
221
                    />
222
                    <MultiSelect
223
                        data={ techniques.map((tech, index) => {
224
                            return {label: tech, value: tech, index}
225
                        }) }
226
                        label="Technique (OR)"
227
                        selectedItems={ selectedTechniques }
228
                        onSelectedItemsChange={ setSelectedTechniques }
229
                    />
230
                    <MultiSelect
231
                        data={ institutions.map((institution, index) => {
232
                            return {label: institution, value: institution, index}
233
                        }) }
234
                        label="Institution (OR)"
235
                        selectedItems={ selectedInstitutions }
236
                        onSelectedItemsChange={ setSelectedInstitutions }
237
                    />
238
                    <MultiSelect
239
                        data={ cities.map((city, index) => {
240
                            return {label: city, value: city, index}
241
                        }) }
242
                        label="City (OR)"
243
                        selectedItems={ selectedCities }
244
                        onSelectedItemsChange={ setSelectedCities }
245
                    />
246
                    <MultiSelect
247
                        data={ countries.map((country, index) => {
248
                            return {label: country, value: country, index}
249
                        }) }
250
                        label="Country (OR)"
251
                        selectedItems={ selectedCountries }
252
                        onSelectedItemsChange={ setSelectedCountries }
253
                    />
254
                    <VStack space={ 2 } mt={ 1 }>
255
                        <SwitchWithLabel label={ "School of Prague" } value={ isSchoolOfPrague }
256
                                         onValueChange={ setIsSchoolOfPrague }/>
257
                        <SwitchWithLabel label={ "Original" } value={ isOriginal } onValueChange={ setIsOriginal }/>
258
                        <SwitchWithLabel label={ "Copy" } value={ isCopy } onValueChange={ setIsCopy }/>
259
                        <SwitchWithLabel label={ "High Quality" } value={ isHighQuality }
260
                                         onValueChange={ setIsHighQuality }/>
261
                        <SwitchWithLabel label={ "Low Quality" } value={ isLowQuality }
262
                                         onValueChange={ setIsLowQuality }/>
263
                    </VStack>
264
                    <Box
265
                        flex={ 1 }
266
                        flexDirection="row"
267
                        justifyContent={ "flex-end" }
268
                        pt={ 2 }
269 9c55d3bb Schwobik
                    >
270 e49b1f44 Schwobik
                        <Button
271
                            borderRadius={ 10 }
272
                            startIcon={
273
                                <CloseIcon size="xs"/>
274
                            }
275
                            onPress={ clearForm }
276
                            variant="outline"
277
                            color="primary.500"
278
                            borderColor="primary.500"
279
                            mr={ 2 }
280
                            size={ "sm" }
281
                        >
282
                            Reset
283
                        </Button>
284
                        <Button
285 ab632fe5 Michal Schwob
                            isLoading={ loading }
286 e49b1f44 Schwobik
                            borderRadius={ 10 }
287
                            onPress={ () => searchSubmit() }
288
                            colorScheme="primary"
289
                            background={ "primary.500" }
290
                            variant="solid"
291
                            size={ "sm" }
292
                        >
293
                            Search
294
                        </Button>
295
                    </Box>
296
                </>
297
            ) }
298 1980ed09 Schwobik
299 e49b1f44 Schwobik
        </>
300 9c55d3bb Schwobik
    )
301
}
302
303
export default SearchForm