Projekt

Obecné

Profil

Stáhnout (11.6 KB) Statistiky
| Větev: | Tag: | Revize:
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 { RootDrawerParamList } from "./Navigation"
8
import { Box, CloseIcon, Select, useToast, Button, Flex, Text, Center, VStack, ScrollView, View } 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 { login } from "../stores/actions/userThunks"
13
import { getPlanItemsRequest } from "../api/planservice"
14
import ItemPreview from "../components/listView/ItemPreview"
15
import PlanView from "../components/plan/CastlePlanView"
16
import { BASE_API_URL } from "../api/constants"
17
import ListView from "../components/listView/ListView"
18
import SearchForm from "../components/search/SearchForm"
19
import { loadItemsByInventory } from "../stores/actions/listViewThunks"
20
import CastlePlanView from "../components/plan/CastlePlanView"
21

    
22
const PlanViewPage = ({ route, navigation }: DrawerScreenProps<RootDrawerParamList, 'Plan'>) => {
23

    
24
  const dispatch = useDispatch<AppDispatch>()
25

    
26
  // 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

    
33
  const DEFAULT_FLOOR = "first_floor"
34

    
35
  const [selectedFloor, setSelectedFloor] = useState<Floor | undefined>(undefined)
36
  const [selectedRoom, setSelectedRoom] = useState<Room | undefined>(undefined)
37
  const [selectedPlace, setSelectedPlace] = useState<Place | undefined>(undefined)
38

    
39
  const [isInteractingWithCastlePlan, setIsInteractingWithCastlePlan] = useState(false);
40

    
41
  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

    
46
  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

    
52
  const floorMapImage = useSelector((state: RootState) => state.planViewState.mapImage)
53
  const floorMapImageLoading = useSelector((state: RootState) => state.planViewState.mapImageLoading)
54

    
55
  // todo
56
  const castlePlanFullScreenMode = false
57

    
58
  const toast = useToast();
59

    
60
  useEffect(() => {
61
    if (floorList && !selectedFloor) {
62

    
63
      if (route.params && route.params.roomId) {
64

    
65
        selectRoom(route.params.roomId)
66

    
67
        if (route.params.placeId) {
68
          selectPlace(route.params.placeId)
69
        }
70

    
71
        // if (!itemListLoading) {
72
        //   searchSubmit(currentPage, route.params.roomId, route.params.placeId)
73
        // }
74
      }
75
      else {
76
        selectFloor(DEFAULT_FLOOR)
77
      }
78
    }
79
  }, [floorList, route.params.roomId, route.params.placeId])
80

    
81
  useEffect(() => {
82
    if (floorList) {
83
      if (!inventoriesLoading) {
84
        dispatch(getPlanInventories({ room: selectedRoom?.id ?? 0, place: selectedPlace?.id }))
85
      }
86
    }
87
  }, [selectedPlace, selectedRoom])
88

    
89
  useEffect(() => {
90
    if (selectedFloor) {
91
      console.log("Dispatching getFloorImage " + selectedFloor.id)
92
      dispatch(getPlanFloorImage(selectedFloor.id))
93
    }
94
  }, [selectedFloor])
95

    
96

    
97
  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
  const searchSubmit = (roomId: number, placeId?: number) => {
113
    log.debug("PlanView", "searchSubmit")
114

    
115
    console.log("searching " + roomId + ", " + placeId)
116

    
117
    dispatch(getPlanInventories({ room: roomId, place: placeId }))
118
  }
119

    
120
  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
  const clearForm = () => {
172
    log.debug("PlanView", "clearForm")
173
    setSelectedPlace(undefined)
174
    setSelectedRoom(undefined)
175
    selectFloor(DEFAULT_FLOOR)
176
  }
177

    
178
  const onBackPressed = () => {
179
    log.debug("back pressed")
180
    navigation.goBack();
181
  }
182

    
183
  return (
184
    <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
                  <Select
198
                    mt={2.5}
199
                    ml={2.5}
200
                    mr={2.5}
201

    
202
                    placeholder={"Floor"}
203
                    selectedValue={selectedFloor?.id}
204
                    onValueChange={selectFloor}
205
                    variant="rounded"
206
                  >
207
                    {floorList.map((floor, index) => {
208
                      return (
209
                        <Select.Item value={floor.id} label={floor.label} />
210
                      );
211
                    })}
212
                  </Select>
213

    
214
                  {/* 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
                  }
235

    
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
                  }
255

    
256
                  {/* 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

    
298
            {/* 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

    
314
            {/* 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
  );
353
}
354

    
355
export default PlanViewPage;
(7-7/8)