-
Notifications
You must be signed in to change notification settings - Fork 77
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
[3팀 김나연] [Chapter 1-1] 프레임워크 없이 SPA 만들기 #34
base: main
Are you sure you want to change the base?
Changes from all commits
a9898ba
71579af
d16ac1b
f2daff1
4cb051b
8c6b9a5
f70ef75
3df32cc
2df1cab
7ce75ac
768c37b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Header from "./Header"; | ||
import Footer from "./Footer"; | ||
|
||
const DefaultLayout = (component) => { | ||
return ` | ||
<div> | ||
${Header()} | ||
${component()} | ||
${Footer()} | ||
</div> | ||
`; | ||
}; | ||
|
||
export default DefaultLayout; |
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>© 2024 항해플러스. All rights reserved.</p> | ||
</footer> | ||
`; | ||
|
||
export default Footer; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Nav from "./Nav"; | ||
|
||
const Header = () => ` | ||
<header class="bg-blue-600 text-white p-4 sticky top-0"> | ||
<h1 class="text-2xl font-bold">항해플러스</h1> | ||
</header> | ||
${Nav()} | ||
`; | ||
|
||
export default Header; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import isLogin from "../utils/isLogin"; | ||
|
||
const Nav = () => { | ||
const activateStyle = "text-blue-600 font-bold"; | ||
const deactivateStyle = "text-gray-600"; | ||
const handleActivate = (x) => (x === path ? activateStyle : deactivateStyle); | ||
const path = window.location.pathname; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nav를 컴포넌트로 분리하고 주소에 따라 다르게 해주셨네요..! 저는 이렇게 못했는데 배워갑니다! |
||
|
||
return ` | ||
<nav class="bg-white shadow-md p-2 sticky top-14"> | ||
<ul class="flex justify-around"> | ||
<li><a href="/" class="${handleActivate("/")}">홈</a></li> | ||
<li><a href="/profile" class="${handleActivate("/profile")}">프로필</a></li> | ||
<li>${ | ||
isLogin() | ||
? '<a id="logout" href="#" class="text-gray-600">로그아웃</a>' | ||
: '<a href="/login" class="text-gray-600">로그인</a>' | ||
}</li> | ||
</ul> | ||
</nav> | ||
`; | ||
}; | ||
|
||
export default Nav; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const render = (component) => { | ||
const root = document.getElementById("root"); | ||
|
||
root.innerHTML = ` | ||
<div class="bg-gray-100 min-h-screen flex justify-center"> | ||
<div class="max-w-md w-full"> | ||
${component} | ||
</div> | ||
</div> | ||
`; | ||
}; | ||
export default render; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,241 +1,45 @@ | ||
const MainPage = () => ` | ||
<div class="bg-gray-100 min-h-screen flex justify-center"> | ||
<div class="max-w-md w-full"> | ||
<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">홈</a></li> | ||
<li><a href="/profile" class="text-gray-600">프로필</a></li> | ||
<li><a href="#" class="text-gray-600">로그아웃</a></li> | ||
</ul> | ||
</nav> | ||
|
||
<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 class="bg-gray-200 p-4 text-center"> | ||
<p>© 2024 항해플러스. All rights reserved.</p> | ||
</footer> | ||
</div> | ||
</div> | ||
`; | ||
|
||
const ErrorPage = () => ` | ||
<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> | ||
`; | ||
|
||
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> | ||
<div class="mb-4"> | ||
<input type="text" placeholder="이메일 또는 전화번호" class="w-full p-2 border rounded"> | ||
</div> | ||
<div class="mb-6"> | ||
<input type="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> | ||
`; | ||
|
||
const ProfilePage = () => ` | ||
<div id="root"> | ||
<div class="bg-gray-100 min-h-screen flex justify-center"> | ||
<div class="max-w-md w-full"> | ||
<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-gray-600">홈</a></li> | ||
<li><a href="/profile" class="text-blue-600">프로필</a></li> | ||
<li><a href="#" class="text-gray-600">로그아웃</a></li> | ||
</ul> | ||
</nav> | ||
|
||
<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> | ||
<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="홍길동" | ||
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="[email protected]" | ||
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" | ||
> | ||
안녕하세요, 항해플러스에서 열심히 공부하고 있는 홍길동입니다.</textarea | ||
> | ||
</div> | ||
<button | ||
type="submit" | ||
class="w-full bg-blue-600 text-white p-2 rounded font-bold" | ||
> | ||
프로필 업데이트 | ||
</button> | ||
</form> | ||
</div> | ||
</main> | ||
|
||
<footer class="bg-gray-200 p-4 text-center"> | ||
<p>© 2024 항해플러스. All rights reserved.</p> | ||
</footer> | ||
</div> | ||
</div> | ||
</div> | ||
`; | ||
|
||
document.body.innerHTML = ` | ||
${MainPage()} | ||
${ProfilePage()} | ||
${LoginPage()} | ||
${ErrorPage()} | ||
`; | ||
import router from "./routes/router"; | ||
import handleProfile from "./pages/handleProfile"; | ||
import handleChangeProfile from "./pages/handleChangeProfile"; | ||
import handleLogin from "./pages/handleLogin"; | ||
import hashRouter from "./routes/hashRouter"; | ||
|
||
window.addEventListener("load", () => { | ||
if (window.location.hash) { | ||
hashRouter(); | ||
} else { | ||
router(); | ||
} | ||
}); | ||
Blue-Kite marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
window.addEventListener("popstate", () => router()); | ||
window.addEventListener("hashchange", () => hashRouter()); | ||
|
||
document.addEventListener("click", (e) => { | ||
if (e.target.tagName === "A") { | ||
e.preventDefault(); | ||
if (e.target.id === "logout") { | ||
localStorage.removeItem("user"); | ||
router("/login"); | ||
} else { | ||
const path = e.target.getAttribute("href"); | ||
router(path); | ||
handleProfile(); | ||
} | ||
} | ||
}); | ||
|
||
document.addEventListener("submit", (e) => { | ||
e.preventDefault(); | ||
const form = e.target; | ||
const formData = new FormData(form); | ||
const data = Object.fromEntries(formData); | ||
|
||
if (form.id == "login-form") { | ||
handleLogin(data); | ||
} | ||
if (form.id == "profile-form") { | ||
handleChangeProfile(data); | ||
} | ||
handleProfile(); | ||
}); | ||
Comment on lines
+33
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기서 처리하는 이벤트가 각 컴포넌트에서 정의되면 더 좋을 지 않을까 싶은데 어떻게 생각하시나요?? |
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.
DefaultLayout으로 따로 분리하니까 더 깔끔해보이고 좋은 것 같아요..! 수정하기도 편할 것 같아요ㅎㅎ