UNPKG

@accounter/client

Version:
1 lines • 9.86 kB
import{t as e}from"./bank-deposits-Cgjxos5O.js";import{t}from"./pencil-DlKYz-XX.js";import{$ as n,An as r,B as i,Dn as a,En as o,G as s,Gi as c,H as l,Ii as u,K as d,Kl as f,Kt as p,Li as m,Mi as h,Ml as g,Nd as _,Ni as v,On as y,Pi as b,Rd as x,Ri as S,Si as C,St as w,Tu as ee,U as T,V as E,W as D,X as te,Y as ne,_i as O,_n as k,bi as A,cs as j,fl as M,fn as N,fs as P,gd as F,gn as I,hd as L,hn as R,ht as z,jl as B,kn as V,mt as H,q as U,qu as W,vi as G,vn as K,wn as q,wt as J,ya as Y,yi as X}from"./index-DJOqGznr.js";var Z=x(_(),1),re=`createDeposit`,ie=()=>{let[{fetching:e},t]=B(j);return{creating:e,createDeposit:(0,Z.useCallback)(async e=>{let n=`Error creating new deposit ${e.name}`,r=`${re}-${e.name}-${e.currency}-${e.openDate}-${e.accountId??`noAccount`}`;G.loading(`Creating deposit`,{id:r});try{let i=O(await t(e),n,r);if(i)return G.success(`Deposit created`,{id:r,description:`Deposit ${i.createDeposit.id} (${i.createDeposit.currency}) created successfully`}),i.createDeposit.id}catch(e){console.error(`${n}: ${e}`),G.error(`Error`,{id:r,description:n,duration:1e5,closeButton:!0})}return null},[t])}},ae=`updateDeposit`,oe=()=>{let[{fetching:e},t]=B(M);return{updating:e,updateDeposit:(0,Z.useCallback)(async e=>{let n=`Error updating deposit`,r=`${ae}-${e.id}`;G.loading(`Updating deposit…`,{id:r});try{if(O(await t(e),n,r))return G.success(`Deposit updated`,{id:r}),!0}catch(e){console.error(`${n}: ${e}`),G.error(`Error`,{id:r,description:n,duration:1e5,closeButton:!0})}return!1},[t])}},Q=ee(),se=w({name:J().min(1,`Name is required`),currency:z(Object.values(P),{error:`Currency is required`}),openDate:J().min(1,`Open date is required`)}),ce=w({name:J().min(1,`Name is required`),openDate:J().min(1,`Open date is required`),closeDate:J().optional()});function le({onSuccess:e,onClose:t}){let{creating:n,createDeposit:i}=ie(),s=q({resolver:H(se),defaultValues:{name:``,currency:``,openDate:``}}),c=async n=>{await i({name:n.name,currency:n.currency,openDate:n.openDate})&&(e?.(),t())};return(0,Q.jsx)(N,{...s,children:(0,Q.jsxs)(`form`,{onSubmit:s.handleSubmit(c),className:`space-y-4`,children:[(0,Q.jsx)(R,{control:s.control,name:`name`,render:({field:e})=>(0,Q.jsxs)(I,{children:[(0,Q.jsx)(k,{children:`Name`}),(0,Q.jsx)(h,{...e,placeholder:`e.g. High-Yield Savings 2025`,disabled:n}),(0,Q.jsx)(K,{})]})}),(0,Q.jsx)(R,{control:s.control,name:`currency`,render:({field:e})=>(0,Q.jsxs)(I,{children:[(0,Q.jsx)(k,{children:`Currency`}),(0,Q.jsxs)(o,{onValueChange:e.onChange,value:e.value,disabled:n,children:[(0,Q.jsx)(V,{children:(0,Q.jsx)(r,{placeholder:`Select currency`})}),(0,Q.jsx)(a,{children:Object.values(P).map(e=>(0,Q.jsx)(y,{value:e,children:e},e))})]}),(0,Q.jsx)(K,{})]})}),(0,Q.jsx)(R,{control:s.control,name:`openDate`,render:({field:e})=>(0,Q.jsxs)(I,{children:[(0,Q.jsx)(k,{children:`Open Date`}),(0,Q.jsx)(h,{...e,type:`date`,disabled:n}),(0,Q.jsx)(K,{})]})}),(0,Q.jsxs)(u,{children:[(0,Q.jsx)(f,{type:`button`,variant:`outline`,onClick:t,disabled:n,children:`Cancel`}),(0,Q.jsx)(f,{type:`submit`,disabled:n,children:n?`Creating…`:`Create Deposit`})]})]})})}function ue({deposit:e,onSuccess:t,onClose:n}){let{updating:r,updateDeposit:i}=oe(),a=q({resolver:H(ce),defaultValues:{name:e.name,openDate:e.openDate??``,closeDate:e.closeDate??``}});(0,Z.useEffect)(()=>{a.reset({name:e.name,openDate:e.openDate??``,closeDate:e.closeDate??``})},[e,a]);let o=async r=>{await i({id:e.id,name:r.name,openDate:r.openDate,closeDate:r.closeDate||null})&&(t?.(),n())},s=async()=>{let r=c(new Date);a.setValue(`closeDate`,r),await i({id:e.id,name:a.getValues(`name`),openDate:a.getValues(`openDate`),closeDate:r})&&(t?.(),n())},l=async()=>{a.setValue(`closeDate`,``),await i({id:e.id,name:a.getValues(`name`),openDate:a.getValues(`openDate`),closeDate:null})&&(t?.(),n())};return(0,Q.jsx)(N,{...a,children:(0,Q.jsxs)(`form`,{onSubmit:a.handleSubmit(o),className:`space-y-4`,children:[(0,Q.jsx)(R,{control:a.control,name:`name`,render:({field:e})=>(0,Q.jsxs)(I,{children:[(0,Q.jsx)(k,{children:`Name`}),(0,Q.jsx)(h,{...e,disabled:r}),(0,Q.jsx)(K,{})]})}),(0,Q.jsxs)(`div`,{className:`grid grid-cols-2 gap-4`,children:[(0,Q.jsx)(R,{control:a.control,name:`openDate`,render:({field:e})=>(0,Q.jsxs)(I,{children:[(0,Q.jsx)(k,{children:`Open Date`}),(0,Q.jsx)(h,{...e,type:`date`,disabled:r}),(0,Q.jsx)(K,{})]})}),(0,Q.jsx)(R,{control:a.control,name:`closeDate`,render:({field:e})=>(0,Q.jsxs)(I,{children:[(0,Q.jsx)(k,{children:`Close Date`}),(0,Q.jsx)(h,{...e,type:`date`,disabled:r}),(0,Q.jsx)(K,{})]})})]}),(0,Q.jsxs)(`div`,{className:`flex items-center justify-between pt-2 border-t`,children:[e.isOpen?(0,Q.jsx)(f,{type:`button`,variant:`destructive`,size:`sm`,onClick:s,disabled:r,children:`Close Deposit`}):(0,Q.jsx)(f,{type:`button`,variant:`outline`,size:`sm`,onClick:l,disabled:r,children:`Re-open Deposit`}),(0,Q.jsxs)(`div`,{className:`flex gap-2`,children:[(0,Q.jsx)(f,{type:`button`,variant:`outline`,onClick:n,disabled:r,children:`Cancel`}),(0,Q.jsx)(f,{type:`submit`,disabled:r,children:r?`Saving…`:`Save Changes`})]})]})]})})}function $({open:e,onOpenChange:t,deposit:n,onSuccess:r}){let i=!!n;return(0,Q.jsx)(v,{open:e,onOpenChange:t,children:(0,Q.jsxs)(b,{className:`max-w-md`,children:[(0,Q.jsx)(m,{children:(0,Q.jsx)(S,{children:i?`Edit Deposit`:`New Deposit`})}),i?(0,Q.jsx)(ue,{deposit:n,onSuccess:r,onClose:()=>t(!1)}):(0,Q.jsx)(le,{onSuccess:r,onClose:()=>t(!1)})]})})}function de(){let[{data:r,fetching:a},o]=g({query:Y}),[c,u]=(0,Z.useState)([{id:`openDate`,desc:!1}]),[m,h]=(0,Z.useState)(!1),[_,v]=(0,Z.useState)(null),y=(0,Z.useMemo)(()=>(r?.allDeposits??[]).map(e=>({id:e.id,name:e.name,currency:e.currency??null,openDate:e.openDate??null,closeDate:e.closeDate??null,isOpen:e.isOpen,currentBalanceRaw:e.metadata.currentBalance?.raw??0,currentBalanceFormatted:e.metadata.currentBalance?.formatted??``,totalDepositRaw:e.metadata.totalDeposit?.raw??0,totalDepositFormatted:e.metadata.totalDeposit?.formatted??``,totalInterestRaw:e.metadata.totalInterest?.raw??0,totalInterestFormatted:e.metadata.totalInterest?.formatted??``})),[r]),b=(0,Z.useRef)([{id:`expander`,header:()=>null,cell:({row:e})=>(0,Q.jsx)(f,{variant:`ghost`,size:`icon`,onClick:()=>e.toggleExpanded(),"aria-label":e.getIsExpanded()?`Collapse`:`Expand`,children:e.getIsExpanded()?(0,Q.jsx)(F,{}):(0,Q.jsx)(L,{})}),enableSorting:!1},{accessorKey:`name`,header:`Deposit Name`,cell:e=>(0,Q.jsx)(`span`,{className:`font-mono text-xs`,children:e.getValue()})},{accessorKey:`currency`,header:`Currency`},{accessorKey:`isOpen`,header:`Status`,cell:e=>e.getValue()?(0,Q.jsx)(p,{className:`bg-green-600 text-white`,variant:`secondary`,children:`Open`}):(0,Q.jsx)(p,{variant:`secondary`,children:`Closed`}),sortingFn:(e,t)=>Number(e.original.isOpen)-Number(t.original.isOpen)},{accessorKey:`openDate`,header:`Open Date`,cell:e=>(0,Q.jsx)(`span`,{children:e.getValue()})},{accessorKey:`closeDate`,header:`Close Date`,cell:e=>(0,Q.jsx)(`span`,{children:e.getValue()??`-`}),sortingFn:(e,t)=>{let n=e.original.closeDate??``,r=t.original.closeDate??``;return n.localeCompare(r)}},{id:`totalDeposit`,header:`Total Deposit`,accessorFn:e=>e.totalDepositRaw,cell:e=>(0,Q.jsx)(`span`,{className:`tabular-nums`,children:e.row.original.totalDepositFormatted}),sortingFn:(e,t)=>e.original.totalDepositRaw-t.original.totalDepositRaw},{id:`currentBalance`,header:`Current Balance`,accessorFn:e=>e.currentBalanceRaw,cell:e=>(0,Q.jsx)(`span`,{className:`tabular-nums`,children:e.row.original.currentBalanceFormatted}),sortingFn:(e,t)=>e.original.currentBalanceRaw-t.original.currentBalanceRaw},{id:`totalInterest`,header:`Total Interest`,accessorFn:e=>e.totalInterestRaw,cell:e=>(0,Q.jsx)(`span`,{className:`tabular-nums`,children:e.row.original.totalInterestFormatted}),sortingFn:(e,t)=>e.original.totalInterestRaw-t.original.totalInterestRaw},{id:`actions`,header:()=>null,cell:({row:e})=>(0,Q.jsxs)(X,{children:[(0,Q.jsx)(C,{asChild:!0,children:(0,Q.jsx)(f,{variant:`ghost`,size:`icon`,onClick:t=>{t.stopPropagation(),v(e.original)},"aria-label":`Edit deposit`,children:(0,Q.jsx)(t,{className:`h-4 w-4`})})}),(0,Q.jsx)(A,{children:`Edit deposit`})]}),enableSorting:!1}]).current,x=U({data:y,columns:b,state:{sorting:c},onSortingChange:u,getCoreRowModel:ne(),getSortedRowModel:n(),getExpandedRowModel:te(),getRowCanExpand:()=>!0});return(0,Q.jsxs)(`div`,{className:`max-w-[95vw]`,children:[(0,Q.jsxs)(`div`,{className:`flex items-center justify-between`,children:[(0,Q.jsx)(`h1`,{className:`text-2xl font-semibold`,children:`Bank Deposits`}),(0,Q.jsxs)(f,{onClick:()=>h(!0),children:[(0,Q.jsx)(W,{className:`mr-2 h-4 w-4`}),`New Deposit`]})]}),(0,Q.jsx)(`div`,{className:`mt-6 overflow-hidden rounded-md border`,children:(0,Q.jsxs)(i,{children:[(0,Q.jsx)(D,{children:x.getHeaderGroups().map(e=>(0,Q.jsx)(s,{children:e.headers.map(e=>(0,Q.jsx)(T,{colSpan:e.colSpan,children:e.isPlaceholder?null:d(e.column.columnDef.header,e.getContext())},e.id))},e.id))}),(0,Q.jsx)(E,{children:a?(0,Q.jsx)(s,{children:(0,Q.jsx)(l,{colSpan:b.length,className:`h-24 text-center`,children:`Loading...`})}):x.getRowModel().rows?.length?x.getRowModel().rows.map(t=>(0,Q.jsxs)(Q.Fragment,{children:[(0,Q.jsx)(s,{"data-state":t.getIsSelected()&&`selected`,children:t.getVisibleCells().map(e=>(0,Q.jsx)(l,{children:d(e.column.columnDef.cell,e.getContext())},e.id))},t.id),t.getIsExpanded()?(0,Q.jsx)(s,{children:(0,Q.jsx)(l,{colSpan:b.length,children:(0,Q.jsx)(e,{depositId:t.original.id,enableReassign:!0,refetch:o})})}):null]})):(0,Q.jsx)(s,{children:(0,Q.jsx)(l,{colSpan:b.length,className:`h-24 text-center`,children:`No results.`})})})]})}),(0,Q.jsx)($,{open:m,onOpenChange:h,onSuccess:()=>o({requestPolicy:`network-only`})}),(0,Q.jsx)($,{open:!!_,onOpenChange:e=>{e||v(null)},deposit:_??void 0,onSuccess:()=>o({requestPolicy:`network-only`})})]})}export{de as DepositsScreen};