Skip to content
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
2 changes: 2 additions & 0 deletions week4/week4-HW/.env
Copy link

Choose a reason for hiding this comment

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

과제라서 상관없긴 하지만 다른 프로젝트에선 보안 등을 위해 해당 파일은 .gitignore에 추가하면 좋을 것 같습니다 ㅎㅎ!!

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# .env
VITE_APP_BASE_URL=https://api.atsopt-seminar4.site
24 changes: 24 additions & 0 deletions week4/week4-HW/.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?
12 changes: 12 additions & 0 deletions week4/week4-HW/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# React + 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) uses [Babel](https://babeljs.io/) for Fast Refresh
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh

## Expanding the ESLint configuration

If you are developing a production application, we recommend using TypeScript with type-aware lint rules enabled. Check out the [TS template](https://github.com/vitejs/vite/tree/main/packages/create-vite/template-react-ts) for information on how to integrate TypeScript and [`typescript-eslint`](https://typescript-eslint.io) in your project.
33 changes: 33 additions & 0 deletions week4/week4-HW/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import js from '@eslint/js'
import globals from 'globals'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'

export default [
{ ignores: ['dist'] },
{
files: ['**/*.{js,jsx}'],
languageOptions: {
ecmaVersion: 2020,
globals: globals.browser,
parserOptions: {
ecmaVersion: 'latest',
ecmaFeatures: { jsx: true },
sourceType: 'module',
},
},
plugins: {
'react-hooks': reactHooks,
'react-refresh': reactRefresh,
},
rules: {
...js.configs.recommended.rules,
...reactHooks.configs.recommended.rules,
'no-unused-vars': ['error', { varsIgnorePattern: '^[A-Z_]' }],
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
},
]
13 changes: 13 additions & 0 deletions week4/week4-HW/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
Copy link

Choose a reason for hiding this comment

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

3주차 과제 때 배웠는데 사소하지만 ko로 바꾸면 좋을 것 같습니다!

<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</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>
31 changes: 31 additions & 0 deletions week4/week4-HW/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "week4-hw",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"lint": "eslint .",
"preview": "vite preview"
},
"dependencies": {
"@emotion/react": "^11.14.0",
"@emotion/styled": "^11.14.0",
"axios": "^1.9.0",
"react": "^19.1.0",
"react-dom": "^19.1.0",
"react-router": "^7.6.0"
},
"devDependencies": {
"@eslint/js": "^9.25.0",
"@types/react": "^19.1.2",
"@types/react-dom": "^19.1.2",
"@vitejs/plugin-react-swc": "^3.9.0",
"eslint": "^9.25.0",
"eslint-plugin-react-hooks": "^5.2.0",
"eslint-plugin-react-refresh": "^0.4.19",
"globals": "^16.0.0",
"vite": "^6.3.5"
}
}
1 change: 1 addition & 0 deletions week4/week4-HW/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.
6 changes: 6 additions & 0 deletions week4/week4-HW/src/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { RouterProvider } from "react-router";
import router from "./router/Router";
const App = () => {
return <RouterProvider router={router} />;
};
export default App;
1 change: 1 addition & 0 deletions week4/week4-HW/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.
83 changes: 83 additions & 0 deletions week4/week4-HW/src/components/MyPageInput.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/** @jsxImportSource @emotion/react */
import { css } from "@emotion/react";
import { useState, useEffect } from "react";
import { btnStyle } from "../pages/Login/LoginStyles.styles";
import {
mainInputStyle,
myPageInputArea,
} from "../pages/MyPage/MyPageStyles.style";
import api from "../services/api";

const MyPageInput = ({ title, label, btnTxt, name }) => {
const [inputValue, setInputValue] = useState("");
const [nicknameList, setNicknameList] = useState([]);
const [userId, setUserId] = useState("");
useEffect(() => {
const storedUserId = localStorage.getItem("userId");
if (storedUserId) {
setUserId(storedUserId);
}
}, []);
const handleUpdateNickname = async () => {
if (name === "myInfo") {
try {
const response = await api.patch(
"/api/v1/users",
{ nickname: inputValue },
{ headers: { userId } }
);
alert(`${inputValue}로 닉네임 변경 성공 !`);
} catch (error) {
console.error("Error updating nickname", error);
alert("닉네임을 변경하는 데 실패했습니다.");
}
} else if (name === "view") {
try {
const response = await api.get(`/api/v1/users?keyword=${inputValue}`);

const nicknameList = response.data.data.nicknameList || [];
if (nicknameList.length === 0) {
alert("닉네임이 없습니다.");
} else {
console.log("User details fetched successfully", response.data);
setNicknameList(nicknameList);
}
} catch (error) {
console.error("Error fetching user details", error);
alert("닉네임을 조회하는 데 실패했습니다.");
}
Comment on lines +34 to +48
Copy link
Member

Choose a reason for hiding this comment

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

닉네임 변경이 아닌 부분은 따로 역할 분리를 하는 게 좋을 거 같아요 ! 혹시 하나로 작성하신 이유가 있을까요?

} else {
alert("회원 정보 보기 페이지에서는 닉네임을 변경할 수 없습니다.");
}
};

return (
<div css={myPageInputArea}>
<h1>{title}</h1>
<div>{label}</div>
<input
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
placeholder={label}
type="text"
css={mainInputStyle}
/>
<div css={btnStyle} onClick={handleUpdateNickname}>
{btnTxt}
</div>
Comment on lines +65 to +67
Copy link
Member

Choose a reason for hiding this comment

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

클릭 가능한 요소는 button 태그를 사용하는게 시맨틱 측면에서 좋을듯 합니다 !!!


{name === "view" && nicknameList.length > 0 && (
<div>
<h3>검색된 닉네임 목록</h3>
<ul>
{nicknameList.map((nickname, index) => (
<li key={index}>{nickname}</li>
))}
Comment on lines +73 to +75

Choose a reason for hiding this comment

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

리스트 렌더링 시 key={index}를 쓰고 있는데, key={nickname}을 쓰면 React가 더 잘 추적해줄 수 있어 더 안정적이라고 합니다!! 😀

</ul>
</div>
)}
</div>
);
};

