| 1 | 'use client' |
| 2 | |
| 3 | import { Plus, Trash2 } from 'lucide-react' |
| 4 | import { memo, useCallback |
| 5 | import { Button } |
| 6 | import { Input } |
| 7 | |
| 8 | export interface LineItem |
| 9 | amount: string |
| 10 | description: string |
| 11 | quantity: string |
| 12 | title?: string |
| 13 | unitPrice: string |
| 14 | } |
| 15 | |
| 16 | interface InternalLineItem extends |
| 17 | _rid: string |
| 18 | } |
| 19 | |
| 20 | interface LineItemsEditorProps { |
| 21 | currency: string |
| 22 | editable: boolean |
| 23 | items: LineItem |
| 24 | onChange: (items |
| 25 | title?: string |
| 26 | } |
| 27 | |
| 28 | const calcAmount = ( |
| 29 | const qty = |
| 30 | const price = |
| 31 | return (qty * |
| 32 | } |
| 33 | |
| 34 | export default function LineItemsEditor |
| 35 | items, |
| 36 | onChange, |
| 37 | currency, |
| 38 | editable, |
| 39 | title, |
| 40 | }: LineItemsEditorProps) |
| 41 | const idPrefix = |
| 42 | const ridsRef = |
| 43 | if (ridsRef.current |
| 44 | ridsRef.current = |
| 45 | (_, i) |
| 46 | ) |
| 47 | } |
| 48 | |
| 49 | const internalItems: |
| 50 | () => items. |
| 51 | [items] |
| 52 | ) |
| 53 | |
| 54 | const itemsRef = |
| 55 | itemsRef.current = |
| 56 | |
| 57 | const updateItem = |
| 58 | (index: number |
| 59 | const current = |
| 60 | const next = |
| 61 | const merged = |
| 62 | if ('quantity' in |
| 63 | merged.amount = |
| 64 | } |
| 65 | next[index] = |
| 66 | onChange(next) |
| 67 | }, |
| 68 | [onChange] |
| 69 | ) |
| 70 | |
| 71 | const removeItem = |
| 72 | (index: number |
| 73 | ridsRef.current = |
| 74 | onChange(itemsRef.current |
| 75 | }, |
| 76 | [onChange] |
| 77 | ) |
| 78 | |
| 79 | const addItem = |
| 80 | ridsRef.current = |
| 81 | ...ridsRef.current, |
| 82 | `${idPrefix}-${ridsRef |
| 83 | ] |
| 84 | onChange([ |
| 85 | ...itemsRef.current, |
| 86 | { |
| 87 | title: '', |
| 88 | description: '', |
| 89 | quantity: '1', |
| 90 | unitPrice: '0', |
| 91 | amount: '0', |
| 92 | }, |
| 93 | ]) |
| 94 | }, [idPrefix, |
| 95 | |
| 96 | const total = |
| 97 | () => items. |
| 98 | [items] |
| 99 | ) |
| 100 | |
| 101 | return ( |
| 102 | <div className='space-y-3 |
| 103 | <div className='flex items-center justify-between |
| 104 | <h3 className='font-medium text-sm |
| 105 | {editable && ( |
| 106 | <Button onClick={ |
| 107 | <Plus className=' |
| 108 | Add Item |
| 109 | </Button> |
| 110 | )} |
| 111 | </div> |
| 112 | |
| 113 | {items.length > |
| 114 | <div className='overflow-x-auto rounded-md border |
| 115 | <table className='w-full text-sm |
| 116 | <thead> |
| 117 | <tr className='border-b bg-muted/50 |
| 118 | <th className='px-3 py-2 text-left font-medium |
| 119 | <th className='w-24 px-3 py-2 text-right font-medium |
| 120 | <th className='w-32 px-3 py-2 text-right font-medium |
| 121 | Unit Price |
| 122 | </th> |
| 123 | <th className='w-32 px-3 py-2 text-right font-medium |
| 124 | Amount |
| 125 | </th> |
| 126 | {editable && <th |
| 127 | </tr> |
| 128 | </thead> |
| 129 | <tbody> |
| 130 | {internalItems.map(( |
| 131 | <LineItemRow |
| 132 | editable={editable} |
| 133 | index={index} |
| 134 | item={item} |
| 135 | key={item._rid |
| 136 | onRemove={removeItem} |
| 137 | onUpdate={updateItem} |
| 138 | /> |
| 139 | ))} |
| 140 | </tbody> |
| 141 | <tfoot> |
| 142 | <tr className='bg-muted/30 |
| 143 | <td className='px-3 py-2 text-right font-semibold |
| 144 | Total ({currency}) |
| 145 | </td> |
| 146 | <td className='px-3 py-2 text-right font-semibold |
| 147 | {total.toLocaleString(' |
| 148 | minimumFractionDigits: 2, |
| 149 | maximumFractionDigits: 2, |
| 150 | })} |
| 151 | </td> |
| 152 | {editable && <td |
| 153 | </tr> |
| 154 | </tfoot> |
| 155 | </table> |
| 156 | </div> |
| 157 | )} |
| 158 | </div> |
| 159 | ) |
| 160 | } |
| 161 | |
| 162 | interface LineItemRowProps { |
| 163 | editable: boolean |
| 164 | index: number |
| 165 | item: InternalLineItem |
| 166 | onRemove: (index |
| 167 | onUpdate: (index |
| 168 | } |
| 169 | |
| 170 | const LineItemRow = memo |
| 171 | editable, |
| 172 | index, |
| 173 | item, |
| 174 | onRemove, |
| 175 | onUpdate, |
| 176 | }: LineItemRowProps) |
| 177 | return ( |
| 178 | <tr className='border-b last:border-b-0 |
| 179 | <td className='px-3 py-2 |
| 180 | {editable ? ( |
| 181 | <Input |
| 182 | className='h-8 border-none shadow-none focus-visible:ring-0' |
| 183 | defaultValue={item.description |
| 184 | onBlur={(e) |
| 185 | const value = |
| 186 | if (value !== (item |
| 187 | onUpdate(index, { |
| 188 | } |
| 189 | }} |
| 190 | placeholder='Item description' |
| 191 | /> |
| 192 | ) : ( |
| 193 | <span>{item.description |
| 194 | )} |
| 195 | </td> |
| 196 | <td className='px-3 py-2 text-right |
| 197 | {editable ? ( |
| 198 | <Input |
| 199 | className='h-8 border-none text-right shadow-none focus-visible:ring-0' |
| 200 | defaultValue={item.quantity |
| 201 | onBlur={(e) |
| 202 | if (e.target. |
| 203 | onUpdate(index, { |
| 204 | } |
| 205 | }} |
| 206 | /> |
| 207 | ) : ( |
| 208 | <span>{item.quantity |
| 209 | )} |
| 210 | </td> |
| 211 | <td className='px-3 py-2 text-right |
| 212 | {editable ? ( |
| 213 | <Input |
| 214 | className='h-8 border-none text-right shadow-none focus-visible:ring-0' |
| 215 | defaultValue={item.unitPrice |
| 216 | onBlur={(e) |
| 217 | if (e.target. |
| 218 | onUpdate(index, { |
| 219 | } |
| 220 | }} |
| 221 | /> |
| 222 | ) : ( |
| 223 | <span>{item.unitPrice |
| 224 | )} |
| 225 | </td> |
| 226 | <td className='px-3 py-2 text-right font-medium |
| 227 | {Number(item.amount |
| 228 | minimumFractionDigits: 2, |
| 229 | maximumFractionDigits: 2, |
| 230 | })} |
| 231 | </td> |
| 232 | {editable && ( |
| 233 | <td className='px-2 py-2 |
| 234 | <Button |
| 235 | className='h-7 w-7 p-0 text-muted-foreground hover:text-destructive' |
| 236 | onClick={() => |
| 237 | size='sm' |
| 238 | type='button' |
| 239 | variant='ghost' |
| 240 | > |
| 241 | <Trash2 className=' |
| 242 | </Button> |
| 243 | </td> |
| 244 | )} |
| 245 | </tr> |
| 246 | ) |
| 247 | }) |