2 lines
29 KiB
JavaScript
2 lines
29 KiB
JavaScript
import{j as e,M as ce,a as U,T as y,u as K,b as _,D as q,c as P,G as g,d as we,e as V,B as v,f as G,g as w,C as O,h as b,L as k,A as Pe,i as ae,I as W,k as Se,l as L,P as I,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 N,v as De,w as ne,x as ke,y as $e,z as Le,E as Ie,F as Ue,H as qe,J as Oe}from"./mui-C4H8cxTH.js";import{u as M,a as B,b as se,c as H,d as Me,e as Be,L as $,f as ze,O as Re,g as z,q as J,h as re,i as Q,r as ee,Q as We,j as Ne,k as Qe,R as Ke,l as _e}from"./tanstack-C0csOcmc.js";import{c as Ve,r as f}from"./react-C_FdcE2X.js";import{i as R,B as Ge,a as He,b as Je,u as F,t as j,T as Xe}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"]'))s(r);new MutationObserver(r=>{for(const l of r)if(l.type==="childList")for(const a of l.addedNodes)a.tagName==="LINK"&&a.rel==="modulepreload"&&s(a)}).observe(document,{childList:!0,subtree:!0});function o(r){const l={};return r.integrity&&(l.integrity=r.integrity),r.referrerPolicy&&(l.referrerPolicy=r.referrerPolicy),r.crossOrigin==="use-credentials"?l.credentials="include":r.crossOrigin==="anonymous"?l.credentials="omit":l.credentials="same-origin",l}function s(r){if(r.ep)return;r.ep=!0;const l=o(r);fetch(r.href,l)}})();var te={},ie=Ve;te.createRoot=ie.createRoot,te.hydrateRoot=ie.hydrateRoot;const Y=15,Ze=3,A="https://khofmann.userpage.fu-berlin.de/phpCourse/exam/api/";let oe;class Ye{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:s,token:r}=await(await this.post("login",{email:n,password:o})).json();this.self=s,this.token=r,this.userListeners.forEach(l=>l(s))},this.logOut=async()=>{try{return this.token?await(await this.postAuth("logout")).json():!0}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=${Y}`;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=${Y}`)).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 s=await(await this.patch(`users/${o??"self"}`,n)).json();return this.self=s,this.userListeners.forEach(r=>r(s)),s},this.updateUserImage=async(n,o)=>{const s=new FormData;n.image&&s.append("image",n.image),!n.image&&n.predefined&&s.append("predefined",n.predefined);const r=await(await this.postAuthRaw(`users/${o??"self"}/image`,s)).json();return this.self=r,this.userListeners.forEach(l=>l(r)),r},this.newPost=async n=>await(await this.postAuth(`posts?l=${Y}`,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=${Ze}&s=desc`)).json(),this.createUser=async n=>await(await this.post("register",n)).json(),this.post=async(n,o,s)=>{const r=await fetch(`${A}${n}`,{mode:"cors",method:"post",headers:s,body:JSON.stringify(o)});if(r.ok)return r;throw await r.json()},this.postAuth=async(n,o,s)=>{const r=await fetch(`${A}${n}`,{mode:"cors",method:"post",headers:{token:this.token??"",...s},body:JSON.stringify(o)});if(r.ok)return r;throw await r.json()},this.postAuthRaw=async(n,o,s)=>{const r=await fetch(`${A}${n}`,{mode:"cors",method:"post",headers:{token:this.token??"",...s},body:o});if(r.ok)return r;throw await r.json()},this.get=async(n,o)=>{const s=await fetch(`${A}${n}`,{mode:"cors",method:"get",headers:o});if(s.ok)return s;throw await s.json()},this.getAuth=async(n,o)=>{const s=await fetch(`${A}${n}`,{mode:"cors",method:"get",headers:{token:this.token??"",...o}});if(s.ok)return s;throw await s.json()},this.delete=async(n,o)=>{const s=await fetch(`${A}${n}`,{mode:"cors",method:"delete",headers:{token:this.token??"",...o}});if(s.ok)return s;throw await s.json()},this.patch=async(n,o,s)=>{const r=await fetch(`${A}${n}`,{mode:"cors",method:"patch",headers:{token:this.token??"",...s},body:JSON.stringify(o)});if(r.ok)return r;throw await r.json()},oe)throw new Error("New instance cannot be created!!");oe=this}}const p=new Ye;var E=(t=>(t.NOT_FOUND="NotFound",t.UNAUTHORIZED="Unauthorized",t.NOT_ALLOWED="NotAllowed",t.FAILED_UPDATE="FailedUpdate",t.MISSING_FIELD="MissingField",t.DUPLICATE="Duplicate",t))(E||{});R.use(Ge).use(He).use(Je).init({fallbackLng:"en",debug:!1,interpolation:{escapeValue:!1},backend:{loadPath:"/phpCourse/exam/dist/locales/{{lng}}/{{ns}}.json"}});const et=({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(U,{selected:R.language==="en",onClick:()=>{R.changeLanguage("en"),n()},children:"English"},"de"),e.jsx(U,{selected:R.language==="de",onClick:()=>{R.changeLanguage("de"),n()},children:"Deutsch"},"en")]});var S=(t=>(t.INDEX="/",t.PROFILE="/profile",t))(S||{});const T=({error:t,context:n,color:o="error.main"})=>{const{t:s}=F();if(!t)return null;if(t.code)switch(t.code){case E.NOT_FOUND:return e.jsx(y,{color:o,children:s(t.code,{context:`${t.entity}:${n}`})});case E.NOT_ALLOWED:return e.jsx(y,{color:o,children:s(t.code,{context:n})});case E.FAILED_UPDATE:return t.fields.map((r,l)=>e.jsx(y,{color:o,children:s(t.code,{context:`${t.reasons[l]}:${r}:${n}`})},`error_${r}`));case E.MISSING_FIELD:return t.fields.map(r=>e.jsx(y,{color:o,children:s(t.code,{context:`${r}:${n}`})},`error_${r}`));case E.DUPLICATE:return e.jsx(y,{color:o,children:s(t.code,{context:`${t.entity}:${n}`})})}return e.jsx(y,{color:o,children:s((t==null?void 0:t.message)??"Unknown",{context:n})})},tt=({open:t,onClose:n})=>{const[o,s]=f.useState(),r=M({mutationFn:({data:i})=>p.createUser(i)}),{t:l}=F(),a=K(),m=_(a.breakpoints.only("xs"),{noSsr:!0}),x=B({defaultValues:{username:"",email:"",password:""},onSubmit:async({value:i})=>{try{r.mutate({data:i},{onSuccess:()=>s(void 0),onError:s})}catch(c){s(c)}}}),u=()=>{x.reset(),s(void 0),n()};return r.isSuccess?e.jsxs(q,{open:t,onClose:u,fullWidth:!0,fullScreen:m,children:[e.jsx(P,{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(V,{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(G,{children:l("Register")}),e.jsx(P,{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(w,{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(w,{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(w,{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||r.isPending,variant:"contained",endIcon:r.isPending&&e.jsx(O,{color:"inherit",size:"20px"}),children:l("Register")})})})}),e.jsx(g,{item:!0,xs:12,children:o&&e.jsx(T,{error:o,context:"register"})})]})})]})},nt=({handleClose:t})=>{const[n,o]=f.useState(),{t:s}=F(),r=se(),l=B({defaultValues:{email:"",password:""},onSubmit:async({value:a})=>{try{await p.logIn(a.email,a.password),r.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(b,{sx:{display:"grid",gap:2,padding:1},children:[e.jsx(l.Field,{name:"email",validators:{onChange:({value:a})=>a?void 0:s("Email required"),onChangeAsyncDebounceMs:250,onChangeAsync:async({value:a})=>!a&&s("Email required")},children:a=>e.jsx(e.Fragment,{children:e.jsx(w,{variant:"outlined",name:a.name,value:a.state.value,onBlur:a.handleBlur,onChange:m=>a.handleChange(m.target.value),size:"small",label:s("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:s("Password required"),onChangeAsyncDebounceMs:250,onChangeAsync:async({value:a})=>!a&&s("Password required")},children:a=>e.jsx(e.Fragment,{children:e.jsx(w,{variant:"outlined",name:a.name,value:a.state.value,onBlur:a.handleBlur,onChange:m=>a.handleChange(m.target.value),size:"small",label:s("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:s("Log in")})})}),n&&e.jsx(T,{error:n,context:"login"})]})})},st=({anchorEl:t,handleClose:n})=>{const[o,s]=f.useState(!1),r=H(),l=se(),a=Me({from:"/profile/",strict:!0,shouldThrow:!1}),m=p.getAuthenticatedUser(),x=()=>{s(!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(U,{selected:!!a,onClick:()=>{r({to:S.PROFILE}),x()},children:j("Profile")},"profile"),e.jsx(U,{onClick:async()=>{await p.logOut(),l.invalidate(),x()},children:j("Log out")},"logout")]:o?e.jsx(tt,{open:o,onClose:()=>s(!1)}):e.jsxs(b,{children:[e.jsx(nt,{handleClose:x}),e.jsx(b,{sx:{padding:1},children:e.jsxs(Xe,{i18nKey:"Register prompt",children:[e.jsx(y,{component:"span"}),e.jsx(k,{sx:{cursor:"pointer"},variant:"body1",underline:"hover",onClick:()=>{s(!0),n()}}),e.jsx(y,{component:"span"})]})})]})})},rt=({children:t})=>{const n=Ae({disableHysteresis:!0,threshold:0});return f.cloneElement(t,{elevation:n?4:0})},at=()=>{const[t,n]=f.useState(null),[o,s]=f.useState(null),[r,l]=f.useState(),{t:a}=F(),m=Be({select:u=>u.status==="pending"});f.useEffect(()=>p.subscribeToAuthenticatedUser(u=>l(u)),[]);const x=()=>{s(null),n(null)};return e.jsx(rt,{children:e.jsxs(e.Fragment,{children:[e.jsx(Pe,{children:e.jsxs(ae,{children:[e.jsxs(b,{sx:{flexGrow:1,alignItems:"center",display:"flex",gap:1},children:[e.jsx(k,{component:$,to:"/",color:"#FFF",variant:"h6",underline:"none",children:a("GuestBook")}),m&&e.jsx(O,{size:16,thickness:10,sx:{color:"white"}})]}),e.jsxs(b,{sx:{display:"flex",gap:1},children:[e.jsx(W,{size:"large",onClick:u=>s(u.currentTarget),children:e.jsx(Se,{sx:{color:"white"}})}),r?e.jsx(W,{onClick:u=>n(u.currentTarget),sx:{p:0},children:e.jsx(L,{alt:r.username,src:`${r.image}`,children:e.jsx(I,{})})}):e.jsx(W,{size:"large",onClick:u=>n(u.currentTarget),color:"inherit",children:e.jsx(Fe,{})})]}),e.jsx(et,{anchorEl:o,handleClose:x}),e.jsx(st,{anchorEl:t,handleClose:x})]})}),e.jsx(ae,{})]})})},it=()=>e.jsxs(e.Fragment,{children:[e.jsx(at,{}),e.jsx(b,{sx:{display:"flex",justifyContent:"center"},children:e.jsx(b,{sx:{maxWidth:"800px",flexGrow:1},children:e.jsx(Re,{})})}),!1]}),X=ze()({component:it}),ot=()=>{const[t,n]=f.useState(),[o,s]=f.useState(0),{t:r}=F(),l=z(),a=H(),m=M({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(b,{sx:{display:"grid",gap:2,padding:1},children:[e.jsx(x.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(w,{variant:"outlined",multiline:!0,minRows:3,name:u.name,value:u.state.value,onBlur:u.handleBlur,onChange:i=>{i.target.value.length<=250&&(s(i.target.value.length),u.handleChange(i.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(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(O,{color:"inherit",size:"20px"}),children:r("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"}),lt=({post:t,open:n,onClose:o})=>{const[s,r]=f.useState(),[l,a]=f.useState(t.content.length),m=M({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:r})}catch(C){r(C)}}}),u=K(),i=_(u.breakpoints.only("xs"),{noSsr:!0}),c=z(),h=()=>{x.reset(),r(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(G,{children:j("Edit post")}),e.jsx(P,{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(w,{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(V,{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(O,{color:"inherit",size:"20px"}),children:j("Save")})]})})}),s&&e.jsx(P,{children:e.jsx(T,{error:s,context:"postUpdate"})})]})},pe=({post:t,disableActions:n})=>{var h,d,C;const[o,s]=f.useState(!1),[r,l]=f.useState(!1),[a,m]=f.useState(),x=M({mutationFn:D=>p.deletePost(D)}),{t:u}=F(),i=z(),c=H();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:$,to:"/profile/$id",params:{id:t.user.id},children:e.jsx(L,{alt:t.user.username,src:`${t.user.image}`,children:e.jsx(I,{})})}):e.jsx(k,{component:$,to:"/profile",children:e.jsx(L,{alt:t.user.username,src:`${t.user.image}`,children:e.jsx(I,{})})}):e.jsx(L,{alt:t.user.username,src:`${t.user.image}`,children:e.jsx(I,{})}),title:!n&&"id"in t.user?t.user.id!==((d=p.getAuthenticatedUser())==null?void 0:d.id)?e.jsx(k,{component:$,to:"/profile/$id",params:{id:t.user.id},children:t.user.username}):e.jsx(k,{component:$,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(lt,{post:t,open:r,onClose:()=>l(!1)})]}),!n&&p.isAdmin()&&e.jsxs(e.Fragment,{children:[e.jsx(v,{size:"small",color:"error",onClick:()=>s(!0),children:u("Delete")}),e.jsxs(q,{open:o,onClose:()=>s(!1),children:[e.jsx(G,{children:u("Confirm post delete title")}),e.jsx(P,{children:e.jsx(Te,{children:u("Confirm post delete body",{name:t.user.username})})}),e.jsxs(V,{children:[e.jsx(v,{onClick:()=>s(!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}),s(!1)},children:u("Yes")})]})]})]})]}),e.jsx(N,{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(N,{open:x.isPending,message:u("Deleting")})]})},ge=t=>J({queryKey:["posts",{page:t??0,hasAuth:p.hasAuth()}],queryFn:()=>p.posts(t)}),ct=()=>{const{page:t}=je.useSearch(),{data:n,isFetching:o}=Q(ge(t)),{t:s}=F(),r=H(),l=K(),a=_(l.breakpoints.not("xs"),{noSsr:!0});return f.useEffect(()=>{(t??0)>=n.pages&&r({to:"/",search:{page:n.pages-1}})},[t]),e.jsxs(e.Fragment,{children:[e.jsx(N,{open:o,message:s("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($e,{...m,component:$,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(ot,{})}):e.jsxs(g,{item:!0,xs:12,children:[e.jsx(y,{variant:"h5",children:s("Leave comment header")}),e.jsx(y,{children:s("Leave comment action")})]})]})]})},je=re(S.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:ct}),ut=({user:t,open:n,onClose:o})=>{const[s,r]=f.useState(),l=M({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:r})}catch(d){r(d)}}}),m=K(),x=_(m.breakpoints.only("xs"),{noSsr:!0}),u=z(),i=()=>{a.reset(),r(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(G,{children:j("Edit data")}),e.jsxs(P,{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(w,{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(w,{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(V,{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(O,{color:"inherit",size:"20px"}),children:j("Save")})]})})}),s&&e.jsx(P,{children:e.jsx(T,{error:s,context:"userUpdate"})})]})},dt=({user:t,open:n,onClose:o})=>{const[s,r]=f.useState(),l=M({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:r})}catch(C){r(C)}}}),m=K(),x=_(m.breakpoints.only("xs"),{noSsr:!0}),u=z(),i=a.useStore(h=>({image:h.values.image,predefined:h.values.predefined})),c=()=>{a.reset(),r(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(G,{children:j("Edit image")}),e.jsx(P,{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(L,{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(I,{sx:{width:"60px",height:"60px"}})})}),e.jsx(g,{item:!0,xs:12,children:e.jsxs(b,{sx:{display:"flex",gap:2},children:[e.jsx(v,{component:"label",role:void 0,variant:"contained",tabIndex:-1,startIcon:e.jsx(Le,{}),fullWidth:!0,children:e.jsx(a.Field,{name:"image",children:h=>{var d;return e.jsxs(e.Fragment,{children:[e.jsx(b,{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(w,{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(W,{color:"error",onClick:()=>a.setFieldValue("image",void 0),children:e.jsx(Ie,{})})]})}),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(Ue,{fullWidth:!0,children:[e.jsx(qe,{size:"small",children:j("Predefined")}),e.jsxs(Oe,{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(U,{value:"",selected:!0,children:"Keine Auswahl"}),[...Array(10).keys()].map(d=>e.jsx(U,{value:`avatar-${d+1}`,children:j("Avatar",{name:d+1})},`avatar-${d+1}`))]})]})})})]})}),e.jsx(V,{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(O,{color:"inherit",size:"20px"}),children:j("Save")})]})})}),s&&e.jsx(P,{children:e.jsx(T,{error:s,context:"userUpdate"})})]})},ye=({user:t,posts:n,canEdit:o})=>{const[s,r]=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(W,{onClick:()=>a(!0),children:e.jsx(L,{alt:t.username,src:`${t.image}`,sx:{width:"100px",height:"100px"},children:e.jsx(I,{sx:{width:"60px",height:"60px"}})})})}),e.jsx(g,{item:!0,sx:{display:"flex",alignItems:"center"},children:e.jsxs(b,{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:()=>r(!0),children:m("Edit")}),e.jsx(ut,{user:t,open:s,onClose:()=>r(!1)}),e.jsx(dt,{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)}),Z=t=>J({queryKey:["profilePosts",{id:t}],queryFn:()=>p.userPosts(t)}),ht=()=>{var r;const{data:t,isFetching:n}=Q(fe),{data:o,isFetching:s}=Q(Z(((r=p.getAuthenticatedUser())==null?void 0:r.id)??0));return e.jsxs(e.Fragment,{children:[e.jsx(N,{open:n||s,message:j("Updating")}),e.jsx(ye,{user:t,posts:o.data,canEdit:!0})]})},mt=re(`${S.PROFILE}/`)({loader:({context:{queryClient:t}})=>{var n;t.ensureQueryData(fe),t.ensureQueryData(Z(((n=p.getAuthenticatedUser())==null?void 0:n.id)??0))},beforeLoad:()=>{if(!p.hasAuth())throw ee({to:S.INDEX})},component:ht}),xt=()=>{const{id:t}=ve.useParams(),{data:n,isFetching:o}=Q(Ce(t)),{data:s,isFetching:r}=Q(Z(t));return e.jsxs(e.Fragment,{children:[e.jsx(N,{open:o||r,message:j("Updating")}),e.jsx(ye,{user:n,posts:s.data,canEdit:p.isAdmin()})]})},ve=re(`${S.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(Z(n))},beforeLoad:({params:{id:t}})=>{var n;if(!p.hasAuth())throw ee({to:S.INDEX});if(t===((n=p.getAuthenticatedUser())==null?void 0:n.id))throw ee({to:S.PROFILE})},component:xt}),pt=je.update({path:"/",getParentRoute:()=>X}),gt=mt.update({path:"/profile/",getParentRoute:()=>X}),jt=ve.update({path:"/profile/$id",getParentRoute:()=>X}),yt=X.addChildren({IndexRoute:pt,ProfileIdRoute:jt,ProfileIndexRoute:gt}),be=new We,ft=({error:t})=>{const n=se(),o=_e(),s=z();return f.useEffect(()=>{o.reset()},[o]),"code"in t&&t.code===E.NOT_ALLOWED&&p.logOut().finally(()=>{s.clear(),n.invalidate()}),e.jsxs("div",{children:[t.message,e.jsx("button",{onClick:()=>{n.invalidate()},children:"retry"})]})},Ct=Ne({routeTree:yt,context:{queryClient:be},defaultPreload:"intent",defaultPreloadStaleTime:0,basepath:"/phpCourse/exam",defaultErrorComponent:ft}),le=document.getElementById("root");le.innerHTML||te.createRoot(le).render(e.jsx(f.StrictMode,{children:e.jsxs(Qe,{client:be,children:[e.jsx(Ke,{router:Ct}),!1]})}));
|