230419 - fetch

๋ฐฑ์Šน์—ฐยท2023๋…„ 4์›” 20์ผ
1

๐Ÿšฉ javascript

fetch

๐Ÿ“ ๋‚ด์šฉ

  • 17์ผ์— ์ž‘์„ฑํ–ˆ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ data ์‘์šฉ์„ fetch๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ ๋ณ€๊ฒฝ

โœ’๏ธ ์ฝ”๋“œ ์ž‘์„ฑ

์ž…๋ ฅ

css

.container { display: flex; flex-direction: column; max-width: 1200px; min-width: 800px; align-items: center; row-gap: 10px; margin-top: 30px; }
h1 { font-size: 1.6em; margin-bottom: 1em; }
.container>button { width: 500px; padding: 10px 0; }
.containerul { display: flex; flex-wrap: wrap; }
.containerulli { width: 25%; padding: 10px; text-align: center; margin: 20px 0; }
.containerimg { width: 100% }
.name { padding: 10px 0; font-weight: bold; }
.price { color: #999; font-size: 14px; }

html

<div class="container">
  <h1>์ƒํ’ˆ๋ชฉ๋ก</h1>
  <button>์ƒํ’ˆ๋ชฉ๋ก ๋ถˆ๋Ÿฌ์˜ค๊ธฐ</button>
  <ul>

  </ul>
</div>

js

// import products from "https://sat2llite.github.io/data/db.json" assert { type: "json" };
//import products from "../../db.json" assert {type:'json'}
//assert {type:'json'} -์™ธ๋ถ€ํŒŒ์ผ์ด json์ด๋ผ๊ณ  ํ™•์‹คํ•˜๊ฒŒ ๋ช…์‹œ


let products;

// fetch ๊ธฐ๋ณธ ๋ฌธ๋ฒ•
async function getJson() {
  // return fetch("https://sat2llite.github.io/data/db.json")
  // .then((response) => response.json)
  // .then(products => products)

  const response = await fetch('https://sat2llite.github.io/data/db.json');
  products = await response.json();
  return products;
}

getJson()
.then((products) => console.log(products));



const button = document.querySelector("button");

//li๋งŒ๋“ค์–ด์„œ ul์— ๋„ฃ์–ด์ฃผ๋Š” ํ•จ์ˆ˜
const createItem = (product) => {
  const ul = document.querySelector("ul");
  const li = document.createElement("li");
  const img = document.createElement("img");
  const p = document.createElement("p");
  const span = document.createElement("span");

  //๊ฐ๊ตญ์— ๋งž๋Š” ์ˆซ์ž ์„œ์‹์„ ์ง€์›ํ•˜๋Š” ๊ฐ์ฒด์˜ ์ƒ์„ฑ์ž
  const price = new Intl.NumberFormat("ko-KR", {
    style: "currency", //ํ†ตํ™”๋‹จ์œ„
    currency: "KRW", //์›ํ™”
  }).format(product.price); //ํฌ๋งท์„ ๋ฐ”๊ฟ€ ๋ฐ์ดํ„ฐ

  img.setAttribute("src", product.img);
  li.id = product.id;
  p.className = "name"; //css
  p.innerHTML = product.name;
  span.className = "price";
  span.innerText = price;

  li.append(img, p, span);
  ul.append(li);
};

//๋งŒ๋“  li๋“ค์ด ๋ฐ˜๋ณต๋˜๊ฒŒ
const importData = () => {
  products.data.map((product) => {
    //๊ณ„์† ์ถ”๊ฐ€๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€(๋™์ผํ•œ์•„์ด๋””๊ฐ’์ด ์ด๋ฏธ ์žˆ์„๋•Œ๋Š” ์ž‘๋™X)
    if (!document.getElementById(product.id)) {
      createItem(product);
    }
  });
};

button.addEventListener("click", importData);

์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด

๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ & ๋„์›€์ด ๋˜๋Š” ๋งํฌ






calculate

๐Ÿ“ ๋‚ด์šฉ

  • ์œ„์˜ ์ƒํ’ˆ๋ชฉ๋ก ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์ฝ”๋“œ๋ฅผ ๋ณ€ํ˜•ํ•˜์—ฌ ์ฒดํฌ ์‹œ ๊ฐ€๊ฒฉ ํ•ฉ์‚ฐ์ด ๋˜๋„๋ก ์ˆ˜์ •

โœ’๏ธ ์ฝ”๋“œ ์ž‘์„ฑ

์ž…๋ ฅ

css

body { display: flex; justify-content: center; font-family: Verdana, Geneva, Tahoma, sans-serif; }
.container { display: flex; flex-direction: column; max-width: 700px; min-width: 500px; align-items: center; row-gap: 10px; margin-top: 30px; }
h1 { font-size: 1.6em; margin-bottom: 1em; }
.container>button { width: 500px; padding: 10px 0; }
.containerul { display: flex; flex-direction: column; row-gap: 20px; width: 100%; margin-bottom: 30px; }
.containerulli { display: flex; align-items: center; column-gap: 10px; padding: 10px; background: #d6d6d6; border-radius: 16px; }
.containerimg { width: 60px; }
.name { padding: 10px 0; font-weight: bold; width: 70%; }
.price { color: #999; font-size: 14px; color: #f01d1d; }
input[type="checkbox"] { width: 16px; height: 16px; }
.total { font-size: 20px; }
.total_price { color: #1d21f0; font-size: 26px; }

html

<div class="container">
  <h1>์ƒํ’ˆ๋ชฉ๋ก</h1>
  <button class="mainBtn">์ƒํ’ˆ๋ชฉ๋ก ๋ถˆ๋Ÿฌ์˜ค๊ธฐ</button>
  <ul>

  </ul>
  <div class="total">
    <span>Total : </span>
    <span class="total_price">โ‚ฉ0</span>
  </div>
</div>

js

// import products from "https://sat2llite.github.io/data/db.json" assert { type: "json" };
//import products from "../../db.json" assert {type:'json'}
//assert {type:'json'} -์™ธ๋ถ€ํŒŒ์ผ์ด json์ด๋ผ๊ณ  ํ™•์‹คํ•˜๊ฒŒ ๋ช…์‹œ

let products; // products ์ „์ฒด
let myProduct; // products.data
let selected = []; // ์ฒดํฌ๋œ ์•„์ดํ…œ์„ ์ €์žฅํ•  ๋ฐฐ์—ด

// fetch ๊ธฐ๋ณธ ๋ฌธ๋ฒ•
async function getJson() {
  // return fetch("https://sat2llite.github.io/data/db.json")
  // .then((response) => response.json)
  // .then(products => products)

  const response = await fetch(
    "https://mlmlmlml00700700700.github.io/data/db.json"
  );
  products = await response.json();
  return products;
}
getJson().then((products) => importData(products));

const button = document.querySelector("button");

// ๊ณ„์‚ฐ๊ฐ’์„ ํ™”๋ฉด์— ๋ณด์—ฌ์ฃผ๋Š” ํ•จ์ˆ˜ ์ •์˜
const printTotal = (price) => {
  const spanResult = document.querySelector(".total_price");
  
  //๊ฐ๊ตญ์— ๋งž๋Š” ์ˆซ์ž ์„œ์‹์„ ์ง€์›ํ•˜๋Š” ๊ฐ์ฒด์˜ ์ƒ์„ฑ์ž
  const priceFormat = new Intl.NumberFormat("ko-KR", {
    style: "currency", //ํ†ตํ™”๋‹จ์œ„
    currency: "KRW", //์›ํ™”
  }).format(price); //ํฌ๋งท์„ ๋ฐ”๊ฟ€ ๋ฐ์ดํ„ฐ
  
  spanResult.innerText = priceFormat;
}

// ๊ณ„์‚ฐํ•ด์ฃผ๋Š” ํ•จ์ˆ˜ ์ •์˜
const calculate = () => {
  const result = selected.reduce((acc, current) => { // acc - ์ด ํ•ฉ๊ณ„, current - ๋ฐฐ์—ด์˜ ์•„์ดํ…œ
    return acc + current.price; // += ์จ๋„ ๋จ
  }, 0);
  // console.log(result);

  printTotal(result); // ํ™”๋ฉด์— ๋ณด์—ฌ์ฃผ๋Š” ํ•จ์ˆ˜ ํ˜ธ์ถœ
};

// ์ฒดํฌ๋ฐ•์Šค๋ฅผ ์ฒดํฌํ•˜๋ฉด ์‹คํ–‰ ๋˜๋Š” ํ•จ์ˆ˜
const addCart = (event) => {
  // console.log(event.target.parentElement.id);

  // ๊ตฌ์กฐ๋ถ„ํ•ด
  const { id } = event.target.parentElement; // ์•„์ด๋”” (์–ด๋–ค li๋ฅผ ์„ ํƒํ–ˆ๋Š”์ง€)
  const { checked } = event.target; // ์„ ํƒ(true/false)
  console.log(id, checked);

  if (checked) {
    // ์ฒดํฌ ๋˜์—ˆ์„ ๊ฒฝ์šฐ
    myProduct.forEach((aa) => {
      if (aa.id === parseInt(id)) {
        // ๋ฌธ์ž์—ด์„ ์ •์ˆ˜๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ ( == )
        selected.push(aa);
      }
    });
  } else {
    // ์ฒดํฌ๊ฐ€ ํ•ด์ œ๋˜๋ฉด ๋ฐฐ์—ด์—์„œ ์‚ญ์ œ
    selected = selected.filter((bb) => {
      return bb.id !== parseInt(id);
    });
  }
  console.log(selected);

  calculate(); // ๊ณ„์‚ฐํ•ด์ฃผ๋Š” ํ•จ์ˆ˜ ํ˜ธ์ถœ
};

//li๋งŒ๋“ค์–ด์„œ ul์— ๋„ฃ์–ด์ฃผ๋Š” ํ•จ์ˆ˜
const createItem = (product) => {
  const ul = document.querySelector("ul");
  const li = document.createElement("li");
  const img = document.createElement("img");
  const p = document.createElement("p");
  const span = document.createElement("span");
  const check = document.createElement("input");

  //๊ฐ๊ตญ์— ๋งž๋Š” ์ˆซ์ž ์„œ์‹์„ ์ง€์›ํ•˜๋Š” ๊ฐ์ฒด์˜ ์ƒ์„ฑ์ž
  const price = new Intl.NumberFormat("ko-KR", {
    style: "currency", //ํ†ตํ™”๋‹จ์œ„
    currency: "KRW", //์›ํ™”
  }).format(product.price); //ํฌ๋งท์„ ๋ฐ”๊ฟ€ ๋ฐ์ดํ„ฐ

  img.setAttribute("src", product.img);
  li.id = product.id;
  p.className = "name"; //css
  p.innerHTML = product.name;
  span.className = "price";
  span.innerText = price;

  // check(input)์— ํƒ€์ž… ์ถ”๊ฐ€, ์ด๋ฒคํŠธ๋ฆฌ์Šค๋„ˆ ์ถ”๊ฐ€
  check.setAttribute("type", "checkbox");
  check.addEventListener("change", addCart); // ์ฒดํฌ๋ฐ•์Šค์— ์ฒดํฌ๋˜๋ฉด addCart ํ•จ์ˆ˜ ์‹คํ–‰

  li.append(check, img, p, span);
  ul.append(li);
};

//๋งŒ๋“  li๋“ค์ด ๋ฐ˜๋ณต๋˜๊ฒŒ
const importData = () => {
  myProduct = products.data;
  myProduct.map((product) => {
    //๊ณ„์† ์ถ”๊ฐ€๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€(๋™์ผํ•œ์•„์ด๋””๊ฐ’์ด ์ด๋ฏธ ์žˆ์„๋•Œ๋Š” ์ž‘๋™X)
    if (!document.getElementById(product.id)) {
      createItem(product);
    }
  });
};

// button.addEventListener("click", importData);

์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด


๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ & ๋„์›€์ด ๋˜๋Š” ๋งํฌ






profile
๊ณต๋ถ€ํ•˜๋Š” ๋ฒจ๋กœ๊ทธ

0๊ฐœ์˜ ๋Œ“๊ธ€