Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[황경수] week16 #1065

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Head from "next/head";
import styled from "styled-components";
import Link from "next/link";
import { useEffect, useState } from "react";
import { safeGetItem } from "@src/utils/safeKey";

const Container = styled.div`
height: 600px;
Expand Down Expand Up @@ -31,10 +32,11 @@ function Home() {

useEffect(() => {
if (typeof window !== "undefined") {
const accessToken = localStorage.getItem("accessToken");
const accessToken = safeGetItem("accessToken");
if (accessToken !== null) setToken(accessToken);
Comment on lines +35 to 36
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

지난번 리뷰 사항을 잘 반영해주셨네요 👍

}
}, []);

return (
<>
<Head>
Expand Down
12 changes: 5 additions & 7 deletions src/components/sign/SigninForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { postSignIn } from "@src/api";
import { useRouter } from "next/router";
import Input from "@src/components/sign/Input";
import ERROR from "@src/components/sign/ErrorMessages";
import { saveToLocalStorage } from "@src/utils/safeKey";
import { emailTextInputProps } from "@src/utils/formProps";

type FormType = {
email: string;
Expand All @@ -32,7 +34,7 @@ function SigninForm() {
console.log(accessToken);

if (accessToken) {
localStorage.setItem("accessToken", accessToken);
saveToLocalStorage("accessToken", accessToken);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기도 반영 굳! 👍

} else {
throw new Error("Access token이 없습니다.");
}
Expand All @@ -56,12 +58,8 @@ function SigninForm() {
<S.Label>이메일</S.Label>
<div style={{ position: "relative" }}>
<Input.TextInput
type="text"
register={register("email", {
required: true,
pattern: /^[a-z0-9]+@[a-z]+\.[a-z]{2,3}$/,
})}
placeholder="이메일을 입력해 주세요"
{...emailTextInputProps}
register={register("email", emailTextInputProps.registerOptions)}
$hasError={!!errors.email}
/>
<S.ErrorMessage>
Expand Down
23 changes: 11 additions & 12 deletions src/components/sign/SignupForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import { postCheckEmailDuplicate, postSignUp } from "@src/api";
import { useRouter } from "next/router";
import Input from "@src/components/sign/Input";
import ERROR from "@src/components/sign/ErrorMessages";
import {
emailTextInputProps,
passwordTextInputProps,
} from "@src/utils/formProps";

type FormType = {
email: string;
Expand Down Expand Up @@ -87,12 +91,8 @@ function SignupForm() {
<S.Label>이메일</S.Label>
<div style={{ position: "relative" }}>
<Input.TextInput
type="text"
register={register("email", {
required: true,
pattern: /^[a-z0-9]+@[a-z]+\.[a-z]{2,3}$/,
})}
placeholder="이메일을 입력해 주세요"
{...emailTextInputProps}
register={register("email", emailTextInputProps.registerOptions)}
$hasError={!!errors.email}
/>
<S.ErrorMessage>
Expand All @@ -106,12 +106,11 @@ function SignupForm() {
<S.Label>비밀번호</S.Label>
<div style={{ position: "relative" }}>
<Input.TextInput
type={inputType}
register={register("password", {
required: true,
pattern: /^(?=.*[a-zA-Z])(?=.*\d).{8,}$/,
})}
placeholder="영문, 숫자를 조합해 8자 이상 입력해 주세요"
{...passwordTextInputProps(inputType)}
register={
(register("password"),
passwordTextInputProps(inputType).registerOptions)
}
$hasError={!!errors.password}
/>
<S.EyeIconWrapper onClick={handleEyeIconClick}>
Expand Down
17 changes: 17 additions & 0 deletions src/utils/formProps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const emailTextInputProps = {
type: "text",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기서 inputType 값은 email 값이 더 일반적입니다.

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input

registerOptions: {
required: true,
pattern: /^[a-z0-9]+@[a-z]+\.[a-z]{2,3}$/,
},
placeholder: "이메일을 입력해 주세요",
};

export const passwordTextInputProps = (inputType: string) => ({
type: inputType,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기서 inputType 값은 password 값이 일반적입니다~

registerOptions: {
required: true,
pattern: /^(?=.*[a-zA-Z])(?=.*\d).{8,}$/,
},
placeholder: "영문, 숫자를 조합해 8자 이상 입력해 주세요",
});
16 changes: 16 additions & 0 deletions src/utils/safeKey.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const safeGetItem = (key: string) => {
try {
return localStorage.getItem(key);
} catch (error) {
console.error(error);
return null;
}
};
Comment on lines +1 to +8
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기서 리턴값이 null로 되어있는 경우에는 키에 대한 값이 없어서 null 일때와 에러가 발생해서 받은 값인 null 경우 구분하기가 어렵기 때문에, 해당 키 값이 없을땐 null / 에러 발생 시 undefined 이렇게 구분해서 리턴해주면 좋습니다~


export const saveToLocalStorage = (key: string, value: string) => {
try {
localStorage.setItem(key, value);
} catch (e) {
console.error(e);
}
};
Loading