Skip to content

Commit

Permalink
refactor: improve comments filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
victorgcramos committed Mar 7, 2022
1 parent 28ee957 commit 8d643d1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
21 changes: 5 additions & 16 deletions plugins-structure/packages/comments/src/ui/Comments/Comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PropTypes from "prop-types";
import { CommentsFilter, CommentsList } from "./";
import { Card, H2 } from "pi-ui";
import styles from "./styles.module.css";
import { getThreadSchema, sortByNew, sortByOld, sortByTop } from "./utils";
import { getThreadSchema } from "./utils";

export const Comments = ({
comments,
Expand All @@ -20,21 +20,10 @@ export const Comments = ({
function handleToggleFlatMode() {
setFlat(!isFlat);
}
// sort handler
function handleSortComments(op) {
let newCommentsList;
switch (op) {
case "new":
newCommentsList = sortByNew(comments);
break;
case "old":
newCommentsList = sortByOld(comments);
break;
default:
newCommentsList = sortByTop(comments);
break;
}
setSortedComments(newCommentsList);

function handleSortComments(sortFn) {
const newSortedComments = sortFn(comments);
setSortedComments(newSortedComments);
}

// Update schema for every filter change
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useState } from "react";
import { Select, Text, classNames } from "pi-ui";
import PropTypes from "prop-types";
import styles from "./styles.module.css";
import { sortByNew, sortByOld, sortByTop } from "./utils";

const options = [
{
Expand All @@ -20,10 +21,24 @@ const options = [

export const CommentsFilter = ({ onSort, onToggleFlatMode, isFlat }) => {
const [selected, setSelected] = useState(options[0]);

function handleFilterChanges(option) {
setSelected(option);
onSort(option.value);
let sortFn;
switch (option.value) {
case "new":
sortFn = sortByNew;
break;
case "old":
sortFn = sortByOld;
break;
default:
sortFn = sortByTop;
break;
}
onSort(sortFn);
}

return (
<div className={styles.filters}>
<Select
Expand Down

0 comments on commit 8d643d1

Please sign in to comment.