Skip to content

Commit 35d6eda

Browse files
author
Balashov Nikita
committed
URL (requests to server side)
1 parent 8a90d57 commit 35d6eda

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

phone-book/src/url/url.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,36 @@
11
class Url{
2-
constructor() {
3-
this.url = 'http://easycode-js.herokuapp.com/myba/users';
2+
constructor(accountName) {
3+
this.url = `https://easycode-js.herokuapp.com/${accountName}/users`;
44
}
55

66
getUsersFromServer() {
7-
return (async () => {
8-
const data = await fetch(this.url);
9-
return this.users = await data.json();
10-
})();
7+
return fetch(this.url)
118
}
129

13-
obtainUsers() {
14-
this.getUsersFromServer();
15-
return this.users;
10+
postUser(user) {
11+
fetch(this.url, {
12+
method: 'POST',
13+
headers: {
14+
'Content-type': 'application/json'
15+
},
16+
body: JSON.stringify(user)
17+
})
1618
}
1719

18-
postUser(user) {
19-
const xhr = new XMLHttpRequest();
20-
xhr.open('POST', this.url, true);
21-
xhr.setRequestHeader('Content-type', 'application/json');
22-
xhr.send(JSON.stringify(user));
20+
editUser(infoToEdit, id) {
21+
fetch(this.url + '/' + id, {
22+
method: 'PATCH',
23+
headers: {
24+
'Content-type': 'application/json'
25+
},
26+
body: JSON.stringify(infoToEdit)
27+
})
2328
}
2429

2530
deleteUserById(id) {
26-
const xhr = new XMLHttpRequest();
27-
xhr.open('DELETE', `${this.url}/${id}`, true);
28-
xhr.setRequestHeader('Content-type', 'application/json');
29-
xhr.send();
31+
fetch(this.url + '/' + id, {
32+
method: 'DELETE'
33+
})
3034
}
3135
}
3236

0 commit comments

Comments
 (0)