Projekt

Obecné

Profil

Stáhnout (5.93 KB) Statistiky
| Větev: | Tag: | Revize:
1 f8083631 Fantič
import { createAsyncThunk } from "@reduxjs/toolkit"
2
import { SortOptions } from "../../types/general";
3
import { Note } from "../../types/note";
4 aac857cf Fantič
import { getPlanAllRequest, getPlanFloorImageRequest, getPlanItemsRequest } from "../../api/planservice";
5 f220395e Fantič
import { Floor, PlanImage, Room } from "../../types/plan";
6 160b7073 Fantič
import { ItemPreviewType } from "../../types/listViewTypes";
7
import { StringLiteralType } from "typescript";
8 a6999074 Fantič
import { Inventory } from "../../types/searchFormTypes";
9 f8083631 Fantič
10 f220395e Fantič
// @ts-ignore
11
import { parseString } from 'react-native-xml2js';
12
13 f8083631 Fantič
export const getFloorList = createAsyncThunk(
14
    "plan/getFloorList",
15
    async () => {
16
        try {
17
            try {
18
                const response = await getPlanAllRequest()
19
                if (response.status === 200) {
20
21
                    let floorsDict: Record<string, Floor> = {
22
                        "ground_floor": { label: "Ground floor", id: "ground_floor", roomList: [] },
23
                        "first_floor": { label: "First floor", id: "first_floor", roomList: [] },
24
                        "second_floor": { label: "Second floor", id: "second_floor", roomList: [] }
25
                    }
26
27
                    const unstructeredRoomList: Room[] = response.data
28
29
                    unstructeredRoomList.map((room) => {
30
                        if (room.floor in floorsDict) {
31
                            floorsDict[room.floor].roomList.push(room)
32
                        }
33
                        else {
34
                            floorsDict[room.floor] = { id: room.floor, label: room.floor, roomList: [room] }
35
                        }
36
                    }
37
                    )
38
39
                    let floorList = []
40
41
                    for (let key in floorsDict) {
42 160b7073 Fantič
                        let floor: Floor = floorsDict[key]
43 f8083631 Fantič
                        floorList.push(floor)
44
                    }
45
46
                    return floorList
47
                } else {
48
                    return Promise.reject(response.data ? response.data : "Get plan all request failed")
49
                }
50
            } catch (err: any) {
51
                return Promise.reject(err.response.data)
52
            }
53
54 160b7073 Fantič
        } catch (err: any) {
55
            console.log(err);
56
            return Promise.reject(err.response.data)
57
        }
58
    }
59
)
60
61
62
export const getPlanInventories = createAsyncThunk(
63
    "plan/getPlanInventories",
64
    async (params: { room: number, place?: number }) => {
65
        try {
66
            try {
67 a6999074 Fantič
                const response = await getPlanItemsRequest({ cursor: 0, room: params.room, items: 1, place: params.place })
68 160b7073 Fantič
                if (response.status === 200) {
69
70 a6999074 Fantič
                    const inventories: Inventory[] = response.data.pagination.inventories
71
                    const allInventoriesRecords = response.data.pagination.records
72 160b7073 Fantič
73
                    return {
74
                        inventories: inventories,
75 a6999074 Fantič
                        allInventoriesRecords: allInventoriesRecords
76 160b7073 Fantič
                    }
77
                } else {
78
                    return Promise.reject(response.data ? response.data : "Get plan items request failed")
79
                }
80
            } catch (err: any) {
81
                return Promise.reject(err.response.data)
82
            }
83
84
        } catch (err: any) {
85
            console.log(err);
86
            return Promise.reject(err.response.data)
87
        }
88
    })
89
90
export const getPlanItems = createAsyncThunk(
91
    "plan/getPlanItems",
92 a6999074 Fantič
    async (params: { cursor: number, room: number, place?: number, inventory: string }) => {
93 160b7073 Fantič
        try {
94
            try {
95 a6999074 Fantič
                const response = await getPlanItemsRequest({ cursor: params.cursor, room: params.room, place: params.place, inventory: params.inventory })
96 160b7073 Fantič
                if (response.status === 200) {
97
98 a6999074 Fantič
                    const items: ItemPreviewType[] = response.data.data
99
                    const inventoryPagination = {
100
                        cursor: response.data.pagination.cursor,
101
                        items: response.data.pagination.items,
102
                        records: response.data.pagination.records
103
                    }
104 160b7073 Fantič
105
                    return {
106
                        items: items,
107 a6999074 Fantič
                        inventoryPagination: inventoryPagination,
108 160b7073 Fantič
                    }
109
                } else {
110
                    return Promise.reject(response.data ? response.data : "Get plan items request failed")
111
                }
112
            } catch (err: any) {
113
                return Promise.reject(err.response.data)
114
            }
115
116 aac857cf Fantič
        } catch (err: any) {
117
            console.log(err);
118
            return Promise.reject(err.response.data)
119
        }
120
    }
121
)
122
123
export const getPlanFloorImage = createAsyncThunk(
124
    "plan/getPlanFloorImage",
125
    async (floorId: string) => {
126
        try {
127
            try {
128
                const response = await getPlanFloorImageRequest(floorId)
129
130
                if (response.status === 200) {
131 f220395e Fantič
                    
132
                    const svg : string = response.data 
133
134
                    const output : PlanImage = {
135
                        svg: svg,
136 28ab969f Fantič
                        viewBox :{ x:0, y:0, width:0, height:0 }
137 f220395e Fantič
                    }
138
139
                    parseString(svg, (err : any, result: any) => {
140
                        if (!err && result && result.svg && result.svg.$.viewBox) {
141
                          const [x, y, width, height] = result.svg.$.viewBox.split(' ').map(Number);
142
                    
143
                          // Set the SVG information in your component's state or use it as needed
144
                          output.viewBox =  { x, y, width, height };
145
                        }
146
                      });
147
148
                    return output
149 aac857cf Fantič
                } else {
150
                    return Promise.reject(response.data ? response.data : "Get plan floor image request failed")
151
                }
152
            } catch (err: any) {
153
                return Promise.reject(err.response.data)
154
            }
155
156 f8083631 Fantič
        } catch (err: any) {
157
            console.log(err);
158
            return Promise.reject(err.response.data)
159
        }
160
    }
161
)