From 8cd6ba59d7733a787d042af265e9f28450ca38f4 Mon Sep 17 00:00:00 2001 From: Gal Elmalah Date: Sun, 11 Oct 2020 22:07:15 +0300 Subject: [PATCH] add useMemo and 3 more sortBy options --- app/src/components/feed.js | 10 +++++---- app/src/components/sortByDropdown.js | 31 +++++++++++++++++----------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/app/src/components/feed.js b/app/src/components/feed.js index 9a39d5f..85e782a 100644 --- a/app/src/components/feed.js +++ b/app/src/components/feed.js @@ -1,3 +1,4 @@ +/* eslint-disable react/jsx-filename-extension */ import React from 'react'; import '../styles/sidebar.css'; import BookCard from './bookcard'; @@ -6,13 +7,14 @@ import SortByDropdown, { FIELDS_TO_SORT_BY, compareFunctions } from './sortByDro export default ({ data, limit }) => { const [sortBy, setSortBy] = React.useState(FIELDS_TO_SORT_BY[0]); - const getSortedBooks = () => [...data.allBooksJson.edges] - .sort(compareFunctions[sortBy]); + const sortedBooks = React.useMemo(() => [...data.allBooksJson.edges] + .sort(compareFunctions[sortBy.value]), + [sortBy]); return ( <> - - {getSortedBooks().map((x, index) => { + + {sortedBooks.map((x, index) => { const book = x.node; if (!limit || index < limit) { if (!book.description || book.description.length < 10) { diff --git a/app/src/components/sortByDropdown.js b/app/src/components/sortByDropdown.js index fb07d59..7b5a92c 100644 --- a/app/src/components/sortByDropdown.js +++ b/app/src/components/sortByDropdown.js @@ -2,12 +2,22 @@ import React from 'react'; import { Dropdown } from 'react-bootstrap'; export const compareFunctions = { - title: ({ node: bookOne }, { node: bookTwo }) => bookOne.title.localeCompare(bookTwo.title), - year: ({ node: bookOne }, { node: bookTwo }) => Number(bookTwo.year) - Number(bookOne.year), - rating: ({ node: bookOne }, { node: bookTwo }) => Number(bookTwo.rating) - Number(bookOne.rating), + title_a_z: ({ node: bookOne }, { node: bookTwo }) => bookOne.title.localeCompare(bookTwo.title), + title_z_a: ({ node: bookOne }, { node: bookTwo }) => bookTwo.title.localeCompare(bookOne.title), + year_descending: ({ node: bookOne }, { node: bookTwo }) => Number(bookTwo.year) - Number(bookOne.year), + year_ascending: ({ node: bookOne }, { node: bookTwo }) => Number(bookOne.year) - Number(bookTwo.year), + rating_descending: ({ node: bookOne }, { node: bookTwo }) => Number(bookTwo.rating) - Number(bookOne.rating), + rating_ascending: ({ node: bookOne }, { node: bookTwo }) => Number(bookOne.rating) - Number(bookTwo.rating), }; -export const FIELDS_TO_SORT_BY = ['rating', 'year', 'title']; +export const FIELDS_TO_SORT_BY = [ + { label: 'Rating, high to low', value: 'rating_descending' }, + { label: 'Rating, low to high', value: 'rating_ascending' }, + { label: 'Publication year, new to old', value: 'year_descending' }, + { label: 'Publication year, old to new', value: 'year_ascending' }, + { label: 'Title, A-Z', value: 'title_a_z' }, + { label: 'Title, Z-A', value: 'title_z_a' }, +]; export default ({ sortBy, onSortByItemClick }) => (
@@ -19,14 +29,11 @@ export default ({ sortBy, onSortByItemClick }) => ( - {FIELDS_TO_SORT_BY - .map((field) => ( - onSortByItemClick(field)} - > - {field} - - ))} + {FIELDS_TO_SORT_BY.map((field) => ( + onSortByItemClick(field)}> + {field.label} + + ))}