-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathDealsChart.tsx
155 lines (148 loc) · 5.46 KB
/
DealsChart.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
import AttachMoneyIcon from '@mui/icons-material/AttachMoney';
import { Box, Stack, Typography } from '@mui/material';
import { ResponsiveBar } from '@nivo/bar';
import { format, startOfMonth } from 'date-fns';
import { useMemo } from 'react';
import { useGetList } from 'react-admin';
import { Deal } from '../types';
const multiplier = {
opportunity: 0.2,
'proposal-sent': 0.5,
'in-negociation': 0.8,
delayed: 0.3,
};
const threeMonthsAgo = new Date(
new Date().setMonth(new Date().getMonth() - 6)
).toISOString();
export const DealsChart = () => {
const { data, isPending } = useGetList<Deal>('deals', {
pagination: { perPage: 100, page: 1 },
sort: {
field: 'created_at',
order: 'ASC',
},
filter: {
'created_at@gte': threeMonthsAgo,
},
});
const months = useMemo(() => {
if (!data) return [];
const dealsByMonth = data.reduce((acc, deal) => {
const month = startOfMonth(
deal.created_at ?? new Date()
).toISOString();
if (!acc[month]) {
acc[month] = [];
}
acc[month].push(deal);
return acc;
}, {} as any);
const amountByMonth = Object.keys(dealsByMonth).map(month => {
return {
date: format(month, 'MMM'),
won: dealsByMonth[month]
.filter((deal: Deal) => deal.stage === 'won')
.reduce((acc: number, deal: Deal) => {
acc += deal.amount;
return acc;
}, 0),
pending: dealsByMonth[month]
.filter(
(deal: Deal) => !['won', 'lost'].includes(deal.stage)
)
.reduce((acc: number, deal: Deal) => {
// @ts-ignore
acc += deal.amount * multiplier[deal.stage];
return acc;
}, 0),
lost: dealsByMonth[month]
.filter((deal: Deal) => deal.stage === 'lost')
.reduce((acc: number, deal: Deal) => {
acc -= deal.amount;
return acc;
}, 0),
};
});
return amountByMonth;
}, [data]);
if (isPending) return null; // FIXME return skeleton instead
const range = months.reduce(
(acc, month) => {
acc.min = Math.min(acc.min, month.lost);
acc.max = Math.max(acc.max, month.won + month.pending);
return acc;
},
{ min: 0, max: 0 }
);
return (
<Stack>
<Box display="flex" alignItems="center" mb={1}>
<Box mr={1} display="flex">
<AttachMoneyIcon color="disabled" fontSize="medium" />
</Box>
<Typography variant="h5" color="textSecondary">
Upcoming Deal Revenue
</Typography>
</Box>
<Box height={400}>
<ResponsiveBar
data={months}
indexBy="date"
keys={['won', 'pending', 'lost']}
colors={['#61cdbb', '#97e3d5', '#e25c3b']}
margin={{ top: 30, right: 50, bottom: 30, left: 0 }}
padding={0.3}
valueScale={{
type: 'linear',
min: range.min * 1.2,
max: range.max * 1.2,
}}
indexScale={{ type: 'band', round: true }}
enableGridX={true}
enableGridY={false}
enableLabel={false}
axisTop={{
tickSize: 0,
tickPadding: 12,
}}
axisBottom={{
legendPosition: 'middle',
legendOffset: 50,
tickSize: 0,
tickPadding: 12,
}}
axisLeft={null}
axisRight={{
format: (v: any) => `${Math.abs(v / 1000)}k`,
tickValues: 8,
}}
markers={
[
{
axis: 'y',
value: 0,
lineStyle: { strokeOpacity: 0 },
textStyle: { fill: '#2ebca6' },
legend: 'Won',
legendPosition: 'top-left',
legendOrientation: 'vertical',
},
{
axis: 'y',
value: 0,
lineStyle: {
stroke: '#f47560',
strokeWidth: 1,
},
textStyle: { fill: '#e25c3b' },
legend: 'Lost',
legendPosition: 'bottom-left',
legendOrientation: 'vertical',
},
] as any
}
/>
</Box>
</Stack>
);
};