mirror of
https://github.com/Ategon/Jamjar.git
synced 2025-02-12 06:16:21 +00:00
Add post sorts
This commit is contained in:
parent
32705c8662
commit
e5aa7c2a2a
2 changed files with 30 additions and 7 deletions
src
|
@ -3,31 +3,53 @@
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import PostCard from "./PostCard";
|
import PostCard from "./PostCard";
|
||||||
import { PostType } from "@/types/PostType";
|
import { PostType } from "@/types/PostType";
|
||||||
import { Button } from "@nextui-org/react";
|
import {
|
||||||
|
Button,
|
||||||
|
Dropdown,
|
||||||
|
DropdownItem,
|
||||||
|
DropdownMenu,
|
||||||
|
DropdownTrigger,
|
||||||
|
} from "@nextui-org/react";
|
||||||
|
import { PostSort } from "@/types/PostSort";
|
||||||
|
|
||||||
export default function Posts() {
|
export default function Posts() {
|
||||||
const [posts, setPosts] = useState<PostType[]>();
|
const [posts, setPosts] = useState<PostType[]>();
|
||||||
|
const [sort, setSort] = useState<PostSort>("newest");
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchPosts = async () => {
|
const fetchPosts = async () => {
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
process.env.NEXT_PUBLIC_MODE === "PROD"
|
process.env.NEXT_PUBLIC_MODE === "PROD"
|
||||||
? "https://d2jam.com/api/v1/posts"
|
? `https://d2jam.com/api/v1/posts?sort=${sort}`
|
||||||
: "http://localhost:3005/api/v1/posts"
|
: `http://localhost:3005/api/v1/posts?sort=${sort}`
|
||||||
);
|
);
|
||||||
setPosts(await response.json());
|
setPosts(await response.json());
|
||||||
};
|
};
|
||||||
|
|
||||||
fetchPosts();
|
fetchPosts();
|
||||||
}, []);
|
}, [sort]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<div className="flex justify-between p-4 pb-0">
|
<div className="flex justify-between p-4 pb-0">
|
||||||
<div className="flex gap-2">
|
<div className="flex gap-2">
|
||||||
<Button size="sm" className="text-xs" variant="faded">
|
<Dropdown>
|
||||||
Newest
|
<DropdownTrigger>
|
||||||
</Button>
|
<Button size="sm" className="text-xs" variant="faded">
|
||||||
|
{sort.charAt(0).toUpperCase() + sort.slice(1)}
|
||||||
|
</Button>
|
||||||
|
</DropdownTrigger>
|
||||||
|
<DropdownMenu
|
||||||
|
onAction={(key) => {
|
||||||
|
setSort(key as PostSort);
|
||||||
|
}}
|
||||||
|
className="text-black"
|
||||||
|
>
|
||||||
|
<DropdownItem key="newest">Newest</DropdownItem>
|
||||||
|
<DropdownItem key="top">Top</DropdownItem>
|
||||||
|
<DropdownItem key="oldest">Oldest</DropdownItem>
|
||||||
|
</DropdownMenu>
|
||||||
|
</Dropdown>
|
||||||
<Button size="sm" className="text-xs" variant="faded">
|
<Button size="sm" className="text-xs" variant="faded">
|
||||||
All Tags
|
All Tags
|
||||||
</Button>
|
</Button>
|
||||||
|
|
1
src/types/PostSort.ts
Normal file
1
src/types/PostSort.ts
Normal file
|
@ -0,0 +1 @@
|
||||||
|
export type PostSort = "newest" | "oldest";
|
Loading…
Reference in a new issue