forked from EFUB/efub4-frontend-assignment-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
825 additions
and
80 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,25 +1,96 @@ | ||
import RegisterPage from "./pages/Register"; | ||
import CreatePage from "./pages/Write"; | ||
import ListPage from "./pages/List"; | ||
import DetailPage from "./pages/Detail"; | ||
import { Routes, Route, NavLink } from "react-router-dom"; | ||
import styled from "styled-components"; | ||
import { useState, useEffect } from "react"; | ||
import { GetPostsApi } from "./api/post"; | ||
import { GetUserHeartApi } from "./api/heart"; | ||
import { Navigate } from "react-router-dom"; | ||
|
||
function App() { | ||
const [posts, setPosts] = useState(null); | ||
const [heartPosts, setHeartPosts] = useState(); | ||
|
||
async function getPosts() { | ||
try { | ||
const data = await GetPostsApi(); | ||
setPosts(data); | ||
} catch (err) { | ||
console.log(err.message); | ||
} | ||
} | ||
|
||
async function getHeartPosts() { | ||
console.log("test"); | ||
try { | ||
const data = await GetUserHeartApi(); | ||
setHeartPosts(data); | ||
} catch (err) { | ||
console.log(err.message); | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
getPosts(); | ||
getHeartPosts(); | ||
}, []); | ||
|
||
return ( | ||
<div> | ||
<Navbar> | ||
<NavLink to="/">글 목록</NavLink> | ||
<NavLink to="/write">글쓰기</NavLink> | ||
<NavLink to="/register">로그인/회원가입</NavLink> | ||
<NavLink to="/heart">좋아요 누른 글</NavLink> | ||
<StyledNavLink to="/posts">글 목록</StyledNavLink> | ||
<StyledNavLink to="/write">글쓰기</StyledNavLink> | ||
<StyledNavLink to="/register">로그인/회원가입</StyledNavLink> | ||
<StyledNavLink to="/heart" onClick={getHeartPosts}> | ||
좋아요 누른 글 | ||
</StyledNavLink> | ||
</Navbar> | ||
<Routes> | ||
{/* <Route path="/" element={<ListPage />} /> */} | ||
{/* <Route path="/detail/:postId" element={<DetailPage />} /> */} | ||
<Route path="/" element={<Navigate to="/posts" />} /> | ||
<Route | ||
path="/posts" | ||
element={<ListPage posts={posts} setPosts={setPosts} />} | ||
/> | ||
<Route | ||
path="/posts/:postId" | ||
element={<DetailPage posts={posts} setPosts={setPosts} />} | ||
/> | ||
<Route path="/write" element={<CreatePage />} /> | ||
<Route path="/register" element={<RegisterPage />} /> | ||
{/* <Route path="/heart" element={<HeartPage />} /> */} | ||
<Route | ||
path="/heart" | ||
element={<ListPage posts={heartPosts} setPosts={setHeartPosts} />} | ||
/> | ||
</Routes> | ||
</div> | ||
); | ||
} | ||
|
||
export default App; | ||
const Navbar = styled.div``; | ||
|
||
const Navbar = styled.div` | ||
display: flex; | ||
justify-content: space-around; | ||
align-items: center; | ||
background-color: #333; | ||
padding: 10px 20px; | ||
`; | ||
|
||
const StyledNavLink = styled(NavLink)` | ||
color: white; | ||
text-decoration: none; | ||
font-size: 18px; | ||
padding: 10px 20px; | ||
&.active { | ||
background-color: #555; | ||
border-radius: 4px; | ||
} | ||
&:hover { | ||
background-color: #444; | ||
border-radius: 4px; | ||
} | ||
`; |
This file contains 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,37 @@ | ||
import client from "."; | ||
import axios from "axios"; | ||
|
||
export const GetCommentApi = async (postId) => { | ||
try { | ||
if (postId) { | ||
const res = await client.get( | ||
`${process.env.REACT_APP_SERVER_URL}/posts/${postId}/comments` | ||
); | ||
console.log(res.data); | ||
return res.data; | ||
} | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
}; | ||
|
||
export const PostCommentApi = async (postId, content) => { | ||
try { | ||
await client.post(`${process.env.REACT_APP_SERVER_URL}/comments`, { | ||
postId, | ||
content, | ||
}); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
}; | ||
|
||
export const DeleteCommentApi = async (commentId) => { | ||
try { | ||
await client.delete( | ||
`${process.env.REACT_APP_SERVER_URL}/comments/${commentId}` | ||
); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
}; |
This file contains 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,57 @@ | ||
import client from "."; | ||
import axios from "axios"; | ||
|
||
export const GetHeartApi = async (postId) => { | ||
try { | ||
if (postId) { | ||
const res = await client.get(`/posts/${postId}/hearts`); | ||
// console.log(res); | ||
return res.data; | ||
} | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
}; | ||
|
||
export const PostHeartApi = async (postId) => { | ||
try { | ||
const res = await client.post(`/posts/${postId}/hearts`); | ||
// console.log(res); | ||
} catch (err) { | ||
console.log(err.message); | ||
} | ||
}; | ||
|
||
export const DeleteHeartApi = async (postId) => { | ||
try { | ||
await client.delete(`/posts/${postId}/hearts`); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
}; | ||
|
||
export const GetUserHeartApi = async () => { | ||
try { | ||
const res = await client.get(`/posts/hearts`); | ||
return res.data; | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
}; | ||
|
||
export const FindIfLiked = async (postId) => { | ||
var liked = false; | ||
try { | ||
const list = await GetUserHeartApi(); | ||
if (list.length > 0) { | ||
list.forEach(function (post) { | ||
if (post.postId.toString() == postId) { | ||
liked = true; | ||
} | ||
}); | ||
} | ||
} catch (err) { | ||
console.log(err.message); | ||
} | ||
return liked; | ||
}; |
This file contains 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
This file contains 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
Oops, something went wrong.