PageSourceSearch

https://blank-canvas-creation-5570.lovable.app/assets/AdminProductDiscountsPage-2215e502.js

js blank-canvas-creation-5570.lovable.app collected 2026-10-03 03:25:08 UTC 11,576 bytes, 5 lines download raw bytes

1import{r as m,j as e}from"./vendor-react-a835ee61.js";import{u as M,e as G,H as J,j as X,A as Q,B as S,q as W,h as v}from"./index-fad5f9b7.js";import{C as k}from"./checkbox-f40e4dc7.js";import{C as Y,a as Z,b as ee}from"./collapsible-15b0b3a5.js";import{S as te,a as se,b as re,c as ae,d as ne}from"./select-8ceb069b.js";import{B as ce}from"./badge-ff88c681.js";import{e as oe,X as ie,L as O,aJ as le,aK as de,A as ue,aG as me}from"./vendor-icons-11b98caa.js";import"./vendor-supabase-722fbc27.js";import"./vendor-radix-core-99f1042f.js";import"./vendor-radix-primitive-4e369f64.js";import"./vendor-radix-dropdown-menu-86d2342e.js";import"./vendor-radix-menu-7fb79491.js";import"./vendor-radix-checkbox-dd292507.js";import"./vendor-radix-collapsible-dfe8d6f4.js";import"./vendor-radix-select-2329efca.js";import"./vendor-radix-number-d203b311.js";import"./vendor-radix-visually-hidden-4f55a9a1.js";const De=()=>{const{toast:f}=M(),[x,P]=m.useState([]),[j,L]=m.useState([]),[U,E]=m.useState(!0),[g,D]=m.useState(""),[T,_]=m.useState({}),{formatPrice:b}=G(),[h,y]=m.useState({}),[C,z]=m.useState(!1);m.useEffect(()=>{(async()=>{E(!0);const{data:a,error:s}=await v.from("country_pricing").select("*").order("country_code");s?console.error("Error fetching countries:",s):L(a||[]);const{data:t,error:n}=await v.from("products").select(`
2                    *,
3                    product_categories (id, name),
4                    product_images (id, image_url, sort_order)
5                `).order("sort_key",{ascending:!0}).order("name");if(n)console.error("Error fetching products:",n),f({variant:"destructive",title:"Error",description:"Failed to load products"});else{const c=(t||[]).map(o=>{var l,u;return{...o,thumbnail:(u=(l=o.product_images)==null?void 0:l.sort((d,N)=>(d.sort_order||0)-(N.sort_order||0))[0])==null?void 0:u.image_url,discount_percent:o.discount_percent||0,discount_countries:o.discount_countries||[]}});P(c);const i={};c.forEach(o=>{var u;const l=((u=o.product_categories)==null?void 0:u.id)||"uncategorized";l in i||(i[l]=!1)}),_(i)}E(!1)})()},[f]),m.useEffect(()=>{if(g.trim().length>0){const r={...T};let a=!1;const s=g.toLowerCase();x.forEach(t=>{var c;if(t.name.toLowerCase().includes(s)||t.sku&&t.sku.toLowerCase().includes(s)){const i=((c=t.product_categories)==null?void 0:c.id)||"uncategorized";r[i]||(r[i]=!0,a=!0)}}),a&&_(r)}},[g,x]);const B=r=>{_(a=>({...a,[r]:!a[r]}))},A=m.useMemo(()=>{const r=g.toLowerCase(),s=x.filter(t=>g?t.name.toLowerCase().includes(r)||t.sku&&t.sku.toLowerCase().includes(r):!0).reduce((t,n)=>{var o,l;const c=((o=n.product_categories)==null?void 0:o.id)||"uncategorized",i=((l=n.product_categories)==null?void 0:l.name)||"Uncategorized";return t[c]||(t[c]={id:c,name:i,products:[]}),t[c].products.push(n),t},{});return Object.values(s).forEach(t=>{t.products.sort((n,c)=>n.name.localeCompare(c.name))}),Object.values(s).sort((t,n)=>t.id==="uncategorized"?1:n.id==="uncategorized"?-1:t.name.localeCompare(n.name))},[x,g]),p=m.useCallback((r,a)=>{if(h[r]&&h[r][a]!==void 0)return h[r][a];const s=x.find(t=>t.id===r);return s?s[a]:a==="discount_countries"?[]:0},[h,x]),w=(r,a,s)=>{y(t=>({...t,[r]:{...t[r],[a]:s}}))},V=(r,a)=>{w(r,"discount_percent",parseInt(a))},$=(r,a)=>{const s=p(r,"discount_countries"),t=Array.isArray(s)?[...s]:[];let n;t.includes(a)?n=t.filter(c=>c!==a):n=[...t,a],w(r,"discount_countries",n)},R=(r,a)=>{const t=!r.every(c=>(p(c.id,"discount_countries")||[]).includes(a)),n={...h};r.forEach(c=>{const i=p(c.id,"discount_countries"),o=Array.isArray(i)?[...i]:[];let l;t?l=o.includes(a)?o:[...o,a]:l=o.filter(u=>u!==a),n[c.id]||(n[c.id]={}),n[c.id].discount_countries=l}),y(n)},H=m.useCallback((r,a)=>{if(!r||r.length===0)return!1;const s=r.filter(t=>(p(t.id,"discount_countries")||[]).includes(a)).length;return s===0?!1:s===r.length?!0:"indeterminate"},[p]),I=async()=>{const r=Object.keys(h);if(r.length===0){f({title:"No changes to save"});return}z(!0);let a=0,s=0;for(const t of r){const n=h[t],c=x.find(d=>d.id===parseInt(t));if(!c)continue;const i=n.discount_percent!==void 0?n.discount_percent:c.discount_percent,o=n.discount_countries!==void 0?n.discount_countries:c.discount_countries||[],l={discount_percent:i,discount_countries:o},{error:u}=await v.from("products").update(l).eq("id",t);u?(console.error(`Error updating product ${t}:`,u),s++):a++}if(z(!1),s===0){f({variant:"success",title:"Saved Successfully",description:`Updated ${a} products.`}),y({});const{data:t}=await v.from("products").select("*, product_categories(id, name), product_images(id, image_url, sort_order)").order("sort_key",{ascending:!0}).order("name");if(t){const n=t.map(c=>{var i,o;return{...c,thumbnail:(o=(i=c.product_images)==null?void 0:i.sort((l,u)=>(l.sort_order||0)-(u.sort_order||0))[0])==null?void 0:o.image_url,discount_percent:c.discount_percent||0,discount_countries:c.discount_countries||[]}});P(n)}}else f({variant:"destructive",title:"Save Completed with Errors",description:`Success: ${a}, Failed: ${s}`})},q=Object.keys(h).length>0,F=[0,5,10,15,20,25,30,35,40,45,50,60];return e.jsx(J,{children:e.jsxs("div",{className:"font-poppins pb-20",children:[" ",e.jsx(X,{children:e.jsx("title",{children:"Product Discounts - Admin"})}),e.jsxs("div",{className:"flex justify-between items-center mb-6 gap-4",children:[e.jsx("h1",{className:"text-3xl font-bold font-heading",children:"Product Discounts"}),e.jsx("div",{className:"flex flex-1 justify-end items-center gap-2",children:e.jsxs("div",{className:"flex-1 max-w-lg relative",children:[e.jsx(Q,{placeholder:"Search products...",value:g,onChange:r=>D(r.target.value),className:"pl-10 pr-10"}),e.jsx(oe,{className:"absolute left-3 top-1/2 -translate-y-1/2 h-5 w-5 text-gray-400"}),g&&e.jsx(S,{variant:"ghost",size:"icon",className:"absolute right-1 top-1/2 -translate-y-1/2 h-8 w-8",onClick:()=>D(""),children:e.jsx(ie,{className:"h-5 w-5 text-gray-400"})})]})})]}),e.jsx("div",{className:"bg-white p-6 rounded-[8px] shadow-md",children:U?e.jsxs("div",{className:"flex items-center justify-center p-12 text-gray-500",children:[e.jsx(O,{className:"h-6 w-6 animate-spin mr-2"}),"Loading products..."]}):A.length===0?e.jsx("div",{className:"text-center py-12 text-gray-500",children:"No products found matching your search."}):e.jsx("div",{className:"space-y-2",children:A.map(r=>{const a=r.products;return e.jsxs(Y,{open:T[r.id],onOpenChange:()=>B(r.id),children:[e.jsx(Z,{asChild:!0,children:e.jsxs("button",{className:"flex justify-between items-center w-full p-4 bg-white border border-gray-200 hover:bg-gray-50 rounded-[8px] shadow-sm transition-all mb-2 cursor-pointer",children:[e.jsxs("h2",{className:"text-lg font-bold font-heading text-gray-800",children:[r.name,e.jsx("span",{className:"ml-2 text-sm font-normal text-gray-500 bg-gray-100 px-2 py-0.5 rounded-full",children:a.length})]}),e.jsx(le,{className:"h-5 w-5 text-gray-400"})]})}),e.jsx(ee,{children:e.jsx("div",{className:"overflow-x-auto mb-4 border rounded-lg bg-white",children:e.jsxs("table",{className:"w-full text-left border-collapse",children:[e.jsx("thead",{className:"bg-gray-50 border-b text-xs uppercase text-gray-500 font-medium",children:e.jsxs("tr",{children:[e.jsx("th",{className:"p-3 w-16 text-center",children:"Img"}),e.jsx("th",{className:"p-3 min-w-[200px]",children:"Product Name / SKU"}),e.jsx("th",{className:"p-3 w-40 text-right",children:"Price Breakdown (EUR)"}),e.jsx("th",{className:"p-3 w-32",children:"Discount"}),e.jsx("th",{className:"p-3 w-12 text-center border-l bg-gray-100",children:"All"}),j.map(s=>e.jsx("th",{className:"p-3 w-12 text-center border-l bg-gray-100",children:e.jsxs("div",{className:"flex flex-col items-center gap-1",children:[e.jsx("span",{children:s.country_code}),e.jsx(k,{checked:H(a,s.country_code),onCheckedChange:()=>R(a,s.country_code),className:"data-[state=checked]:bg-blue-600 data-[state=checked]:border-blue-600 cursor-pointer",title:`Toggle all products for ${s.country_code}`})]})},s.id))]})}),e.jsx("tbody",{className:"divide-y",children:a.map(s=>{const t=p(s.id,"discount_percent"),n=p(s.id,"discount_countries")||[],c={...s,discount_percent:t}
5,{originalPrice:i,finalPrice:o,isOnSale:l,discountType:u}=W(c);return e.jsxs("tr",{className:"transition-colors duration-200 hover:bg-blue-50/50",children:[e.jsx("td",{className:"p-3",children:e.jsx("div",{className:"h-10 w-10 bg-gray-100 rounded overflow-hidden mx-auto border",children:s.thumbnail?e.jsx("img",{src:s.thumbnail,alt:s.name,className:"h-full w-full object-cover",loading:"lazy"}):e.jsx("div",{className:"h-full w-full flex items-center justify-center text-gray-300",children:e.jsx(de,{className:"h-4 w-4"})})})}),e.jsxs("td",{className:"p-3",children:[e.jsx("div",{className:"font-semibold text-sm text-gray-900 line-clamp-2",children:s.name}),e.jsx("div",{className:"text-xs text-gray-500 font-mono mt-0.5",children:s.sku||"-"})]}),e.jsx("td",{className:"p-3 text-sm text-right",children:u==="sale_price"?e.jsxs("div",{className:"flex flex-col items-end",children:[e.jsx(ce,{variant:"outline",className:"text-[10px] border-green-200 bg-green-50 text-green-700 mb-1 whitespace-nowrap",children:"OVERRIDES DISCOUNT"}),e.jsx("span",{className:"line-through text-gray-400 text-xs",children:b(i)}),e.jsx("span",{className:"font-bold text-green-600",children:b(o)})]}):l?e.jsxs("div",{className:"flex flex-col items-end",children:[e.jsx("span",{className:"line-through text-gray-400 text-xs",children:b(i)}),e.jsx("span",{className:"font-bold text-red-600",children:b(o)})]}):e.jsx("span",{className:"font-medium text-gray-700",children:b(i)})}),e.jsxs("td",{className:"p-3 relative z-10",children:[e.jsxs(te,{value:String(t),onValueChange:d=>V(s.id,d),children:[e.jsx(se,{className:"h-8 w-24 text-xs cursor-pointer bg-white",children:e.jsx(re,{})}),e.jsx(ae,{children:F.map(d=>e.jsxs(ne,{value:String(d),className:"cursor-pointer",children:[d,"%"]},d))})]}),u==="sale_price"&&e.jsx("div",{className:"absolute top-1 right-2 text-gray-400 pointer-events-none",title:"Manual Sale Price Set",children:e.jsx(ue,{className:"h-4 w-4 text-orange-400/50"})})]}),e.jsx("td",{className:"p-3 text-center border-l bg-gray-50/10",children:e.jsx(k,{checked:j.length>0&&j.every(d=>n.includes(d.country_code)),onCheckedChange:d=>{const N=d?j.map(K=>K.country_code):[];w(s.id,"discount_countries",N)},className:"data-[state=checked]:bg-blue-600 data-[state=checked]:border-blue-600 cursor-pointer",title:"Toggle all countries for this product"})}),j.map(d=>{const N=n.includes(d.country_code);return e.jsx("td",{className:"p-3 text-center border-l bg-gray-50/10",children:e.jsx(k,{checked:N,onCheckedChange:()=>$(s.id,d.country_code),className:"data-[state=checked]:bg-green-600 data-[state=checked]:border-green-600 cursor-pointer"})},d.id)})]},s.id)})})]})})})]},r.id)})})}),q&&e.jsxs("div",{className:"fixed bottom-6 left-1/2 transform -translate-x-1/2 bg-gray-900 text-white px-6 py-3 rounded-full shadow-2xl z-50 flex items-center gap-4 animate-in slide-in-from-bottom-10 border border-gray-700",children:[e.jsxs("span",{className:"text-sm font-medium",children:[Object.keys(h).length," products modified"]}),e.jsx("div",{className:"h-4 w-px bg-gray-700"}),e.jsx(S,{size:"sm",variant:"ghost",className:"text-gray-300 hover:text-white hover:bg-gray-800 h-8",onClick:()=>y({}),disabled:C,children:"Discard"}),e.jsxs(S,{size:"sm",className:"bg-green-600 hover:bg-green-500 text-white h-8 border-0",onClick:I,disabled:C,children:[C?e.jsx(O,{className:"mr-2 h-4 w-4 animate-spin"}):e.jsx(me,{className:"mr-2 h-4 w-4"}),"Save Changes"]})]})]})})};export{De as default};

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.