Projekt

Obecné

Profil

Stáhnout (10.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 } 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 { 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

    
44
  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

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

    
53
  const toast = useToast();
54

    
55
  useEffect(() => {
56
    if (floorList && !selectedFloor) {
57

    
58
      if (route.params && route.params.roomId) {
59

    
60
        selectRoom(route.params.roomId)
61

    
62
        if (route.params.placeId) {
63
          selectPlace(route.params.placeId)
64
        }
65

    
66
        // if (!itemListLoading) {
67
        //   searchSubmit(currentPage, route.params.roomId, route.params.placeId)
68
        // }
69
      }
70
      else {
71
        selectFloor(DEFAULT_FLOOR)
72
      }
73
    }
74
  }, [floorList, route.params.roomId, route.params.placeId])
75

    
76
  useEffect(() => {
77
    if (floorList) {
78
      if (!inventoriesLoading) {
79
        dispatch(getPlanInventories({ room: selectedRoom?.id ?? 0, place: selectedPlace?.id }))
80
      }
81
    }
82
  }, [selectedPlace, selectedRoom])
83

    
84
  useEffect(() => {
85
    if (selectedFloor) {
86
      console.log("Dispatching getFloorImage " + selectedFloor.id)
87
      dispatch(getPlanFloorImage(selectedFloor.id))
88
    }
89
  }, [selectedFloor])
90

    
91

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

    
110
    console.log("searching " + roomId + ", " + placeId)
111

    
112
    dispatch(getPlanInventories({ room: roomId, place: placeId }))
113
  }
114

    
115
  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
  const clearForm = () => {
167
    log.debug("PlanView", "clearForm")
168
    setSelectedPlace(undefined)
169
    setSelectedRoom(undefined)
170
    selectFloor(DEFAULT_FLOOR)
171
  }
172

    
173
  const onBackPressed = () => {
174
    log.debug("back pressed")
175
    navigation.goBack();
176
  }
177

    
178
  return (
179
    <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
                    return (
204
                      <Select.Item value={floor.id} label={floor.label} />
205
                    );
206
                  })}
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
                }
230

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

    
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
                    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
          {selectedFloor && !floorListLoading && (
297
            floorMapImageLoading ?
298
              <LoadingBox text="Loading castle plan" />
299
              :
300
              floorMapImage &&
301
              <Box mt={2.5} ml={2.5}>
302
                <CastlePlanView mapImage={floorMapImage} selectedRoom={selectedRoom} />
303
              </Box>
304
          )
305
          }
306

    
307
          {/* Item List */}
308
          <>
309
            {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
          </>
332

    
333
        </VStack>
334
      </ScrollView>
335
    </Center>
336

    
337

    
338
  );
339
}
340

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