PageSourceSearch

https://herpowerapp.com/assets/AdminChallenges-BttIq9WI.js

js herpowerapp.com collected 2026-10-09 22:04:07 UTC 12,232 bytes, 4 lines download raw bytes

1import{b as fe,u as pe,K as ve,g as ge,r,s as y,j as e,a2 as ye,B as n,M as Ce,a5 as Se,C as U,y as q,z as R,a as M,L as c,N as C,I as we,X as ee,h as $}from"./index-DO0ZNUzu.js";import{u as Ne}from"./use-mobile-CrCAEjpp.js";import{T as be}from"./textarea-CcV1k9Cd.js";import{S as x,a as j,b as f,c as p,d}from"./select-Tn3h6yZZ.js";import{T as Fe,a as Ae,b as se,c as S,d as Te,e as w}from"./table-CnjIDQIw.js";import{B as Ie}from"./Breadcrumb-Dk4aQzCY.js";import{H as De}from"./house-CUUNLlcb.js";import{U as ke}from"./upload-5tqj8CNI.js";import{P as Ee}from"./plus-BWXN0Mzt.js";import{F as Le}from"./filter-b1rxWdXb.js";import{S as He}from"./search-DfJrq0Pb.js";import{S as te}from"./save-BxkEtuAW.js";import{S as Ve}from"./square-pen-DtA2o_es.js";import{T as _e}from"./trash-2-VtFXnL4x.js";import"./check-Cyw8qn0D.js";const Qe=()=>{const D=fe();pe();const{isAdmin:N,loading:k}=ve();Ne();const{toast:l}=ge(),[v,b]=r.useState([]),[ie,G]=r.useState(!0),[F,E]=r.useState(null),[W,L]=r.useState(!1),[K,O]=r.useState(!1),[A,ae]=r.useState(""),[H,le]=r.useState("all"),[V,re]=r.useState("all"),[T,ne]=r.useState("all"),[i,u]=r.useState({title:"",description:"",difficulty:"easy",points:10,category:"self-care",icon:"Heart",action:""}),_=["easy","medium","hard"],X=["self-care","mindfulness","creativity","social","physical","mental"],ce=["Heart","Sparkles","Star","Crown","Flame","Zap","Sun","Moon"];r.useEffect(()=>{!k&&!N&&(D("/admin/dashboard"),l({title:"Access Denied",description:"You must be an admin to access this page",variant:"destructive"}))},[N,k,D]),r.useEffect(()=>{N&&g()},[N]);const g=async()=>{G(!0);const{data:s,error:t}=await y.from("challenges").select("*").order("created_at",{ascending:!1});!t&&s&&b(s),G(!1)},de=async()=>{if(!i.title.trim()||!i.description.trim()){l({title:"Error",description:"Title and description are required",variant:"destructive"});return}const{error:s}=await y.from("challenges").insert({title:i.title,description:i.description,difficulty:i.difficulty,points:i.points,category:i.category,icon:i.icon,action:i.action||null,is_active:!0});s?l({title:"Error",description:"Failed to add challenge",variant:"destructive"}):(l({title:"Success",description:"Challenge added successfully"}),u({title:"",description:"",difficulty:"easy",points:10,category:"self-care",icon:"Heart",action:""}),L(!1),g())},oe=async s=>{const t=v.find(o=>o.id===s);if(!t)return;const{error:a}=await y.from("challenges").update({title:t.title,description:t.description,difficulty:t.difficulty,points:t.points,category:t.category,icon:t.icon,action:t.action}).eq("id",s);a?l({title:"Error",description:"Failed to update challenge",variant:"destructive"}):(l({title:"Success",description:"Challenge updated successfully"}),E(null),g())},he=async s=>{if(!confirm("Are you sure you want to delete this challenge?"))return;const{error:t}=await y.from("challenges").delete().eq("id",s);t?l({title:"Error",description:"Failed to delete challenge",variant:"destructive"}):(l({title:"Success",description:"Challenge deleted successfully"}),g())},ue=async(s,t)=>{const{error:a}=await y.from("challenges").update({is_active:!t}).eq("id",s);a?l({title:"Error",description:"Failed to update status",variant:"destructive"}):g()},me=()=>{const s=`title,description,difficulty,points,category,icon,action
2"Daily Gratitude Practice","Write down three things you are grateful for",easy,10,mindfulness,Heart,complete
3"Self-Care Sunday","Take an hour for self-care activities",medium,25,self-care,Sparkles,complete`,t=new Blob([s],{type:"text/csv"}),a=window.URL.createObjectURL(t),o=document.createElement("a");o.href=a,o.download="challenges_template.csv",o.click()},xe=async s=>{var o;const t=(o=s.target.files)==null?void 0:o[0];if(!t)return;O(!0);const a=new FileReader;a.onload=async z=>{var Y,Z;try{const P=((Y=z.target)==null?void 0:Y.result).split(`
4`).filter(m=>m.trim()),Be=P[0].split(",").map(m=>m.trim().replace(/"/g,"")),I=[];for(let m=1;m<P.length;m++){const h=(Z=P[m].match(/(".*?"|[^,]+)(?=\s*,|\s*$)/g))==null?void 0:Z.map(je=>je.replace(/^"|"$/g,"").trim());!h||h.length<3||I.push({title:h[0],description:h[1],difficulty:h[2]||"easy",points:parseInt(h[3])||10,category:h[4]||"self-care",icon:h[5]||"Heart",action:h[6]||null,is_active:!0})}if(I.length===0){l({title:"No Data",description:"No valid challenges found in CSV",variant:"destructive"});return}const{error:Q}=await y.from("challenges").insert(I);Q?l({title:"Import Failed",description:Q.message,variant:"destructive"}):(l({title:"Success",description:`Imported ${I.length} challenges`}),g())}catch(J){l({title:"Error",description:J.message,variant:"destructive"})}finally{O(!1),s.target.value=""}},a.readAsText(t)};if(k||ie)return e.jsx(ye,{});if(!N)return null;const B=v.filter(s=>{const t=A===""||s.title.toLowerCase().includes(A.toLowerCase())||s.description.toLowerCase().includes(A.toLowerCase()),a=H==="all"||s.category===H,o=V==="all"||s.difficulty===V,z=T==="all"||T==="active"&&s.is_active||T==="inactive"&&!s.is_active;return t&&a&&o&&z});return e.jsx("div",{className:"min-h-screen bg-gradient-to-b from-background to-secondary/20 p-6",children:e.jsxs("div",{className:"max-w-7xl mx-auto 
4space-y-6",children:[e.jsx(Ie,{items:[{label:"Admin",href:"/admin/dashboard"},{label:"Challenges",href:"/admin/challenges"}],className:"mb-4"}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsx(n,{variant:"outline",size:"icon",onClick:()=>D("/admin/dashboard"),children:e.jsx(De,{className:"h-4 w-4"})}),e.jsxs("div",{children:[e.jsx("h1",{className:"text-3xl font-bold text-foreground",children:"Manage Challenges"}),e.jsx("p",{className:"text-muted-foreground",children:"Add, edit, or remove challenges"})]}),e.jsx(Ce,{})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(n,{variant:"outline",onClick:me,children:[e.jsx(Se,{className:"h-4 w-4 mr-2"}),"Download Template"]}),e.jsx(n,{variant:"outline",disabled:K,asChild:!0,children:e.jsxs("label",{children:[e.jsx(ke,{className:"h-4 w-4 mr-2"}),K?"Importing...":"Import CSV",e.jsx("input",{type:"file",accept:".csv",onChange:xe,className:"hidden"})]})}),e.jsxs(n,{onClick:()=>L(!W),children:[e.jsx(Ee,{className:"h-4 w-4 mr-2"}),"Add Challenge"]})]})]}),e.jsxs(U,{children:[e.jsx(q,{children:e.jsxs(R,{className:"flex items-center gap-2",children:[e.jsx(Le,{className:"h-5 w-5"}),"Search & Filter"]})}),e.jsxs(M,{children:[e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-4 gap-4",children:[e.jsxs("div",{children:[e.jsx(c,{htmlFor:"search",children:"Search"}),e.jsxs("div",{className:"relative",children:[e.jsx(He,{className:"absolute left-3 top-3 h-4 w-4 text-muted-foreground"}),e.jsx(C,{id:"search",placeholder:"Search challenges...",value:A,onChange:s=>ae(s.target.value),className:"pl-9"})]})]}),e.jsxs("div",{children:[e.jsx(c,{htmlFor:"category-filter",children:"Category"}),e.jsxs(x,{value:H,onValueChange:le,children:[e.jsx(j,{id:"category-filter",children:e.jsx(f,{})}),e.jsxs(p,{children:[e.jsx(d,{value:"all",children:"All Categories"}),X.map(s=>e.jsx(d,{value:s,children:s.split("-").map(t=>t.charAt(0).toUpperCase()+t.slice(1)).join(" ")},s))]})]})]}),e.jsxs("div",{children:[e.jsx(c,{htmlFor:"difficulty-filter",children:"Difficulty"}),e.jsxs(x,{value:V,onValueChange:re,children:[e.jsx(j,{id:"difficulty-filter",children:e.jsx(f,{})}),e.jsxs(p,{children:[e.jsx(d,{value:"all",children:"All Difficulties"}),_.map(s=>e.jsx(d,{value:s,children:s.charAt(0).toUpperCase()+s.slice(1)},s))]})]})]}),e.jsxs("div",{children:[e.jsx(c,{htmlFor:"status-filter",children:"Status"}),e.jsxs(x,{value:T,onValueChange:ne,children:[e.jsx(j,{id:"status-filter",children:e.jsx(f,{})}),e.jsxs(p,{children:[e.jsx(d,{value:"all",children:"All Status"}),e.jsx(d,{value:"active",children:"Active"}),e.jsx(d,{value:"inactive",children:"Inactive"})]})]})]})]}),e.jsxs("div",{className:"mt-4 text-sm text-muted-foreground",children:["Showing ",B.length," of ",v.length," challenges"]})]})]}),W&&e.jsxs(U,{children:[e.jsxs(q,{children:[e.jsx(R,{children:"Add New Challenge"}),e.jsx(we,{children:"Create a new challenge for the community"})]}),e.jsxs(M,{className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx(c,{htmlFor:"title",children:"Title*"}),e.jsx(C,{id:"title",value:i.title,onChange:s=>u({...i,title:s.target.value}),placeholder:"Daily Gratitude Practice"})]}),e.jsxs("div",{children:[e.jsx(c,{htmlFor:"description",children:"Description*"}),e.jsx(be,{id:"description",value:i.description,onChange:s=>u({...i,description:s.target.value}),placeholder:"Write down three things you're grateful for today...",rows:3})]}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-3 gap-4",children:[e.jsxs("div",{children:[e.jsx(c,{htmlFor:"difficulty",children:"Difficulty"}),e.jsxs(x,{value:i.difficulty,onValueChange:s=>u({...i,difficulty:s}),children:[e.jsx(j,{children:e.jsx(f,{})}),e.jsx(p,{children:_.map(s=>e.jsx(d,{value:s,children:s},s))})]})]}),e.jsxs("div",{children:[e.jsx(c,{htmlFor:"points",children:"Points"}),e.jsx(C,{id:"points",type:"number",value:i.points,onChange:s=>u({...i,points:parseInt(s.target.value)||0}),min:"0"})]}),e.jsxs("div",{children:[e.jsx(c,{htmlFor:"category",children:"Category"}),e.jsxs(x,{value:i.category,onValueChange:s=>u({...i,category:s}),children:[e.jsx(j,{children:e.jsx(f,{})}),e.jsx(p,{children:X.map(s=>e.jsx(d,{value:s,children:s},s))})]})]})]}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx(c,{htmlFor:"icon",children:"Icon"}),e.jsxs(x,{value:i.icon,onValueChange:s=>u({...i,icon:s}),children:[e.jsx(j,{children:e.jsx(f,{})}),e.jsx(p,{children:ce.map(s=>e.jsx(d,{value:s,children:s},s))})]})]}),e.jsxs("div",{children:[e.jsx(c,{htmlFor:"action",children:"Action (optional)"}),e.jsx(C,{id:"action",value:i.action,onChange:s=>u({...i,action:s.target.value}),placeholder:"complete"})]})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(n,{onClick:de,children:[e.jsx(te,{className:"h-4 w-4 mr-2"}),"Save Challenge"]}),e.jsxs(n,{variant:"outline",onClick:()=>L(!1),children:[e.jsx(ee,{className:"h-4 w-4 mr-2"}),"Cancel"]})]})]})]}),e.jsxs(U,{children:[e.jsx(q,{children:e.jsxs(R,{children:["All Challenges (",B.length,")"]})}),e.jsx(M,{children:e.jsx("div",{className:"overflow-x-auto",children:e.jsxs(Fe,{children:[e.jsx(Ae,{children:e.jsxs(se,{children:[e.jsx(S,{children:"Title"}),e.jsx(S,{children:"Difficulty"}),e.jsx(S,{children:"Points"}),e.jsx(S,{children:"Category"}),e.jsx(S,{children:"Status"}),e.jsx(S,{children:"Actions"})]})}),e.jsx(Te,{children:B.map(s=>e.jsxs(se,{children:[e.jsx(w,{className:"max-w-xs",children:F===s.id?e.jsx(C,{value:s.title,onChange:t=>b(v.map(a=>a.id===s.id?{...a,title:t.target.value}:a))}):e.jsx("span",{className:"font-medium",children:s.title})}),e.jsx(w,{children:F===s.id?e.jsxs(x,{value:s.difficulty,onValueChange:t=>b(v.map(a=>a.id===s.id?{...a,difficulty:t}:a)),children:[e.jsx(j,{children:e.jsx(f,{})}),e.jsx(p,{children:_.map(t=>e.jsx(d,{value:t,children:t},t))})]}):e.jsx($,{variant:s.difficulty==="easy"?"default":s.difficulty==="medium"?"secondary":"destructive",children:s.difficulty})}),e.jsx(w,{children:F===s.id?e.jsx(C,{type:"number",value:s.points,onChange:t=>b(v.map(a=>a.id===s.id?{...a,points:parseInt(t.target.value)||0}:a)),className:"w-20"}):e.jsx("span",{children:s.points})}),e.jsx(w,{children:e.jsx($,{variant:"outline",children:s.category})}),e.jsx(w,{children:e.jsx(n,{variant:"ghost",size:"sm",onClick:()=>ue(s.id,s.is_active),children:e.jsx($,{variant:s.is_active?"default":"secondary",children:s.is_active?"Active":"Inactive"})})}),e.jsx(w,{children:e.jsx("div",{className:"flex gap-2",children:F===s.id?e.jsxs(e.Fragment,{children:[e.jsx(n,{size:"sm",variant:"outline",onClick:()=>oe(s.id),children:e.jsx(te,{className:"h-3 w-3"})}),e.jsx(n,{size:"sm",variant:"outline",onClick:()=>E(null),children:e.jsx(ee,{className:"h-3 w-3"})})]}):e.jsxs(e.Fragment,{children:[e.jsx(n,{size:"sm",variant:"outline",onClick:()=>E(s.id),children:e.jsx(Ve,{className:"h-3 w-3"})}),e.jsx(n,{size:"sm",variant:"destructive",onClick:()=>he(s.id),children:e.jsx(_e,{className:"h-3 w-3"})})]})})})]},s.id))})]})})})]})]})})};export{Qe 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.