Skip to content

Commit

Permalink
fix: fix bugs in profilepage (#1973)
Browse files Browse the repository at this point in the history
* fix bugs in profile

* fix bugs in profile
  • Loading branch information
youyc22 authored Aug 29, 2024
1 parent 4b446c1 commit 0668e4b
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 25 deletions.
14 changes: 12 additions & 2 deletions src/api/utils/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,18 @@ export const validatePassword = (password: string) => {
);
};

export const validateNumber = (number: string) => {
return /^[0-9]*$/.test(number);
export const validatePhoneNumber = (phoneNumber: string): boolean => {
if (!/^[0-9]+$/.test(phoneNumber)) {
return false;
}
if (phoneNumber.length !== 11 || !phoneNumber.startsWith("1")) {
return false;
}
return true;
};

export const validateStudentID = (input: string): boolean => {
return /^[0-9]+$/.test(input);
};

export const validateClass = (className: string) => {
Expand Down
4 changes: 1 addition & 3 deletions src/app/Components/Authenticate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ const Authenticate: React.FC<AuthenticateProps> = ({
}
});

if (!user.isLoggedIn) {
return <Navigate to="/user/login" />;
}
if (!user.isLoggedIn) return <Navigate to="/user/login" />;

if (role.includes(user.role)) return children;

Expand Down
26 changes: 14 additions & 12 deletions src/app/InfoSite/MentorApplicationPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,15 @@ const MentorApplicationPage: React.FC<PageProps> = ({ mode, user }) => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [mentorInfoListData, selectedYear]);

const [getMentorApplicationsCount] =
graphql.useGetMentorApplicationsCountLazyQuery();
const [getMentorApplicationsApprovedCount] =
graphql.useGetMentorApplicationsApprovedCountLazyQuery();
const { refetch: getMentorApplicationsCount } =
graphql.useGetMentorApplicationsCountQuery({
skip: true,
});

const { refetch: getMentorApplicationsApprovedCount } =
graphql.useGetMentorApplicationsApprovedCountQuery({
skip: true,
});

