diff --git a/components/SortLinkDropdown.tsx b/components/SortLinkDropdown.tsx new file mode 100644 index 0000000..11d2db7 --- /dev/null +++ b/components/SortLinkDropdown.tsx @@ -0,0 +1,69 @@ +// Copyright (C) 2022-present Daniel31x13 +// This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3. +// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +// You should have received a copy of the GNU General Public License along with this program. If not, see . + +import React, { ChangeEvent } from "react"; +import ClickAwayHandler from "./ClickAwayHandler"; +import RadioButton from "./RadioButton"; + +type Props = { + handleSortChange: (e: ChangeEvent) => void; + sortBy: string; + toggleSortDropdown: Function; +}; + +export default function SortLinkDropdown({ + handleSortChange, + sortBy, + toggleSortDropdown, +}: Props) { + return ( + { + const target = e.target as HTMLInputElement; + if (target.id !== "sort-dropdown") toggleSortDropdown(); + }} + className="absolute top-8 right-0 shadow-md bg-gray-50 rounded-md p-2 z-10 border border-sky-100 w-48" + > +

Sort by

+
+ + + + + + + + + + + +
+
+ ); +} diff --git a/pages/collections/[id].tsx b/pages/collections/[id].tsx index 9f0c767..b123555 100644 --- a/pages/collections/[id].tsx +++ b/pages/collections/[id].tsx @@ -19,13 +19,12 @@ import { } from "@fortawesome/free-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { useRouter } from "next/router"; -import { ChangeEvent, useEffect, useState } from "react"; +import { useEffect, useState } from "react"; import MainLayout from "@/layouts/MainLayout"; -import RadioButton from "@/components/RadioButton"; -import ClickAwayHandler from "@/components/ClickAwayHandler"; import { useSession } from "next-auth/react"; import ProfilePhoto from "@/components/ProfilePhoto"; import TeamManagement from "@/components/Modal/Collection/TeamManagement"; +import SortLinkDropdown from "@/components/SortLinkDropdown"; export default function () { const router = useRouter(); @@ -64,10 +63,6 @@ export default function () { setDeleteCollectionModal(!deleteCollectionModal); }; - const handleSortChange = (event: ChangeEvent) => { - setSortBy(event.target.value); - }; - useEffect(() => { setActiveCollection( collections.find((e) => e.id === Number(router.query.id)) @@ -182,54 +177,11 @@ export default function () { {sortDropdown ? ( - { - const target = e.target as HTMLInputElement; - if (target.id !== "sort-dropdown") setSortDropdown(false); - }} - className="absolute top-8 right-0 shadow-md bg-gray-50 rounded-md p-2 z-10 border border-sky-100 w-48" - > -

- Sort by -

-
- - - - - - - - - - - -
-
+ setSortBy(e.target.value)} + sortBy={sortBy} + toggleSortDropdown={() => setSortDropdown(!sortDropdown)} + /> ) : null}
diff --git a/pages/links.tsx b/pages/links.tsx index 3b7924e..903a9df 100644 --- a/pages/links.tsx +++ b/pages/links.tsx @@ -6,6 +6,7 @@ import ClickAwayHandler from "@/components/ClickAwayHandler"; import LinkList from "@/components/LinkList"; import RadioButton from "@/components/RadioButton"; +import SortLinkDropdown from "@/components/SortLinkDropdown"; import MainLayout from "@/layouts/MainLayout"; import useLinkStore from "@/store/links"; import { faBookmark, faSort } from "@fortawesome/free-solid-svg-icons"; @@ -78,54 +79,11 @@ export default function Links() {
{sortDropdown ? ( - { - const target = e.target as HTMLInputElement; - if (target.id !== "sort-dropdown") setSortDropdown(false); - }} - className="absolute top-8 right-0 shadow-md bg-gray-50 rounded-md p-2 z-10 border border-sky-100 w-48" - > -

- Sort by -

-
- - - - - - - - - - - -
-
+ setSortBy(e.target.value)} + sortBy={sortBy} + toggleSortDropdown={() => setSortDropdown(!sortDropdown)} + /> ) : null} diff --git a/pages/tags/[id].tsx b/pages/tags/[id].tsx index 9d7dbfc..cce0372 100644 --- a/pages/tags/[id].tsx +++ b/pages/tags/[id].tsx @@ -14,6 +14,7 @@ import RadioButton from "@/components/RadioButton"; import ClickAwayHandler from "@/components/ClickAwayHandler"; import { Tag } from "@prisma/client"; import useTagStore from "@/store/tags"; +import SortLinkDropdown from "@/components/SortLinkDropdown"; export default function () { const router = useRouter(); @@ -55,14 +56,16 @@ export default function () { setSortedLinks( linksArray.sort( (a, b) => - new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime() + new Date(b.createdAt as string).getTime() - + new Date(a.createdAt as string).getTime() ) ); else if (sortBy === "Date (Oldest First)") setSortedLinks( linksArray.sort( (a, b) => - new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime() + new Date(a.createdAt as string).getTime() - + new Date(b.createdAt as string).getTime() ) ); }, [links, router, tags, sortBy]); @@ -95,54 +98,11 @@ export default function () { {sortDropdown ? ( - { - const target = e.target as HTMLInputElement; - if (target.id !== "sort-dropdown") setSortDropdown(false); - }} - className="absolute top-8 right-0 shadow-md bg-gray-50 rounded-md p-2 z-10 border border-sky-100 w-48" - > -

- Sort by -

-
- - - - - - - - - - - -
-
+ setSortBy(e.target.value)} + sortBy={sortBy} + toggleSortDropdown={() => setSortDropdown(!sortDropdown)} + /> ) : null}