-
Notifications
You must be signed in to change notification settings - Fork 122
/
CustomerInfo.tsx
748 lines (728 loc) · 28.6 KB
/
CustomerInfo.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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
/* eslint-disable no-nested-ternary */
/* eslint-disable react/jsx-props-no-spreading */
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable no-shadow */
/* eslint-disable camelcase */
import React, { FC, useEffect } from "react";
import { Column } from "@ant-design/plots";
import { useQueryClient, useMutation } from '@tanstack/react-query';
import { Select, Form, Typography, Tooltip } from "antd";
import dayjs from "dayjs";
import { toast } from "react-toastify";
import { DraftInvoiceType } from "../../types/invoice-type";
import { Customer } from "../../api/api";
import { country_json } from "../../assets/country_codes";
import { integrationsMap } from "../../types/payment-processor-type";
import { CustomerType } from "../../types/customer-type";
import { CustomerCostType } from "../../types/revenue-type";
import { CurrencyType } from "../../types/pricing-unit-type";
import CustomerCard from "./Card/CustomerCard";
import { PencilSquareIcon } from "../base/PencilIcon";
import CopyText from "../base/CopytoClipboard";
import createShortenedText from "../../helpers/createShortenedText";
import useMediaQuery from "../../hooks/useWindowQuery";
import Divider from "../base/Divider/Divider";
import Badge from "../base/Badges/Badges";
import { fourDP } from "../../helpers/fourDP";
import { timezones } from "../../assets/timezones";
import { components } from "../../gen-types";
interface CustomerInfoViewProps {
data: components["schemas"]["CustomerDetail"];
cost_data: CustomerCostType;
pricingUnits: CurrencyType[];
onDateChange: (start_date: string, end_date: string) => void;
refetch: VoidFunction;
}
const CustomerInfoView: FC<CustomerInfoViewProps> = ({
data,
cost_data,
pricingUnits,
onDateChange,
refetch,
}) => {
const windowWidth = useMediaQuery();
const [transformedGraphData, setTransformedGraphData] = React.useState<
{
date: string;
amount: number;
metric: string | undefined;
type: string;
}[]
>([]);
const [form] = Form.useForm();
const [currentCurrency, setCurrentCurrency] = React.useState<string>(
data.default_currency.code ? data.default_currency.code : ""
);
const [taxRate, setTaxRate] = React.useState(0);
const [customerName, setCustomerName] = React.useState(data.customer_name);
const [customerId, setCustomerId] = React.useState(data.customer_id);
const [line1, setLine1] = React.useState("");
const [line2, setLine2] = React.useState("");
const [city, setCity] = React.useState("");
const [state, setState] = React.useState("");
const [country, setCountry] = React.useState("");
const [postalCode, setPostalCode] = React.useState("");
const [timezone, setTimezone] =
React.useState<(typeof timezones)[number]>("UTC");
const [isEditing, setIsEditing] = React.useState(false);
const queryClient = useQueryClient();
const invoiceData: DraftInvoiceType | undefined = queryClient.getQueryData([
"draft_invoice",
data.customer_id,
]);
const updateCustomer = useMutation(
(obj: {
customer_id: string;
default_currency_code: string;
address: components["schemas"]["PatchedCustomerUpdateRequest"]["billing_address"];
tax_rate: number;
timezone: string;
customer_name?: string;
new_customer_id?: string;
}) =>
Customer.updateCustomer(
obj.customer_id,
obj.default_currency_code,
obj.address,
obj.address,
obj.tax_rate,
obj.timezone,
obj.customer_name,
obj.new_customer_id
),
{
onSuccess: () => {
toast.success("Successfully Updated Customer Details", {
position: toast.POSITION.TOP_CENTER,
});
form.resetFields();
},
onError: () => {
toast.error("Failed to Update Customer Details", {
position: toast.POSITION.TOP_CENTER,
});
},
}
);
const makeEditable = () => {
setIsEditing(true);
};
const EditCustomerHandler = async () => {
if ((import.meta as any).env.VITE_IS_DEMO === "true") {
return;
} else {
let submittedAddress;
if (
city === "" &&
line1 === "" &&
country === "" &&
postalCode === "" &&
state === "" &&
line2 === ""
) {
submittedAddress = null;
} else {
submittedAddress = {
city: city === "" ? null : city,
line1: line1 === "" ? null : line1,
line2: line2 === "" ? null : line2,
country: country === "" ? null : country,
postal_code: postalCode === "" ? null : postalCode,
state: state === "" ? null : state,
};
}
await updateCustomer.mutateAsync({
customer_id: data.customer_id,
address: submittedAddress,
default_currency_code: currentCurrency,
tax_rate: fourDP(taxRate),
timezone,
customer_name: customerName,
new_customer_id: customerId,
});
refetch();
}
setIsEditing(false);
};
const displayMetric = (metric: number | undefined): number => {
if (metric === undefined) {
return 0;
}
return metric;
};
useEffect(() => {
const newgraphdata = cost_data.per_day.map((day) => {
const result_list = day.cost_data.map((metric) => ({
date: day.date,
amount: metric.cost,
metric: metric.metric.billable_metric_name,
type: "cost",
}));
result_list.push({
date: day.date,
amount: day.revenue,
type: "revenue",
metric: "Revenue",
});
return result_list;
});
setTransformedGraphData(newgraphdata.flat(1));
}, [cost_data]);
const onSwitch = (key: string) => {
let start_date;
const end_date = dayjs().format("YYYY-MM-DD");
switch (key) {
case "1":
start_date = dayjs().subtract(1, "month").format("YYYY-MM-DD");
break;
case "2":
start_date = dayjs().subtract(2, "month").format("YYYY-MM-DD");
break;
case "3":
start_date = dayjs().startOf("month").format("YYYY-MM-DD");
break;
case "4":
start_date = dayjs().startOf("year").format("YYYY-MM-DD");
break;
default:
break;
}
onDateChange(start_date, end_date);
};
const config = {
data: transformedGraphData,
xField: "date",
yField: "amount",
isGroup: true,
isStack: true,
seriesField: "metric",
groupField: "type",
legend: false as const,
colorField: "type", // or seriesField in some cases
color: (type) => {
switch (type.metric) {
case "cost":
return "#E4D5C5";
case "Revenue":
return "#C3986B";
case "type3":
return "#D9D9D9";
case "type4":
return "#171412";
case "type5":
return "#547AA5";
default:
return "#E4D5C5"; // fallback color
}
},
};
return (
<div className="flex flex-col">
<div className="grid grid-cols-1 gap-16 md:grid-cols-3">
<div className="col-span-2">
<CustomerCard
className={`overflow-x-clip ${
!isEditing ? "h-[235px]" : "min-h-[250px]"
} `}
>
<CustomerCard.Heading>
<div className="flex items-center">
<Typography.Title className="pt-4 flex font-alliance !text-[18px]">
Customer Details
</Typography.Title>
<div className="ml-auto">
{!isEditing ? (
<PencilSquareIcon onClick={makeEditable} />
) : (
<div className="flex items-center gap-4">
<span
aria-hidden
className="text-card-offGrey"
onClick={() => setIsEditing(false)}
>
Cancel
</span>
<span
aria-hidden
className="text-gold"
onClick={EditCustomerHandler}
>
Save
</span>
</div>
)}
</div>
</div>
<Divider className="mt-[3.60px]" />
</CustomerCard.Heading>
<CustomerCard.Container className="grid gap-72 items-center grid-cols-1 md:grid-cols-2">
<CustomerCard.Block className="text-[14px] p-2 -mt-4 justify-between w-full">
<CustomerCard.Item>
<div className="text-card-text font-normal font-alliance whitespace-nowrap leading-4">
Name
</div>
<div className="flex gap-1">
{" "}
{isEditing ? (
<input
placeholder="Customer Name"
className="input-class focus:none focus-visible:none outline-none border border-black p-2 rounded-sm"
defaultValue={data.customer_name}
onChange={(e) => setCustomerName(e.target.value)}
required
/>
) : (
<div className="Inter">{data.customer_name}</div>
)}
</div>
</CustomerCard.Item>
<CustomerCard.Item>
<div className="font-normal text-card-text font-alliance whitespace-nowrap leading-4">
ID
</div>
<div className="flex gap-1">
{" "}
{false ? (
<input
placeholder="Customer ID"
className="input-class focus:none focus-visible:none outline-none border border-black p-2 rounded-sm"
defaultValue={data.customer_id}
onChange={(e) => setCustomerId(e.target.value)}
required
/>
) : (
<div className="flex gap-1 !text-card-grey font-menlo">
<div>
{createShortenedText(
data.customer_id as string,
windowWidth >= 2500
)}
</div>
<CopyText
showIcon
onlyIcon
textToCopy={data.customer_id as string}
/>
</div>
)}
</div>
</CustomerCard.Item>
<CustomerCard.Item>
<div className="text-card-text font-normal font-alliance whitespace-nowrap leading-4">
Billing Address
</div>
<div className="flex gap-1">
{" "}
{!isEditing ? (
<div className="Inter">
{data.address ? (
<div>
{data.address.line1},{data.address.state},
{data.address.country} {data.address.postal_code}
</div>
) : (
"N/A"
)}
</div>
) : (
<div className="flex flex-col gap-4 w-full">
<div className="flex gap-2">
<input
placeholder="Address Line 1"
className="input-class focus:none focus-visible:none outline-none border border-black p-2 rounded-sm"
defaultValue={
data.address ? data.address.line1 : line1
}
onChange={(e) => setLine1(e.target.value)}
required
/>
<input
placeholder="Address Line 2"
className="input-class focus:none focus-visible:none outline-none border border-black p-2 rounded-sm"
defaultValue={
data.address && data.address.line2
? data.address.line2
: line2
}
onChange={(e) => setLine2(e.target.value)}
/>
</div>
<div className="flex gap-2">
<select
name="Country"
id="country"
className=" w-1/2 bg-white border border-black rounded-sm p-2"
defaultValue={country}
onChange={(e) => setCountry(e.target.value)}
>
{country_json.map((country) => (
<option key={country.Code} value={country.Code}>
{country.Name}
</option>
))}
</select>
<input
placeholder="City"
className="input-class-last focus:none focus-visible:none outline-none border border-black p-2 rounded-sm"
onChange={(e) => setCity(e.target.value)}
defaultValue={
data.address ? data.address.city : city
}
required
/>
</div>
<div className="flex gap-2">
<input
placeholder="State"
className="w-1/2 focus:none focus-visible:none outline-none border border-black p-2 rounded-sm"
defaultValue={
data.address ? data.address.state : state
}
onChange={(e) => setState(e.target.value)}
required
/>
<input
defaultValue={
data.address
? data.address.postal_code
: postalCode
}
className="w-1/2 focus:none focus-visible:none outline-none border border-black p-2 rounded-sm"
placeholder="Zip Code"
onChange={(e) => setPostalCode(e.target.value)}
required
/>
</div>
</div>
)}
</div>
</CustomerCard.Item>
<CustomerCard.Item>
<div className="text-card-text font-normal font-alliance whitespace-nowrap leading-4">
Timezone
</div>
<div className="flex gap-1">
{" "}
{!isEditing ? (
<div className="Inter">
{data.timezone ? (
<div>{data.timezone}</div>
) : (
<div>{timezone}</div>
)}
</div>
) : (
<div className="min-w-[100px]">
<select
className="w-full bg-white border border-black p-4"
name="timezone"
id="timezone"
onChange={(e) =>
setTimezone(e.target.value as typeof timezone)
}
defaultValue={data.timezone ? timezone : timezone}
>
{timezones.map((tz) => (
<option key={tz} value={tz}>
{tz}
</option>
))}
</select>
</div>
)}
</div>
</CustomerCard.Item>
<CustomerCard.Item>
<div className="text-card-text font-normal font-alliance whitespace-nowrap leading-4"></div>
<div className="flex gap-1"></div>
</CustomerCard.Item>
</CustomerCard.Block>
<CustomerCard.Block className="w-full p-2 -mt-4 ml-auto text-[14px] justify-between">
<CustomerCard.Item>
<div className="text-card-text font-normal font-alliance whitespace-nowrap leading-4">
Email
</div>
<div className="flex gap-1">
{" "}
<div className="flex gap-1 !text-card-grey font-menlo">
{" "}
<div>
{createShortenedText(
data?.email || ("" as string),
windowWidth >= 2500
)}
</div>
<CopyText
showIcon
onlyIcon
textToCopy={data?.email || ("" as string)}
/>
</div>
</div>
</CustomerCard.Item>
<CustomerCard.Item>
<div className="font-normal text-card-text font-alliance whitespace-nowrap leading-4">
Default Currency
</div>
{!isEditing ? (
<div className="flex gap-1 !text-card-text Inter">
{" "}
<div>
{data.default_currency.code}-
{data.default_currency.symbol}
</div>
</div>
) : (
<div className="min-w-[100px]">
<select
className="w-full bg-white border border-black p-4"
name="currency"
id="currency"
defaultValue={data.default_currency.code}
onChange={(e) => setCurrentCurrency(e.target.value)}
>
{pricingUnits?.map((pc) => (
<option
key={pc.code}
value={pc.code}
>{`${pc.name} ${pc.symbol}`}</option>
))}
</select>
</div>
)}
</CustomerCard.Item>
<CustomerCard.Item>
<div className="text-card-text font-normal font-alliance whitespace-nowrap leading-4">
Tax Rate
</div>
<div className="flex gap-1">
{" "}
{isEditing ? (
<input
type="number"
placeholder="Tax Rate"
className="input-class focus:none focus-visible:none outline-none border border-black p-2 rounded-sm"
defaultValue={data.tax_rate ? data.tax_rate : taxRate}
step=".01"
max={999.9999}
onChange={(e) =>
setTaxRate(e.target.value as unknown as number)
}
required
/>
) : (
<div className="Inter">
{data.tax_rate ? <span>{data.tax_rate}%</span> : "0%"}
</div>
)}
</div>
</CustomerCard.Item>
<CustomerCard.Item>
<div className="text-card-text font-normal font-alliance whitespace-nowrap leading-4">
Payment Method Connected
</div>
{data.payment_provider ? (
<Tooltip title={data.payment_provider_id}>
<div className="flex gap-1">
{data.payment_provider_url ? (
<a
href={data.payment_provider_url}
target="_blank"
rel="noopener noreferrer"
>
<img
width={25}
src={
data.payment_provider === "stripe"
? integrationsMap.stripe.icon
: integrationsMap.braintree.icon
}
alt="payment provider logo"
/>
</a>
) : (
<div className="Inter">
{data.payment_provider === "stripe" ? (
<img
width={25}
src={integrationsMap.stripe.icon}
alt="stripe logo"
/>
) : data.payment_provider === "braintree" ? (
<img
width={25}
src={integrationsMap.braintree.icon}
alt="braintree logo"
/>
) : (
"N/A"
)}
</div>
)}
</div>
</Tooltip>
) : (
<div className="flex gap-1">
<div className="Inter">N/A</div>
</div>
)}
</CustomerCard.Item>
<CustomerCard.Item>
<div className="text-card-text font-normal font-alliance whitespace-nowrap leading-4">
CRM Connected
</div>
{data.crm_provider ? (
<Tooltip title={data.crm_provider_id}>
<div className="flex gap-1">
<div className="Inter">
{data.crm_provider === "salesforce" ? (
data.crm_provider_url ? (
<a
href={data.crm_provider_url}
target="_blank"
rel="noopener noreferrer"
>
<img
width={25}
src={integrationsMap.salesforce.icon}
alt="salesforce logo"
/>
</a>
) : (
<img
width={25}
src={integrationsMap.salesforce.icon}
alt="salesforce logo"
/>
)
) : (
"N/A"
)}
</div>
</div>
</Tooltip>
) : (
<div className="flex gap-1">
<div className="Inter">N/A</div>
</div>
)}
</CustomerCard.Item>
</CustomerCard.Block>
</CustomerCard.Container>
</CustomerCard>
</div>
<div className="col-span-1 mr-8">
<CustomerCard className="h-[235px]">
<CustomerCard.Heading>
<Typography.Title className="pt-4 flex font-alliance !text-[18px]">
Revenue Details
</Typography.Title>
</CustomerCard.Heading>
<Divider />
<CustomerCard.Container>
<CustomerCard.Block className="text-sm">
<CustomerCard.Item>
<div className="text-card-text font-normal font-alliance whitespace-nowrap leading-4">
Earned Revenue
</div>
<div className="Inter">
{data.default_currency.symbol}
{(cost_data.total_revenue || 0).toFixed(2)}
</div>
</CustomerCard.Item>
<CustomerCard.Item>
<div className="text-card-text font-normal font-alliance whitespace-nowrap leading-4">
Total Cost
</div>
<div className="Inter">
{data.default_currency.symbol}
{(cost_data.total_cost || 0).toFixed(2)}
</div>
</CustomerCard.Item>
<CustomerCard.Item>
<div className="text-card-text font-normal font-alliance whitespace-nowrap leading-4">
Profit Margin
</div>
<div
className={`Inter ${
cost_data.profit_margin * 100 > 0
? "text-emerald-800"
: "text-rose-700"
}`}
>
{((cost_data.profit_margin as number) * 100).toFixed(2)}%
</div>
</CustomerCard.Item>
<CustomerCard.Item>
<div className="text-card-text font-normal font-alliance whitespace-nowrap leading-4">
Markup
</div>
<div
className={`Inter ${
cost_data.markup * 100 > 0
? "text-emerald-800"
: "text-rose-700"
}`}
>
{((cost_data.markup as number) * 100).toFixed(2)}%
</div>
</CustomerCard.Item>
<CustomerCard.Item>
<div className="text-card-text font-normal font-alliance whitespace-nowrap leading-4">
Next Invoice Due
</div>
<div className="Inter text-card-grey">
{data.default_currency.symbol}
{data.invoices[0]?.cost_due.toFixed(2)}
</div>
</CustomerCard.Item>
</CustomerCard.Block>
</CustomerCard.Container>
</CustomerCard>
</div>
</div>
<div className="space-y-4 mt-8">
<CustomerCard>
<CustomerCard.Heading>
<div className="flex">
<Typography.Title className="pt-4 flex font-alliance !text-[18px]">
Revenue vs Cost Per Day
</Typography.Title>
<div className="ml-auto">
<div className="flex gap-4 items-center">
<div>
<Badge className="bg-transparent">
<Badge.Dot className="text-[#E4D5C5]" />
<Badge.Content>Cost</Badge.Content>
</Badge>
</div>
<div>
<Badge className="bg-transparent">
<Badge.Dot className="text-darkgold" />
<Badge.Content>Revenue</Badge.Content>
</Badge>
</div>
<div className="">
{" "}
<Select defaultValue="1" onChange={onSwitch}>
<Select.Option value="1">Last 30 Days</Select.Option>
<Select.Option value="2">Last 60 Days</Select.Option>
<Select.Option value="3">This Month</Select.Option>
<Select.Option value="4">Year to date</Select.Option>
</Select>
</div>
</div>
</div>
</div>
</CustomerCard.Heading>
<CustomerCard.Container className=" mt-8">
<CustomerCard.Block>
<Column {...config} />
</CustomerCard.Block>
</CustomerCard.Container>
</CustomerCard>
</div>
</div>
);
};
export default CustomerInfoView;