-
Notifications
You must be signed in to change notification settings - Fork 14
/
listingChart.tsx
153 lines (145 loc) · 5.27 KB
/
listingChart.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"use client";
import { useEffect, useState } from "react";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { AlertTriangle, ChevronLeft, ChevronRight } from "lucide-react";
import ItemCard from "@/components/itemCard";
import { getItems } from "@/db/utils";
import { Item, User } from "@/db/schema";
import { SORTS, Sort, valueToSort } from "@/lib/sorting";
import { CATEGORIES, Category, valueToCategory } from "@/lib/categories";
/*
This ListingChart component is loaded on the client side and is responsible for retrieving
and displaying the listings for a given seller.
*/
export default function ListingChart({ seller }: { seller: User }) {
const [items, setItems] = useState<Item[]>([]); // The seller's listings
const [sorting, setSorting] = useState<Sort | null>(null); // The sorting method
const [category, setCategory] = useState<Category | null>(null); // The category
const [listed, setListed] = useState<boolean>(true); // Whether to show listed or unlisted items
const [pageNumber, setPageNumber] = useState(1); // The current page number
const [pageSize] = useState(10); // The number of items per page
/*
On page load and whenever the sorting, category, or listed state changes, get the items.
*/
useEffect(() => {
/*
TODO: Get the items with the given parameters and update the items state.
HINT: Use the following parameters:
- user: The seller's username
- sort: The sorting method
- category: The category
- listed: Whether to show listed or unlisted items
*/
}, [sorting, category, listed, seller]);
return (
<div className="flex flex-col gap-4 items-center py-4">
<div className="w-full flex flex-row items-center justify-between gap-4 px-4">
<div></div>
<div className="flex flex-row gap-2">
<Select
value={listed ? "listed" : "unlisted"}
onValueChange={(value) => {
setListed(value === "listed");
}}
>
<SelectTrigger className="w-[180px]">
<SelectValue placeholder="Show listed or unlisted items" />
</SelectTrigger>
<SelectContent>
<SelectItem value="listed">Listed</SelectItem>
<SelectItem value="unlisted">Unlisted</SelectItem>
</SelectContent>
</Select>
<Select
value={category?.value}
onValueChange={(value) => {
if (value === category?.value) {
setCategory(null);
} else {
setCategory(valueToCategory(value));
}
}}
>
<SelectTrigger className="w-[180px]">
<SelectValue placeholder="Select a category" />
</SelectTrigger>
<SelectContent>
{CATEGORIES.map((category) => (
<SelectItem value={category.value} key={category.value}>
{category.display}
</SelectItem>
))}
</SelectContent>
</Select>
<Select
value={sorting?.value}
onValueChange={(value) => {
if (value === sorting?.value) {
setSorting(null);
} else {
setSorting(valueToSort(value));
}
}}
>
<SelectTrigger className="w-[180px] focus-visible:ring-0 focus:ring-0">
<SelectValue placeholder="Sort by" />
</SelectTrigger>
<SelectContent>
{SORTS.map((sort) => (
<SelectItem value={sort.value} key={sort.value}>
{sort.display}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
</div>
{
/*
TODO: If there are no items, display the provided loading message.
```
<div className="flex flex-col justify-center items-center w-full h-full">
<AlertTriangle className="w-16 h-16 text-yellow-400" />
<span className="text-xl">No listings found</span>
</div>
```
*/
}
<div className="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5 gap-4 h-full">
{
/*
TODO: Paginate the items and map over each item and return an ItemCard component with the itemId prop set to the item's id.
*/
"PLACEHOLDER"
}
</div>
<div className="flex flex-row justify-center items-center gap-4">
{pageNumber > 1 && (
<div
className="flex flex-row justify-center items-center gap-4 cursor-pointer"
onClick={() => setPageNumber(pageNumber - 1)}
>
<ChevronLeft />
<span>{pageNumber - 1}</span>
</div>
)}
<span className="underline underline-offset-4">{pageNumber}</span>
{items.length > pageNumber * pageSize && (
<div
className="flex flex-row justify-center items-center gap-4 cursor-pointer"
onClick={() => setPageNumber(pageNumber + 1)}
>
<span>{pageNumber + 1}</span>
<ChevronRight />
</div>
)}
</div>
</div>
);
}