Skip to content

Commit

Permalink
corrected main.js (new render), api.js(authorization),
Browse files Browse the repository at this point in the history
  • Loading branch information
anguseva25 committed Apr 7, 2024
1 parent 85dad85 commit ed4be7e
Show file tree
Hide file tree
Showing 4 changed files with 448 additions and 290 deletions.
45 changes: 41 additions & 4 deletions api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
export let name = ''
export let token = ''
const host = 'https://wedev-api.sky.pro/api/user/login'

export function getComments() {
return fetch("https://wedev-api.sky.pro/api/v1/anka-anny/comments", {
return fetch("https://wedev-api.sky.pro/api/v2/anka-anny/comments", {
method: "GET",
})
.then((response) => {
Expand All @@ -11,13 +15,46 @@ export function getComments() {
});
}

export function getAuthorization(login, password) {
return fetch(host, {
method: "POST",
body: JSON.stringify({
login: login,
password: password,
}),
})
.then((response) => {
if (response.status === 400)
// throw new Error("слишком короткий запрос. Укажи больше 3 символов");
return response.json();
if (response.status === 500)
// throw new Error("ошибка на сервере");
return response.json();
if (response.status === 201)
return response.json();
})
.then((data) => {
if (data.error)
return alert(data.error)

name = data.user.name
token = `Bearer ${data.user.token}`
return "Ok"
})
}



export function postComment( { text, nameText }) {
return fetch("https://wedev-api.sky.pro/api/v1/anka-anny/comments", {
return fetch("https://wedev-api.sky.pro/api/v2/anka-anny/comments", {
method: "POST",
headers: {
Authorization: token,
},
body: JSON.stringify({
text: text.trim().safeCode(),
name: nameText.trim().safeCode(),
forceError: true,
//name: nameText.trim().safeCode(),
//forceError: true,
}),
})
.then((response) => {
Expand Down
27 changes: 2 additions & 25 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,8 @@
<div class="preloader hide" id="preloader">
<span class="loader"></span>
</div>
<ul id="list-comments" class="comments">
<!-- Список рендерится из JS -->
</ul>
<div class="preloaderNew hide" id="preloaderNew">
<span class="loaderNew"></span>
</div>
<div class="add-form">
<input
type="text"
id="name-input"
class="add-form-name"
placeholder="Введите ваше имя"
value=""
/>
<textarea
type="textarea"
id="text-input"
class="add-form-text"
placeholder="Введите ваш коментарий"
value=""
rows="4"
></textarea>
<div class="add-form-row">
<button id="add-button" class="add-form-button">Написать</button>
</div>
<div class="main">
<!-- Содержимое рендерится из JS -->
</div>
</div>
</body>
Expand Down
Loading

0 comments on commit ed4be7e

Please sign in to comment.