| 1 | 'use client' |
| 2 | |
| 3 | import { useRouter } from '@bprogress/next/app' |
| 4 | import { Plus, Receipt |
| 5 | import { useAction } |
| 6 | import { useState } |
| 7 | import { toast } |
| 8 | import SendToClientDialog from '@/components/send-to-client-dialog |
| 9 | import { Button } |
| 10 | import { |
| 11 | Empty, |
| 12 | EmptyDescription, |
| 13 | EmptyHeader, |
| 14 | EmptyMedia, |
| 15 | EmptyTitle, |
| 16 | } from '@/components/ui/empty' |
| 17 | import { Tabs, TabsContent |
| 18 | import { CategoryManager } |
| 19 | import { ClientExpensesView } |
| 20 | import { ExpenseApproval } |
| 21 | import { ExpenseForm } |
| 22 | import { ExpensesTable } |
| 23 | import { sendExpensesToClientAction, submitExpensesAction |
| 24 | import type { ExpensesClientProps |
| 25 | |
| 26 | export default function ExpensesClient |
| 27 | expenses, |
| 28 | categories, |
| 29 | allCategories, |
| 30 | clients, |
| 31 | canCreate, |
| 32 | canSubmit, |
| 33 | isAdmin, |
| 34 | isClient, |
| 35 | projectId, |
| 36 | organizationId, |
| 37 | defaultCurrency, |
| 38 | currentMemberId, |
| 39 | isClientInvolved, |
| 40 | recipients, |
| 41 | }: ExpensesClientProps) |
| 42 | const [formOpen, |
| 43 | const [categoriesOpen, |
| 44 | const [sendToClientOpen, |
| 45 | const [selectedIds, |
| 46 | const router = |
| 47 | const submitAction = |
| 48 | onSuccess: () => |
| 49 | toast.success('Expenses submitted for approval |
| 50 | setSelectedIds(new Set |
| 51 | router.refresh() |
| 52 | }, |
| 53 | onError: ({ error |
| 54 | toast.error(error |
| 55 | }, |
| 56 | }) |
| 57 | |
| 58 | const mySubmittableExpenses = |
| 59 | (e) => |
| 60 | e.memberId === |
| 61 | (e.status === |
| 62 | ) |
| 63 | const submittedExpenses = |
| 64 | (e) => |
| 65 | ) |
| 66 | const sendableExpenses = |
| 67 | (e) => |
| 68 | ) |
| 69 | |
| 70 | const selectedSubmittableIds = |
| 71 | mySubmittableExpenses.some((e |
| 72 | ) |
| 73 | |
| 74 | function toggleSelect(id |
| 75 | setSelectedIds((prev) |
| 76 | const next = |
| 77 | if (next.has |
| 78 | next.delete(id |
| 79 | } else { |
| 80 | next.add(id |
| 81 | } |
| 82 | return next |
| 83 | }) |
| 84 | } |
| 85 | |
| 86 | function toggleAll() { |
| 87 | const allSelected = |
| 88 | if (allSelected) { |
| 89 | setSelectedIds(new Set |
| 90 | } else { |
| 91 | setSelectedIds(new Set |
| 92 | } |
| 93 | } |
| 94 | const { execute, |
| 95 | onSuccess() { |
| 96 | toast.success('Expenses sent to client |
| 97 | setSendToClientOpen(false) |
| 98 | router.refresh() |
| 99 | }, |
| 100 | onError({ error |
| 101 | toast.error(error |
| 102 | }, |
| 103 | }) |
| 104 | |
| 105 | if (isClient) { |
| 106 | return ( |
| 107 | <div className='w-full |
| 108 | <div className='mb-6 flex items-center justify-between |
| 109 | <h1 className='font-semibold text-2xl |
| 110 | </div> |
| 111 | <ClientExpensesView |
| 112 | currentMemberId={currentMemberId} |
| 113 | expenses={expenses} |
| 114 | recipients={recipients} |
| 115 | /> |
| 116 | </div> |
| 117 | ) |
| 118 | } |
| 119 | |
| 120 | return ( |
| 121 | <div className='w-full |
| 122 | <div className='mb-6 flex items-center justify-between |
| 123 | <h1 className='font-semibold text-2xl |
| 124 | <div className='flex items-center gap-2 |
| 125 | {isAdmin && isClientInvolved |
| 126 | <Button |
| 127 | disabled={selectedIds.size |
| 128 | onClick={() => |
| 129 | variant='outline' |
| 130 | > |
| 131 | <Send className=' |
| 132 | Send to Client |
| 133 | </Button> |
| 134 | )} |
| 135 | {!isAdmin && canSubmit |
| 136 | <Button |
| 137 | disabled={submitAction.isPending |
| 138 | loading={submitAction.isPending |
| 139 | onClick={() => |
| 140 | submitAction.execute({ |
| 141 | } |
| 142 | variant='outline' |
| 143 | > |
| 144 | Submit (${selectedSubmittableIds.length}) |
| 145 | </Button> |
| 146 | )} |
| 147 | {isAdmin && ( |
| 148 | <Button onClick={ |
| 149 | <Settings2 className=' |
| 150 | Categories |
| 151 | </Button> |
| 152 | )} |
| 153 | {canCreate && ( |
| 154 | <Button kbd=' |
| 155 | <Plus className=' |
| 156 | Log Expense |
| 157 | </Button> |
| 158 | )} |
| 159 | </div> |
| 160 | </div> |
| 161 | |
| 162 | {expenses.length === |
| 163 | <Empty> |
| 164 | <EmptyHeader> |
| 165 | <EmptyMedia variant=' |
| 166 | <Receipt /> |
| 167 | </EmptyMedia> |
| 168 | <EmptyTitle>No expenses yet</EmptyTitle |
| 169 | <EmptyDescription> |
| 170 | Track project expenses and reimbursements. |
| 171 | </EmptyDescription> |
| 172 | </EmptyHeader> |
| 173 | </Empty> |
| 174 | ) : ( |
| 175 | <Tabs defaultValue={ |
| 176 | <TabsList className=' |
| 177 | <TabsTrigger value=' |
| 178 | {isAdmin && ( |
| 179 | <TabsTrigger value=' |
| 180 | Approval |
| 181 | {submittedExpenses.length > |
| 182 | <span className='ml-1.5 inline-flex size-5 items-center justify-center rounded-full bg-primary text-primary-foreground text-xs |
| 183 | {submittedExpenses.length} |
| 184 | </span> |
| 185 | )} |
| 186 | </TabsTrigger> |
| 187 | )} |
| 188 | </TabsList> |
| 189 | |
| 190 | <TabsContent value=' |
| 191 | <ExpensesTable |
| 192 | canSubmit={canSubmit} |
| 193 | categories={categories} |
| 194 | currentMemberId={currentMemberId} |
| 195 | expenses={expenses} |
| 196 | isAdmin={isAdmin} |
| 197 | isClientInvolved={isClientInvolved} |
| 198 | onToggleAll={toggleAll} |
| 199 | onToggleSelect={toggleSelect} |
| 200 | projectId={projectId} |
| 201 | recipients={recipients} |
| 202 | selectedIds={selectedIds} |
| 203 | /> |
| 204 | </TabsContent> |
| 205 | |
| 206 | {isAdmin && ( |
| 207 | <TabsContent value=' |
| 208 | <ExpenseApproval expenses={ |
| 209 | </TabsContent> |
| 210 | )} |
| 211 | </Tabs> |
| 212 | )} |
| 213 | |
| 214 | {canCreate && ( |
| 215 | <ExpenseForm |
| 216 | categories={categories} |
| 217 | defaultCurrency={defaultCurrency} |
| 218 | onOpenChange={setFormOpen} |
| 219 | open={formOpen} |
| 220 | projectId={projectId} |
| 221 | /> |
| 222 | )} |
| 223 | |
| 224 | {isAdmin && ( |
| 225 | <CategoryManager |
| 226 | categories={allCategories} |
| 227 | onOpenChange={setCategoriesOpen} |
| 228 | open={categoriesOpen} |
| 229 | organizationId={organizationId} |
| 230 | /> |
| 231 | )} |
| 232 | {isAdmin && isClientInvolved |
| 233 | <SendToClientDialog |
| 234 | clients={clients} |
| 235 | description='Select clients to send the selected expense(s) to for review and sign-off. They will receive an email notification.' |
| 236 | emptyMessage='No clients assigned to this project. Add clients from the Team page first.' |
| 237 | onOpenChange={setSendToClientOpen} |
| 238 | onSend={(clientIds) |
| 239 | execute({ |
| 240 | clientMemberIds: clientIds, |
| 241 | expenseIds: Array.from |
| 242 | }) |
| 243 | }} |
| 244 | open={sendToClientOpen} |
| 245 | recipientLabel='client' |
| 246 | sendDisabled={isExecuting} |
| 247 | title='Send for Sign' |
| 248 | /> |
| 249 | )} |
| 250 | </div> |
| 251 | ) |
| 252 | } |