Skip to content

Commit f964811

Browse files
authored
Merge pull request #13 from PAVLUXAN/module12-task1
2 parents a25a21b + f4ebe6c commit f964811

File tree

7 files changed

+142
-59
lines changed

7 files changed

+142
-59
lines changed

js/api.js

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
const getData = (onSuccess, onError) => {
2-
fetch('https://31.javascript.htmlacademy.pro/kekstagram/data')
3-
.then((response) => {
4-
if (response.ok) {
5-
return response.json();
6-
}
7-
throw new Error(`${response.status} - ${response.statusText}`);
8-
})
9-
.then((data) => onSuccess(data))
10-
.catch(() => onError());
11-
};
1+
const getData = () => fetch('https://31.javascript.htmlacademy.pro/kekstagram/data')
2+
.then((response) => {
3+
if (response.ok) {
4+
return response.json();
5+
}
6+
throw new Error(`${response.status} - ${response.statusText}`);
7+
})
8+
.then((data) => ({
9+
hasError: false,
10+
data,
11+
}))
12+
.catch((error) => ({
13+
hasError: true,
14+
error,
15+
}));
16+
17+
const sendData = (body) => fetch('https://31.javascript.htmlacademy.pro/kekstagram',
18+
{
19+
method: 'POST',
20+
body
21+
}
22+
)
23+
.then((response) => {
24+
if (response.ok) {
25+
return null;
26+
}
27+
throw new Error(`${response.status} - ${response.statusText}`);
28+
})
29+
.catch((error) => error);
1230

13-
const sendData = (onSuccess, onError, body) => {
14-
fetch('https://31.javascript.htmlacademy.pro/kekstagram',
15-
{
16-
method: 'POST',
17-
body,
18-
},
19-
)
20-
.then((response) => {
21-
if (response.ok) {
22-
onSuccess();
23-
return;
24-
}
25-
onError();
26-
})
27-
.catch(() => {
28-
onError();
29-
});
30-
};
3131

3232
export { getData, sendData };

js/filter.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import {debounce} from './util.js';
2+
3+
import { renderPhoto } from './render-photo.js';
4+
5+
const FILTER = {
6+
default:'filter-default',
7+
random: 'filter-random',
8+
discussed: 'filter-discussed'
9+
};
10+
11+
let currentFilter = FILTER.default;
12+
13+
const filterPanel = document.querySelector('.img-filters');
14+
const ACTIVE_BUTTON_CLASS = 'img-filters__button--active';
15+
16+
const debounceRender = debounce(renderPhoto);
17+
18+
const applyFilter = (photos) => {
19+
let filteredPictures = [];
20+
if(currentFilter === FILTER.default) {
21+
filteredPictures = photos;
22+
}
23+
if(currentFilter === FILTER.random) {
24+
filteredPictures = photos.toSorted(() => 0.5 - Math.random()).slice(0, 10);
25+
}
26+
if(currentFilter === FILTER.discussed) {
27+
filteredPictures = photos.toSorted((a,b) => b.comments.length - a.comments.length);
28+
}
29+
//renderPhoto(filteredPictures);
30+
debounceRender(filteredPictures);
31+
};
32+
33+
const getHandleFilter = (photos) => (evt) => {
34+
const targetButton = evt.target;
35+
const activeButton = document.querySelector(`.${ACTIVE_BUTTON_CLASS}`);
36+
if (!targetButton.matches('button')) {
37+
return;
38+
}
39+
if (activeButton === targetButton) {
40+
return;
41+
}
42+
activeButton.classList.toggle(ACTIVE_BUTTON_CLASS);
43+
targetButton.classList.toggle(ACTIVE_BUTTON_CLASS);
44+
currentFilter = targetButton.getAttribute('id');
45+
applyFilter(photos);
46+
};
47+
48+
export const initFilter = (photos) => {
49+
filterPanel.classList.remove('img-filters--inactive');
50+
filterPanel.addEventListener('click', getHandleFilter(photos));
51+
};

