-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetUsers.js
More file actions
121 lines (106 loc) · 3.71 KB
/
getUsers.js
File metadata and controls
121 lines (106 loc) · 3.71 KB
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
import updateHistory from './updateHistory.js';
import showAvatar from './showAvatar.js';
import {sortWeightHistory, sortNames} from './sortData.js';
let apiUrl = 'https://myrestapi01.herokuapp.com/users';
// get form id
let form = document.getElementById('newUser');
let updateForm = document.getElementById('updateUser');
let message = document.getElementById('message');
let page1 = document.getElementById('page-1');
let page2 = document.getElementById('page-2');
let utterance = new SpeechSynthesisUtterance();
utterance.rate = 0.7;
// get all users to show in users dropdown
let getUsers = () => {
axios.get(apiUrl)
.then(res => {
let x = document.getElementById("namesList");
sortNames(res.data);
res.data.forEach((data) => {
let option = document.createElement("option");
option.text = data.name;
option.className = "dropdown-item";
x.add(option);
});
page2.classList.add('d-none');
page1.classList.remove('d-none');
})
.catch(err => console.error(err))
}
// add new user method
let addUser = (event) => {
event.preventDefault();
let userInput = {
name: form.name.value,
age: form.age.value,
weightHistory: []
}
axios.post(apiUrl, userInput)
.then((res) => {
user = JSON.parse(JSON.stringify(res.data));
page1.classList.add('d-none');
page2.classList.remove('d-none');
showAvatar();
updateHistory(user);
})
.catch((err) => console.error(err));
}
// call addUser function upon form submit
form.addEventListener('submit', addUser);
var user = {};
// show weight history, weight chart for an existing user
document.getElementById('ok').addEventListener('click', () => {
// document.getElementById('newUser').style.visibility = "hidden";
page1.classList.add('d-none');
page2.classList.remove('d-none');
let input = document.getElementById('namesList').value;
axios.get(apiUrl)
.then(res => {
console.log(res)
let found = res.data.filter((user) => {
return user.name === input
});
user = found[0];
console.log(user);
showAvatar();
updateHistory(user);
})
.catch(err => console.error(err))
});
// add new weight to the history
document.getElementById('input-weight').addEventListener('click', (e) => {
e.preventDefault();
if (updateForm.weight.value && updateForm.inputDate.value) {
let weightLog = {
weight: parseInt(updateForm.weight.value),
date: updateForm.inputDate.value,
}
if (user.weightHistory.find(({ date }) => date === weightLog.date)) {
utterance.text = 'Entry is already created for date ' + weightLog.date;
speechSynthesis.speak(utterance);
message.style.color= 'red';
message.textContent = 'Entry is already created for date ' + weightLog.date;
} else {
user.weightHistory.push(weightLog);
sortWeightHistory(user);
message.style.color= 'green';
message.textContent = "Weight Logged Successfully";
}
updateForm.reset();
} else {
message.style.color= 'red';
message.textContent = "Enter valid values";
}
axios.patch(`https://myrestapi01.herokuapp.com/users/${user.id}`, user)
.then((user) => {
updateHistory(user.data);
})
.catch((err) => console.error(err));
setTimeout(() =>{
message.innerHTML = '';
}, 7000);
});
export {
getUsers,
user
};