@accounter/client
Version:
Accounter client application
1 lines • 12.2 kB
JavaScript
import{Al as e,An as t,Bu as n,Cu as r,Dn as i,Dt as a,En as o,Et as s,Fi as ee,Gu as c,Id as l,Ii as u,Kt as d,Li as f,Mi as p,Ml as m,Ni as te,Nu as h,On as g,Pi as ne,Rc as _,Ri as v,St as y,Ts as b,Tt as x,Wa as S,Wi as C,Wl as w,Xi as re,Zi as ie,_i as T,_n as E,_t as D,bt as O,dn as k,ds as A,fn as ae,gn as j,gt as M,hn as N,ht as P,in as F,jd as I,ji as oe,kl as L,kn as R,mt as se,ns as ce,oc as z,pn as B,ss as V,ul as H,vi as U,vn as W,vt as G,wn as le,wt as K,xt as q,yd as J,zi as ue}from"./index-DUZECcXr.js";var de=J(`book-copy`,[[`path`,{d:`M5 7a2 2 0 0 0-2 2v11`,key:`1yhqjt`}],[`path`,{d:`M5.803 18H5a2 2 0 0 0 0 4h9.5a.5.5 0 0 0 .5-.5V21`,key:`edzzo5`}],[`path`,{d:`M9 15V4a2 2 0 0 1 2-2h9.5a.5.5 0 0 1 .5.5v14a.5.5 0 0 1-.5.5H11a2 2 0 0 1 0-4h10`,key:`1nwzrg`}]]),Y=l(I(),1),X=`createContract`,fe=()=>{let[{fetching:e},t]=L(V);return{creating:e,createContract:(0,Y.useCallback)(async e=>{let n=`Error creating new contract for client[${e.input.clientId}]`,r=`${X}-${e.input.clientId}`;U.loading(`Creating contract`,{id:r});try{T(await t(e),n,r)&&U.success(`Success`,{id:r,description:`Contract for client ${e.input.clientId} was successfully created`})}catch(e){console.error(`${n}: ${e}`),U.error(`Error`,{id:r,description:n,duration:1e5,closeButton:!0})}},[t])}},Z=`updateContract`,pe=()=>{let[{fetching:e},t]=L(H);return{updating:e,updateContract:(0,Y.useCallback)(async e=>{let n=`Error updating contract ID [${e.contractId}]`,r=`${Z}-${e.contractId}`;U.loading(`Updating contract`,{id:r});try{let i=T(await t(e),n,r,`updateContract`);if(i)return U.success(`Success`,{id:r,description:`Contract updated`}),i.updateContract}catch(e){console.error(`${n}: ${e}`),U.error(`Error`,{id:r,description:n,duration:1e5,closeButton:!0})}},[t])}},Q=r(),me=y({id:s().optional(),startDate:a(`Start date is required`),endDate:a(`End date is required`),pos:M(K()),paymentAmount:q().min(0,`Payment amount must be non-negative`),paymentCurrency:P(Object.values(A),`Currency is required`),productType:P(Object.values(z)).optional(),msCloudLink:x().optional().or(O(``)),billingCycle:P(Object.values(S)).optional(),subscriptionPlan:P(Object.values(_)).optional(),isActive:G(),defaultRemark:K().optional(),defaultDocumentType:P(Object.values(b)),operationsLimit:D().min(BigInt(0),`Operations limit must be non-negative`)}),he={startDate:``,endDate:``,pos:[],paymentAmount:0,paymentCurrency:A.Usd,productType:z.Hive,msCloudLink:void 0,billingCycle:S.Monthly,subscriptionPlan:void 0,isActive:!0,defaultRemark:void 0,defaultDocumentType:b.Proforma,operationsLimit:BigInt(0)};function $({clientId:r,contract:a,contractId:s,onDone:l,duplicate:y}){let{isEditing:x,isDuplicating:T}=(0,Y.useMemo)(()=>{let e=!!a||!!s,t=y===!0&&e;return{isEditing:e&&!t,isDuplicating:t}},[a,s,y]),[D,O]=(0,Y.useState)(!1),[M,P]=(0,Y.useState)(!1),[I,L]=(0,Y.useState)(he),[z,V]=(0,Y.useState)(``),{updateContract:H,updating:U}=pe(),{createContract:G,creating:K}=fe(),[{data:q,fetching:J}]=e({query:ce,pause:!s||!D,variables:{contractId:s??``}}),X=le({resolver:se(me),defaultValues:I}),Z=()=>{let e=z.trim();if(e){let t=X.getValues(`pos`);t.includes(e)||X.setValue(`pos`,[...t,e],{shouldDirty:!0}),V(``)}},$=e=>{let t=X.getValues(`pos`);X.setValue(`pos`,t.filter((t,n)=>n!==e),{shouldDirty:!0})};(0,Y.useEffect)(()=>{a&&L(a)},[a]),(0,Y.useEffect)(()=>{if(q?.contractsById){let e=q.contractsById,t={startDate:e.startDate,endDate:e.endDate,pos:e.purchaseOrders,paymentAmount:e.amount.raw,paymentCurrency:e.amount.currency,productType:e.product??void 0,msCloudLink:e.msCloud?.toString()??void 0,billingCycle:e.billingCycle,subscriptionPlan:e.plan??void 0,isActive:e.isActive,defaultRemark:e.remarks??void 0,defaultDocumentType:e.documentType,operationsLimit:BigInt(e.operationsLimit)};L({id:e.id,...t})}},[q]),(0,Y.useEffect)(()=>{I&&X.reset({startDate:I.startDate,endDate:I.endDate,pos:I.pos,paymentAmount:I.paymentAmount,paymentCurrency:I.paymentCurrency,productType:I.productType,msCloudLink:I.msCloudLink,billingCycle:I.billingCycle,subscriptionPlan:I.subscriptionPlan,isActive:I.isActive,defaultRemark:I.defaultRemark,defaultDocumentType:I.defaultDocumentType,operationsLimit:BigInt(I.operationsLimit)})},[(0,Y.useMemo)(()=>I?JSON.stringify(I):null,[I]),X.reset]);let ge=(0,Y.useCallback)(async e=>{if(T||x){if(!I?.id)return;if(T){if(!e.billingCycle){X.setError(`billingCycle`,{message:`Billing cycle is required`});return}await G({input:{clientId:r,amount:{raw:e.paymentAmount,currency:e.paymentCurrency},billingCycle:e.billingCycle,documentType:e.defaultDocumentType,endDate:m(new Date(e.endDate),`yyyy-MM-dd`),isActive:e.isActive,msCloud:e.msCloudLink,plan:e.subscriptionPlan,product:e.productType,purchaseOrders:e.pos,remarks:e.defaultRemark,startDate:m(new Date(e.startDate),`yyyy-MM-dd`),operationsLimit:e.operationsLimit,deactivateContracts:M?[I.id]:void 0}})}else await H({contractId:I.id,input:{amount:{raw:e.paymentAmount,currency:e.paymentCurrency},billingCycle:e.billingCycle,documentType:e.defaultDocumentType,endDate:m(new Date(e.endDate),`yyyy-MM-dd`),isActive:e.isActive,msCloud:e.msCloudLink,plan:e.subscriptionPlan,product:e.productType,purchaseOrders:e.pos,remarks:e.defaultRemark,startDate:m(new Date(e.startDate),`yyyy-MM-dd`),operationsLimit:e.operationsLimit}})}else{if(!e.billingCycle){X.setError(`billingCycle`,{message:`Billing cycle is required`});return}await G({input:{clientId:r,amount:{raw:e.paymentAmount,currency:e.paymentCurrency},billingCycle:e.billingCycle,documentType:e.defaultDocumentType,endDate:m(new Date(e.endDate),`yyyy-MM-dd`),isActive:e.isActive,msCloud:e.msCloudLink,plan:e.subscriptionPlan,product:e.productType,purchaseOrders:e.pos,remarks:e.defaultRemark,startDate:m(new Date(e.startDate),`yyyy-MM-dd`),operationsLimit:e.operationsLimit}})}O(!1),l?.()},[I,l,G,H,r,X,x,T,M]);return(0,Q.jsxs)(te,{open:D,onOpenChange:O,children:[(0,Q.jsx)(ue,{asChild:!0,children:x?(0,Q.jsx)(w,{variant:`ghost`,size:`sm`,onClick:()=>O(!0),children:(0,Q.jsx)(n,{className:`size-5`})}):T?(0,Q.jsx)(w,{variant:`ghost`,size:`sm`,onClick:()=>O(!0),children:(0,Q.jsx)(de,{className:`size-5`})}):(0,Q.jsxs)(w,{size:`sm`,onClick:()=>O(!0),children:[(0,Q.jsx)(c,{className:`size-4 mr-2`}),`New Contract`]})}),(0,Q.jsxs)(ne,{className:`max-w-2xl max-h-[90vh] overflow-y-auto`,children:[(0,Q.jsxs)(f,{children:[(0,Q.jsx)(v,{children:x?`Edit Contract`:`Create New Contract`}),(0,Q.jsx)(ee,{children:x?`Update contract details`:T?`Create a new contract based on an existing one`:`Add a new contract with all required details`})]}),J?(0,Q.jsx)(Q.Fragment,{children:`Fetching contract details...`}):(0,Q.jsx)(ae,{...X,children:(0,Q.jsxs)(`form`,{onSubmit:X.handleSubmit(ge),children:[(0,Q.jsxs)(`div`,{className:`grid gap-4 py-4`,children:[(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-2`,children:[(0,Q.jsx)(N,{control:X.control,name:`operationsLimit`,render:({field:e})=>(0,Q.jsxs)(j,{children:[(0,Q.jsx)(E,{children:`Operations Limit`}),(0,Q.jsx)(B,{children:(0,Q.jsx)(p,{placeholder:`500`,...e,value:e.value?.toLocaleString(),onChange:t=>{let n=t.target.value.replace(/[^0-9]/g,``);e.onChange(BigInt(n||0))}})}),(0,Q.jsx)(W,{})]})}),(0,Q.jsx)(N,{control:X.control,name:`pos`,render:({field:e})=>(0,Q.jsxs)(j,{children:[(0,Q.jsx)(E,{children:`Purchase Orders`}),(0,Q.jsxs)(`div`,{className:`flex gap-2`,children:[(0,Q.jsx)(p,{placeholder:`Add PO...`,value:z,onChange:e=>V(e.target.value),onKeyDown:e=>{e.key===`Enter`&&(e.preventDefault(),Z())}}),(0,Q.jsx)(w,{type:`button`,size:`sm`,onClick:Z,children:(0,Q.jsx)(c,{className:`h-4 w-4`})})]}),(0,Q.jsx)(`div`,{className:`flex flex-wrap gap-2 mt-2`,children:e.value?.map((t,n)=>(0,Q.jsxs)(d,{variant:`secondary`,className:`gap-1 max-w-xs truncate`,children:[t,n===(e.value?.length??0)-1&&(0,Q.jsx)(w,{variant:`ghost`,size:`icon`,className:`p-0 size-3`,onClick:()=>$(n),children:(0,Q.jsx)(h,{className:`size-3 cursor-pointer flex-shrink-0`})})]},t))}),(0,Q.jsx)(W,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-2`,children:[(0,Q.jsx)(N,{control:X.control,name:`startDate`,render:({field:{onChange:e,...t}})=>(0,Q.jsxs)(j,{children:[(0,Q.jsx)(E,{children:`Start Date`}),(0,Q.jsx)(B,{children:(0,Q.jsx)(p,{type:`date`,...t,onInput:t=>e(t)})}),(0,Q.jsx)(W,{})]})}),(0,Q.jsx)(N,{control:X.control,name:`endDate`,render:({field:{onChange:e,...t}})=>(0,Q.jsxs)(j,{children:[(0,Q.jsx)(E,{children:`End Date`}),(0,Q.jsx)(B,{children:(0,Q.jsx)(p,{type:`date`,...t,onInput:t=>e(t)})}),(0,Q.jsx)(W,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-3`,children:[(0,Q.jsx)(N,{control:X.control,name:`paymentAmount`,render:({field:e})=>(0,Q.jsxs)(j,{className:`md:col-span-2`,children:[(0,Q.jsx)(E,{children:`Payment Amount`}),(0,Q.jsx)(B,{children:(0,Q.jsx)(p,{type:`number`,placeholder:`24000`,...e,onChange:t=>{e.onChange(t?.target.value?Number(t?.target.value):void 0)}})}),(0,Q.jsx)(W,{})]})}),(0,Q.jsx)(N,{control:X.control,name:`paymentCurrency`,render:({field:e})=>(0,Q.jsxs)(j,{children:[(0,Q.jsx)(E,{children:`Currency`}),(0,Q.jsxs)(o,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(B,{children:(0,Q.jsx)(R,{children:(0,Q.jsx)(t,{})})}),(0,Q.jsx)(i,{children:Object.values(A).map(e=>(0,Q.jsx)(g,{value:e,children:e},e))})]}),(0,Q.jsx)(W,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-2`,children:[(0,Q.jsx)(N,{control:X.control,name:`productType`,render:({field:e})=>(0,Q.jsxs)(j,{children:[(0,Q.jsx)(E,{children:`Product Type`}),(0,Q.jsx)(B,{children:(0,Q.jsx)(p,{placeholder:`Cloud Services`,...e})}),(0,Q.jsx)(W,{})]})}),(0,Q.jsx)(N,{control:X.control,name:`billingCycle`,render:({field:e})=>(0,Q.jsxs)(j,{children:[(0,Q.jsx)(E,{children:`Billing Cycle`}),(0,Q.jsxs)(o,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(B,{children:(0,Q.jsx)(R,{children:(0,Q.jsx)(t,{})})}),(0,Q.jsx)(i,{children:Object.values(S).map(e=>(0,Q.jsx)(g,{value:e,children:re(e)},e))})]}),(0,Q.jsx)(W,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-2`,children:[(0,Q.jsx)(N,{control:X.control,name:`subscriptionPlan`,render:({field:e})=>(0,Q.jsxs)(j,{children:[(0,Q.jsx)(E,{children:`Subscription Plan`}),(0,Q.jsx)(B,{children:(0,Q.jsxs)(o,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(B,{children:(0,Q.jsx)(R,{children:(0,Q.jsx)(t,{})})}),(0,Q.jsx)(i,{children:Object.values(_).map(e=>(0,Q.jsx)(g,{value:e,children:ie(e)},e))})]})}),(0,Q.jsx)(W,{})]})}),(0,Q.jsx)(N,{control:X.control,name:`isActive`,render:({field:e})=>(0,Q.jsxs)(j,{className:`flex flex-row items-center justify-between rounded-lg border p-4`,children:[(0,Q.jsx)(`div`,{className:`space-y-0.5`,children:(0,Q.jsx)(E,{className:`text-base`,children:`Active Status`})}),(0,Q.jsx)(B,{children:(0,Q.jsx)(k,{checked:e.value,onCheckedChange:e.onChange})})]})})]}),(0,Q.jsx)(N,{control:X.control,name:`msCloudLink`,render:({field:e})=>(0,Q.jsxs)(j,{children:[(0,Q.jsx)(E,{children:`MS Cloud Link`}),(0,Q.jsx)(B,{children:(0,Q.jsx)(p,{type:`url`,placeholder:`https://portal.azure.com/contract-id`,...e})}),(0,Q.jsx)(W,{})]})}),(0,Q.jsx)(N,{control:X.control,name:`defaultDocumentType`,render:({field:e})=>(0,Q.jsxs)(j,{children:[(0,Q.jsx)(E,{children:`Default Document Type`}),(0,Q.jsxs)(o,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(B,{children:(0,Q.jsx)(R,{children:(0,Q.jsx)(t,{})})}),(0,Q.jsx)(i,{children:Object.values(b).map(e=>(0,Q.jsx)(g,{value:e,children:C(e)},e))})]}),(0,Q.jsx)(W,{})]})}),(0,Q.jsx)(N,{control:X.control,name:`defaultRemark`,render:({field:e})=>(0,Q.jsxs)(j,{children:[(0,Q.jsx)(E,{children:`Default Remark`}),(0,Q.jsx)(B,{children:(0,Q.jsx)(F,{placeholder:`Enter default remark for this contract...`,rows:3,...e})}),(0,Q.jsx)(W,{})]})}),T&&(0,Q.jsxs)(`div`,{className:`flex flex-row items-center justify-between rounded-lg border p-4`,children:[(0,Q.jsx)(`div`,{className:`space-y-0.5`,children:(0,Q.jsx)(oe,{className:`text-base`,children:`Deactivate Original Contract`})}),(0,Q.jsx)(k,{checked:M,onCheckedChange:P})]})]}),(0,Q.jsxs)(u,{children:[(0,Q.jsx)(w,{type:`button`,variant:`outline`,onClick:()=>O(!1),children:`Cancel`}),(0,Q.jsx)(w,{type:`submit`,disabled:K||U,children:x?`Update Contract`:`Create Contract`})]})]})})]})]})}export{$ as t};