|
1 | | -const placeholderRepos = [ |
2 | | - { |
3 | | - name: 'SampleRepo1', |
4 | | - description: 'This repository is meant to be a sample', |
5 | | - forks: 5, |
6 | | - updated: '2020-05-27 12:00:00', |
7 | | - }, |
8 | | - { |
9 | | - name: 'AndAnotherOne', |
10 | | - description: 'Another sample repo! Can you believe it?', |
11 | | - forks: 9, |
12 | | - updated: '2020-05-27 12:00:00', |
13 | | - }, |
14 | | - { |
15 | | - name: 'HYF-Is-The-Best', |
16 | | - description: |
17 | | - "This repository contains all things HackYourFuture. That's because HYF is amazing!!!!", |
18 | | - forks: 130, |
19 | | - updated: '2020-05-27 12:00:00', |
20 | | - }, |
21 | | -]; |
22 | | -const fetchData = () => { |
23 | | - fetch('https://pokeapi.co/api/v2/pokemon/ditto') |
24 | | - .then((response) => response.json()) |
25 | | - .then((data) => { |
26 | | - createOptions(data); |
| 1 | +class Pokemon { |
| 2 | + constructor() { |
| 3 | + this.select = document.getElementById('select') |
| 4 | + this.result = document.getElementById('result') |
| 5 | + |
| 6 | + this.data = this.fetchData('https://pokeapi.co/api/v2/pokemon?limit=151') |
| 7 | + // this.Urls = []; |
| 8 | + } |
| 9 | + |
| 10 | + fetchData(url) { |
| 11 | + return axios |
| 12 | + .get(url) |
| 13 | + .then(response => { |
| 14 | + return response |
| 15 | + |
| 16 | + }); |
| 17 | + } |
| 18 | + |
| 19 | + createOptions() { |
| 20 | + this.data.then(response => { |
| 21 | + response.data.results.forEach((poki, counter) => { |
| 22 | + const option = document.createElement('option') |
| 23 | + // |
| 24 | + option.textContent = poki.name |
| 25 | + // |
| 26 | + option.setAttribute('value', counter) |
| 27 | + // |
| 28 | + this.select.appendChild(option) |
| 29 | + // |
| 30 | + // console.log(poki) |
| 31 | + }) |
| 32 | + |
| 33 | + |
27 | 34 | }); |
28 | | - // .then((data) => { |
29 | | - // console.log(data.sprites.back_default); |
30 | | - // creatImg(data.sprites.back_shiny); |
31 | | - // }); |
32 | | -}; |
33 | | -fetchData(); |
34 | | - |
35 | | -const createOptions = (jsonData) => { |
36 | | - // create select HTML tag |
37 | | - const select = document.createElement('select'); |
38 | | - //appending |
39 | | - document.body.appendChild(select); |
40 | | - jsonData.forEach((element, counter) => { |
41 | | - // create option HTML tag |
42 | | - const option = document.createElement('option'); |
43 | | - //appending |
44 | | - select.appendChild(option); |
45 | | - option.value = counter; |
46 | | - option.textContent = element; |
47 | | - }); |
48 | | -}; |
49 | | - |
50 | | -// createOptions(placeholderRepos); |
51 | | - |
52 | | -const creatImg = (url) => { |
53 | | - const img = document.createElement('img'); |
54 | | - img.src = url; |
55 | | - document.body.appendChild(img); |
56 | | -}; |
| 35 | + |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + insertImages(e) { |
| 40 | + |
| 41 | + this.data.then(res => { |
| 42 | + |
| 43 | + axios |
| 44 | + .get(res.data.results[e.target.value].url) |
| 45 | + .then(resForImg => { |
| 46 | + const imgUrl = resForImg.data.sprites.other["official-artwork"].front_default; |
| 47 | + const img = document.createElement('img') |
| 48 | + img.src = imgUrl |
| 49 | + img.setAttribute('width', '150px') |
| 50 | + this.result.appendChild(img) |
| 51 | + |
| 52 | + }) |
| 53 | + |
| 54 | + }) |
| 55 | + } |
| 56 | + addAndRemove(e) { |
| 57 | + if (this.result.firstChild === null) { |
| 58 | + this.insertImages(e) |
| 59 | + } else { |
| 60 | + this.result.removeChild(this.result.firstChild) |
| 61 | + this.insertImages(e) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | +} |
| 66 | +const poki = new Pokemon() |
| 67 | +poki.fetchData() |
| 68 | +poki.createOptions() |
| 69 | +// poki.insertImages() |
| 70 | + |
| 71 | +poki.select.addEventListener('change', poki.addAndRemove.bind(poki)) |
0 commit comments