diff --git a/frontend/src/pages/quests.tsx b/frontend/src/pages/quests.tsx index 9c02f5e0..adac5de1 100644 --- a/frontend/src/pages/quests.tsx +++ b/frontend/src/pages/quests.tsx @@ -122,10 +122,54 @@ const QuestsPage: NextPage = () => { } }; + const [questSubscribers, setQuestSubscribers] = React.useState([]); + 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 ( { )} + {/* Share Quest Modal */} {activeQuest && ( setDisplayShareModal(!displayShareModal)}> Share Quest
-
Active Participants
+
Active Participants: {questSubscribers.length}
Share the code with your participants to join this quest using the Fusion mobile app diff --git a/server/controllers/quest.js b/server/controllers/quest.js index a6ddce74..0ece1b43 100644 --- a/server/controllers/quest.js +++ b/server/controllers/quest.js @@ -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 diff --git a/server/index.ts b/server/index.ts index c8952119..50099a2e 100644 --- a/server/index.ts +++ b/server/index.ts @@ -126,6 +126,7 @@ app.get( "/api/quest/userSubscription", questController.getUserQuestSubscription ); +app.get("/api/quest/subscribers", questController.getQuestSubscribers); /** * Start server