made sorting logic typesafe
This commit is contained in:
parent
d375f8f914
commit
8d094f320a
|
@ -1,10 +1,11 @@
|
|||
import React, { ChangeEvent } from "react";
|
||||
import ClickAwayHandler from "./ClickAwayHandler";
|
||||
import RadioButton from "./RadioButton";
|
||||
import { Sort } from "@/types/global";
|
||||
|
||||
type Props = {
|
||||
handleSortChange: (e: ChangeEvent<HTMLInputElement>) => void;
|
||||
sortBy: string;
|
||||
handleSortChange: (e: Sort) => void;
|
||||
sortBy: Sort;
|
||||
toggleSortDropdown: Function;
|
||||
};
|
||||
|
||||
|
@ -25,38 +26,38 @@ export default function SortLinkDropdown({
|
|||
<div className="flex flex-col gap-2">
|
||||
<RadioButton
|
||||
label="Name (A-Z)"
|
||||
state={sortBy === "Name (A-Z)"}
|
||||
onClick={handleSortChange}
|
||||
state={sortBy === Sort.NameAZ}
|
||||
onClick={() => handleSortChange(Sort.NameAZ)}
|
||||
/>
|
||||
|
||||
<RadioButton
|
||||
label="Name (Z-A)"
|
||||
state={sortBy === "Name (Z-A)"}
|
||||
onClick={handleSortChange}
|
||||
state={sortBy === Sort.NameZA}
|
||||
onClick={() => handleSortChange(Sort.NameZA)}
|
||||
/>
|
||||
|
||||
<RadioButton
|
||||
label="Title (A-Z)"
|
||||
state={sortBy === "Title (A-Z)"}
|
||||
onClick={handleSortChange}
|
||||
state={sortBy === Sort.TitleAZ}
|
||||
onClick={() => handleSortChange(Sort.TitleAZ)}
|
||||
/>
|
||||
|
||||
<RadioButton
|
||||
label="Title (Z-A)"
|
||||
state={sortBy === "Title (Z-A)"}
|
||||
onClick={handleSortChange}
|
||||
state={sortBy === Sort.TitleZA}
|
||||
onClick={() => handleSortChange(Sort.TitleZA)}
|
||||
/>
|
||||
|
||||
<RadioButton
|
||||
label="Date (Newest First)"
|
||||
state={sortBy === "Date (Newest First)"}
|
||||
onClick={handleSortChange}
|
||||
state={sortBy === Sort.DateNewestFirst}
|
||||
onClick={() => handleSortChange(Sort.DateNewestFirst)}
|
||||
/>
|
||||
|
||||
<RadioButton
|
||||
label="Date (Oldest First)"
|
||||
state={sortBy === "Date (Oldest First)"}
|
||||
onClick={handleSortChange}
|
||||
state={sortBy === Sort.DateOldestFirst}
|
||||
onClick={() => handleSortChange(Sort.DateOldestFirst)}
|
||||
/>
|
||||
</div>
|
||||
</ClickAwayHandler>
|
||||
|
|
|
@ -2,7 +2,7 @@ import Dropdown from "@/components/Dropdown";
|
|||
import LinkCard from "@/components/LinkCard";
|
||||
import useCollectionStore from "@/store/collections";
|
||||
import useLinkStore from "@/store/links";
|
||||
import { CollectionIncludingMembers } from "@/types/global";
|
||||
import { CollectionIncludingMembers, Sort } from "@/types/global";
|
||||
import {
|
||||
faEllipsis,
|
||||
faFolder,
|
||||
|
@ -10,7 +10,7 @@ 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 { useSession } from "next-auth/react";
|
||||
import ProfilePhoto from "@/components/ProfilePhoto";
|
||||
|
@ -29,15 +29,15 @@ export default function Index() {
|
|||
|
||||
const [expandDropdown, setExpandDropdown] = useState(false);
|
||||
const [sortDropdown, setSortDropdown] = useState(false);
|
||||
const [sortBy, setSortBy] = useState("Name (A-Z)");
|
||||
const [sortBy, setSortBy] = useState<Sort>(Sort.NameAZ);
|
||||
|
||||
const [activeCollection, setActiveCollection] =
|
||||
useState<CollectionIncludingMembers>();
|
||||
|
||||
const [sortedLinks, setSortedLinks] = useState(links);
|
||||
|
||||
const handleSortChange = (event: ChangeEvent<HTMLInputElement>) => {
|
||||
setSortBy(event.target.value);
|
||||
const handleSortChange = (e: Sort) => {
|
||||
setSortBy(e);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
|
@ -51,15 +51,15 @@ export default function Index() {
|
|||
...links.filter((e) => e.collection.id === Number(router.query.id)),
|
||||
];
|
||||
|
||||
if (sortBy === "Name (A-Z)")
|
||||
if (sortBy === Sort.NameAZ)
|
||||
setSortedLinks(linksArray.sort((a, b) => a.name.localeCompare(b.name)));
|
||||
else if (sortBy === "Title (A-Z)")
|
||||
else if (sortBy === Sort.TitleAZ)
|
||||
setSortedLinks(linksArray.sort((a, b) => a.title.localeCompare(b.title)));
|
||||
else if (sortBy === "Name (Z-A)")
|
||||
else if (sortBy === Sort.NameZA)
|
||||
setSortedLinks(linksArray.sort((a, b) => b.name.localeCompare(a.name)));
|
||||
else if (sortBy === "Title (Z-A)")
|
||||
else if (sortBy === Sort.TitleZA)
|
||||
setSortedLinks(linksArray.sort((a, b) => b.title.localeCompare(a.title)));
|
||||
else if (sortBy === "Date (Newest First)")
|
||||
else if (sortBy === Sort.DateNewestFirst)
|
||||
setSortedLinks(
|
||||
linksArray.sort(
|
||||
(a, b) =>
|
||||
|
@ -67,7 +67,7 @@ export default function Index() {
|
|||
new Date(a.createdAt as string).getTime()
|
||||
)
|
||||
);
|
||||
else if (sortBy === "Date (Oldest First)")
|
||||
else if (sortBy === Sort.DateOldestFirst)
|
||||
setSortedLinks(
|
||||
linksArray.sort(
|
||||
(a, b) =>
|
||||
|
|
|
@ -2,6 +2,7 @@ import LinkCard from "@/components/LinkCard";
|
|||
import SortLinkDropdown from "@/components/SortLinkDropdown";
|
||||
import MainLayout from "@/layouts/MainLayout";
|
||||
import useLinkStore from "@/store/links";
|
||||
import { Sort } from "@/types/global";
|
||||
import { faLink, faSort } from "@fortawesome/free-solid-svg-icons";
|
||||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
||||
import { ChangeEvent, useEffect, useState } from "react";
|
||||
|
@ -10,25 +11,25 @@ export default function Links() {
|
|||
const { links } = useLinkStore();
|
||||
|
||||
const [sortDropdown, setSortDropdown] = useState(false);
|
||||
const [sortBy, setSortBy] = useState("Name (A-Z)");
|
||||
const [sortBy, setSortBy] = useState<Sort>(Sort.NameAZ);
|
||||
const [sortedLinks, setSortedLinks] = useState(links);
|
||||
|
||||
const handleSortChange = (event: ChangeEvent<HTMLInputElement>) => {
|
||||
setSortBy(event.target.value);
|
||||
const handleSortChange = (e: Sort) => {
|
||||
setSortBy(e);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
const linksArray = [...links];
|
||||
|
||||
if (sortBy === "Name (A-Z)")
|
||||
if (sortBy === Sort.NameAZ)
|
||||
setSortedLinks(linksArray.sort((a, b) => a.name.localeCompare(b.name)));
|
||||
else if (sortBy === "Title (A-Z)")
|
||||
else if (sortBy === Sort.TitleAZ)
|
||||
setSortedLinks(linksArray.sort((a, b) => a.title.localeCompare(b.title)));
|
||||
else if (sortBy === "Name (Z-A)")
|
||||
else if (sortBy === Sort.NameZA)
|
||||
setSortedLinks(linksArray.sort((a, b) => b.name.localeCompare(a.name)));
|
||||
else if (sortBy === "Title (Z-A)")
|
||||
else if (sortBy === Sort.TitleZA)
|
||||
setSortedLinks(linksArray.sort((a, b) => b.title.localeCompare(a.title)));
|
||||
else if (sortBy === "Date (Newest First)")
|
||||
else if (sortBy === Sort.DateNewestFirst)
|
||||
setSortedLinks(
|
||||
linksArray.sort(
|
||||
(a, b) =>
|
||||
|
@ -36,7 +37,7 @@ export default function Links() {
|
|||
new Date(a.createdAt as string).getTime()
|
||||
)
|
||||
);
|
||||
else if (sortBy === "Date (Oldest First)")
|
||||
else if (sortBy === Sort.DateOldestFirst)
|
||||
setSortedLinks(
|
||||
linksArray.sort(
|
||||
(a, b) =>
|
||||
|
|
|
@ -3,6 +3,7 @@ import LinkCard from "@/components/LinkCard";
|
|||
import SortLinkDropdown from "@/components/SortLinkDropdown";
|
||||
import MainLayout from "@/layouts/MainLayout";
|
||||
import useLinkStore from "@/store/links";
|
||||
import { Sort } from "@/types/global";
|
||||
import { faFilter, faSearch, faSort } from "@fortawesome/free-solid-svg-icons";
|
||||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
||||
import { useRouter } from "next/router";
|
||||
|
@ -35,11 +36,11 @@ export default function Links() {
|
|||
|
||||
const [filterDropdown, setFilterDropdown] = useState(false);
|
||||
const [sortDropdown, setSortDropdown] = useState(false);
|
||||
const [sortBy, setSortBy] = useState("Name (A-Z)");
|
||||
const [sortBy, setSortBy] = useState<Sort>(Sort.NameAZ);
|
||||
const [sortedLinks, setSortedLinks] = useState(links);
|
||||
|
||||
const handleSortChange = (event: ChangeEvent<HTMLInputElement>) => {
|
||||
setSortBy(event.target.value);
|
||||
const handleSortChange = (e: Sort) => {
|
||||
setSortBy(e);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
|
@ -61,15 +62,15 @@ export default function Links() {
|
|||
}),
|
||||
];
|
||||
|
||||
if (sortBy === "Name (A-Z)")
|
||||
if (sortBy === Sort.NameAZ)
|
||||
setSortedLinks(linksArray.sort((a, b) => a.name.localeCompare(b.name)));
|
||||
else if (sortBy === "Title (A-Z)")
|
||||
else if (sortBy === Sort.TitleAZ)
|
||||
setSortedLinks(linksArray.sort((a, b) => a.title.localeCompare(b.title)));
|
||||
else if (sortBy === "Name (Z-A)")
|
||||
else if (sortBy === Sort.NameZA)
|
||||
setSortedLinks(linksArray.sort((a, b) => b.name.localeCompare(a.name)));
|
||||
else if (sortBy === "Title (Z-A)")
|
||||
else if (sortBy === Sort.TitleZA)
|
||||
setSortedLinks(linksArray.sort((a, b) => b.title.localeCompare(a.title)));
|
||||
else if (sortBy === "Date (Newest First)")
|
||||
else if (sortBy === Sort.DateNewestFirst)
|
||||
setSortedLinks(
|
||||
linksArray.sort(
|
||||
(a, b) =>
|
||||
|
@ -77,7 +78,7 @@ export default function Links() {
|
|||
new Date(a.createdAt as string).getTime()
|
||||
)
|
||||
);
|
||||
else if (sortBy === "Date (Oldest First)")
|
||||
else if (sortBy === Sort.DateOldestFirst)
|
||||
setSortedLinks(
|
||||
linksArray.sort(
|
||||
(a, b) =>
|
||||
|
|
|
@ -8,6 +8,7 @@ import MainLayout from "@/layouts/MainLayout";
|
|||
import { Tag } from "@prisma/client";
|
||||
import useTagStore from "@/store/tags";
|
||||
import SortLinkDropdown from "@/components/SortLinkDropdown";
|
||||
import { Sort } from "@/types/global";
|
||||
|
||||
export default function Index() {
|
||||
const router = useRouter();
|
||||
|
@ -16,14 +17,14 @@ export default function Index() {
|
|||
const { tags } = useTagStore();
|
||||
|
||||
const [sortDropdown, setSortDropdown] = useState(false);
|
||||
const [sortBy, setSortBy] = useState("Name (A-Z)");
|
||||
const [sortBy, setSortBy] = useState<Sort>(Sort.NameAZ);
|
||||
|
||||
const [activeTag, setActiveTag] = useState<Tag>();
|
||||
|
||||
const [sortedLinks, setSortedLinks] = useState(links);
|
||||
|
||||
const handleSortChange = (event: ChangeEvent<HTMLInputElement>) => {
|
||||
setSortBy(event.target.value);
|
||||
const handleSortChange = (e: Sort) => {
|
||||
setSortBy(e);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
|
@ -37,15 +38,15 @@ export default function Index() {
|
|||
),
|
||||
];
|
||||
|
||||
if (sortBy === "Name (A-Z)")
|
||||
if (sortBy === Sort.NameAZ)
|
||||
setSortedLinks(linksArray.sort((a, b) => a.name.localeCompare(b.name)));
|
||||
else if (sortBy === "Title (A-Z)")
|
||||
else if (sortBy === Sort.TitleAZ)
|
||||
setSortedLinks(linksArray.sort((a, b) => a.title.localeCompare(b.title)));
|
||||
else if (sortBy === "Name (Z-A)")
|
||||
else if (sortBy === Sort.NameZA)
|
||||
setSortedLinks(linksArray.sort((a, b) => b.name.localeCompare(a.name)));
|
||||
else if (sortBy === "Title (Z-A)")
|
||||
else if (sortBy === Sort.TitleZA)
|
||||
setSortedLinks(linksArray.sort((a, b) => b.title.localeCompare(a.title)));
|
||||
else if (sortBy === "Date (Newest First)")
|
||||
else if (sortBy === Sort.DateNewestFirst)
|
||||
setSortedLinks(
|
||||
linksArray.sort(
|
||||
(a, b) =>
|
||||
|
@ -53,7 +54,7 @@ export default function Index() {
|
|||
new Date(a.createdAt as string).getTime()
|
||||
)
|
||||
);
|
||||
else if (sortBy === "Date (Oldest First)")
|
||||
else if (sortBy === Sort.DateOldestFirst)
|
||||
setSortedLinks(
|
||||
linksArray.sort(
|
||||
(a, b) =>
|
||||
|
|
|
@ -42,3 +42,12 @@ export interface PublicCollectionIncludingLinks
|
|||
ownerName?: string;
|
||||
links: Link[];
|
||||
}
|
||||
|
||||
export enum Sort {
|
||||
NameAZ,
|
||||
NameZA,
|
||||
TitleAZ,
|
||||
TitleZA,
|
||||
DateNewestFirst,
|
||||
DateOldestFirst,
|
||||
}
|
||||
|
|
Ŝarĝante…
Reference in New Issue