-
Notifications
You must be signed in to change notification settings - Fork 1
/
AllComplaints.jsx
394 lines (345 loc) · 11.2 KB
/
AllComplaints.jsx
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import React, { useContext, useEffect, useRef, useState } from "react";
import { Table, Input, Form, Popconfirm } from "antd";
import axios from "axios";
import './AllComplaints.css';
import LoginNavBar from '../../components/LoginNavBar/LoginNavBar'
import 'antd/dist/antd.css';
import Accordion from 'react-bootstrap/Accordion';
const { Search } = Input;
import Posts from "../../components/Posts/Posts"
import complaint from "../../assests/complaint.png";
import Toast from 'react-bootstrap/Toast';
import AdminPosts from "../../components/AdminPosts/AdminPosts";
import Spinner from "../../components/Spinner/Spinner";
import { useRoutes } from "react-router-dom";
const EditableContext = React.createContext(null)
const EditableRow = ({ index, ...props }) => {
const [form] = Form.useForm()
return (
<Form form={form} component={false}>
<EditableContext.Provider value={form}>
<tr {...props} />
</EditableContext.Provider>
</Form>
)
}
const EditableCell = ({
title,
editable,
children,
dataIndex,
record,
handleSave,
...restProps
}) => {
const [editing, setEditing] = useState(false)
const inputRef = useRef(null)
const form = useContext(EditableContext)
useEffect(() => {
if (editing) {
inputRef.current.focus()
}
}, [editing])
const toggleEdit = () => {
setEditing(!editing)
form.setFieldsValue({ [dataIndex]: record[dataIndex] })
}
const save = async () => {
try {
const values = await form.validateFields()
toggleEdit()
handleSave({ ...record, ...values })
} catch (errInfo) {
console.log("Save failed:", errInfo)
}
}
let childNode = children
if (editable) {
childNode = editing ? (
<Form.Item
style={{ margin: 0 }}
name={dataIndex}
rules={[
{
required: true,
message: `${title} is required.`
}
]}
>
<Input ref={inputRef} onPressEnter={save} onBlur={save} />
</Form.Item>
) : (
<div
className="editable-cell-value-wrap"
style={{ paddingRight: 24 }}
onClick={toggleEdit}
>
{children}
</div>
)
}
return <td {...restProps}>{childNode}</td>
}
const fetchUsers = async () => {
const { data } = await axios.get(
"https://lodha-backend.onrender.com/api/v1/AllComplaints"
);
const users = data.complaints;
return users;
};
function AllComplaints() {
const [loading1, setloading1] = useState(true);
const [Issues, setIssues] = useState([]);
const [dataSource, setDataSource] = useState([])
const [searchVal, setSearchVal] = useState("")
const [origData, setOrigData] = useState([]);
const [searchIndex, setSearchIndex] = useState([]);
const [loading, setLoading] = useState(true);
const [isvalid, setIsvalid] = useState(false);
const [selectedOption, setSelectedOption] = useState("all");
const [filteredData, setFilteredData] = useState([]);
const refreshPage = () => {
window.location.reload();
}
useEffect(() => {
setLoading(true);
const crawl = (user, allValues) => {
if (!allValues) allValues = [];
for (var key in user) {
if (typeof user[key] === "object") crawl(user[key], allValues);
else if (key !== "Description") {
if (key === "Time") {
let timeStamp = Date.parse(user[key]);
var date1 = new Date(timeStamp);
var date2 = new Date();
var Difference_In_Time = date2.getTime() - date1.getTime();
var days = Difference_In_Time / (1000 * 3600 * 24);
days = (Math.ceil(days)).toString();
user[key] = days + ((days > 1) ? " days " : " day ");
allValues.push(days + " ");
}
else {
allValues.push(user[key] + " ");
}
}
}
return allValues;
};
const fetchData = async () => {
const users = await fetchUsers();
setloading1(false);
setOrigData(users);
setFilteredData(users);
const searchInd = users.map(user => {
const allValues = crawl(user);
return { allValues: allValues.toString() };
});
setSearchIndex(searchInd);
if (users) setLoading(false);
};
fetchData();
}, [selectedOption]);
useEffect(() => {
if (searchVal) {
const reqData = searchIndex.map((user, index) => {
if (user.allValues.toLowerCase().indexOf(searchVal.toLowerCase()) >= 0)
return origData[index];
return null;
});
setFilteredData(
reqData.filter(user => {
if (user) return true;
return false;
})
);
} else {
setFilteredData(origData);
}
}, [searchVal, origData, searchIndex]);
const delete_Issue = async (ele) => {
const issue = await axios.post("https://lodha-backend.onrender.com/api/v1/delete_issue", {
issue: ele.Name,
Admin: JSON.parse(localStorage.getItem("User")).FlatNo
})
setIssues([]);
}
const issue = async (issue) => {
const { data } = await axios.post("https://lodha-backend.onrender.com/api/v1/new_issue", {
issue: issue
})
setIssues([]);
}
const issueSubmit = (e) => {
e.preventDefault();
issue(e.target.issue.value);
}
const fetchIssues = async () => {
const { data } = await axios.get("https://lodha-backend.onrender.com/api/v1/issue_types");
setIssues(data.issues);
}
useEffect(() => {
fetchIssues();
}, [Issues.length]);
const deleteComplaint = async (key) => {
const { data } = await axios.get("https://lodha-backend.onrender.com/api/v1/deletecomplaint", { params: { FlatNo: key.FlatNo, Issue: key.Issue, Description: key.Description } });
refreshPage();
}
const handleDelete = (key) => {
deleteComplaint(key);
const newData = dataSource.filter(item => item.FlatNo !== key.FlatNo)
setDataSource(newData)
}
const confirm = () => {
setIsvalid(true);
}
const defaultColumns = [
{
title: "Flat Number",
dataIndex: "FlatNo",
key: "FlatNo",
editable: false
},
{
title: "Issue",
dataIndex: "Issue",
key: "Issue",
editable: false
},
{
title: "Days Since Posted",
dataIndex: "Time",
key: "Time",
editable: false
},
{
title: "Status",
dataIndex: "Status",
key: "Status",
editable: true
},
]
const updateComplaint = async (row) => {
const { data } = await axios.get("https://lodha-backend.onrender.com/api/v1/updatecomplaint", { params: { complaint: row } });
refreshPage();
}
const handleSave = row => {
updateComplaint(row);
}
const components = {
body: {
row: EditableRow,
cell: EditableCell
}
}
const columns = defaultColumns.map(col => {
if (!col.editable) {
return col
}
return {
...col,
onCell: record => ({
record,
editable: col.editable,
dataIndex: col.dataIndex,
title: col.title,
handleSave
})
}
})
return (
<>
<LoginNavBar />
<div className="KeyContactDiv">
<div style={{ display: "flex", marginTop: "100px", justifyContent:"center", }}>
<img src={complaint} style={{ height: "70px", width: "70px", marginTop : "-3px", marginBottom: "50px", marginRight: "5px"}}></img>
<p id="userDashboardTitle">ALL COMPLAINTS</p>
</div>
<p style={{textAlign:"center", letterSpacing:"1px", fontSize:"20px"}}>All complaints posted by the users will appear here.</p>
<Accordion >
<Accordion.Item eventKey="0" id="IssueAccord" style={{ marginTop: "50px", width: "90%", border: "2px solid #d3d3d3", boxShadow: "rgba(0, 0, 0, 0.35) 0px 5px 15px", }}>
<Accordion.Header>
<p className="IssueTitle">Add New Complaint Type</p>
</Accordion.Header>
<Accordion.Body>
<div>
<form onSubmit={
issueSubmit
} className="ComplaintForm">
<div style={{ display: "flex" }}>
<input placeholder="Complaint Type" type="text" name="issue" className="IssueInput" />
<Popconfirm
title='Click "ok" button to confirm'
onConfirm={confirm}>
<button type="submit" className="IssueButton">Add</button>
</Popconfirm>
</div>
</form>
</div>
<div className="ToastDiv">
{
Issues.map(({ Name }) => {
return (
<Toast onClose={(e) => { e.preventDefault(); delete_Issue({ Name }) }} animation={false} className="ToastIssue">
<Toast.Header>
{Name}
</Toast.Header>
</Toast>);
})
}
</div>
</Accordion.Body>
</Accordion.Item>
</Accordion>
<div className="Note" style={{ marginTop: "50px" }}>
<p className="NoteTitle">NOTE</p>
<ul>
<li className="NoteList">
Status Field can only be edited.
</li>
<li className="NoteList">
Please Enter the value to be Edited in the Input and press enter button to be edited.
</li>
<li className="NoteList">
Please Press Delete button to delete the Complaint details
</li>
</ul>
</div>
<Search
onChange={e => setSearchVal(e.target.value)}
placeholder="Search Complaint"
enterButton
size="large"
style={{ width: "90%", marginTop: "50px", border: "1px solid black", borderRadius: "5px" }}
/>
<div style={{ display: "flex", flexDirection: "column", marginTop: "30px" }}>
<p style={{ fontSize: "20px", letterSpacing: "1px", marginBottom:"0px"}}>Filter Complaints</p>
<select onChange={(e) => setSelectedOption(e.target.value)} style={{ width: "300px", height: "50px", padding: "10px", borderRadius: "10px", border: "2px solid black" }}>
<option value="all" >All</option>
<option value="month">More than 1 month</option>
<option value="twomonths">More than 2 months</option>
<option value="threemonths">More than 3 months</option>
<option value="fourmonths">More than 4 months</option>
<option value="fivemonths">More than 5 months</option>
<option value="sixmonths">More than 6 Months</option>
</select>
</div>
<div className="AdminPostsDiv">
{
loading ?
<Spinner />
:
filteredData.length===0 ?
<>
<p style={{fontSize:"16px", letterSpacing:"1px", textAlign:"center"}}>No Complaints !</p>
</>
:
filteredData.map(item => <AdminPosts props={item} selectedOption={selectedOption} />)
}
</div>
</div>
<div style={{ height: "100px", color: "white" }}>
</div>
</>
);
}
export default AllComplaints;