1
|
import React, { useEffect, useState } from "react"
|
2
|
import { useDispatch, useSelector } from "react-redux"
|
3
|
import { AppDispatch, RootState } from "../stores/store"
|
4
|
import LoadingBox from "../components/loading/LoadingBox"
|
5
|
import { log } from "../logging/logger"
|
6
|
import { DrawerScreenProps } from "@react-navigation/drawer"
|
7
|
import { RootStackParamList } from "./Navigation"
|
8
|
import { Box, CloseIcon, Select, useToast, Button, Flex, Text, VStack, ScrollView, HStack, Divider } from "native-base"
|
9
|
import { ErrorToast } from "../components/toast/ErrorToast"
|
10
|
import { Floor, Place, Room } from "../types/plan"
|
11
|
import { getFloorList, getPlanFloorImage, getPlanInventories, getPlanItems } from "../stores/actions/planThunks"
|
12
|
import ListView from "../components/listView/ListView"
|
13
|
import CastlePlanView from "../components/plan/CastlePlanView"
|
14
|
|
15
|
import { Dimensions } from 'react-native';
|
16
|
import { login } from "../stores/actions/userThunks"
|
17
|
|
18
|
const PlanViewPage = ({ route, navigation }: DrawerScreenProps<RootStackParamList, 'Plan'>) => {
|
19
|
|
20
|
const dispatch = useDispatch<AppDispatch>()
|
21
|
|
22
|
// Route: set selected room / place from
|
23
|
useEffect(() => {
|
24
|
console.log("dispatching get floor list")
|
25
|
// dispatch(login({username: "Viktorie", password: "Golem123."}))
|
26
|
dispatch(getFloorList())
|
27
|
}, [])
|
28
|
|
29
|
const DEFAULT_FLOOR = "first_floor"
|
30
|
|
31
|
const [castlePlanShown, setCastlePlanShown] = useState<boolean>(true)
|
32
|
|
33
|
const [selectedFloor, setSelectedFloor] = useState<Floor | undefined>(undefined)
|
34
|
const [selectedRoom, setSelectedRoom] = useState<Room | undefined>(undefined)
|
35
|
const [selectedPlace, setSelectedPlace] = useState<Place | undefined>(undefined)
|
36
|
|
37
|
const [isInteractingWithCastlePlan, setIsInteractingWithCastlePlan] = useState(false)
|
38
|
|
39
|
|
40
|
const { floorListLoading, floorList, totalItemsCount, lastError } = useSelector((state: RootState) => state.planViewState)
|
41
|
|
42
|
const inventories = useSelector((state: RootState) => state.planViewState.itemInventories)
|
43
|
const inventoriesLoading = useSelector((state: RootState) => state.planViewState.itemInventoriesLoading)
|
44
|
|
45
|
const numberOfResults = useSelector((state: RootState) => state.planViewState.totalItemsCount)
|
46
|
|
47
|
const data = useSelector((state: RootState) => state.planViewState.itemList)
|
48
|
const dataLoading = useSelector((state: RootState) => state.planViewState.itemListLoading)
|
49
|
const pagination = useSelector((state: RootState) => state.planViewState.itemListPagination)
|
50
|
|
51
|
const floorMapImage = useSelector((state: RootState) => state.planViewState.mapImage)
|
52
|
const floorMapImageLoading = useSelector((state: RootState) => state.planViewState.mapImageLoading)
|
53
|
|
54
|
const toast = useToast();
|
55
|
|
56
|
useEffect(() => {
|
57
|
if (floorList && !selectedFloor) {
|
58
|
|
59
|
if (route.params && route.params.roomId) {
|
60
|
|
61
|
selectRoom(route.params?.roomId)
|
62
|
|
63
|
if (route.params.placeId) {
|
64
|
selectPlace(route.params.placeId)
|
65
|
}
|
66
|
|
67
|
// if (!itemListLoading) {
|
68
|
// searchSubmit(currentPage, route.params.roomId, route.params.placeId)
|
69
|
// }
|
70
|
}
|
71
|
else {
|
72
|
selectFloor(DEFAULT_FLOOR)
|
73
|
}
|
74
|
}
|
75
|
}, [floorList, route.params?.roomId, route.params?.placeId])
|
76
|
|
77
|
useEffect(() => {
|
78
|
if (floorList) {
|
79
|
if (!inventoriesLoading) {
|
80
|
dispatch(getPlanInventories({ room: selectedRoom?.id ?? 0, place: selectedPlace?.id }))
|
81
|
}
|
82
|
}
|
83
|
}, [selectedPlace, selectedRoom])
|
84
|
|
85
|
useEffect(() => {
|
86
|
if (selectedFloor) {
|
87
|
console.log("Dispatching getFloorImage " + selectedFloor.id)
|
88
|
dispatch(getPlanFloorImage(selectedFloor.id))
|
89
|
}
|
90
|
}, [selectedFloor])
|
91
|
|
92
|
|
93
|
useEffect(() => {
|
94
|
if (lastError) {
|
95
|
toast.closeAll()
|
96
|
toast.show({
|
97
|
render: ({
|
98
|
id
|
99
|
}) => {
|
100
|
return <ErrorToast headerText={"Error"} text={lastError} onClose={() => toast.close(id)} />;
|
101
|
},
|
102
|
duration: 3000
|
103
|
});
|
104
|
}
|
105
|
}, [lastError])
|
106
|
|
107
|
|
108
|
const searchSubmit = (roomId: number, placeId?: number) => {
|
109
|
log.debug("PlanView", "searchSubmit")
|
110
|
|
111
|
console.log("searching " + roomId + ", " + placeId)
|
112
|
|
113
|
dispatch(getPlanInventories({ room: roomId, place: placeId }))
|
114
|
}
|
115
|
|
116
|
const selectFloor = (floorId: string) => {
|
117
|
if (floorId == selectedFloor?.id) {
|
118
|
return;
|
119
|
}
|
120
|
|
121
|
for (let i = 0; i < floorList.length; i++) {
|
122
|
let floor = floorList[i]
|
123
|
if (floor.id == floorId) {
|
124
|
setSelectedFloor(floor)
|
125
|
setSelectedRoom(undefined)
|
126
|
setSelectedPlace(undefined)
|
127
|
return;
|
128
|
}
|
129
|
}
|
130
|
}
|
131
|
|
132
|
const selectRoom = (roomId: number) => {
|
133
|
if (roomId == selectedRoom?.id) {
|
134
|
return;
|
135
|
}
|
136
|
|
137
|
for (let i = 0; i < floorList.length; i++) {
|
138
|
let floor = floorList[i]
|
139
|
|
140
|
for (let j = 0; j < floor.roomList.length; j++) {
|
141
|
let room = floor.roomList[j]
|
142
|
if (room.id == roomId) {
|
143
|
setSelectedFloor(floor)
|
144
|
setSelectedRoom(room)
|
145
|
setSelectedPlace(undefined)
|
146
|
return;
|
147
|
}
|
148
|
}
|
149
|
}
|
150
|
}
|
151
|
|
152
|
const selectPlace = (placeId: number) => {
|
153
|
if (placeId == selectedPlace?.id) {
|
154
|
return;
|
155
|
}
|
156
|
|
157
|
for (let i = 0; selectedRoom?.places && i < selectedRoom?.places?.length; i++) {
|
158
|
let place = selectedRoom?.places[i]
|
159
|
if (place.id == placeId) {
|
160
|
setSelectedPlace(place)
|
161
|
return;
|
162
|
}
|
163
|
}
|
164
|
}
|
165
|
|
166
|
|
167
|
const clearForm = () => {
|
168
|
log.debug("PlanView", "clearForm")
|
169
|
setSelectedPlace(undefined)
|
170
|
setSelectedRoom(undefined)
|
171
|
selectFloor(DEFAULT_FLOOR)
|
172
|
}
|
173
|
|
174
|
const onBackPressed = () => {
|
175
|
log.debug("back pressed")
|
176
|
navigation.goBack();
|
177
|
}
|
178
|
|
179
|
return (
|
180
|
<Box flex={1}>
|
181
|
<Box h={Dimensions.get('window').height - 55}>
|
182
|
<ScrollView flex={1} w={"100%"} nestedScrollEnabled={!isInteractingWithCastlePlan}>
|
183
|
<VStack space={1}>
|
184
|
|
185
|
{/* Selection */}
|
186
|
|
187
|
{
|
188
|
floorListLoading ?
|
189
|
<LoadingBox text="Floor list loading"></LoadingBox>
|
190
|
:
|
191
|
<>
|
192
|
{/* Floor */}
|
193
|
<Select
|
194
|
mt={2.5}
|
195
|
ml={2.5}
|
196
|
mr={2.5}
|
197
|
|
198
|
placeholder={"Floor"}
|
199
|
selectedValue={selectedFloor?.id}
|
200
|
onValueChange={selectFloor}
|
201
|
variant="rounded"
|
202
|
>
|
203
|
{floorList.map((floor, index) => {
|
204
|
return (
|
205
|
<Select.Item value={floor.id} label={floor.label} />
|
206
|
);
|
207
|
})}
|
208
|
</Select>
|
209
|
|
210
|
{/* Room */}
|
211
|
|
212
|
< Select
|
213
|
mt={2.5}
|
214
|
ml={2.5}
|
215
|
mr={2.5}
|
216
|
|
217
|
isDisabled={(selectedFloor && selectedFloor.roomList && selectedFloor.roomList.length) ? false : true}
|
218
|
|
219
|
placeholder={"Room"}
|
220
|
selectedValue={selectedRoom?.id.toString()}
|
221
|
onValueChange={(value: string) => selectRoom(parseInt(value))}
|
222
|
variant="rounded"
|
223
|
>
|
224
|
{selectedFloor?.roomList?.map((room, index) => {
|
225
|
if (room.room_list) {
|
226
|
return (
|
227
|
<Select.Item value={room.id.toString()} label={room.label} />
|
228
|
);
|
229
|
}
|
230
|
})}
|
231
|
</Select>
|
232
|
|
233
|
{/* Place */}
|
234
|
<Select
|
235
|
mt={2.5}
|
236
|
ml={2.5}
|
237
|
mr={2.5}
|
238
|
|
239
|
isDisabled={(selectedRoom && selectedRoom.places && selectedRoom.places.length > 0) ? false : true}
|
240
|
placeholder={"Place"}
|
241
|
selectedValue={selectedPlace?.id.toString()}
|
242
|
onValueChange={(value: string) => selectPlace(parseInt(value))}
|
243
|
variant="rounded"
|
244
|
>
|
245
|
{selectedRoom?.places?.map((place, index) => {
|
246
|
return (
|
247
|
<Select.Item value={place.id.toString()} label={place.label} />
|
248
|
);
|
249
|
})}
|
250
|
</Select>
|
251
|
|
252
|
{/* Filter */}
|
253
|
<Flex direction="row" alignItems="center" justify="flex-end"
|
254
|
mt={2.5}
|
255
|
ml={2.5}
|
256
|
mr={3}
|
257
|
>
|
258
|
<Button
|
259
|
startIcon={
|
260
|
<CloseIcon size="xs" />
|
261
|
}
|
262
|
onPress={clearForm}
|
263
|
variant="subtle"
|
264
|
color="secondary.500"
|
265
|
mr={2}
|
266
|
borderRadius={10}
|
267
|
size={"sm"}
|
268
|
>
|
269
|
Reset
|
270
|
</Button>
|
271
|
<Button
|
272
|
onPress={() => {
|
273
|
if (selectedRoom && !selectedPlace) {
|
274
|
searchSubmit(selectedRoom.id)
|
275
|
}
|
276
|
else if (selectedRoom && selectedPlace) {
|
277
|
searchSubmit(selectedRoom.id, selectedPlace.id)
|
278
|
}
|
279
|
|
280
|
}}
|
281
|
backgroundColor={"#654B07"}
|
282
|
variant="subtle"
|
283
|
borderRadius={10}
|
284
|
size={"sm"}
|
285
|
>
|
286
|
<Text color="white" fontSize={11}>
|
287
|
Search
|
288
|
</Text>
|
289
|
</Button>
|
290
|
</Flex>
|
291
|
</>
|
292
|
}
|
293
|
|
294
|
{/* Plan */}
|
295
|
{castlePlanShown && selectedFloor && !floorListLoading && (
|
296
|
floorMapImageLoading ?
|
297
|
<LoadingBox text="Loading castle plan" />
|
298
|
:
|
299
|
floorMapImage &&
|
300
|
<Box
|
301
|
mt={2.5}
|
302
|
ml={2.5}
|
303
|
mr={2.5}
|
304
|
height={Dimensions.get("window").height - 350}
|
305
|
onTouchStart={() => setIsInteractingWithCastlePlan(true)}
|
306
|
onTouchEnd={() => setIsInteractingWithCastlePlan(true)}>
|
307
|
<CastlePlanView mapImage={floorMapImage} selectedRoom={selectedRoom} roomList={selectedFloor.roomList} height={Dimensions.get("window").height - 350}
|
308
|
setSelectedRoom={(room: Room) => {
|
309
|
selectRoom(room.id)
|
310
|
}}
|
311
|
fullScreenMode={false}
|
312
|
/>
|
313
|
</Box>
|
314
|
)
|
315
|
}
|
316
|
|
317
|
{/* Item List */}
|
318
|
<>
|
319
|
{!castlePlanShown && (
|
320
|
inventoriesLoading ?
|
321
|
<LoadingBox text="Loading available inventories..." />
|
322
|
:
|
323
|
<Box mt={5} ml={2.5} mr={2.5}>
|
324
|
<ListView
|
325
|
|
326
|
navigation={navigation}
|
327
|
hideHeaderText
|
328
|
|
329
|
inventories={inventories}
|
330
|
numOfResults={numberOfResults}
|
331
|
|
332
|
data={data}
|
333
|
loadedPages={pagination ?? {}}
|
334
|
inventoriesDataLoading={dataLoading ?? {}}
|
335
|
handleLoadMoreItems={(inventoryName: string, cursor: number) => {
|
336
|
dispatch(getPlanItems({ cursor: cursor, inventory: inventoryName, room: selectedRoom?.id ?? 0, place: selectedPlace?.id }))
|
337
|
}}
|
338
|
/>
|
339
|
</Box>
|
340
|
)
|
341
|
}
|
342
|
</>
|
343
|
|
344
|
</VStack>
|
345
|
</ScrollView>
|
346
|
{/* item notes switch tab */}
|
347
|
<HStack alignItems="center">
|
348
|
<Button
|
349
|
variant={"unstyled"}
|
350
|
w="50%"
|
351
|
onPress={() => { setCastlePlanShown(true) }}
|
352
|
>
|
353
|
<VStack alignItems={"center"}>
|
354
|
<Text color="primary" bold={castlePlanShown} mb={castlePlanShown ? undefined : 2}>
|
355
|
Castle
|
356
|
</Text>
|
357
|
{castlePlanShown && <Divider color="primary" borderWidth={1} width={60} mt={2} />}
|
358
|
</VStack>
|
359
|
</Button>
|
360
|
<Button
|
361
|
variant={"unstyled"}
|
362
|
w="50%"
|
363
|
onPress={() => { setCastlePlanShown(false) }}
|
364
|
>
|
365
|
<VStack alignItems={"center"}>
|
366
|
<Text color="primary" bold={!castlePlanShown} mb={!castlePlanShown ? undefined : 2}>
|
367
|
Results
|
368
|
</Text>
|
369
|
{!castlePlanShown && <Divider color="primary" borderWidth={1} width={60} mt={2} />}
|
370
|
</VStack>
|
371
|
</Button>
|
372
|
</HStack>
|
373
|
</Box>
|
374
|
</Box>
|
375
|
);
|
376
|
}
|
377
|
|
378
|
export default PlanViewPage;
|