Projekt

Obecné

Profil

Stáhnout (10.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 a6999074 Fantič
import { Box, CloseIcon, Select, useToast, Button, Flex, Text, Center, VStack, ScrollView } 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 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
        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 f8083631 Fantič
  }, [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 a6999074 Fantič
    <Center m={2} mr={0} mb={6} flex={1}>
180
181
      <ScrollView flex={1} w={"100%"} >
182
        <VStack space={1} mr={4}>
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
197
                  placeholder={"Floor"}
198
                  selectedValue={selectedFloor?.id}
199
                  onValueChange={selectFloor}
200
                  variant="rounded"
201
                >
202
                  {floorList.map((floor, index) => {
203 f8083631 Fantič
                    return (
204 a6999074 Fantič
                      <Select.Item value={floor.id} label={floor.label} />
205 f8083631 Fantič
                    );
206 a6999074 Fantič
                  })}
207
                </Select>
208
209
                {/* Room */}
210
                {selectedFloor && selectedFloor.roomList && selectedFloor.roomList.length > 0 &&
211
                  < Select
212
                    mt={2.5}
213
                    ml={2.5}
214
                    mr={2.5}
215
216
                    placeholder={"Room"}
217
                    selectedValue={selectedRoom?.id.toString()}
218
                    onValueChange={(value: string) => selectRoom(parseInt(value))}
219
                    variant="rounded"
220
                  >
221
                    {selectedFloor?.roomList?.map((room, index) => {
222
                      if (room.room_list) {
223
                        return (
224
                          <Select.Item value={room.id.toString()} label={room.label} />
225
                        );
226
                      }
227
                    })}
228
                  </Select>
229 f8083631 Fantič
                }
230 eb8efa3f Fantič
231 a6999074 Fantič
                {/* Place */}
232
                {selectedRoom && selectedRoom.places && selectedRoom.places.length > 0 &&
233
                  <Select
234
                    mt={2.5}
235
                    ml={2.5}
236
                    mr={2.5}
237
238
                    placeholder={"Place"}
239
                    selectedValue={selectedPlace?.id.toString()}
240
                    onValueChange={(value: string) => selectPlace(parseInt(value))}
241
                    variant="rounded"
242
                  >
243
                    {selectedRoom?.places?.map((place, index) => {
244
                      return (
245
                        <Select.Item value={place.id.toString()} label={place.label} />
246
                      );
247
                    })}
248
                  </Select>
249
                }
250 eb8efa3f Fantič
251 a6999074 Fantič
                {/* 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
                    endIcon={
259
                      <CloseIcon size="xs" />
260
                    }
261
                    onPress={clearForm}
262
                    variant="outline"
263
                    color="primary.500"
264
                    borderColor="primary.100"
265
                    mr={2}
266
                    borderRadius={15}
267
                    size={"md"}
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
                    colorScheme="primary"
282
                    background={"primary.100"}
283
                    variant="solid"
284
                    borderRadius={15}
285
                    size={"md"}
286
                  >
287
                    Search
288
                  </Button>
289
                </Flex>
290
              </>
291
          }
292
293
          {/* Plan */}
294
          {/* temporary prototype */}
295
296 aac857cf Fantič
          {selectedFloor && !floorListLoading && (
297
            floorMapImageLoading ?
298
              <LoadingBox text="Loading castle plan" />
299
              :
300 f220395e Fantič
              floorMapImage &&
301
              <Box mt={2.5} ml={2.5}>
302
                <CastlePlanView mapImage={floorMapImage} selectedRoom={selectedRoom} />
303
              </Box>
304 aac857cf Fantič
          )
305
          }
306 a6999074 Fantič
307
          {/* Item List */}
308 160b7073 Fantič
          <>
309 a6999074 Fantič
            {inventoriesLoading ?
310
              <LoadingBox text="Loading available inventories..." />
311
              :
312
              <Box mt={2.5} ml={2.5}>
313
                <ListView
314
315
                  navigation={navigation}
316
                  hideHeaderText
317
318
                  inventories={inventories}
319
                  numOfResults={numberOfResults}
320
321
                  data={data}
322
                  loadedPages={pagination ?? {}}
323
                  inventoriesDataLoading={dataLoading ?? {}}
324
                  handleLoadMoreItems={(inventoryName: string, cursor: number) => {
325
                    dispatch(getPlanItems({ cursor: cursor, inventory: inventoryName, room: selectedRoom?.id ?? 0, place: selectedPlace?.id }))
326
                  }
327
                  }
328
                />
329
              </Box>
330
            }
331 160b7073 Fantič
          </>
332
333 a6999074 Fantič
        </VStack>
334
      </ScrollView>
335
    </Center>
336 eb8efa3f Fantič
337
338 c2c8470e Fantič
  );
339
}
340
341
export default PlanViewPage;