forked from makersacademy/frontend-api-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chitterView.js
132 lines (104 loc) · 3.66 KB
/
chitterView.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
class ChitterView {
constructor(model, api) {
this.model = model;
this.api = api;
this.peepList = document.querySelector("#peep-list");
this.successAlert = document.querySelector("#success-alert");
this.failAlert = document.querySelector("#fail-alert");
this.createAccountBtn = document.querySelector("#user-create-btn");
this.createHandleInput = document.querySelector("#create-account-handle");
this.createPasswordInput = document.querySelector(
"#create-account-password"
);
this.createAccountSubmit = document.querySelector("#create-account-submit");
this.createAccountSubmit.addEventListener("click", () => {
this.createAccount(
this.createHandleInput.value,
this.createPasswordInput.value
);
});
this.loginBtn = document.querySelector("#user-login-btn");
this.loginHandleInput = document.querySelector("#login-handle");
this.loginPasswordInput = document.querySelector("#login-password");
this.loginSubmit = document.querySelector("#login-submit");
this.loginSubmit.addEventListener("click", () => {
this.signIn(this.loginHandleInput.value, this.loginPasswordInput.value);
});
this.peepInput = document.querySelector("#peep-input");
this.peepSubmit = document.querySelector("#peep-submit");
this.peepSubmit.addEventListener("click", () => {
this.newPeep(
this.model.getSession().session_key,
this.model.getSession().user_id,
this.peepInput.value
);
});
}
async displayPeepsFromApi() {
const peeps = await this.api.fetchPeeps();
this.model.setPeeps(peeps);
this.displayPeeps();
}
displayPeeps() {
this.clearPeepDivs();
let peeps = this.model.getPeeps();
peeps.forEach((peep) => {
this.addPeepLi(peep);
});
}
clearPeepDivs() {
const peeps = document.querySelectorAll("li.peep");
peeps.forEach((peep) => {
peep.remove();
});
}
async createAccount(handle, password) {
if (handle && password) {
const user = await this.api.createUser(handle, password);
// this.showAndHide("#success-alert");
// this.successAlert.innerText = `Thanks ${user.handle} your account has been created`;
this.createHandleInput.value = null;
this.createPasswordInput.value = null;
this.signIn(handle, password);
}
}
async signIn(handle, password) {
if (handle && password) {
const response = await this.api.logInUser(handle, password);
this.model.saveSession(response);
this.showAndHide("#success-alert");
this.successAlert.innerText = `Thanks ${response.session_key} you have successfully logged in`;
this.loginHandleInput.value = null;
this.loginPasswordInput.value = null;
this.showAndHide("#user-container");
this.showAndHide("#peep-container");
}
}
async newPeep(session_key, user_id, body) {
const peep = await this.api.createPeep(session_key, user_id, body);
this.model.addNewPeep(peep);
this.displayPeeps();
this.peepInput.value = null;
}
addPeepLi(peep) {
const time = this.formatTime(peep);
this.peepList.innerHTML += `<li class="list-group-item peep">
<h5 class="fw-bold">${peep.user.handle}</h5>
<p class="text-muted mb-2 fw-bold">${time}</p>
<p class="text-muted mb-0 peep">${peep.body}</p>
</li>`;
}
formatTime(peep) {
let time = new Date(peep.created_at);
return time.toDateString();
}
showAndHide(element) {
var x = document.querySelector(element);
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
}
module.exports = ChitterView;