Skip to content

Commit 489a12e

Browse files
authored
Merge pull request #14 from natalya87324/criteria-check-and-tests
исправляет ошибки
2 parents 6295b75 + cdef90c commit 489a12e

File tree

9 files changed

+128
-128
lines changed

9 files changed

+128
-128
lines changed

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,4 +257,4 @@ <h2 class="data-error__title">Не удалось загрузить данны
257257
<script src="./js/main.js" type="module"></script>
258258
</body>
259259

260-
</html>
260+
</html>

js/api.js

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,36 @@
1-
import { initFilters } from './photos-filters.js';
2-
31
const BASE_URL = 'https://31.javascript.htmlacademy.pro/kekstagram';
42

3+
const STATUS_CODE_SUCCESS = 200;
4+
55
const Route = {
66
GET_DATA: '/data',
77
SEND_DATA: '/',
88
};
99

10-
const DELAY_TIME = 5000;
11-
12-
const dataErrorTemplate = document.querySelector('#data-error').content;
13-
14-
const showErrorMessage = () => {
15-
const errorElement = dataErrorTemplate.cloneNode(true);
16-
const errorMessage = errorElement.querySelector('.data-error');
17-
document.body.appendChild(errorMessage);
18-
setTimeout(() => {
19-
document.body.removeChild(errorMessage);
20-
}, DELAY_TIME);
21-
return errorMessage;
10+
const Method = {
11+
GET: 'GET',
12+
POST: 'POST',
2213
};
2314

15+
const ErrorText = {
16+
GET_DATA: 'Не удалось загрузить данные. Попробуйте обновить страницу',
17+
SEND_DATA: 'Не удалось отправить форму. Попробуйте ещё раз',
18+
};
2419

25-
const getData = (onSuccess) => {
26-
fetch(`${BASE_URL}${Route.GET_DATA}`)
20+
const load = (route, errorText, method = Method.GET, body = null) =>
21+
fetch(`${BASE_URL}${route}`, { method, body })
2722
.then((response) => {
28-
if (!response.ok) {
29-
throw new Error(showErrorMessage);
23+
if (response.status !== STATUS_CODE_SUCCESS) {
24+
throw new Error();
3025
}
3126
return response.json();
3227
})
33-
.then((photos) => {
34-
onSuccess(photos);
35-
initFilters(photos);
36-
})
37-
.catch((error) => showErrorMessage(error));
38-
};
39-
40-
const sendData = (onSuccess, onFail, body) => {
41-
fetch(`${BASE_URL}${Route.SEND_DATA}`,
42-
{
43-
method: 'POST',
44-
body,
45-
},
46-
)
47-
.then(() => {
48-
onSuccess();
49-
})
5028
.catch(() => {
51-
onFail();
29+
throw new Error(errorText);
5230
});
53-
};
5431

55-
export { getData, sendData, showErrorMessage };
32+
const getData = () => load(Route.GET_DATA, ErrorText.GET_DATA);
33+
34+
const sendData = (body) => load(Route.SEND_DATA, ErrorText.SEND_DATA, Method.POST, body);
35+
36+
export { getData, sendData };

js/full-size-photo.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import { toggleClass, isEscapeKey } from './util.js';
22

3+
const COMMENT_STEP = 5;
4+
35
const fullSizePhotoContainer = document.querySelector('.big-picture');
46
const fullSizePhoto = fullSizePhotoContainer.querySelector('.big-picture__img img');
57
const likesCount = document.querySelector('.likes-count');
68
const commentCount = document.querySelector('.social__comment-count');
79
const userCommentsContainer = document.querySelector('.social__comments');
810
const userComment = userCommentsContainer.querySelector('.social__comment');
9-
const commentsLoaderBtn = document.querySelector('.comments-loader');
11+
const commentsLoaderButton = document.querySelector('.comments-loader');
1012
const caption = document.querySelector('.social__caption');
11-
const closeBtn = document.querySelector('#picture-cancel');
12-
13-
const COMMENT_STEP = 5;
13+
const closeButton = document.querySelector('.big-picture__cancel');
1414

1515
const commentFragment = document.createDocumentFragment();
1616

@@ -53,15 +53,15 @@ const renderComments = () => {
5353
}
5454

5555
if (currentComments.length <= COMMENT_STEP || commentsCount >= currentComments.length) {
56-
commentsLoaderBtn.classList.add('hidden');
56+
commentsLoaderButton.classList.add('hidden');
5757
} else {
58-
commentsLoaderBtn.classList.remove('hidden');
58+
commentsLoaderButton.classList.remove('hidden');
5959
}
6060

6161
userCommentsContainer.appendChild(commentFragment);
6262
};
6363

64-
const onCloseFullSizePhoto = () => {
64+
const onCloseFullSizePhotoClick = () => {
6565
closeFullSizePhoto();
6666
};
6767

@@ -74,11 +74,11 @@ const onFullSizePhotoEscKey = (evt) => {
7474

7575
function closeFullSizePhoto() {
7676
commentsCount = COMMENT_STEP;
77-
document.removeEventListener('keydown', onFullSizePhotoEscKey);
7877
toggleModal();
78+
document.removeEventListener('keydown', onFullSizePhotoEscKey);
7979
}
8080

81-
const onCommentsLoaderBtn = () => {
81+
const onCommentsLoaderButtonClick = () => {
8282
commentsCount += COMMENT_STEP;
8383
renderComments();
8484
};
@@ -87,12 +87,12 @@ const renderFullSizePhoto = (photo) => {
8787
currentComments = photo.comments.slice();
8888
show(photo);
8989
renderComments();
90-
document.addEventListener('keydown', onFullSizePhotoEscKey);
9190
toggleModal();
91+
document.addEventListener('keydown', onFullSizePhotoEscKey);
9292
};
9393

94-
commentsLoaderBtn.addEventListener('click', onCommentsLoaderBtn);
95-
closeBtn.addEventListener('click', onCloseFullSizePhoto);
94+
commentsLoaderButton.addEventListener('click', onCommentsLoaderButtonClick);
95+
closeButton.addEventListener('click', onCloseFullSizePhotoClick);
9696

9797

9898
export { renderFullSizePhoto };

js/main.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import { renderPhotos } from './photos.js';
22
import { getData } from './api.js';
3-
import { initPhotoUploadForm, setUserFormSubmit, showSuccess, showError } from './upload-photo.js';
4-
5-
6-
getData((photos) => {
7-
renderPhotos(photos);
8-
});
3+
import { initPhotoUploadForm } from './upload-photo.js';
4+
import { initFilters } from './photos-filters.js';
5+
import { showErrorMessage } from './util.js';
96

7+
initPhotoUploadForm();
108

11-
setUserFormSubmit(showSuccess, showError);
9+
const displayPhotos = (data) => {
10+
renderPhotos(data);
11+
initFilters(data);
12+
};
1213

13-
initPhotoUploadForm();
14+
getData()
15+
.then((photos) => {
16+
displayPhotos(photos.slice());
17+
})
18+
.catch((showErrorMessage));
1419

js/photos-filters.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const MAX_RANDOM_PHOTO_COUNT = 10;
55

66
const RENDER_DELAY = 500;
77

8-
const FilterNames = {
8+
const FilterName = {
99
DEFAULT: 'filter-default',
1010
RANDOM: 'filter-random',
1111
POPULAR: 'filter-discussed'
@@ -25,24 +25,18 @@ let posts = [];
2525

2626
const renderWithDelay = debounce(renderPhotos, RENDER_DELAY);
2727

28-
const removeFilteredPhotos = () => {
29-
const filteredPhotos = document.querySelectorAll('.picture');
30-
filteredPhotos.forEach((photo) => photo.remove());
31-
};
32-
3328
const setFilter = (filter) => {
3429
let filterFunction = filterFunctions.setDefault;
3530

3631
switch (filter) {
37-
case FilterNames.RANDOM:
32+
case FilterName.RANDOM:
3833
filterFunction = filterFunctions.setRandom;
3934
break;
40-
case FilterNames.POPULAR:
35+
case FilterName.POPULAR:
4136
filterFunction = filterFunctions.setPopular;
4237
break;
4338
}
4439

45-
removeFilteredPhotos();
4640
renderWithDelay(filterFunction(posts));
4741
};
4842

@@ -60,10 +54,8 @@ const onPhotosFilterFormClick = (evt) => {
6054

6155
const initFilters = (data) => {
6256
posts = data;
63-
6457
photosFilterContainer.classList.remove('img-filters--inactive');
6558
photosFilterForm.addEventListener('click', onPhotosFilterFormClick);
66-
6759
};
6860

6961
export { initFilters };

js/photos.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ const renderPhoto = (photo) => {
2020
};
2121

2222
const renderPhotos = (photos) => {
23-
photos?.slice().forEach((photo) => {
23+
photos?.forEach((photo) => {
2424
userPhotosFragment.appendChild(renderPhoto(photo));
2525
});
2626

27+
userPhotosContainer.querySelectorAll('.picture').forEach((photo) => photo.remove());
2728
userPhotosContainer.appendChild(userPhotosFragment);
2829
};
2930

js/update-photo-mode.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ const Scale = {
66
};
77

88
const scaleValueField = document.querySelector('.scale__control--value');
9-
const scaleDownBtn = document.querySelector('.scale__control--smaller');
10-
const scaleUpBtn = document.querySelector('.scale__control--bigger');
9+
const scaleDownButton = document.querySelector('.scale__control--smaller');
10+
const scaleUpButton = document.querySelector('.scale__control--bigger');
1111
const preview = document.querySelector('.img-upload__preview img');
1212
const sliderContainer = document.querySelector('.img-upload__effect-level');
1313
const sliderElement = document.querySelector('.effect-level__slider');
@@ -123,12 +123,12 @@ function onEffectItemClick() {
123123
sliderContainer.style.display = '';
124124
sliderElement.noUiSlider.updateOptions(currentEffect.options);
125125
preview.style.filter = currentEffect.setFilter(currentEffect.options.range.max);
126-
sliderElement.noUiSlider.set(100);
126+
sliderElement.noUiSlider.set(Scale.MAX);
127127
}
128128

129129
const updateScale = () => {
130-
scaleUpBtn.addEventListener('click', onScaleUp);
131-
scaleDownBtn.addEventListener('click', onScaleDown);
130+
scaleUpButton.addEventListener('click', onScaleUp);
131+
scaleDownButton.addEventListener('click', onScaleDown);
132132
};
133133

134134

0 commit comments

Comments
 (0)