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