Projekt

Obecné

Profil

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