@bjsdistribution/dms-client-return 0.1.44-prod → 0.1.45-prod

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.1.44-prod",
2
+ "version": "0.1.45-prod",
3
3
  "license": "MIT",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
@@ -1,4 +1,4 @@
1
- import React, { useEffect } from 'react';
1
+ import React, { useEffect, useState } from 'react';
2
2
  import OrderActionModal from '../../../../../common/modals/order-action-modal/OrderActionModal';
3
3
  import styled from 'styled-components';
4
4
  import CustomerInformation from './customer-info';
@@ -11,26 +11,28 @@ import {
11
11
  useCurrentAction,
12
12
  useCustomerInfo,
13
13
  useErrorConfirmOrder,
14
- useGlobalStateHitOrder,
15
14
  useNotificationConfirmedDate,
16
15
  useOrderDetails,
17
16
  useOrderDetailsConfirmOrder,
18
17
  useOrderId,
19
18
  usePrevOrderDetails,
20
19
  useSelectionAction,
21
- useStatusDetails,
22
20
  } from '../../../store';
23
-
21
+ import { useConfirmOrderDate } from '../../store';
24
22
  import { ApiResponse } from '../../../types';
25
23
  import { compareOrderDetailsStates, filterObject } from '../../../utils';
26
24
  import { format } from 'date-fns';
27
- import { useConfirmOrderDate } from '../../store';
25
+ import SendComms from './send-comms/SendComms';
26
+ // import { useClientPortal } from '../../../../store';
28
27
 
29
28
  interface IProps {
29
+ callOrdersApi: any;
30
+ callOrderStatusApi: any;
30
31
  callOrderAlertsApi: any;
31
32
  }
32
33
 
