Skip to content

Commit

Permalink
Update to use dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasdeluna committed Sep 25, 2024
1 parent 6bbe497 commit 549eae0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 81 deletions.
17 changes: 5 additions & 12 deletions app/components/Comments/CommentView.css
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
.headerContainer {
margin-bottom: 0.25rem;
margin-bottom: var(--spacing-sm);
}

.sortIcon {
flex-direction: column;
}

.iconWrapper {
margin-left: 0.25rem;
}

.sortList {
margin-left: 0.5rem;
min-width: 10rem;
.dropdownElement {
padding: var(--spacing-sm);
cursor: pointer;
width: var(--spacing-ss);
}
119 changes: 50 additions & 69 deletions app/components/Comments/CommentView.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import {
FilterSection,
Flex,
Icon,
LoadingIndicator,
} from '@webkom/lego-bricks';
import { Flex, Icon, LoadingIndicator } from '@webkom/lego-bricks';
import { ListFilter } from 'lucide-react';
import moment from 'moment';
import { useEffect, useState, type CSSProperties } from 'react';
import CommentForm from 'app/components/CommentForm';
import Dropdown from 'app/components/Dropdown';
import { generateTreeStructure } from 'app/utils';
import { SelectInput } from '../Form';
import CommentTree from './CommentTree';
import styles from './CommentView.css';
import type Comment from 'app/store/models/Comment';
Expand All @@ -36,27 +31,16 @@ const orderingOptions: Array<Option> = [
value: 'createdAtInv',
},
{
label: 'Likes',
label: 'Reaksjoner',
value: 'reactionsGrouped',
},
{
label: 'Kontroversielle',
value: 'reactionsControversial',
},
];

type Option = {
label: string;
value: string;
};

const getReactionScore = (comment: Comment, emojiString: string): number => {
const count =
comment.reactionsGrouped.find((reaction) => reaction.emoji === emojiString)
?.count || 0;
return count;
};

const Title = ({ displayTitle }: { displayTitle: boolean }) =>
displayTitle && <h3>Kommentarer</h3>;

Expand All @@ -68,35 +52,28 @@ const CommentView = (props: Props) => {
style,
displayTitle = true,
contentAuthors,
newOnTop,
} = props;
const commentFormProps = {
contentTarget,
};
const [ordering, setOrdering] = useState<Option>(orderingOptions[0]);
const [sortedComments, setSortedComments] = useState<Comment[]>(comments);
const [displaySorting, setDisplaySorting] = useState(false);
const tree = generateTreeStructure(sortedComments);

useEffect(() => {
const sorted = [...comments].sort((a: Comment, b: Comment) => {
if (ordering.value === 'createdAt') {
return moment(b.createdAt).valueOf() - moment(a.createdAt).valueOf();
} else if (ordering.value === 'createdAtInv') {
return moment(a.createdAt).valueOf() - moment(b.createdAt).valueOf();
} else if (ordering.value === 'reactionsGrouped') {
const scoreA =
getReactionScore(a, ':+1:') - getReactionScore(a, ':-1:');
const scoreB =
getReactionScore(b, ':+1:') - getReactionScore(b, ':-1:');
return scoreB - scoreA;
} else if (ordering.value === 'reactionsControversial') {
return getReactionScore(b, ':-1:') - getReactionScore(a, ':-1:');
}
return 0;
});

setSortedComments(sorted);
}, [ordering, comments]);
const sortedComments = comments.sort((a: Comment, b: Comment) => {
if (ordering.value === 'createdAt') {
return moment(b.createdAt).valueOf() - moment(a.createdAt).valueOf();
} else if (ordering.value === 'createdAtInv') {
return moment(a.createdAt).valueOf() - moment(b.createdAt).valueOf();
} else if (ordering.value === 'reactionsGrouped') {
return (
b.reactionsGrouped.reduce((acc, reaction) => acc + reaction.count, 0) -
a.reactionsGrouped.reduce((acc, reaction) => acc + reaction.count, 0)
);
}
return 0;
});
const tree = generateTreeStructure(sortedComments);

return (
<div style={style}>
Expand All @@ -107,41 +84,45 @@ const CommentView = (props: Props) => {
className={styles.headerContainer}
>
<Title displayTitle={displayTitle} />
<Flex flex-row alignItems="center" className={styles.iconWrapper}>
<Icon
name="arrow-down-wide-narrow"
size={20}
onClick={() => {
setDisplaySorting(!displaySorting);
}}
className={styles.sortIcon}
iconNode={<ListFilter />}
/>
{displaySorting && (
<div className={styles.sortList}>
<FilterSection title="">
<SelectInput
name="sorting_selector"
value={ordering}
onChange={(selectedOption: Option) => {
setOrdering(selectedOption);

<Dropdown
show={displaySorting}
toggle={() => setDisplaySorting(!displaySorting)}
contentClassName={styles.dropdownElement}
triggerComponent={
<Icon
size={20}
className={styles.sortIcon}
iconNode={<ListFilter />}
/>
}
>
<Dropdown.List>
{orderingOptions.map((o: Option) => (
<>
<Dropdown.ListItem
key={o.value}
onClick={() => {
setOrdering(o);
setDisplaySorting(!displaySorting);
}}
isClearable={false}
options={orderingOptions}
/>
</FilterSection>
</div>
)}
</Flex>
>
{o.label}
</Dropdown.ListItem>
<Dropdown.Divider />
</>
))}
</Dropdown.List>
</Dropdown>
</Flex>

<Flex column gap="var(--spacing-sm)">
{!formDisabled && <CommentForm {...commentFormProps} />}

<LoadingIndicator loading={!sortedComments}>
{sortedComments && (
<LoadingIndicator loading={!comments}>
{comments && (
<CommentTree
comments={tree}
comments={newOnTop ? tree : tree.reverse()}
commentFormProps={commentFormProps}
contentTarget={contentTarget}
contentAuthors={contentAuthors}
Expand Down

0 comments on commit 549eae0

Please sign in to comment.