Projekt

Obecné

Profil

Stáhnout (12.5 KB) Statistiky
| Větev: | Tag: | Revize:
1
import {
2
    Box,
3
    Button,
4
    ChevronDownIcon,
5
    ChevronUpIcon,
6
    CloseIcon,
7
    HStack,
8
    Input,
9
    Text,
10
    VStack
11
} from "native-base"
12
import { useDispatch, useSelector } from "react-redux"
13
import { useCallback, useEffect, useState } from "react"
14
import { AppDispatch, RootState } from "../../stores/store"
15
import { Inventory } from "../../types/searchFormTypes"
16
import { search } from "../../stores/actions/listViewThunks"
17
import MultiSelect from "./MultiSelect"
18
import SwitchWithLabel from "./SwitchWithLabel"
19
import { log } from "../../logging/logger"
20
import { setFilterState } from "../../stores/reducers/listViewSlice"
21
import { SearchParams } from "../../api/searchService"
22

    
23
interface SearchFormProps {
24
    inventoryId: string | null
25
}
26

    
27

    
28
const SearchForm = (props: SearchFormProps) => {
29
    const [isFilterOpen, setIsFilterOpen] = useState<boolean>(true)
30

    
31
    const inventories: Inventory[] = useSelector((state: RootState) => state.searchForm.inventories)
32
    const nationalities = useSelector((state: RootState) => state.searchForm.nationalities)
33
    const artists = useSelector((state: RootState) => state.searchForm.artists)
34
    const subjects = useSelector((state: RootState) => state.searchForm.subjects)
35
    const rooms = useSelector((state: RootState) => state.searchForm.rooms)
36
    const techniques = useSelector((state: RootState) => state.searchForm.techniques)
37
    const countries = useSelector((state: RootState) => state.searchForm.countries)
38
    const cities = useSelector((state: RootState) => state.searchForm.cities)
39
    const institutions = useSelector((state: RootState) => state.searchForm.institutions)
40

    
41
    const [selectedInventories, setSelectedInventories] = useState<{ label: string, value: string }[]>([])
42
    const [selectedNationalities, setSelectedNationalities] = useState<{ label: string, value: string }[]>([])
43
    const [selectedArtists, setSelectedArtists] = useState<{ label: string, value: string }[]>([])
44
    const [selectedSubjects, setSelectedSubjects] = useState<{ label: string, value: string }[]>([])
45
    const [selectedRooms, setSelectedRooms] = useState<{ label: string, value: string }[]>([])
46
    const [selectedTechniques, setSelectedTechniques] = useState<{ label: string, value: string }[]>([])
47
    const [selectedCountries, setSelectedCountries] = useState<{ label: string, value: string }[]>([])
48
    const [selectedCities, setSelectedCities] = useState<{ label: string, value: string }[]>([])
49
    const [selectedInstitutions, setSelectedInstitutions] = useState<{ label: string, value: string }[]>([])
50
    const [searchQuery, setSearchQuery] = useState<string>("")
51

    
52
    const [isSchoolOfPrague, setIsSchoolOfPrague] = useState(false)
53
    const [isOriginal, setIsOriginal] = useState(false)
54
    const [isCopy, setIsCopy] = useState(false)
55
    const [isHighQuality, setIsHighQuality] = useState(false)
56
    const [isLowQuality, setIsLowQuality] = useState(false)
57

    
58
    const dispatch = useDispatch<AppDispatch>()
59

    
60
    const searchSubmit = () => {
61
        const filterState: SearchParams = {
62
            inventories: selectedInventories.map(i => i.value),
63
            nationalities: selectedNationalities.map(n => n.value),
64
            artists: selectedArtists.map(a => a.value),
65
            subjects: selectedSubjects.map(s => s.value),
66
            rooms: selectedRooms.map(r => r.value),
67
            techniques: selectedTechniques.map(t => t.value),
68
            countries: selectedCountries.map(c => c.value),
69
            cities: selectedCities.map(c => c.value),
70
            institutions: selectedInstitutions.map(i => i.value),
71
            isSchoolOfPrague,
72
            isOriginal,
73
            isCopy,
74
            isHighQuality,
75
            isLowQuality,
76
            searchQuery: searchQuery,
77
        }
78
        dispatch(setFilterState(filterState))
79
        dispatch(search(filterState))
80
        setIsFilterOpen(false)
81
    }
82

    
83
    const clearForm = () => {
84
        log.debug("SearchForm", "clearForm")
85

    
86
        setSelectedInventories([])
87
        setSelectedNationalities([])
88
        setSelectedArtists([])
89
        setSelectedSubjects([])
90
        setSelectedRooms([])
91
        setSelectedTechniques([])
92
        setSelectedCountries([])
93
        setSelectedCities([])
94
        setSelectedInstitutions([])
95
        setSearchQuery("")
96
    }
97

    
98
    const getSearchHeader = useCallback(() => {
99
        if (selectedInventories && selectedInventories.length === 1) {
100
            return `Search: ${ selectedInventories[0].label }`
101
        } else if (selectedRooms && selectedRooms.length === 1) {
102
            return `Search: ${ selectedRooms[0].label }`
103
        } else {
104
            return "Search:"
105
        }
106
    }, [inventories, rooms])
107

    
108
    useEffect(() => {
109
        log.debug("SearchForm", "useEffect", "props.inventoryId", props.inventoryId)
110
        if (props.inventoryId) {
111
            clearForm()
112
            setIsFilterOpen(false)
113
            setSelectedInventories([{ label: props.inventoryId, value: props.inventoryId }])
114
            dispatch(setFilterState({ inventories: [props.inventoryId] }))
115
            dispatch(search({ inventories: [props.inventoryId] }))
116
        }
117
    }, [props.inventoryId])
118

    
119
    return (
120
        <>
121
            <Text
122
                fontSize={ "2xl" }
123
                fontWeight={ "bold" }
124
                color={ "primary.500" }
125
            >
126
                { getSearchHeader() }
127
            </Text>
128
            <Input
129
                placeholder="Search"
130
                size={ "md" }
131
                variant="underlined"
132
                value={searchQuery}
133
                onChangeText={ setSearchQuery }
134
            />
135
            <HStack justifyContent={"space-between"}>
136
                <Button
137
                    onPress={ () => setIsFilterOpen(!isFilterOpen) }
138
                    variant="outline"
139
                    endIcon={
140
                        <>
141
                            { isFilterOpen ?
142
                                (<ChevronUpIcon size={ 4 } color={ "primary.500" }/>)
143
                                : (<ChevronDownIcon size={ 4 } color={ "primary.500" }/>) }
144
                        </>
145
                    }
146
                    flexWrap={ "nowrap" }
147
                    borderColor={ "primary.500" }
148
                    size={ "sm" }
149
                    mt={ 2 }
150
                    p={ 2 }
151

    
152
                >
153
                    Filter
154
                </Button>
155
                { !isFilterOpen && (
156
                    <Button
157
                        borderRadius={ 10 }
158
                        onPress={ () => searchSubmit() }
159
                        colorScheme="primary"
160
                        background={ "primary.500" }
161
                        variant="solid"
162
                        mt={2}
163
                        p={2}
164
                        size={ "md" }
165
                    >
166
                        Search
167
                    </Button>
168
                )}
169
            </HStack>
170
            { isFilterOpen && (
171
                <>
172
                    <MultiSelect
173
                        data={ inventories.map((inventory, index) => {
174
                            return {label: inventory.label, value: inventory.name, index}
175
                        }) }
176
                        label="Inventory (OR)"
177
                        selectedItems={ selectedInventories }
178
                        onSelectedItemsChange={ setSelectedInventories }
179
                    />
180
                    <MultiSelect
181
                        data={ rooms.map((room, index) => {
182
                            return {label: `${ room.id } - ${ room.label }`, value: room.id.toString(), index}
183
                        }) }
184
                        label="Rooms (OR)"
185
                        selectedItems={ selectedRooms }
186
                        onSelectedItemsChange={ setSelectedRooms }
187
                    />
188
                    <MultiSelect
189
                        data={ artists.map((art, index) => {
190
                            return {label: art.display_name, value: art.getty_id, index}
191
                        }) }
192
                        label="Artists/Copyists (OR)"
193
                        selectedItems={ selectedArtists }
194
                        onSelectedItemsChange={ setSelectedArtists }
195
                    />
196
                    <MultiSelect
197
                        data={ nationalities.map((nat, index) => {
198
                            return {label: nat, value: nat, index}
199
                        }) }
200
                        label="Artist`s Origin (OR)"
201
                        selectedItems={ selectedNationalities }
202
                        onSelectedItemsChange={ setSelectedNationalities }
203
                    />
204
                    <MultiSelect
205
                        data={ subjects.map((subj, index) => {
206
                            return {label: subj, value: subj, index}
207
                        }) }
208
                        label="Subject (AND)"
209
                        selectedItems={ selectedSubjects }
210
                        onSelectedItemsChange={ setSelectedSubjects }
211
                    />
212
                    <MultiSelect
213
                        data={ techniques.map((tech, index) => {
214
                            return {label: tech, value: tech, index}
215
                        }) }
216
                        label="Technique (OR)"
217
                        selectedItems={ selectedTechniques }
218
                        onSelectedItemsChange={ setSelectedTechniques }
219
                    />
220
                    <MultiSelect
221
                        data={ institutions.map((institution, index) => {
222
                            return {label: institution, value: institution, index}
223
                        }) }
224
                        label="Institution (OR)"
225
                        selectedItems={ selectedInstitutions }
226
                        onSelectedItemsChange={ setSelectedInstitutions }
227
                    />
228
                    <MultiSelect
229
                        data={ cities.map((city, index) => {
230
                            return {label: city, value: city, index}
231
                        }) }
232
                        label="City (OR)"
233
                        selectedItems={ selectedCities }
234
                        onSelectedItemsChange={ setSelectedCities }
235
                    />
236
                    <MultiSelect
237
                        data={ countries.map((country, index) => {
238
                            return {label: country, value: country, index}
239
                        }) }
240
                        label="Country (OR)"
241
                        selectedItems={ selectedCountries }
242
                        onSelectedItemsChange={ setSelectedCountries }
243
                    />
244
                    <VStack space={ 2 } mt={ 1 }>
245
                        <SwitchWithLabel label={ "School of Prague" } value={ isSchoolOfPrague }
246
                                         onValueChange={ setIsSchoolOfPrague }/>
247
                        <SwitchWithLabel label={ "Original" } value={ isOriginal } onValueChange={ setIsOriginal }/>
248
                        <SwitchWithLabel label={ "Copy" } value={ isCopy } onValueChange={ setIsCopy }/>
249
                        <SwitchWithLabel label={ "High Quality" } value={ isHighQuality }
250
                                         onValueChange={ setIsHighQuality }/>
251
                        <SwitchWithLabel label={ "Low Quality" } value={ isLowQuality }
252
                                         onValueChange={ setIsLowQuality }/>
253
                    </VStack>
254
                    <Box
255
                        flex={ 1 }
256
                        flexDirection="row"
257
                        justifyContent={ "flex-end" }
258
                        pt={ 2 }
259
                    >
260
                        <Button
261
                            borderRadius={ 10 }
262
                            startIcon={
263
                                <CloseIcon size="xs"/>
264
                            }
265
                            onPress={ clearForm }
266
                            variant="outline"
267
                            color="primary.500"
268
                            borderColor="primary.500"
269
                            mr={ 2 }
270
                            size={ "sm" }
271
                        >
272
                            Reset
273
                        </Button>
274
                        <Button
275
                            borderRadius={ 10 }
276
                            onPress={ () => searchSubmit() }
277
                            colorScheme="primary"
278
                            background={ "primary.500" }
279
                            variant="solid"
280
                            size={ "sm" }
281
                        >
282
                            Search
283
                        </Button>
284
                    </Box>
285
                </>
286
            ) }
287

    
288
        </>
289
    )
290
}
291

    
292
export default SearchForm
(2-2/3)