session link with mobile voting
This commit is contained in:
@@ -1,8 +1,14 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { Nunito } from "next/font/google";
|
import { Nunito } from "next/font/google";
|
||||||
import { useState } from "react";
|
import { useState, useEffect } from "react";
|
||||||
|
import { useRouter } from "next/navigation";
|
||||||
|
import { useAuth } from "react-oidc-context";
|
||||||
|
import { getUser } from "muzak/data/fetcher";
|
||||||
import Image from "next/image";
|
import Image from "next/image";
|
||||||
|
import getSessionId from "muzak/data/session";
|
||||||
|
import { useSession } from "muzak/contexts/SessionContext";
|
||||||
|
import Router from "next/router";
|
||||||
|
|
||||||
const nunito = Nunito({
|
const nunito = Nunito({
|
||||||
weight: "900",
|
weight: "900",
|
||||||
@@ -13,6 +19,82 @@ export default function MobileVoting() {
|
|||||||
const [vote, setVote] = useState(0.5);
|
const [vote, setVote] = useState(0.5);
|
||||||
const [noHighlight, setNoHighlight] = useState(false);
|
const [noHighlight, setNoHighlight] = useState(false);
|
||||||
const [yesHighlight, setYesHighlight] = useState(false);
|
const [yesHighlight, setYesHighlight] = useState(false);
|
||||||
|
const auth = useAuth();
|
||||||
|
const router = useRouter();
|
||||||
|
const { sessionId, setSessionId } = useSession();
|
||||||
|
const [sessionSocket, setSessionSocket] = useState<WebSocket | null>(null);
|
||||||
|
|
||||||
|
async function waitForOpenSocket() {
|
||||||
|
await new Promise<void>((resolve) => {
|
||||||
|
const interval = setInterval(() => {
|
||||||
|
console.log("Checking socket state...");
|
||||||
|
if (sessionSocket?.readyState === WebSocket.OPEN) {
|
||||||
|
clearInterval(interval);
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
}, 1000);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const user = getUser();
|
||||||
|
|
||||||
|
async function connect() {
|
||||||
|
console.log(`ik ben ${user.profile.nickname}`);
|
||||||
|
console.log(sessionSocket);
|
||||||
|
console.log(sessionSocket?.readyState);
|
||||||
|
await waitForOpenSocket();
|
||||||
|
console.log("socket is open");
|
||||||
|
sessionSocket.send(
|
||||||
|
JSON.stringify({
|
||||||
|
action: "login",
|
||||||
|
userr: user.profile.nickname,
|
||||||
|
token: user.access_token,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize WebSocket connection when sessionId changes
|
||||||
|
useEffect(() => {
|
||||||
|
if (sessionId) {
|
||||||
|
console.log("Socket session ID:", sessionId);
|
||||||
|
const socket = new WebSocket(
|
||||||
|
`${process.env.NEXT_PUBLIC_WS_HOST}/voting/session/${sessionId}/`,
|
||||||
|
);
|
||||||
|
setSessionSocket(socket);
|
||||||
|
//setTimeout(() => connect(), 2000);
|
||||||
|
}
|
||||||
|
}, [sessionId]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (sessionSocket) {
|
||||||
|
connect();
|
||||||
|
}
|
||||||
|
}, [sessionSocket]);
|
||||||
|
|
||||||
|
async function setSession() {
|
||||||
|
const id = await getSessionId();
|
||||||
|
setSessionId(id);
|
||||||
|
console.log("Session ID:", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!auth.isLoading && !auth.error && auth.isAuthenticated) {
|
||||||
|
setSession();
|
||||||
|
} else if (!auth.isLoading && !auth.isAuthenticated) {
|
||||||
|
router.push("/");
|
||||||
|
}
|
||||||
|
}, [auth]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (sessionSocket?.readyState == WebSocket.OPEN) {
|
||||||
|
sessionSocket.send(
|
||||||
|
JSON.stringify({
|
||||||
|
action: "vote",
|
||||||
|
token: user.access_token,
|
||||||
|
points: vote,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}, [vote]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main className="m-3 h-screen rounded bg-blue-950/80 p-8 sm:p-14 md:p-20">
|
<main className="m-3 h-screen rounded bg-blue-950/80 p-8 sm:p-14 md:p-20">
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import { useRouter, usePathname } from "next/navigation";
|
|||||||
import { api, Session } from "muzak/data/fetcher";
|
import { api, Session } from "muzak/data/fetcher";
|
||||||
import { getUser } from "muzak/data/fetcher";
|
import { getUser } from "muzak/data/fetcher";
|
||||||
import { useSession } from "muzak/contexts/SessionContext";
|
import { useSession } from "muzak/contexts/SessionContext";
|
||||||
|
import getSessionId from "muzak/data/session";
|
||||||
|
|
||||||
export default function SessionChecker() {
|
export default function SessionChecker() {
|
||||||
const auth = useAuth();
|
const auth = useAuth();
|
||||||
@@ -15,30 +16,31 @@ export default function SessionChecker() {
|
|||||||
const [link, setLink] = useState("/lobby");
|
const [link, setLink] = useState("/lobby");
|
||||||
const { sessionId, setSessionId } = useSession();
|
const { sessionId, setSessionId } = useSession();
|
||||||
|
|
||||||
async function getSessions() {
|
// async function getSessions() {
|
||||||
if (!auth.isLoading && !auth.error && auth.isAuthenticated) {
|
// if (!auth.isLoading && !auth.error && auth.isAuthenticated) {
|
||||||
const { data: sessions } = await api?.sessions?.list({});
|
// const { data: sessions } = await api?.sessions?.list({});
|
||||||
console.log(sessions);
|
// console.log(sessions);
|
||||||
if (sessions?.length > 0) {
|
// if (sessions?.length > 0) {
|
||||||
setIsSession(true);
|
// setIsSession(true);
|
||||||
const user = getUser();
|
// const user = getUser();
|
||||||
setSessionId(sessions[0].session_id);
|
// setSessionId(sessions[0].session_id);
|
||||||
if (sessions[0].host === user?.profile?.email) {
|
// if (sessions[0].host === user?.profile?.email) {
|
||||||
setLink("/lobby");
|
// setLink("/lobby");
|
||||||
} else {
|
// } else {
|
||||||
setLink("/session");
|
// setLink("/session");
|
||||||
}
|
// }
|
||||||
} else {
|
// } else {
|
||||||
setIsSession(false);
|
// setIsSession(false);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
getSessions();
|
getSessionId();
|
||||||
|
console.log("hheufheufh");
|
||||||
}, [auth]);
|
}, [auth]);
|
||||||
|
|
||||||
if (isSession) {
|
if (sessionId) {
|
||||||
return (
|
return (
|
||||||
<Link
|
<Link
|
||||||
href={link}
|
href={link}
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
"use client";
|
||||||
|
import { useAuth } from "react-oidc-context";
|
||||||
|
import { api, getUser, Session } from "muzak/data/fetcher";
|
||||||
|
import { useSession } from "muzak/contexts/SessionContext";
|
||||||
|
|
||||||
|
export default async function getSessionId() {
|
||||||
|
//const auth = useAuth();
|
||||||
|
//const { sessionId, setSessionId } = useSession();
|
||||||
|
|
||||||
|
//if (!auth.isLoading && !auth.error && auth.isAuthenticated) {
|
||||||
|
const { data: sessions } = await api?.sessions?.list({});
|
||||||
|
if (sessions?.length > 0) {
|
||||||
|
return sessions[0].session_id;
|
||||||
|
}
|
||||||
|
//}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user