Projekt

Obecné

Profil

Stáhnout (9.04 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 { useWindowDimensions } from "react-native"
5
import LoadingBox from "../components/loading/LoadingBox"
6
import { log } from "../logging/logger"
7
import { DrawerScreenProps } from "@react-navigation/drawer"
8
import { RootDrawerParamList } from "./Navigation"
9
import { Box, CloseIcon, Select, useToast, Button, Flex, Text } from "native-base"
10
import { ErrorToast } from "../components/toast/ErrorToast"
11
import { Floor, Place, Room } from "../types/plan"
12
import { getFloorList, getPlanInventories, getPlanItems } from "../stores/actions/planThunks"
13
import { login } from "../stores/actions/userThunks"
14
import { getPlanItemsRequest } from "../api/planservice"
15
import ItemPreview from "../components/listView/ItemPreview"
16
import { parseArtists, parseImage } from "../components/listView/ListViewInventoryGroup"
17
import PlanView from "../components/plan/PlanView"
18
import { BASE_API_URL } from "../api/constants"
19

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

    
22
  const layout = useWindowDimensions();
23

    
24
  // Route: set selected room / place from 
25
  useEffect(() => {
26
    console.log("dispatching get floor list")
27
    // dispatch(login({username: "Viktorie", password: "Golem123."}))
28
    dispatch(getFloorList())
29
  }, [])
30

    
31
  const pageSize = 20
32
  const currentPage = 0
33
  const totalPages = 100
34

    
35
  const DEFAULT_FLOOR = "first_floor"
36

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

    
41
  const dispatch = useDispatch<AppDispatch>();
42

    
43
  const { floorListLoading, floorList, itemListLoading, itemList, itemInventories, itemInventoriesLoading, totalItemsCount, lastError } = useSelector((state: RootState) => state.planViewState)
44

    
45
  const toast = useToast();
46

    
47
  useEffect(() => {
48
    if (floorList && !selectedFloor) {
49
      if (route.params && route.params.roomId) {
50

    
51
        selectRoom(route.params.roomId)
52

    
53
        if (route.params.placeId) {
54
          selectPlace(route.params.placeId)
55
        }
56

    
57
        if (!itemListLoading) {
58
          searchSubmit(currentPage, route.params.roomId, route.params.placeId)
59
        }
60

    
61
      }
62
      else {
63
        selectFloor(DEFAULT_FLOOR)
64
      }
65
    }
66
  }, [floorList, route.params.roomId, route.params.placeId])
67

    
68
  useEffect(() => {
69
    if (floorList && selectedRoom) {
70
      if (!itemInventoriesLoading) {
71
        dispatch(getPlanInventories({ room: selectedRoom.id, place: selectedPlace?.id }))
72
      }
73
    }
74
  }, [selectedPlace, selectedRoom])
75

    
76

    
77
  useEffect(() => {
78
    if (lastError) {
79
      toast.closeAll()
80
      toast.show({
81
        render: ({
82
          id
83
        }) => {
84
          return <ErrorToast headerText={"Error"} text={lastError} onClose={() => toast.close(id)} />;
85
        },
86
        duration: 3000
87
      });
88
    }
89
  }, [lastError])
90

    
91

    
92
  const searchSubmit = (pageNumber: number, roomId: number, placeId?: number) => {
93
    log.debug("PlanView", "searchSubmit")
94

    
95
    console.log("searching " + roomId + ", " + placeId)
96

    
97
    dispatch(getPlanItems({ page: pageNumber, items: pageSize, room: roomId, place: placeId, activeTab: 0 }))
98
  }
99

    
100
  const selectFloor = (floorId: string) => {
101
    if (floorId == selectedFloor?.id) {
102
      return;
103
    }
104

    
105
    for (let i = 0; i < floorList.length; i++) {
106
      let floor = floorList[i]
107
      if (floor.id == floorId) {
108
        setSelectedFloor(floor)
109
        setSelectedRoom(undefined)
110
        setSelectedPlace(undefined)
111
        return;
112
      }
113
    }
114
  }
115

    
116
  const selectRoom = (roomId: number) => {
117
    if (roomId == selectedRoom?.id) {
118
      return;
119
    }
120

    
121
    for (let i = 0; i < floorList.length; i++) {
122
      let floor = floorList[i]
123

    
124
      for (let j = 0; j < floor.roomList.length; j++) {
125
        let room = floor.roomList[j]
126
        if (room.id == roomId) {
127
          setSelectedFloor(floor)
128
          setSelectedRoom(room)
129
          setSelectedPlace(undefined)
130
          return;
131
        }
132
      }
133
    }
134
  }
135

    
136
  const selectPlace = (placeId: number) => {
137
    if (placeId == selectedPlace?.id) {
138
      return;
139
    }
140

    
141
    for (let i = 0; selectedRoom?.places && i < selectedRoom?.places?.length; i++) {
142
      let place = selectedRoom?.places[i]
143
      if (place.id == placeId) {
144
        setSelectedPlace(place)
145
        return;
146
      }
147
    }
148
  }
149

    
150

    
151
  const clearForm = () => {
152
    log.debug("PlanView", "clearForm")
153
    selectFloor(DEFAULT_FLOOR)
154
    setSelectedRoom(undefined)
155
    setSelectedPlace(undefined)
156
  }
157

    
158
  const onBackPressed = () => {
159
    log.debug("back pressed")
160
    navigation.goBack();
161
  }
162

    
163
  return (
164
    <Box>
165
      {/* Selection */}
166

    
167
      {
168
        floorListLoading ?
169
          <LoadingBox text="Floor list loading"></LoadingBox>
170
          :
171
          <>
172
            {/* Floor */}
173
            <Select
174
              mt={2.5}
175
              ml={2.5}
176
              mr={2.5}
177

    
178
              placeholder={"Floor"}
179
              selectedValue={selectedFloor?.id}
180
              onValueChange={selectFloor}
181
              variant="rounded"
182
            >
183
              {floorList.map((floor, index) => {
184
                return (
185
                  <Select.Item value={floor.id} label={floor.label} />
186
                );
187
              })}
188
            </Select>
189

    
190
            {/* Room */}
191
            {selectedFloor && selectedFloor.roomList && selectedFloor.roomList.length > 0 &&
192
              < Select
193
                mt={2.5}
194
                ml={2.5}
195
                mr={2.5}
196

    
197
                placeholder={"Room"}
198
                selectedValue={selectedRoom?.id.toString()}
199
                onValueChange={(value: string) => selectRoom(parseInt(value))}
200
                variant="rounded"
201
              >
202
                {selectedFloor?.roomList?.map((room, index) => {
203
                  if (room.room_list) {
204
                    return (
205
                      <Select.Item value={room.id.toString()} label={room.label} />
206
                    );
207
                  }
208
                })}
209
              </Select>
210
            }
211

    
212
            {/* Place */}
213
            {selectedRoom && selectedRoom.places && selectedRoom.places.length > 0 &&
214
              <Select
215
                mt={2.5}
216
                ml={2.5}
217
                mr={2.5}
218

    
219
                placeholder={"Place"}
220
                selectedValue={selectedPlace?.id.toString()}
221
                onValueChange={(value: string) => selectPlace(parseInt(value))}
222
                variant="rounded"
223
              >
224
                {selectedRoom?.places?.map((place, index) => {
225
                  return (
226
                    <Select.Item value={place.id.toString()} label={place.label} />
227
                  );
228
                })}
229
              </Select>
230
            }
231

    
232
            {/* Filter */}
233
            <Flex direction="row" alignItems="center" justify="flex-end"
234
              mt={2.5}
235
              ml={2.5}
236
              mr={3}
237
            >
238
              <Button
239
                endIcon={
240
                  <CloseIcon size="xs" />
241
                }
242
                onPress={clearForm}
243
                variant="outline"
244
                color="primary.500"
245
                borderColor="primary.100"
246
                mr={2}
247
                borderRadius={15}
248
                size={"md"}
249
              >
250
                Reset
251
              </Button>
252
              <Button
253
                onPress={() => {
254
                  if (selectedRoom && !selectedPlace) {
255
                    searchSubmit(currentPage, selectedRoom.id)
256
                  }
257
                  else if (selectedRoom && selectedPlace) {
258
                    searchSubmit(currentPage, selectedRoom.id, selectedPlace.id)
259
                  }
260

    
261
                }}
262
                colorScheme="primary"
263
                background={"primary.100"}
264
                variant="solid"
265
                borderRadius={15}
266
                size={"md"}
267
              >
268
                Search
269
              </Button>
270
            </Flex>
271
          </>
272
      }
273

    
274

    
275

    
276
      {/* Plan */}
277
      {/* temporary prototype */}
278

    
279
      {selectedFloor && !floorListLoading &&
280
        <PlanView imageUrl={BASE_API_URL + "/downloads/" + selectedFloor.id} />
281
      }
282

    
283
      {/* Item List */}
284
      <>
285
        {itemInventoriesLoading ?
286
          <LoadingBox text="Loading available inventories..." />
287
          :
288
          <>
289
            <Text>TODO view inventories in SearchItemPreview way</Text>
290
            {itemInventories.map((inventory) => {
291
              <Text>{inventory.label}</Text>
292
            })}
293
          </>
294

    
295
        }
296
        {/* 
297
        {itemList.map((item, index) => {
298
          return (
299
            <Box>
300
              <ItemPreview
301
                // @ts-ignore
302
                caption={item.object[0].caption}
303
                title={item.text}
304
                name={parseArtists(item)}
305
                image={parseImage(item)}
306
                itemId={item.xml_id}
307
                inventoryLabel={item.inventory.label}
308
                navigation={navigation}
309
              />
310
            </Box> */}
311
      </>
312

    
313

    
314
    </Box >
315

    
316

    
317
  );
318
}
319

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