Skip to content

Commit

Permalink
refactor: reg page
Browse files Browse the repository at this point in the history
  • Loading branch information
zerosoul committed Jun 20, 2022
1 parent b938b6b commit aca0afe
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 72 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
"emmet.triggerExpansionOnTab": true,
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
},
"cSpell.words": ["btns"]
}
4 changes: 2 additions & 2 deletions src/common/component/Manifest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Prompt from "./Prompt";
import usePrompt from "./usePrompt";

export default function Manifest() {
const { setCanneled, prompted } = usePrompt();
const { setCanceled: setCanceled, prompted } = usePrompt();
const deferredPromptRef = useRef(null);
const [popup, setPopup] = useState(false);
// const { data, isSuccess } = useGetServerQuery();
Expand Down Expand Up @@ -59,7 +59,7 @@ export default function Manifest() {
deferredPromptRef.current = null;
};
const handleClose = async () => {
setCanneled();
setCanceled();
setPopup(false);
};
if (!popup || prompted) return null;
Expand Down
2 changes: 1 addition & 1 deletion src/common/component/Manifest/usePrompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default function usePrompt() {
};

return {
setCanneled: setPrompt,
setCanceled: setPrompt,
prompted: !!localStorage.getItem(KEY_PWA_INSTALLED),
resetPrompt
};
Expand Down
101 changes: 72 additions & 29 deletions src/routes/reg/Register.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { useState, useEffect } from "react";
// import { useDispatch } from "react-redux";
import toast from "react-hot-toast";
import { useNavigate } from "react-router-dom";
// import toast from "react-hot-toast";
import BASE_URL from "../../app/config";
import Input from "../../common/component/styled/Input";
import Button from "../../common/component/styled/Button";
import { useSendRegMagicLinkMutation } from "../../app/services/auth";
import EmailNextTip from "./EmailNextStepTip";
// import { useSendLoginMagicLinkMutation } from "../../app/services/auth";
import SignInLink from "./SignInLink";
import { useGetLoginConfigQuery } from "../../app/services/server";
import useGithubAuthConfig from "../../common/hook/useGithubAuthConfig";
import useGoogleAuthConfig from "../../common/hook/useGoogleAuthConfig";
import GoogleLoginButton from "../../common/component/GoogleLoginButton";
import GithubLoginButton from "../../common/component/GithubLoginButton";

export default function Reg() {
const [sendRegMagicLink, { isLoading, data, isSuccess }] = useSendRegMagicLinkMutation();
Expand Down Expand Up @@ -45,7 +50,12 @@ export default function Reg() {
});
// sendMagicLink(email);
};

const handleCompare = () => {
const { password, confirmPassword } = input;
if (password !== confirmPassword) {
toast.error("Not Same Password!");
}
};
const handleInput = (evt) => {
const { type } = evt.target.dataset;
const { value } = evt.target;
Expand All @@ -55,32 +65,65 @@ export default function Reg() {
return { ...prev };
});
};
const { email, password } = input;
const { clientId } = useGoogleAuthConfig();
const { config: githubAuthConfig } = useGithubAuthConfig();
const { data: loginConfig, isSuccess: loginConfigSuccess } = useGetLoginConfigQuery();
if (!loginConfigSuccess) return null;
const {
github: enableGithubLogin,
google: enableGoogleLogin,
who_can_sign_up: whoCanSignUp
} = loginConfig;
const googleLogin = enableGoogleLogin && clientId;
// 没有开放注册
if (whoCanSignUp !== "EveryOne") return `Open Register is Closed!`;
const { email, password, confirmPassword } = input;
if (data?.mail_is_sent) return <EmailNextTip />;
return (
<form onSubmit={handleReg}>
<Input
className="large"
name="email"
value={email}
required
placeholder="Enter your email"
data-type="email"
onChange={handleInput}
/>
<Input
className="large"
type="password"
value={password}
name="password"
required
data-type="password"
onChange={handleInput}
placeholder="Enter your password"
/>
<Button type="submit" disabled={isLoading}>
{isLoading ? "Signing Up" : `Sign Up`}
</Button>
</form>
<>
<div className="tips">
<img src={`${BASE_URL}/resource/organization/logo`} alt="logo" className="logo" />
<h2 className="title">Sign Up to Rustchat</h2>
<span className="desc">Please enter your details.</span>
</div>

<form onSubmit={handleReg}>
<Input
className="large"
name="email"
value={email}
required
placeholder="Enter email"
data-type="email"
onChange={handleInput}
/>
<Input
className="large"
type="password"
value={password}
name="password"
required
data-type="password"
onChange={handleInput}
placeholder="Enter password"
/>
<Input
onBlur={handleCompare}
type="password"
name={"confirmPassword"}
value={confirmPassword}
data-type="confirmPassword"
onChange={handleInput}
placeholder="Confirm Password"
></Input>
<Button type="submit" disabled={isLoading}>
{isLoading ? "Signing Up" : `Sign Up`}
</Button>
</form>
<hr className="or" />
{googleLogin && <GoogleLoginButton clientId={clientId} />}
{enableGithubLogin && <GithubLoginButton config={githubAuthConfig} />}
<SignInLink />
</>
);
}
44 changes: 5 additions & 39 deletions src/routes/reg/index.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,15 @@
import { Outlet } from "react-router-dom";
import { useMatch } from "react-router-dom";
import BASE_URL from "../../app/config";
import SignInLink from "./SignInLink";
import { useGetLoginConfigQuery } from "../../app/services/server";
import useGithubAuthConfig from "../../common/hook/useGithubAuthConfig";
import useGoogleAuthConfig from "../../common/hook/useGoogleAuthConfig";
import GoogleLoginButton from "../../common/component/GoogleLoginButton";
import GithubLoginButton from "../../common/component/GithubLoginButton";
// import { useMatch } from "react-router-dom";

import StyledWrapper from "./styled";

export default function Reg() {
const isRegHome = useMatch(`/register`);
const { clientId } = useGoogleAuthConfig();
const { config: githubAuthConfig } = useGithubAuthConfig();
const { data: loginConfig, isSuccess: loginConfigSuccess } = useGetLoginConfigQuery();
if (!loginConfigSuccess) return null;
const {
github: enableGithubLogin,
google: enableGoogleLogin,
who_can_sign_up: whoCanSignUp
} = loginConfig;
const googleLogin = enableGoogleLogin && clientId;
// 没有开放注册
if (whoCanSignUp !== "EveryOne") return `Open Register is Closed!`;
export default function RegContainer() {
// const isRegHome = useMatch(`/register`);

return (
<StyledWrapper>
<div className="form">
{isRegHome && (
<>
<div className="tips">
<img src={`${BASE_URL}/resource/organization/logo`} alt="logo" className="logo" />
<h2 className="title">Sign Up to Rustchat</h2>
<span className="desc">Please enter your details.</span>
</div>
</>
)}
<Outlet />
{isRegHome && (
<>
<hr className="or" />
{googleLogin && <GoogleLoginButton clientId={clientId} />}
{enableGithubLogin && <GithubLoginButton config={githubAuthConfig} />}
<SignInLink />
</>
)}
</div>
</StyledWrapper>
);
Expand Down

0 comments on commit aca0afe

Please sign in to comment.