/DayMaker

๐Ÿ“ŒTo do List

Primary LanguageJavaScript

DayMaker

๐ŸŒฑ ์„œ๋น„์Šค

video.mov

๐Ÿ”— LINK : DayMaker

๐Ÿ’ป ๊ฐœ๋ฐœ ํ™˜๊ฒฝ

๐Ÿ”จ Library & Language

๐Ÿ”จ API

image

OpenWeatherMap

๐Ÿš€ ์‹คํ–‰ ๋ฐฉ๋ฒ•

$ git clone https://github.com/seunghyun0522/DayMaker.git

๐Ÿ“ ๋””๋ ‰ํ† ๋ฆฌ ๊ตฌ์กฐ

โ”œโ”€โ”€ ๐Ÿ“‘css
โ”‚   โ”œโ”€โ”€ ๐Ÿ“œreset.css
โ”‚   โ””โ”€โ”€ ๐Ÿ“œstyle.css
โ”œโ”€โ”€ ๐Ÿ“‘imgs
โ”‚   โ”œโ”€โ”€ background
โ”‚   โ””โ”€โ”€ icon
โ”œโ”€โ”€ ๐Ÿ“œindex.html
โ””โ”€โ”€ ๐Ÿ“‘script
    โ”œโ”€โ”€ ๐Ÿ“œbackground.js
    โ”œโ”€โ”€ ๐Ÿ“œcalender.js
    โ”œโ”€โ”€ ๐Ÿ“œclock.js
    โ”œโ”€โ”€ ๐Ÿ“œquotes.js
    โ”œโ”€โ”€ ๐Ÿ“œtodoList.js
    โ”œโ”€โ”€ ๐Ÿ“œuser.js
    โ””โ”€โ”€ ๐Ÿ“œweather.js

๐Ÿ’ก ๊ตฌํ˜„ ๊ธฐ๋Šฅ ๋ฐ ๊ตฌํ˜„ ๋ฐฉ์‹

1. Data ์ž…๋ ฅ์‹œ ์ƒˆ๋กœ๊ณ ์นจ ๋ฐฉ์ง€

  • html tag๋ฅผ form์œผ๋กœ ์„ค์ •ํ•˜๊ณ  input์„ ํ†ตํ•ด ์ž…๋ ฅํ•˜๊ณ  ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ ๊ฒฐ๊ณผ๊ฐ’์ด ๋”ฐ๋กœ ์ถœ๋ ฅ์ด ๋˜์ง€ ์•Š๋Š” ํ˜„์ƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์€ ์ƒํ™ฉ์€ ์‹คํ–‰์ด ๋˜์ง€ ์•Š๋Š”๋‹ค.
const loginForm = document.querySelector("#login-form");
const loginInput = document.querySelector("#login-form input");

function onLoginSubmit() {
  const username = loginInput.value;
  console.log(username);
}
loginForm.addEventListener("submit", onLoginSubmit);

form ํƒœ๊ทธ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ submit๋ฅผ ํ•˜๋ฉด ๋ธŒ๋ผ์šฐ์ €๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ํŽ˜์ด์ง€๋ฅผ ์ƒˆ๋กœ๊ณ ์นจํ•˜๋„๋ก ๋˜์–ด์žˆ๋‹ค. โ†’ ์ด๊ฒƒ์„ ๋ง‰๋Š” ๊ฒƒ์ด preventDefault() ์ด๋‹ค.

์‹คํ–‰์ด ๋˜๊ธฐ ์œ„ํ•ด์„œ๋Š” preventDefault()๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. โ†’ ์ฆ‰ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ๊ธฐ๋ณธ ๋™์ž‘์„ ์‹คํ–‰ํ•˜์ง€ ๋ชปํ•˜๊ฒŒ ๋ง‰์•„์ฃผ๋Š” ๊ฒƒ ๋ฟ์ด๋‹ค. preventDefault()๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” function๋‚ด argument๋ฅผ ์ผ๋‹จ ์ฑ„์›Œ์•ผ ํ•œ๋‹ค.

