-
Notifications
You must be signed in to change notification settings - Fork 0
/
JoinChatRoom.js
75 lines (70 loc) · 2.3 KB
/
JoinChatRoom.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import '../assets/JoinChatRoom.css';
import { useRef } from 'react';
import { useState } from 'react';
import mockBackEnd from '../assets/mockBackEndRoomList.json'
function JoinChatRoom() {
//take record of the state of the meeting
const state = useRef(0);
//take record of the meetingID
const meetingID = useRef('');
//registration, set username
const [name, setName] = useState('');
//checking errors
const [submitted, setSubmitted] = useState(false);
const [error, setError] = useState(false);
function registerNewRoomButton(){
const userInput = document.getElementById('register').value;
if (localStorage.getItem(userInput)) {
state.current=0;
} else {
console.log('newMeeting');
localStorage.setItem(userInput, JSON.stringify([0, 0]));
state.current=1;
meetingID.current = userInput;
}
}
function JoinMeetingButton(){
const userInput = document.getElementById('register').value;
if (localStorage.getItem(userInput)) {
meetingID.current = userInput;
localStorage.setItem(userInput, JSON.stringify([0, 0]));
state.current=1;
} else {
state.current=0;
}
}
//not log-in or reigstered yet
if (state.current == 0){
return(
<div className="register">
<input type="text" placeholder="Please register a meeting" id="register" />
<button type="submit" onClick={registerNewRoomButton}>Register</button>
<br />
<input type="text" placeholder="Please join yout meeting" id="login" />
<button type="submit" onClick={JoinMeetingButton}>Login</button>
<br />
</div>
);
}
function chatroom(){
state.current = 2;
}
function logout(){
state.current = 0;
const bestScore = (JSON.parse(localStorage.getItem(meetingID.current)))[1];
localStorage.setItem(meetingID.current, JSON.stringify([0, bestScore]));
}
//has finished log-in, Jump to a new page ChatRoom()
if (state == 1){
return(
<div className="ChatRoom">
<input type="text" placeholder="In Chat Room" id="Chat Room ID" />
<button type="submit" onClick={chatroom}>Start the room</button>
</div>
);
}
function homePage(){
state.current = 1;
}
}
export default App;