feat: move guest toggler from login method to overview

This commit is contained in:
Tristan Yang
2022-09-02 21:12:26 +08:00
parent c4435662c0
commit 54ad5a8511
2 changed files with 57 additions and 92 deletions
+55 -66
View File
@@ -1,12 +1,7 @@
import { useState, useEffect, ChangeEvent } from "react"; import { useState, useEffect, ChangeEvent } from "react";
import styled from "styled-components"; import styled from "styled-components";
import toast from "react-hot-toast"; import toast from "react-hot-toast";
import { import { useUpdateServerMutation, useUpdateLogoMutation } from "../../app/services/server";
useUpdateServerMutation,
useUpdateLogoMutation,
useUpdateLoginConfigMutation,
useGetLoginConfigQuery
} from "../../app/services/server";
import LogoUploader from "../../common/component/AvatarUploader"; import LogoUploader from "../../common/component/AvatarUploader";
import Input from "../../common/component/styled/Input"; import Input from "../../common/component/styled/Input";
import Label from "../../common/component/styled/Label"; import Label from "../../common/component/styled/Label";
@@ -15,6 +10,8 @@ import SaveTip from "../../common/component/SaveTip";
import StyledRadio from "../../common/component/styled/Radio"; import StyledRadio from "../../common/component/styled/Radio";
import { useAppSelector } from "../../app/store"; import { useAppSelector } from "../../app/store";
import { LoginConfig, WhoCanSignUp } from "../../types/server"; import { LoginConfig, WhoCanSignUp } from "../../types/server";
import Toggle from "../../common/component/styled/Toggle";
import useConfig from "../../common/hook/useConfig";
const StyledWrapper = styled.div` const StyledWrapper = styled.div`
position: relative; position: relative;
@@ -60,6 +57,7 @@ const StyledWrapper = styled.div`
flex-direction: column; flex-direction: column;
align-items: flex-start; align-items: flex-start;
gap: 24px; gap: 24px;
margin-bottom: 64px;
.input { .input {
width: 100%; width: 100%;
display: flex; display: flex;
@@ -67,23 +65,23 @@ const StyledWrapper = styled.div`
align-items: flex-start; align-items: flex-start;
gap: 8px; gap: 8px;
} }
> .radioButton { }
> .label { > .setting {
margin-top: 64px; font-size: 14px;
font-weight: 500; line-height: 20px;
font-size: 14px; > .label {
line-height: 20px; font-weight: 500;
} }
> .tip { > .tip {
font-weight: 400; font-weight: 400;
font-size: 14px; color: #667085;
line-height: 20px; display: flex;
color: #667085; width: 100%;
} justify-content: space-between;
> form { }
margin-top: 16px; > form {
width: 512px; margin-top: 16px;
} width: 512px;
} }
} }
`; `;
@@ -94,20 +92,15 @@ export default function Overview() {
}); });
const [changed, setChanged] = useState(false); const [changed, setChanged] = useState(false);
const [serverValues, setServerValues] = useState<typeof server>(server); const [serverValues, setServerValues] = useState<typeof server>(server);
const [loginConfigValues, setLoginConfigValues] = useState<Partial<LoginConfig>>(); const { values: loginConfig, updateConfig: updateLoginConfig } = useConfig("login");
const { data: loginConfig, refetch: refetchLoginConfig } = useGetLoginConfigQuery(); const [updateServer] = useUpdateServerMutation();
const [updateServer, { isSuccess: serverUpdated }] = useUpdateServerMutation();
const [uploadLogo, { isSuccess: uploadSuccess }] = useUpdateLogoMutation(); const [uploadLogo, { isSuccess: uploadSuccess }] = useUpdateLogoMutation();
const [updateLoginConfig, { isSuccess: loginConfigUpdated }] = useUpdateLoginConfigMutation(); const handleUpdateServer = () => {
const handleUpdate = () => {
const { name, description } = serverValues; const { name, description } = serverValues;
updateServer({ name, description }); updateServer({ name, description });
if (loginUser?.is_admin && loginConfigValues?.who_can_sign_up) { };
updateLoginConfig({ const handleUpdateWhoCanSignUp = (value: WhoCanSignUp) => {
...loginConfig, updateLoginConfig({ ...loginConfig, who_can_sign_up: value });
who_can_sign_up: loginConfigValues.who_can_sign_up
});
}
}; };
const handleChange = (evt: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => { const handleChange = (evt: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
const newValue = evt.target.value; const newValue = evt.target.value;
@@ -118,7 +111,9 @@ export default function Overview() {
}; };
const handleReset = () => { const handleReset = () => {
setServerValues(server); setServerValues(server);
setLoginConfigValues(loginConfig); };
const handleGuestToggle = (val: Partial<Pick<LoginConfig, "guest">>) => {
updateLoginConfig({ ...loginConfig, ...val });
}; };
useEffect(() => { useEffect(() => {
if (uploadSuccess) { if (uploadSuccess) {
@@ -131,33 +126,19 @@ export default function Overview() {
} }
}, [server]); }, [server]);
useEffect(() => { useEffect(() => {
if (loginConfig) { if (server && serverValues) {
setLoginConfigValues(loginConfig);
}
}, [loginConfig]);
useEffect(() => {
if (server && serverValues && loginConfig && loginConfigValues) {
const { name, description } = serverValues; const { name, description } = serverValues;
const { name: oName, description: oDescription } = server; const { name: oName, description: oDescription } = server;
const { who_can_sign_up: whoCanSignUp } = loginConfigValues; if (oName !== name || oDescription !== description) {
const { who_can_sign_up: oWhoCanSignUp } = loginConfig;
if (oName !== name || oDescription !== description || oWhoCanSignUp !== whoCanSignUp) {
setChanged(true); setChanged(true);
} else { } else {
setChanged(false); setChanged(false);
} }
} }
}, [server, serverValues, loginConfig, loginConfigValues]); }, [server, serverValues]);
useEffect(() => { if (!serverValues || !loginConfig) return null;
if (serverUpdated && loginConfigUpdated) {
toast.success("Configuration updated!");
refetchLoginConfig();
}
}, [serverUpdated, loginConfigUpdated]);
if (!serverValues || !loginConfigValues) return null;
const { name, description, logo } = serverValues; const { name, description, logo } = serverValues;
const { who_can_sign_up: whoCanSignUp } = loginConfigValues; const { who_can_sign_up: whoCanSignUp, guest } = loginConfig as LoginConfig;
const isAdmin = loginUser?.is_admin; const isAdmin = loginUser?.is_admin;
return ( return (
@@ -207,26 +188,34 @@ export default function Overview() {
placeholder="Tell the world a bit about this server" placeholder="Tell the world a bit about this server"
/> />
</div> </div>
{isAdmin && ( </div>
<div className="radioButton"> {isAdmin && (
<>
<div className="setting">
<p className="label">Default Sign Up</p> <p className="label">Default Sign Up</p>
<p className="tip">Who can sign up this server.</p> <p className="tip">Who can sign up this server.</p>
<StyledRadio <StyledRadio
options={["Everyone", "Invitation Link Only"]} options={["Everyone", "Invitation Link Only"]}
values={["EveryOne", "InvitationOnly"]} values={["EveryOne", "InvitationOnly"]}
value={whoCanSignUp} value={whoCanSignUp}
onChange={(v: WhoCanSignUp) => onChange={(v: WhoCanSignUp) => {
setLoginConfigValues({ handleUpdateWhoCanSignUp(v);
...loginConfig, }}
who_can_sign_up: v
})
}
/> />
</div> </div>
)} <div className="setting">
</div> <p className="label">Guest Mode</p>
{changed && <SaveTip saveHandler={handleUpdate} resetHandler={handleReset} />} <div className="tip">
{/* <button onClick={handleUpdate} className="btn">update</button> */} <span className="txt">Allow Guest visiting homepage.</span>
<Toggle
onClick={handleGuestToggle.bind(null, { guest: !guest })}
data-checked={guest}
></Toggle>
</div>
</div>
</>
)}
{changed && <SaveTip saveHandler={handleUpdateServer} resetHandler={handleReset} />}
</StyledWrapper> </StyledWrapper>
); );
} }
+2 -26
View File
@@ -56,9 +56,7 @@ export default function Logins() {
} }
}; };
const handleToggle = ( const handleToggle = (
val: Partial< val: Partial<Pick<LoginConfig, "github" | "google" | "password" | "magic_link" | "metamask">>
Pick<LoginConfig, "github" | "google" | "guest" | "password" | "magic_link" | "metamask">
>
) => { ) => {
setValues((prev) => { setValues((prev) => {
if (!prev) return prev; if (!prev) return prev;
@@ -66,34 +64,12 @@ export default function Logins() {
}); });
}; };
if (!values) return null; if (!values) return null;
const { const { google, magic_link, github, metamask, password, oidc = [] } = values as LoginConfig;
google,
magic_link,
github,
metamask,
password,
guest,
oidc = []
} = values as LoginConfig;
const valuesChanged = clientIdChanged || changed || githubChanged; const valuesChanged = clientIdChanged || changed || githubChanged;
return ( return (
<StyledContainer> <StyledContainer>
<div className="inputs"> <div className="inputs">
<div className="input">
<div className="row">
<div className="title">
<div className="txt">
<Label>Guest Mode</Label>
</div>
<span className="desc">Allows guest to access</span>
</div>
<Toggle
onClick={handleToggle.bind(null, { guest: !guest })}
data-checked={guest}
></Toggle>
</div>
</div>
<div className="input"> <div className="input">
<div className="row"> <div className="row">
<div className="title"> <div className="title">