-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddNewBuilding.tsx
196 lines (190 loc) · 8.93 KB
/
AddNewBuilding.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { AutoComplete, Avatar, Breadcrumb, Button, Card, Form, Layout, Row, Select } from "antd"
import Col from "antd/es/grid/col";
import Input from "antd/lib/input/Input";
import { Option } from "antd/lib/mentions";
import { useState } from "react";
import api from "../../api";
import LoadingSpinner from "../../Components/LoadingSpinner";
import { setAllOrganization } from "../../reducers/allOrganization";
import { AccountSubTitle } from "../../Components/CustomComponents"
import { useEffect } from "react";
import BuildingOptions from "./BuildingOptions";
import { ExpandAltOutlined } from '@ant-design/icons';
import "./style.css"
import { useNavigate } from "react-router-dom";
import { PageHeader } from "@ant-design/pro-components";
import { Organization, UserProps } from "../../types";
import { addBuilding, handleCoords, onSelect } from "../../buildingsUtils";
import IconFont from "../../Iconfont";
import { useAppDispatch } from "../../hooks";
const AddNewBuildings = (user: UserProps) => {
const dispatch = useAppDispatch()
const [options, setOptions] = useState([])
const [name, setName] = useState("")
const [contact, setContact] = useState("")
const [address, setAddress] = useState("")
const [type, setType] = useState("")
const [lat, setLat] = useState(0)
const [long, setLon] = useState(0)
const [sqft, setSqft] = useState("")
const [allOrganizations, setOrganizations] = useState<Array<Organization>>([])
const [organizationId, setOrganization] = useState([])
const [show, setShow] = useState(false)
const navigate = useNavigate()
const fetchOrganization = async () =>
await api.organization.fetch().then(res => {
setOrganizations(res)
dispatch(setAllOrganization(res))
}).catch(err => console.log(err))
useEffect(() => {
fetchOrganization()
}, [])
return (
<Layout
className="site-layout-background"
style={{
paddingLeft: 24,
paddingRight: 24,
}}
>
{show && <LoadingSpinner message={"Creating new building..."} />}
<Row gutter={[16, 16]} style={{ marginTop: "32px" }}>
<Breadcrumb
items={[
{
title: 'Home',
},
{
title: <a>Buildings</a>
},
{
title: <a>Create Building</a>
},
]}
/>
</Row>
<PageHeader
style={{ paddingLeft: 0 }}
className="site-page-header"
title="Create Building"
subTitle="Add a new Building to your account"
onBack={() => navigate("/Dashboard")}
/>
<Card style={{ borderRadius: 20, marginTop: "12px", boxShadow: "0 2px 4px rgba(0,0,0,0.2)" }}>
<AccountSubTitle style={{ marginLeft: 15 }}>Add a new building to your account</AccountSubTitle>
<Row gutter={[32, 0]} style={{ marginTop: "32px", }}>
<Col lg={12} md={24}>
<Col span={24}>
<Form.Item
name="name"
rules={[{ required: true, message: 'Please input the building name' }]}
>
<Input
onChange={(e) => setName(e.target.value)}
allowClear
size="large"
placeholder="Building Name"
prefix={<IconFont type="i-bianji" style={{ marginRight: 5 }} />}
/>
</Form.Item>
</Col>
<Col span={24}>
<Form.Item
name="Contact Name"
rules={[{ required: true, message: 'Please input the building name' }]}
>
<Input
onChange={(e) => setContact(e.target.value)}
allowClear
size="large"
placeholder="Building Owner Name"
prefix={<IconFont type="i-shouye" style={{ marginRight: 5 }} />} />
</Form.Item>
</Col>
<Col span={24}>
<Form.Item
name="Building type"
rules={[{ required: true, message: 'Please input the building type' }]}
>
<BuildingOptions setType={setType} />
</Form.Item>
</Col>
</Col>
<Col lg={12} md={24}>
<Col span={24}>
<Form.Item rules={[{ required: true, message: 'Please input the building size' }]}
>
<Input size="large"
onChange={(e) => setSqft(e.target.value)}
min={1}
type={"number"}
allowClear
placeholder="Building Size (Sqmt)"
prefix={<ExpandAltOutlined
style={{ fontSize: 10, paddingTop: 7, paddingBottom: 7, marginRight: 5 }}
/>}
/>
</Form.Item>
</Col>
<Col span={24}>
<Form.Item
name="Address"
rules={[{ required: true, message: 'Please input the building name' }]}
>
<AutoComplete
className="test"
size="large"
allowClear
placeholder="Building Address"
onSearch={(value) => handleCoords(value, setOptions)}
options={options}
onSelect={(value) => onSelect(value, options, setAddress, setLat, setLon)}
/>
</Form.Item>
</Col>
<Col span={24}>
<Form.Item rules={[{ required: true, message: 'Please input the building organization' }]}>
<Select size="large"
data-testid="select1"
placeholder={
<Row align="middle">
<IconFont type="i-dianpu" style={{ marginRight: 5 }} />
Building Organization
</Row>}
onChange={(val) => { setOrganization(val) }}>
{allOrganizations.length > 0 && allOrganizations.map((el) =>
<Option key={el._id} value={el._id}>
<Row align="middle">
<Avatar src={el.icon} style={{ marginRight: 5 }} />{el.name}
</Row>
</Option>
)}
</Select>
</Form.Item>
</Col>
</Col>
</Row>
<Row align="middle" justify="end" style={{ marginRight: 22 }} >
<Button type="primary" style={{ borderRadius: 10 }}
onClick={() =>
addBuilding(
name,
contact,
address,
sqft,
type,
lat,
long,
organizationId,
user,
setShow,
dispatch
)}>
Add
</Button>
</Row>
</Card>
</Layout>
)
}
export default AddNewBuildings