Projekt

Obecné

Profil

« Předchozí | Další » 

Revize f8083631

Přidáno uživatelem Fantič před více než 1 rok

re #10844: NoteViewPage: GetPlanAll redux implementation

Zobrazit rozdíly:

src/api/planservice.ts
1
import { axiosInstance } from "./api"
2

  
3

  
4
export const getPlanAllRequest = async () => {
5
    return await axiosInstance.get(
6
        `/plan/all`
7
    )
8
}
9

  
10
export const getPlanListRequest = async () => {
11
    return await axiosInstance.get(
12
        `/plan/list`
13
    )
14
}
15

  
16
export const getPlanItemsRequest = async (page: number, pageSize: number, room: number, place?: number) => {
17
    let url = `search_v2?activeTab=0&tabbed=false&cursor=0&page=${page}&items=${pageSize}&room=${room}`
18

  
19
    if(place){
20
        url += `&place=${place}`
21
    }
22

  
23
    return await axiosInstance.get(
24
        url
25
    )
26
}
27

  
src/pages/PlanViewPage.tsx
9 9
import { Box, CloseIcon, Select, useToast, Button, Flex } from "native-base"
10 10
import { ErrorToast } from "../components/toast/ErrorToast"
11 11
import { Floor, Place, Room } from "../types/plan"
12

  
12
import { getFloorList } from "../stores/actions/planThunks"
13
import { login } from "../stores/actions/userThunks"
13 14

  
14 15
const PlanViewPage = ({ route, navigation }: DrawerScreenProps<RootDrawerParamList, 'Plan'>) => {
15 16

  
16 17
  const layout = useWindowDimensions();
17 18

  
18
  // // Route: set selected room / place from 
19
  // useEffect(() => {
20
  //   if (route.params.roomId) {
21
  //     if (route.params.placeId) {
22
  //       // todo
23
  //     }
19
    // Route: set selected room / place from 
20
    useEffect(() => {
21
        console.log("dispatching get floor list")
22
        // dispatch(login({username: "Viktorie", password: "Golem123."}))
23
        dispatch(getFloorList())
24
    }, [])
25

  
24 26

  
25
  //     // todo
26
  //   }
27
  // }, [route.params.roomId, route.params.placeId])
27
  let page = 1
28
  let pageSize = 20
29
  let room = 0
28 30

  
29 31
  const DEFAULT_FLOOR = "first_floor"
30 32

  
......
40 42

  
41 43
  useEffect(() => {
42 44
    if (floorList && !selectedFloor) {
45
      if (route.params && route.params.roomId) {
46

  
47
        selectRoom(route.params.roomId)
48
  
49
        if (route.params.placeId) {
50
          selectPlace(route.params.placeId)
51
        }
52
  
53
        // todo dispatch get items
54
        // getPlanItems(page, pageSize, room)
55
      }
56
      else{
43 57
        selectFloor(DEFAULT_FLOOR)
58
      }
44 59
    }
45
  }, [floorList])
60
  }, [floorList, route.params.roomId, route.params.placeId])
46 61

  
47 62
  useEffect(() => {
48 63
    if (lastError) {
......
58 73
    }
59 74
  }, [lastError])
60 75

  
61
  // // initial: load selected plan
62
  // useEffect(() => {
63
  //   dispatch(getItem(route.params.itemId));
64
  //   log.debug("PlanViewPage", "useEffect", "getPlan", route.params.itemId);
65
  // }, [route.params.itemId])
