Skip to content

Commit 2e23cab

Browse files
authored
Add files via upload
1 parent 241022b commit 2e23cab

6 files changed

Lines changed: 272 additions & 0 deletions

File tree

prototype/chatroom/index.html

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,107 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
13

4+
<head>
5+
<meta charset="UTF-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<title>Ninja room</title>
9+
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
10+
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
11+
<link rel="stylesheet" href="style.css">
12+
</head>
13+
14+
<body>
15+
16+
<!-- container && title -->
17+
<div class="container my-4">
18+
<h1 class="my-4 text-center">chatroom Wild Code School</h1>
19+
20+
21+
22+
<!-- button -->
23+
<div class="chat-rooms mb-3 text-center">
24+
<div class="my-2 choose">choose a chatroom:</div>
25+
<button class="btn" id="general">#general</button>
26+
<button class="btn" id="html">#html/css</button>
27+
<button class="btn" id="javascript">#javascript</button>
28+
<button class="btn" id="framework">#framework</button>
29+
30+
</div>
31+
<!-- chat list -->
32+
<div class="chat-window">
33+
<ul class="chat-list list-group"></ul>
34+
</div>
35+
<!-- new chatroom -->
36+
<form class="new-chat my-3">
37+
<div class="input-group">
38+
<div class="input-group-prepend">
39+
<div class="input-group-text">tape your message:</div>
40+
</div>
41+
<input type="text" id="message" class="form-control" required>
42+
<div class="input-group-append">
43+
<input type="submit" value="send" class="btn">
44+
</div>
45+
</div>
46+
</form>
47+
<!-- update name -->
48+
<form class="new-name my-3">
49+
<div class="input-group">
50+
<div class="input-group-prepend">
51+
<div class="input-group-text">update your name</div>
52+
53+
</div>
54+
<input type="text" id="name" class="form-control" required>
55+
<div class="input-group-append">
56+
<input type="submit" value="update" class="btn">
57+
</div>
58+
59+
</div>
60+
<div class="update-mssg"></div>
61+
</form>
62+
</div>
63+
64+
<script src="https://www.gstatic.com/firebasejs/8.10.0/firebase-app.js"></script>
65+
<script src="https://www.gstatic.com/firebasejs/8.10.0/firebase-firestore.js"></script>
66+
<script>
67+
68+
// Import the functions you need from the SDKs you need
69+
70+
71+
// TODO: Add SDKs for Firebase products that you want to use
72+
73+
// https://firebase.google.com/docs/web/setup#available-libraries
74+
75+
76+
// Your web app's Firebase configuration
77+
78+
var config = {
79+
80+
apiKey: "AIzaSyD3_x4jiOAP_fllNZNufGFQmZqCNbzOKPI",
81+
82+
authDomain: "fir-1f6a3.firebaseapp.com",
83+
84+
projectId: "fir-1f6a3",
85+
86+
storageBucket: "fir-1f6a3.appspot.com",
87+
88+
messagingSenderId: "240332069056",
89+
90+
appId: "1:240332069056:web:7dacaae1d475880189f525"
91+
92+
};
93+
94+
95+
// Initialize Firebase
96+
97+
firebase.initializeApp(config);
98+
const db = firebase.firestore();
99+
</script>
100+
<script src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.30.1/date_fns.min.js"></script>
101+
<script src="scripts/chat.js"></script>
102+
<script src="scripts/ui.js"></script>
103+
<script src="scripts/app.js"></script>
104+
105+
</body>
106+
107+
</html>

prototype/chatroom/index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php header('Location: /index.html');

