Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions backend/controllers/questController.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const createQuestUnAuth = async (req, res) => {
}
};

// FIXME add auth MUST --- Give kudos >>>>> Doesn't prevent from liking more than once - why??
// -------- Give kudos ----------
const giveKudos = async (req, res) => {
//console.log("Give kudos");
const { id } = req.params;
Expand All @@ -49,7 +49,7 @@ const giveKudos = async (req, res) => {
const update = { $inc: { kudos: 1 }, $push: { kudosByUser: userId } };
const options = { new: true, runValidators: true };

const addKudos = await Quest.findByIdAndUpdate(
const addKudos = await Quest.findOneAndUpdate(
{ _id: id, kudosByUser: { $ne: userId } },
update,
options,
Expand All @@ -61,7 +61,7 @@ const giveKudos = async (req, res) => {
message: "Can't give cudos more than once to the same quest",
});
}
return res.status(200).json({ succes: true, response: addKudos });
return res.status(200).json({ success: true, response: addKudos });
} catch (err) {
return res.status(500).json({
success: false,
Expand Down
10 changes: 5 additions & 5 deletions backend/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,19 @@ const deleteUser = async (req, res) => {
const user = await User.findByIdAndDelete(id);
if (!user) {
return res.status(404).json({
succes: false,
success: false,
message: `User with id ${id} doesn't exist or was permanently deleted`,
});
}
return res.status(200).json({
succes: true,
success: true,
response: [user.email, user.name],
message: "User was permanently deleted",
});
} catch (err) {
return res
.status(500)
.json({ succes: false, message: "Server error", error: err.message });
.json({ success: false, message: "Server error", error: err.message });
}
};

Expand Down Expand Up @@ -140,7 +140,7 @@ const userStreak = async (req, res) => {

if (completedQuests.length === 0) {
return res.status(200).json({
succes: true,
success: true,
message: "There are no completed quests yet",
streak: 0,
});
Expand Down Expand Up @@ -175,7 +175,7 @@ const userStreak = async (req, res) => {
return res.status(200).json({ success: true, response: streak });
} catch (err) {
return res.status(500).json({
succes: false,
success: false,
message: "Somethng went wrong at the server",
error: err.errors,
});
Expand Down
43 changes: 18 additions & 25 deletions frontend/src/components/cards/FriendQuestCard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@ export const FriendQuestCard = ({
}) => {
const { user } = useUserStore();
const [kudosCount, setKudosCount] = useState(kudos || 0);
const [loading, setLoading] = useState(false);

//conditional check?
useEffect(() => {
setKudosCount(kudos || 0);
}, [kudos]);

//conditional check
const handleClick = async () => {
if (loading) return;

setLoading(true);
setKudosCount((prev) => prev + 1);

try {
Expand All @@ -30,46 +38,31 @@ export const FriendQuestCard = ({
});

const data = await response.json();
//console.log("STATUS:", response.status);
//console.log("DATA:", data);

if (!response.ok || !data.success) {
throw new Error(data.message || "Couldn't add kudos");
throw new Error(data.message || "Failed to give kudos");
}

setKudosCount(data.response.kudos);
} catch (err) {
setKudosCount((prev) => prev - 1);
console.error(err);
} finally {
setLoading(false);
}

/* fetch(apiUrl + `/quests/${id}/kudos`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: user?.accessToken,
},
})
.then((res) => res.json())
.then((data) => {
if (data.success) {
setKudosCount(data.response.kudos);
} else {
setKudosCount((prev) => prev - 1);
alert(data.message);
}
})
.catch(() => {
setKudosCount((prev) => prev - 1);
console.error("Couldn't add kudos");
}); */
};

return (
<MainWrapper isNew={isNew}>
<MainWrapper $isNew={isNew}>
<Cardheader>
<Name>{createdBy.name || "User"}</Name>
{/* <Avatar src={createdBy.moodUrl} alt={createdBy.name || 'User'} /> */}
<ActionWrapper>
<Button onClick={handleClick}>Kudos: {kudosCount}</Button>
<Button onClick={handleClick} disabled={loading}>
Kudos: {kudosCount}
</Button>
</ActionWrapper>
</Cardheader>
<QuestInfoWrapper>
Expand Down