Skip to content

Commit

Permalink
Merge pull request #62 from Soongsil-Developers/FinallyBlog
Browse files Browse the repository at this point in the history
[#63] 전반적인 수정 후 최종본
  • Loading branch information
hoyyChoi authored Sep 16, 2022
2 parents 02a3f76 + 6fdc104 commit c77331a
Show file tree
Hide file tree
Showing 16 changed files with 158 additions and 48 deletions.
4 changes: 2 additions & 2 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ function App() {
<Route path='/register' element={<Signup />}/>
<Route path='/settings' element={<Settings />}/>
<Route path='/editor' element={<Newarticle />}/>
<Route path='/a' element={<Profile />}/>
<Route path='/b' element={<ArticleDetail />}/>
<Route path='/:username' element={<Profile />}/>
<Route path='/article/:slug' element={<ArticleDetail />}/>
</Routes>
<Footer />
</div>
Expand Down
2 changes: 2 additions & 0 deletions src/atoms/auth.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export const commentsState = atom({
default:[]
})



// export const newArticleState = atom({
// key: 'src/atoms/auth.jsx-newArticleState',
// default:{
Expand Down
8 changes: 6 additions & 2 deletions src/component/Article/ArticleContainer.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import { useRecoilValue } from 'recoil'
import { authState } from '../../atoms/auth'
import { authState,commentsState } from '../../atoms/auth'
import ArticleContents from './ArticleContents'
import ShowComment from './ShowComment'
import WriteCommet from './WriteCommet'
Expand All @@ -9,11 +9,15 @@ import { Link } from 'react-router-dom'
const ArticleContainer = ({data}) => {

const auth = useRecoilValue(authState)
const comments = useRecoilValue(commentsState)

return (
<div className="container page">

<ArticleContents data={data}/>
{auth?<WriteCommet />:<div style={{textAlign:'center', margin:'10px'}}>
<div style={{textAlign:'center'}}>댓글 갯수 : {comments.length}</div>
{auth ? <WriteCommet />
:<div style={{textAlign:'center', margin:'10px'}}>
<Link to="/login">sign in</Link> or <Link to="/register">sign up</Link>
</div>}
<ShowComment />
Expand Down
27 changes: 22 additions & 5 deletions src/component/Article/ArticleDetail.jsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
import React from 'react'
import ArticleContainer from './ArticleContainer'
import ArticleTitle from './ArticleTitle'
import { getSlugArticle } from '../../remote/index'
import { getSlugArticle,getLoginComment,getComment } from '../../remote/index'
import { useEffect } from 'react'
import { useRecoilValue } from 'recoil'
import { slugState } from '../../atoms/auth'
import { useRecoilValue, useRecoilState } from 'recoil'
import { slugState, authState, commentsState } from '../../atoms/auth'
import { useState } from 'react'

const ArticleDetail = () => {

const slug = useRecoilValue(slugState)
const [data,setArticleData]=useState()

const [comments, setComments] = useRecoilState(commentsState);
const auth = useRecoilValue(authState)

useEffect(()=>{
getSlugArticle(localStorage.getItem('slug'))
.then(res=>{
setArticleData(res.data.article)
}).catch(err=>{
console.log(err)
})
},[])
auth ? (getLoginComment(localStorage.getItem('slug'))
.then((res) => {
setComments(res.data.comments)
})
.catch((err) => {
console.log(err);
}))
: (getComment(localStorage.getItem('slug'))
.then((res) => {
setComments(res.data.comments)
})
.catch((err) => {
console.log(err);
}))
},[localStorage.getItem('slug')])


return (
Expand Down
28 changes: 23 additions & 5 deletions src/component/Article/ArticleTitle.jsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
import React from "react";
import { useNavigate } from "react-router-dom";
import { useRecoilValue } from "recoil";
import { commentsState } from "../../atoms/auth";
import { deleteSlugArticle } from "../../remote/index";
import WritterInfo from "../WritterInfo";


const ArticleTitle = ({data}) => {

const navigate = useNavigate()


const deleteArticle =()=>{
deleteSlugArticle(data.slug)
.then(res=>{
navigate('/')
}).catch(err=>{
console.log(err)
alert('다른 상대방의 article은 지울 수 없습니다.')
})
}

return (
<div className="banner">
<div className="container">
<h1>{data&&data.title}</h1>

<div className="article-meta">
<WritterInfo data={data}/>
<button className="btn btn-sm btn-outline-secondary">
<i className="ion-plus-round"></i>
&nbsp; Follow {data&&data.author.username}
</button>
&nbsp;&nbsp;
<button className="btn btn-sm btn-outline-primary">
<i className="ion-heart"></i>
&nbsp; Favorite Post <span className="counter">({data&&data.favoritesCount})</span>
</button>
&nbsp;&nbsp;
<button className="btn btn-sm btn-outline-primary" onClick={deleteArticle}>
<i class="ion-trash-a"></i>
&nbsp; Delete
</button>
</div>
</div>
</div>
Expand Down
22 changes: 16 additions & 6 deletions src/component/Article/ShowComment.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,33 @@ import React from "react";
import { useState } from "react";
import { useEffect } from "react";
import { useRecoilValue,useRecoilState } from "recoil";
import { commentsState, slugState } from "../../atoms/auth";
import { getComment } from "../../remote/index";
import { commentsState, slugState,authState} from "../../atoms/auth";
import { getComment, getLoginComment} from "../../remote/index";
import WritterInfo from "../WritterInfo";


const ShowComment = () => {
const slug = useRecoilValue(slugState);
let [comments, setComments] = useRecoilState(commentsState);
const [comments, setComments] = useRecoilState(commentsState);
const auth = useRecoilValue(authState)



useEffect(() => {
getComment(slug)
auth ? (getLoginComment(slug)
.then((res) => {
setComments(res.data.comments)
})
.catch((err) => {
console.log(err);
}))
: (getComment(slug)
.then((res) => {
console.log(res.data.comments)
setComments(res.data.comments)
})
.catch((err) => {
console.log(err);
});
}))
}, [comments.length]);

return (
Expand Down
17 changes: 10 additions & 7 deletions src/component/Article/WriteCommet.jsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
import React, { useEffect } from 'react'
import { useState } from 'react'
import { useRecoilValue } from 'recoil'
import { commentsState, slugState, userState } from '../../atoms/auth'
import { useRecoilValue,useRecoilState } from 'recoil'
import { commentsState, slugState} from '../../atoms/auth'
import { postComment } from '../../remote/index'

const WriteCommet = () => {

const [body,setTextComment] = useState('')
const slug = useRecoilValue(slugState)
const user = useRecoilValue(userState)
let comments = useRecoilValue(commentsState)
const [comments,setComments] = useRecoilState(commentsState)


const submitComment = (e) =>{
e.preventDefault()

postComment(slug,{body})
.then(res=>{
comments = [...comments,res.data.comment]
console.log(comments)
setComments([...comments,res.data.comment])
//rerender 되면서 코멘트 부분이 추가됨. 페이지 전환
}).catch(err=>{
console.log(err)
})

setTextComment('')

}



return (
Expand All @@ -38,7 +41,7 @@ return (
</div>
<div className="card-footer">
<img
src="http://i.imgur.com/Qr71crq.jpg"
src="https://api.realworld.io/images/smiley-cyrus.jpeg"
className="comment-author-img"
/>
<button className="btn btn-sm btn-primary" type='submit'>Post Comment</button>
Expand Down
2 changes: 1 addition & 1 deletion src/component/ArticleList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const ArticleList = ({data}) => {
const spaceArticle = () =>{
localStorage.setItem('slug',data.slug)
setSlug(data.slug)
navigate('/b')
navigate(`/article/${localStorage.getItem('slug')}`)
}

// data.slug를 상태에 업데이트를 시켜서 Api 연결 get slug -> ARTICLE DETAIL 컴포넌트 -> 업데이트된 정보를 뿌려준다.
Expand Down
3 changes: 0 additions & 3 deletions src/component/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ const Home = () => {
<div className="col-md-9">
<div className="feed-toggle">
<ul className="nav nav-pills outline-active">
<li className="nav-item">
<a className="nav-link disabled" href="" style={auth?{display:'flex'}:{display:'none'}}>Your Feed</a>
</li>
<li className="nav-item">
<a className="nav-link active" href="">Global Feed</a>
</li>
Expand Down
2 changes: 1 addition & 1 deletion src/component/Navbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const Navbar = () => {
getProfile(user.username)
.then((res)=>{
setProfile(res.data.profile)
navigate('/a')
navigate(`/${user.username}`)
}).catch((err)=>{
console.log(err)
})
Expand Down
7 changes: 4 additions & 3 deletions src/component/Newarticle.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ const Newarticle = () => {
const submitArticle = (e)=>{
e.preventDefault()

createArticle({title,description,body,tag},{user})
createArticle({title,description,body,tag})
.then(res=>{
setSlug(res.data.article.slug)
navigate('/b')
localStorage.setItem('slug',res.data.article.slug)
navigate(`/article/${localStorage.getItem('slug')}`)
}).catch(err=>{
console.log(err)
alert('이미 있는 제목입니다.')
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/component/Profile.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const Profile = () => {
const auth = useRecoilValue(authState)

useEffect(()=>{
auth?(getLoginArticles(profile.username)
auth ? (getLoginArticles(profile.username)
.then(res=>{
setArticleData(res.data.articles)

Expand Down
6 changes: 4 additions & 2 deletions src/component/Signin.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ const Signin = () => {
setAuth(true)
navigate('/')
}
).catch(err=>
console.log(err)
).catch(err=>{
alert('이메일 또는 비밀번호가 잘못 입력 되었습니다.')
}

)
}

Expand Down
9 changes: 4 additions & 5 deletions src/component/Signup.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ const Signup = () => {
navigate('/')

})
.catch((err)=>console.log(err));
.catch((err)=>{
alert('이메일 또는 사용자 이름이 중복됩니다'+' 아니면 칸이 비어있습니다')

});

}

Expand All @@ -43,10 +46,6 @@ const Signup = () => {
<a href="/login">Have an account?</a>
</p>

<ul className="error-messages">
<li>That email is already taken</li>
</ul>

<form onSubmit={(e)=>handleSubmit(e)}>
<fieldset className="form-group">
<input className="form-control form-control-lg" type="text" placeholder="Your Name"
Expand Down
2 changes: 1 addition & 1 deletion src/component/WritterInfo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const WritterInfo = ({data}) => {
getProfile(data.author.username)
.then((res)=>{
setProfile(res.data.profile)
navigate('/a')
navigate(`/${data.author.username}`)
}).catch((err)=>{
console.log(err)
})
Expand Down
Loading

0 comments on commit c77331a

Please sign in to comment.