@accounter/client
Version:
Accounter client application
1 lines • 12.8 kB
JavaScript
import{s as e}from"./dist-ypP48_Ax.js";import{i as t,o as n}from"./utils-CbS-oR5I.js";import{Ea as r,En as i,Fi as a,Rn as o,es as s,ir as c,jn as l,oo as u,r as d,st as f,t as p,ts as ee}from"./error-handling-Bgq5bXpB.js";import{n as m}from"./loader-circle-CfW9jOIr.js";import{Xn as h,hr as te,vt as g}from"./common-DXImSOXZ.js";import{a as _,c as ne,d as re,f as ie,i as v,l as ae,lt as oe,m as y,n as b,o as se,p as x,r as S,s as C,t as w,u as T,ut as E}from"./select-CGkwaf8-.js";import{o as D}from"./separator-B1NtWEAX.js";import{t as O}from"./button-DUNsVBiu.js";import{Bc as k,Cn as A,Fr as j,Gc as M,Hc as N,Ht as P,In as F,Kc as I,Ln as L,Rt as R,Ul as z,Vc as ce,Vt as B,Wc as V,Xc as le,Xn as H,dr as U,f as ue,jt as W,qc as G,ur as K}from"./table-BMPR7vqs.js";var de=m(`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`}]]);function q(e){switch(e){case f.Annual:return`Annual`;case f.Monthly:return`Monthly`;default:throw Error(`Unsupported Billing Cycle: ${e}`)}}function J(e){switch(e){case r.Enterprise:return`Enterprise`;case r.Pro:return`Pro`;default:throw Error(`Unsupported Subscription plan: ${e}`)}}var Y=e(n(),1),X=`createContract`,fe=()=>{let[{fetching:e},t]=s(l);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}`;d.loading(`Creating contract`,{id:r});try{let i=await t(e);p(i,n,r)&&d.success(`Success`,{id:r,description:`Contract for client ${e.input.clientId} was successfully created`})}catch(e){console.error(`${n}: ${e}`),d.error(`Error`,{id:r,description:n,duration:1e5,closeButton:!0})}},[t])}},Z=`updateContract`,pe=()=>{let[{fetching:e},t]=s(u);return{updating:e,updateContract:(0,Y.useCallback)(async e=>{let n=`Error updating contract ID [${e.contractId}]`,r=`${Z}-${e.contractId}`;d.loading(`Updating contract`,{id:r});try{let i=await t(e),a=p(i,n,r,`updateContract`);if(a)return d.success(`Success`,{id:r,description:`Contract updated`}),a.updateContract}catch(e){console.error(`${n}: ${e}`),d.error(`Error`,{id:r,description:n,duration:1e5,closeButton:!0})}},[t])}},Q=t(),me=L({id:U().optional(),startDate:j(`Start date is required`),endDate:j(`End date is required`),pos:R(H()),paymentAmount:F().min(0,`Payment amount must be non-negative`),paymentCurrency:W(Object.values(o),`Currency is required`),productType:W(Object.values(a)).optional(),msCloudLink:K().optional().or(A(``)),billingCycle:W(Object.values(f)).optional(),subscriptionPlan:W(Object.values(r)).optional(),isActive:P(),defaultRemark:H().optional(),defaultDocumentType:W(Object.values(c)),operationsLimit:B().min(BigInt(0),`Operations limit must be non-negative`)}),he={startDate:``,endDate:``,pos:[],paymentAmount:0,paymentCurrency:o.Usd,productType:a.Hive,msCloudLink:void 0,billingCycle:f.Monthly,subscriptionPlan:void 0,isActive:!0,defaultRemark:void 0,defaultDocumentType:c.Proforma,operationsLimit:BigInt(0)};function $({clientId:e,contract:t,contractId:n,onDone:a,duplicate:s}){let{isEditing:l,isDuplicating:u}=(0,Y.useMemo)(()=>{let e=!!t||!!n,r=s===!0&&e;return{isEditing:e&&!r,isDuplicating:r}},[t,n,s]),[d,p]=(0,Y.useState)(!1),[m,A]=(0,Y.useState)(!1),[j,P]=(0,Y.useState)(he),[F,L]=(0,Y.useState)(``),{updateContract:R,updating:B}=pe(),{createContract:H,creating:U}=fe(),[{data:W,fetching:K}]=ee({query:i,pause:!n||!d,variables:{contractId:n??``}}),X=le({resolver:ue(me),defaultValues:j}),Z=()=>{let e=F.trim();if(e){let t=X.getValues(`pos`);t.includes(e)||X.setValue(`pos`,[...t,e],{shouldDirty:!0}),L(``)}},$=e=>{let t=X.getValues(`pos`);X.setValue(`pos`,t.filter((t,n)=>n!==e),{shouldDirty:!0})};(0,Y.useEffect)(()=>{t&&P(t)},[t]),(0,Y.useEffect)(()=>{if(W?.contractsById){let e=W.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)};P({id:e.id,...t})}},[W]);let ge=(0,Y.useMemo)(()=>j?JSON.stringify(j):null,[j]);(0,Y.useEffect)(()=>{j&&X.reset({startDate:j.startDate,endDate:j.endDate,pos:j.pos,paymentAmount:j.paymentAmount,paymentCurrency:j.paymentCurrency,productType:j.productType,msCloudLink:j.msCloudLink,billingCycle:j.billingCycle,subscriptionPlan:j.subscriptionPlan,isActive:j.isActive,defaultRemark:j.defaultRemark,defaultDocumentType:j.defaultDocumentType,operationsLimit:BigInt(j.operationsLimit)})},[ge,X.reset]);let _e=(0,Y.useCallback)(async t=>{if(u||l){if(!j?.id)return;if(u){if(!t.billingCycle){X.setError(`billingCycle`,{message:`Billing cycle is required`});return}await H({input:{clientId:e,amount:{raw:t.paymentAmount,currency:t.paymentCurrency},billingCycle:t.billingCycle,documentType:t.defaultDocumentType,endDate:z(new Date(t.endDate),`yyyy-MM-dd`),isActive:t.isActive,msCloud:t.msCloudLink||null,plan:t.subscriptionPlan,product:t.productType,purchaseOrders:t.pos,remarks:t.defaultRemark,startDate:z(new Date(t.startDate),`yyyy-MM-dd`),operationsLimit:t.operationsLimit,deactivateContracts:m?[j.id]:void 0}})}else await R({contractId:j.id,input:{amount:{raw:t.paymentAmount,currency:t.paymentCurrency},billingCycle:t.billingCycle,documentType:t.defaultDocumentType,endDate:z(new Date(t.endDate),`yyyy-MM-dd`),isActive:t.isActive,msCloud:t.msCloudLink||null,plan:t.subscriptionPlan,product:t.productType,purchaseOrders:t.pos,remarks:t.defaultRemark,startDate:z(new Date(t.startDate),`yyyy-MM-dd`),operationsLimit:t.operationsLimit}})}else{if(!t.billingCycle){X.setError(`billingCycle`,{message:`Billing cycle is required`});return}await H({input:{clientId:e,amount:{raw:t.paymentAmount,currency:t.paymentCurrency},billingCycle:t.billingCycle,documentType:t.defaultDocumentType,endDate:z(new Date(t.endDate),`yyyy-MM-dd`),isActive:t.isActive,msCloud:t.msCloudLink||null,plan:t.subscriptionPlan,product:t.productType,purchaseOrders:t.pos,remarks:t.defaultRemark,startDate:z(new Date(t.startDate),`yyyy-MM-dd`),operationsLimit:t.operationsLimit}})}p(!1),a?.()},[j,a,H,R,e,X,l,u,m]);return(0,Q.jsxs)(ne,{open:d,onOpenChange:p,children:[(0,Q.jsx)(y,{asChild:!0,children:l?(0,Q.jsx)(O,{variant:`ghost`,size:`sm`,onClick:()=>p(!0),children:(0,Q.jsx)(te,{className:`size-5`})}):u?(0,Q.jsx)(O,{variant:`ghost`,size:`sm`,onClick:()=>p(!0),children:(0,Q.jsx)(de,{className:`size-5`})}):(0,Q.jsxs)(O,{size:`sm`,onClick:()=>p(!0),children:[(0,Q.jsx)(E,{className:`size-4 mr-2`}),`New Contract`]})}),(0,Q.jsxs)(ae,{className:`max-w-2xl max-h-[90vh] overflow-y-auto`,children:[(0,Q.jsxs)(ie,{children:[(0,Q.jsx)(x,{children:l?`Edit Contract`:`Create New Contract`}),(0,Q.jsx)(T,{children:l?`Update contract details`:u?`Create a new contract based on an existing one`:`Add a new contract with all required details`})]}),K?(0,Q.jsx)(Q.Fragment,{children:`Fetching contract details...`}):(0,Q.jsx)(ce,{...X,children:(0,Q.jsxs)(`form`,{onSubmit:X.handleSubmit(_e),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)(V,{control:X.control,name:`operationsLimit`,render:({field:e})=>(0,Q.jsxs)(M,{children:[(0,Q.jsx)(I,{children:`Operations Limit`}),(0,Q.jsx)(N,{children:(0,Q.jsx)(C,{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)(G,{})]})}),(0,Q.jsx)(V,{control:X.control,name:`pos`,render:({field:e})=>(0,Q.jsxs)(M,{children:[(0,Q.jsx)(I,{children:`Purchase Orders`}),(0,Q.jsxs)(`div`,{className:`flex gap-2`,children:[(0,Q.jsx)(C,{placeholder:`Add PO...`,value:F,onChange:e=>L(e.target.value),onKeyDown:e=>{e.key===`Enter`&&(e.preventDefault(),Z())}}),(0,Q.jsx)(O,{type:`button`,size:`sm`,onClick:Z,children:(0,Q.jsx)(E,{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)(k,{variant:`secondary`,className:`gap-1 max-w-xs truncate`,children:[t,n===(e.value?.length??0)-1&&(0,Q.jsx)(O,{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)(G,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-2`,children:[(0,Q.jsx)(V,{control:X.control,name:`startDate`,render:({field:{onChange:e,...t}})=>(0,Q.jsxs)(M,{children:[(0,Q.jsx)(I,{children:`Start Date`}),(0,Q.jsx)(N,{children:(0,Q.jsx)(C,{type:`date`,...t,onInput:t=>e(t)})}),(0,Q.jsx)(G,{})]})}),(0,Q.jsx)(V,{control:X.control,name:`endDate`,render:({field:{onChange:e,...t}})=>(0,Q.jsxs)(M,{children:[(0,Q.jsx)(I,{children:`End Date`}),(0,Q.jsx)(N,{children:(0,Q.jsx)(C,{type:`date`,...t,onInput:t=>e(t)})}),(0,Q.jsx)(G,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-3`,children:[(0,Q.jsx)(V,{control:X.control,name:`paymentAmount`,render:({field:e})=>(0,Q.jsxs)(M,{className:`md:col-span-2`,children:[(0,Q.jsx)(I,{children:`Payment Amount`}),(0,Q.jsx)(N,{children:(0,Q.jsx)(C,{type:`number`,placeholder:`24000`,...e,onChange:t=>{e.onChange(t?.target.value?Number(t?.target.value):void 0)}})}),(0,Q.jsx)(G,{})]})}),(0,Q.jsx)(V,{control:X.control,name:`paymentCurrency`,render:({field:e})=>(0,Q.jsxs)(M,{children:[(0,Q.jsx)(I,{children:`Currency`}),(0,Q.jsxs)(w,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(N,{children:(0,Q.jsx)(v,{children:(0,Q.jsx)(_,{})})}),(0,Q.jsx)(b,{children:Object.values(o).map(e=>(0,Q.jsx)(S,{value:e,children:e},e))})]}),(0,Q.jsx)(G,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-2`,children:[(0,Q.jsx)(V,{control:X.control,name:`productType`,render:({field:e})=>(0,Q.jsxs)(M,{children:[(0,Q.jsx)(I,{children:`Product Type`}),(0,Q.jsx)(N,{children:(0,Q.jsx)(C,{placeholder:`Cloud Services`,...e})}),(0,Q.jsx)(G,{})]})}),(0,Q.jsx)(V,{control:X.control,name:`billingCycle`,render:({field:e})=>(0,Q.jsxs)(M,{children:[(0,Q.jsx)(I,{children:`Billing Cycle`}),(0,Q.jsxs)(w,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(N,{children:(0,Q.jsx)(v,{children:(0,Q.jsx)(_,{})})}),(0,Q.jsx)(b,{children:Object.values(f).map(e=>(0,Q.jsx)(S,{value:e,children:q(e)},e))})]}),(0,Q.jsx)(G,{})]})})]}),(0,Q.jsxs)(`div`,{className:`grid gap-4 md:grid-cols-2`,children:[(0,Q.jsx)(V,{control:X.control,name:`subscriptionPlan`,render:({field:e})=>(0,Q.jsxs)(M,{children:[(0,Q.jsx)(I,{children:`Subscription Plan`}),(0,Q.jsx)(N,{children:(0,Q.jsxs)(w,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(N,{children:(0,Q.jsx)(v,{children:(0,Q.jsx)(_,{})})}),(0,Q.jsx)(b,{children:Object.values(r).map(e=>(0,Q.jsx)(S,{value:e,children:J(e)},e))})]})}),(0,Q.jsx)(G,{})]})}),(0,Q.jsx)(V,{control:X.control,name:`isActive`,render:({field:e})=>(0,Q.jsxs)(M,{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)(I,{className:`text-base`,children:`Active Status`})}),(0,Q.jsx)(N,{children:(0,Q.jsx)(g,{checked:e.value,onCheckedChange:e.onChange})})]})})]}),(0,Q.jsx)(V,{control:X.control,name:`msCloudLink`,render:({field:e})=>(0,Q.jsxs)(M,{children:[(0,Q.jsx)(I,{children:`MS Cloud Link`}),(0,Q.jsx)(N,{children:(0,Q.jsx)(C,{type:`url`,placeholder:`https://portal.azure.com/contract-id`,...e})}),(0,Q.jsx)(G,{})]})}),(0,Q.jsx)(V,{control:X.control,name:`defaultDocumentType`,render:({field:e})=>(0,Q.jsxs)(M,{children:[(0,Q.jsx)(I,{children:`Default Document Type`}),(0,Q.jsxs)(w,{onValueChange:e.onChange,defaultValue:e.value,children:[(0,Q.jsx)(N,{children:(0,Q.jsx)(v,{children:(0,Q.jsx)(_,{})})}),(0,Q.jsx)(b,{children:Object.values(c).map(e=>(0,Q.jsx)(S,{value:e,children:h(e)},e))})]}),(0,Q.jsx)(G,{})]})}),(0,Q.jsx)(V,{control:X.control,name:`defaultRemark`,render:({field:e})=>(0,Q.jsxs)(M,{children:[(0,Q.jsx)(I,{children:`Default Remark`}),(0,Q.jsx)(N,{children:(0,Q.jsx)(D,{placeholder:`Enter default remark for this contract...`,rows:3,...e})}),(0,Q.jsx)(G,{})]})}),u&&(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)(se,{className:`text-base`,children:`Deactivate Original Contract`})}),(0,Q.jsx)(g,{checked:m,onCheckedChange:A})]})]}),(0,Q.jsxs)(re,{children:[(0,Q.jsx)(O,{type:`button`,variant:`outline`,onClick:()=>p(!1),children:`Cancel`}),(0,Q.jsx)(O,{type:`submit`,disabled:U||B,children:l?`Update Contract`:`Create Contract`})]})]})})]})]})}export{q as n,J as r,$ as t};