Skip to content

Commit b1576b6

Browse files
authored
Merge pull request #70 from deepraj21/main
fix: star projects component by other users
2 parents 837e5db + ec879ac commit b1576b6

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

client/src/components/Projects/ProjectCard.tsx

Lines changed: 19 additions & 6 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,10 +61,12 @@ 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 {
65-
const response = await fetch(`${backendUrl}/profile/${paramsUsername}/projects/${project.projectId}/star`, {
69+
const response = await fetch(`${backendUrl}/profile/${username}/projects/${project.projectId}/star`, {
6670
method: "POST",
6771
headers: { "Content-Type": "application/json" },
6872
});
@@ -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

@@ -97,21 +108,23 @@ export function ProjectCard({ project, onProjectChange }: ProjectProps) {
97108
>
98109
{project.title}
99110
</CardTitle>
100-
<ReactMarkdown>{project.description}</ReactMarkdown>
111+
<ReactMarkdown>
112+
{project.description.split(' ').slice(0, 5).join(' ') + (project.description.split(' ').length > 5 ? '...' : '')}
113+
</ReactMarkdown>
101114
</div>
102115
<div className="flex items-center rounded-md bg-secondary text-secondary-foreground">
103116
<Button
104117
variant="secondary"
105-
className=" shadow-none"
118+
className="shadow-none"
106119
onClick={handleStarClick}
107-
disabled={isStarred}
120+
disabled={isStarred || isStarRequestInProgress} // Disable during star request
108121
>
109122
{isStarred ? (
110123
<FilledStarIcon className="mr-2 h-4 w-4 text-yellow-400" />
111124
) : (
112125
<StarIcon className="mr-2 h-4 w-4" />
113126
)}
114-
Star
127+
{isStarRequestInProgress ? "Starring..." : "Star"} {/* Show loading text */}
115128
</Button>
116129
</div>
117130
</CardHeader>

client/src/pages/ProjectDisplay.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const ProjectDisplay = () => {
3434
const [isStarred, setIsStarred] = useState(false);
3535
const [starCount, setStarCount] = useState(0); // Initialize as 0
3636
const [isStarRequestInProgress, setIsStarRequestInProgress] = useState(false); // To disable button during request
37+
const loggedInUsername = localStorage.getItem("devhub_username");
3738

3839
useEffect(() => {
3940
const fetchProject = async () => {
@@ -69,7 +70,7 @@ const ProjectDisplay = () => {
6970
setIsStarRequestInProgress(true); // Disable the button while the request is in progress
7071

7172
try {
72-
const response = await fetch(`${backendUrl}/profile/${username}/projects/${project.projectId}/star`, {
73+
const response = await fetch(`${backendUrl}/profile/${loggedInUsername}/projects/${project.projectId}/star`, {
7374
method: "POST",
7475
headers: { "Content-Type": "application/json" },
7576
});

0 commit comments

Comments
 (0)