UNPKG

@accounter/client

Version:
1 lines 4.71 kB
import{s as e}from"./dist-C51EwTaa.js";import{i as t,o as n}from"./utils-DdVdMk8X.js";import{B as r,Io as i,Lo as a,_a as o,r as s,ro as c,t as l}from"./error-handling-Dpfd9svJ.js";import{c as u,f as d,ft as f,l as p,m,p as h}from"./select-CrsvQyhZ.js";import{t as g}from"./arrow-up-down-BcUE5_dQ.js";import{t as _}from"./button-Dzp2INW0.js";import{P as v,U as y,a as b,c as x,i as S,m as C,n as w,o as T,p as E,r as D,s as O,t as k,u as A}from"./table--bF0wbxq.js";import{Bt as j,G as M,W as N,Xr as P,cn as F}from"./index-CL8-JoMZ.js";import{t as I}from"./page-layout-DoLDsSrs.js";var L=e(n(),1),R=`updateTaxCategory`,z=()=>{let[{fetching:e},t]=i(c);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=l(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)(u,{open:n,onOpenChange:r,children:[(0,B.jsx)(m,{asChild:!0,children:(0,B.jsx)(_,{variant:`outline`,size:`icon`,children:(0,B.jsx)(P,{})})}),(0,B.jsxs)(p,{className:`w-[400px] max-w-screen-md`,children:[(0,B.jsx)(d,{children:(0,B.jsx)(h,{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=y({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)(v,{...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)(M,{isInsert:!1,formManager:r,setFetching:o,ownerId:e.ownerId??void 0})}),(0,B.jsx)(`div`,{className:`flex justify-end mt-4`,children:(0,B.jsx)(_,{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)(_,{variant:`ghost`,onClick:()=>e.toggleSorting(e.getIsSorted()===`asc`),children:[`Name`,(0,B.jsx)(g,{})]})},{id:`sortCode`,accessorKey:`sortCode.key`,cell:({row:e})=>(0,B.jsx)(G,{data:e.original}),header:({column:e})=>(0,B.jsxs)(_,{variant:`ghost`,onClick:()=>e.toggleSorting(e.getIsSorted()===`asc`),children:[`Sort Code`,(0,B.jsx)(g,{})]})},{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=x({data:c,columns:K,getCoreRowModel:A(),getPaginationRowModel:E(),getSortedRowModel:C(),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)(j,{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)(N,{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)(k,{children:[(0,B.jsx)(b,{children:l.getHeaderGroups().map(e=>(0,B.jsx)(T,{children:e.headers.map(e=>(0,B.jsx)(S,{children:e.isPlaceholder?null:O(e.column.columnDef.header,e.getContext())},e.id))},e.id))}),(0,B.jsx)(w,{children:l.getRowModel().rows.length===0?(0,B.jsx)(T,{children:(0,B.jsx)(D,{colSpan:K.length,className:`text-center py-8`,children:`No tax categories found`})}):l.getRowModel().rows.map(e=>(0,B.jsx)(T,{children:e.getVisibleCells().map(e=>(0,B.jsx)(D,{children:O(e.column.columnDef.cell,e.getContext())},e.id))},e.id))})]})})})};export{q as TaxCategories};