some code cleanup
This commit is contained in:
parent
785ddb9a3f
commit
8f5dba6ed4
|
@ -0,0 +1,69 @@
|
||||||
|
// Copyright (C) 2022-present Daniel31x13 <daniel31x13@gmail.com>
|
||||||
|
// 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 <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import React, { ChangeEvent } from "react";
|
||||||
|
import ClickAwayHandler from "./ClickAwayHandler";
|
||||||
|
import RadioButton from "./RadioButton";
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
handleSortChange: (e: ChangeEvent<HTMLInputElement>) => void;
|
||||||
|
sortBy: string;
|
||||||
|
toggleSortDropdown: Function;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function SortLinkDropdown({
|
||||||
|
handleSortChange,
|
||||||
|
sortBy,
|
||||||
|
toggleSortDropdown,
|
||||||
|
}: Props) {
|
||||||
|
return (
|
||||||
|
<ClickAwayHandler
|
||||||
|
onClickOutside={(e: Event) => {
|
||||||
|
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"
|
||||||
|
>
|
||||||
|
<p className="mb-2 text-sky-900 text-center font-semibold">Sort by</p>
|
||||||
|
<div className="flex flex-col gap-2">
|
||||||
|
<RadioButton
|
||||||
|
label="Name (A-Z)"
|
||||||
|
state={sortBy === "Name (A-Z)"}
|
||||||
|
onClick={handleSortChange}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<RadioButton
|
||||||
|
label="Name (Z-A)"
|
||||||
|
state={sortBy === "Name (Z-A)"}
|
||||||
|
onClick={handleSortChange}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<RadioButton
|
||||||
|
label="Title (A-Z)"
|
||||||
|
state={sortBy === "Title (A-Z)"}
|
||||||
|
onClick={handleSortChange}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<RadioButton
|
||||||
|
label="Title (Z-A)"
|
||||||
|
state={sortBy === "Title (Z-A)"}
|
||||||
|
onClick={handleSortChange}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<RadioButton
|
||||||
|
label="Date (Newest First)"
|
||||||
|
state={sortBy === "Date (Newest First)"}
|
||||||
|
onClick={handleSortChange}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<RadioButton
|
||||||
|
label="Date (Oldest First)"
|
||||||
|
state={sortBy === "Date (Oldest First)"}
|
||||||
|
onClick={handleSortChange}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</ClickAwayHandler>
|
||||||
|
);
|
||||||
|
}
|
|
@ -19,13 +19,12 @@ import {
|
||||||
} from "@fortawesome/free-solid-svg-icons";
|
} from "@fortawesome/free-solid-svg-icons";
|
||||||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
||||||
import { useRouter } from "next/router";
|
import { useRouter } from "next/router";
|
||||||
import { ChangeEvent, useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import MainLayout from "@/layouts/MainLayout";
|
import MainLayout from "@/layouts/MainLayout";
|
||||||
import RadioButton from "@/components/RadioButton";
|
|
||||||
import ClickAwayHandler from "@/components/ClickAwayHandler";
|
|
||||||
import { useSession } from "next-auth/react";
|
import { useSession } from "next-auth/react";
|
||||||
import ProfilePhoto from "@/components/ProfilePhoto";
|
import ProfilePhoto from "@/components/ProfilePhoto";
|
||||||
import TeamManagement from "@/components/Modal/Collection/TeamManagement";
|
import TeamManagement from "@/components/Modal/Collection/TeamManagement";
|
||||||
|
import SortLinkDropdown from "@/components/SortLinkDropdown";
|
||||||
|
|
||||||
export default function () {
|
export default function () {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
@ -64,10 +63,6 @@ export default function () {
|
||||||
setDeleteCollectionModal(!deleteCollectionModal);
|
setDeleteCollectionModal(!deleteCollectionModal);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSortChange = (event: ChangeEvent<HTMLInputElement>) => {
|
|
||||||
setSortBy(event.target.value);
|
|
||||||
};
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setActiveCollection(
|
setActiveCollection(
|
||||||
collections.find((e) => e.id === Number(router.query.id))
|
collections.find((e) => e.id === Number(router.query.id))
|
||||||
|
@ -182,54 +177,11 @@ export default function () {
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{sortDropdown ? (
|
{sortDropdown ? (
|
||||||
<ClickAwayHandler
|
<SortLinkDropdown
|
||||||
onClickOutside={(e: Event) => {
|
handleSortChange={(e) => setSortBy(e.target.value)}
|
||||||
const target = e.target as HTMLInputElement;
|
sortBy={sortBy}
|
||||||
if (target.id !== "sort-dropdown") setSortDropdown(false);
|
toggleSortDropdown={() => setSortDropdown(!sortDropdown)}
|
||||||
}}
|
|
||||||
className="absolute top-8 right-0 shadow-md bg-gray-50 rounded-md p-2 z-10 border border-sky-100 w-48"
|
|
||||||
>
|
|
||||||
<p className="mb-2 text-sky-900 text-center font-semibold">
|
|
||||||
Sort by
|
|
||||||
</p>
|
|
||||||
<div className="flex flex-col gap-2">
|
|
||||||
<RadioButton
|
|
||||||
label="Name (A-Z)"
|
|
||||||
state={sortBy === "Name (A-Z)"}
|
|
||||||
onClick={handleSortChange}
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<RadioButton
|
|
||||||
label="Name (Z-A)"
|
|
||||||
state={sortBy === "Name (Z-A)"}
|
|
||||||
onClick={handleSortChange}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<RadioButton
|
|
||||||
label="Title (A-Z)"
|
|
||||||
state={sortBy === "Title (A-Z)"}
|
|
||||||
onClick={handleSortChange}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<RadioButton
|
|
||||||
label="Title (Z-A)"
|
|
||||||
state={sortBy === "Title (Z-A)"}
|
|
||||||
onClick={handleSortChange}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<RadioButton
|
|
||||||
label="Date (Newest First)"
|
|
||||||
state={sortBy === "Date (Newest First)"}
|
|
||||||
onClick={handleSortChange}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<RadioButton
|
|
||||||
label="Date (Oldest First)"
|
|
||||||
state={sortBy === "Date (Oldest First)"}
|
|
||||||
onClick={handleSortChange}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</ClickAwayHandler>
|
|
||||||
) : null}
|
) : null}
|
||||||
</div>
|
</div>
|
||||||
<div className="relative">
|
<div className="relative">
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
import ClickAwayHandler from "@/components/ClickAwayHandler";
|
import ClickAwayHandler from "@/components/ClickAwayHandler";
|
||||||
import LinkList from "@/components/LinkList";
|
import LinkList from "@/components/LinkList";
|
||||||
import RadioButton from "@/components/RadioButton";
|
import RadioButton from "@/components/RadioButton";
|
||||||
|
import SortLinkDropdown from "@/components/SortLinkDropdown";
|
||||||
import MainLayout from "@/layouts/MainLayout";
|
import MainLayout from "@/layouts/MainLayout";
|
||||||
import useLinkStore from "@/store/links";
|
import useLinkStore from "@/store/links";
|
||||||
import { faBookmark, faSort } from "@fortawesome/free-solid-svg-icons";
|
import { faBookmark, faSort } from "@fortawesome/free-solid-svg-icons";
|
||||||
|
@ -78,54 +79,11 @@ export default function Links() {
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{sortDropdown ? (
|
{sortDropdown ? (
|
||||||
<ClickAwayHandler
|
<SortLinkDropdown
|
||||||
onClickOutside={(e: Event) => {
|
handleSortChange={(e) => setSortBy(e.target.value)}
|
||||||
const target = e.target as HTMLInputElement;
|
sortBy={sortBy}
|
||||||
if (target.id !== "sort-dropdown") setSortDropdown(false);
|
toggleSortDropdown={() => setSortDropdown(!sortDropdown)}
|
||||||
}}
|
|
||||||
className="absolute top-8 right-0 shadow-md bg-gray-50 rounded-md p-2 z-10 border border-sky-100 w-48"
|
|
||||||
>
|
|
||||||
<p className="mb-2 text-sky-900 text-center font-semibold">
|
|
||||||
Sort by
|
|
||||||
</p>
|
|
||||||
<div className="flex flex-col gap-2">
|
|
||||||
<RadioButton
|
|
||||||
label="Name (A-Z)"
|
|
||||||
state={sortBy === "Name (A-Z)"}
|
|
||||||
onClick={handleSortChange}
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<RadioButton
|
|
||||||
label="Name (Z-A)"
|
|
||||||
state={sortBy === "Name (Z-A)"}
|
|
||||||
onClick={handleSortChange}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<RadioButton
|
|
||||||
label="Title (A-Z)"
|
|
||||||
state={sortBy === "Title (A-Z)"}
|
|
||||||
onClick={handleSortChange}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<RadioButton
|
|
||||||
label="Title (Z-A)"
|
|
||||||
state={sortBy === "Title (Z-A)"}
|
|
||||||
onClick={handleSortChange}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<RadioButton
|
|
||||||
label="Date (Newest First)"
|
|
||||||
state={sortBy === "Date (Newest First)"}
|
|
||||||
onClick={handleSortChange}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<RadioButton
|
|
||||||
label="Date (Oldest First)"
|
|
||||||
state={sortBy === "Date (Oldest First)"}
|
|
||||||
onClick={handleSortChange}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</ClickAwayHandler>
|
|
||||||
) : null}
|
) : null}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -14,6 +14,7 @@ import RadioButton from "@/components/RadioButton";
|
||||||
import ClickAwayHandler from "@/components/ClickAwayHandler";
|
import ClickAwayHandler from "@/components/ClickAwayHandler";
|
||||||
import { Tag } from "@prisma/client";
|
import { Tag } from "@prisma/client";
|
||||||
import useTagStore from "@/store/tags";
|
import useTagStore from "@/store/tags";
|
||||||
|
import SortLinkDropdown from "@/components/SortLinkDropdown";
|
||||||
|
|
||||||
export default function () {
|
export default function () {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
@ -55,14 +56,16 @@ export default function () {
|
||||||
setSortedLinks(
|
setSortedLinks(
|
||||||
linksArray.sort(
|
linksArray.sort(
|
||||||
(a, b) =>
|
(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)")
|
else if (sortBy === "Date (Oldest First)")
|
||||||
setSortedLinks(
|
setSortedLinks(
|
||||||
linksArray.sort(
|
linksArray.sort(
|
||||||
(a, b) =>
|
(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]);
|
}, [links, router, tags, sortBy]);
|
||||||
|
@ -95,54 +98,11 @@ export default function () {
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{sortDropdown ? (
|
{sortDropdown ? (
|
||||||
<ClickAwayHandler
|
<SortLinkDropdown
|
||||||
onClickOutside={(e: Event) => {
|
handleSortChange={(e) => setSortBy(e.target.value)}
|
||||||
const target = e.target as HTMLInputElement;
|
sortBy={sortBy}
|
||||||
if (target.id !== "sort-dropdown") setSortDropdown(false);
|
toggleSortDropdown={() => setSortDropdown(!sortDropdown)}
|
||||||
}}
|
|
||||||
className="absolute top-8 right-0 shadow-md bg-gray-50 rounded-md p-2 z-10 border border-sky-100 w-48"
|
|
||||||
>
|
|
||||||
<p className="mb-2 text-sky-900 text-center font-semibold">
|
|
||||||
Sort by
|
|
||||||
</p>
|
|
||||||
<div className="flex flex-col gap-2">
|
|
||||||
<RadioButton
|
|
||||||
label="Name (A-Z)"
|
|
||||||
state={sortBy === "Name (A-Z)"}
|
|
||||||
onClick={handleSortChange}
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<RadioButton
|
|
||||||
label="Name (Z-A)"
|
|
||||||
state={sortBy === "Name (Z-A)"}
|
|
||||||
onClick={handleSortChange}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<RadioButton
|
|
||||||
label="Title (A-Z)"
|
|
||||||
state={sortBy === "Title (A-Z)"}
|
|
||||||
onClick={handleSortChange}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<RadioButton
|
|
||||||
label="Title (Z-A)"
|
|
||||||
state={sortBy === "Title (Z-A)"}
|
|
||||||
onClick={handleSortChange}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<RadioButton
|
|
||||||
label="Date (Newest First)"
|
|
||||||
state={sortBy === "Date (Newest First)"}
|
|
||||||
onClick={handleSortChange}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<RadioButton
|
|
||||||
label="Date (Oldest First)"
|
|
||||||
state={sortBy === "Date (Oldest First)"}
|
|
||||||
onClick={handleSortChange}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</ClickAwayHandler>
|
|
||||||
) : null}
|
) : null}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
Ŝarĝante…
Reference in New Issue