Projekt

Obecné

Profil

Stáhnout (12.4 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(login({username: "Viktorie", password: "Golem123."}))
26
    dispatch(getFloorList())
27
  }, [])
28 eb8efa3f Fantič
29 9641b62f Fantič
  const DEFAULT_FLOOR = "first_floor"
30 c2c8470e Fantič
31 7a3ad946 Fantič
  const [castlePlanShown, setCastlePlanShown] = useState<boolean>(true)
32
33 9641b62f Fantič
  const [selectedFloor, setSelectedFloor] = useState<Floor | undefined>(undefined)
34
  const [selectedRoom, setSelectedRoom] = useState<Room | undefined>(undefined)
35
  const [selectedPlace, setSelectedPlace] = useState<Place | undefined>(undefined)
36 c2c8470e Fantič
37 d26c4168 Fantič
  const [isInteractingWithCastlePlan, setIsInteractingWithCastlePlan] = useState(false)
38
39 28ab969f Fantič
40 a6999074 Fantič
  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 c2c8470e Fantič
45 a6999074 Fantič
  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 c2c8470e Fantič
51 aac857cf Fantič
  const floorMapImage = useSelector((state: RootState) => state.planViewState.mapImage)
52
  const floorMapImageLoading = useSelector((state: RootState) => state.planViewState.mapImageLoading)
53
54 c2c8470e Fantič
  const toast = useToast();
55
56 9641b62f Fantič
  useEffect(() => {
57
    if (floorList && !selectedFloor) {
58 a6999074 Fantič
59 f8083631 Fantič
      if (route.params && route.params.roomId) {
60
61 723e4dda Michal Schwob
        selectRoom(route.params?.roomId)
62 160b7073 Fantič
63 f8083631 Fantič
        if (route.params.placeId) {
64
          selectPlace(route.params.placeId)
65
        }
66 160b7073 Fantič
67 a6999074 Fantič
        // if (!itemListLoading) {
68
        //   searchSubmit(currentPage, route.params.roomId, route.params.placeId)
69
        // }
70 f8083631 Fantič
      }
71 160b7073 Fantič
      else {
72 9641b62f Fantič
        selectFloor(DEFAULT_FLOOR)
73 f8083631 Fantič
      }
74 9641b62f Fantič
    }
75 723e4dda Michal Schwob
  }, [floorList, route.params?.roomId, route.params?.placeId])
76 9641b62f Fantič
77 160b7073 Fantič
  useEffect(() => {
78 a6999074 Fantič
    if (floorList) {
79
      if (!inventoriesLoading) {
80
        dispatch(getPlanInventories({ room: selectedRoom?.id ?? 0, place: selectedPlace?.id }))
81 160b7073 Fantič
      }
82
    }
83
  }, [selectedPlace, selectedRoom])
84
85 aac857cf Fantič
  useEffect(() => {
86
    if (selectedFloor) {
87
      console.log("Dispatching getFloorImage " + selectedFloor.id)
88
      dispatch(getPlanFloorImage(selectedFloor.id))
89
    }
90
  }, [selectedFloor])
91
92 160b7073 Fantič
93 c2c8470e Fantič
  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 a6999074 Fantič
  const searchSubmit = (roomId: number, placeId?: number) => {
109 90af86f9 Fantič
    log.debug("PlanView", "searchSubmit")
110
111 160b7073 Fantič
    console.log("searching " + roomId + ", " + placeId)
112
113 a6999074 Fantič
    dispatch(getPlanInventories({ room: roomId, place: placeId }))
114 90af86f9 Fantič
  }
115
116 9641b62f Fantič
  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 90af86f9 Fantič
  const clearForm = () => {
168
    log.debug("PlanView", "clearForm")
169
    setSelectedPlace(undefined)
170 a6999074 Fantič
    setSelectedRoom(undefined)
171
    selectFloor(DEFAULT_FLOOR)
172 90af86f9 Fantič
  }
173 c2c8470e Fantič
174
  const onBackPressed = () => {
175
    log.debug("back pressed")
176
    navigation.goBack();
177
  }
178
179
  return (
180 5cee436e Fantič
    <Box flex={1}>
181 d26c4168 Fantič
      <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 7a3ad946 Fantič
198 d26c4168 Fantič
                    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 7a3ad946 Fantič
217 d26c4168 Fantič
                    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 7a3ad946 Fantič
                        return (
227 d26c4168 Fantič
                          <Select.Item value={room.id.toString()} label={room.label} />
228 7a3ad946 Fantič
                        );
229 d26c4168 Fantič
                      }
230
                    })}
231
                  </Select>
232 a6999074 Fantič
233 d26c4168 Fantič
                  {/* Place */}
234
                  <Select
235
                    mt={2.5}
236
                    ml={2.5}
237
                    mr={2.5}
238 7a3ad946 Fantič
239 d26c4168 Fantič
                    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 5cee436e Fantič
                    >
269 d26c4168 Fantič
                      Reset
270
                    </Button>
271
                    <Button
272
                      onPress={() => {
273
                        if (selectedRoom && !selectedPlace) {
274
                          searchSubmit(selectedRoom.id)
275 5cee436e Fantič
                        }
276 d26c4168 Fantič
                        else if (selectedRoom && selectedPlace) {
277
                          searchSubmit(selectedRoom.id, selectedPlace.id)
278 5cee436e Fantič
                        }
279
280 d26c4168 Fantič
                      }}
281 fb120216 Fantič
                      backgroundColor={"#654B07"}
282 d26c4168 Fantič
                      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 7a3ad946 Fantič
                  :
323 d26c4168 Fantič
                  <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 36ede89c Fantič
                      }}
338
                    />
339 7a3ad946 Fantič
                  </Box>
340
              )
341 5cee436e Fantič
              }
342 d26c4168 Fantič
            </>
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 f4af30c8 Fantič
              <Text color="primary" bold={castlePlanShown} mb={castlePlanShown ? undefined : 2}>
355 d26c4168 Fantič
                Castle
356
              </Text>
357 f4af30c8 Fantič
              {castlePlanShown && <Divider color="primary" borderWidth={1} width={60} mt={2} />}
358 7a3ad946 Fantič
            </VStack>
359 d26c4168 Fantič
          </Button>
360
          <Button
361
            variant={"unstyled"}
362
            w="50%"
363
            onPress={() => { setCastlePlanShown(false) }}
364
          >
365
            <VStack alignItems={"center"}>
366 f4af30c8 Fantič
              <Text color="primary" bold={!castlePlanShown} mb={!castlePlanShown ? undefined : 2}>
367 d26c4168 Fantič
                Results
368
              </Text>
369 f4af30c8 Fantič
              {!castlePlanShown && <Divider color="primary" borderWidth={1} width={60} mt={2} />}
370 d26c4168 Fantič
            </VStack>
371
          </Button>
372
        </HStack>
373
      </Box>
374 5cee436e Fantič
    </Box>
375 c2c8470e Fantič
  );
376
}
377
378
export default PlanViewPage;