Repository navigation
Expand file tree
/
Copy pathDistributionTable.tsx
More file actions
234 lines (213 loc) · 6.62 KB
/
Copy pathDistributionTable.tsx
File metadata and controls
234 lines (213 loc) · 6.62 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
import AdvancedBaseTable, {
AdvancedBaseTableHandle,
FilterList,
ColumnDefinition,
} from "./baseTable/AdvancedBaseTable";
import { useCallback, useRef, useState } from "react";
import { useApiClient } from "@/hooks/useApiClient";
import Portal from "./baseTable/Portal";
import toast from "react-hot-toast";
import { CheckCircle, DotsThree, XCircle } from "@phosphor-icons/react";
import DistributionsGeneralItemChipGroup from "./chips/DistributionsGeneralItemChipGroup";
import { TableDistribution } from "@/types/api/distribution.types";
import { useUser } from "@/components/context/UserContext";
import { hasPermission } from "@/lib/userUtils";
export default function DistributionTable() {
const { apiClient } = useApiClient();
const { user } = useUser();
const canManageDistributions = hasPermission(user, "distributionWrite");
const tableRef = useRef<AdvancedBaseTableHandle<TableDistribution>>(null);
const fetchTableData = useCallback(
async (
pageSize: number,
page: number,
filters: FilterList<TableDistribution>
) => {
const searchParams = new URLSearchParams({
page: page.toString(),
pageSize: pageSize.toString(),
filters: JSON.stringify(filters),
});
const res = await apiClient.get<{
data: TableDistribution[];
total: number;
}>(`/api/distributions?${searchParams.toString()}`);
return {
data: res.data,
total: res.total,
};
},
[apiClient]
);
const columns = [
{
id: "partnerName",
header: "Partner Name",
filterType: "string",
cell: (row) => row.partner.name,
},
{
id: "pending",
header: "Status",
filterType: "enum",
filterOptions: ["Pending", "Approved"],
cell: (row) => (
<span
className={`px-3 py-1 rounded ${row.pending ? "bg-yellow-primary/60 text-orange-primary" : "bg-green-primary/60 text-green-dark"}`}
>
{row.pending ? "Pending" : "Approved"}
</span>
),
},
{
id: "donors",
header: "Donors",
cell: (row) => (
<div className="flex">
{Array.from(
new Set(
row.generalItems.map(
(generalItem) => generalItem.donorOffer.donorName
)
)
).map((name) => (
<span
key={name}
className="rounded-lg border m-2 px-2 py-1 text-sm flex items-center gap-1 border-blue-primary text-blue-primary"
>
{name}
</span>
))}
</div>
),
},
{
id: "createdAt",
header: "Created",
cell: (row) => {
const date = new Date(row.createdAt);
if (isNaN(date.getTime())) {
return "N/A";
}
return date.toLocaleString('en-US', {
month: 'short',
day: 'numeric',
year: 'numeric',
hour: 'numeric',
minute: '2-digit',
hour12: true
});
},
},
] as ColumnDefinition<TableDistribution>[];
if (canManageDistributions) {
columns.push({
id: "Manage",
headerClassName: "text-right",
header: "",
cell: (distribution) => (
<div className="flex justify-end">
<OptionsButton
distribution={distribution}
fetchTableData={tableRef.current!.reload}
/>
</div>
),
});
}
return (
<AdvancedBaseTable
ref={tableRef}
columns={columns}
fetchFn={fetchTableData}
rowId={"id"}
rowBody={(distribution) => (
<DistributionsGeneralItemChipGroup
generalItems={distribution.generalItems}
allocations={distribution.allocations}
fetchTableData={tableRef.current!.reload}
pending={distribution.pending}
canTransfer={canManageDistributions}
distributionId={distribution.id}
/>
)}
/>
);
}
function OptionsButton({
distribution,
fetchTableData,
}: {
distribution: TableDistribution;
fetchTableData: () => void;
}) {
const [isDropdownOpen, setIsDropdownOpen] = useState(false);
const buttonRef = useRef<HTMLButtonElement>(null);
const { apiClient } = useApiClient();
async function approveDistribution() {
const formData = new FormData();
formData.append("pending", "false");
const promise = apiClient.patch(`/api/distributions/${distribution.id}`, {
body: formData,
});
toast.promise(promise, {
loading: "Approving distribution...",
success: "Distribution approved!",
error: "Failed to approve distribution.",
});
await promise;
setIsDropdownOpen(false);
fetchTableData();
}
async function unapproveDistribution() {
const formData = new FormData();
formData.append("pending", "true");
const promise = apiClient.patch(`/api/distributions/${distribution.id}`, {
body: formData,
});
toast.promise(promise, {
loading: "Unapproving distribution...",
success: "Distribution unapproved!",
error: "Failed to unapprove distribution.",
});
await promise;
setIsDropdownOpen(false);
fetchTableData();
}
return (
<div className="relative">
<button
ref={buttonRef}
onClick={() => setIsDropdownOpen(!isDropdownOpen)}
className="px-2 py-1 rounded hover:bg-gray-100"
>
<DotsThree size={16} />
</button>
<Portal
isOpen={isDropdownOpen}
onClose={() => setIsDropdownOpen(false)}
triggerRef={buttonRef}
position="bottom-right"
className="w-48 rounded-md bg-white shadow-lg ring-1 ring-black/5 py-1"
>
{distribution.pending ? (
<button
onClick={approveDistribution}
className="flex items-center w-full px-4 py-2 text-sm text-left transition-colors duration-150 hover:bg-gray-50 text-gray-900 cursor-pointer"
>
<CheckCircle size={16} className="mr-3 flex-shrink-0" />
<p>Approve</p>
</button>
) : (
<button
onClick={unapproveDistribution}
className="flex items-center w-full px-4 py-2 text-sm text-left transition-colors duration-150 hover:bg-gray-50 text-gray-900 cursor-pointer"
>
<XCircle size={16} className="mr-3 flex-shrink-0" />
<p>Unapprove</p>
</button>
)}
</Portal>
</div>
);
}