PHP-Course/exam/dist/assets/index-tFNBNNKb.js

2 lines
29 KiB
JavaScript

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