function onLoginSubmit(event) {
  event.preventDefault();
  console.log(loginInput.value);
}

2. Data ๋ฅผ local์— ์ €์žฅํ•˜๊ธฐ

  • ํ˜„์žฌ DB๋ฅผ ๋”ฐ๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— local ๋‚ด์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•ด์•ผ ํ•œ๋‹ค.
  • ๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•ด์„œ๋Š” local Storage ๋ผ๋Š” ์ž‘์€ DB์— ์ €์žฅํ•ด์•ผ ํ•œ๋‹ค. (์ƒˆ๋กœ๊ณ ์นจ ํ›„์—๋„ ์‚ด์•„ ์žˆ๊ฒŒ!) localstorage.setItem(key,value) : ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ
    localstorage.getItem(key,value) : ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
    localstorage.removeItem(key,value) : ๋ฐ์ดํ„ฐ ์ง€์šฐ๊ธฐ

3. ์‹ค์‹œ๊ฐ„ ์‹œ๊ฐ„ ๋ฐ›์•„์˜ค๊ธฐ

  • js ๋‚ด์— ์žˆ๋Š” ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์ด ์žˆ์ง€๋งŒ ๊ฐ„๋‹จํ•œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

  • new Date()๋ฅผ ์‚ฌ์šฉํ•ด ์‹ค์‹œ๊ฐ„ ์‹œ๊ฐ„์„ ๊ฐ€์ ธ์˜จ๋‹ค.

  • ์‹œ๊ฐ„์€ 1์ดˆ์”ฉ ํ๋ฅด๊ธฐ๋•Œ๋ฌธ์— setInterval(function(),1000)๋ฅผ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

    const clock = document.querySelector("h2#clock");
    
    function getClock() {
      const date = new Date();
    
      clock.innerText = `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
    }
    
    getClock();
    setInterval(getClock, 1000);

4. weather API ์‚ฌ์šฉํ•˜๊ธฐ

  • ์‚ฌ์šฉ์ž์˜ ํ˜„์žฌ ์œ„์น˜๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋‚ ์”จ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์™€ ์›น ํŽ˜์ด์ง€์— ํ‘œ์‹œํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์•ˆ๋‚ดํ•œ๋‹ค.

  • OpenWeatherMap API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ˜„์žฌ ์œ„์น˜์˜ ๋‚ ์”จ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

    const API_KEY = API_KEY ๋„ฃ๊ธฐ
    
    // ์œ„์น˜ ์ •๋ณด๋ฅผ ์„ฑ๊ณต์ ์œผ๋กœ ๊ฐ€์ ธ์˜จ ๊ฒฝ์šฐ ์‹คํ–‰๋˜๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜
    function onGeoOk(position) {
      const lat = position.coords.latitude;
      const lon = position.coords.longitude;
      const url =       `https://api.openweathermap.org/data/2.5/weatherlat=${lat}&lon=${lon}&appid=${API_KEY}&units=metric`    ;
    
      // OpenWeatherMap API๋กœ ๋‚ ์”จ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
      fetch(url)
      .then((response) => response.json())
      .then((data) => {
        // HTML์—์„œ ๋‚ ์”จ ์ •๋ณด๋ฅผ ํ‘œ์‹œํ•  ์š”์†Œ๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.
        // ๋‚ ์”จ ์ •๋ณด๋ฅผ ํ™”๋ฉด์— ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.
    
      });
    }
    
      // ์œ„์น˜ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ ์‹คํ–‰๋˜๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜
      function onGeoError() {
        alert("Can't find you, No weather for you.");
      }
    
      // ์‚ฌ์šฉ์ž์˜ ํ˜„์žฌ ์œ„์น˜ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
      navigator.geolocation.getCurrentPosition(onGeoOk, onGeoError);