Skip to content

Commit

Permalink
feat: fetch quest subscribers
Browse files Browse the repository at this point in the history
  • Loading branch information
oreHGA committed May 14, 2024
1 parent 1259c12 commit c42db07
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
47 changes: 46 additions & 1 deletion frontend/src/pages/quests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,54 @@ const QuestsPage: NextPage = () => {
}
};

const [questSubscribers, setQuestSubscribers] = React.useState<any[]>([]);
const getQuestSubscribers = async (questId: string) => {
try {
const res = await api.get(
"/quest/subscribers",

{
params: {
questId,
},
headers: {
Authorization: `Bearer ${session.data?.user?.authToken}`,
},
}
);

if (res.status === 200) {
console.log("Quest Subscribers fetched successfully");
console.log(res.data);
return res.data.userQuests;
} else {
console.error("Failed to fetch quest subscribers");
}
} catch (error) {
console.error("Failed to fetch quest subscribers", error);
}
};

React.useEffect(() => {
getSavedQuests();
}, []);

React.useEffect(() => {
(async () => {
if (activeQuest) {
const subscribers = await getQuestSubscribers(activeQuest.guid);
console.log("subscribers", subscribers);

if (subscribers) {
// update activeQuest with participants
setQuestSubscribers(subscribers);
} else {
setQuestSubscribers([]);
}
}
})();
}, [activeQuest]);

return (
<DashboardLayout>
<Meta
Expand Down Expand Up @@ -262,12 +306,13 @@ const QuestsPage: NextPage = () => {
</table>
</>
)}
{/* Share Quest Modal */}
{activeQuest && (
<Dialog open={displayShareModal} onOpenChange={() => setDisplayShareModal(!displayShareModal)}>
<DialogContent>
<DialogTitle>Share Quest</DialogTitle>
<div>
<div>Active Participants</div>
<div>Active Participants: {questSubscribers.length}</div>
</div>
<DialogDescription>
Share the code with your participants to join this quest using the Fusion mobile app
Expand Down
41 changes: 41 additions & 0 deletions server/controllers/quest.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,45 @@ exports.getUserQuestSubscription = async (req, res) => {
}
};

exports.getQuestSubscribers = async (req, res) => {
try {
const userQuests = await db.UserQuest.findAll({
where: {
questGuid: req.query.questId,
},
});

console.log(userQuests);

if (!userQuests) {
res.status(404).json({
error: "UserQuests not found",
});
return;
}

// go ahead and get the userNpub from the userMetadata table
for (let i = 0; i < userQuests.length; i++) {
const user = await db.UserMetadata.findOne({
where: {
userGuid: userQuests[i].userGuid,
},
});

if (user) {
userQuests[i].user = user;
}
}

res.status(200).json({
userQuests,
});
} catch (err) {
console.error(err);
res.status(500).json({
error: "Error getting userQuests",
});
}
};

// TODO: exports.resetJoinCode
1 change: 1 addition & 0 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ app.get(
"/api/quest/userSubscription",
questController.getUserQuestSubscription
);
app.get("/api/quest/subscribers", questController.getQuestSubscribers);

/**
* Start server
Expand Down

0 comments on commit c42db07

Please sign in to comment.