Skip to content

Commit acb5140

Browse files
ideas working yay
1 parent 06c11ae commit acb5140

2 files changed

Lines changed: 32 additions & 12 deletions

File tree

src/api/fetchIdeas.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import axios from "./axiosConfig";
2-
import { z } from "zod";
2+
import { number, z } from "zod";
33

44
const submissionSchema = z.object({
55
title: z.string().nullable(),
@@ -40,19 +40,17 @@ export type ideaType = z.infer<typeof ideaSchema>;
4040

4141
export type ideaResponseType = z.infer<typeof ideasResponseSchema>;
4242

43-
44-
45-
46-
4743
export const fetchIdeas = async ({
4844
limit,
4945
cursorId,
5046
name,
47+
track,
5148
}: {
5249
limit: number;
5350
cursorId?: string;
54-
name?: string;}
55-
) => {
51+
name?: string;
52+
track?: number;
53+
}) => {
5654
try {
5755
const params = new URLSearchParams({ limit: String(limit) });
5856

@@ -61,17 +59,20 @@ export const fetchIdeas = async ({
6159
} else if (cursorId) {
6260
params.append("cursor", cursorId);
6361
}
62+
const url = track !== undefined? `admin/ideas/${track}/?${params.toString()}` : `admin/ideas?${params.toString()}`;
6463

65-
const response = await axios.get<ideaResponseType>(`admin/ideas`);
64+
const response = await axios.get<ideaResponseType>(url);
6665
const parsedResponse = ideasResponseSchema.parse(response.data);
6766
console.log(parsedResponse.data);
68-
const nextCursor = 2;
67+
68+
//send in next cursor when data is done
69+
const nextCursor = "1";
6970

7071
return {
7172
idea: parsedResponse.data,
7273
nextCursor,
7374
};
74-
} catch (err) {
75+
} catch (err) {
7576
console.log(err);
7677
throw err;
7778
}

src/app/idea/page.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ import { type ColumnDef } from "@tanstack/react-table";
1717
import { useEffect, useState } from "react";
1818

1919
export default function TeamsIdeasTable() {
20+
const [cursorHistory, setCursorHistory] = useState<string[]>([]);
21+
const [currentCursor, setCurrentCursor] = useState<string | undefined>(
22+
undefined,
23+
);
24+
2025
const [pageLimit, setPageLimit] = useState(10);
2126
const [searchTerm, setSearchTerm] = useState("");
2227
const [currentPage, setCurrentPage] = useState(0);
@@ -35,7 +40,21 @@ export default function TeamsIdeasTable() {
3540
cursorId: undefined,
3641
}),
3742
});
43+
const handleNextPage = () => {
44+
if (ideasData?.nextCursor) {
45+
console.log("yes cursor available");
46+
setCursorHistory((prev) => [...prev, currentCursor ?? ""]); // Store current cursor
47+
setCurrentCursor(ideasData.nextCursor); // Move to the next page
48+
}
49+
};
3850

51+
const handlePrevPage = () => {
52+
if (cursorHistory.length > 0) {
53+
const prevCursor = cursorHistory[cursorHistory.length - 1]; // Get last cursor
54+
setCursorHistory((prev) => prev.slice(0, -1)); // Remove last cursor from history
55+
setCurrentCursor(prevCursor ?? undefined); // Move to previous page
56+
}
57+
};
3958
const columns: ColumnDef<ideaType, unknown>[] = [
4059
{
4160
accessorKey: "numberOfPeople",
@@ -137,8 +156,8 @@ export default function TeamsIdeasTable() {
137156
<DataTable
138157
columns={columns}
139158
data={ideasData?.idea}
140-
handleNextPage={() => setCurrentPage((prev) => prev + 1)}
141-
handlePrevPage={() => setCurrentPage((prev) => prev - 1)}
159+
handleNextPage={handleNextPage}
160+
handlePrevPage={handlePrevPage}
142161
setPageLimit={setPageLimit}
143162
pageLimit={pageLimit}
144163
/>

0 commit comments

Comments
 (0)