Projekt

Obecné

Profil

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