export default MyPageInput;
9 changes: 9 additions & 0 deletions week4/week4-HW/src/main.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import App from "./App.jsx";

createRoot(document.getElementById("root")).render(
<StrictMode>
<App />
</StrictMode>
);
76 changes: 76 additions & 0 deletions week4/week4-HW/src/pages/Login/Login.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/** @jsxImportSource @emotion/react */
import { css } from "@emotion/react";
import { useState, useEffect } from "react";
import { useNavigate } from "react-router";
import axios from "axios";
import api from "../../services/api";
import {
mainStyle,
formStyle,
formInputStyle,
btnStyle,
} from "./LoginStyles.styles";

const Login = () => {
const [loginId, setLoginId] = useState("");
const [password, setPassword] = useState("");
const [userId, setUserId] = useState("");
const navigate = useNavigate();
useEffect(() => {
if (userId) {
localStorage.setItem("userId", userId); // userId가 변경되면 localStorage에 저장
}
}, [userId]);
const fetchDetailData = async () => {
try {
const res = await api.post("/api/v1/auth/signin", {
Copy link

Choose a reason for hiding this comment

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

api 부분 axiosInstance 등을 통해 따로 분리해서 관리하면 더 편하고 깔끔해지더라구요!! 한 번 시도해보시는 걸 추천드리옵니다

loginId,
password,
});
console.log(res.data.data.userId);
setUserId(res.data.data.userId);
localStorage.setItem("userId", userId);
Comment on lines +31 to +32

Choose a reason for hiding this comment

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

setUserId() 직후에 localStorage에 저장해서, 아직 이전 상태값이 들어갈 수도 있어요!! 응답 받은 userId를 변수에 따로 담아서 바로 저장해주면 더 확실할 것 같습니다!!

navigate("/mypage/myInfo");
} catch (error) {
console.log(error.code);
}
};
const handleLogin = () => {
if (loginId && password) {
fetchDetailData();
} else {
alert("아이디와 비밀번호를 모두 입력해주세요.");
}
};

return (
<div css={mainStyle}>
<h2>로그인</h2>
<form css={formStyle}>
<h3>비밀번호</h3>
<div css={formInputStyle}>
<input
value={loginId}
onChange={(e) => setLoginId(e.target.value)}
placeholder="아이디"
type="text"
/>
<input
value={password}
onChange={(e) => setPassword(e.target.value)}
placeholder="비밀번호"
type="password"
/>
</div>
<div css={btnStyle} onClick={handleLogin}>
로그인
</div>
Comment on lines +65 to +67

Choose a reason for hiding this comment

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

로그인/회원가입 버튼이 div로 되어 있어서, disabled 같은 속성이 실제로는 적용되지 않는 것 같아 접근성이나 키보드 이동 같은 면에서도 button 태그로 바꾸면 더 좋을 것 같아요:)

<button css={btnStyle} onClick={handleLogin}>로그인</button>

<div css={btnStyle} onClick={() => navigate("/sign")}>
회원가입
</div>
</form>
</div>
);
};

export default Login;
44 changes: 44 additions & 0 deletions week4/week4-HW/src/pages/Login/LoginStyles.styles.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/** @jsxImportSource @emotion/react */
import { css } from "@emotion/react";
export const mainStyle = css`
background-color: rgb(228, 228, 228);
width: 100%;
height: 100vh;
display: flex;
flex-direction: column;
gap: 50px;
align-items: center;
`;

export const formStyle = css`
display: flex;
flex-direction: column;
gap: 20px;
justify-content: center;
align-items: center;
`;
export const formInputStyle = css`
display: flex;
flex-direction: row;
gap: 20px;
& input {
width: 200px;
height: 30px;
border-radius: 10px;
border: none;
padding-left: 10px;
&:focus {
outline: none;
border: 1px solid black;
}
}
`;
export const btnStyle = css`
background-color: rgb(0, 0, 0);
color: white;
width: 100px;
padding: 10px;
border-radius: 10px;
cursor: pointer;
text-align: center;
`;
Loading