prototype/chatroom/scripts/app.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
3+
const chatList = document.querySelector('.chat-list')
4+
const newChatForm = document.querySelector('.new-chat')
5+
const newNameForm = document.querySelector('.new-name')
6+
const updateMssg = document.querySelector('.update-mssg')
7+
const rooms = document.querySelector('.chat-rooms')
8+
newChatForm.addEventListener('submit', e => {
9+
e.preventDefault()
10+
const message = newChatForm.message.value.trim()
11+
12+
chatroom.addChat(message)
13+
.then(() => newChatForm.reset())
14+
.catch(err => console.log(err))
15+
16+
})
17+
18+
19+
newNameForm.addEventListener('submit', e => {
20+
e.preventDefault()
21+
22+
const newName = newNameForm.name.value.trim()
23+
chatroom.updateName(newName)
24+
newNameForm.reset()
25+
updateMssg.innerText = `<div class='changename'>your nickname are ${newName}</div>`
26+
setTimeout(() => {
27+
updateMssg.innerText = ''
28+
}, 3000)
29+
30+
31+
})
32+
rooms.addEventListener('click', e => {
33+
if (e.target.tagName === 'BUTTON') {
34+
chatUI.clear()
35+
chatroom.updateRoom(e.target.getAttribute('id'))
36+
chatroom.getChats(chat => chatUI.render(chat))
37+
}
38+
})
39+
40+
const username = localStorage.username ? localStorage.username : 'anonymous'
41+
const chatUI = new ChatUI(chatList)
42+
const chatroom = new Chatroom('general', username)
43+
44+
chatroom.getChats((data) => {
45+
chatUI.render(data)
46+
})

prototype/chatroom/scripts/chat.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Chatroom {
2+
constructor(room, username) {
3+
this.room = room
4+
this.username = username
5+
this.chats = db.collection('chats')
6+
this.unsub
7+
return this
8+
9+
}
10+
11+
async addChat(message) {
12+
const now = new Date()
13+
14+
const chat = {
15+
message: message,
16+
username: this.username,
17+
room: this.room,
18+
created_at: firebase.firestore.Timestamp.fromDate(now)
19+
}
20+
const response = await this.chats.add(chat)
21+
return response
22+
}
23+
getChats(callback) {
24+
this.unsub = this.chats
25+
.where('room', '==', this.room)
26+
.orderBy('created_at')
27+
.onSnapshot(snapshot => {
28+
29+
snapshot.docChanges().forEach(change => {
30+
31+
if (change.type === 'added') {
32+
callback(change.doc.data())
33+
}
34+
35+
36+
});
37+
})
38+
}
39+
updateName(username) {
40+
this.username = username
41+
}
42+
updateRoom(room) {
43+
this.room = room
44+
if (this.unsub) {
45+
this.unsub()
46+
}
47+
}
48+
}
49+
50+
51+

prototype/chatroom/scripts/ui.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
3+
class ChatUI {
4+
constructor(list) {
5+
this.list = list
6+
}
7+
clear() {
8+
this.list.innerHTML = ''
9+
}
10+
render(data) {
11+
const when = dateFns.distanceInWordsToNow(
12+
data.created_at.toDate(),
13+
{ addSuffix: true }
14+
)
15+
const html = `
16+
<li class="list-group-item">
17+
<span class="username">${data.username}</span>
18+
<span class="message">${data.message}</span>
19+
<div class="time">${when}</div>
20+
21+
</li>
22+
`
23+
this.list.innerHTML += html
24+
};
25+
26+
}

prototype/chatroom/style.css

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
@import url('https://fonts.googleapis.com/css2?family=ABeeZee&family=Birthstone&family=Bonheur+Royale&family=Dongle:wght@700&family=Roboto:wght@100&family=Shizuru&display=swap');
2+
3+
body {
4+
background-color: firebrick;
5+
}
6+
h1 {
7+
font-family: Shizuru;
8+
color: cornsilk;
9+
}
10+
.container {
11+
max-width: 600px;
12+
}
13+
.update-mssg, .choose {
14+
color: azure;
15+
text-align: center;
16+
margin: 5px 0;
17+
}
18+
.btn {
19+
background-color: crimson;
20+
color: white;
21+
outline: none!;
22+
cursor: pointer;
23+
}
24+
25+
.btn:focus {
26+
outline:none!;
27+
color: darkred!;
28+
cursor: pointer;
29+
30+
}
31+
.username {
32+
font-weight: bold;
33+
}
34+
.time {
35+
font-weight: 0.7em;
36+
color: grey;
37+
}
38+
39+
.chat-list {
40+
height: 40vh;
41+
overflow: auto;
42+
}

0 commit comments

Comments
 (0)