more code

This commit is contained in:
Ste Vaidis 2023-09-15 23:59:13 +03:00
parent 6d12abab13
commit 2560086787
5 changed files with 55 additions and 30 deletions

2
.env
View File

@ -1 +1 @@
WDS_SOCKET_PORT=443 WDS_SOCKET_PORT=4000

View File

@ -49,8 +49,12 @@ function App() {
function onJoin(data) { function onJoin(data) {
console.log(' 🪲 APP onJoin data: ', data) console.log(' 🪲 APP onJoin data: ', data)
setUser(data.user) setUser(data.user)
localStorage.setItem(data.pid, data.token) if (data.code !== 404) {
navigate(`/poll/${data.pid}`) localStorage.setItem(data.pid, data.token)
}
if (data.code === 204) {
navigate(`/poll/${data.pid}/join`)
}
} }
function onPoll(data) { function onPoll(data) {
console.log(' 🪲 APP onPoll data: ', data) console.log(' 🪲 APP onPoll data: ', data)

View File

@ -1,12 +1,12 @@
import { io } from 'socket.io-client'; import { io } from 'socket.io-client';
const options = { const options = {
rejectUnauthorized: false, // allow self-signed certs rejectUnauthorized: false,
requestCert: false, requestCert: false,
secure: false secure: false
}; };
// const URL = 'http://localhost:4000'; const URL = 'http://localhost:4000';
const URL = 'https://pollback.vaidis.eu'; // const URL = 'https://pollback.vaidis.eu';
export const socket = io(URL, options); export const socket = io(URL, options);

View File

@ -2,12 +2,13 @@ import { useState, useEffect, useRef, useContext } from "react";
import classNames from "classnames"; import classNames from "classnames";
const Answers = (props) => { const Answers = (props) => {
const { poll, user, socket, id, pid } = props; const { poll, user, socket, id, pid, token } = props;
const [winner, setWinner] = useState(0); const [winner, setWinner] = useState(0);
const token = localStorage.getItem(id) // const token = localStorage.getItem(id)
const onVote = async (answer) => { const onVote = async (answer) => {
const data = { user:user, answer:answer, pid:pid, token:token } const data = { user:user, answer:answer, pid:pid, token:token }
console.log('VOTE data: ', data)
socket.emit("vote", data); socket.emit("vote", data);
}; };

View File

@ -41,38 +41,58 @@ function Poll(props) {
} }
useEffect(() => { useEffect(() => {
console.log('POLL 🎱 props:', props)
console.log('POLL 🎱 id:', id) console.log('POLL 🎱 id:', id)
console.log('POLL 🎱 exist:', exist) console.log('POLL 🎱 exist:', exist)
console.log('POLL 🎱 anon:', anon) console.log('POLL 🎱 anon:', anon)
console.log('POLL 🎱 user:', user)
console.log('POLL 🎱 token:', Boolean(token)) console.log('POLL 🎱 token:', Boolean(token))
console.log('POLL 🎱 poll:', poll)
if (exist === undefined) { if (!token) {
console.log('POLL emit.exist.id:', id) const data = {pid:id};
socket.emit('exist', id) console.log(' 💾 POLL emit.join.data:', data)
}
if (exist && anon === undefined) {
console.log('POLL emit.info.id:', id)
socket.emit('info', id)
}
if (exist && anon !== undefined && anon && !token) {
const data = {pid:id, user:randomUser(24)};
console.log('POLL emit.join.data:', data)
socket.emit('join', data); socket.emit('join', data);
} }
if (exist && anon !== undefined && !anon && !token) { if (token) {
console.log('POLL redirect anon:', JSON.stringify(anon)) const data = {pid:id, token: token};
return navigate(`/poll/${id}/join`) console.log(' 💾 POLL emit.join.data:', data)
}
if (exist && anon !== undefined && anon && token) {
const data = {pid:id, user:user};
console.log('POLL emit.join.data:', data)
socket.emit('join', data); socket.emit('join', data);
} }
if (exist && token) { if (token && user) {
console.log('POLL emit.poll.token:', token) console.log('POLL emit.poll.token:', token)
socket.emit('poll', token); socket.emit('poll', token);
} }
// if (exist === undefined) {
// console.log('POLL emit.exist.id:', id)
// socket.emit('exist', id)
// }
// if (exist && anon === undefined) {
// console.log('POLL emit.info.id:', id)
// socket.emit('info', id)
// }
// if (exist && anon !== undefined && anon && !token) {
// const data = {pid:id, user:randomUser(24)};
// console.log(' 💾 POLL emit.join.data:', data)
// socket.emit('join', data);
// }
// if (exist && anon !== undefined && !anon && !token) {
// console.log('POLL redirect anon:', JSON.stringify(anon))
// return navigate(`/poll/${id}/join`)
// }
// if (exist && anon !== undefined && anon && !token) {
// const data = {pid:id, user:randomUser(24)};
// console.log(' 💾 POLL emit.join.data:', data)
// socket.emit('join', data);
// }
// if (exist && anon !== undefined && anon && token) {
// const data = {pid:id, user:user};
// console.log(' 💾 💾 💾 POLL emit.join.data:', data)
// socket.emit('join', data);
// }
// if (exist && token) {
// console.log('POLL emit.poll.token:', token)
// socket.emit('poll', token);
// }
},[id, exist, anon, token]) },[id, exist, anon, token])
const onCopy = () => { const onCopy = () => {
@ -86,8 +106,8 @@ function Poll(props) {
{poll && {poll &&
<div className="mb-8"> <div className="mb-8">
<Title variant={1} label={poll.title} /> <Title variant={1} label={poll.title} />
<p className="text-white mt-6 text-sm">Hello <strong>{poll.anonymous ? 'Anonymous' : user}</strong>, please choose the answer you like</p> <p className="text-white mt-6 text-sm">Hello <strong>{poll.anonymous ? 'Anonymous' : user}{user}</strong>, please choose the answer you like</p>
<Answers poll={poll} id={id} user={user} socket={socket} /> <Answers poll={poll} pid={id} user={user} socket={socket} token={token}/>
</div> </div>
} }
{/* SHARE */} {/* SHARE */}