Skip to content

Commit 0dd99bd

Browse files
committed
feat: adding toast message to star
1 parent a609f60 commit 0dd99bd

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

client/src/components/Projects/ProjectCard.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { Cross1Icon } from "@radix-ui/react-icons";
1717
import { Skeleton } from "../ui/skeleton";
1818
import { useNavigate, useParams } from "react-router-dom";
1919
import ReactMarkdown from 'react-markdown';
20+
import { toast } from "sonner";
2021

2122
interface Project {
2223
projectId: string;
@@ -38,6 +39,7 @@ const backendUrl = import.meta.env.VITE_BACKEND_URL || 'http://localhost:5000';
3839
export function ProjectCard({ project, onProjectChange }: ProjectProps) {
3940
const [isStarred, setIsStarred] = useState(false);
4041
const [starCount, setStarCount] = useState(project.starCount);
42+
const [isStarRequestInProgress, setIsStarRequestInProgress] = useState(false); // State to handle star request
4143
const username = localStorage.getItem('devhub_username'); // Logged-in user
4244
const { username: paramsUsername } = useParams<{ username: string }>(); // Extract username from URL
4345

@@ -59,7 +61,9 @@ export function ProjectCard({ project, onProjectChange }: ProjectProps) {
5961
}, [project.projectId]);
6062

6163
const handleStarClick = async () => {
62-
if (isStarred) return; // Prevent multiple stars
64+
if (isStarred || isStarRequestInProgress) return; // Prevent multiple stars and requests
65+
66+
setIsStarRequestInProgress(true); // Start request
6367

6468
try {
6569
const response = await fetch(`${backendUrl}/profile/${username}/projects/${project.projectId}/star`, {
@@ -71,11 +75,18 @@ export function ProjectCard({ project, onProjectChange }: ProjectProps) {
7175
setStarCount((prevCount) => prevCount + 1);
7276
setIsStarred(true);
7377
localStorage.setItem(`starred_${project.projectId}`, "true");
78+
79+
// Show success toast
80+
toast.success("Project starred!");
7481
} else {
7582
console.error("Failed to star the project");
83+
toast.error("Failed to star the project");
7684
}
7785
} catch (error) {
7886
console.error("Error starring the project:", error);
87+
toast.error("Failed to star the project");
88+
} finally {
89+
setIsStarRequestInProgress(false); // End request
7990
}
8091
};
8192

@@ -104,16 +115,16 @@ export function ProjectCard({ project, onProjectChange }: ProjectProps) {
104115
<div className="flex items-center rounded-md bg-secondary text-secondary-foreground">
105116
<Button
106117
variant="secondary"
107-
className=" shadow-none"
118+
className="shadow-none"
108119
onClick={handleStarClick}
109-
disabled={isStarred}
120+
disabled={isStarred || isStarRequestInProgress} // Disable during star request
110121
>
111122
{isStarred ? (
112123
<FilledStarIcon className="mr-2 h-4 w-4 text-yellow-400" />
113124
) : (
114125
<StarIcon className="mr-2 h-4 w-4" />
115126
)}
116-
Star
127+
{isStarRequestInProgress ? "Starring..." : "Star"} {/* Show loading text */}
117128
</Button>
118129
</div>
119130
</CardHeader>

0 commit comments

Comments
 (0)