js/img-form-uploader.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,18 @@ const unblockSubmitButton = () => {
105105
const handleSubmit = (evt) =>{
106106
evt.preventDefault();
107107
blockSubmitButton();
108-
109-
sendData(
110-
() => {
111-
document.removeEventListener('keydown', onEscapeKeydown);
108+
sendData(new FormData(evt.target))
109+
.then((error) => {
112110
unblockSubmitButton();
113-
showSuccess();
114-
},
115-
() => {
116111
document.removeEventListener('keydown', onEscapeKeydown);
117-
showErrorSending();
118-
unblockSubmitButton();
119-
},
120-
new FormData(evt.target),
121-
);
122-
};
123112

124-
imgUploadForm.addEventListener('submit', handleSubmit);
113+
if (error) {
114+
showErrorSending();
115+
return;
116+
}
125117

118+
showSuccess();
119+
uploadOverlay.classList.add('hidden');
120+
});
121+
};
122+
imgUploadForm.addEventListener('submit', handleSubmit);

js/main.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@ import {renderPhoto} from './render-photo.js';
22
import './img-form-uploader.js';
33
import { getData } from './api.js';
44
import { showErrorGetting } from './response.js';
5+
import { initFilter } from './filter.js';
56

6-
getData(renderPhoto, showErrorGetting);
7+
getData()
8+
.then((response) => {
9+
if (response.hasError) {
10+
showErrorGetting();
11+
return;
12+
}
13+
const { data = [] } = response;
14+
renderPhoto(data);
15+
initFilter(data);
16+
});
717

js/render-big-photo.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ const closeBigPicture = () =>{
2828
document.removeEventListener('keydown', onEscKeyDown);
2929
};
3030

31-
export const openBigPicture = (pictureId, photos) =>{
32-
const currentPhoto = photos.find((photo) => photo.id === Number(pictureId));
33-
31+
export const openBigPicture = (photo) =>{
32+
const currentPhoto = photo;
3433
bigPictureImgNode.src = currentPhoto.url;
3534
likesCountNode.textContent = currentPhoto.likes;
3635
commentsCaptionNode.textContent = currentPhoto.description;

js/render-photo.js

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,42 @@
1-
//import {photos} from './create-desc.js';
21
import { openBigPicture } from './render-big-photo.js';
32

43
const template = document.querySelector('#picture').content.querySelector('.picture');
54
const container = document.querySelector('.pictures');
65
const fragment = document.createDocumentFragment();
76

7+
const clearPhotos = () => {
8+
document.querySelectorAll('a.picture').forEach((item) => item.remove());
9+
};
10+
11+
let currentPhotos = [];
12+
13+
const onPictureClick = (evt) => {
14+
const target = evt.target.closest('.picture');
15+
if (!target) {
16+
return;
17+
}
18+
19+
const id = Number(target.dataset.pictureId);
20+
const photo = currentPhotos.find((p) => p.id === id);
21+
if (photo) {
22+
openBigPicture(photo);
23+
}
24+
};
25+
826
export const renderPhoto = (photos) => {
9-
photos.forEach((photo) => {
27+
clearPhotos();
28+
currentPhotos = photos;
29+
30+
currentPhotos.forEach((photo) => {
1031
const thumbnail = template.cloneNode(true);
1132
thumbnail.dataset.pictureId = photo.id;
12-
const image = thumbnail.querySelector('.picture__img');
13-
image.src = photo.url;
14-
image.alt = photo.description;
33+
thumbnail.querySelector('.picture__img').src = photo.url;
34+
thumbnail.querySelector('.picture__img').alt = photo.description;
1535
thumbnail.querySelector('.picture__comments').textContent = photo.comments.length;
1636
thumbnail.querySelector('.picture__likes').textContent = photo.likes;
1737
fragment.appendChild(thumbnail);
1838
});
39+
1940
container.appendChild(fragment);
20-
container.addEventListener('click', (evt) => {
21-
const currentPictureNode = evt.target.closest('.picture');
22-
if (currentPictureNode) {
23-
openBigPicture(currentPictureNode.dataset.pictureId, photos);
24-
}
25-
});
41+
container.addEventListener('click',onPictureClick);
2642
};

js/util.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,14 @@ const selectWordByCount = (num, nominative, genitiveSingular, genitivePlural) =>
1818
: genitiveSingular;
1919
};
2020

21-
export {getRandomNumber, getRandomItem, isEscapeKey, selectWordByCount,};
21+
const debounce = (callback, timeoutDelay = 500) => {
22+
let timeoutId;
23+
return (rest) => {
24+
clearTimeout(timeoutId);
25+
timeoutId = setTimeout(() => {
26+
callback.call(this,rest);
27+
}, timeoutDelay);
28+
};
29+
};
30+
31+
export {getRandomNumber, getRandomItem, isEscapeKey, selectWordByCount, debounce};

0 commit comments

Comments
 (0)