diff --git a/src/components/ToolbarButton.tsx b/src/components/ToolbarButton.tsx index b75f767..fc66c8f 100644 --- a/src/components/ToolbarButton.tsx +++ b/src/components/ToolbarButton.tsx @@ -4,10 +4,12 @@ export default function ToolbarButton({ children, className, onClick, + disabled = false, }: { children?: React.ReactNode; className?: string; onClick?: () => void; + disabled?: boolean; }) { return ( diff --git a/src/pages/coverphoto/CoverPhotos.tsx b/src/pages/coverphoto/CoverPhotos.tsx index 3004f76..dc026e6 100644 --- a/src/pages/coverphoto/CoverPhotos.tsx +++ b/src/pages/coverphoto/CoverPhotos.tsx @@ -1,6 +1,6 @@ -import { useEffect, useState } from "react"; +import { useCallback, useEffect, useState } from "react"; -import { PlusCircle, Sort } from "iconoir-react"; +import { PlusCircle, Sort, SortDown } from "iconoir-react"; import { getCoverPhotos } from "../../api/coverPhotos"; import type { CoverPhoto } from "../../types/coverphoto"; @@ -8,23 +8,39 @@ import type { CoverPhoto } from "../../types/coverphoto"; import ToolbarButton from "../../components/ToolbarButton"; import CoverPhotoCard from "./CoverPhotoCard"; +type SortingStrategy = "asc" | "desc"; + export default function CoverPhotos() { const [isLoading, setIsLoading] = useState(true); const [coverPhotos, setCoverPhotos] = useState([]); + const [sortingStrategy, setSortingStrategy] = + useState("desc"); + + const toggleSortingStrategy = useCallback(() => { + setSortingStrategy((prev) => (prev === "asc" ? "desc" : "asc")); + }, []); useEffect(() => { setIsLoading(true); - setTimeout(() => { - // TODO: remove this setTimeout -- just to simulate an artificial delay - void getCoverPhotos().then((data) => - setCoverPhotos( - data.sort((a, b) => (a.createdAt > b.createdAt ? -1 : 1)) - ) - ); - setIsLoading(false); - }, 1000); + void getCoverPhotos().then((data) => + setCoverPhotos(data.sort((a, b) => (a.createdAt > b.createdAt ? -1 : 1))) + ); + setIsLoading(false); }, []); + useEffect(() => { + console.log(sortingStrategy); + setCoverPhotos((prev) => + prev.sort((a, b) => { + if (sortingStrategy === "asc") { + return a.createdAt > b.createdAt ? -1 : 1; + } else { + return a.createdAt > b.createdAt ? 1 : -1; + } + }) + ); + }, [sortingStrategy]); + if (isLoading) return

Loading...

; return ( @@ -34,8 +50,13 @@ export default function CoverPhotos() { New Cover Photo - - Sort by created time + + {sortingStrategy === "desc" ? : } Sort by created + time