Skip to content

Commit

Permalink
feat(web): add invitecode to homepage and login (#1170)
Browse files Browse the repository at this point in the history
  • Loading branch information
newfish-cmyk committed May 24, 2023
1 parent 34d85dd commit 6f604be
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 26 deletions.
39 changes: 39 additions & 0 deletions web/src/hooks/useInviteCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { useEffect, useState } from "react";

const useInviteCode = () => {
const [inviteCode, setInviteCode] = useState<string | null>(null);

useEffect(() => {
let code = new URLSearchParams(window.location.search).get("code");

if (code) {
const now = new Date();
const expirationDays = 7;
const expiration = new Date(now.getTime() + expirationDays * 24 * 60 * 60 * 1000);

const item = {
value: code,
expiration: expiration.getTime(),
};

localStorage.setItem("inviteCode", JSON.stringify(item));
} else {
const item = localStorage.getItem("inviteCode");

if (item) {
const data = JSON.parse(item);
if (new Date().getTime() > data.expiration) {
localStorage.removeItem("inviteCode");
} else {
code = data.value;
}
}
}

setInviteCode(code);
}, []);

return inviteCode;
};

export default useInviteCode;
4 changes: 4 additions & 0 deletions web/src/pages/auth/signin/mods/LoginByPhonePanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { t } from "i18next";

import { Routes } from "@/constants";

import useInviteCode from "@/hooks/useInviteCode";
import { useSendSmsCodeMutation, useSigninBySmsCodeMutation } from "@/pages/auth/service";
import useGlobalStore from "@/pages/globalStore";

Expand Down Expand Up @@ -47,10 +48,13 @@ export default function LoginByPhonePanel({
},
});

const inviteCode = useInviteCode();

const onSubmit = async (data: FormData) => {
const res = await signinBySmsCodeMutation.mutateAsync({
phone: data.phone,
code: data.validationCode,
inviteCode: inviteCode,
});

if (res?.data) {
Expand Down
28 changes: 2 additions & 26 deletions web/src/pages/auth/signup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { t } from "i18next";

import { COLOR_MODE } from "@/constants";

import useInviteCode from "@/hooks/useInviteCode";
import {
useGetProvidersQuery,
useSendSmsCodeMutation,
Expand Down Expand Up @@ -63,32 +64,7 @@ export default function SignUp() {
const [isSendSmsCode, setIsSendSmsCode] = useState(false);
const [countdown, setCountdown] = useState(60);
const [isShowPassword, setIsShowPassword] = useState(false);

let inviteCode = new URLSearchParams(window.location.search).get("code");

if (inviteCode) {
const now = new Date();
const expirationDays = 7;
const expiration = new Date(now.getTime() + expirationDays * 24 * 60 * 60 * 1000);

const item = {
value: inviteCode,
expiration: expiration.getTime(),
};

localStorage.setItem("inviteCode", JSON.stringify(item));
} else {
const item = localStorage.getItem("inviteCode");

if (item) {
const data = JSON.parse(item);
if (new Date().getTime() > data.expiration) {
localStorage.removeItem("inviteCode");
} else {
inviteCode = data.value;
}
}
}
const inviteCode = useInviteCode();

const {
register,
Expand Down
4 changes: 4 additions & 0 deletions web/src/pages/homepage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import Navbar from "./navbar";

import "./homepage.css";

import useInviteCode from "@/hooks/useInviteCode";

export default function Home() {
useInviteCode();

return (
<div style={{ fontSize: "16px" }} className="homepage">
<Navbar />
Expand Down

0 comments on commit 6f604be

Please sign in to comment.