Skip to content

Commit 4c38ed5

Browse files
committed
배포하기 연습
1 parent e15e0ab commit 4c38ed5

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed

src/atoms/auth.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ export const slugState = atom({
3333
default:''
3434
})
3535

36-
36+
export const commentsState = atom({
37+
key:'src/atoms/auth.jsx-commentsState',
38+
default:[]
39+
})
3740

3841
// export const newArticleState = atom({
3942
// key: 'src/atoms/auth.jsx-newArticleState',

src/component/Article/ShowComment.jsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
import React from "react";
22
import { useState } from "react";
33
import { useEffect } from "react";
4-
import { useRecoilValue } from "recoil";
5-
import { slugState } from "../../atoms/auth";
4+
import { useRecoilValue,useRecoilState } from "recoil";
5+
import { commentsState, slugState } from "../../atoms/auth";
66
import { getComment } from "../../remote/index";
77
import WritterInfo from "../WritterInfo";
88

99
const ShowComment = () => {
1010
const slug = useRecoilValue(slugState);
11-
const [comments, setComments] = useState([]);
11+
let [comments, setComments] = useRecoilState(commentsState);
1212

1313
useEffect(() => {
1414
getComment(slug)
1515
.then((res) => {
16-
setComments(res.data.comments);
16+
console.log(res.data.comments)
17+
setComments(res.data.comments)
1718
})
1819
.catch((err) => {
1920
console.log(err);
2021
});
21-
}, [comments]);
22+
}, [comments.length]);
2223

2324
return (
2425
<div className="row">

src/component/Article/WriteCommet.jsx

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
1-
import React from 'react'
1+
import React, { useEffect } from 'react'
22
import { useState } from 'react'
33
import { useRecoilValue } from 'recoil'
4-
import { slugState, userState } from '../../atoms/auth'
4+
import { commentsState, slugState, userState } from '../../atoms/auth'
55
import { postComment } from '../../remote/index'
66

77
const WriteCommet = () => {
88

99
const [body,setTextComment] = useState('')
1010
const slug = useRecoilValue(slugState)
1111
const user = useRecoilValue(userState)
12-
13-
const submitComment = (e) =>{
14-
e.preventDefault()
15-
16-
postComment(slug,{body},{user})
17-
.then(res=>{
18-
console.log(res)
19-
//rerender 되면서 코멘트 부분이 추가됨. 페이지 전환
20-
}).catch(err=>{
21-
console.log(err)
22-
})
23-
}
12+
let comments = useRecoilValue(commentsState)
13+
14+
const submitComment = (e) =>{
15+
e.preventDefault()
16+
17+
postComment(slug,{body})
18+
.then(res=>{
19+
comments = [...comments,res.data.comment]
20+
console.log(comments)
21+
//rerender 되면서 코멘트 부분이 추가됨. 페이지 전환
22+
}).catch(err=>{
23+
console.log(err)
24+
})
25+
}
26+
2427

2528
return (
2629
<div className="row">

src/remote/index.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ const getSlugArticle = (slug) => conduitAxios.get(`/articles/${slug}`)
289289
]
290290
}}
291291
*/
292-
const getComment = (slug) => conduitAxios.get(`/articles/${slug}/comments`)
292+
const getComment = (slug) => conduitAxios.get(`/articles/${slug}/comments`,{headers:{authorization:`Bearer ${localStorage.getItem('token')}`}})
293293

294294
/**
295295
@param {string} slug
@@ -299,7 +299,7 @@ const getComment = (slug) => conduitAxios.get(`/articles/${slug}/comments`)
299299
}
300300
}} comment
301301
@returns {{
302-
comments: [
302+
comment: [
303303
{
304304
id: 0;
305305
createdAt: 2022-09-13T09:11:17.036Z;
@@ -316,6 +316,6 @@ const getComment = (slug) => conduitAxios.get(`/articles/${slug}/comments`)
316316
}}
317317
*/
318318

319-
const postComment =(slug,comment,{user}) => conduitAxios.post(`/articles/${slug}/comments`,{comment},{headers:{authorization:`Bearer ${user.token}`}})
319+
const postComment =(slug,comment) => conduitAxios.post(`/articles/${slug}/comments`,{comment},{headers:{authorization:`Bearer ${localStorage.getItem('token')}`}})
320320

321321
export {postRegisterUser,postLoginUser,getLoginUser,putLoginUser,getProfile,getGlobalLoginArticles,getGlobalArticles,getArticles,getLoginArticles,createArticle,getSlugArticle,getComment,postComment};

0 commit comments

Comments
 (0)