UNPKG

@accounter/client

Version:
1 lines 4.72 kB
import{s as e}from"./dist-BBBZL-zT.js";import{i as t,o as n}from"./utils-C9b7NsUD.js";import{B as r,Ga as i,So as a,oa as o,r as s,t as c,xo as l}from"./error-handling-DIXN-FX1.js";import{c as u,d,dt as f,f as p,p as m,s as h}from"./select-D5MmcncR.js";import{C as g,Gt as _,Ir as v,Mt as y,Wt as b,w as x}from"./common-vBU3G9KD.js";import{t as S}from"./arrow-up-down-Bj9Vaefk.js";import{t as C}from"./button-DATCl8sW.js";import{a as w,c as T,i as E,m as D,n as O,o as k,p as A,r as j,s as M,t as N,u as P}from"./table-69kLt-n3.js";import{x as F}from"./index-hp82RaKL.js";import{t as I}from"./page-layout-UGQ6tzsv.js";var L=e(n(),1),R=`updateTaxCategory`,z=()=>{let[{fetching:e},t]=l(i);return{fetching:e,updateTaxCategory:(0,L.useCallback)(async e=>{let n=`Error updating tax category`,r=`${R}-${e.taxCategoryId}`;s.loading(`Uploading Tax Category`,{id:r});try{let i=c(await t(e),n,r,`updateTaxCategory`);if(i)return s.success(`Success`,{id:r,description:`${i.updateTaxCategory.name} was updated`}),i.updateTaxCategory}catch(e){console.error(`${n}: ${e}`),s.error(`Error`,{id:r,description:n,duration:1e5,closeButton:!0})}},[t])}},B=t();function V({taxCategoryId:e,onAdd:t}){let[n,r]=(0,L.useState)(!1);return(0,B.jsxs)(h,{open:n,onOpenChange:r,children:[(0,B.jsx)(m,{asChild:!0,children:(0,B.jsx)(C,{variant:`outline`,size:`icon`,children:(0,B.jsx)(v,{})})}),(0,B.jsxs)(u,{className:`w-[400px] max-w-screen-md`,children:[(0,B.jsx)(d,{children:(0,B.jsx)(p,{children:`Edit Tax Category`})}),(0,B.jsx)(H,{taxCategoryId:e,close:()=>r(!1),onAdd:t})]})]})}function H({taxCategoryId:e,close:t,onAdd:n}){let[{data:r,fetching:i}]=a({query:o,variables:{id:e}}),c=r?.taxCategory;return!c&&!i&&s.error(`Error`,{description:`Couldn'nt find tax category`}),i?(0,B.jsx)(f,{className:`h-10 w-10 animate-spin`}):(0,B.jsx)(U,{taxCategory:c,close:t,onAdd:n})}function U({taxCategory:e,close:t,onAdd:n}){let r=b({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)(y,{...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)(x,{isInsert:!1,formManager:r,setFetching:o})}),(0,B.jsx)(`div`,{className:`flex justify-end mt-4`,children:(0,B.jsx)(C,{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:e})=>(0,B.jsxs)(C,{variant:`ghost`,onClick:()=>e.toggleSorting(e.getIsSorted()===`asc`),children:[`Name`,(0,B.jsx)(S,{})]})},{id:`sortCode`,accessorKey:`sortCode.key`,cell:({row:e})=>(0,B.jsx)(G,{data:e.original}),header:({column:e})=>(0,B.jsxs)(C,{variant:`ghost`,onClick:()=>e.toggleSorting(e.getIsSorted()===`asc`),children:[`Sort Code`,(0,B.jsx)(S,{})]})},{id:`edit`,cell:({row:e})=>(0,B.jsx)(V,{taxCategoryId:e.original.id})}],q=()=>{let[{data:e,fetching:t,error:n},i]=a({query:r}),{setFiltersContext:o}=(0,L.useContext)(F),c=e?.taxCategories??[],l=T({data:c,columns:K,getCoreRowModel:P(),getPaginationRowModel:A(),getSortedRowModel:D(),initialState:{pagination:{pageSize:30}}});return(0,L.useEffect)(()=>{o((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:l})})}))},[o,l,l.getPageOptions()]),(0,L.useEffect)(()=>{n&&s.error(`Error`,{description:`Error fetching tax categories`})},[n]),(0,B.jsx)(I,{title:`Tax Categories (${c.length})`,description:`All tax categories`,headerActions:(0,B.jsx)(g,{onAdd:()=>i()}),children:t?(0,B.jsx)(`div`,{className:`flex flex-row justify-center`,children:(0,B.jsx)(f,{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)(N,{children:[(0,B.jsx)(w,{children:l.getHeaderGroups().map(e=>(0,B.jsx)(k,{children:e.headers.map(e=>(0,B.jsx)(E,{children:e.isPlaceholder?null:M(e.column.columnDef.header,e.getContext())},e.id))},e.id))}),(0,B.jsx)(O,{children:l.getRowModel().rows.length===0?(0,B.jsx)(k,{children:(0,B.jsx)(j,{colSpan:K.length,className:`text-center py-8`,children:`No tax categories found`})}):l.getRowModel().rows.map(e=>(0,B.jsx)(k,{children:e.getVisibleCells().map(e=>(0,B.jsx)(j,{children:M(e.column.columnDef.cell,e.getContext())},e.id))},e.id))})]})})})};export{q as TaxCategories};