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