Challenging and room creating working now

This commit is contained in:
Moon Patel
2023-07-02 19:21:36 +05:30
parent dd5f3069d0
commit 40acca7dd1
3 changed files with 17 additions and 4 deletions
+3 -1
View File
@@ -23,9 +23,11 @@ router.post("/create", checkAuth, async (req, res, next) => {
let challenges = pendingChallenges.get(challenged);
challenges.push(challenger);
} else {
pendingChallenges.set(challenged, [challenger]);
pendingChallenges.set(challenged, [{ challenger, roomID }]);
}
console.log("Pending challenges", pendingChallenges);
// sendEmail(
// challengedEmail,
// `Challenge from ${challenger}`,
+1
View File
@@ -37,6 +37,7 @@ router.get("/:username/challenges", async (req, res, next) => {
let username = req.params.username;
let challenges = pendingChallenges.get(username);
if (!challenges) challenges = [];
console.log(username,challenges);
res.json({ success: true, challenges: challenges });
});
+13 -3
View File
@@ -1,6 +1,7 @@
import { Button, Group, Stack, Text, Title } from '@mantine/core';
import React, { useEffect, useState } from 'react'
import { useNavigate } from 'react-router-dom';
import { getUserData } from '../../utils/auth'
const Challenges = () => {
const navigate = useNavigate();
@@ -8,14 +9,15 @@ const Challenges = () => {
{ challenger: 'moonpatel', roomID: 'sgnkjsdbnojsnvjsdnkl' },
{ challenger: 'user1', roomID: 'sgnkjsdbnojsnvjsdnkl' }
]
const [challenges, setChallenges] = useState(dummyChallenges);
const [challenges, setChallenges] = useState([]);
const { username } = getUserData()
useEffect(() => {
const abortController = new AbortController();
let response = null;
const fetchData = async () => {
let url = `${import.meta.env.VITE_BACKEND_HOST}/api/user/challenges`;
let url = `${import.meta.env.VITE_BACKEND_HOST}/api/user/${username}/challenges`;
try {
response = await fetch(url, { signal: abortController.signal })
const data = await response.json();
@@ -32,7 +34,15 @@ const Challenges = () => {
}
}
}
})
fetchData();
return () => {
if (!response) {
abortController.abort();
}
}
}, [])
if (!challenges || challenges.length === 0) {
return (