Skip to content

Commit 4e64ece

Browse files
ideas FINALLY WORKING OMG
1 parent 2f542a1 commit 4e64ece

2 files changed

Lines changed: 34 additions & 21 deletions

File tree

src/api/fetchIdeas.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@ const ideaSchema = z.object({
3232
});
3333

3434
export const ideasResponseSchema = z.object({
35-
status: z.string(), // Ensures the status is always "success"
35+
status: z.string(),
3636
message: z.string(),
37-
data: z.array(ideaSchema),
38-
next_cursor: z.string().nullable(),
37+
data: z.object({
38+
ideas: z.array(ideaSchema).nullable(),
39+
next_cursor: z.string().nullable(),
40+
}),
3941
});
42+
4043
export type ideaType = z.infer<typeof ideaSchema>;
4144

4245
export type ideaResponseType = z.infer<typeof ideasResponseSchema>;
@@ -60,18 +63,21 @@ export const fetchIdeas = async ({
6063
} else if (cursorId) {
6164
params.append("cursor", cursorId);
6265
}
63-
const url = track !== ""? `admin/ideas/${track}?${params.toString()}` : `admin/ideas?${params.toString()}`;
66+
const url =
67+
track !== ""
68+
? `admin/ideas/${track}?${params.toString()}`
69+
: `admin/ideas?${params.toString()}`;
6470

6571
const response = await axios.get<ideaResponseType>(url);
6672
const parsedResponse = ideasResponseSchema.parse(response.data);
6773
console.log(parsedResponse.data);
6874

6975
//send in next cursor when data is done
70-
const nextCursor = parsedResponse.next_cursor;
76+
const nextCursor = parsedResponse.data.next_cursor;
7177

7278
return {
7379
idea: parsedResponse.data,
74-
nextCursor,
80+
nextCursor,
7581
};
7682
} catch (err) {
7783
console.log(err);

src/app/idea/page.tsx

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,39 @@ import loading from "@/assets/images/loading.gif";
1515
import Image from "next/image";
1616

1717
import { type Team } from "@/data/schema";
18-
import { useQuery } from "@tanstack/react-query";
18+
import { useQuery, useQueryClient } from "@tanstack/react-query";
1919
import { type ColumnDef } from "@tanstack/react-table";
2020
import { useEffect, useState } from "react";
21+
import toast from "react-hot-toast";
2122

2223
export default function TeamsIdeasTable() {
24+
const queryClient = useQueryClient();
2325
const [cursorHistory, setCursorHistory] = useState<string[]>([]);
2426
const [currentCursor, setCurrentCursor] = useState<string | undefined>(
2527
undefined,
2628
);
2729

2830
const [pageLimit, setPageLimit] = useState(10);
2931
const [searchTerm, setSearchTerm] = useState("");
30-
const [currentPage, setCurrentPage] = useState(0);
3132
const [selectedTrack, setSelectedTrack] = useState<string>("");
3233
const tracks = [
3334
"Media and Entertainment",
3435
"Finance and Fintech",
3536
"Healthcare and Education",
36-
"Digital Security",
37-
"Environment and Sustainability",
3837
"Environment and Sustainability",
38+
"Digital Security",
3939
"Open Innovation",
4040
];
4141
const {
4242
data: ideasData,
4343
isLoading: ideasLoading,
4444
isError: ideasError,
4545
} = useQuery({
46-
queryKey: ["idea", currentPage, pageLimit],
46+
queryKey: ["idea", currentCursor, pageLimit],
4747
queryFn: () =>
4848
fetchIdeas({
4949
limit: pageLimit,
50-
cursorId: undefined,
50+
cursorId: currentCursor,
5151
track: selectedTrack,
5252
}),
5353
});
@@ -58,7 +58,12 @@ export default function TeamsIdeasTable() {
5858
setCurrentCursor(ideasData.nextCursor); // Move to the next page
5959
}
6060
};
61-
61+
useEffect(() => {
62+
void queryClient.invalidateQueries({
63+
queryKey: ["idea"],
64+
});
65+
toast.error(selectedTrack);
66+
}, [selectedTrack]);
6267
const handlePrevPage = () => {
6368
if (cursorHistory.length > 0) {
6469
const prevCursor = cursorHistory[cursorHistory.length - 1]; // Get last cursor
@@ -133,7 +138,7 @@ export default function TeamsIdeasTable() {
133138
if (ideasError) {
134139
return (
135140
<div className="flex justify-center p-8">
136-
<div className="text-lg text-red-500">Error loading teams data</div>
141+
<div className="text-lg text-red-500">Error loading ideas</div>
137142
</div>
138143
);
139144
}
@@ -153,28 +158,30 @@ export default function TeamsIdeasTable() {
153158
onChange={(e) => setSearchTerm(e.target.value)}
154159
/>
155160
<Select
156-
value={selectedTrack ?? "all"}
157-
onValueChange={(value) =>
158-
setSelectedTrack(value === "all" ? "" : value)
159-
}
161+
value={selectedTrack ?? ""}
162+
onValueChange={(value) => {
163+
setSelectedTrack(
164+
value === "all" ? "" : String(Number(value) ),
165+
);
166+
}}
160167
>
161168
<SelectTrigger className="w-48 p-6">
162169
<SelectValue placeholder="Filter by track" />
163170
</SelectTrigger>
164171
<SelectContent>
165172
<SelectItem value="all">All Tracks</SelectItem>
166173
{tracks.map((track, index) => (
167-
<SelectItem key={track} value={String(index)}>
174+
<SelectItem key={track} value={String(index + 1)}>
168175
{track}
169176
</SelectItem>
170177
))}
171178
</SelectContent>
172179
</Select>
173180
</div>
174-
{ideasData?.idea && (
181+
{ (
175182
<DataTable
176183
columns={columns}
177-
data={ideasData?.idea}
184+
data={ideasData?.idea.ideas ?? []}
178185
handleNextPage={handleNextPage}
179186
handlePrevPage={handlePrevPage}
180187
setPageLimit={setPageLimit}

0 commit comments

Comments
 (0)