forked from averkova-a/rick-and-morty-page
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
70 lines (58 loc) · 2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { CharactersView } from './src/charactersView.js';
import { sortByDateAsc, sortByDateDesc, sortByEpisodesAsc, sortByEpisodesDesc} from './src/sorting.js';
import { trackScroll, hideCards} from './src/cardDisplay.js';
import './style.css'
const url = 'https://rickandmortyapi.com/api/character';
document.querySelector('button#sort-ep-asc').onclick = function () {
sortByEpisodesAsc();
}
document.querySelector('button#sort-ep-desc').onclick = function () {
sortByEpisodesDesc();
}
document.querySelector('button#sort-date-asc').onclick = function () {
sortByDateAsc();
}
document.querySelector('button#sort-date-desc').onclick = function () {
sortByDateDesc();
}
document.querySelector('button#display').onclick = function () {
hideCards();
}
document.addEventListener("click", function(event) {
if(event.target.matches('button.delete')) {
const cardID = event.target.className.split(' ')[0];
this.querySelector('.character.'+cardID).remove();
if (this.querySelector("[style='display: none;']") !== null){
this.querySelector("[style='display: none;']").style.display = '';
}
}
});
window.addEventListener('scroll', trackScroll);
function callApi(url) {
return fetch(url)
.then(res => res.json())
.then(data => {
return data.results; // массив объектов
})
.catch((error) => {
console.log(JSON.stringify(error));
});
};
class App {
constructor() {
this.startApp();
}
static rootElement = document.getElementById('root');
async startApp() {
try {
const characters = await callApi(url);
const charactersView = new CharactersView(characters);
const charactersElement = charactersView.element;
App.rootElement.appendChild(charactersElement);
hideCards();
} catch (error) {
console.warn(error);
}
}
}
new App();