(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[405],{8312:function(e,o,n){(window.__NEXT_P=window.__NEXT_P||[]).push(["/",function(){return n(5124)}])},5124:function(e,o,n){"use strict";n.r(o),n.d(o,{default:function(){return z}});var t=n(5893),r=n(9008),l=n.n(r),i=n(6312),s=n(4523),a=n(1017),d=n(4403),c=n(9236),u=n(5117),h=n(7564),v=n(50),m=n(9876),p=n(7841),x=n(7688),j=n(7536),w=n(1604),g=n(61),b=function(e){let{name:o,control:n,...r}=e;return(0,t.jsx)(j.Qr,{name:o,control:n,render(e){var o;let{field:n,fieldState:l}=e;return(0,t.jsx)(g.o,{...r,error:null===(o=l.error)||void 0===o?void 0:o.message,value:n.value,onChange:e=>n.onChange(e)})}})},f=n(8228),_=n(9332),k=n(7294),y=n(3991),C=n(1163);let E=w.z.object({email:w.z.string().email(),password:w.z.string(),host:w.z.string(),port:w.z.preprocess(Number,w.z.number()).optional()});function S(){var e;let o=(0,_.useSearchParams)(),[n,r]=(0,k.useState)(),l=(0,C.useRouter)(),{isLoading:w,error:g,data:S,mutateAsync:z}=(0,f.D)({mutationFn(e){let{serverUrl:o,email:n,password:t}=e;return fetch("".concat(o,"/user/token/"),{method:"POST",body:JSON.stringify({email:n,password:t}),headers:{"Content-Type":"application/json"}}).then(e=>e.json())}}),{control:P,handleSubmit:N,setValue:q}=(0,j.cI)({resolver:(0,i.F)(E)});async function I(e){let o=e.host.startsWith("https://")||e.host.startsWith("http://"),t="".concat(o?"":"http://").concat(e.host).concat(e.port?":".concat(e.port):"");await z({serverUrl:t,email:e.email,password:e.password},{onSuccess(e){"connect"===n&&e.token&&(localStorage.setItem("token",e.token),localStorage.setItem("serverUrl",t),l.push("/dashboard"))}})}return(0,k.useEffect)(()=>{if(window.django){var e,o,n,t,r;(null===(e=window.location)||void 0===e?void 0:e.port)&&q("port",window.location.port),(null===(o=window.location)||void 0===o?void 0:o.hostname)&&q("host",window.location.hostname),(null===(n=window.django)||void 0===n?void 0:null===(t=n.user)||void 0===t?void 0:t.includes("@"))&&q("email",null===(r=window.django)||void 0===r?void 0:r.user)}},[]),(0,k.useEffect)(()=>{var e,o;(null==l?void 0:null===(e=l.query)||void 0===e?void 0:e.token)&&window.django&&(localStorage.setItem("token",null==l?void 0:null===(o=l.query)||void 0===o?void 0:o.token),localStorage.setItem("serverUrl",window.location.origin),l.push("/dashboard"))},[null==l?void 0:null===(e=l.query)||void 0===e?void 0:e.token]),(0,t.jsx)(s.x,{sx:{display:"flex",alignItems:"center",height:"100vh"},children:(0,t.jsxs)(a.Z,{withBorder:!0,shadow:"md",p:30,radius:"md",sx:{width:"95%",maxWidth:"600px",margin:"0 auto"},children:[(null==o?void 0:o.get("redirected"))&&(null==o?void 0:o.get("reason"))==="not_activated"&&(0,t.jsx)(d.b,{mb:"md",color:"red","aria-label":"Result",children:"Your account is not activated. Please check your email for activation link."}),(null==o?void 0:o.get("redirected"))&&(null==o?void 0:o.get("reason"))==="not_superuser"&&(0,t.jsx)(d.b,{mb:"md",icon:(0,t.jsx)(y.s3j,{}),color:"red","aria-label":"Result",children:"You are not a superuser. Please contact your administrator."}),(0,t.jsxs)("form",{onSubmit:N(I),children:[(0,t.jsxs)(s.x,{mb:"xl",children:[(0,t.jsx)(c.D,{children:"Login"}),(0,t.jsx)(u.x,{color:"dimmed",children:"Connect to a server to start"})]}),(0,t.jsxs)(h.K,{children:[(0,t.jsxs)(v.r,{sx:{width:"100%",display:"flex"},columns:6,children:[(0,t.jsx)(v.r.Col,{span:4,children:(0,t.jsx)(b,{required:!0,control:P,label:"Host",name:"host",placeholder:"Enter host e.g localhost"})}),(0,t.jsx)(v.r.Col,{span:2,children:(0,t.jsx)(b,{placeholder:"Enter port e.g 8888",name:"port",control:P,label:"Port"})})]}),(0,t.jsx)(b,{required:!0,control:P,name:"email",placeholder:"Enter your email",label:"Email"}),(0,t.jsx)(b,{control:P,required:!0,placeholder:"Enter your password",name:"password",type:"password",label:"Password"}),(0,t.jsxs)(m.k,{justify:"space-between",mt:"lg",children:[(0,t.jsx)(p.z,{loading:w,type:"submit",onClick:()=>r("test"),color:"gray",children:"Test Connection"}),(0,t.jsx)(p.z,{loading:w,type:"submit",onClick:()=>r("connect"),color:"teal","aria-label":"Connect to Server",children:"Connect"})]}),(0,t.jsx)(x.u,{mounted:(null==S?void 0:S.token)||(null==S?void 0:S.non_field_errors)||g,transition:"fade",duration:400,timingFunction:"ease",children:e=>(0,t.jsxs)("div",{style:e,children:[(null==S?void 0:S.token)&&(0,t.jsx)(d.b,{color:"green","aria-label":"Result",children:"Connection successful"}),(null==S?void 0:S.non_field_errors)&&(0,t.jsx)(d.b,{color:"red",children:null==S?void 0:S.non_field_errors[0]}),g&&(0,t.jsx)(d.b,{color:"red",children:"Unable to established connection"})]})})]})]})]})})}var z=function(){return(0,t.jsxs)("div",{children:[(0,t.jsx)(l(),{children:(0,t.jsx)("title",{children:"Login"})}),(0,t.jsx)(S,{})]})}}},function(e){e.O(0,[76,113,774,888,179],function(){return e(e.s=8312)}),_N_E=e.O()}]);