Projekt

Obecné

Profil

Stáhnout (6.44 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 08be9ecd Fantič
32
                            // TODO handle on backend transformations :-)
33
                            if (room.floor == "first_floor") {
34
                                room.number_x = room.number_x - 1044.46
35
                                room.number_y = room.number_y - 1088.65
36
                            }
37
                            else if (room.floor == "second_floor") {
38
                                room.number_x = room.number_x - 2088.7
39
                                room.number_y = room.number_y - 1088.58
40
                            }
41
42 f8083631 Fantič
                            floorsDict[room.floor].roomList.push(room)
43
                        }
44
                        else {
45
                            floorsDict[room.floor] = { id: room.floor, label: room.floor, roomList: [room] }
46
                        }
47
                    }
48
                    )
49
50
                    let floorList = []
51
52
                    for (let key in floorsDict) {
53 160b7073 Fantič
                        let floor: Floor = floorsDict[key]
54 f8083631 Fantič
                        floorList.push(floor)
55
                    }
56
57
                    return floorList
58
                } else {
59
                    return Promise.reject(response.data ? response.data : "Get plan all request failed")
60
                }
61
            } catch (err: any) {
62
                return Promise.reject(err.response.data)
63
            }
64
65 160b7073 Fantič
        } catch (err: any) {
66
            console.log(err);
67
            return Promise.reject(err.response.data)
68
        }
69
    }
70
)
71
72
73
export const getPlanInventories = createAsyncThunk(
74
    "plan/getPlanInventories",
75
    async (params: { room: number, place?: number }) => {
76
        try {
77
            try {
78 a6999074 Fantič
                const response = await getPlanItemsRequest({ cursor: 0, room: params.room, items: 1, place: params.place })
79 160b7073 Fantič
                if (response.status === 200) {
80
81 a6999074 Fantič
                    const inventories: Inventory[] = response.data.pagination.inventories
82
                    const allInventoriesRecords = response.data.pagination.records
83 160b7073 Fantič
84
                    return {
85
                        inventories: inventories,
86 a6999074 Fantič
                        allInventoriesRecords: allInventoriesRecords
87 160b7073 Fantič
                    }
88
                } else {
89
                    return Promise.reject(response.data ? response.data : "Get plan items request failed")
90
                }
91
            } catch (err: any) {
92
                return Promise.reject(err.response.data)
93
            }
94
95
        } catch (err: any) {
96
            console.log(err);
97
            return Promise.reject(err.response.data)
98
        }
99
    })
100
101
export const getPlanItems = createAsyncThunk(
102
    "plan/getPlanItems",
103 a6999074 Fantič
    async (params: { cursor: number, room: number, place?: number, inventory: string }) => {
104 160b7073 Fantič
        try {
105
            try {
106 a6999074 Fantič
                const response = await getPlanItemsRequest({ cursor: params.cursor, room: params.room, place: params.place, inventory: params.inventory })
107 160b7073 Fantič
                if (response.status === 200) {
108
109 a6999074 Fantič
                    const items: ItemPreviewType[] = response.data.data
110
                    const inventoryPagination = {
111
                        cursor: response.data.pagination.cursor,
112
                        items: response.data.pagination.items,
113
                        records: response.data.pagination.records
114
                    }
115 160b7073 Fantič
116
                    return {
117
                        items: items,
118 a6999074 Fantič
                        inventoryPagination: inventoryPagination,
119 160b7073 Fantič
                    }
120
                } else {
121
                    return Promise.reject(response.data ? response.data : "Get plan items request failed")
122
                }
123
            } catch (err: any) {
124
                return Promise.reject(err.response.data)
125
            }
126
127 aac857cf Fantič
        } catch (err: any) {
128
            console.log(err);
129
            return Promise.reject(err.response.data)
130
        }
131
    }
132
)
133
134
export const getPlanFloorImage = createAsyncThunk(
135
    "plan/getPlanFloorImage",
136
    async (floorId: string) => {
137
        try {
138
            try {
139
                const response = await getPlanFloorImageRequest(floorId)
140
141
                if (response.status === 200) {
142 f220395e Fantič
143 08be9ecd Fantič
                    const svg: string = response.data
144
145
                    const output: PlanImage = {
146 f220395e Fantič
                        svg: svg,
147 08be9ecd Fantič
                        viewBox: { x: 0, y: 0, width: 0, height: 0 }
148 f220395e Fantič
                    }
149
150 08be9ecd Fantič
                    parseString(svg, (err: any, result: any) => {
151 f220395e Fantič
                        if (!err && result && result.svg && result.svg.$.viewBox) {
152 08be9ecd Fantič
                            const [x, y, width, height] = result.svg.$.viewBox.split(' ').map(Number);
153
154
                            // Set the SVG information in your component's state or use it as needed
155
                            output.viewBox = { x, y, width, height };
156 f220395e Fantič
                        }
157 08be9ecd Fantič
                    });
158 f220395e Fantič
159
                    return output
160 aac857cf Fantič
                } else {
161
                    return Promise.reject(response.data ? response.data : "Get plan floor image request failed")
162
                }
163
            } catch (err: any) {
164
                return Promise.reject(err.response.data)
165
            }
166
167 f8083631 Fantič
        } catch (err: any) {
168
            console.log(err);
169
            return Promise.reject(err.response.data)
170
        }
171
    }
172
)