-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathEventTable.tsx
More file actions
142 lines (136 loc) · 5.22 KB
/
EventTable.tsx
File metadata and controls
142 lines (136 loc) · 5.22 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
import { useState } from "react";
import { Event_Permissions, usePermissionMap } from "@/features/Permissions/v1";
import { Event } from "../Event.type";
type EventProps = {
events: Event[];
itemsPerPage: number;
};
const statusConfig: Record<Event["status"], { bg: string; color: string }> = {
Live: { bg: "var(--cd-success-subtle)", color: "var(--cd-success)" },
Upcoming: { bg: "var(--cd-primary-subtle)", color: "var(--cd-primary-text)" },
Completed: { bg: "var(--cd-surface-2)", color: "var(--cd-text-2)" },
};
function EventTable({ events, itemsPerPage }: EventProps) {
const { canView, canEdit, canDelete, canPublish } = usePermissionMap({
canView: Event_Permissions.VIEW_EVENT,
canEdit: Event_Permissions.UPDATE_EVENT,
canDelete: Event_Permissions.DELETE_EVENT,
canPublish: Event_Permissions.PUBLISH_EVENT,
});
const [currentPage, setCurrentPage] = useState(1);
const totalPages = Math.ceil(events.length / itemsPerPage);
const indexOfLast = currentPage * itemsPerPage;
const indexOfFirst = indexOfLast - itemsPerPage;
const currentItems = events.slice(indexOfFirst, indexOfLast);
const canManageActions = canView || canEdit || canDelete || canPublish;
return (
<div
className="rounded-xl overflow-hidden h-[63vh] relative"
style={{
backgroundColor: "var(--cd-surface)",
border: "1px solid var(--cd-border)",
boxShadow: "0 1px 3px var(--cd-shadow)",
}}
>
<table className="cd-table">
<thead>
<tr>
<th>Name</th>
<th>Date</th>
<th>Status</th>
<th>Teams</th>
<th>Submissions</th>
{canManageActions && <th className="text-right">Actions</th>}
</tr>
</thead>
<tbody>
{currentItems.map((event) => {
const s = statusConfig[event.status];
return (
<tr key={event.id}>
<td>
<div className="flex items-center gap-3">
<div
className="w-9 h-9 rounded-lg flex items-center justify-center text-lg shrink-0"
style={{
backgroundColor: "var(--cd-primary-subtle)",
color: "var(--cd-primary)",
}}
>
{event.logo}
</div>
<div>
<p className="font-medium" style={{ color: "var(--cd-text)" }}>
{event.name}
</p>
<p className="text-xs" style={{ color: "var(--cd-text-2)" }}>
{event.subtitle}
</p>
</div>
</div>
</td>
<td style={{ color: "var(--cd-text-2)" }}>{event.date}</td>
<td>
<span className="cd-badge" style={{ backgroundColor: s.bg, color: s.color }}>
{event.status}
</span>
</td>
<td style={{ color: "var(--cd-text-2)" }}>{event.teams}</td>
<td style={{ color: "var(--cd-text-2)" }}>{event.submissions}</td>
{canManageActions && (
<td className="text-right">
<div className="flex flex-wrap justify-end gap-2">
{canView && (
<button className="cd-btn cd-btn-secondary px-3 py-1 text-xs">View</button>
)}
{canEdit && (
<button className="cd-btn cd-btn-secondary px-3 py-1 text-xs">Edit</button>
)}
{canPublish && event.status !== "Completed" && (
<button className="cd-btn cd-btn-primary px-3 py-1 text-xs">
Publish
</button>
)}
{canDelete && (
<button className="cd-btn cd-btn-danger px-3 py-1 text-xs">Delete</button>
)}
</div>
</td>
)}
</tr>
);
})}
</tbody>
</table>
<div
className="flex justify-between items-center px-6 py-3 text-sm border-t absolute w-full bottom-0"
style={{
backgroundColor: "var(--cd-surface)",
borderColor: "var(--cd-border)",
color: "var(--cd-text-2)",
}}
>
<p>
Showing {indexOfFirst + 1}–{Math.min(indexOfLast, events.length)} of {events.length}
</p>
<div className="flex gap-2">
<button
onClick={() => setCurrentPage((p) => Math.max(p - 1, 1))}
disabled={currentPage === 1}
className="cd-btn cd-btn-secondary px-3 py-1 text-xs disabled:opacity-40"
>
Previous
</button>
<button
onClick={() => setCurrentPage((p) => Math.min(p + 1, totalPages))}
disabled={currentPage === totalPages}
className="cd-btn cd-btn-secondary px-3 py-1 text-xs disabled:opacity-40"
>
Next
</button>
</div>
</div>
</div>
);
}
export default EventTable;