Sfoglia il codice sorgente

done hw OmelchienkoHryhorii

unknown 3 anni fa
parent
commit
9bd257c491
8 ha cambiato i file con 346 aggiunte e 15174 eliminazioni
  1. 152 15171
      package-lock.json
  2. 1 2
      package.json
  3. BIN
      src/images/bg-pattern.png
  4. BIN
      src/images/intro.png
  5. 6 0
      src/index.html
  6. 1 1
      src/index.js
  7. 134 0
      src/java-script/hw.js
  8. 52 0
      src/styles.css

File diff suppressed because it is too large
+ 152 - 15171
package-lock.json


+ 1 - 2
package.json

@@ -57,7 +57,6 @@
     "lodash.debounce": "^4.0.8",
     "lodash.throttle": "^4.1.1",
     "material-design-icons": "^3.0.1",
-    "pnotify": "^5.1.2",
-    "redux": "^4.1.2"
+    "pnotify": "^5.1.2"
   }
 }

BIN
src/images/bg-pattern.png


BIN
src/images/intro.png


+ 6 - 0
src/index.html

@@ -8,5 +8,11 @@
     <link rel="stylesheet" href="styles.css" />
   </head>
   <body>
+    <h2>FetchBasic</h2>
+    <table class="semen" id="semen"></table>
+    <h2>FetchImproved</h2>
+    <table class="coco" id="coco"></table>
+    <h2>FetchImprovedRecursion</h2>
+    <table class="oil" id="oil"></table>
   </body>
 </html>

+ 1 - 1
src/index.js

@@ -1,2 +1,2 @@
 import './styles.css';
-import './java-script/hw.js';
+import './java-script/hw';

+ 134 - 0
src/java-script/hw.js

@@ -0,0 +1,134 @@
+const tableOne = document.getElementById('semen');
+
+const dataLuke = async () =>
+  fetch('https://swapi.py4e.com/api/people/1/')
+    .then(res => res.json())
+    .then(data => data)
+    .catch(() => {});
+
+const FetchBasic = async function (root, dataFetch) {
+  this.data = await dataFetch();
+  for (const key in this.data) {
+    const tr = document.createElement('tr');
+    if (typeof this.data[key] === 'string') {
+      tr.textContent = this.data[key];
+      root.append(tr);
+    }
+  }
+};
+
+const LukeOne = new FetchBasic(tableOne, dataLuke);
+
+const tableTwo = document.getElementById('coco');
+const FetchImproved = async function (root, data, allData = []) {
+  const fetchData = async url =>
+    fetch(url)
+      .then(res => res.json())
+      .then(data => data)
+      .catch(() => {});
+  if (!data) {
+    data = await fetchData('https://swapi.py4e.com/api/people/1/');
+    allData = [data];
+  }
+
+  const tr = document.createElement('tr');
+
+  for (const key in data) {
+    const td = document.createElement('td');
+    const btn = document.createElement('button');
+    if (data[key].includes('https://swapi.py4e.com/')) {
+      tr.id = data[key];
+      btn.textContent = data[key];
+      btn.addEventListener('click', async () => {
+        btn.style.backgroundColor = 'grey';
+        const newData = await fetchData(data[key]);
+        if (allData.some(el => el.name === newData.name)) return;
+        allData.push(newData);
+        await FetchImproved(root, newData, allData);
+      });
+      td.append(btn);
+      tr.append(td);
+    } else if (typeof data[key] === 'string') {
+      td.textContent = data[key];
+      tr.append(td);
+    }
+  }
+  root.append(tr);
+};
+
+const LukeTwo = new FetchImproved(tableTwo);
+
+const tableThree = document.getElementById('oil');
+const FetchImprovedRecursion = async function (root, data, allData = []) {
+  const fetchData = async url =>
+    fetch(url)
+      .then(res => res.json())
+      .then(data => data)
+      .catch(() => {});
+  if (!data) {
+    data = await fetchData('https://swapi.py4e.com/api/people/1/');
+    allData = [data];
+  }
+
+  const tr = document.createElement('tr');
+
+  for (const key in data) {
+    const td = document.createElement('td');
+    if (data[key].includes('https://swapi.py4e.com/')) {
+      td.textContent = data[key];
+      const a = document.createElement('a');
+      a.setAttribute('href', data[key]);
+      a.setAttribute('target', 'blank');
+      a.append(td);
+      tr.append(a);
+    } else if (typeof data[key] === 'string') {
+      td.textContent = data[key];
+      tr.append(td);
+    } else if (typeof data[key] === 'object') {
+      data[key].forEach(async key => {
+        const newData = await fetchData(key);
+        if (allData.some(el => el.name === newData.name)) return;
+        allData.push(newData);
+        await FetchImprovedRecursion(root, newData, allData);
+      });
+    }
+  }
+  root.append(tr);
+};
+
+const LukeThree = new FetchImprovedRecursion(tableThree);
+
+function myFetch(URL) {
+  return new Promise(function (resolve, reject) {
+    const request = new XMLHttpRequest();
+    request.open('GET', URL, false);
+    request.onreadystatechange = function () {
+      if (request.readyState != 4) return;
+      if (request.status == 200) resolve(JSON.parse(request.responseText));
+    };
+    request.onerror = () => reject(request);
+    request.send();
+  });
+}
+
+myFetch('https://swapi.py4e.com/api/people/1/')
+  .then(luke => console.log(luke))
+  .catch(e => console.error(e));
+
+function myFetchDelay(URL, delay = 0) {
+  return new Promise(function (resolve, reject) {
+    const request = new XMLHttpRequest();
+    request.open('GET', URL, false);
+    request.onreadystatechange = function () {
+      if (request.readyState != 4) return;
+      if (request.status == 200) setTimeout(resolve, delay, delay);
+    };
+    request.onerror = () => reject(request);
+    request.send();
+  });
+}
+
+Promise.race([
+  myFetchDelay('https://swapi.py4e.com/api/people/1/', 26),
+  myFetchDelay('https://swapi.py4e.com/api/people/1/', 0),
+]).then(delay => console.log(delay));

+ 52 - 0
src/styles.css

@@ -0,0 +1,52 @@
+body{
+  position: relative;
+  padding-bottom: 300px;
+  background-color: rgb(226, 221, 221);
+  min-height: 100vh;
+}
+button{
+ min-width: 40px;
+ min-height: 20px;
+ background-color: rgb(149, 240, 4);
+ font-size: 14px;
+}
+
+table{
+  max-width: 100%;
+  margin: 0 auto;
+  margin-bottom: 30px;
+}
+
+.semen{
+  background-color: rgb(233, 213, 101);
+  padding: 10px;
+  border-radius: 10px;
+  border: solid 3px orange;
+  color: rgb(247, 45, 9);
+}
+
+.coco{
+  background-color: rgb(50, 177, 236);
+  padding: 10px;
+  border-radius: 10px;
+  border: solid 3px rgb(11, 7, 238);
+  color: rgb(25, 9, 247);
+}
+
+.oil{
+  background-color: rgb(228, 109, 168);
+  padding: 10px;
+  border-radius: 10px;
+  border: solid 3px rgb(238, 7, 7);
+  color: rgb(247, 247, 247);
+}
+
+h2{
+  text-align: center;
+  color: brown;
+}
+
+a{
+  height: auto;
+}
+