PHP-Course/exam/dist/assets/index-CB6IjOSm.js
2024-07-29 00:43:12 +02:00

2 lines
32 KiB
JavaScript

import{j as e,M as Pe,a as z,T as w,u as Y,b as ee,D as N,c as U,G as v,d as We,e as te,B as P,f as ne,g as A,C as Q,h as E,L as M,A as Qe,i as we,I as Z,k as He,l as B,P as _,m as Ke,n as Ge,o as Fe,S as W,p as Ee,q as Ae,r as Ve,s as Te,t as De,v as Je,w as ge,x as Ze,y as Xe,z as Ye,E as et,F as tt,H as nt,J as rt}from"./mui-53GMXZgr.js";import{u as H,a as K,b as je,c as ae,d as st,e as at,L as R,f as ot,O as it,g as ct,h as G,q as oe,i as fe,j as X,r as me,k as lt,R as ut,Q as dt,l as ht}from"./tanstack-ZGp-Rrdw.js";import{c as mt,r as j}from"./react-C_FdcE2X.js";import{i as J,B as xt,a as pt,b as gt,u as $,t as C,T as jt}from"./i18n-DyW0LrNj.js";(function(){const n=document.createElement("link").relList;if(n&&n.supports&&n.supports("modulepreload"))return;for(const r of document.querySelectorAll('link[rel="modulepreload"]'))c(r);new MutationObserver(r=>{for(const d of r)if(d.type==="childList")for(const h of d.addedNodes)h.tagName==="LINK"&&h.rel==="modulepreload"&&c(h)}).observe(document,{childList:!0,subtree:!0});function i(r){const d={};return r.integrity&&(d.integrity=r.integrity),r.referrerPolicy&&(d.referrerPolicy=r.referrerPolicy),r.crossOrigin==="use-credentials"?d.credentials="include":r.crossOrigin==="anonymous"?d.credentials="omit":d.credentials="same-origin",d}function c(r){if(r.ep)return;r.ep=!0;const d=i(r);fetch(r.href,d)}})();var xe={},be=mt;xe.createRoot=be.createRoot,xe.hydrateRoot=be.hydrateRoot;const O=new Map;function ft(t,n){const i=n==null?void 0:n.serializer,[c]=j.useState(n==null?void 0:n.defaultValue);return yt(t,c,n==null?void 0:n.storageSync,i==null?void 0:i.parse,i==null?void 0:i.stringify)}function yt(t,n,i=!0,c=vt,r=JSON.stringify){const d=j.useRef({string:null,parsed:void 0}),h=j.useSyncExternalStore(j.useCallback(o=>{const p=u=>{t===u&&o()};return pe.add(p),()=>{pe.delete(p)}},[t]),()=>{var o;const p=(o=se(()=>localStorage.getItem(t)))!==null&&o!==void 0?o:null;if(O.has(t))d.current.parsed=O.get(t);else if(p!==d.current.string){let u;try{u=p===null?n:c(p)}catch{u=n}d.current.parsed=u}return d.current.string=p,p===null&&n!==void 0&&se(()=>{const u=r(n);localStorage.setItem(t,u),d.current={string:u,parsed:n}}),d.current.parsed},()=>n),s=j.useCallback(o=>{const p=o instanceof Function?o(d.current.parsed):o;try{localStorage.setItem(t,r(p)),O.delete(t)}catch{O.set(t,p)}de(t)},[t,r]);return j.useEffect(()=>{if(!i)return;const o=p=>{p.storageArea===se(()=>localStorage)&&p.key===t&&de(t)};return window.addEventListener("storage",o),()=>window.removeEventListener("storage",o)},[t,i]),j.useMemo(()=>[h,s,{isPersistent:h===n||!O.has(t),removeItem(){se(()=>localStorage.removeItem(t)),O.delete(t),de(t)}}],[t,s,h,n])}const pe=new Set;function de(t){for(const n of[...pe])n(t)}function vt(t){return t==="undefined"?void 0:JSON.parse(t)}function se(t){try{return t()}catch{}}const he=15,Ct=3,q="https://khofmann.userpage.fu-berlin.de/phpCourse/exam/api/",Ue=j.createContext({hasAuth:!1,currentSession:[void 0,void 0]}),F=()=>{const{hasAuth:t,authenticatedUser:n,currentSession:i,logIn:c,logOut:r,posts:d,newPost:h,updatePost:s,deletePost:o,user:p,createUser:u,updateUser:a,updateUserImage:l,userPosts:m}=j.useContext(Ue);if(c&&r&&d&&h&&s&&o&&p&&u&&a&&l&&m)return{hasAuth:t,authenticatedUser:n,currentSession:i,logIn:c,logOut:r,posts:d,newPost:h,updatePost:s,deletePost:o,user:p,createUser:u,updateUser:a,updateUserImage:l,userPosts:m};throw new Error("Couldn't find context. Is your component inside an ApiProvider?")},wt=({children:t})=>{const[n,i]=j.useState(!1),[c,r]=j.useState(),[d,h]=ft("egb_session",{defaultValue:[void 0,void 0]}),s=j.useRef();j.useEffect(()=>{d[0]&&!s.current&&(s.current=d[0],Ce())},[d]);const o=async(g,f)=>{const{user:y,token:S,refreshToken:Ne}=await(await ye("login",{email:g,password:f})).json();r(y),h([S,Ne]),i(!0),s.current=S},p=async()=>{try{return s.current?await(await T(()=>le("logout"))).json():!0}catch{return!1}finally{r(void 0),h([void 0,void 0]),i(!1),s.current=void 0}},u=async g=>{const f=`posts?p=${g??0}&l=${he}`;return n?await(await T(()=>ue(f))).json():await(await _e(f)).json()},a=async g=>await(await T(()=>le(`posts?l=${he}`,g))).json(),l=async(g,f)=>await(await T(()=>ve(`posts/${f}`,g))).json(),m=async g=>await(await T(()=>ze(`posts/${g}?l=${he}`))).json(),x=async g=>await(await T(()=>ue(`users/${g??(c==null?void 0:c.id)}`))).json(),b=async g=>await(await ye("register",g)).json(),L=async(g,f)=>{const y=await(await T(()=>ve(`users/${f??"self"}`,g))).json();return r(y),y},re=async(g,f)=>{const y=new FormData;g.image&&y.append("image",g.image),!g.image&&g.predefined&&y.append("predefined",g.predefined);const S=await(await T(()=>Be(`users/${f??"self"}/image`,y))).json();return r(S),S},V=async g=>await(await T(()=>ue(`users/${g}/posts?l=${Ct}&s=desc`))).json(),ye=async(g,f,y)=>{const S=await fetch(`${q}${g}`,{mode:"cors",method:"post",headers:y,body:JSON.stringify(f)});if(S.ok)return S;throw await S.json()},le=async(g,f,y)=>{const S=await fetch(`${q}${g}`,{mode:"cors",method:"post",headers:{token:s.current??"",...y},body:JSON.stringify(f)});if(S.ok)return S;throw await S.json()},Be=async(g,f,y)=>{const S=await fetch(`${q}${g}`,{mode:"cors",method:"post",headers:{token:s.current??"",...y},body:f});if(S.ok)return S;throw await S.json()},_e=async(g,f)=>{const y=await fetch(`${q}${g}`,{mode:"cors",method:"get",headers:f});if(y.ok)return y;throw await y.json()},ue=async(g,f)=>{const y=await fetch(`${q}${g}`,{mode:"cors",method:"get",headers:{token:s.current??"",...f}});if(y.ok)return y;throw await y.json()},ze=async(g,f)=>{const y=await fetch(`${q}${g}`,{mode:"cors",method:"delete",headers:{token:s.current??"",...f}});if(y.ok)return y;throw await y.json()},ve=async(g,f,y)=>{const S=await fetch(`${q}${g}`,{mode:"cors",method:"patch",headers:{token:s.current??"",...y},body:JSON.stringify(f)});if(S.ok)return S;throw await S.json()},T=async g=>{try{return console.log("[REAUTH] fetching"),await g()}catch{try{return console.log("[REAUTH] fail, refreshing"),await Ce(),console.log("[REAUTH] refreshed, fetching again"),await g()}catch(f){throw console.log("[REAUTH] terminating session",f),r(void 0),i(!1),h([void 0,void 0]),s.current=void 0,f}}},Ce=async()=>{const{user:g,token:f,refreshToken:y}=await(await le("refresh",{refreshToken:d[1]??"INVALID DEFAULT"})).json();r(g),h([f,y]),i(!0),s.current=f};return e.jsx(Ue.Provider,{value:{hasAuth:n,authenticatedUser:c,currentSession:d,logIn:o,logOut:p,posts:u,newPost:a,updatePost:l,deletePost:m,user:x,createUser:b,updateUser:L,updateUserImage:re,userPosts:V},children:t})};J.use(xt).use(pt).use(gt).init({fallbackLng:"en",debug:!1,interpolation:{escapeValue:!1},backend:{loadPath:"/phpCourse/exam/dist/locales/{{lng}}/{{ns}}.json"}});const bt=({anchorEl:t,handleClose:n})=>e.jsxs(Pe,{anchorEl:t,anchorOrigin:{vertical:"bottom",horizontal:"right"},keepMounted:!0,transformOrigin:{vertical:"top",horizontal:"right"},open:!!t,onClose:n,sx:{"& .MuiMenu-paper":{minWidth:"240px"}},children:[e.jsx(z,{selected:J.language==="en",onClick:()=>{J.changeLanguage("en"),n()},children:"English"},"de"),e.jsx(z,{selected:J.language==="de",onClick:()=>{J.changeLanguage("de"),n()},children:"Deutsch"},"en")]});var I=(t=>(t.INDEX="/",t.PROFILE="/profile",t))(I||{}),D=(t=>(t.NOT_FOUND="NotFound",t.UNAUTHORIZED="Unauthorized",t.NOT_ALLOWED="NotAllowed",t.FAILED_UPDATE="FailedUpdate",t.MISSING_FIELD="MissingField",t.DUPLICATE="Duplicate",t))(D||{});const k=({error:t,context:n,color:i="error.main"})=>{const{t:c}=$();if(!t)return null;if(t.code)switch(t.code){case D.NOT_FOUND:return e.jsx(w,{color:i,children:c(t.code,{context:`${t.entity}:${n}`})});case D.NOT_ALLOWED:case D.UNAUTHORIZED:return e.jsx(w,{color:i,children:c(t.code,{context:n})});case D.FAILED_UPDATE:return t.fields.map((r,d)=>e.jsx(w,{color:i,children:c(t.code,{context:`${t.reasons[d]}:${r}:${n}`})},`error_${r}`));case D.MISSING_FIELD:return t.fields.map(r=>e.jsx(w,{color:i,children:c(t.code,{context:`${r}:${n}`})},`error_${r}`));case D.DUPLICATE:return e.jsx(w,{color:i,children:c(t.code,{context:`${t.entity}:${n}`})})}return e.jsx(w,{color:i,children:c((t==null?void 0:t.message)??"Unknown",{context:n})})},St=({open:t,onClose:n})=>{const[i,c]=j.useState(),{t:r}=$(),d=Y(),h=ee(d.breakpoints.only("xs"),{noSsr:!0}),s=F(),o=H({mutationFn:({data:a})=>s.createUser(a)}),p=K({defaultValues:{username:"",email:"",password:""},onSubmit:async({value:a})=>{try{o.mutate({data:a},{onSuccess:()=>c(void 0),onError:c})}catch(l){c(l)}}}),u=()=>{p.reset(),c(void 0),n()};return o.isSuccess?e.jsxs(N,{open:t,onClose:u,fullWidth:!0,fullScreen:h,children:[e.jsx(U,{children:e.jsxs(v,{container:!0,spacing:2,children:[e.jsx(v,{item:!0,xs:12,sx:{display:"flex",justifyContent:"center"},children:e.jsx(We,{color:"action",sx:{fontSize:"200px"}})}),e.jsx(v,{item:!0,xs:12,sx:{display:"flex",justifyContent:"center"},children:e.jsx(w,{variant:"h5",children:r("Confirm header")})}),e.jsx(v,{item:!0,xs:12,sx:{display:"flex",justifyContent:"center"},children:e.jsx(w,{children:r("Confirm mail")})})]})}),e.jsx(te,{children:e.jsx(P,{onClick:u,children:r("Close")})})]}):e.jsxs(N,{open:t,onClose:u,fullWidth:!0,fullScreen:h,PaperProps:{component:"form",encType:"multipart/form-data",onSubmit:a=>{a.preventDefault(),a.stopPropagation(),p.handleSubmit()},onKeyDown:a=>{a.key==="Tab"&&a.stopPropagation()},noValidate:!0},children:[e.jsx(ne,{children:r("Register")}),e.jsx(U,{sx:{gap:2},children:e.jsxs(v,{container:!0,spacing:2,children:[e.jsx(v,{item:!0,xs:12,children:e.jsx(p.Field,{name:"username",validators:{onChange:({value:a})=>a?void 0:r("Username required"),onChangeAsyncDebounceMs:250,onChangeAsync:async({value:a})=>!a&&r("Username required")},children:a=>e.jsx(e.Fragment,{children:e.jsx(A,{variant:"outlined",name:a.name,value:a.state.value,onBlur:a.handleBlur,onChange:l=>a.handleChange(l.target.value),size:"small",label:r("Username"),required:!0,error:a.state.meta.isTouched&&a.state.meta.errors.length>0,helperText:a.state.meta.isTouched?a.state.meta.errors.join(","):"",autoComplete:"new-username",fullWidth:!0,margin:"dense"})})})}),e.jsx(v,{item:!0,xs:12,children:e.jsx(p.Field,{name:"email",validators:{onChange:({value:a})=>a?void 0:r("Email required"),onChangeAsyncDebounceMs:250,onChangeAsync:async({value:a})=>!a&&r("Email required")},children:a=>e.jsx(e.Fragment,{children:e.jsx(A,{variant:"outlined",name:a.name,value:a.state.value,onBlur:a.handleBlur,onChange:l=>a.handleChange(l.target.value),size:"small",label:r("Email"),required:!0,error:a.state.meta.isTouched&&a.state.meta.errors.length>0,helperText:a.state.meta.isTouched?a.state.meta.errors.join(","):"",type:"email",autoComplete:"new-email",inputMode:"email",fullWidth:!0})})})}),e.jsx(v,{item:!0,xs:12,children:e.jsx(p.Field,{name:"password",validators:{onChange:({value:a})=>a?void 0:r("Password required"),onChangeAsyncDebounceMs:250,onChangeAsync:async({value:a})=>!a&&r("Password required")},children:a=>e.jsx(e.Fragment,{children:e.jsx(A,{variant:"outlined",name:a.name,value:a.state.value,onBlur:a.handleBlur,onChange:l=>a.handleChange(l.target.value),size:"small",label:r("Password"),required:!0,error:a.state.meta.isTouched&&a.state.meta.errors.length>0,helperText:a.state.meta.isTouched?a.state.meta.errors.join(","):"",type:"password",autoComplete:"new-password",fullWidth:!0})})})}),e.jsx(v,{item:!0,xs:12,children:e.jsx(p.Subscribe,{selector:a=>[a.canSubmit,a.isSubmitting],children:([a])=>e.jsx(e.Fragment,{children:e.jsx(P,{type:"submit",disabled:!a||o.isPending,variant:"contained",endIcon:o.isPending&&e.jsx(Q,{color:"inherit",size:"20px"}),children:r("Register")})})})}),e.jsx(v,{item:!0,xs:12,children:i&&e.jsx(k,{error:i,context:"register"})})]})})]})},Pt=({handleClose:t})=>{const[n,i]=j.useState(),{t:c}=$(),r=je(),d=F(),h=K({defaultValues:{email:"",password:""},onSubmit:async({value:s})=>{try{await d.logIn(s.email,s.password),r.invalidate(),t()}catch(o){i(o)}}});return e.jsx("form",{onSubmit:s=>{s.preventDefault(),s.stopPropagation(),h.handleSubmit()},onKeyDown:s=>{s.key==="Tab"&&s.stopPropagation()},noValidate:!0,children:e.jsxs(E,{sx:{display:"grid",gap:2,padding:1},children:[e.jsx(h.Field,{name:"email",validators:{onChange:({value:s})=>s?void 0:c("Email required"),onChangeAsyncDebounceMs:250,onChangeAsync:async({value:s})=>!s&&c("Email required")},children:s=>e.jsx(e.Fragment,{children:e.jsx(A,{variant:"outlined",name:s.name,value:s.state.value,onBlur:s.handleBlur,onChange:o=>s.handleChange(o.target.value),size:"small",label:c("Email"),required:!0,error:s.state.meta.isTouched&&s.state.meta.errors.length>0,helperText:s.state.meta.isTouched?s.state.meta.errors.join(","):"",type:"email",autoComplete:"email",inputMode:"email",fullWidth:!0})})}),e.jsx(h.Field,{name:"password",validators:{onChange:({value:s})=>s?void 0:c("Password required"),onChangeAsyncDebounceMs:250,onChangeAsync:async({value:s})=>!s&&c("Password required")},children:s=>e.jsx(e.Fragment,{children:e.jsx(A,{variant:"outlined",name:s.name,value:s.state.value,onBlur:s.handleBlur,onChange:o=>s.handleChange(o.target.value),size:"small",label:c("Password"),required:!0,error:s.state.meta.isTouched&&s.state.meta.errors.length>0,helperText:s.state.meta.isTouched?s.state.meta.errors.join(","):"",type:"password",autoComplete:"current-password",fullWidth:!0})})}),e.jsx(h.Subscribe,{selector:s=>[s.canSubmit,s.isSubmitting],children:([s])=>e.jsx(e.Fragment,{children:e.jsx(P,{type:"submit",disabled:!s,variant:"contained",children:c("Log in")})})}),n&&e.jsx(k,{error:n,context:"login"})]})})},Ft=({anchorEl:t,handleClose:n})=>{const[i,c]=j.useState(!1),r=ae(),d=je(),h=st({from:"/profile/",strict:!0,shouldThrow:!1}),s=F(),o=()=>{c(!1),n()};return e.jsx(Pe,{anchorEl:t,anchorOrigin:{vertical:"bottom",horizontal:"right"},keepMounted:!0,transformOrigin:{vertical:"top",horizontal:"right"},open:!!t,onClose:o,sx:{"& .MuiMenu-paper":{minWidth:"240px"}},children:s.authenticatedUser?[e.jsx(z,{selected:!!h,onClick:()=>{r({to:I.PROFILE}),o()},children:C("Profile")},"profile"),e.jsx(z,{onClick:async()=>{await s.logOut(),d.invalidate(),o()},children:C("Log out")},"logout")]:i?e.jsx(St,{open:i,onClose:()=>c(!1)}):e.jsxs(E,{children:[e.jsx(Pt,{handleClose:o}),e.jsx(E,{sx:{padding:1},children:e.jsxs(jt,{i18nKey:"Register prompt",children:[e.jsx(w,{component:"span"}),e.jsx(M,{sx:{cursor:"pointer"},variant:"body1",underline:"hover",onClick:()=>{c(!0),n()}}),e.jsx(w,{component:"span"})]})})]})})},Et=({children:t})=>{const n=Ge({disableHysteresis:!0,threshold:0});return j.cloneElement(t,{elevation:n?4:0})},At=()=>{const[t,n]=j.useState(null),[i,c]=j.useState(null),{t:r}=$(),d=at({select:o=>o.status==="pending"}),h=F(),s=()=>{c(null),n(null)};return e.jsx(Et,{children:e.jsxs(e.Fragment,{children:[e.jsx(Qe,{children:e.jsxs(we,{children:[e.jsxs(E,{sx:{flexGrow:1,alignItems:"center",display:"flex",gap:1},children:[e.jsx(M,{component:R,to:"/",color:"#FFF",variant:"h6",underline:"none",children:r("GuestBook")}),d&&e.jsx(Q,{size:16,thickness:10,sx:{color:"white"}})]}),e.jsxs(E,{sx:{display:"flex",gap:1},children:[e.jsx(Z,{size:"large",onClick:o=>c(o.currentTarget),children:e.jsx(He,{sx:{color:"white"}})}),h.authenticatedUser?e.jsx(Z,{onClick:o=>n(o.currentTarget),sx:{p:0},children:e.jsx(B,{alt:h.authenticatedUser.username,src:`${h.authenticatedUser.image}`,children:e.jsx(_,{})})}):e.jsx(Z,{size:"large",onClick:o=>n(o.currentTarget),color:"inherit",children:e.jsx(Ke,{})})]}),e.jsx(bt,{anchorEl:i,handleClose:s}),e.jsx(Ft,{anchorEl:t,handleClose:s})]})}),e.jsx(we,{})]})})},Tt=()=>e.jsxs(e.Fragment,{children:[e.jsx(At,{}),e.jsx(E,{sx:{display:"flex",justifyContent:"center"},children:e.jsx(E,{sx:{maxWidth:"800px",flexGrow:1},children:e.jsx(it,{})})}),!1]}),Dt=({error:t})=>{const n=je(),i=ct();return j.useEffect(()=>{i.reset()},[i]),e.jsxs("div",{children:[t.message,e.jsx("button",{onClick:()=>{n.invalidate()},children:"retry"})]})},ie=ot()({component:Tt,errorComponent:Dt}),Ut=()=>{const[t,n]=j.useState(),[i,c]=j.useState(0),{t:r}=$(),d=G(),h=ae(),s=F(),o=H({mutationFn:({data:u})=>s.newPost(u)}),p=K({defaultValues:{content:""},onSubmit:async({value:u})=>{try{o.mutate({data:u},{onSuccess:async a=>{p.reset(),await d.invalidateQueries({queryKey:["posts"]}),h({to:"/",search:{page:a.pages-1}})},onError:n})}catch(a){n(a)}}});return e.jsx("form",{onSubmit:u=>{u.preventDefault(),u.stopPropagation(),p.handleSubmit()},onKeyDown:u=>{u.key==="Tab"&&u.stopPropagation()},noValidate:!0,children:e.jsxs(E,{sx:{display:"grid",gap:2,padding:1},children:[e.jsx(p.Field,{name:"content",validators:{onChange:({value:u})=>u?void 0:r("Content required"),onChangeAsyncDebounceMs:250,onChangeAsync:async({value:u})=>!u&&r("Content required")},children:u=>e.jsxs(e.Fragment,{children:[e.jsx(A,{variant:"outlined",multiline:!0,minRows:3,name:u.name,value:u.state.value,onBlur:u.handleBlur,onChange:a=>{a.target.value.length<=250&&(c(a.target.value.length),u.handleChange(a.target.value))},size:"small",label:r("Comment"),required:!0,error:u.state.meta.isTouched&&u.state.meta.errors.length>0,helperText:u.state.meta.isTouched?u.state.meta.errors.join(","):"",autoComplete:"off",fullWidth:!0}),e.jsx(Fe,{variant:"determinate",value:i/250*100,color:i===250?"error":i>=200?"warning":"primary"})]})}),e.jsx(p.Subscribe,{selector:u=>[u.canSubmit,u.isSubmitting],children:([u])=>e.jsx(e.Fragment,{children:e.jsx(P,{type:"submit",disabled:!u||o.isPending,variant:"contained",endIcon:o.isPending&&e.jsx(Q,{color:"inherit",size:"20px"}),children:r("Post comment")})})}),e.jsx(W,{open:o.isError,autoHideDuration:2e3,onClose:()=>{o.reset()},TransitionProps:{onExited:()=>n(void 0)},children:e.jsx(Ee,{severity:"error",variant:"filled",sx:{width:"100%"},children:t&&e.jsx(k,{error:t,context:"newPost",color:"white"})})})]})})},Ie=t=>new Date(t.date).toLocaleString(navigator.languages[0]??"de-DE",{timeZone:t.timezone,day:"2-digit",month:"2-digit",year:"numeric",hour:"2-digit",hour12:!1,minute:"2-digit"}),It=({post:t,open:n,onClose:i})=>{const[c,r]=j.useState(),[d,h]=j.useState(t.content.length),s=Y(),o=ee(s.breakpoints.only("xs"),{noSsr:!0}),p=G(),u=F(),a=H({mutationFn:({data:x,id:b})=>u.updatePost(x,b)}),l=K({defaultValues:{content:t.content.replaceAll("<br />","")},onSubmit:async({value:x})=>{try{a.mutate({data:x,id:t.id},{onSuccess:()=>{m(),p.invalidateQueries({queryKey:["posts"]})},onError:r})}catch(b){r(b)}}}),m=()=>{l.reset(),r(void 0),i()};return e.jsxs(N,{open:n,onClose:m,fullWidth:!0,fullScreen:o,PaperProps:{component:"form",onSubmit:x=>{x.preventDefault(),x.stopPropagation(),l.handleSubmit()},onKeyDown:x=>{x.key==="Tab"&&x.stopPropagation()},noValidate:!0},children:[e.jsx(ne,{children:C("Edit post")}),e.jsx(U,{children:e.jsx(l.Field,{name:"content",validators:{onChange:({value:x})=>x?void 0:C("Content required"),onChangeAsyncDebounceMs:250,onChangeAsync:async({value:x})=>!x&&C("Content required")},children:x=>e.jsxs(e.Fragment,{children:[e.jsx(A,{variant:"outlined",multiline:!0,minRows:3,name:x.name,value:x.state.value,onBlur:x.handleBlur,onChange:b=>{b.target.value.length<=250&&(h(b.target.value.length),x.handleChange(b.target.value))},size:"small",label:C("Comment"),required:!0,error:x.state.meta.isTouched&&x.state.meta.errors.length>0,helperText:x.state.meta.isTouched?x.state.meta.errors.join(","):"",autoComplete:"off",margin:"dense",fullWidth:!0}),e.jsx(Fe,{variant:"determinate",value:d/250*100,color:d===250?"error":d>=200?"warning":"primary"})]})})}),e.jsx(te,{children:e.jsx(l.Subscribe,{selector:x=>[x.canSubmit,x.isSubmitting],children:([x])=>e.jsxs(e.Fragment,{children:[e.jsx(P,{variant:"outlined",onClick:()=>{m()},children:C("Cancel")}),e.jsx(P,{type:"submit",disabled:!x||a.isPending,autoFocus:!0,variant:"contained",endIcon:a.isPending&&e.jsx(Q,{color:"inherit",size:"20px"}),children:C("Save")})]})})}),c&&e.jsx(U,{children:e.jsx(k,{error:c,context:"postUpdate"})})]})},$e=({post:t,disableActions:n})=>{var m,x,b,L,re;const[i,c]=j.useState(!1),[r,d]=j.useState(!1),[h,s]=j.useState(),{t:o}=$(),p=G(),u=ae(),a=F(),l=H({mutationFn:V=>a.deletePost(V)});return e.jsxs(Ae,{children:[e.jsx(Ve,{avatar:!n&&"id"in t.user?t.user.id!==((m=a.authenticatedUser)==null?void 0:m.id)?e.jsx(M,{component:R,to:"/profile/$id",params:{id:t.user.id},children:e.jsx(B,{alt:t.user.username,src:`${t.user.image}`,children:e.jsx(_,{})})}):e.jsx(M,{component:R,to:"/profile",children:e.jsx(B,{alt:t.user.username,src:`${t.user.image}`,children:e.jsx(_,{})})}):e.jsx(B,{alt:t.user.username,src:`${t.user.image}`,children:e.jsx(_,{})}),title:!n&&"id"in t.user?t.user.id!==((x=a.authenticatedUser)==null?void 0:x.id)?e.jsx(M,{component:R,to:"/profile/$id",params:{id:t.user.id},children:t.user.username}):e.jsx(M,{component:R,to:"/profile",children:t.user.username}):t.user.username,subheader:Ie(t.postedAt)}),e.jsx(Te,{children:e.jsx(w,{children:e.jsx("span",{dangerouslySetInnerHTML:{__html:t.content}})})}),e.jsxs(De,{children:[!n&&(((b=a.authenticatedUser)==null?void 0:b.isAdmin)||"id"in t.user&&t.user.id===((L=a.authenticatedUser)==null?void 0:L.id))&&e.jsxs(e.Fragment,{children:[e.jsx(P,{size:"small",onClick:()=>d(!0),children:o("Edit")}),e.jsx(It,{post:t,open:r,onClose:()=>d(!1)})]}),!n&&((re=a.authenticatedUser)==null?void 0:re.isAdmin)&&e.jsxs(e.Fragment,{children:[e.jsx(P,{size:"small",color:"error",onClick:()=>c(!0),children:o("Delete")}),e.jsxs(N,{open:i,onClose:()=>c(!1),children:[e.jsx(ne,{children:o("Confirm post delete title")}),e.jsx(U,{children:e.jsx(Je,{children:o("Confirm post delete body",{name:t.user.username})})}),e.jsxs(te,{children:[e.jsx(P,{onClick:()=>c(!1),autoFocus:!0,variant:"contained",children:o("No")}),e.jsx(P,{variant:"outlined",color:"error",onClick:()=>{l.mutate(t.id,{onSuccess:async V=>{await p.invalidateQueries({queryKey:["posts"]}),u({to:"/",search:{page:V.pages-1}})},onError:s}),c(!1)},children:o("Yes")})]})]})]})]}),e.jsx(W,{open:l.isError,autoHideDuration:2e3,onClose:()=>{l.reset()},TransitionProps:{onExited:()=>s(void 0)},children:e.jsx(Ee,{severity:"error",variant:"filled",sx:{width:"100%"},children:h&&e.jsx(k,{error:h,context:"deletePost",color:"white"})})}),e.jsx(W,{open:l.isPending,message:o("Deleting")})]})},Le=(t,n)=>oe({queryKey:["posts",{page:n??0,hasAuth:t.hasAuth}],queryFn:async()=>await t.posts(n)}),$t=()=>{const t=F(),{page:n}=qe.useSearch(),{data:i,isFetching:c}=X(Le(t,n)),{t:r}=$(),d=ae(),h=Y(),s=ee(h.breakpoints.not("xs"),{noSsr:!0});return j.useEffect(()=>{(n??0)>=i.pages&&d({to:"/",search:{page:i.pages-1}})},[n]),e.jsxs(e.Fragment,{children:[e.jsx(W,{open:c,message:r("Updating")}),e.jsxs(v,{container:!0,spacing:2,children:[i.data.map(o=>e.jsx(v,{item:!0,xs:12,children:e.jsx($e,{post:o})},o.id)),e.jsx(v,{item:!0,xs:12,children:e.jsx(ge,{variant:"middle"})}),e.jsx(v,{item:!0,xs:12,sx:{display:"flex",justifyContent:"center"},children:e.jsx(Ze,{page:(n??0)+1,count:i.pages,siblingCount:s?1:0,color:"primary",renderItem:o=>e.jsx(Xe,{...o,component:R,to:"/",search:{page:(o.page??0)>0?(o.page??1)-1:void 0},onClick:p=>o.onClick(p)})})}),t.hasAuth?e.jsx(v,{item:!0,xs:12,children:e.jsx(Ut,{})}):e.jsxs(v,{item:!0,xs:12,children:[e.jsx(w,{variant:"h5",children:r("Leave comment header")}),e.jsx(w,{children:r("Leave comment action")})]})]})]})},qe=fe(I.INDEX)({loaderDeps:({search:{page:t}})=>({page:t}),loader:({context:{queryClient:t,Api:n},deps:{page:i}})=>t.ensureQueryData(Le(n,i)),validateSearch:t=>({page:(t==null?void 0:t.page)!==void 0?Number((t==null?void 0:t.page)??0):void 0}),component:$t}),Lt=({user:t,open:n,onClose:i})=>{const[c,r]=j.useState(),d=Y(),h=ee(d.breakpoints.only("xs"),{noSsr:!0}),s=G(),o=F(),p=H({mutationFn:({data:l,id:m})=>o.updateUser(l,m)}),u=K({defaultValues:{username:t.username,email:t.email},onSubmit:async({value:l})=>{var m;try{p.mutate({data:l,id:((m=o.authenticatedUser)==null?void 0:m.id)===t.id?void 0:t.id},{onSuccess:()=>{var b;a();const x=((b=o.authenticatedUser)==null?void 0:b.id)===t.id?["profile"]:["profile",{id:t.id}];s.invalidateQueries({queryKey:x})},onError:r})}catch(x){r(x)}}}),a=()=>{u.reset(),r(void 0),i()};return e.jsxs(N,{open:n,onClose:a,fullWidth:!0,fullScreen:h,PaperProps:{component:"form",onSubmit:l=>{l.preventDefault(),l.stopPropagation(),u.handleSubmit()},onKeyDown:l=>{l.key==="Tab"&&l.stopPropagation()},noValidate:!0},children:[e.jsx(ne,{children:C("Edit data")}),e.jsxs(U,{children:[e.jsx(u.Field,{name:"username",validators:{onChange:({value:l})=>l?void 0:C("Username required"),onChangeAsyncDebounceMs:250,onChangeAsync:async({value:l})=>!l&&C("Username required")},children:l=>e.jsx(e.Fragment,{children:e.jsx(A,{name:l.name,value:l.state.value,onBlur:l.handleBlur,onChange:m=>l.handleChange(m.target.value),size:"small",label:C("Username"),required:!0,margin:"dense",autoComplete:"new-username",fullWidth:!0,error:l.state.meta.isTouched&&l.state.meta.errors.length>0,helperText:l.state.meta.isTouched?l.state.meta.errors.join(","):""})})}),e.jsx(u.Field,{name:"email",validators:{onChange:({value:l})=>l?void 0:C("Email required"),onChangeAsyncDebounceMs:250,onChangeAsync:async({value:l})=>!l&&C("Email required")},children:l=>e.jsx(e.Fragment,{children:e.jsx(A,{name:l.name,value:l.state.value,onBlur:l.handleBlur,onChange:m=>l.handleChange(m.target.value),size:"small",label:C("Email"),required:!0,margin:"dense",autoComplete:"new-email",fullWidth:!0,error:l.state.meta.isTouched&&l.state.meta.errors.length>0,helperText:l.state.meta.isTouched?l.state.meta.errors.join(","):""})})})]}),e.jsx(te,{children:e.jsx(u.Subscribe,{selector:l=>[l.canSubmit,l.isSubmitting],children:([l])=>e.jsxs(e.Fragment,{children:[e.jsx(P,{variant:"outlined",onClick:()=>{a()},children:C("Cancel")}),e.jsx(P,{type:"submit",disabled:!l||p.isPending,autoFocus:!0,variant:"contained",endIcon:p.isPending&&e.jsx(Q,{color:"inherit",size:"20px"}),children:C("Save")})]})})}),c&&e.jsx(U,{children:e.jsx(k,{error:c,context:"userUpdate"})})]})},qt=({user:t,open:n,onClose:i})=>{const[c,r]=j.useState(),d=Y(),h=ee(d.breakpoints.only("xs"),{noSsr:!0}),s=G(),o=F(),p=H({mutationFn:({data:m,id:x})=>o.updateUserImage(m,x)}),u=K({onSubmit:async({value:m})=>{var x;try{p.mutate({data:m,id:((x=o.authenticatedUser)==null?void 0:x.id)===t.id?void 0:t.id},{onSuccess:()=>{var L;l();const b=((L=o.authenticatedUser)==null?void 0:L.id)===t.id?["profile"]:["profile",{id:t.id}];s.invalidateQueries({queryKey:b})},onError:r})}catch(b){r(b)}}}),a=u.useStore(m=>({image:m.values.image,predefined:m.values.predefined})),l=()=>{u.reset(),r(void 0),i()};return e.jsxs(N,{open:n,onClose:l,fullWidth:!0,fullScreen:h,PaperProps:{component:"form",encType:"multipart/form-data",onSubmit:m=>{m.preventDefault(),m.stopPropagation(),u.handleSubmit()},onKeyDown:m=>{m.key==="Tab"&&m.stopPropagation()},noValidate:!0},children:[e.jsx(ne,{children:C("Edit image")}),e.jsx(U,{sx:{gap:2},children:e.jsxs(v,{container:!0,spacing:2,children:[e.jsx(v,{item:!0,xs:12,sx:{display:"flex",justifyContent:"center"},children:e.jsx(B,{alt:t.username,src:a.image?URL.createObjectURL(a.image):a.predefined?`profilbilder/default/${a.predefined}.svg`:`${t.image}`,sx:{width:"100px",height:"100px"},children:e.jsx(_,{sx:{width:"60px",height:"60px"}})})}),e.jsx(v,{item:!0,xs:12,children:e.jsxs(E,{sx:{display:"flex",gap:2},children:[e.jsx(P,{component:"label",role:void 0,variant:"contained",tabIndex:-1,startIcon:e.jsx(Ye,{}),fullWidth:!0,children:e.jsx(u.Field,{name:"image",children:m=>{var x;return e.jsxs(e.Fragment,{children:[e.jsx(E,{sx:{textOverflow:"ellipsis",textWrap:"nowrap",overflow:"hidden"},children:m.state.value?C("Upload named",{name:(x=m.state.value)==null?void 0:x.name}):C("Upload image")}),e.jsx(A,{name:m.name,onBlur:m.handleBlur,value:m.state.value?void 0:"",onChange:b=>m.handleChange(b.target.files[0]),size:"small",type:"file",required:!0,sx:{display:"none"},autoComplete:"off"})]})}})}),e.jsx(Z,{color:"error",onClick:()=>u.setFieldValue("image",void 0),children:e.jsx(et,{})})]})}),e.jsx(v,{item:!0,xs:12,children:e.jsx(ge,{variant:"middle",children:e.jsx(w,{sx:{opacity:.36},children:C("or")})})}),e.jsx(v,{item:!0,xs:12,children:e.jsx(u.Field,{name:"predefined",children:m=>e.jsxs(tt,{fullWidth:!0,children:[e.jsx(nt,{size:"small",children:C("Predefined")}),e.jsxs(rt,{name:m.name,value:m.state.value??"",onBlur:m.handleBlur,onChange:x=>m.handleChange(x.target.value),size:"small",label:C("Predefined"),autoComplete:"off",fullWidth:!0,children:[e.jsx(z,{value:"",selected:!0,children:"Keine Auswahl"}),[...Array(10).keys()].map(x=>e.jsx(z,{value:`avatar-${x+1}`,children:C("Avatar",{name:x+1})},`avatar-${x+1}`))]})]})})})]})}),e.jsx(te,{children:e.jsx(u.Subscribe,{selector:m=>[m.canSubmit,m.isSubmitting],children:([m])=>e.jsxs(e.Fragment,{children:[e.jsx(P,{variant:"outlined",onClick:()=>{l()},children:C("Cancel")}),e.jsx(P,{type:"submit",disabled:!m||p.isPending,autoFocus:!0,variant:"contained",endIcon:p.isPending&&e.jsx(Q,{color:"inherit",size:"20px"}),children:C("Save")})]})})}),c&&e.jsx(U,{children:e.jsx(k,{error:c,context:"userUpdate"})})]})},ke=({user:t,posts:n,canEdit:i})=>{const[c,r]=j.useState(!1),[d,h]=j.useState(!1),{t:s}=$();return e.jsxs(v,{container:!0,sx:{justifyContent:"center"},spacing:2,children:[e.jsx(v,{item:!0,children:e.jsxs(Ae,{children:[e.jsx(Te,{children:e.jsxs(v,{container:!0,spacing:2,children:[e.jsx(v,{item:!0,sx:{display:"flex",flexGrow:1,justifyContent:"center"},children:e.jsx(Z,{onClick:()=>h(!0),children:e.jsx(B,{alt:t.username,src:`${t.image}`,sx:{width:"100px",height:"100px"},children:e.jsx(_,{sx:{width:"60px",height:"60px"}})})})}),e.jsx(v,{item:!0,sx:{display:"flex",alignItems:"center"},children:e.jsxs(E,{sx:{display:"grid",gridTemplateColumns:"120px 1fr",columnGap:1},children:[e.jsxs(w,{fontWeight:"bold",children:[s("Username"),":"]}),e.jsx(w,{children:t.username}),e.jsxs(w,{fontWeight:"bold",children:[s("Email"),":"]}),e.jsx(w,{children:t.email}),e.jsxs(w,{fontWeight:"bold",children:[s("Member since"),":"]}),e.jsx(w,{children:Ie(t.memberSince)}),e.jsxs(w,{fontWeight:"bold",children:[s("Post count"),":"]}),e.jsx(w,{children:t.postCount})]})})]})}),e.jsx(De,{children:i&&e.jsxs(e.Fragment,{children:[e.jsx(P,{size:"small",onClick:()=>r(!0),children:s("Edit")}),e.jsx(Lt,{user:t,open:c,onClose:()=>r(!1)}),e.jsx(qt,{user:t,open:d,onClose:()=>h(!1)})]})})]})}),e.jsx(v,{item:!0,xs:12,children:e.jsx(ge,{variant:"middle",children:e.jsx(w,{sx:{opacity:.36},children:s("Recent posts")})})}),n.map(o=>e.jsx(v,{item:!0,xs:12,children:e.jsx($e,{post:o,disableActions:!0})},`post_${o.id}`))]})},Oe=t=>oe({queryKey:["profile"],queryFn:async()=>await t.user()}),Me=(t,n)=>oe({queryKey:["profile",{id:n}],queryFn:async()=>await t.user(n)}),ce=(t,n)=>oe({queryKey:["profilePosts",{id:n}],queryFn:async()=>await t.userPosts(n)}),kt=()=>{var h;const t=F(),{data:n,isFetching:i,failureReason:c}=X(Oe(t)),{data:r,isFetching:d}=X(ce(t,((h=t.authenticatedUser)==null?void 0:h.id)??0));if(c&&"code"in c&&c.code===D.UNAUTHORIZED)throw c;return e.jsxs(e.Fragment,{children:[e.jsx(W,{open:i||d,message:C("Updating")}),e.jsx(ke,{user:n,posts:r.data,canEdit:!0})]})},Ot=fe(`${I.PROFILE}/`)({loader:({context:{queryClient:t,Api:n}})=>{var i;t.ensureQueryData(Oe(n)),t.ensureQueryData(ce(n,((i=n.authenticatedUser)==null?void 0:i.id)??0))},beforeLoad:({context:{Api:t}})=>{if(!t.hasAuth)throw me({to:I.INDEX})},component:kt}),Mt=()=>{var s;const t=F(),{id:n}=Re.useParams(),{data:i,isFetching:c,failureReason:r}=X(Me(t,n)),{data:d,isFetching:h}=X(ce(t,n));if(r&&"code"in r&&r.code===D.UNAUTHORIZED)throw r;return e.jsxs(e.Fragment,{children:[e.jsx(W,{open:c||h,message:C("Updating")}),e.jsx(ke,{user:i,posts:d.data,canEdit:(s=t.authenticatedUser)==null?void 0:s.isAdmin})]})},Re=fe(`${I.PROFILE}/$id`)({params:{parse:({id:t})=>({id:parseInt(t)}),stringify:({id:t})=>({id:t.toString()})},loader:({context:{queryClient:t,Api:n},params:{id:i}})=>{t.ensureQueryData(Me(n,i)),t.ensureQueryData(ce(n,i))},beforeLoad:({params:{id:t},context:{Api:n}})=>{var i;if(!n.hasAuth)throw me({to:I.INDEX});if(t===((i=n.authenticatedUser)==null?void 0:i.id))throw me({to:I.PROFILE})},component:Mt}),Rt=qe.update({path:"/",getParentRoute:()=>ie}),Bt=Ot.update({path:"/profile/",getParentRoute:()=>ie}),_t=Re.update({path:"/profile/$id",getParentRoute:()=>ie}),zt=ie.addChildren({IndexRoute:Rt,ProfileIdRoute:_t,ProfileIndexRoute:Bt}),Nt=lt({routeTree:zt,context:{queryClient:void 0,Api:void 0},defaultPreload:"intent",defaultPreloadStaleTime:0,basepath:"/phpCourse/exam"}),Wt=()=>{const t=F(),n=G();return e.jsx(ut,{router:Nt,context:{queryClient:n,Api:t}})},Qt=new dt,Se=document.getElementById("root");Se.innerHTML||xe.createRoot(Se).render(e.jsx(j.StrictMode,{children:e.jsxs(ht,{client:Qt,children:[e.jsx(wt,{children:e.jsx(Wt,{})}),!1]})}));