@accounter/client
Version:
Accounter client application
1 lines • 10.1 kB
JavaScript
import{s as e}from"./dist-ypP48_Ax.js";import{i as t,o as n}from"./utils-CbS-oR5I.js";import{r,t as i}from"./error-handling-CKe_iwSC.js";import{i as a,r as o,t as s}from"./bank-deposits-e7ndUl2A.js";import{a as c,c as l,d as u,f as d,ft as f,i as p,l as m,n as h,p as g,pt as _,r as v,s as y,t as b,ut as ee}from"./select-Zdatwv1b.js";import{t as x}from"./pencil-COHyeksM.js";import{t as S}from"./button-DUNsVBiu.js";import{Bc as C,Fl as w,Gc as T,Kc as E,Ln as D,Ml as O,Nl as k,Vc as A,Wc as j,Xc as M,Xn as N,a as te,c as ne,d as P,f as F,i as I,jt as L,n as R,o as z,qc as B,r as V,s as H,t as U}from"./table-AJA7Uyxy.js";import{On as W,Pn as G,Qo as K,Zo as q,ro as J,w as Y}from"./graphql-DC5GYCFE.js";var X=e(n(),1),re=`createDeposit`,ie=()=>{let[{fetching:e},t]=q(W);return{creating:e,createDeposit:(0,X.useCallback)(async e=>{let n=`Error creating new deposit ${e.name}`,a=`${re}-${e.name}-${e.currency}-${e.openDate}-${e.accountId??`noAccount`}`;r.loading(`Creating deposit`,{id:a});try{let o=await t(e),s=i(o,n,a);if(s)return r.success(`Deposit created`,{id:a,description:`Deposit ${s.createDeposit.id} (${s.createDeposit.currency}) created successfully`}),s.createDeposit.id}catch(e){console.error(`${n}: ${e}`),r.error(`Error`,{id:a,description:n,duration:1e5,closeButton:!0})}return null},[t])}},ae=`updateDeposit`,oe=()=>{let[{fetching:e},t]=q(J);return{updating:e,updateDeposit:(0,X.useCallback)(async e=>{let n=`Error updating deposit`,a=`${ae}-${e.id}`;r.loading(`Updating deposit…`,{id:a});try{let o=await t(e);if(i(o,n,a))return r.success(`Deposit updated`,{id:a}),!0}catch(e){console.error(`${n}: ${e}`),r.error(`Error`,{id:a,description:n,duration:1e5,closeButton:!0})}return!1},[t])}},Z=t(),se=D({name:N().min(1,`Name is required`),currency:L(Object.values(G),{error:`Currency is required`}),openDate:N().min(1,`Open date is required`)}),Q=D({name:N().min(1,`Name is required`),openDate:N().min(1,`Open date is required`),closeDate:N().optional()});function ce({onSuccess:e,onClose:t}){let{creating:n,createDeposit:r}=ie(),i=M({resolver:F(se),defaultValues:{name:``,currency:``,openDate:``}}),a=async n=>{await r({name:n.name,currency:n.currency,openDate:n.openDate})&&(e?.(),t())};return(0,Z.jsx)(A,{...i,children:(0,Z.jsxs)(`form`,{onSubmit:i.handleSubmit(a),className:`space-y-4`,children:[(0,Z.jsx)(j,{control:i.control,name:`name`,render:({field:e})=>(0,Z.jsxs)(T,{children:[(0,Z.jsx)(E,{children:`Name`}),(0,Z.jsx)(y,{...e,placeholder:`e.g. High-Yield Savings 2025`,disabled:n}),(0,Z.jsx)(B,{})]})}),(0,Z.jsx)(j,{control:i.control,name:`currency`,render:({field:e})=>(0,Z.jsxs)(T,{children:[(0,Z.jsx)(E,{children:`Currency`}),(0,Z.jsxs)(b,{onValueChange:e.onChange,value:e.value,disabled:n,children:[(0,Z.jsx)(p,{children:(0,Z.jsx)(c,{placeholder:`Select currency`})}),(0,Z.jsx)(h,{children:Object.values(G).map(e=>(0,Z.jsx)(v,{value:e,children:e},e))})]}),(0,Z.jsx)(B,{})]})}),(0,Z.jsx)(j,{control:i.control,name:`openDate`,render:({field:e})=>(0,Z.jsxs)(T,{children:[(0,Z.jsx)(E,{children:`Open Date`}),(0,Z.jsx)(y,{...e,type:`date`,disabled:n}),(0,Z.jsx)(B,{})]})}),(0,Z.jsxs)(u,{children:[(0,Z.jsx)(S,{type:`button`,variant:`outline`,onClick:t,disabled:n,children:`Cancel`}),(0,Z.jsx)(S,{type:`submit`,disabled:n,children:n?`Creating…`:`Create Deposit`})]})]})})}function le({deposit:e,onSuccess:t,onClose:n}){let{updating:r,updateDeposit:i}=oe(),o=M({resolver:F(Q),defaultValues:{name:e.name,openDate:e.openDate??``,closeDate:e.closeDate??``}});(0,X.useEffect)(()=>{o.reset({name:e.name,openDate:e.openDate??``,closeDate:e.closeDate??``})},[e,o]);let s=async r=>{await i({id:e.id,name:r.name,openDate:r.openDate,closeDate:r.closeDate||null})&&(t?.(),n())},c=async()=>{let r=a(new Date);o.setValue(`closeDate`,r),await i({id:e.id,name:o.getValues(`name`),openDate:o.getValues(`openDate`),closeDate:r})&&(t?.(),n())},l=async()=>{o.setValue(`closeDate`,``),await i({id:e.id,name:o.getValues(`name`),openDate:o.getValues(`openDate`),closeDate:null})&&(t?.(),n())};return(0,Z.jsx)(A,{...o,children:(0,Z.jsxs)(`form`,{onSubmit:o.handleSubmit(s),className:`space-y-4`,children:[(0,Z.jsx)(j,{control:o.control,name:`name`,render:({field:e})=>(0,Z.jsxs)(T,{children:[(0,Z.jsx)(E,{children:`Name`}),(0,Z.jsx)(y,{...e,disabled:r}),(0,Z.jsx)(B,{})]})}),(0,Z.jsxs)(`div`,{className:`grid grid-cols-2 gap-4`,children:[(0,Z.jsx)(j,{control:o.control,name:`openDate`,render:({field:e})=>(0,Z.jsxs)(T,{children:[(0,Z.jsx)(E,{children:`Open Date`}),(0,Z.jsx)(y,{...e,type:`date`,disabled:r}),(0,Z.jsx)(B,{})]})}),(0,Z.jsx)(j,{control:o.control,name:`closeDate`,render:({field:e})=>(0,Z.jsxs)(T,{children:[(0,Z.jsx)(E,{children:`Close Date`}),(0,Z.jsx)(y,{...e,type:`date`,disabled:r}),(0,Z.jsx)(B,{})]})})]}),(0,Z.jsxs)(`div`,{className:`flex items-center justify-between pt-2 border-t`,children:[e.isOpen?(0,Z.jsx)(S,{type:`button`,variant:`destructive`,size:`sm`,onClick:c,disabled:r,children:`Close Deposit`}):(0,Z.jsx)(S,{type:`button`,variant:`outline`,size:`sm`,onClick:l,disabled:r,children:`Re-open Deposit`}),(0,Z.jsxs)(`div`,{className:`flex gap-2`,children:[(0,Z.jsx)(S,{type:`button`,variant:`outline`,onClick:n,disabled:r,children:`Cancel`}),(0,Z.jsx)(S,{type:`submit`,disabled:r,children:r?`Saving…`:`Save Changes`})]})]})]})})}function $({open:e,onOpenChange:t,deposit:n,onSuccess:r}){let i=!!n;return(0,Z.jsx)(l,{open:e,onOpenChange:t,children:(0,Z.jsxs)(m,{className:`max-w-md`,children:[(0,Z.jsx)(d,{children:(0,Z.jsx)(g,{children:i?`Edit Deposit`:`New Deposit`})}),i?(0,Z.jsx)(le,{deposit:n,onSuccess:r,onClose:()=>t(!1)}):(0,Z.jsx)(ce,{onSuccess:r,onClose:()=>t(!1)})]})})}function ue(){let[{data:e,fetching:t},n]=K({query:Y}),[r,i]=(0,X.useState)([{id:`openDate`,desc:!1}]),[a,c]=(0,X.useState)(!1),[l,u]=(0,X.useState)(null),[d,p]=(0,X.useState)(0),m=(0,X.useCallback)(()=>{p(e=>e+1),n({requestPolicy:`network-only`})},[n]),h=(0,X.useMemo)(()=>(e?.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??``})),[e]),g=o(h),v=(0,X.useMemo)(()=>[{id:`expander`,header:()=>null,cell:({row:e})=>(0,Z.jsx)(S,{variant:`ghost`,size:`icon`,onClick:()=>e.toggleExpanded(),"aria-label":e.getIsExpanded()?`Collapse`:`Expand`,children:e.getIsExpanded()?(0,Z.jsx)(_,{}):(0,Z.jsx)(f,{})}),enableSorting:!1},{accessorKey:`name`,header:`Deposit Name`,cell:e=>(0,Z.jsx)(`span`,{className:`font-mono text-xs`,children:e.getValue()})},{accessorKey:`currency`,header:`Currency`},{accessorKey:`isOpen`,header:`Status`,cell:e=>e.getValue()?(0,Z.jsx)(C,{className:`bg-green-600 text-white`,variant:`secondary`,children:`Open`}):(0,Z.jsx)(C,{variant:`secondary`,children:`Closed`}),sortFn:(e,t)=>Number(e.original.isOpen)-Number(t.original.isOpen)},{accessorKey:`openDate`,header:`Open Date`,cell:e=>(0,Z.jsx)(`span`,{children:e.getValue()})},{accessorKey:`closeDate`,header:`Close Date`,cell:e=>(0,Z.jsx)(`span`,{children:e.getValue()??`-`}),sortFn:(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,Z.jsx)(`span`,{className:`tabular-nums`,children:e.row.original.totalDepositFormatted}),sortFn:(e,t)=>e.original.totalDepositRaw-t.original.totalDepositRaw},{id:`currentBalance`,header:`Current Balance`,accessorFn:e=>e.currentBalanceRaw,cell:e=>(0,Z.jsx)(`span`,{className:`tabular-nums`,children:e.row.original.currentBalanceFormatted}),sortFn:(e,t)=>e.original.currentBalanceRaw-t.original.currentBalanceRaw},{id:`totalInterest`,header:`Total Interest`,accessorFn:e=>e.totalInterestRaw,cell:e=>(0,Z.jsx)(`span`,{className:`tabular-nums`,children:e.row.original.totalInterestFormatted}),sortFn:(e,t)=>e.original.totalInterestRaw-t.original.totalInterestRaw},{id:`actions`,header:()=>null,cell:({row:e})=>(0,Z.jsxs)(O,{children:[(0,Z.jsx)(w,{asChild:!0,children:(0,Z.jsx)(S,{variant:`ghost`,size:`icon`,onClick:t=>{t.stopPropagation(),u(e.original)},"aria-label":`Edit deposit`,children:(0,Z.jsx)(x,{className:`h-4 w-4`})})}),(0,Z.jsx)(k,{children:`Edit deposit`})]}),enableSorting:!1}],[u]),y=ne({features:H,data:g,columns:v,state:{sorting:r},onSortingChange:i,getRowCanExpand:()=>!0});return(0,Z.jsxs)(`div`,{className:`max-w-[95vw]`,children:[(0,Z.jsxs)(`div`,{className:`flex items-center justify-between`,children:[(0,Z.jsx)(`h1`,{className:`text-2xl font-semibold`,children:`Bank Deposits`}),(0,Z.jsxs)(S,{onClick:()=>c(!0),children:[(0,Z.jsx)(ee,{className:`mr-2 h-4 w-4`}),`New Deposit`]})]}),(0,Z.jsx)(`div`,{className:`mt-6 overflow-hidden rounded-md border`,children:(0,Z.jsxs)(U,{children:[(0,Z.jsx)(te,{children:y.getHeaderGroups().map(e=>(0,Z.jsx)(z,{children:e.headers.map(e=>(0,Z.jsx)(I,{colSpan:e.colSpan,children:e.isPlaceholder?null:P(e.column.columnDef.header,e.getContext())},e.id))},e.id))}),(0,Z.jsx)(R,{children:t&&!e?(0,Z.jsx)(z,{children:(0,Z.jsx)(V,{colSpan:v.length,className:`h-24 text-center`,children:`Loading...`})}):y.getRowModel().rows?.length?y.getRowModel().rows.map(e=>(0,Z.jsxs)(X.Fragment,{children:[(0,Z.jsx)(z,{"data-state":e.getIsSelected()&&`selected`,children:e.getVisibleCells().map(e=>(0,Z.jsx)(V,{children:P(e.column.columnDef.cell,e.getContext())},e.id))}),e.getIsExpanded()?(0,Z.jsx)(z,{children:(0,Z.jsx)(V,{colSpan:v.length,children:(0,Z.jsx)(s,{depositId:e.original.id,enableReassign:!0,refetch:m,reassignToken:d})})}):null]},e.id)):(0,Z.jsx)(z,{children:(0,Z.jsx)(V,{colSpan:v.length,className:`h-24 text-center`,children:`No results.`})})})]})}),(0,Z.jsx)($,{open:a,onOpenChange:c,onSuccess:()=>n({requestPolicy:`network-only`})}),(0,Z.jsx)($,{open:!!l,onOpenChange:e=>{e||u(null)},deposit:l??void 0,onSuccess:()=>n({requestPolicy:`network-only`})})]})}export{ue as DepositsScreen};