66 76

  
67 77
  const searchSubmit = () => {
68 78
    log.debug("PlanView", "searchSubmit")
......
137 147
      {/* Selection */}
138 148

  
139 149
      {
140
      floorListLoading ?
141
        <LoadingBox text="Floor list loading"></LoadingBox>
142
        :
143
        <>
144
          {/* Floor */}
145
          <Select
146
            mt={2.5}
147
            ml={2.5}
148
            mr={2.5}
149

  
150
            placeholder={"Floor"}
151
            selectedValue={selectedFloor?.id}
152
            onValueChange={selectFloor}
153
            variant="rounded"
154
          >
155
            {floorList.map((floor, index) => {
156
              return (
157
                <Select.Item value={floor.id} label={floor.label} />
158
              );
159
            })}
160
          </Select>
161

  
162
          {/* Room */}
163
          <Select
164
            mt={2.5}
165
            ml={2.5}
166
            mr={2.5}
167

  
168
            placeholder={"Room"}
169
            selectedValue={selectedRoom?.id.toString()}
170
            onValueChange={(value: string) => selectRoom(parseInt(value))}
171
          variant="rounded"
172
          >
173
          {selectedFloor?.roomList?.map((room, index) => {
174
            return (
175
              <Select.Item value={room.id.toString()} label={room.label} />
176
            );
177
          })}
178
        </Select>
179

  
180
      {/* Place */}
181
      <Select
182
        mt={2.5}
183
        ml={2.5}
184
        mr={2.5}
185

  
186
        placeholder={"Place"}
187
        selectedValue={selectedPlace?.id.toString()}
188
        onValueChange={(value: string) => selectPlace(parseInt(value))}
189
        variant="rounded"
190
      >
191
        {selectedRoom?.places?.map((place, index) => {
192
          return (
193
            <Select.Item value={place.id.toString()} label={place.label} />
194
          );
195
        })}
196
      </Select>
197

  
198
      {/* Filter */}
199
      <Flex direction="row" alignItems="center" justify="flex-end"
200
        mt={2.5}
201
        ml={2.5}
202
        mr={3}
203
      >
204
        <Button
205
          endIcon={
206
            <CloseIcon size="xs" />
207
          }
208
          onPress={clearForm}
209
          variant="outline"
210
          color="primary.500"
211
          borderColor="primary.100"
212
          mr={2}
213
          borderRadius={15}
214
          size={"md"}
215
        >
216
          Reset
217
        </Button>
218
        <Button
219
          onPress={() => searchSubmit()}
220
          colorScheme="primary"
221
          background={"primary.100"}
222
          variant="solid"
223
          borderRadius={15}
224
          size={"md"}
225
        >
226
          Search
227
        </Button>
228
      </Flex>
229
    </>
150
        floorListLoading ?
151
          <LoadingBox text="Floor list loading"></LoadingBox>
152
          :
153
          <>
154
            {/* Floor */}
155
            <Select
156
              mt={2.5}
157
              ml={2.5}
158
              mr={2.5}
159

  
160
              placeholder={"Floor"}
161
              selectedValue={selectedFloor?.id}
162
              onValueChange={selectFloor}
163
              variant="rounded"
164
            >
165
              {floorList.map((floor, index) => {
166
                return (
167
                  <Select.Item value={floor.id} label={floor.label} />
168
                );
169
              })}
170
            </Select>
171

  
172
            {/* Room */}
173
            {selectedFloor && selectedFloor.roomList && selectedFloor.roomList.length > 0 &&
174
              < Select
175
                mt={2.5}
176
                ml={2.5}
177
                mr={2.5}
178

  
179
                placeholder={"Room"}
180
                selectedValue={selectedRoom?.id.toString()}
181
                onValueChange={(value: string) => selectRoom(parseInt(value))}
182
                variant="rounded"
183
              >
184
                {selectedFloor?.roomList?.map((room, index) => {
185
                  if (room.room_list) {
186
                    return (
187
                      <Select.Item value={room.id.toString()} label={room.label} />
188
                    );
189
                  }
190
                })}
191
              </Select>
192
            }
193

  
194
            {/* Place */}
195
            {selectedRoom && selectedRoom.places && selectedRoom.places.length > 0 &&
196
              <Select
197
                mt={2.5}
198
                ml={2.5}
199
                mr={2.5}
200

  
201
                placeholder={"Place"}
202
                selectedValue={selectedPlace?.id.toString()}
203
                onValueChange={(value: string) => selectPlace(parseInt(value))}
204
                variant="rounded"
205
              >
206
                {selectedRoom?.places?.map((place, index) => {
207
                  return (
208
                    <Select.Item value={place.id.toString()} label={place.label} />
209
                  );
210
                })}
211
              </Select>
212
            }
213

  
214
            {/* Filter */}
215
            <Flex direction="row" alignItems="center" justify="flex-end"
216
              mt={2.5}
217
              ml={2.5}
218
              mr={3}
219
            >
220
              <Button
221
                endIcon={
222
                  <CloseIcon size="xs" />
223
                }
224
                onPress={clearForm}
225
                variant="outline"
226
                color="primary.500"
227
                borderColor="primary.100"
228
                mr={2}
229
                borderRadius={15}
230
                size={"md"}
231
              >
232
                Reset
233
              </Button>
234
              <Button
235
                onPress={() => searchSubmit()}
236
                colorScheme="primary"
237
                background={"primary.100"}
238
                variant="solid"
239
                borderRadius={15}
240
                size={"md"}
241
              >
242
                Search
243
              </Button>
244
            </Flex>
245
          </>
230 246
      }
231 247

  
232 248

  
233 249

  
234
{/* Plan */ }
250
      {/* Plan */}
235 251

  
236 252

  
237
{/* Item List */ }
253
      {/* Item List */}
238 254

  
239
{/* <ItemPreview
255
      {/* <ItemPreview
240 256
          // @ts-ignore
241 257
          caption={item.object[0].caption}
242 258
          title={item.text}
src/stores/actions/planThunks.ts
1
import { createAsyncThunk } from "@reduxjs/toolkit"
2
import { SortOptions } from "../../types/general";
3
import { Note } from "../../types/note";
4
import { getPlanAllRequest } from "../../api/planservice";
5
import { Floor, Room } from "../../types/plan";
6

  
7
export const getFloorList = createAsyncThunk(
8
    "plan/getFloorList",
9
    async () => {
10
        try {
11
            try {
12
                const response = await getPlanAllRequest()
13
                if (response.status === 200) {
14

  
15
                    let floorsDict: Record<string, Floor> = {
16
                        "ground_floor": { label: "Ground floor", id: "ground_floor", roomList: [] },
17
                        "first_floor": { label: "First floor", id: "first_floor", roomList: [] },
18
                        "second_floor": { label: "Second floor", id: "second_floor", roomList: [] }
19
                    }
20

  
21
                    const unstructeredRoomList: Room[] = response.data
22

  
23
                    unstructeredRoomList.map((room) => {
24
                        if (room.floor in floorsDict) {
25
                            floorsDict[room.floor].roomList.push(room)
26
                        }
27
                        else {
28
                            floorsDict[room.floor] = { id: room.floor, label: room.floor, roomList: [room] }
29
                        }
30
                    }
31
                    )
32

  
33
                    let floorList = []
34

  
35
                    for (let key in floorsDict) {
36
                        let floor : Floor = floorsDict[key]
37
                        floorList.push(floor)
38
                    }
39

  
40
                    return floorList
41
                } else {
42
                    return Promise.reject(response.data ? response.data : "Get plan all request failed")
43
                }
44
            } catch (err: any) {
45
                return Promise.reject(err.response.data)
46
            }
47

  
48
        } catch (err: any) {
49
            console.log(err);
50
            return Promise.reject(err.response.data)
51
        }
52
    }
53
)
src/stores/reducers/planSlice.ts
1 1
import { PayloadAction, createSlice } from "@reduxjs/toolkit"
2 2
import { PlanViewState } from "../../types/plan";
3
import { getFloorList } from "../actions/planThunks";
3 4

  
4 5
const initialState: PlanViewState = {
5 6
    lastError: "",
6
   
7

  
7 8

  
8 9

  
9 10
    // TODO backend -> retrieve list of floors, not static -> not implemented at the time of coding
......
23 24
    initialState: initialState,
24 25
    reducers: {},
25 26
    extraReducers: (builder) => {
27

  
28
        builder.addCase(getFloorList.fulfilled, (state, action) => {
29

  
30
            state.floorList = action.payload
31
            state.floorListLoading = false;
32
        })
33
        builder.addCase(getFloorList.pending, (state, action) => {
34
            state.floorListLoading = true;
35
        })
36
        builder.addCase(getFloorList.rejected, (state, action) => {
37
            state.floorListLoading = false;
38
            state.lastError = action.error.message
39
        })
26 40
    }
27 41
})
28 42

  

Také k dispozici: Unified diff