@accounter/client
Version:
Accounter client application
1 lines • 12.4 kB
JavaScript
import{s as e}from"./dist-BbgEzppd.js";import{i as t,o as n}from"./utils-BUNz070n.js";import{Dn as r,Ii as i,Ja as a,Mn as o,Sn as s,Xn as c,Ya as ee,et as l,ga as u,oi as d,r as f,t as p}from"./error-handling-AQOG1RNy.js";import{a as m,c as h,d as g,f as te,gt as _,i as v,l as ne,n as y,o as re,p as ie,r as b,s as x,t as S,u as ae,ut as oe}from"./select-6mBYg_R6.js";import{At as se,B as C,Ft as w,G as T,I as ce,It as E,J as D,Mr as O,Nt as k,Pt as A,R as j,U as M,V as N,Vt as le,W as P,X as F,Y as I,br as L,jt as R,kt as z,mr as B,q as V,xr as H,z as U}from"./common-Cy-x5Vhe.js";import{n as ue,p as W,y as G}from"./separator-EFzchRgs.js";import{t as K}from"./button-CBTN3XMQ.js";import{at as q,h as J}from"./table-tK8pMUMS.js";var de=_(`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=e(n(),1),X=`createContract`,fe=()=>{let[{fetching:e},t]=a(r);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}`;f.loading(`Creating contract`,{id:r});try{p(await t(e),n,r)&&f.success(`Success`,{id:r,description:`Contract for client ${e.input.clientId} was successfully created`})}catch(e){console.error(`${n}: ${e}`),f.error(`Error`,{id:r,description:n,duration:1e5,closeButton:!0})}},[t])}},Z=`updateContract`,pe=()=>{let[{fetching:e},t]=a(u);return{updating:e,updateContract:(0,Y.useCallback)(async e=>{let n=`Error updating contract ID [${e.contractId}]`,r=`${Z}-${e.contractId}`;f.loading(`Updating contract`,{id:r});try{let i=p(await t(e),n,r,`updateContract`);if(i)return f.success(`Success`,{id:r,description:`Contract updated`}),i.updateContract}catch(e){console.error(`${n}: ${e}`),f.error(`Error`,{id:r,description:n,duration:1e5,closeButton:!0})}},[t])}},Q=t(),me=T({id:I().optional(),startDate:F(`Start date is required`),endDate:F(`End date is required`),pos:U(V()),paymentAmount:P().min(0,`Payment amount must be non-negative`),paymentCurrency:j(Object.values(o),`Currency is required`),productType:j(Object.values(d)).optional(),msCloudLink:D().optional().or(M(``)),billingCycle:j(Object.values(l)).optional(),subscriptionPlan:j(Object.values(i)).optional(),isActive:N(),defaultRemark:V().optional(),defaultDocumentType:j(Object.values(c)),operationsLimit:C().min(BigInt(0),`Operations limit must be non-negative`)}),he={startDate:``,endDate:``,pos:[],paymentAmount:0,paymentCurrency:o.Usd,productType:d.Hive,msCloudLink:void 0,billingCycle:l.Monthly,subscriptionPlan:void 0,isActive:!0,defaultRemark:void 0,defaultDocumentType:c.Proforma,operationsLimit:BigInt(0)};function $({clientId:e,contract:t,contractId:n,onDone:r,duplicate:a}){let{isEditing:u,isDuplicating:d}=(0,Y.useMemo)(()=>{let e=!!t||!!n,r=a===!0&&e;return{isEditing:e&&!r,isDuplicating:r}},[t,n,a]),[f,p]=(0,Y.useState)(!1),[_,C]=(0,Y.useState)(!1),[T,D]=(0,Y.useState)(he),[j,M]=(0,Y.useState)(``),{updateContract:N,updating:P}=pe(),{createContract:F,creating:I}=fe(),[{data:V,fetching:U}]=ee({query:s,pause:!n||!f,variables:{contractId:n??``}}),X=le({resolver:ce(me),defaultValues:T}),Z=()=>{let e=j.trim();if(e){let t=X.getValues(`pos`);t.includes(e)||X.setValue(`pos`,[...t,e],{shouldDirty:!0}),M(``)}},$=e=>{let t=X.getValues(`pos`);X.setValue(`pos`,t.filter((t,n)=>n!==e),{shouldDirty:!0})};(0,Y.useEffect)(()=>{t&&D(t)},[t]),(0,Y.useEffect)(()=>{if(V?.contractsById){let e=V.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)};D({id:e.id,...t})}},[V]),(0,Y.useEffect)(()=>{T&&X.reset({startDate:T.startDate,endDate:T.endDate,pos:T.pos,paymentAmount:T.paymentAmount,paymentCurrency:T.paymentCurrency,productType:T.productType,msCloudLink:T.msCloudLink,billingCycle:T.billingCycle,subscriptionPlan:T.subscriptionPlan,isActive:T.isActive,defaultRemark:T.defaultRemark,defaultDocumentType:T.defaultDocumentType,operationsLimit:BigInt(T.operationsLimit)})},[(0,Y.useMemo)(()=>T?JSON.stringify(T):null,[T]),X.reset]);let ge=(0,Y.useCallback)(async t=>{if(d||u){if(!T?.id)return;if(d){if(!t.billingCycle){X.setError(`billingCycle`,{message:`Billing cycle is required`});return}await F({input:{clientId:e,amount:{raw:t.paymentAmount,currency:t.paymentCurrency},billingCycle:t.billingCycle,documentType:t.defaultDocumentType,endDate:q(new Date(t.endDate),`yyyy-MM-dd`),isActive:t.isActive,msCloud:t.msCloudLink,plan:t.subscriptionPlan,product:t.productType,purchaseOrders:t.pos,remarks:t.defaultRemark,startDate:q(new Date(t.startDate),`yyyy-MM-dd`),operationsLimit:t.operationsLimit,deactivateContracts:_?[T.id]:void 0}})}else await N({contractId:T.id,input:{amount:{raw:t.paymentAmount,currency:t.paymentCurrency},billingCycle:t.billingCycle,documentType:t.defaultDocumentType,endDate:q(new Date(t.endDate),`yyyy-MM-dd`),isActive:t.isActive,msCloud:t.msCloudLink,plan:t.subscriptionPlan,product:t.productType,purchaseOrders:t.pos,remarks:t.defaultRemark,startDate:q(new Date(t.startDate),`yyyy-MM-dd`),operationsLimit:t.operationsLimit}})}else{if(!t.billingCycle){X.setError(`billingCycle`,{message:`Billing cycle is required`});return}await F({input:{clientId:e,amount:{raw:t.paymentAmount,currency:t.paymentCurrency},billingCycle:t.billingCycle,documentType:t.defaultDocumentType,endDate:q(new Date(t.endDate),`yyyy-MM-dd`),isActive:t.isActive,msCloud:t.msCloudLink,plan:t.subscriptionPlan,product:t.productType,purchaseOrders:t.pos,remarks:t.defaultRemark,startDate:q(new Date(t.startDate),`yyyy-MM-dd`),operationsLimit:t.operationsLimit}})}p(!1),r?.()},[T,r,F,N,e,X,u,d,_]);return(0,Q.jsxs)(x,{open:f,onOpenChange:p,children:[(0,Q.jsx)(ie,{asChild:!0,children:u?(0,Q.jsx)(K,{variant:`ghost`,size:`sm`,onClick:()=>p(!0),children:(0,Q.jsx)(O,{className:`size-5`})}):d?(0,Q.jsx)(K,{variant:`ghost`,size:`sm`,onClick:()=>p(!0),children:(0,Q.jsx)(de,{className:`size-5`})}):(0,Q.jsxs)(K,{size:`sm`,onClick:()=>p(!0),children:[(0,Q.jsx)(G,{className:`size-4 mr-2`}),`New Contract`]})}),(0,Q.jsxs)(h,{className:`max-w-2xl max-h-[90vh] overflow-y-auto`,children:[(0,Q.jsxs)(g,{children:[(0,Q.jsx)(te,{children:u?`Edit Contract`:`Create New Contract`}),(0,Q.jsx)(ne,{children:u?`Update contract details`:d?`Create a new contract based on an existing one`:`Add a new contract with all required details`})]}),U?(0,Q.jsx)(Q.Fragment,{children:`Fetching contract details...`}):(0,Q.jsx)(se,{...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)(k,{control:X.control,name:`operationsLimit`,render:({field:e})=>(0,Q.jsxs)(A,{children:[(0,Q.jsx)(w,{children:`Operations Limit`}),(0,Q.jsx)(R,{children:(0,Q.jsx)(W,{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)(E,{})]})}),(0,Q.jsx)(k,{control:X.control,name:`pos`,render:({field:e})=>(0,Q.jsxs)(A,{children:[(0,Q.jsx)(w,{children:`Purchase Orders`}),(0,Q.jsxs)(`div`,{className:`flex gap-2`,children:[(0,Q.jsx)(W,{placeholder:`Add PO...`,value:j,onChange:e=>M(e.target.value),onKeyDown:e=>{e.key===`Enter`&&(e.preventDefault(),Z())}}),(0,Q.jsx)(K,{type:`button`,size:`sm`,onClick:Z,children:(0,Q.jsx)(G,{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)(J,{variant:`secondary`,className:`gap-1 max-w-xs truncate`,children:[t,n===(e.value?.length??0)-1&&(0,Q.jsx)(K,{variant:`ghost`,size:`icon`,className:`p-0 size-3`,onClick:()=>$(n),children:(0,Q.jsx)(oe,{className:`size-3 cursor-pointer flex-shrink-0`})})]},t))}),(0,Q.jsx)(E,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-2`,children:[(0,Q.jsx)(k,{control:X.control,name:`startDate`,render:({field:{onChange:e,...t}})=>(0,Q.jsxs)(A,{children:[(0,Q.jsx)(w,{children:`Start Date`}),(0,Q.jsx)(R,{children:(0,Q.jsx)(W,{type:`date`,...t,onInput:t=>e(t)})}),(0,Q.jsx)(E,{})]})}),(0,Q.jsx)(k,{control:X.control,name:`endDate`,render:({field:{onChange:e,...t}})=>(0,Q.jsxs)(A,{children:[(0,Q.jsx)(w,{children:`End Date`}),(0,Q.jsx)(R,{children:(0,Q.jsx)(W,{type:`date`,...t,onInput:t=>e(t)})}),(0,Q.jsx)(E,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-3`,children:[(0,Q.jsx)(k,{control:X.control,name:`paymentAmount`,render:({field:e})=>(0,Q.jsxs)(A,{className:`md:col-span-2`,children:[(0,Q.jsx)(w,{children:`Payment Amount`}),(0,Q.jsx)(R,{children:(0,Q.jsx)(W,{type:`number`,placeholder:`24000`,...e,onChange:t=>{e.onChange(t?.target.value?Number(t?.target.value):void 0)}})}),(0,Q.jsx)(E,{})]})}),(0,Q.jsx)(k,{control:X.control,name:`paymentCurrency`,render:({field:e})=>(0,Q.jsxs)(A,{children:[(0,Q.jsx)(w,{children:`Currency`}),(0,Q.jsxs)(S,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(R,{children:(0,Q.jsx)(v,{children:(0,Q.jsx)(m,{})})}),(0,Q.jsx)(y,{children:Object.values(o).map(e=>(0,Q.jsx)(b,{value:e,children:e},e))})]}),(0,Q.jsx)(E,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-2`,children:[(0,Q.jsx)(k,{control:X.control,name:`productType`,render:({field:e})=>(0,Q.jsxs)(A,{children:[(0,Q.jsx)(w,{children:`Product Type`}),(0,Q.jsx)(R,{children:(0,Q.jsx)(W,{placeholder:`Cloud Services`,...e})}),(0,Q.jsx)(E,{})]})}),(0,Q.jsx)(k,{control:X.control,name:`billingCycle`,render:({field:e})=>(0,Q.jsxs)(A,{children:[(0,Q.jsx)(w,{children:`Billing Cycle`}),(0,Q.jsxs)(S,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(R,{children:(0,Q.jsx)(v,{children:(0,Q.jsx)(m,{})})}),(0,Q.jsx)(y,{children:Object.values(l).map(e=>(0,Q.jsx)(b,{value:e,children:L(e)},e))})]}),(0,Q.jsx)(E,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-2`,children:[(0,Q.jsx)(k,{control:X.control,name:`subscriptionPlan`,render:({field:e})=>(0,Q.jsxs)(A,{children:[(0,Q.jsx)(w,{children:`Subscription Plan`}),(0,Q.jsx)(R,{children:(0,Q.jsxs)(S,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(R,{children:(0,Q.jsx)(v,{children:(0,Q.jsx)(m,{})})}),(0,Q.jsx)(y,{children:Object.values(i).map(e=>(0,Q.jsx)(b,{value:e,children:H(e)},e))})]})}),(0,Q.jsx)(E,{})]})}),(0,Q.jsx)(k,{control:X.control,name:`isActive`,render:({field:e})=>(0,Q.jsxs)(A,{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)(w,{className:`text-base`,children:`Active Status`})}),(0,Q.jsx)(R,{children:(0,Q.jsx)(z,{checked:e.value,onCheckedChange:e.onChange})})]})})]}),(0,Q.jsx)(k,{control:X.control,name:`msCloudLink`,render:({field:e})=>(0,Q.jsxs)(A,{children:[(0,Q.jsx)(w,{children:`MS Cloud Link`}),(0,Q.jsx)(R,{children:(0,Q.jsx)(W,{type:`url`,placeholder:`https://portal.azure.com/contract-id`,...e})}),(0,Q.jsx)(E,{})]})}),(0,Q.jsx)(k,{control:X.control,name:`defaultDocumentType`,render:({field:e})=>(0,Q.jsxs)(A,{children:[(0,Q.jsx)(w,{children:`Default Document Type`}),(0,Q.jsxs)(S,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(R,{children:(0,Q.jsx)(v,{children:(0,Q.jsx)(m,{})})}),(0,Q.jsx)(y,{children:Object.values(c).map(e=>(0,Q.jsx)(b,{value:e,children:B(e)},e))})]}),(0,Q.jsx)(E,{})]})}),(0,Q.jsx)(k,{control:X.control,name:`defaultRemark`,render:({field:e})=>(0,Q.jsxs)(A,{children:[(0,Q.jsx)(w,{children:`Default Remark`}),(0,Q.jsx)(R,{children:(0,Q.jsx)(ue,{placeholder:`Enter default remark for this contract...`,rows:3,...e})}),(0,Q.jsx)(E,{})]})}),d&&(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)(re,{className:`text-base`,children:`Deactivate Original Contract`})}),(0,Q.jsx)(z,{checked:_,onCheckedChange:C})]})]}),(0,Q.jsxs)(ae,{children:[(0,Q.jsx)(K,{type:`button`,variant:`outline`,onClick:()=>p(!1),children:`Cancel`}),(0,Q.jsx)(K,{type:`submit`,disabled:I||P,children:u?`Update Contract`:`Create Contract`})]})]})})]})]})}export{$ as t};