UNPKG

@accounter/client

Version:
1 lines 4.72 kB
import{s as e}from"./dist-BbgEzppd.js";import{i as t,o as n}from"./utils-BUNz070n.js";import{B as r,Da as i,Hi as a,Ja as o,Ya as s,r as c,t as l}from"./error-handling-AQOG1RNy.js";import{c as u,d,dt as f,f as p,p as m,s as h}from"./select-6mBYg_R6.js";import{At as g,E as _,Ht as v,Mr as y,T as b,Vt as x}from"./common-Cy-x5Vhe.js";import{t as S}from"./arrow-up-down-C39PJhum.js";import{t as C}from"./button-CBTN3XMQ.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-tK8pMUMS.js";import{w as F}from"./index-B8E5XsZe.js";import{t as I}from"./page-layout-BO3YzLcj.js";var L=e(n(),1),R=`updateTaxCategory`,z=()=>{let[{fetching:e},t]=o(i);return{fetching:e,updateTaxCategory:(0,L.useCallback)(async e=>{let n=`Error updating tax category`,r=`${R}-${e.taxCategoryId}`;c.loading(`Uploading Tax Category`,{id:r});try{let i=l(await t(e),n,r,`updateTaxCategory`);if(i)return c.success(`Success`,{id:r,description:`${i.updateTaxCategory.name} was updated`}),i.updateTaxCategory}catch(e){console.error(`${n}: ${e}`),c.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)(y,{})})}),(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}]=s({query:a,variables:{id:e}}),o=r?.taxCategory;return!o&&!i&&c.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:o,close:t,onAdd:n})}function U({taxCategory:e,close:t,onAdd:n}){let r=x({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)(g,{...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)(_,{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]=s({query:r}),{setFiltersContext:a}=(0,L.useContext)(F),o=e?.taxCategories??[],l=T({data:o,columns:K,getCoreRowModel:P(),getPaginationRowModel:A(),getSortedRowModel:D(),initialState:{pagination:{pageSize:30}}});return(0,L.useEffect)(()=>{a((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)(v,{table:l})})}))},[a,l,l.getPageOptions()]),(0,L.useEffect)(()=>{n&&c.error(`Error`,{description:`Error fetching tax categories`})},[n]),(0,B.jsx)(I,{title:`Tax Categories (${o.length})`,description:`All tax categories`,headerActions:(0,B.jsx)(b,{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};