33
34
  function ConfirmOrder(props: IProps) {
35
+ // const [isClientPortal] = useClientPortal();
34
36
  const [, setCurrentAction] = useCurrentAction();
35
37
  const [orderDetails, setOrderDetails] = useOrderDetails();
36
38
  const [prevOrderDetails, setPrevOrderDetails] = usePrevOrderDetails();
@@ -43,11 +45,12 @@ function ConfirmOrder(props: IProps) {
43
45
  const [deliveryDate, setDeliveryDate] = useConfirmOrderDate();
44
46
  const [orderId] = useOrderId();
45
47
  const [errorState] = useErrorConfirmOrder();
46
- const [, setRecallOrders] = useGlobalStateHitOrder();
47
48
  const [confirmedDate] = useNotificationConfirmedDate();
48
49
  const [selectedAction] = useSelectionAction();
49
- const [, setOrderStatus] = useStatusDetails();
50
-
50
+ const [confirmOrderSendComms, setConfirmOrderSendComms] = useState({
51
+ sms: true,
52
+ email: true,
53
+ });
51
54
  const contactHome = orderDetailsConfirmOrder?.order_recipient?.contact_home;
52
55
  const contactMobile =
53
56
  orderDetailsConfirmOrder?.order_recipient?.contact_mobile;
@@ -65,7 +68,7 @@ function ConfirmOrder(props: IProps) {
65
68
  const { fetchData: notificationCall } = useAxios<{
66
69
  data: ApiResponse[];
67
70
  }>({
68
- endpoint: `orders/${orderId}/send-comms/?notification_type={Confirm - Accepted} `,
71
+ endpoint: `orders/${orderId}/send-comms/?notification_type=Confirm - Attempted&send_sms=${confirmOrderSendComms?.sms}&send_email=${confirmOrderSendComms?.email}`,
69
72
  method: 'POST',
70
73
  });
71
74
 
@@ -79,43 +82,31 @@ function ConfirmOrder(props: IProps) {
79
82
  const { fetchData: confirmDeliveryDate } = useAxios<{
80
83
  data: ApiResponse[];
81
84
  }>({
82
- endpoint: `orders/${orderId}/confirm-date/?date=${confirmedDate?.currentDate}`,
85
+ endpoint: `orders/${orderId}/confirm-date/?date=${confirmedDate?.currentDate}&send_sms=${confirmOrderSendComms?.sms}&send_email=${confirmOrderSendComms?.email}`,
83
86
  method: 'POST',
84
87
  });
85
88
 
86
89
  const { fetchData: sendComs } = useAxios<{
87
90
  data: ApiResponse[];
88
91
  }>({
89
- endpoint: `orders/${orderId}/send-comms/?notification_type=Precall`,
92
+ endpoint: `orders/${orderId}/send-comms/?notification_type=Precall&send_sms=${confirmOrderSendComms?.sms}&send_email=${confirmOrderSendComms?.email}`,
90
93
  method: 'POST',
91
94
  });
92
95
 
93
96
  const { fetchData: noResponseApi } = useAxios<{
94
97
  data: ApiResponse[];
95
98
  }>({
96
- endpoint: `orders/${orderId}/no-response-date/?date=${confirmedDate?.currentDate}`,
99
+ endpoint: `orders/${orderId}/no-response-date/?date=${confirmedDate?.currentDate}&send_sms=${confirmOrderSendComms?.sms}&send_email=${confirmOrderSendComms?.email}`,
97
100
  method: 'POST',
98
101
  });
99
102
 
100
103
  const { fetchData: proposeDate } = useAxios<{
101
104
  data: ApiResponse[];
102
105
  }>({
103
- endpoint: `orders/${orderId}/propose-date/?date=${confirmedDate?.currentDate}`,
106
+ endpoint: `orders/${orderId}/propose-date/?date=${confirmedDate?.currentDate}&send_sms=${confirmOrderSendComms?.sms}&send_email=${confirmOrderSendComms?.email}`,
104
107
  method: 'POST',
105
108
  });
106
109
 
107
- //Order status api
108
- const {
109
- fetchData: fetchStatusData,
110
- data: statusData,
111
- loading: statusDataLoading,
112
- } = useAxios<{
113
- data: ApiResponse[];
114
- }>({
115
- endpoint: `/orders/get_live_status/${orderId}`,
116
- method: 'GET',
117
- });
118
-
119
110
  const onClose = () => {
120
111
  setCurrentAction(undefined);
121
112
  };
@@ -128,69 +119,99 @@ function ConfirmOrder(props: IProps) {
128
119
  };
129
120
  }, []);
130
121
 
131
- useEffect(() => {
132
- if (statusData && statusData?.data) {
133
- setOrderStatus(statusData?.data);
134
- }
135
- }, [statusDataLoading]);
136
-
137
- // THIS HAS BEEN REMOVED AS WE HAVE NEW APIS
138
- // const {
139
- // loading: loadingForConfirm,
140
- // data: response,
141
- // setShouldPerformAction: setPerformAction,
142
- // } = useApiHook<{
143
- // data: ApiResponse[];
144
- // }>({
145
- // endpoint: `orders/${orderId}/confirm-date/?date=${deliveryDate}`,
146
- // method: 'POST',
147
- // performAction: false,
148
- // });
149
-
150
122
  function hasOnlyIdKey(obj: any) {
151
123
  return Object.keys(obj).length === 1 && obj.hasOwnProperty('id');
152
124
  }
153
125
 
126
+ const handleSuccess = (message: string, res?: any) => {
127
+ const apiMessage =
128
+ res?.data?.message || res?.message || res?.data?.data?.message;
129
+
130
+ notification.success({
131
+ message: 'Success',
132
+ description: apiMessage || message,
133
+ });
134
+ };
135
+
136
+ const handleError = (error: any, fallback: string) => {
137
+ const err = error as any;
138
+ notification.error({
139
+ message: 'Request Failed',
140
+ description:
141
+ err?.response?.data?.error ||
142
+ err?.response?.data?.message ||
143
+ err?.message ||
144
+ fallback,
145
+ });
146
+ };
147
+
154
148
  const executeActions = async () => {
155
149
  switch (selectedAction) {
156
150
  case 'decline_and_propose_new_date':
157
- // call propose date api
158
- if (confirmedDate?.previousDate !== null) {
159
- await declineDate();
151
+ try {
152
+ if (confirmedDate?.previousDate !== null) {
153
+ const response = (await declineDate()) as any;
154
+ handleSuccess('Previous date declined successfully', response);
155
+ }
156
+
157
+ const response = (await proposeDate()) as any;
158
+ handleSuccess('New date proposed successfully', response);
159
+ } catch (error) {
160
+ handleError(error, 'Failed to propose new date');
160
161
  }
161
- await proposeDate();
162
- await fetchStatusData();
163
162
  break;
164
163
 
165
164
  case 'decline_and_confirm_new_date':
166
- if (confirmedDate?.previousDate !== null) {
167
- await declineDate();
165
+ try {
166
+ if (confirmedDate?.previousDate !== null) {
167
+ const response = (await declineDate()) as any;
168
+ handleSuccess('Previous date declined successfully', response);
169
+ }
170
+
171
+ const response = (await confirmDeliveryDate()) as any;
172
+ handleSuccess('New date confirmed successfully', response);
173
+ } catch (error) {
174
+ handleError(error, 'Failed to confirm new date');
168
175
  }
169
- await confirmDeliveryDate();
170
- await fetchStatusData();
171
176
  break;
172
177
 
173
178
  case 'declined_proposed_date(outstanding)':
174
- if (confirmedDate?.previousDate !== null) {
175
- await declineDate();
179
+ try {
180
+ if (confirmedDate?.previousDate !== null) {
181
+ const response = (await declineDate()) as any;
182
+ handleSuccess('Proposed date declined successfully', response);
183
+ }
184
+ } catch (error) {
185
+ handleError(error, 'Failed to decline proposed date');
176
186
  }
177
- await fetchStatusData();
178
187
  break;
179
188
 
180
189
  case 'no_response_propose_new_date':
181
- if (confirmedDate?.previousDate !== null) {
182
- await declineDate();
190
+ try {
191
+ if (confirmedDate?.previousDate !== null) {
192
+ const response = (await declineDate()) as any;
193
+ handleSuccess('Previous date declined successfully', response);
194
+ }
195
+
196
+ const response = (await noResponseApi()) as any;
197
+ handleSuccess('No response date proposed successfully', response);
198
+ } catch (error) {
199
+ handleError(error, 'Failed to propose date (no response)');
183
200
  }
184
- await noResponseApi();
185
- await fetchStatusData();
186
201
  break;
187
202
 
188
203
  case 'confirmed':
189
- if (confirmedDate?.previousDate !== null) {
190
- await declineDate();
204
+ try {
205
+ if (confirmedDate?.previousDate !== null) {
206
+ const response = (await declineDate()) as any;
207
+ handleSuccess('Previous date declined successfully', response);
208
+ }
209
+
210
+ const response = (await confirmDeliveryDate()) as any;
211
+ handleSuccess('Delivery date confirmed successfully', response);
212
+ } catch (error) {
213
+ handleError(error, 'Failed to confirm delivery date');
191
214
  }
192
- await confirmDeliveryDate();
193
- await fetchStatusData();
194
215
  break;
195
216
 
196
217
  default:
@@ -203,115 +224,167 @@ function ConfirmOrder(props: IProps) {
203
224
  case 'left_message':
204
225
  case 'contact_made':
205
226
  case 'no_contact':
206
- // await confirmDeliveryDate();
207
- notificationCall();
208
- await fetchStatusData();
227
+ try {
228
+ const response = (await notificationCall()) as any;
229
+ handleSuccess('Notification sent successfully', response);
230
+ } catch (error) {
231
+ handleError(error, 'Failed to send notification');
232
+ }
209
233
  break;
210
234
 
211
235
  case 'precall':
212
- if (confirmedDate?.previousDate !== null) {
213
- await declineDate();
236
+ try {
237
+ if (confirmedDate?.previousDate !== null) {
238
+ const response = (await declineDate()) as any;
239
+ handleSuccess('Previous date declined successfully', response);
240
+ }
241
+
242
+ const response = (await sendComs()) as any;
243
+ handleSuccess('Precall communication sent successfully', response);
244
+ } catch (error) {
245
+ handleError(error, 'Failed to send precall communication');
214
246
  }
215
- // await confirmDeliveryDate();
216
- sendComs();
217
- await fetchStatusData();
218
247
  break;
248
+
219
249
  default:
220
250
  break;
221
251
  }
222
252
  };
223
253
 
224
254
  const confirmOrder = async () => {
225
- setRecallOrders(false);
226
-
227
- if (
228
- selectedAction === 'left_message' ||
229
- selectedAction === 'contact_made' ||
230
- selectedAction === 'no_contact' ||
231
- selectedAction === 'precall'
232
- ) {
233
- if (
234
- contactHome?.length === 0 &&
235
- contactMobile?.length === 0 &&
236
- contactWork?.length === 0
237
- ) {
238
- notification.info({
239
- message: 'Info',
240
- description: 'Please add contact information',
241
- });
242
- return;
243
- }
255
+ try {
244
256
  if (
245
- confirmedDate?.previousDate === null &&
246
- confirmedDate?.currentDate === null
257
+ selectedAction === 'left_message' ||
258
+ selectedAction === 'contact_made' ||
259
+ selectedAction === 'no_contact' ||
260
+ selectedAction === 'precall'
247
261
  ) {
248
- notification.info({
249
- message: 'Info',
250
- description: 'Please add delivery date',
251
- });
252
- return;
262
+ if (
263
+ contactHome?.length === 0 &&
264
+ contactMobile?.length === 0 &&
265
+ contactWork?.length === 0
266
+ ) {
267
+ notification.info({
268
+ message: 'Info',
269
+ description: 'Please add contact information',
270
+ });
271
+ return;
272
+ }
273
+
274
+ if (
275
+ confirmedDate?.previousDate === null &&
276
+ confirmedDate?.currentDate === null
277
+ ) {
278
+ notification.info({
279
+ message: 'Info',
280
+ description: 'Please add delivery date',
281
+ });
282
+ return;
283
+ }
253
284
  }
254
- }
255
285
 
256
- const keysToRemovefromOrders = [
257
- 'order_locked',
258
- 'order_communications',
259
- 'updated_at',
260
- 'updated_by',
261
- 'stock_alias',
262
- 'order_delivery_declined_dates',
263
- 'order_eta_updates',
264
- ];
265
-
266
- const filteredOrderDetails = filterObject(
267
- orderDetailsConfirmOrder,
268
- keysToRemovefromOrders
269
- );
270
- const filteredPrevOrderDetails = filterObject(
271
- prevOrderDetails,
272
- keysToRemovefromOrders
273
- );
274
- const payload = compareOrderDetailsStates(
275
- filteredPrevOrderDetails,
276
- filteredOrderDetails
277
- );
278
- const notUpdateOrder = hasOnlyIdKey(payload);
279
-
280
- if (!notUpdateOrder) {
286
+ const keysToRemovefromOrders = [
287
+ 'order_locked',
288
+ 'order_communications',
289
+ 'updated_at',
290
+ 'updated_by',
291
+ 'stock_alias',
292
+ 'order_delivery_declined_dates',
293
+ 'order_eta_updates',
294
+ 'order_status',
295
+ 'tomtom_status',
296
+ 'refused_service',
297
+ 'order_type',
298
+ 'order_failures',
299
+ 'order_on_hold',
300
+ 'order_cancellations',
301
+ 'order_routes',
302
+ ];
303
+
304
+ const filteredOrderDetails = filterObject(
305
+ orderDetailsConfirmOrder,
306
+ keysToRemovefromOrders
307
+ );
308
+
309
+ const filteredPrevOrderDetails = filterObject(
310
+ prevOrderDetails,
311
+ keysToRemovefromOrders
312
+ );
313
+
314
+ const payload = compareOrderDetailsStates(
315
+ filteredPrevOrderDetails,
316
+ filteredOrderDetails
317
+ );
318
+
281
319
  const updatedPayload = {
282
320
  ...payload,
283
321
  action_date: confirmedDate?.currentDate,
284
322
  };
285
323
 
286
- const response = (await callUpdateOrderAPI({
287
- payload: [updatedPayload],
288
- })) as {
289
- code: number | null;
290
- error?: string | null | undefined;
291
- [key: string]: any;
292
- };
324
+ const notUpdateOrder = hasOnlyIdKey(updatedPayload);
325
+
326
+ if (!notUpdateOrder) {
327
+ try {
328
+ const response = (await callUpdateOrderAPI({
329
+ payload: [updatedPayload],
330
+ })) as any;
331
+
332
+ if (response?.data?.length > 0) {
333
+ const updatedOrder = response?.data[0];
334
+ setOrderDetails(updatedOrder);
335
+ setPrevOrderDetails(updatedOrder);
336
+ handleSuccess('Order updated successfully', response);
337
+ } else {
338
+ setOrderDetails(prevOrderDetails);
339
+ }
340
+ } catch (error) {
341
+ handleError(error, 'Failed to update order');
342
+ setOrderDetails(prevOrderDetails);
343
+ }
344
+ }
293
345
 
294
- if (response?.data?.length > 0) {
295
- const updatedOrder = response?.data[0];
296
- setOrderDetails(updatedOrder);
297
- setPrevOrderDetails(updatedOrder);
298
- setRecallOrders(true);
299
- } else {
300
- setOrderDetails(prevOrderDetails);
346
+ try {
347
+ await executeActions();
348
+ } catch (error) {
349
+ handleError(error, 'Failed to execute actions');
301
350
  }
302
- }
303
351
 
304
- // Execute actions first
305
- await executeActions();
306
- await executeSendComs();
352
+ try {
353
+ await executeSendComs();
354
+ } catch (error) {
355
+ handleError(error, 'Failed to send communications');
356
+ }
307
357
 
308
- if (orderId && deliveryDate) {
309
- const formattedDate = format(new Date(deliveryDate), 'yyyy-MM-dd');
310
- setDeliveryDate(formattedDate);
311
- }
312
- await props?.callOrderAlertsApi();
358
+ if (orderId && deliveryDate) {
359
+ const formattedDate = format(new Date(deliveryDate), 'yyyy-MM-dd');
360
+ setDeliveryDate(formattedDate);
361
+ }
313
362
 
314
- setCurrentAction(undefined);
363
+ try {
364
+ await props?.callOrderStatusApi();
365
+ handleSuccess('Order status refreshed successfully');
366
+ } catch (error) {
367
+ handleError(error, 'Failed to refresh order status');
368
+ }
369
+
370
+ try {
371
+ await props?.callOrderAlertsApi();
372
+ handleSuccess('Order alerts refreshed successfully');
373
+ } catch (error) {
374
+ handleError(error, 'Failed to refresh alerts');
375
+ }
376
+
377
+ try {
378
+ await props?.callOrdersApi();
379
+ handleSuccess('Orders refreshed successfully');
380
+ } catch (error) {
381
+ handleError(error, 'Failed to refresh orders');
382
+ }
383
+
384
+ handleSuccess('Order processed successfully');
385
+ } catch (error) {
386
+ handleError(error, 'Something went wrong');
387
+ }
315
388
  };
316
389
 
317
390
  const errorFound = () => {
@@ -328,13 +401,19 @@ function ConfirmOrder(props: IProps) {
328
401
  return (
329
402
  <OrderActionModal
330
403
  headerTitle="Confirm Order"
331
- minWidth="28vw"
404
+ minWidth="60vw"
332
405
  showAdditionalInfo
333
406
  infoText={infoText}
334
407
  onCloseHeader={onClose}
335
408
  onSubmit={confirmOrder}
336
409
  submitBtnTitle="Save Details"
337
410
  isSubmitDisabled={errorFound() && disabledButton}
411
+ footerActions={
412
+ <SendComms
413
+ sendCommsState={confirmOrderSendComms}
414
+ setSendCommsState={setConfirmOrderSendComms}
415
+ />
416
+ }
338
417
  >
339
418
  <ContentContainer>
340
419
  <Alert
@@ -375,6 +454,7 @@ const ContentContainer = styled.div`
375
454
  overflow-x: hidden;
376
455
  max-height: 500px;
377
456
  scrollbar-width: thin;
457
+ width: 100%;
378
458
  `;
379
459
 
380
460
  export default ConfirmOrder;
@@ -0,0 +1,86 @@
1
+ import { Checkbox } from 'antd';
2
+ import React from 'react';
3
+ import styled from 'styled-components';
4
+ import { useOrderDetailsConfirmOrder } from '../../../../store';
5
+
6
+ interface IProps {
7
+ sendCommsState: any;
8
+ setSendCommsState: any;
9
+ }
10
+
11
+ const SendComms = (props: IProps) => {
12
+ const [orderDetailsConfirmOrder] = useOrderDetailsConfirmOrder();
13
+
14
+ const onChange = (e: any, type: string) => {
15
+ if (type === 'email') {
16
+ const updatedData = {
17
+ sms: props?.sendCommsState?.sms,
18
+ email: e.target.checked,
19
+ };
20
+ props?.setSendCommsState(updatedData);
21
+ } else {
22
+ const updatedData = {
23
+ sms: e.target.checked,
24
+ email: props?.sendCommsState?.email,
25
+ };
26
+ props?.setSendCommsState(updatedData);
27
+ }
28
+ };
29
+
30
+ const contactHome = orderDetailsConfirmOrder?.order_recipient?.contact_home;
31
+ const contactMobile =
32
+ orderDetailsConfirmOrder?.order_recipient?.contact_mobile;
33
+ const contactWork = orderDetailsConfirmOrder?.order_recipient?.contact_work;
34
+ const contactEmail = orderDetailsConfirmOrder?.order_recipient?.contact_email;
35
+
36
+ const showSms =
37
+ (contactHome?.length !== 0 && contactHome != null) ||
38
+ (contactMobile?.length !== 0 && contactMobile != null) ||
39
+ (contactWork?.length !== 0 && contactWork != null);
40
+
41
+ const showEmail = contactEmail?.length !== 0 && contactEmail != null;
42
+
43
+ return (
44
+ <SendCommsMainContainer>
45
+ {showEmail && (
46
+ <div className="send-comms-container">
47
+ <span className="label">Send Email</span>
48
+ <Checkbox
49
+ checked={props?.sendCommsState?.email}
50
+ onChange={(e: any) => onChange(e, 'email')}
51
+ />
52
+ </div>
53
+ )}
54
+ {showSms && (
55
+ <div className="send-comms-container">
56
+ <span className="label">Send SMS</span>
57
+ <Checkbox
58
+ checked={props?.sendCommsState?.sms}
59
+ onChange={(e: any) => onChange(e, 'sms')}
60
+ />
61
+ </div>
62
+ )}
63
+ </SendCommsMainContainer>
64
+ );
65
+ };
66
+
67
+ const SendCommsMainContainer = styled.div`
68
+ display: flex;
69
+ align-items: center;
70
+ gap: 15px;
71
+
72
+ .send-comms-container {
73
+ display: flex;
74
+ gap: 5px;
75
+ align-items: center;
76
+ .label {
77
+ color: #6a7378;
78
+ font-size: 14px;
79
+ font-weight: 400;
80
+ font-family: 'Lato', sans-serif;
81
+ line-height: 20px;
82
+ }
83
+ }
84
+ `;
85
+
86
+ export default React.memo(SendComms);
@@ -250,7 +250,13 @@ function OrderActions() {
250
250
  />
251
251
  );
252
252
  case 'confirm-order':
253
- return <ConfirmOrder callOrderAlertsApi={callOrderAlertsApi} />;
253
+ return (
254
+ <ConfirmOrder
255
+ callOrdersApi={callOrdersApi}
256
+ callOrderStatusApi={fetchStatusData}
257
+ callOrderAlertsApi={callOrderAlertsApi}
258
+ />
259
+ );
254
260
  case 'send-sms':
255
261
  return <SendSmsModal />;
256
262