// Get mentor list detail
const mentorListWithApplicationsCount = async (
Expand All @@ -269,18 +274,14 @@ const MentorApplicationPage: React.FC<PageProps> = ({ mode, user }) => {
mentorInfoListData.mentor_info.map(async (mentor) => {
const { data: applicationsCountResult } =
await getMentorApplicationsCount({
variables: {
uuid: mentor.mentor_uuid,
year: selectedYear,
},
uuid: mentor.mentor_uuid,
year: selectedYear,
});

const { data: applicationsApprovedCountResult } =
await getMentorApplicationsApprovedCount({
variables: {
uuid: mentor.mentor_uuid,
year: selectedYear,
},
uuid: mentor.mentor_uuid,
year: selectedYear,
});

return {
Expand Down Expand Up @@ -584,6 +585,7 @@ const MentorApplicationPage: React.FC<PageProps> = ({ mode, user }) => {

const [queryStudentByStudentNo] =
graphql.useQueryStudentByStudentNoLazyQuery();

const [queryTeacherByRealname] = graphql.useQueryTeacherByRealnameLazyQuery();

/**
Expand Down
8 changes: 6 additions & 2 deletions src/app/UserSite/Components/Start.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Button, Form, Input, Tooltip, message } from "antd";
import Center from "../../Components/Center";
import { validateEmail, validateNumber } from "@/api/utils/validator";
import { validateEmail, validatePhoneNumber } from "@/api/utils/validator";
import { QuestionCircleOutlined, UserOutlined } from "@ant-design/icons";
import { useNavigate } from "react-router-dom";
import * as graphql from "@/generated/graphql";
Expand Down Expand Up @@ -82,7 +82,11 @@ const Start: React.FC<StartProps> = ({
{ required: true, message: "请输入邮箱/手机号" },
() => ({
validator(rule, value) {
if (!value || validateEmail(value) || validateNumber(value)) {
if (
!value ||
validateEmail(value) ||
validatePhoneNumber(value)
) {
return Promise.resolve();
}
return Promise.reject("请输入正确的邮箱/手机号");
Expand Down
4 changes: 2 additions & 2 deletions src/app/UserSite/LoginPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useUrl } from "../../api/hooks/url";
import Background from "./Components/Background";
import {
validateEmail,
validateNumber,
validatePhoneNumber,
validateUsername,
} from "../../api/utils/validator";
import { UserProps } from ".";
Expand Down Expand Up @@ -70,7 +70,7 @@ const LoginPage: React.FC<UserProps> = ({ mode, user, setUser }) => {
if (
!value ||
validateEmail(value) ||
validateNumber(value) ||
validatePhoneNumber(value) ||
validateUsername(value)
) {
return Promise.resolve();
Expand Down
33 changes: 29 additions & 4 deletions src/app/UserSite/ProfilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import dayjs from "dayjs";
import {
validateClass,
validateEmail,
validateNumber,
validateStudentID,
validatePhoneNumber,
validateUsername,
} from "../../api/utils/validator";
import { UserProps } from ".";
Expand Down Expand Up @@ -80,6 +81,12 @@ const ProfilePage: React.FC<UserProps> = ({ mode, user, setUser }) => {
// const handleLogin = () => {
// window.location.href = `https://github.com/login/oauth/authorize?client_id=${CLIENT_ID}&redirect_uri=${encodeURIComponent(REDIRECT_URI)}`;
// };
const { refetch: refetchUserByEmail } = graphql.useGetUserByEmailQuery({
variables: { email: "" },
});
const { refetch: refetchUserByPhone } = graphql.useGetUserByPhoneQuery({
variables: { phone: "" },
});

useEffect(() => {
if (getProfileError) {
Expand All @@ -92,6 +99,7 @@ const ProfilePage: React.FC<UserProps> = ({ mode, user, setUser }) => {
graphql.useGetDepartmentsSuspenseQuery({
variables: {},
});

useEffect(() => {
if (getDepartmentsError) {
message.error("获取院系信息失败");
Expand Down Expand Up @@ -134,7 +142,7 @@ const ProfilePage: React.FC<UserProps> = ({ mode, user, setUser }) => {
},
{
key: "phone",
label: "电话",
label: "手机号",
children: profileData.users_by_pk?.phone || "",
editable: () => true,
},
Expand Down Expand Up @@ -198,6 +206,7 @@ const ProfilePage: React.FC<UserProps> = ({ mode, user, setUser }) => {

const [updateProfileMutation, { error: updateProfileError }] =
graphql.useUpdateProfileMutation();

useEffect(() => {
if (updateProfileError) {
if (
Expand All @@ -212,20 +221,31 @@ const ProfilePage: React.FC<UserProps> = ({ mode, user, setUser }) => {
console.log(updateProfileError);
}
}, [updateProfileError]);

const handleEdit = async (key: any, record: any) => {
if (key === "email") {
if (!validateEmail(record[key])) {
message.error("请输入正确的邮箱格式");
return Promise.reject();
}
const { data } = await refetchUserByEmail({ email: record[key] });
if (data.users.length) {
message.error("邮箱已被注册");
return Promise.reject();
}
navigate(url.append("email", record[key]).link("update"));
return Promise.resolve();
}
if (key === "phone") {
if (!validateNumber(record[key])) {
if (!validatePhoneNumber(record[key])) {
message.error("请输入正确的手机号");
return Promise.reject();
}
const { data } = await refetchUserByPhone({ phone: record[key] });
if (data.users.length) {
message.error("手机号已被注册");
return Promise.reject();
}
navigate(url.append("phone", record[key]).link("update"));
return Promise.resolve();
}
Expand All @@ -234,6 +254,11 @@ const ProfilePage: React.FC<UserProps> = ({ mode, user, setUser }) => {
message.error("请输入正确的邮箱格式");
return Promise.reject();
}
const { data } = await refetchUserByEmail({ email: record[key] });
if (data.users.length) {
message.error("邮箱已被注册");
return Promise.reject();
}
navigate(
url
.append("tsinghua", "true")
Expand All @@ -259,7 +284,7 @@ const ProfilePage: React.FC<UserProps> = ({ mode, user, setUser }) => {
}
}
if (key === "student_no") {
if (!validateNumber(record[key])) {
if (!validateStudentID(record[key])) {
message.error("请输入正确的学号");
return Promise.reject();
}
Expand Down
1 change: 1 addition & 0 deletions src/app/UserSite/RegisterPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const RegisterPage: React.FC<UserProps> = ({ mode, user, setUser }) => {
if (password) {
handleRegister();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [password]);

return (
Expand Down

0 comments on commit 0668e4b

Please sign in to comment.