Skip to content

Commit

Permalink
406 comments order is created at asc order (#429)
Browse files Browse the repository at this point in the history
* Add filter icons

* Add outlined variant style for button

* Add button to filter comments by date

* Update icon from filter to sort
  • Loading branch information
camilovegag authored Apr 30, 2024
1 parent 8f4c804 commit 6b50df6
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 7 deletions.
3 changes: 3 additions & 0 deletions packages/berlin/public/icons/filter-dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions packages/berlin/public/icons/filter-light.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions packages/berlin/public/icons/sort-dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions packages/berlin/public/icons/sort-light.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions packages/berlin/src/components/button/Button.styled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ export const StyledButton = styled.button<StyledButtonProps>`
color: var(--color-black);
`}
${(props) =>
props.$variant === 'outlined' &&
css`
background-color: var(--color-white);
border: 1px solid var(--color-black);
color: var(--color-black);
`}
${(props) =>
props.$variant === 'link' &&
css`
Expand Down
36 changes: 29 additions & 7 deletions packages/berlin/src/pages/Option.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import Button from '../components/button';
import CommentCard from '../components/comment-card';
import IconButton from '../components/icon-button';
import Textarea from '../components/textarea';
import { Bold } from '../components/typography/Bold.styled';

function Option() {
const theme = useAppStore((state) => state.theme);
Expand All @@ -41,6 +42,7 @@ function Option() {
>([]);
const [localOptionHearts, setLocalOptionHearts] = useState(0);
const [comment, setComment] = useState('');
const [sortOrder, setSortOrder] = useState('desc'); // 'asc' for ascending, 'desc' for descending

const { data: option, isLoading } = useQuery({
queryKey: ['option', optionId],
Expand All @@ -60,11 +62,15 @@ function Option() {
refetchInterval: 5000, // Poll every 5 seconds
});

const sortedComments = comments?.sort((a, b) => {
const dateA = new Date(a.createdAt).getTime();
const dateB = new Date(b.createdAt).getTime();
return dateB - dateA; // Sort by newest first
});
const sortedComments = useMemo(() => {
if (!comments) return [];

return comments.sort((a, b) => {
const dateA = new Date(a.createdAt).getTime();
const dateB = new Date(b.createdAt).getTime();
return sortOrder === 'desc' ? dateB - dateA : dateA - dateB;
});
}, [comments, sortOrder]);

useEffect(() => {
if (optionId) {
Expand Down Expand Up @@ -171,9 +177,25 @@ function Option() {
/>
<Button onClick={handlePostComment}>Comment</Button>
</Form>
{sortedComments && (
{sortedComments.length > 0 && (
<>
<Title>Total comments ({sortedComments.length})</Title>
<FlexRow $justify="space-between">
<Title>Total comments ({sortedComments.length})</Title>
<div style={{ display: 'flex', alignItems: 'center', gap: '0.25rem' }}>
<Bold>Sort</Bold>
<IconButton
onClick={() => setSortOrder((prevOrder) => (prevOrder === 'asc' ? 'desc' : 'asc'))}
icon={{
src: `/icons/sort-${theme}.svg`,
alt: 'Sort icon',
}}
$padding={4}
$color="secondary"
$height={24}
$width={24}
/>
</div>
</FlexRow>
{sortedComments.map((comment) => (
<CommentCard key={comment.id} comment={comment} />
))}
Expand Down

0 comments on commit 6b50df6

Please sign in to comment.