|
1 | 1 | "use client";
|
2 |
| -import React from "react"; |
| 2 | +import React, { useEffect, useState } from "react"; |
3 | 3 | import { useRouter } from "next/navigation";
|
4 | 4 | import PeerprepButton from "../shared/PeerprepButton";
|
5 | 5 | import { useQuestionFilter } from "@/contexts/QuestionFilterContext";
|
| 6 | +const QUERY_INTERVAL_MILLISECONDS = 3000; |
| 7 | +const usePeriodicCallback = ( |
| 8 | + callback: () => void, |
| 9 | + intervalTime: number, |
| 10 | + isActive: boolean |
| 11 | +) => { |
| 12 | + useEffect(() => { |
| 13 | + if (!isActive) return; |
| 14 | + |
| 15 | + const interval = setInterval(callback, intervalTime); |
| 16 | + |
| 17 | + return () => clearInterval(interval); |
| 18 | + }, [callback, intervalTime, isActive]); |
| 19 | +}; |
6 | 20 |
|
7 | 21 | const Matchmaking = () => {
|
8 | 22 | const router = useRouter();
|
| 23 | + const [isMatching, setIsMatching] = useState(false); |
9 | 24 | const { difficulty, topics } = useQuestionFilter();
|
10 | 25 |
|
11 | 26 | const handleMatch = () => {
|
12 |
| - console.log("Match attempted"); |
13 |
| - console.log("Selected Difficulty:", difficulty); |
14 |
| - console.log("Selected Topics:", topics); |
| 27 | + if (!isMatching) { |
| 28 | + setIsMatching(true); |
| 29 | + console.log("Match attempted"); |
| 30 | + console.log("Selected Difficulty:", difficulty); |
| 31 | + console.log("Selected Topics:", topics); |
| 32 | + } else { |
| 33 | + setIsMatching(false); |
| 34 | + console.debug("User stopped matching"); |
| 35 | + } |
| 36 | + |
15 | 37 | // username as userid?
|
16 | 38 | // should probably just use the questionlist selections as params
|
17 | 39 | };
|
18 | 40 |
|
| 41 | + const queryResource = () => { |
| 42 | + console.debug("Querying resource blob for matchmaking status"); |
| 43 | + }; |
| 44 | + |
| 45 | + usePeriodicCallback(queryResource, QUERY_INTERVAL_MILLISECONDS, isMatching); |
| 46 | + |
19 | 47 | return (
|
20 | 48 | // TODO: move this to some admin panel or something
|
21 | 49 | <div className="p-4 space-x-4">
|
22 | 50 | <PeerprepButton onClick={() => router.push(`questions/new`)}>
|
23 | 51 | Add Question
|
24 | 52 | </PeerprepButton>
|
25 |
| - <PeerprepButton onClick={handleMatch}>Find Match</PeerprepButton> |
| 53 | + <PeerprepButton onClick={handleMatch}> |
| 54 | + {isMatching ? "Cancel Match" : "Find Match"} |
| 55 | + </PeerprepButton> |
26 | 56 | </div>
|
27 | 57 | );
|
28 | 58 | };
|
|
0 commit comments