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

[15팀 이규리] [Chapter 1-1] 프레임워크 없이 SPA 만들기 #17

Open
wants to merge 35 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
1f9f326
refactor: 페이지 및 컴포넌트 모듈 분리
9yurilee Dec 15, 2024
81248b1
refactor: 헤더, 푸터 모듈 적용
9yurilee Dec 15, 2024
d79060c
feat: 라우팅 구현 및 라우팅 예외 처리(#1, #7)
9yurilee Dec 15, 2024
742d43f
fix: 네비게이션 a태그 검색 방법 수정
9yurilee Dec 15, 2024
596dbc6
refactor: UI/이벤트 파일 분리
9yurilee Dec 15, 2024
622c479
feat: 로그인 기능 추가
9yurilee Dec 15, 2024
6bdf1e6
refactor: 초기화함수 대표함수 initEvents로 설정 및 적용
9yurilee Dec 15, 2024
16883ab
feat: 네비게이션 탭 노출조건 localStorage의 user 활용
9yurilee Dec 15, 2024
954a47f
refactor: export default로 변경
9yurilee Dec 15, 2024
37ff5ca
feat: 로그인 전 profile 접근 막기 추가
9yurilee Dec 15, 2024
1927a35
chore: 로그인 input id 수정
9yurilee Dec 15, 2024
318968c
feat: 로그아웃 기능 추가
Dec 16, 2024
1cf4f0f
fix: 로그인 버튼과 프로필 업데이트 버튼과의 중복 이슈 수정
Dec 16, 2024
7085e1c
feat: 프로필 업데이트 기능 기본 작업 완료
Dec 16, 2024
e931a37
feat: 네비게이션 상태 관리 적용 기초 작업
Dec 16, 2024
f724373
fix: 로그인 폼 이벤트 id 타겟 수정
Dec 16, 2024
21c1073
fix: 함수 연결 이슈 초기화함수 분리해서 적용하는 것으로 수정
Dec 17, 2024
d999b8f
fix: 로그인 유효성 검사 조건 수정
Dec 17, 2024
8e7ed74
refactor: 로그아웃 이벤트 네비게이션 이벤트핸들러로 위치 이동
9yurilee Dec 17, 2024
77f66d5
refactor: 로그아웃 이벤트 네비게이션 이벤트핸들러로 위치 이동
Dec 17, 2024
a4e0bd6
Merge branch 'main' of https://github.com/9yurilee/front_4th_chapter1-1
9yurilee Dec 17, 2024
0530c7c
fix: 프로필 업데이트 로그인 submit과 중복 문제해결
9yurilee Dec 17, 2024
905ff67
refactor: 로그아웃 이벤트 네비게이션 이벤트 핸들러에서 처리하는 것으로 수정
9yurilee Dec 17, 2024
5ea3228
docs: 필요없는 주석 제거 및 추가
9yurilee Dec 17, 2024
5659e3c
chore: 개행 맞춤
9yurilee Dec 17, 2024
08c5453
fix: e2e 테스트 SPA 기본 기능 1 수정
9yurilee Dec 17, 2024
916d2ee
feat: 로그인한 사용자가 로그인 페이지 접근 막기(심화)
9yurilee Dec 17, 2024
69dfdb4
style: 네비게이션 홈 탭 스타일링 추가
9yurilee Dec 17, 2024
f525cdb
refactor: props drilling 수정 위해 Header의 로그인 유무는 state활용하는 것으로 수정
9yurilee Dec 18, 2024
ac4e194
fix: form 이벤트 버블링으로 로그인 안되는 문제 해결
9yurilee Dec 18, 2024
461a27f
refactor: 상수 적용, 로컬스토리지 핸들러 분리 등 리팩토링 진행
9yurilee Dec 18, 2024
6a40333
fix: handleLogin username 옵셔널 체이닝 추가
9yurilee Dec 18, 2024
88a00ee
refactor: 복잡도 증가로 인한 상수화 복구
9yurilee Dec 18, 2024
9c4924c
feat: 해시 라우터 추가 완료
9yurilee Dec 18, 2024
ed86af9
rename: 컴포넌트명 변경
9yurilee Dec 19, 2024
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
7 changes: 7 additions & 0 deletions src/components/common/Footer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const Footer = () => `
<footer class="bg-gray-200 p-4 text-center">
<p>&copy; 2024 항해플러스. All rights reserved.</p>
</footer>
`;

export default Footer;
29 changes: 29 additions & 0 deletions src/components/common/Header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { state } from "../../store/store";

const Header = () => {
const LoginMenu = (isLoggedIn) => {
return isLoggedIn
? `
<li><a href="/profile" class="text-gray-600">프로필</a></li>
<li><a href="/login" id="logout" class="text-gray-600">로그아웃</a></li>
`
: `
<li><a href="/login" class="text-gray-600">로그인</a></li>
`;
};

return `
<header class="bg-blue-600 text-white p-4 sticky top-0">
<h1 class="text-2xl font-bold">항해플러스</h1>
</header>

<nav class="bg-white shadow-md p-2 sticky top-14">
<ul class="flex justify-around">
<li><a href="/" class="text-blue-600 font-bold">홈</a></li>
${LoginMenu(state.user)}
</ul>
</nav>
`;
};

export default Header;
101 changes: 101 additions & 0 deletions src/components/pages/HomePage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import Header from "../common/Header";
import Footer from "../common/Footer";

export const HomePage = () => `
<div class="bg-gray-100 min-h-screen flex justify-center">
<div class="max-w-md w-full">
${Header()}
<main class="p-4">
<div class="mb-4 bg-white rounded-lg shadow p-4">
<textarea class="w-full p-2 border rounded" placeholder="무슨 생각을 하고 계신가요?"></textarea>
<button class="mt-2 bg-blue-600 text-white px-4 py-2 rounded">게시</button>
</div>

<div class="space-y-4">

<div class="bg-white rounded-lg shadow p-4">
<div class="flex items-center mb-2">
<img src="https://via.placeholder.com/40" alt="프로필" class="rounded-full mr-2">
<div>
<p class="font-bold">홍길동</p>
<p class="text-sm text-gray-500">5분 전</p>
</div>
</div>
<p>오늘 날씨가 정말 좋네요. 다들 좋은 하루 보내세요!</p>
<div class="mt-2 flex justify-between text-gray-500">
<button>좋아요</button>
<button>댓글</button>
<button>공유</button>
</div>
</div>

<div class="bg-white rounded-lg shadow p-4">
<div class="flex items-center mb-2">
<img src="https://via.placeholder.com/40" alt="프로필" class="rounded-full mr-2">
<div>
<p class="font-bold">김철수</p>
<p class="text-sm text-gray-500">15분 전</p>
</div>
</div>
<p>새로운 프로젝트를 시작했어요. 열심히 코딩 중입니다!</p>
<div class="mt-2 flex justify-between text-gray-500">
<button>좋아요</button>
<button>댓글</button>
<button>공유</button>
</div>
</div>

<div class="bg-white rounded-lg shadow p-4">
<div class="flex items-center mb-2">
<img src="https://via.placeholder.com/40" alt="프로필" class="rounded-full mr-2">
<div>
<p class="font-bold">이영희</p>
<p class="text-sm text-gray-500">30분 전</p>
</div>
</div>
<p>오늘 점심 메뉴 추천 받습니다. 뭐가 좋을까요?</p>
<div class="mt-2 flex justify-between text-gray-500">
<button>좋아요</button>
<button>댓글</button>
<button>공유</button>
</div>
</div>

<div class="bg-white rounded-lg shadow p-4">
<div class="flex items-center mb-2">
<img src="https://via.placeholder.com/40" alt="프로필" class="rounded-full mr-2">
<div>
<p class="font-bold">박민수</p>
<p class="text-sm text-gray-500">1시간 전</p>
</div>
</div>
<p>주말에 등산 가실 분 계신가요? 함께 가요!</p>
<div class="mt-2 flex justify-between text-gray-500">
<button>좋아요</button>
<button>댓글</button>
<button>공유</button>
</div>
</div>

<div class="bg-white rounded-lg shadow p-4">
<div class="flex items-center mb-2">
<img src="https://via.placeholder.com/40" alt="프로필" class="rounded-full mr-2">
<div>
<p class="font-bold">정수연</p>
<p class="text-sm text-gray-500">2시간 전</p>
</div>
</div>
<p>새로 나온 영화 재미있대요. 같이 보러 갈 사람?</p>
<div class="mt-2 flex justify-between text-gray-500">
<button>좋아요</button>
<button>댓글</button>
<button>공유</button>
</div>
</div>
</div>
</main>

${Footer()}
</div>
</div>
`;
23 changes: 23 additions & 0 deletions src/components/pages/LoginPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export const LoginPage = () => `
<main class="bg-gray-100 flex items-center justify-center min-h-screen">
<div class="bg-white p-8 rounded-lg shadow-md w-full max-w-md">
<h1 class="text-2xl font-bold text-center text-blue-600 mb-8">항해플러스</h1>
<form id="login-form">
<div class="mb-4">
<input type="text" id="username" placeholder="사용자 이름" class="w-full p-2 border rounded">
</div>
<div class="mb-6">
<input type="password" id="password" placeholder="비밀번호" class="w-full p-2 border rounded">
</div>
<button type="submit" class="w-full bg-blue-600 text-white p-2 rounded font-bold">로그인</button>
</form>
<div class="mt-4 text-center">
<a href="#" class="text-blue-600 text-sm">비밀번호를 잊으셨나요?</a>
</div>
<hr class="my-6">
<div class="text-center">
<button class="bg-green-500 text-white px-4 py-2 rounded font-bold">새 계정 만들기</button>
</div>
</div>
</main>
`;
15 changes: 15 additions & 0 deletions src/components/pages/NotFoundPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const NotFoundPage = () => `
<main class="bg-gray-100 flex items-center justify-center min-h-screen">
<div class="bg-white p-8 rounded-lg shadow-md w-full text-center" style="max-width: 480px">
<h1 class="text-2xl font-bold text-blue-600 mb-4">항해플러스</h1>
<p class="text-4xl font-bold text-gray-800 mb-4">404</p>
<p class="text-xl text-gray-600 mb-8">페이지를 찾을 수 없습니다</p>
<p class="text-gray-600 mb-8">
요청하신 페이지가 존재하지 않거나 이동되었을 수 있습니다.
</p>
<a href="/" class="bg-blue-600 text-white px-4 py-2 rounded font-bold">
홈으로 돌아가기
</a>
</div>
</main>
`;
74 changes: 74 additions & 0 deletions src/components/pages/ProfilePage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import Header from "../common/Header";
import Footer from "../common/Footer";
import { state } from "../../store/store";

export const ProfilePage = () => {
return `
<div id="root">
<div class="bg-gray-100 min-h-screen flex justify-center">
<div class="max-w-md w-full">
${Header()}
<main class="p-4">
<div class="bg-white p-8 rounded-lg shadow-md">
<h2 class="text-2xl font-bold text-center text-blue-600 mb-8">
내 프로필
</h2>
<form id="profile-form">
<div class="mb-4">
<label
for="username"
class="block text-gray-700 text-sm font-bold mb-2"
>사용자 이름</label
>
<input
type="text"
id="username"
name="username"
value="${state.user?.username || ""}"
class="w-full p-2 border rounded"
/>
</div>
<div class="mb-4">
<label
for="email"
class="block text-gray-700 text-sm font-bold mb-2"
>이메일</label
>
<input
type="email"
id="email"
name="email"
value="${state.user?.email || ""}"
class="w-full p-2 border rounded"
/>
</div>
<div class="mb-6">
<label
for="bio"
class="block text-gray-700 text-sm font-bold mb-2"
>자기소개</label
>
<textarea
id="bio"
name="bio"
rows="4"
class="w-full p-2 border rounded"
>${state.user?.bio || ""}</textarea
>
</div>
<button
type="submit"
class="w-full bg-blue-600 text-white p-2 rounded font-bold"
>
프로필 업데이트
</button>
</form>
</div>
</main>

${Footer()}
</div>
</div>
</div>
`;
};
31 changes: 31 additions & 0 deletions src/events/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { navigateTo } from "../router/router.js";
import { setState } from "../store/store.js";
import { setStorage } from "../utils/storageHandler.js";

export const handleLogin = () => {
const username = document.getElementById("username")?.value || "";

const userInfo = { username, email: "", bio: "" };

if (username) {
setState({ user: userInfo }); // 상태 업데이트

setStorage("user", userInfo);

navigateTo("/");
} else {
alert("아이디 혹은 비밀번호를 확인해주세요.");
}
};

export const initLogin = () => {
document.body.addEventListener("submit", (e) => {
e.preventDefault();

// login-form을 정확히 찾기 위해 closest 사용
const loginForm = e.target.closest("#login-form");
if (!loginForm) return;

handleLogin();
});
};
23 changes: 23 additions & 0 deletions src/events/navigation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { navigateTo } from "../router/router.js";
import { clearStorage } from "../utils/storageHandler.js";
import { setState } from "./../store/store";

export const handleNavigation = (e) => {
const selected = e.target.closest("a");
if (!selected) return;

const isLogoutBtn = selected.hasAttribute("id") && selected.id === "logout";

Choose a reason for hiding this comment

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

isLogoutBtn을 함수로 만들면 어떨까요?

  const isLogoutBtn = (x => x.hasAttribute("id") && x.id === "logout";

  if (isLogoutBtn(selected)) {

Copy link
Author

Choose a reason for hiding this comment

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

아이고 석호님- 2주차 후딱 끝내고 리뷰해주신거 확인해야지 했는데 2주차를 꽉꽉 채워버렸네요..!
오 한 곳에서만 쓰인다는 생각에 함수로 뺄 생각은 한 번도 못해본 것 같아요!
단순히 변수로만 생각했었는데, 의견 감사합니다!


if (isLogoutBtn) {
clearStorage();
setState({ user: null }); // 상태 초기화
navigateTo("/login");
} else {
const path = selected.getAttribute("href");
navigateTo(path);
}
};

export const initNavigation = () => {
document.body.addEventListener("click", handleNavigation);
};
30 changes: 30 additions & 0 deletions src/events/profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { setStorage } from "../utils/storageHandler";
import { setState } from "./../store/store";

const handleProfileUpdate = () => {
const usernameInput = document.getElementById("username")?.value;
const emailInput = document.getElementById("email")?.value;
const bioInput = document.getElementById("bio")?.value;

const updatedInfo = {
username: usernameInput,
email: emailInput,
bio: bioInput,
};

setStorage("user", updatedInfo);

setState({ user: updatedInfo });
};

export const initProfile = () => {
document.body.addEventListener("submit", (e) => {
e.preventDefault();

// profile-form을 정확히 찾기 위해 closest 사용
const profileForm = e.target.closest("#profile-form");
if (!profileForm) return;

handleProfileUpdate();
});
};
Loading
Loading