From dac51ed4a47c0c4d74971816deac0b3f37c483a2 Mon Sep 17 00:00:00 2001 From: Ste Vaidis Date: Sat, 16 Sep 2023 15:15:48 +0300 Subject: [PATCH] more code --- src/App.js | 18 +++++++++---- src/view/Create.jsx | 2 +- src/view/Poll.jsx | 62 +++++++++++++++------------------------------ 3 files changed, 34 insertions(+), 48 deletions(-) diff --git a/src/App.js b/src/App.js index 99a27a2..b2de23b 100644 --- a/src/App.js +++ b/src/App.js @@ -29,15 +29,18 @@ function App() { console.log(' 🐞 APP onConnect') setIsConnected(true) } + function onDisconnect() { console.log(' 🐞 APP onDisconnect') setIsConnected(false) } + function onCreate(data) { console.log(' 🪲 APP onCreate data:', data) setAnon(undefined) navigate(`/poll/${data}`) } + function onExist(data) { console.log(' 🪲 APP onExist data: ', data) setExist(Boolean(data)) @@ -46,31 +49,36 @@ function App() { socket.emit('join', data); } } + function onJoin(data) { console.log(' 🪲 APP onJoin data: ', data) setUser(data.user) - if (data.code !== 404) { - localStorage.setItem(data.pid, data.token) - } - if (data.code === 204) { + if (data.code === 401) { navigate(`/poll/${data.pid}/join`) } + if (data.code === 200) { + localStorage.setItem(data.pid, data.token) + navigate(`/poll/${data.pid}`) + } } + function onPoll(data) { console.log(' 🪲 APP onPoll data: ', data) setPoll(data.poll) - setUser(data.user) } + function onInfo(data) { console.log(' 🪲 APP onInfo data: ', data) if (data.anonymous !== undefined) { setAnon(data.anonymous) } } + function onList(data) { console.log(' 🪲 APP onList data: ', data) setList(data) } + socket.on('connect', onConnect); socket.on('create', onCreate); socket.on('join', onJoin); diff --git a/src/view/Create.jsx b/src/view/Create.jsx index 0e9f93b..4e732f0 100644 --- a/src/view/Create.jsx +++ b/src/view/Create.jsx @@ -15,7 +15,7 @@ const Create = (props) => { const [formData, setFormData] = useState({ title: "", answers: ["", ""], - anonymous: false + anonymous: true }); useEffect(() => { diff --git a/src/view/Poll.jsx b/src/view/Poll.jsx index b73b9f6..6f31e75 100644 --- a/src/view/Poll.jsx +++ b/src/view/Poll.jsx @@ -40,6 +40,23 @@ function Poll(props) { return result; } + useEffect(() => { + if (!token) { + console.log('POLL 🎱 id:', id) + console.log('POLL 🎱 token:', Boolean(token)) + const data = {pid:id}; + console.log(' 💾 POLL emit.join.data:', data) + socket.emit('join', data); + } + if (token) { + console.log('POLL 🎱 id:', id) + console.log('POLL 🎱 token:', Boolean(token)) + const data = {pid:id, token: token}; + console.log(' 💾 POLL emit.join.data:', data) + socket.emit('join', data); + } + },[id, token]) + useEffect(() => { console.log('POLL 🎱 id:', id) console.log('POLL 🎱 exist:', exist) @@ -48,52 +65,13 @@ function Poll(props) { console.log('POLL 🎱 token:', Boolean(token)) console.log('POLL 🎱 poll:', poll) - if (!token) { - const data = {pid:id}; - console.log(' 💾 POLL emit.join.data:', data) - socket.emit('join', data); - } if (token) { - const data = {pid:id, token: token}; - console.log(' 💾 POLL emit.join.data:', data) - socket.emit('join', data); - } - if (token && user) { + console.log('POLL 🎱 id:', id) + console.log('POLL 🎱 token:', Boolean(token)) console.log('POLL emit.poll.token:', 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]) + },[token]) const onCopy = () => { console.log('onCopy')