Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import js from '@eslint/js';
import globals from 'globals';
import { defineConfig } from 'eslint/config';
import config from 'eslint-config-prettier';
import plugin from 'eslint-plugin-prettier/recommended';

export default defineConfig([
{
files: ['**/*.{js,mjs,cjs}'],
plugins: [config],
extends: [plugin],
plugins: { js },
extends: ['js/recommended'],
},
{ files: ['**/*.js'], languageOptions: { sourceType: 'commonjs' } },
{
files: ['**/*.{js,mjs,cjs}'],
languageOptions: { globals: globals.browser },
Expand Down
57 changes: 29 additions & 28 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<!doctype html>
<html>
<head>
<title>Проект "Комменты"</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="styles.css" />
</head>
<head>
<title>Проект "Комменты"</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="styles.css" />
</head>

<body>
<div class="container">
<ul class="comments">
<!--<li class="comment">
<body>
<div class="container">
<ul class="comments">
<p>Загрузка</p>
<!--<li class="comment">
<div class="comment-header">
<div>Глеб Фокин</div>
<div>12.02.22 12:18</div>
Expand Down Expand Up @@ -43,25 +44,25 @@
</div>
</div>
</li>-->
</ul>
<div class="add-form">
<input
type="text"
class="add-form-name"
placeholder="Введите ваше имя"
/>
<textarea
type="textarea"
class="add-form-text"
placeholder="Введите ваш коментарий"
rows="4"
></textarea>
<div class="add-form-row">
<button class="add-form-button">Написать</button>
</ul>
<div class="add-form">
<input
type="text"
class="add-form-name"
placeholder="Введите ваше имя"
/>
<textarea
type="textarea"
class="add-form-text"
placeholder="Введите ваш коментарий"
rows="4"
></textarea>
<div class="add-form-row">
<button class="add-form-button">Написать</button>
</div>
</div>
</div>
</div>
</div>
</body>
</body>

<script type="module" src="./index.js"></script>
<script type="module" src="./index.js"></script>
</html>
13 changes: 2 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import { updateComments } from './modules/comments.js';
import { renderComments } from './modules/render.js';
import { fetchGetComments } from './modules/fetchGetComments.js';
import { addClickBtnAddComment } from './modules/eventsBtnAddComment.js';

const API = 'https://wedev-api.sky.pro/api/v1/arusskov/comments';

fetch(API, { method: 'GET' })
.then((response) => {
return response.json();
})
.then((data) => {
updateComments(data.comments);
renderComments();
});

fetchGetComments(API);
addClickBtnAddComment(API);
28 changes: 16 additions & 12 deletions modules/eventsBtnAddComment.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { renderComments } from './render.js';
import { comments, updateComments } from './comments.js';
import { replaceSymbol } from './replace.js';
import { fetchGetComments } from './fetchGetComments.js';

let btnWrite = document.querySelector('.add-form-button');
let inputName = document.querySelector('.add-form-name');
let inputText = document.querySelector('.add-form-text');
const formAddComment = document.querySelector('.add-form');
const container = document.querySelector('.container');

function removeClassError(input) {
input.classList.remove('input-error');
Expand Down Expand Up @@ -39,21 +40,24 @@ export function addClickBtnAddComment(API) {
name: replaceSymbol(inputName.value),
};

inputName.value = '';
inputText.value = '';
formAddComment.style.display = 'none';

const loaderText = document.createElement('p');
loaderText.textContent = 'Комментарий добавляется';
container.appendChild(loaderText);

fetch(API, {
method: 'POST',
body: JSON.stringify(newComment),
});

fetch(API, { method: 'GET' })
.then((response) => {
return response.json();
})
.then(() => {
return fetchGetComments(API);
})
.then((data) => {
updateComments(data.comments);
renderComments();
.then(() => {
inputName.value = '';
inputText.value = '';
formAddComment.style.display = 'flex';
container.removeChild(loaderText);
});
});
}
24 changes: 18 additions & 6 deletions modules/eventsComment.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
import { renderComments } from './render.js';
import { comments } from './comments.js';

function delay(interval = 300) {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, interval);
});
}

export function addClickBtnLike() {
const arrayBtnLike = document.querySelectorAll('.like-button');

for (const btnLike of arrayBtnLike) {
btnLike.addEventListener('click', (event) => {
event.stopPropagation();

comments[btnLike.dataset.index].isLiked
? comments[btnLike.dataset.index].likes--
: comments[btnLike.dataset.index].likes++;
btnLike.classList.add('loading-like');
console.log(btnLike.classList);

comments[btnLike.dataset.index].isLiked =
!comments[btnLike.dataset.index].isLiked;
const index = btnLike.dataset.index;

renderComments();
delay(2000).then(() => {
comments[index].isLiked
? comments[index].likes--
: comments[index].likes++;
comments[index].isLiked = !comments[index].isLiked;
renderComments();
});
});
}
}
Expand Down
13 changes: 13 additions & 0 deletions modules/fetchGetComments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { renderComments } from './render.js';
import { updateComments } from './comments.js';

export function fetchGetComments(API) {
return fetch(API, { method: 'GET' })
.then((response) => {
return response.json();
})
.then((data) => {
updateComments(data.comments);
renderComments();
});
}
Loading