generated from scaffold-eth/scaffold-eth-2
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from electrone901/main
Add SuggestedProjects API with several changes and new files
- Loading branch information
Showing
7 changed files
with
66 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
packages/nextjs/hooks/scaffold-eth/useSuggestedProjects.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
export function useSuggestedProjects(category: any, currentProjectId: string) { | ||
const [suggestedProjects, setSuggestedProjects] = useState([]); | ||
|
||
useEffect(() => { | ||
const getSuggestedProjectsByCategory = async () => { | ||
try { | ||
const response = await fetch( | ||
`/api/suggestedProjects?category=${category}¤tProjectId=${currentProjectId}`, | ||
); | ||
const data = await response.json(); | ||
setSuggestedProjects(data); | ||
} catch (error) { | ||
console.error("Error fetching /api/suggested-projects/category", error); | ||
throw error; | ||
} | ||
}; | ||
if (category && currentProjectId) { | ||
getSuggestedProjectsByCategory(); | ||
} | ||
}, [category, currentProjectId]); | ||
|
||
return { | ||
suggestedProjects, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
import dbConnect from "~~/lib/dbConnect"; | ||
import Project, { ProjectDocument } from "~~/models/Project"; | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
if (req.method !== "GET") { | ||
return res.status(405).json({ message: "Method not allowed." }); | ||
} | ||
await dbConnect(); | ||
const { category, currentProjectId } = req.query; | ||
const suggestedProjects: ProjectDocument[] = await Project.find({ category, _id: { $ne: currentProjectId } }).limit( | ||
3, | ||
); | ||
|
||
return res.status(200).json(suggestedProjects); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters