1
|
import { createAsyncThunk } from "@reduxjs/toolkit"
|
2
|
import { SortOptions } from "../../types/general";
|
3
|
import { Note } from "../../types/note";
|
4
|
import { getPlanAllRequest, getPlanFloorImageRequest, getPlanItemsRequest } from "../../api/planservice";
|
5
|
import { Floor, PlanImage, Room } from "../../types/plan";
|
6
|
import { ItemPreviewType } from "../../types/listViewTypes";
|
7
|
import { StringLiteralType } from "typescript";
|
8
|
import { Inventory } from "../../types/searchFormTypes";
|
9
|
|
10
|
// @ts-ignore
|
11
|
import { parseString } from 'react-native-xml2js';
|
12
|
|
13
|
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
|
let floor: Floor = floorsDict[key]
|
43
|
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
|
} 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
|
const response = await getPlanItemsRequest({ cursor: 0, room: params.room, items: 1, place: params.place })
|
68
|
if (response.status === 200) {
|
69
|
|
70
|
const inventories: Inventory[] = response.data.pagination.inventories
|
71
|
const allInventoriesRecords = response.data.pagination.records
|
72
|
|
73
|
return {
|
74
|
inventories: inventories,
|
75
|
allInventoriesRecords: allInventoriesRecords
|
76
|
}
|
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
|
async (params: { cursor: number, room: number, place?: number, inventory: string }) => {
|
93
|
try {
|
94
|
try {
|
95
|
const response = await getPlanItemsRequest({ cursor: params.cursor, room: params.room, place: params.place, inventory: params.inventory })
|
96
|
if (response.status === 200) {
|
97
|
|
98
|
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
|
|
105
|
return {
|
106
|
items: items,
|
107
|
inventoryPagination: inventoryPagination,
|
108
|
}
|
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
|
} 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
|
|
132
|
const svg : string = response.data
|
133
|
|
134
|
const output : PlanImage = {
|
135
|
svg: svg,
|
136
|
viewBox :{ x:0, y:0, width:0, height:0 }
|
137
|
}
|
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
|
} 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
|
} catch (err: any) {
|
157
|
console.log(err);
|
158
|
return Promise.reject(err.response.data)
|
159
|
}
|
160
|
}
|
161
|
)
|