-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuildingsTab.tsx
209 lines (191 loc) · 7.75 KB
/
BuildingsTab.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
197
198
199
200
201
202
203
204
205
206
207
208
209
import { AutoComplete, Breadcrumb, Button, Card, Empty, Input, Layout, Row, Select, message } from "antd";
import { useEffect } from "react";
import { useState } from "react";
import { fetchBuildings } from "../../reducers/buildings";
import LoadingSpinner from '../../Components/LoadingSpinner';
import moment from "moment";
import EditBuildingModal from "./EditBuildingModal";
import { useNavigate } from "react-router-dom";
import BuildingCard from "./BuildingCard";
import { useAppDispatch, useAppSelector } from "../../hooks";
import { PageHeader } from "@ant-design/pro-components";
import api from "../../api";
import "./style.css"
import { getBills, sortDate } from "../utils";
import { deleteBuilding } from "./utils";
const { Search } = Input;
interface BuildingTabProps {
updateRoute: (arg: string) => void
}
const BuildingTab = ({ updateRoute }: BuildingTabProps) => {
const buildings = useAppSelector((state) => state.buildings.buildings)
const user = useAppSelector((state) => state.user.user)
const navigate = useNavigate()
const dispatch = useAppDispatch()
const [show, setShow] = useState(false)
const [bills, setBills] = useState<any>([])
const [isModalVisible, setIsModalVisible] = useState(false);
const [filter, setFilter] = useState("Address");
const [buildingsFilter, setBuildingsFilter] = useState(buildings);
const [name, setName] = useState("")
const [contact, setContact] = useState("")
const [address, setAddress] = useState("")
const [buildingId, setBuildingId] = useState("")
const [type, setType] = useState("")
const [myMessage, setMessage] = useState("")
useEffect(() => {
getBills(user._id, setBills)
window.scroll(0, 0)
}, [buildings, show])
const getData = (id: string, type: string) => {
const buildingBills = bills?.all?.find((el: any) => el.buildingId === id);
if (!buildingBills) return [];
const orderData = buildingBills.bills
.filter((el: any) => el[type.toLowerCase()] !== undefined)
.map((el: any) => ({
x: moment.utc(el.date).local().format(),
y: el[type.toLowerCase()]
}))
sortDate(orderData)
return [{
name: type,
data: orderData
}];
};
const renderItem = () => {
if (!buildings || buildings.length === undefined || buildings.length === 0) return [];
return buildings.map(({ _id, address, name }) => ({
value: filter === "Address" ? address : name,
label: filter === "Address" ? address : name,
key: _id,
props: _id
}));
};
const renderBuildings = (element: string) => {
const res = buildings.find((el) =>
filter === "Address" ? el.address === element : el.name === element
)
res ? setBuildingsFilter([res]) : setBuildingsFilter([])
};
const updateBuilding = async (buildingId: string) => {
try {
setMessage("Updating...");
setShow(true);
const data = { name, contact, address, type };
await api.buildings.updateBuilding(buildingId, data);
const updatedBuildings = await api.buildings.fetchBuildingsByUserId(user._id);
setBuildingsFilter(updatedBuildings);
dispatch(fetchBuildings(updatedBuildings));
setShow(false);
message.success("Updated successfully");
} catch (error) {
setShow(false);
message.error("Failed to update building");
console.error(error);
}
}
return (
<Layout
className="site-layout-background"
style={{
padding: 24,
minHeight: 280,
}}
>
{show && <LoadingSpinner message={myMessage}></LoadingSpinner>}
<Row gutter={[16, 16]} >
<Breadcrumb
items={[
{
title: 'Home',
},
{
title: <a>Buildings</a>
}
]}
/>
</Row>
<PageHeader
style={{ paddingLeft: 0 }}
className="site-page-header"
title="Buildings Portfolio"
subTitle="Browse and check your buildings"
onBack={() => navigate("/Dashboard")}
/>
<Row style={{ width: "100%" }}>
<Input.Group compact>
<Select
onChange={(val) => setFilter(val)}
defaultValue="Address"
style={{ width: "35%" }}
options={[
{ value: "Address", label: "Address", },
{ value: "Building", label: "Building", }
]}
/>
<AutoComplete
allowClear
onClear={() => {
setBuildingsFilter(buildings)
window.scroll(0, 0)
}}
style={{ width: "65%" }}
dataSource={renderItem() as any}
onSelect={(value) => renderBuildings(value)}
>
<Search placeholder="Search by Name" />
</AutoComplete>
</Input.Group>
</Row>
{
!buildingsFilter || buildingsFilter.length === undefined ?
<Card style={{ marginTop: "32px" }}>
<Empty description="No Buildings found...">
<Button style={{ height: 40, borderRadius: 8 }}
type="primary"
onClick={() => updateRoute("/building/New")}>
Add a new Building to your account!
</Button>
</Empty>
</Card>
:
buildingsFilter.map((item) =>
<BuildingCard
key={item._id}
bills={bills}
deleteBuilding={() =>
deleteBuilding(
item._id,
user._id,
setMessage,
setShow,
setBuildingsFilter,
dispatch
)
}
getData={getData}
setAddress={setAddress}
setBuildingId={setBuildingId}
setContact={setContact}
item={item}
setIsModalVisible={setIsModalVisible}
setName={setName}
setType={setType}
/>)
}
<EditBuildingModal
setName={(val) => setName(val)}
setContact={(val) => setContact(val)}
setType={(val) => setType(val)}
buildingId={buildingId}
name={name}
contact={contact}
address={address}
type={type}
visible={isModalVisible}
setVisible={() => setIsModalVisible(false)}
updateBuilding={() => updateBuilding(buildingId)}
/>
</Layout >)
}
export default (props: BuildingTabProps) => <BuildingTab {...props} />