Skip to content

Commit

Permalink
lecture-5
Browse files Browse the repository at this point in the history
  • Loading branch information
danilandreevic committed Oct 12, 2024
1 parent dd531cb commit b3de5cc
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 20 deletions.
3 changes: 0 additions & 3 deletions src/components/article-comment/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ const ArticleComments = ({ comments, replyTo, setReplyTo, handleAddComment, exis
const cn = bem('ArticleComments');
const maxDepth = 3;
const replyFormRef = useRef(null);
useEffect(() => {
console.log(comments)
}, [comments]);
useEffect(() => {
if (replyFormRef.current) {
replyFormRef.current.scrollIntoView({ behavior: 'smooth' });
Expand Down
3 changes: 0 additions & 3 deletions src/containers/article-comments/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ function ArticleCommentsContainer() {
}, [dispatch, params.id, storeSelect.token]);

const organizedComments = listToTree(select.comments);
useEffect(() => {
console.log(organizedComments)
}, [organizedComments]);
return (
<ArticleComments
comments={organizedComments}
Expand Down
1 change: 0 additions & 1 deletion src/store-redux/comments/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export const fetchComments = (articleId) => async (dispatch) => {
export const addCommentSuccess = (comment) => ({ type: 'comments/addCommentSuccess', payload: comment });

export const addComment = ({ text, parent, article, token }) => async (dispatch) => {
console.log('Token in addComment:', token);
try {
const response = await services.api.request({
url: '/comments',
Expand Down
15 changes: 2 additions & 13 deletions src/utils/list-to-tree/index.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,32 @@
export default function listToTree(list, key = '_id', maxDepth = 6) {
let trees = {};
let roots = {};
let depths = {}; // Track the depth of each node
let depths = {};

for (const item of list) {
// Initialize depth for the current item
if (!depths[item[key]]) {
depths[item[key]] = 0;
}

// Add item to the node index and create children property

if (!trees[item[key]]) {
trees[item[key]] = item;
trees[item[key]].children = [];
// Initially consider it as a root
roots[item[key]] = trees[item[key]];
} else {
trees[item[key]] = Object.assign(trees[item[key]], item);
}

// If the item has a parent, add it to the parent's children
if (item.parent?.[key]) {
// Check if the parent exists in the original list
const parentExists = list.some(parentItem => parentItem[key] === item.parent[key]);

if (parentExists) {
// If the parent is not in the index, create its node (if not created earlier)
if (!trees[item.parent[key]]) {
trees[item.parent[key]] = { children: [] };
}

// Check the depth of the parent
if (depths[item.parent[key]] < maxDepth - 1) {
// Add the current item as a child to the parent
trees[item.parent[key]].children.push(trees[item[key]]);
// Set the depth of the current item
depths[item[key]] = depths[item.parent[key]] + 1;

// Remove the current item from roots as it is now a child
if (roots[item[key]]) {
delete roots[item[key]];
}
Expand Down

0 comments on commit b3de5cc

Please sign in to comment.