PageSourceSearch

https://the-gazel.lovable.app/assets/useDeductions-CgaSxkdv.js

js the-gazel.lovable.app collected 2026-10-03 03:54:03 UTC 9,922 bytes, 1 lines download raw bytes

1import{r as i}from"./vendor-CbTYwD9p.js";import{u as S,d as v,s as E}from"./index-D4z10nuM.js";function F(){const{user:e}=S(),{toast:s}=v(),[r,l]=i.useState([]),[c,p]=i.useState(!0),[g,a]=i.useState(null),[o,d]=i.useState(null),h=i.useCallback(async()=>{console.log("fetchDeductions called with user:",e==null?void 0:e.id);try{if(p(!0),a(null),!e)return console.log("No user found, skipping deductions fetch"),l([]),p(!1),[];console.log("Starting deduction fetch for user:",e.id);const{data:n,error:D}=await E.from("deductions").select("*").eq("user_id",e.id).order("created_at",{ascending:!1});if(D)throw D;console.log(`Fetched ${(n==null?void 0:n.length)||0} deductions successfully:`,n);const y=(n||[]).map(f=>({id:f.id,user_id:f.user_id,name:f.name,amount:typeof f.amount=="number"?f.amount:parseFloat(String(f.amount)),category:f.category,status:f.status,confidence:f.confidence||null,receipt_url:f.receipt_url||null,date:f.date||void 0,source:f.source||void 0,created_at:f.created_at,updated_at:f.updated_at}));return console.log("Processed deductions:",y.map(f=>({id:f.id,name:f.name,amount:f.amount,status:f.status,amountType:typeof f.amount}))),l(y),d(new Date),y}catch(n){const D=n instanceof Error?n:new Error(String(n));return console.error("Error in fetchDeductions:",D),a(D),e&&s({title:"Error fetching deductions",description:D.message,variant:"destructive"}),[]}finally{p(!1)}},[e,s]);return i.useEffect(()=>{let n=!0;return(async()=>{try{console.log("useFetchDeductions effect triggered, user:",e==null?void 0:e.id);const y=await h();n&&console.log("Effect completed, deductions count:",y==null?void 0:y.length)}catch(y){console.error("Error in useEffect deductions fetch:",y)}})(),()=>{n=!1}},[h]),{deductions:r,isLoading:c,error:g,fetchDeductions:h,setDeductions:l,lastFetchTime:o}}function I(e){return i.useMemo(()=>{if(!Array.isArray(e)||e.length===0)return console.log("No deductions available to calculate totals"),{total:0};const r=e.reduce((l,c)=>(!c||typeof c.amount!="number"||!c.category||(l[c.category]||(l[c.category]=0),l[c.category]+=c.amount,l.total+=c.amount),l),{total:0});return console.log("Category totals calculated:",r),r},[e])}function L({fetchDeductions:e}){const{user:s}=S(),{toast:r}=v(),[l,c]=i.useState(!1),[p,g]=i.useState(null);return{addDeduction:i.useCallback(async o=>{console.log("Adding deduction:",o);try{if(c(!0),g(null),!s)throw new Error("User must be logged in to add deductions");if(!["verified","pending","rejected"].includes(o.status))throw new Error(`Invalid status: ${o.status}`);const{data:d,error:h}=await E.from("deductions").insert({user_id:s.id,name:o.name,amount:o.amount,category:o.category,status:o.status,confidence:o.confidence||null,receipt_url:o.receipt_url||null,date:o.date||new Date().toISOString().split("T")[0],source:o.source||"Manual Entry"}).select().single();if(h)throw h;return console.log("Added deduction successfully:",d),{data:d,error:null}}catch(d){const h=d instanceof Error?d:new Error(String(d));return console.error("Error adding deduction:",h),g(h),{data:null,error:d}}finally{c(!1)}},[s,r]),isLoading:l,error:p}}function O({deductions:e,setDeductions:s}){const{toast:r}=v();return{updateDeductionStatus:i.useCallback(async(c,p)=>{if(!Array.isArray(e))return console.error("Deductions is not an array",e),!1;try{console.log(`Updating deduction ${c} status to ${p}`);const g=e.map(o=>o.id===c?{...o,status:p,updated_at:new Date().toISOString()}:o);s(g);const{error:a}=await E.from("deductions").update({status:p,updated_at:new Date().toISOString()}).eq("id",c);return a?(console.error("Supabase error updating status:",a),s(e),r({title:"Error updating status",description:a.message,variant:"destructive"}),!1):!0}catch(g){return console.error("Error updating deduction status:",g),s(e),r({title:"Error updating status",description:"Could not update the deduction status",variant:"destructive"}),!1}},[e,s,r])}}function q({fetchDeductions:e}){const{user:s}=S();v();const[r,l]=i.useState(!1),[c,p]=i.useState(null);return{editDeduction:i.useCallback(async a=>{console.log("Editing deduction:",a);try{if(l(!0),p(null),!s)throw new Error("User not authenticated");if(!["verified","pending","rejected"].includes(a.status))throw new Error(`Invalid status: ${a.status}`);const{error:o}=await E.from("deductions").update({name:a.name,amount:a.amount,category:a.category,status:a.status,confidence:a.confidence||null,receipt_url:a.receipt_url||null,date:a.date||null,source:a.source||null,updated_at:new Date().toISOString()}).eq("id",a.id).eq("user_id",s.id);if(o)throw o;return setTimeout(()=>{e().catch(console.error)},500),{success:!0}}catch(o){const d=o instanceof Error?o:new Error(String(o));return console.error("Error in editDeduction:",d),p(d),{success:!1,message:d.message}}finally{l(!1)}},[s,e]),isLoading:r,error:c}}function P({fetchDeductions:e}){const{user:s}=S();v();const[r,l]=i.useState(!1),[c,p]=i.useState(null);return{removeDeduction:i.useCallback(async a=>{console.log("Removing deduction:",a);try{if(l(!0),p(null),!s)throw new Error("User not authenticated");const{error:o}=await E.from("deductions").delete().eq("id",a).eq("user_id",s.id);if(o)throw o;return setTimeout(()=>{e().catch(console.error)},500),{success:!0}}catch(o){const d=o instanceof Error?o:new Error(String(o));return console.error("Error in removeDeduction:",d),p(d),{success:!1,message:d.message}}finally{l(!1)}},[s,e]),isLoading:r,error:c}}function M(){const{toast:e}=v(),{user:s}=S(),{deductions:r,isLoading:l,error:c,fetchDeductions:p,setDeductions:g}=F(),[a,o]=i.useState(!1),d=Array.isArray(r)?r:[],h=I(d),n=i.useCallback(async()=>{console.log("Manual refresh triggered in useDeductions");try{const t=await p();return console.log("Manual refresh completed with results:",(t==null?void 0:t.length)||0),t}catch(t){return console.error("Error in manual refresh:",t),e({title:"Refresh failed",description:"Could not refresh deductions. Please try again.",variant:"destructive"}),[]}},[p,e]),{addDeduction:D}=L({fetchDeductions:n}),{editDeduction:y}=q({fetchDeductions:n}),{removeDeduction:f}=P({fetchDeductions:n}),b=i.useCallback(async t=>{try{if(console.log("Adding deduction in useDeductions wrapper:",t),Array.isArray(r)){const m={id:`temp-${Date.now()}`,user_id:"current-user",created_at:new Date().toISOString(),updated_at:new Date().toISOString(),...t};g([m,...r])}const u=await D(t);return e({title:"Deduction added",description:`Added ${t.name} for $${typeof t.amount=="number"?t.amount.toFixed(2):t.amount}`}),setTimeout(()=>{n().catch(console.error)},500),u}catch(u){throw console.error("Error in addDeduction wrapper:",u),e({title:"Error adding deduction",description:"Please try again later",variant:"destructive"}),n().catch(console.error),u}},[D,n,e,r,g]),A=i.useCallback(async t=>{try{if(console.log("Editing deduction in useDeductions wrapper:",t),Array.isArray(r)){const m=r.map(w=>w.id===t.id?{...w,...t,updated_at:new Date().toISOString()}:w);g(m)}const u=await y(t);if(u.success)e({title:"Deduction updated",description:`Updated ${t.name} for $${typeof t.amount=="number"?t.amount.toFixed(2):t.amount}`});else throw new Error(u.message||"Failed to update deduction");return setTimeout(()=>{n().catch(console.error)},500),u}catch(u){throw console.error("Error in editDeduction wrapper:",u),e({title:"Error updating deduction",description:"Please try again later",variant:"destructive"}),n().catch(console.error),u}},[y,n,e,r,g]),_=i.useCallback(async t=>{try{console.log("Removing deduction in useDeductions wrapper:",t);const u=r==null?void 0:r.find(w=>w.id===t);if(Array.isArray(r)){const w=r.filter(k=>k.id!==t);g(w)}const m=await f(t);if(m.success)e({title:"Deduction removed",description:u?`Removed ${u.name}`:"Deduction has been removed"});else throw new Error(m.message||"Failed to remove deduction");return setTimeout(()=>{n().catch(console.error)},500),m}catch(u){throw console.error("Error in removeDeduction wrapper:",u),e({title:"Error removing deduction",description:"Please try again later",variant:"destructive"}),n().catch(console.error),u}},[f,n,e,r,g]),{updateDeductionStatus:C}=O({deductions:d,setDeductions:g});i.useEffect(()=>{let t=!0;return(async()=>{console.log("Initial deductions fetch starting in useDeductions");try{const m=await n();t&&(console.log("Initial deductions fetch completed:",(m==null?void 0:m.length)||0,"deductions"),o(!0))}catch(m){console.error("Error in initial deductions fetch:",m),t&&o(!0)}})(),()=>{t=!1}},[n]),i.useEffect(()=>{console.log("useDeductions current state:",{deductionsCount:d.length,deductionsPresent:Array.isArray(r),isLoading:l,error:c?c.message:null,initialFetchDone:a,categoryTotalsTotal:(
1h==null?void 0:h.total)||0})},[r,d,l,c,a,h]);const T=i.useCallback(async t=>{if(!(!s||t.length===0))try{if(Array.isArray(r)){const m=new Set(t);g(r.filter(w=>!m.has(w.id)))}const{error:u}=await E.from("deductions").delete().in("id",t).eq("user_id",s.id);if(u)throw u;e({title:"Deductions deleted",description:`Successfully deleted ${t.length} deduction(s).`}),setTimeout(()=>{n().catch(console.error)},500)}catch(u){throw console.error("Error bulk deleting deductions:",u),e({title:"Error deleting deductions",description:"Please try again later",variant:"destructive"}),n().catch(console.error),u}},[s,r,g,n,e]),$=i.useCallback(async()=>{if(s)try{const{error:t}=await E.from("deductions").delete().eq("user_id",s.id);if(t)throw t;g([]),e({title:"All deductions cleared",description:"All your deductions have been removed."}),await n()}catch(t){console.error("Error clearing all deductions:",t),e({title:"Error clearing deductions",description:"Please try again later",variant:"destructive"}),n().catch(console.error)}},[s,g,n,e]);return{deductions:d,isLoading:l,error:c,categoryTotals:h,fetchDeductions:n,addDeduction:b,editDeduction:A,removeDeduction:_,deleteDeductionsByIds:T,clearAllDeductions:$,updateDeductionStatus:C,initialFetchDone:a}}export{O as a,M as u};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.