diff --git a/components/core/App.js b/components/core/App.js index f0171b8..0847bd6 100644 --- a/components/core/App.js +++ b/components/core/App.js @@ -18,6 +18,7 @@ const App = (props) => { const [loading, setLoading] = useState(true); // shapes loading const [searchTerm, setSearchTerm] = useState(""); // search + const [sort, setSort] = useState("popularity"); // sort const { user } = props; @@ -92,7 +93,9 @@ const App = (props) => { <>
+ setSearchTerm={setSearchTerm} + sort={sort} + setSort={setSort} /> {loading ? ( { width={300} /> ) : ( - + )} ); diff --git a/components/utils/Header.js b/components/utils/Header.js index 1a8b4db..1b0ece8 100644 --- a/components/utils/Header.js +++ b/components/utils/Header.js @@ -91,6 +91,7 @@ const NavbarSearchInputControl = styled(FormControl)` const CloseIcon = styled(FiX)` margin: 0.37rem; + cursor: pointer; `; const UserThumb = styled.div` @@ -131,7 +132,9 @@ const Header = ({ user, setUser, searchTerm, - setSearchTerm + setSearchTerm, + sort, + setSort }) => { const [searchterm, setSearchterm] = useState(''); @@ -167,12 +170,27 @@ const Header = ({ value={searchTerm} onChange={(e) => setSearchTerm(e.target.value)} /> - + setSearchTerm('')}/> - - Likes - Popularity - Newly Added + + setSort('oldest')}> + Oldest + + setSort('popularity')}> + Popularity + + setSort('recent')}> + Recent + diff --git a/components/utils/ShapeList.js b/components/utils/ShapeList.js index 057d27f..edcc87f 100644 --- a/components/utils/ShapeList.js +++ b/components/utils/ShapeList.js @@ -191,7 +191,14 @@ const LikeFilledIcon = styled(BsFillHeartFill)` cursor: pointer; `; -const ShapeList = ({ setOpen, user, data, searchTerm }) => { +const ShapeList = ( + { + setOpen, + user, + data, + searchTerm, + sort + }) => { const filterShape = (shapes, searchTerm) => { if (!searchTerm) { @@ -209,8 +216,16 @@ const ShapeList = ({ setOpen, user, data, searchTerm }) => { const [shapeToExport, setShapeToExport] = useState(); useEffect(() =>{ - setFilteredShape(filterShape(shapes, searchTerm)); - }, [searchTerm, shapes]); + const copy = [...shapes]; + if(sort === 'recent') { + copy.sort((a, b) => b.__createdtime__ - a.__createdtime__); + } else if(sort === 'popularity') { + copy.sort((a, b) => b.likes - a.likes); + } else if(sort === 'oldest') { + copy.sort((a, b) => a.__createdtime__ - b.__createdtime__); + } + setFilteredShape(filterShape(copy, searchTerm)); + }, [searchTerm, shapes, sort]); const handleSwicth = (shapeName) => { @@ -359,19 +374,18 @@ const ShapeList = ({ setOpen, user, data, searchTerm }) => { /> - performLike(event, shape['shape_id'])}> { shape.liked ? ( - ) : ( - )