feat: email validation in all forms

This commit is contained in:
Tristan Yang
2022-12-06 21:53:22 +08:00
parent 70e440b6b0
commit d0714485e5
5 changed files with 27 additions and 31 deletions
+1
View File
@@ -93,6 +93,7 @@ interface Props
| "onBlur" | "onBlur"
| "pattern" | "pattern"
| "disabled" | "disabled"
| "minLength"
>, >,
HTMLInputElement HTMLInputElement
> { > {
+1
View File
@@ -138,6 +138,7 @@ export default function LoginPage() {
className="large" className="large"
name="email" name="email"
value={email} value={email}
type="email"
required required
placeholder={t("placeholder_email")} placeholder={t("placeholder_email")}
data-type="email" data-type="email"
+3 -4
View File
@@ -55,10 +55,6 @@ export default function Reg() {
const handleReg = async (evt: FormEvent<HTMLFormElement>) => { const handleReg = async (evt: FormEvent<HTMLFormElement>) => {
evt.preventDefault(); evt.preventDefault();
const { email, password, confirmPassword } = input; const { email, password, confirmPassword } = input;
if (password.length < 6) {
toast.error("Password can't be less than 6 digits!");
return;
}
if (password !== confirmPassword) { if (password !== confirmPassword) {
toast.error("Not Same Password!"); toast.error("Not Same Password!");
return; return;
@@ -123,6 +119,7 @@ export default function Reg() {
name="email" name="email"
value={email} value={email}
required required
type="email"
placeholder={t("placeholder_email")} placeholder={t("placeholder_email")}
data-type="email" data-type="email"
onChange={handleInput} onChange={handleInput}
@@ -133,6 +130,7 @@ export default function Reg() {
value={password} value={password}
name="password" name="password"
required required
minLength={6}
data-type="password" data-type="password"
onChange={handleInput} onChange={handleInput}
placeholder={t("placeholder_pwd")} placeholder={t("placeholder_pwd")}
@@ -141,6 +139,7 @@ export default function Reg() {
required required
onBlur={handleCompare} onBlur={handleCompare}
type="password" type="password"
minLength={6}
name={"confirmPassword"} name={"confirmPassword"}
value={confirmPassword} value={confirmPassword}
data-type="confirmPassword" data-type="confirmPassword"
+5 -4
View File
@@ -100,12 +100,12 @@ const StyledWrapper = styled.div`
border-radius: 8px; border-radius: 8px;
} }
`; `;
type EditFields = "name" | "email"; type EditField = "name" | "email" | "";
export default function MyAccount() { export default function MyAccount() {
const { t } = useTranslation("member"); const { t } = useTranslation("member");
const { t: ct } = useTranslation(); const { t: ct } = useTranslation();
const [passwordModal, setPasswordModal] = useState(false); const [passwordModal, setPasswordModal] = useState(false);
const [editModal, setEditModal] = useState<EditFields | null>(null); const [editModal, setEditModal] = useState<EditField>("");
const [removeConfirmVisible, setRemoveConfirmVisible] = useState(false); const [removeConfirmVisible, setRemoveConfirmVisible] = useState(false);
const [uploadAvatar, { isSuccess: uploadSuccess }] = useUpdateAvatarMutation(); const [uploadAvatar, { isSuccess: uploadSuccess }] = useUpdateAvatarMutation();
const EditModalInfo = { const EditModalInfo = {
@@ -132,12 +132,12 @@ export default function MyAccount() {
}, [uploadSuccess]); }, [uploadSuccess]);
const handleBasicEdit = (evt: MouseEvent<HTMLButtonElement>) => { const handleBasicEdit = (evt: MouseEvent<HTMLButtonElement>) => {
const { edit } = evt.currentTarget.dataset as { edit: EditFields }; const { edit } = evt.currentTarget.dataset as { edit: EditField };
setEditModal(edit); setEditModal(edit);
}; };
const closeBasicEditModal = () => { const closeBasicEditModal = () => {
setEditModal(null); setEditModal("");
}; };
const togglePasswordModal = () => { const togglePasswordModal = () => {
@@ -198,6 +198,7 @@ export default function MyAccount() {
</StyledWrapper> </StyledWrapper>
{editModal && ( {editModal && (
<ProfileBasicEditModal <ProfileBasicEditModal
type={editModal == "email" ? "email" : "text"}
valueKey={editModal} valueKey={editModal}
{...EditModalInfo[editModal]} {...EditModalInfo[editModal]}
value={eval(editModal)} value={eval(editModal)}
+17 -23
View File
@@ -1,5 +1,4 @@
import { ChangeEvent, FC, useEffect, useState } from "react"; import { ChangeEvent, FC, useEffect, useState, useRef } from "react";
import styled from "styled-components";
import toast from "react-hot-toast"; import toast from "react-hot-toast";
import Input from "../../common/component/styled/Input"; import Input from "../../common/component/styled/Input";
import { useUpdateInfoMutation } from "../../app/services/user"; import { useUpdateInfoMutation } from "../../app/services/user";
@@ -8,25 +7,11 @@ import Button from "../../common/component/styled/Button";
import Modal from "../../common/component/Modal"; import Modal from "../../common/component/Modal";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
const StyledEdit = styled(StyledModal)`
.input {
margin: 48px 0;
width: 100%;
display: flex;
flex-direction: column;
gap: 8px;
label {
font-weight: 600;
font-size: 14px;
line-height: 20px;
color: #6b7280;
}
}
`;
interface Props { interface Props {
label?: string; label?: string;
valueKey?: "name" | "email"; valueKey?: "name" | "email";
type?: string;
value?: string; value?: string;
title?: string; title?: string;
intro?: string; intro?: string;
@@ -37,10 +22,12 @@ const ProfileBasicEditModal: FC<Props> = ({
label = "Username", label = "Username",
valueKey = "name", valueKey = "name",
value = "", value = "",
type = "text",
title = "Change your username", title = "Change your username",
intro = "Enter a new username and your existing password.", intro = "Enter a new username and your existing password.",
closeModal closeModal
}) => { }) => {
const formRef = useRef(null);
const { t } = useTranslation(); const { t } = useTranslation();
const [input, setInput] = useState(value); const [input, setInput] = useState(value);
const [update, { isLoading, isSuccess }] = useUpdateInfoMutation(); const [update, { isLoading, isSuccess }] = useUpdateInfoMutation();
@@ -48,6 +35,13 @@ const ProfileBasicEditModal: FC<Props> = ({
setInput(evt.target.value); setInput(evt.target.value);
}; };
const handleUpdate = () => { const handleUpdate = () => {
if (!formRef || !formRef.current) return;
const formEle = formRef.current as HTMLFormElement;
if (!formEle.checkValidity()) {
formEle.reportValidity();
return;
}
update({ [valueKey]: input }); update({ [valueKey]: input });
}; };
useEffect(() => { useEffect(() => {
@@ -59,7 +53,7 @@ const ProfileBasicEditModal: FC<Props> = ({
}, [isSuccess]); }, [isSuccess]);
return ( return (
<Modal id="modal-modal"> <Modal id="modal-modal">
<StyledEdit <StyledModal
title={title} title={title}
description={intro} description={intro}
buttons={ buttons={
@@ -71,11 +65,11 @@ const ProfileBasicEditModal: FC<Props> = ({
</> </>
} }
> >
<div className="input"> <form ref={formRef} className="flex flex-col gap-2 w-full" action="/">
<label htmlFor={valueKey}>{label}</label> <label htmlFor={valueKey} className="text-sm text-[#6b7280]">{label}</label>
<Input name={valueKey} value={input} onChange={handleChange}></Input> <Input name={valueKey} value={input} onChange={handleChange} type={type} required></Input>
</div> </form>
</StyledEdit> </StyledModal>
</Modal> </Modal>
); );
}; };