Projekt

Obecné

Profil

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