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

[4주차 기본/심화/공유 과제] 회원가입 & 로그인 (Typescript) #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions week4/.gitignore
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?
50 changes: 50 additions & 0 deletions week4/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# 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:

- Configure the top-level `parserOptions` property like this:

```js
export default tseslint.config({
languageOptions: {
// other options...
parserOptions: {
project: ['./tsconfig.node.json', './tsconfig.app.json'],
tsconfigRootDir: import.meta.dirname,
},
},
})
```

- Replace `tseslint.configs.recommended` to `tseslint.configs.recommendedTypeChecked` or `tseslint.configs.strictTypeChecked`
- Optionally add `...tseslint.configs.stylisticTypeChecked`
- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and update the config:

```js
// eslint.config.js
import react from 'eslint-plugin-react'

export default tseslint.config({
// Set the react version
settings: { react: { version: '18.3' } },
plugins: {
// Add the react plugin
react,
},
rules: {
// other rules...
// Enable its recommended rules
...react.configs.recommended.rules,
...react.configs['jsx-runtime'].rules,
},
})
```
28 changes: 28 additions & 0 deletions week4/eslint.config.js
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 },
],
},
},
)
17 changes: 17 additions & 0 deletions week4/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!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>
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined"
/>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
34 changes: 34 additions & 0 deletions week4/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "assignment4",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc -b && vite build",
"lint": "eslint .",
"preview": "vite preview"
},
"dependencies": {
"@emotion/react": "^11.13.5",
"@emotion/styled": "^11.13.5",
"@types/styled-components": "^5.1.34",
"axios": "^1.7.7",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "^6.27.0"
},
"devDependencies": {
"@eslint/js": "^9.13.0",
"@types/react": "^18.3.12",
"@types/react-dom": "^18.3.1",
"@vitejs/plugin-react-swc": "^3.5.0",
"eslint": "^9.13.0",
"eslint-plugin-react-hooks": "^5.0.0",
"eslint-plugin-react-refresh": "^0.4.14",
"globals": "^15.11.0",
"typescript": "~5.6.2",
"typescript-eslint": "^8.11.0",
"vite": "^5.4.10"
}
}
1 change: 1 addition & 0 deletions week4/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions week4/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ThemeProvider, Global } from "@emotion/react";
import theme from "./styles/theme";
import GlobalStyle from "./styles/global";

import {
BrowserRouter as Router,
Routes,
Route,
Navigate,
} from "react-router-dom";

import Login from "./pages/Login"; // 로그인 페이지
import Join from "./pages/Join"; // 회원가입 페이지
import Home from "./pages/Home"; // 메인 홈 페이지

function App() {
return (
<ThemeProvider theme={theme}>
<Global styles={GlobalStyle} />
<Router>
<Routes>
<Route path="/" element={<Navigate to="/login" />} />
<Route path="/login" element={<Login />} />
<Route path="/join" element={<Join />} />
<Route path="/home" element={<Home />} />
</Routes>
</Router>
</ThemeProvider>
);
}

export default App;
1 change: 1 addition & 0 deletions week4/src/assets/react.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
93 changes: 93 additions & 0 deletions week4/src/components/HomeHobby.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/* eslint-disable no-empty-pattern */
import axios from "axios";
import { useEffect, useState } from "react";
import {
HomeBox,
HomeContainer,
Input,
Ladel,
MainTitle,
MyHobby,
MyHobbyText,
OtherHobby,
SearchChoreBtn,
SearchHobby,
} from "../styles/HomeStyled";

type HomeHobbyProps = object;

const HomeHobby = ({}: HomeHobbyProps) => {
const [usernum, setUsernum] = useState("");
const [hobby, setHobby] = useState("");
const [myHobby, setMyHobby] = useState("");

useEffect(() => {
const findMyHobby = async () => {
const myHobbyResponse = await axios.get(
"http://223.130.135.50:8085/user/my-hobby",
{
headers: {
token: localStorage.getItem("token"),
},
}
);
setMyHobby(myHobbyResponse.data.result.hobby);
};

findMyHobby();
}, []); //지금 useEffect의 두 번째 인수인 '[]'는 '의존성 배열'! 이 배열에 비어 있으면, 컴포넌트가 처음 나타날 때 한 번만 실행된다

const search = async () => {
try {
const response = await axios.get(
`http://223.130.135.50:8085/user/${usernum}/hobby`,
{
headers: {
token: localStorage.getItem("token"),
},
}
);
setHobby(response.data.result.hobby);
} catch (error) {
console.error("해당 번호에 데이터가 존재하지 않습니다", error);
alert("해당 번호에 데이터가 존재하지 않습니다");
}
};

return (
<HomeContainer>
<MainTitle>취미</MainTitle>
<HomeBox>
<MyHobby>
<Ladel>나의 취미</Ladel>
<MyHobbyText>{myHobby}</MyHobbyText>
</MyHobby>
<OtherHobby>
<Ladel>다른 사람들의 취미</Ladel>
<Input
type="text"
placeholder="사용자 번호"
onChange={(e) => {
setUsernum(e.target.value);
}}
/>
{usernum &&
hobby && ( //usernum과 hobby가 있을 때만 p 태그가 뜨도록!!
<SearchHobby>
{usernum}번 사용자의 취미 {hobby}
</SearchHobby>
)}
</OtherHobby>
<SearchChoreBtn
onClick={() => {
search();
}}
>
검색
</SearchChoreBtn>
</HomeBox>
</HomeContainer>
);
};

export default HomeHobby;
61 changes: 61 additions & 0 deletions week4/src/components/HomeInfo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {
HomeBox,
HomeContainer,
Input,
Ladel,
MainTitle,
NewInfo,
SearchChoreBtn,
} from "../styles/HomeStyled";

type HomeInfoProps = {
password: string;
setPassword: React.Dispatch<React.SetStateAction<string>>;
hobby: string;
setHobby: React.Dispatch<React.SetStateAction<string>>;
change: () => void;
};

const HomeInfo = ({
password,
hobby,
setPassword,
setHobby,
change,
}: HomeInfoProps) => {
const handleSubmit = () => {
if (password.trim() === "" && hobby.trim() === "") {
alert("비밀번호 또는 취미를 입력해주세요");
return;
}
change();
};
return (
<HomeContainer>
<MainTitle>내 정보 수정하기</MainTitle>
<HomeBox>
<NewInfo>
<Ladel>새 비밀번호</Ladel>
<Input
type="password"
onChange={(e) => {
setPassword(e.target.value);
}}
/>
</NewInfo>
<NewInfo>
<Ladel>새 취미</Ladel>
<Input
type="text"
onChange={(e) => {
setHobby(e.target.value);
}}
/>
</NewInfo>
<SearchChoreBtn onClick={handleSubmit}>수정하기</SearchChoreBtn>
</HomeBox>
</HomeContainer>
);
};

export default HomeInfo;
Loading