-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildingsUtils.tsx
81 lines (78 loc) · 2.33 KB
/
buildingsUtils.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { message } from "antd";
import api from "./api";
import { fetchBuildings } from "./reducers/buildings";
import { UserProps } from "./types";
import { AppDispatch } from "./store";
export const onSelect = (
value: string,
options: any,
setAddress: (arg: string) => void,
setLat: (arg: number) => void,
setLon: (arg: number) => void,
) => {
setAddress(value)
const res = options.filter((el: any) => el.value === value)[0].props
setLat(res.lat)
setLon(res.lon)
};
export const handleCoords = async (address: string, setOptions: (arg: any) => void) => {
const requestOptions = {
method: 'GET',
};
await fetch(`https://api.geoapify.com/v1/geocode/search?text=${address}&format=json&apiKey=e92ee477e1114d5c80988f7fd2d838d6`, requestOptions)
.then(response => response.json())
.then(({ results }) => {
const tmp = results.map((element: any) => ({
label: element.formatted,
value: element.formatted,
key: element.place_id,
props: element
}))
setOptions(tmp)
})
.catch(error => console.log('error', error));
}
export const addBuilding = async (
name: string,
contact: string,
address: string,
sqft: string,
type: string,
lat: number,
long: number,
organizationId: Array<any>,
user: UserProps,
setShow: (arg: boolean) => void,
dispatch: AppDispatch
) => {
if (name === "" || contact === "" || address === "" || sqft === "" || type === "" || organizationId.length === 0)
message.error("Fill the form to submit a building")
const data = {
name,
contact,
userId: user._id,
address,
sqft,
type,
lat,
long,
organizationId: organizationId
}
setShow(true)
try {
await api.buildings.addBuilding(data).then(async () => {
setTimeout(() => {
setShow(false)
message.success("Building created!")
}, 1000);
})
await api.buildings.fetchBuildingsByUserId(user._id).then((res) => {
dispatch(fetchBuildings(res))
})
} catch (error) {
setTimeout(() => {
setShow(false)
message.error("Building not created!")
}, 1000);
}
}