UNPKG

@accounter/client

Version:
1 lines 4.5 kB
import{t as e}from"./arrow-up-down-Dj4MZ_1J.js";import{$ as t,Al as n,B as r,Bu as i,Cu as a,Da as o,G as s,H as c,Hc as l,Id as u,K as d,Li as f,Ni as p,Pi as m,Q as h,Ri as g,Tn as _,U as v,V as y,W as b,Wl as x,Y as S,Zu as C,_i as w,er as T,fn as E,hl as D,it as O,jd as k,kl as A,q as j,rt as M,vi as N,wn as P,zi as F}from"./index-DUZECcXr.js";import{t as I}from"./page-layout-BfWJC_SV.js";var L=u(k(),1),R=`updateTaxCategory`,z=()=>{let[{fetching:e},t]=A(D);return{fetching:e,updateTaxCategory:(0,L.useCallback)(async e=>{let n=`Error updating tax category`,r=`${R}-${e.taxCategoryId}`;N.loading(`Uploading Tax Category`,{id:r});try{let i=w(await t(e),n,r,`updateTaxCategory`);if(i)return N.success(`Success`,{id:r,description:`${i.updateTaxCategory.name} was updated`}),i.updateTaxCategory}catch(e){console.error(`${n}: ${e}`),N.error(`Error`,{id:r,description:n,duration:1e5,closeButton:!0})}},[t])}},B=a();function V({taxCategoryId:e,onAdd:t}){let[n,r]=(0,L.useState)(!1);return(0,B.jsxs)(p,{open:n,onOpenChange:r,children:[(0,B.jsx)(F,{asChild:!0,children:(0,B.jsx)(x,{variant:`outline`,size:`icon`,children:(0,B.jsx)(i,{})})}),(0,B.jsxs)(m,{className:`w-[400px] max-w-screen-md`,children:[(0,B.jsx)(f,{children:(0,B.jsx)(g,{children:`Edit Tax Category`})}),(0,B.jsx)(H,{taxCategoryId:e,close:()=>r(!1),onAdd:t})]})]})}function H({taxCategoryId:e,close:t,onAdd:r}){let[{data:i,fetching:a}]=n({query:l,variables:{id:e}}),o=i?.taxCategory;return!o&&!a&&N.error(`Error`,{description:`Couldn'nt find tax category`}),a?(0,B.jsx)(C,{className:`h-10 w-10 animate-spin`}):(0,B.jsx)(U,{taxCategory:o,close:t,onAdd:r})}function U({taxCategory:e,close:t,onAdd:n}){let r=P({defaultValues:{sortCode:e.sortCode?.key,name:e.name,irsCode:e.irsCode}}),{handleSubmit:i}=r,[a,o]=(0,L.useState)(!1),{updateTaxCategory:s,fetching:c}=z(),l=r=>{r.sortCode&&=parseInt(r.sortCode.toString()),s({fields:r,taxCategoryId:e.id}).then(e=>{e?.id&&(n?.(e.id),t())})};return(0,B.jsx)(E,{...r,children:(0,B.jsxs)(`form`,{onSubmit:i(l),children:[(0,B.jsx)(`div`,{className:`flex flex-col gap-4 my-4`,children:(0,B.jsx)(O,{isInsert:!1,formManager:r,setFetching:o})}),(0,B.jsx)(`div`,{className:`flex justify-end mt-4`,children:(0,B.jsx)(x,{type:`submit`,disabled:c||a,children:`Update`})})]})})}var W=({data:e})=>(0,B.jsx)(`td`,{children:e.name}),G=({data:e})=>(0,B.jsx)(`td`,{children:e.sortCode?(0,B.jsxs)(`span`,{children:[e.sortCode?.name,` (`,e.sortCode?.id,`)`]}):`N/A`}),K=[{id:`name`,accessorKey:`name`,cell:({row:e})=>(0,B.jsx)(W,{data:e.original}),header:({column:t})=>(0,B.jsxs)(x,{variant:`ghost`,onClick:()=>t.toggleSorting(t.getIsSorted()===`asc`),children:[`Name`,(0,B.jsx)(e,{})]})},{id:`sortCode`,accessorKey:`sortCode.key`,cell:({row:e})=>(0,B.jsx)(G,{data:e.original}),header:({column:t})=>(0,B.jsxs)(x,{variant:`ghost`,onClick:()=>t.toggleSorting(t.getIsSorted()===`asc`),children:[`Sort Code`,(0,B.jsx)(e,{})]})},{id:`edit`,cell:({row:e})=>(0,B.jsx)(V,{taxCategoryId:e.original.id})}],q=()=>{let[{data:e,fetching:i,error:a},l]=n({query:o}),{setFiltersContext:u}=(0,L.useContext)(T),f=e?.taxCategories??[],p=j({data:f,columns:K,getCoreRowModel:S(),getPaginationRowModel:h(),getSortedRowModel:t(),initialState:{pagination:{pageSize:30}}});return(0,L.useEffect)(()=>{u((0,B.jsx)(`div`,{className:`flex items-center justify-end gap-10 space-x-2 py-4`,children:(0,B.jsx)(`div`,{className:`flex items-center justify-between px-2`,children:(0,B.jsx)(_,{table:p})})}))},[u,p,p.getPageOptions()]),(0,L.useEffect)(()=>{a&&N.error(`Error`,{description:`Error fetching tax categories`})},[a]),(0,B.jsx)(I,{title:`Tax Categories (${f.length})`,description:`All tax categories`,headerActions:(0,B.jsx)(M,{onAdd:()=>l()}),children:i?(0,B.jsx)(`div`,{className:`flex flex-row justify-center`,children:(0,B.jsx)(C,{className:`h-10 w-10 animate-spin mr-2`})}):(0,B.jsx)(`div`,{className:`flex flex-col gap-4 rounded-md border`,children:(0,B.jsxs)(r,{children:[(0,B.jsx)(b,{children:p.getHeaderGroups().map(e=>(0,B.jsx)(s,{children:e.headers.map(e=>(0,B.jsx)(v,{children:e.isPlaceholder?null:d(e.column.columnDef.header,e.getContext())},e.id))},e.id))}),(0,B.jsx)(y,{children:p.getRowModel().rows.length===0?(0,B.jsx)(s,{children:(0,B.jsx)(c,{colSpan:K.length,className:`text-center py-8`,children:`No tax categories found`})}):p.getRowModel().rows.map(e=>(0,B.jsx)(s,{children:e.getVisibleCells().map(e=>(0,B.jsx)(c,{children:d(e.column.columnDef.cell,e.getContext())},e.id))},e.id))})]})})})};export{q as TaxCategories};