-
Notifications
You must be signed in to change notification settings - Fork 0
[week4/mission] 첫 커스텀 훅 만들어보기, 로그인 / 회원가입 기능 구현해보기 (feat. 유효성 검사) #24
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
Open
namgungseok12
wants to merge
3
commits into
jay/main
Choose a base branch
from
jay/#20
base: jay/main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
d1868ce
[week4/mission] 첫 커스텀 훅 만들어보기, 로그인 / 회원가입 기능 구현해보기 (feat. 유효성 검사)
namgungseok12 8f59cfc
[week4/mission] 첫 커스텀 훅 만들어보기, 로그인 / 회원가입 기능 구현해보기 (feat. 유효성 검사)
namgungseok12 b7fe915
[week4/mission] 첫 커스텀 훅 만들어보기, 로그인 / 회원가입 기능 구현해보기 (feat. 유효성 검사)
namgungseok12 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Logs | ||
| logs | ||
| *.log | ||
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* | ||
| pnpm-debug.log* | ||
| lerna-debug.log* | ||
|
|
||
| node_modules | ||
| dist | ||
| dist-ssr | ||
| *.local | ||
|
|
||
| # Editor directories and files | ||
| .vscode/* | ||
| !.vscode/extensions.json | ||
| .idea | ||
| .DS_Store | ||
| *.suo | ||
| *.ntvs* | ||
| *.njsproj | ||
| *.sln | ||
| *.sw? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # React + TypeScript + Vite | ||
|
|
||
| This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules. | ||
|
|
||
| Currently, two official plugins are available: | ||
|
|
||
| - [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh | ||
| - [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh | ||
|
|
||
| ## Expanding the ESLint configuration | ||
|
|
||
| If you are developing a production application, we recommend updating the configuration to enable type-aware lint rules: | ||
|
|
||
| ```js | ||
| export default tseslint.config({ | ||
| extends: [ | ||
| // Remove ...tseslint.configs.recommended and replace with this | ||
| ...tseslint.configs.recommendedTypeChecked, | ||
| // Alternatively, use this for stricter rules | ||
| ...tseslint.configs.strictTypeChecked, | ||
| // Optionally, add this for stylistic rules | ||
| ...tseslint.configs.stylisticTypeChecked, | ||
| ], | ||
| languageOptions: { | ||
| // other options... | ||
| parserOptions: { | ||
| project: ['./tsconfig.node.json', './tsconfig.app.json'], | ||
| tsconfigRootDir: import.meta.dirname, | ||
| }, | ||
| }, | ||
| }) | ||
| ``` | ||
|
|
||
| You can also install [eslint-plugin-react-x](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-x) and [eslint-plugin-react-dom](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-dom) for React-specific lint rules: | ||
|
|
||
| ```js | ||
| // eslint.config.js | ||
| import reactX from 'eslint-plugin-react-x' | ||
| import reactDom from 'eslint-plugin-react-dom' | ||
|
|
||
| export default tseslint.config({ | ||
| plugins: { | ||
| // Add the react-x and react-dom plugins | ||
| 'react-x': reactX, | ||
| 'react-dom': reactDom, | ||
| }, | ||
| rules: { | ||
| // other rules... | ||
| // Enable its recommended typescript rules | ||
| ...reactX.configs['recommended-typescript'].rules, | ||
| ...reactDom.configs.recommended.rules, | ||
| }, | ||
| }) | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import js from '@eslint/js' | ||
| import globals from 'globals' | ||
| import reactHooks from 'eslint-plugin-react-hooks' | ||
| import reactRefresh from 'eslint-plugin-react-refresh' | ||
| import tseslint from 'typescript-eslint' | ||
|
|
||
| export default tseslint.config( | ||
| { ignores: ['dist'] }, | ||
| { | ||
| extends: [js.configs.recommended, ...tseslint.configs.recommended], | ||
| files: ['**/*.{ts,tsx}'], | ||
| languageOptions: { | ||
| ecmaVersion: 2020, | ||
| globals: globals.browser, | ||
| }, | ||
| plugins: { | ||
| 'react-hooks': reactHooks, | ||
| 'react-refresh': reactRefresh, | ||
| }, | ||
| rules: { | ||
| ...reactHooks.configs.recommended.rules, | ||
| 'react-refresh/only-export-components': [ | ||
| 'warn', | ||
| { allowConstantExport: true }, | ||
| ], | ||
| }, | ||
| }, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <link rel="icon" type="image/svg+xml" href="/vite.svg" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>Vite + React + TS</title> | ||
| </head> | ||
| <body> | ||
| <div id="root"></div> | ||
| <script type="module" src="/src/main.tsx"></script> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| { | ||
| "name": "login-fe", | ||
| "private": true, | ||
| "version": "0.0.0", | ||
| "type": "module", | ||
| "scripts": { | ||
| "dev": "vite", | ||
| "build": "tsc -b && vite build", | ||
| "lint": "eslint .", | ||
| "preview": "vite preview" | ||
| }, | ||
| "dependencies": { | ||
| "@tailwindcss/vite": "^4.1.3", | ||
| "react": "^19.0.0", | ||
| "react-dom": "^19.0.0", | ||
| "react-router-dom": "^7.5.0", | ||
| "tailwindcss": "^4.1.3" | ||
| }, | ||
| "devDependencies": { | ||
| "@eslint/js": "^9.21.0", | ||
| "@types/react": "^19.0.10", | ||
| "@types/react-dom": "^19.0.4", | ||
| "@vitejs/plugin-react-swc": "^3.8.0", | ||
| "eslint": "^9.21.0", | ||
| "eslint-plugin-react-hooks": "^5.1.0", | ||
| "eslint-plugin-react-refresh": "^0.4.19", | ||
| "globals": "^15.15.0", | ||
| "typescript": "~5.7.2", | ||
| "typescript-eslint": "^8.24.1", | ||
| "vite": "^6.2.0" | ||
| } | ||
| } |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
|
|
||
| import "./App.css" | ||
| import { createBrowserRouter, RouterProvider } from 'react-router' | ||
|
|
||
| import NotFoundPage from './pages/NotFoundPage' | ||
| import LoginPage from './pages/LoginPage' | ||
| import HomeLayout from './layouts/HomeLayouts' | ||
| import HomePage from './pages/HomePage' | ||
| import SignupPage from './pages/SignupPage' | ||
|
|
||
|
|
||
| const router = createBrowserRouter([ | ||
| { | ||
| path: "/", | ||
| element: <HomeLayout />, | ||
| errorElement: <NotFoundPage />, | ||
| children: [ | ||
| {index: true, element: <HomePage />}, | ||
| {path: 'login', element: <LoginPage />}, | ||
| {path: 'signup', element: <SignupPage />}, | ||
| ], | ||
| } | ||
| ]) | ||
|
|
||
| function App() { | ||
|
|
||
| return <RouterProvider router={router} /> | ||
| } | ||
|
|
||
| export default App |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| declare module '*.css' { | ||
| const content: { [className: string]: string }; | ||
| export default content; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { ChangeEvent, useEffect, useState } from 'react'; | ||
|
|
||
| interface UseFormProps<T> { | ||
| initialValues: T; // {email: '', password: ''} | ||
| validate: (values: T) => Record<keyof T, string> //값이 올마른지 검증하는 함수 | ||
| } | ||
|
|
||
| function useForm<T>({initialValues, validate}: UseFormProps<T>) { | ||
|
|
||
| const [values, setValues] = useState(initialValues); | ||
|
|
||
| const [touched, setTouched] = useState<Record<string, boolean>>() // 아직입력 안했는데 에러뜨면 기분 나쁨 | ||
|
|
||
| const [errors, setErrors] = useState<Record<string, string>>() | ||
|
|
||
| //사용자가 입력값 바꿀 때 실행되는 함수 | ||
| const handleChange = (name: keyof T, text: string) => { | ||
| setValues({ | ||
| ...values, //기존 입력값 유지 | ||
| [name]: text, | ||
| }) | ||
| } | ||
|
|
||
| const handleBlur = (name: keyof T) => { | ||
| setTouched({ | ||
| ...touched, | ||
| [name]: true, | ||
| }) | ||
| } | ||
|
|
||
|
|
||
| //이메일 인풋, 패스워드 인풋, 속성들을 가져오는 것 | ||
| const getInputProps = (name: keyof T) => { | ||
| const value: T[keyof T] = values[name] | ||
| const onChange = ( | ||
| e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>, | ||
| ) => handleChange(name, e.target.value) | ||
| const onBlur = () => handleBlur(name) | ||
|
|
||
| return {value, onChange, onBlur} | ||
| } | ||
|
|
||
| //values가 변경될 때마다 에러 검증 로직 | ||
| // {email: ''} | ||
| useEffect(() => { | ||
| if (validate) { | ||
| const newErrors = validate(values) | ||
| setErrors(newErrors) // update errors message | ||
| } | ||
| },[validate, values]) | ||
|
|
||
| return {values, errors, touched, getInputProps} | ||
| } | ||
|
|
||
| export default useForm |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| @import "tailwindcss" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { Outlet } from 'react-router-dom'; | ||
|
|
||
|
|
||
| const HomeLayout = () => { | ||
| return ( | ||
| <div className = "h-dvh flex flex-col"> | ||
| <nav>navigation bar</nav> | ||
| <main className='flex-1'> | ||
| <Outlet /> | ||
| </main> | ||
| <footer>footer</footer> | ||
| </div> | ||
| ); | ||
| } | ||
| export default HomeLayout; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
|
|
||
| import { StrictMode } from 'react' | ||
| import { createRoot } from 'react-dom/client' | ||
| import './index.css' | ||
| import App from './App.tsx' | ||
|
|
||
| createRoot(document.getElementById('root')!).render( | ||
| <StrictMode> | ||
| <App /> | ||
| </StrictMode>, | ||
| ) | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| const HomePage = () => { | ||
| return ( | ||
| <div> | ||
| home page | ||
| </div> | ||
| ); | ||
| } | ||
| export default HomePage; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import useForm from '../hooks/useForm'; | ||
| import { UserSigninInformation, validateSignin } from '../utils/validate'; | ||
|
|
||
| const LoginPage = () => { | ||
|
|
||
| const {values, errors,touched, getInputProps} = useForm<UserSigninInformation>({ | ||
| initialValues: { | ||
| email: '', | ||
| password: '', | ||
| }, | ||
| validate: validateSignin, | ||
| }) | ||
|
|
||
|
|
||
| const handleSubmit = () => {} | ||
|
|
||
| return ( | ||
| <div className="flex flex-col items-center justify-center h-full gap-4"> | ||
| <div className='flex flex-col gap-3'> | ||
| <input | ||
| {...getInputProps('email')} | ||
| type={"email"} | ||
| className={`border border-[#ccc] w-[300px] p-[10px] focus:border-[#807bff] rounded-sm | ||
| ${errors?.email &&touched?.email ? "border-red-500 bg-red-200" : "border-gray-300"}`} | ||
| placeholder={"이메일"} | ||
| /> | ||
| {errors?.email &&touched?.email &&(<div className='text-red-500 text-sm'>{errors.email}</div>)} | ||
| <input | ||
| {...getInputProps('password')} | ||
| type={"password"} | ||
| className={'border border-[#ccc] w-[300px] p-[10px] focus:border-[#807bff] rounded-sm'} | ||
| placeholder={"비밀번호"} | ||
| /> | ||
| <button | ||
| type='button' | ||
| onClick={handleSubmit} | ||
| disabled={false} | ||
| className='w-full bg-[#807bff] text-white p-[10px] rounded-sm hover:bg-[#807bff]/80 active:bg-[#807bff]/90 disabled:bg-[#807bff]/50' | ||
| > | ||
| 로그인 | ||
| </button> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
| export default LoginPage; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| const NotFoundPage = () => { | ||
| return ( | ||
| <div> | ||
| Not found | ||
| </div> | ||
| ); | ||
| } | ||
| export default NotFoundPage; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
|
|
||
|
|
||
| const SignupPage = () => { | ||
| return ( | ||
| <div>SignupPage</div> | ||
| ) | ||
| } | ||
| export default SignupPage |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| export type UserSigninInformation = { | ||
| email: string; | ||
| password: string; | ||
| } | ||
|
|
||
| function validateUser(values: UserSigninInformation) { | ||
| const errors = { | ||
| email: '', | ||
| password: '', | ||
| } | ||
|
|
||
| if(!/^[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*@[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*\.[a-zA-Z]{2,3}$/i.test(values.email)) { | ||
| errors.email = '올바른 이메일 형식이 아닙니다.' | ||
| } | ||
|
|
||
| // 비밀번호는 8자 이상 20자 이하 | ||
| if (values.password.length < 8 || values.password.length > 20) { | ||
| errors.password = '비밀번호는 8자 이상 20자 이하로 입력해주세요.' | ||
| } | ||
| return errors | ||
| } | ||
|
|
||
| //로그인 유효성 검사 | ||
| function validateSignin(values: UserSigninInformation) { | ||
| return validateUser(values) | ||
| } | ||
|
|
||
| export {validateSignin} |
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
고생하셨습니다~!