Skip to content

Commit a99ff90

Browse files
authored
Merge pull request #16 from PAVLUXAN/master
2 parents 631bc63 + b5e4ceb commit a99ff90

17 files changed

+470
-484
lines changed

index.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ <h2 class="img-upload__title visually-hidden">Загрузка фотограф
3535

3636
<!-- Изначальное состояние поля для загрузки изображения -->
3737
<fieldset class="img-upload__start">
38-
<input type="file" id="upload-file" class="img-upload__input visually-hidden" name="filename" required>
38+
<input type="file" id="upload-file" class="img-upload__input visually-hidden" name="filename">
3939
<label for="upload-file" class="img-upload__label img-upload__control">Загрузить</label>
4040
</fieldset>
4141

@@ -235,7 +235,6 @@ <h2 class="data-error__title">Не удалось загрузить данны
235235
</section>
236236
</template>
237237
<script src="./vendor/nouislider/nouislider.js"></script>
238-
<script src="./vendor/pristine/pristine.min.js"></script>
239238
<script src="./js/main.js" type="module"></script>
240239
</body>
241240
</html>

js/api.js

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
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-
}));
1+
const BASE_URL = 'https://31.javascript.htmlacademy.pro/kekstagram/';
2+
3+
const getData = () =>
4+
fetch(`${BASE_URL}data`)
5+
.then((response) => {
6+
if (response.ok) {
7+
return response.json();
8+
}
9+
throw new Error(`${response.status} - ${response.statusText}`);
10+
})
11+
.then((data) => ({
12+
hasError: false,
13+
data,
14+
}))
15+
.catch((err) => ({
16+
hasError: true,
17+
err,
18+
}));
1619

17-
const sendData = (body) => fetch('https://31.javascript.htmlacademy.pro/kekstagram',
18-
{
20+
const sendData = (body) =>
21+
fetch(BASE_URL, {
1922
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}`);
23+
body,
2824
})
29-
.catch((error) => error);
30-
25+
.then((response) => {
26+
if (response.ok) {
27+
return null;
28+
}
29+
throw new Error(`${response.status} - ${response.statusText}`);
30+
})
31+
.catch((error) => error);
3132

3233
export { getData, sendData };

js/create-desc.js

Lines changed: 0 additions & 61 deletions
This file was deleted.

js/filter.js

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,72 @@
1-
import {debounce} from './util.js';
1+
import { debounce } from './util.js';
22

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;
3+
const ACTIVE_BUTTON_CLASS = 'img-filters__button--active';
4+
const DEFAULT_FILTER = 'filter-default';
5+
const RANDOM_FILTER = 'filter-random';
6+
const DISCUSSED_FILTER = 'filter-discussed';
7+
const RANDOM_FILTER_LIMIT = 10;
128

139
const filterPanel = document.querySelector('.img-filters');
14-
const ACTIVE_BUTTON_CLASS = 'img-filters__button--active';
1510

16-
const debounceRender = debounce(renderPhoto);
11+
const filterDefault = (photos) => photos;
12+
const filterDiscused = (photos) =>
13+
photos.toSorted((a, b) => b.comments.length - a.comments.length);
14+
const filterRandom = (photos) =>
15+
[...photos].sort(() => Math.random() - 0.5).slice(0, RANDOM_FILTER_LIMIT);
1716

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);
17+
const Filters = {
18+
[DEFAULT_FILTER]: filterDefault,
19+
[RANDOM_FILTER]: filterRandom,
20+
[DISCUSSED_FILTER]: filterDiscused,
21+
};
22+
23+
const filter = (photos, type) => {
24+
if (!photos.length) {
25+
return [];
2826
}
29-
debounceRender(filteredPictures);
27+
const filterByType = Filters[type] || Filters.DEFAULT_FILTER;
28+
return filterByType(photos);
3029
};
3130

32-
const getHandleFilter = (photos) => (evt) => {
31+
const state = {
32+
type: DEFAULT_FILTER,
33+
photos: [],
34+
debounceRender: null,
35+
render: null
36+
};
37+
38+
const handleFilter = (evt) => {
3339
const targetButton = evt.target;
3440
const activeButton = document.querySelector(`.${ACTIVE_BUTTON_CLASS}`);
41+
3542
if (!targetButton.matches('button')) {
3643
return;
3744
}
45+
3846
if (activeButton === targetButton) {
3947
return;
4048
}
49+
4150
activeButton.classList.toggle(ACTIVE_BUTTON_CLASS);
4251
targetButton.classList.toggle(ACTIVE_BUTTON_CLASS);
43-
currentFilter = targetButton.getAttribute('id');
44-
applyFilter(photos);
52+
state.type = targetButton.getAttribute('id');
53+
54+
if (!state.photos.length) {
55+
return;
56+
}
57+
const filteredPictures = filter(state.photos, state.type);
58+
state.debounceRender(filteredPictures);
59+
};
60+
61+
export const initFilter = (render) => {
62+
state.debounceRender = debounce(render);
63+
state.render = render;
64+
filterPanel.addEventListener('click', handleFilter);
4565
};
4666

47-
export const initFilter = (photos) => {
67+
export const setPhotos = (data) => {
68+
state.photos = [...data];
69+
state.type = DEFAULT_FILTER;
70+
state.render(filter(state.photos, state.type));
4871
filterPanel.classList.remove('img-filters--inactive');
49-
filterPanel.addEventListener('click', getHandleFilter(photos));
5072
};

0 commit comments

Comments
 (0)