| 1 | 'use client' |
| 2 | |
| 3 | import { useRouter } from '@bprogress/next/app' |
| 4 | import { CheckCircle2, Pencil |
| 5 | import { useAction } |
| 6 | import { useMemo, useState |
| 7 | import { toast } |
| 8 | import { Badge } |
| 9 | import { Button } |
| 10 | import { Card, CardContent |
| 11 | import { Checkbox } |
| 12 | import { |
| 13 | Empty, |
| 14 | EmptyDescription, |
| 15 | EmptyHeader, |
| 16 | EmptyMedia, |
| 17 | EmptyTitle, |
| 18 | } from '@/components/ui/empty' |
| 19 | import { useSetSelection } |
| 20 | import { approveTimeEntriesAction, rejectTimeEntriesAction |
| 21 | import { formatMinutes } |
| 22 | import type { ProjectMember, |
| 23 | import { RejectTimeEntriesDialog } |
| 24 | import { TimeEntryForm } |
| 25 | |
| 26 | interface TimesheetApprovalProps { |
| 27 | entries: TimeEntry |
| 28 | projectId: string |
| 29 | projectMembers: ProjectMember |
| 30 | requirements: Requirement |
| 31 | } |
| 32 | |
| 33 | export function TimesheetApproval({ |
| 34 | entries, |
| 35 | projectMembers, |
| 36 | projectId, |
| 37 | requirements, |
| 38 | }: TimesheetApprovalProps) |
| 39 | const { selectedIds, |
| 40 | (e) => |
| 41 | ) |
| 42 | const [rejectOpen, |
| 43 | const [editEntry, |
| 44 | const router = |
| 45 | const approveAction = |
| 46 | onSuccess: () => |
| 47 | toast.success('Time entries approved |
| 48 | clear() |
| 49 | router.refresh() |
| 50 | }, |
| 51 | onError: ({ error |
| 52 | toast.error(error |
| 53 | }, |
| 54 | }) |
| 55 | |
| 56 | const rejectAction = |
| 57 | onSuccess: () => |
| 58 | toast.success('Time entries rejected |
| 59 | clear() |
| 60 | setRejectOpen(false) |
| 61 | router.refresh() |
| 62 | }, |
| 63 | onError: ({ error |
| 64 | toast.error(error |
| 65 | }, |
| 66 | }) |
| 67 | |
| 68 | const grouped = |
| 69 | () => |
| 70 | entries.reduce<Record |
| 71 | const key = |
| 72 | if (!acc[key |
| 73 | acc[key] = |
| 74 | } |
| 75 | acc[key].push |
| 76 | return acc |
| 77 | }, {}), |
| 78 | [entries] |
| 79 | ) |
| 80 | |
| 81 | if (entries.length |
| 82 | return ( |
| 83 | <Empty> |
| 84 | <EmptyHeader> |
| 85 | <EmptyMedia variant=' |
| 86 | <CheckCircle2 /> |
| 87 | </EmptyMedia> |
| 88 | <EmptyTitle>No pending approvals</EmptyTitle |
| 89 | <EmptyDescription> |
| 90 | All submitted timesheets have been reviewed. |
| 91 | </EmptyDescription> |
| 92 | </EmptyHeader> |
| 93 | </Empty> |
| 94 | ) |
| 95 | } |
| 96 | |
| 97 | return ( |
| 98 | <div className='space-y-4 |
| 99 | <div className='flex items-center justify-between |
| 100 | <p className='text-muted-foreground text-sm |
| 101 | {entries.length} submitted{ |
| 102 | {entries.length === |
| 103 | </p> |
| 104 | <div className='flex items-center gap-2 |
| 105 | <Button |
| 106 | disabled={selectedIds.size |
| 107 | onClick={() => |
| 108 | variant='outline' |
| 109 | > |
| 110 | <XCircle className=' |
| 111 | Reject |
| 112 | </Button> |
| 113 | <Button |
| 114 | disabled={selectedIds.size |
| 115 | onClick={() => |
| 116 | approveAction.execute({ |
| 117 | } |
| 118 | > |
| 119 | <CheckCircle2 className=' |
| 120 | {approveAction.isPending ? |
| 121 | </Button> |
| 122 | </div> |
| 123 | </div> |
| 124 | |
| 125 | {Object.entries(grouped |
| 126 | const member = |
| 127 | const totalMinutes = |
| 128 | (sum, e) |
| 129 | 0 |
| 130 | ) |
| 131 | const allSelected = |
| 132 | |
| 133 | return ( |
| 134 | <Card key={ |
| 135 | <CardHeader className=' |
| 136 | <div className='flex items-center justify-between |
| 137 | <div className='flex items-center gap-3 |
| 138 | <Checkbox |
| 139 | checked={allSelected} |
| 140 | onCheckedChange={() => |
| 141 | /> |
| 142 | <div> |
| 143 | <CardTitle className=' |
| 144 | {member?.name ?? |
| 145 | </CardTitle> |
| 146 | <p className='text-muted-foreground text-sm |
| 147 | {memberEntries.length}{' |
| 148 | {memberEntries.length === |
| 149 | {formatMinutes(totalMinutes)} |
| 150 | </p> |
| 151 | </div> |
| 152 | </div> |
| 153 | <Badge variant=' |
| 154 | </div> |
| 155 | </CardHeader> |
| 156 | <CardContent> |
| 157 | <div className='space-y-2 |
| 158 | {memberEntries.map(( |
| 159 | <div |
| 160 | className='flex items-center gap-3 rounded-md border px-3 py-2' |
| 161 | key={entry.id |
| 162 | > |
| 163 | <Checkbox |
| 164 | checked={selectedIds.has |
| 165 | onCheckedChange={() => |
| 166 | /> |
| 167 | <div className='min-w-0 flex-1 |
| 168 | <p className='truncate text-sm |
| 169 | <p className='text-muted-foreground text-xs |
| 170 | {entry.requirementTitle ?? |
| 171 | {new Date(entry |
| 172 | weekday: 'short', |
| 173 | month: 'short', |
| 174 | day: 'numeric', |
| 175 | })} |
| 176 | </p> |
| 177 | </div> |
| 178 | <div className='flex items-center gap-2 |
| 179 | {entry.billable && |
| 180 | <Badge className=' |
| 181 | Billable |
| 182 | </Badge> |
| 183 | )} |
| 184 | <span className='font-medium text-sm |
| 185 | {formatMinutes(entry.durationMinutes |
| 186 | </span> |
| 187 | <Button |
| 188 | className='size-7' |
| 189 | onClick={() => |
| 190 | size='icon' |
| 191 | title='Edit entry before approving' |
| 192 | variant='ghost' |
| 193 | > |
| 194 | <Pencil className=' |
| 195 | </Button> |
| 196 | </div> |
| 197 | </div> |
| 198 | ))} |
| 199 | </div> |
| 200 | </CardContent> |
| 201 | </Card> |
| 202 | ) |
| 203 | })} |
| 204 | |
| 205 | <RejectTimeEntriesDialog |
| 206 | isPending={rejectAction.isPending |
| 207 | onOpenChange={setRejectOpen} |
| 208 | onSubmit={(reason) |
| 209 | rejectAction.execute({ |
| 210 | timeEntryIds: [...selectedIds |
| 211 | reason, |
| 212 | }) |
| 213 | } |
| 214 | open={rejectOpen} |
| 215 | /> |
| 216 | |
| 217 | {editEntry && ( |
| 218 | <TimeEntryForm |
| 219 | editEntry={editEntry} |
| 220 | onOpenChange={(open) |
| 221 | if (!open) { |
| 222 | setEditEntry(null) |
| 223 | } |
| 224 | }} |
| 225 | open={!!editEntry} |
| 226 | projectId={projectId} |
| 227 | requirements={requirements} |
| 228 | /> |
| 229 | )} |
| 230 | </div> |
| 231 | ) |
| 232 | } |