-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.jsx
More file actions
310 lines (277 loc) · 11.2 KB
/
index.jsx
File metadata and controls
310 lines (277 loc) · 11.2 KB
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
import * as moment from "moment-timezone";
import PropTypes from "prop-types";
import React, { useState, useEffect } from "react";
import { useTranslation } from "react-i18next";
import InfiniteScroll from "react-infinite-scroll-component";
import { toast } from "sonner";
import styles from "./index.module.scss";
import { offerRequestStatuses, scrollableTargetId } from "../../constant";
import { getEmployees } from "../../services/employee";
import { assignOfferRequest } from "../../services/offerRequestAssignment";
import DropDown from "../drop-down";
import Loader from "../loader";
import Status from "../status";
// It utilizes the 'offerRequestStatuses' object to retrieve the corresponding label for the 'status' prop.
// If 'status' is a valid key in the 'offerRequestStatuses' object, it assigns the label to the 'value' prop of the 'Status' component.
// Additionally, it passes the 'status' prop to the 'statusLabel' prop of the 'Status' component to show relevant label on the UI
function OfferRequestStatus({ status }) {
return (
<Status value={offerRequestStatuses[status]?.label} statusLabel={status} />
);
}
// The 'SubmittedBy' React functional component displays information about a customer who submitted offer request.
// It displays the customer's full name using the 'customer?.fullName' property.
// Additionally, it shows the customer's address, city, and zip code, if available.
function SubmittedBy({ customer }) {
return (
<div className={`${styles.submittedByContainer}`}>
<p>{customer?.fullName}</p>
<p>
{customer?.address}, {customer?.city} {customer?.zipCode}
</p>
</div>
);
}
// The 'TimeStampAndStatus' React functional component is responsible for displaying a status and timestamp of the offer request
// It takes two props, 'status' and 'time', which are optional.
// If 'status' is provided, it displays the status.
// If 'time' is provided, it formats and displays it in two paragraphs: one for the date (DD.MM.yyyy) and one for the time (HH:mm).
function TimeStampAndStatus({ status, time }) {
return (
<div>
{status && <p>{status}</p>}
{time && (
<>
<p>{moment(time).format("DD.MM.yyyy")}</p>
<p>{moment(time).format("HH:mm")}</p>
</>
)}
{!time && <p>-</p>}
</div>
);
}
// The 'AssignedEmployee' React functional component is responsible for managing and
// displaying the assignment of employees to an offer request.
function AssignedEmployee({ employees, request, assignment, className }) {
// It relies on the 'employees' prop, which represents a list of available employees,
// and the 'request' and 'assignment' props to determine the current assignment.
// it utilizes the 'useTranslation' hook to handle internationalization (i18n).
const { t } = useTranslation("lang");
// It maintains a piece of state 'assignedEmployee' which holds the currently assigned employee,
// initializing it with the 'assignment' prop or 'null' if not provided.
const [assignedEmployee, setAssignedEmployee] = useState(assignment || null);
// The 'useEffect' hook updates 'assignedEmployee' when 'assignment' changes.
useEffect(() => {
if (assignment) {
setAssignedEmployee(assignment);
}
}, [assignment]);
const onOfferRequestAssign = async (employeeId) => {
try {
// The 'onOfferRequestAssign' function is an asynchronous function that assigns an employee to the request
// using 'assignOfferRequest' and updates 'assignedEmployee' accordingly.
// 'employee.value' indicates employeeId from the dropdown
await assignOfferRequest({
offerRequestId: request.id,
employeeId,
});
setAssignedEmployee(employees.find((emp) => emp.id === employeeId));
toast.success(t("offer.request.request_assigned"));
} catch (err) {
toast.error(err.message);
}
};
return (
<div>
{/* The component renders a 'DropDown' component with a dropdown list of employees.
It allows selecting an employee for assignment and displays the assigned employee's name.
The dropdown is disabled if there's only one employee available. */}
<DropDown
selected={assignedEmployee?.id}
options={employees}
placeholder="components.data_table.not_assigned"
onChange={(value) => onOfferRequestAssign(value, request)}
className={className}
selectionKey="value"
disabled={Object.keys(employees).length === 1}
/>
</div>
);
}
// The 'DataTable' React component is a versatile table for displaying data, commonly used for managing and viewing lists of offer requests. It can optionally include columns for assigned employees based on the 'showAssignedColumn' prop.
// It receives various props such as 'responseData', 'showAssignedColumn', 'onRowClick', 'isOfferRequestType', 'hasNextPage', and 'fetchNextPage' to configure and display the table.
// Inside the component:
// - It initializes some variables, including the 'headers' array, which defines the table column headers based on the 'showAssignedColumn' prop and translates them using the 'useTranslation' hook.
// - It fetches and sets the 'employees' state if 'showAssignedColumn' is true, allowing the assignment of employees to requests.
// - When 'showAssignedColumn' is enabled, it provides an 'AssignedEmployee' component to assign employees to requests.
function DataTable({
responseData,
showAssignedColumn,
onRowClick,
isOfferRequestType,
hasNextPage,
fetchNextPage,
}) {
const offerRequests = responseData;
const { t } = useTranslation("lang");
const [employees, setEmployees] = useState([]);
let headers = [];
if (showAssignedColumn) {
headers = [
t("components.data_table.submitted_by"),
t("components.data_table.received_time"),
t("components.data_table.completed_time"),
t("components.data_table.assigned_to"),
t("components.data_table.status"),
];
} else {
headers = [
t("components.data_table.submitted_by"),
t("components.data_table.received_time"),
t("components.data_table.completed_time"),
t("components.data_table.status"),
];
}
const fetchEmployees = async () => {
try {
const {
data: { data },
} = await getEmployees();
setEmployees(
data.employees.map((emp) => ({
...emp,
label: `${emp.firstName} ${emp.lastName}`,
value: emp.id,
}))
);
} catch (error) {
toast.error(error.message);
}
};
useEffect(() => {
if (showAssignedColumn) fetchEmployees();
}, []);
return (
<div className={`${styles.dataTableMainContainer}`}>
<div className={`${styles.tableHeadingContainer}`}>
<div
className={`d-flex align-items-center justify-content-between ${styles.flexContainer}`}
>
{headers.map((head) => (
<div key={head}>{head}</div>
))}
</div>
</div>
{/* The 'InfiniteScroll' component is used for handling paginated data,
and it dynamically loads more data when the user scrolls to the bottom of the table. */}
<InfiniteScroll
dataLength={offerRequests.length}
hasMore={hasNextPage}
scrollableTarget={scrollableTargetId}
loader={<Loader />}
next={fetchNextPage}
>
{/* It maps over the 'offerRequests' and renders each row of data,
which includes information about customers, timestamps, assigned employees,
and offer request statuses. The 'moment' library is used for timestamp formatting. */}
{offerRequests.map((request, index) => (
<div
role="button"
onClick={() => onRowClick(request, index)}
tabIndex={0}
onKeyDown={(e) => {
if (e.key === "Enter") {
onRowClick(request, index);
}
}}
className={`d-flex align-items-center justify-content-between ${styles.tableContentContainer} ${styles.flexContainer}`}
key={request.taskId || request.id}
>
<div>
<SubmittedBy customer={request.customer} />
</div>
<div>
<p>{moment(request.createdAt).format("DD.MM.yyyy")}</p>
<p>{moment(request.createdAt).format("HH:mm")}</p>
</div>
<div>
<TimeStampAndStatus
status={
isOfferRequestType
? t(
offerRequestStatuses[
request.offerRequestProgress[1]?.event
]?.readableLabel
)
: ""
}
time={
isOfferRequestType
? request.offerRequestProgress[0]?.createdAt
: request.completedAt
}
/>
</div>
{showAssignedColumn && (
<div
role="button"
tabIndex={0}
onKeyDown={(e) => {
e.stopPropagation();
}}
onClick={(e) => e.stopPropagation()}
>
<AssignedEmployee
employees={employees}
assignment={request.assignment?.employee}
request={request}
className={`${styles.assignedDropDown}`}
/>
</div>
)}
<div className={styles.statusMainContainer}>
<OfferRequestStatus
status={request.offerRequestProgress[0].event}
/>
</div>
</div>
))}
</InfiniteScroll>
</div>
);
}
// AssignedEmployee component props validation
AssignedEmployee.propTypes = {
employees: PropTypes.instanceOf(Array).isRequired, // Expects an array of employees
request: PropTypes.instanceOf(Object).isRequired, // Expects an object for the request
};
// TimeStampAndStatus component props validation
TimeStampAndStatus.propTypes = {
status: PropTypes.string.isRequired, // Expects a string for status
time: PropTypes.string.isRequired, // Expects a string for time
};
// DataTable component props validation
DataTable.propTypes = {
responseData: PropTypes.instanceOf(Object).isRequired, // Expects an object for responseData
showAssignedColumn: PropTypes.bool, // Optional boolean prop
onRowClick: PropTypes.func.isRequired, // Expects a function for onRowClick
isOfferRequestType: PropTypes.bool, // Optional boolean prop
hasNextPage: PropTypes.bool, // Optional boolean prop
fetchNextPage: PropTypes.func, // Optional function prop
};
// DataTable component default props
DataTable.defaultProps = {
showAssignedColumn: true, // Default value for showAssignedColumn
isOfferRequestType: true, // Default value for isOfferRequestType
hasNextPage: false, // Default value for hasNextPage
fetchNextPage: () => {}, // Default value for fetchNextPage, an empty function
};
// SubmittedBy component props validation
SubmittedBy.propTypes = {
customer: PropTypes.instanceOf(Object).isRequired, // Expects an object for the customer
};
// OfferRequestStatus component props validation
OfferRequestStatus.propTypes = {
status: PropTypes.string.isRequired, // Expects a string for status
};
export default DataTable;