-
Notifications
You must be signed in to change notification settings - Fork 102
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
1 parent
9d3c4c0
commit 8c3412c
Showing
3 changed files
with
101 additions
and
36 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { memo, useCallback, useEffect, useState } from 'react'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import { useParams } from 'react-router-dom'; | ||
import shallowequal from 'shallowequal'; | ||
import { fetchComments, addComment } from '../../store-redux/comments/actions'; | ||
import ArticleComments from '../../components/article-comment'; | ||
import listToTree from '../../utils/list-to-tree'; | ||
import useStoreSelector from '../../hooks/use-selector'; | ||
|
||
function ArticleCommentsContainer() { | ||
const dispatch = useDispatch(); | ||
const params = useParams(); | ||
const [replyTo, setReplyTo] = useState(null); | ||
|
||
useEffect(() => { | ||
dispatch(fetchComments(params.id)); | ||
}, [params.id, dispatch]); | ||
|
||
const select = useSelector( | ||
state => ({ | ||
comments: state.comments.comments, | ||
}), | ||
shallowequal, | ||
); | ||
|
||
const storeSelect = useStoreSelector( | ||
state => ({ | ||
exists: state.session.exists, | ||
token: state.session.token, | ||
user: state.session.user, | ||
}), | ||
); | ||
|
||
const handleAddComment = useCallback((text, parentId) => { | ||
const parent = parentId ? { _id: parentId, _type: 'comment' } : { _id: params.id, _type: 'article' }; | ||
dispatch(addComment({ text, parent, article: params.id, token: storeSelect.token })); | ||
setReplyTo(null); | ||
}, [dispatch, params.id, storeSelect.token]); | ||
|
||
const organizedComments = listToTree(select.comments); | ||
return ( | ||
<ArticleComments | ||
comments={organizedComments} | ||
replyTo={replyTo} | ||
setReplyTo={setReplyTo} | ||
handleAddComment={handleAddComment} | ||
exists={storeSelect.exists} | ||
link="/login" | ||
user={storeSelect.user} | ||
/> | ||
); | ||
} | ||
|
||
export default memo(ArticleCommentsContainer); |
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