Skip to content

๐ŸŒˆ ๊ณต๊ณตAPI๋กœ ์ œ๊ณตํ•˜๋Š” ๋‚ ์”จ ์œ„์ ฏ ์„œ๋น„์Šค

Notifications You must be signed in to change notification settings

fe-Jay/weatherSPA

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

34 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

weatherSPA

๊ณต๊ณต API๋กœ ๋‚ ์”จ ์œ„์ ฏ ๋งŒ๋“ค๊ธฐ

๐Ÿ”—๋‚ ์”จ ์œ„์ ฏ ๋ฐ”๋กœ๊ฐ€๊ธฐ



๐Ÿ” ๋‚ ์”จ๋ฅผ ํ™•์ธํ•˜๊ณ  ์‹ถ์€ ์ง€์—ญ์„ ์ž…๋ ฅํ•ด์„œ ๋‚ ์”จ ๊ฒ€์ƒ‰

  • searchForm ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค์–ด์„œ, ๊ฒ€์ƒ‰์ฐฝ์„ ๋งŒ๋“ ๋‹ค.
  • ๊ฒ€์ƒ‰form์„ ์ œ์ถœํ•˜๋ฉด, ํ•ด๋‹น ์ง€์—ญ์˜ ๋‚ ์”จ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•œ๋‹ค.
  • ํŽ˜์ด์ง€ ์ด๋™์€ useNavigate Hook์„ ์‚ฌ์šฉํ•œ๋‹ค.
  • setInputLocation ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ, preInput ๊ฐ’์„ ๋„˜๊ฒจ์ค€๋‹ค.

Image


const navigate = useNavigate();

const submitForm = (event) => {
  event.preventDefault();
  setInputLocation(preInput);
  setPreInput('');
  navigate('/');
};

<SearchForm>
  <h2>Local search</h2>
  <form onSubmit={submitForm}>
    <input type='text' placeholder='Seoul' value={preInput} onChange={(event) => setPreInput(event.target.value)} />
    <button type='submit'>๐Ÿ”</button>
  </form>
</SearchForm>;





๐ŸŒก๏ธ Weather Api ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€์„œ ํ˜„์žฌ ๋‚ ์”จ์™€ ์ดํ›„ 3์ผ๊ฐ„์˜ ๋‚ ์”จ๋ฅผ ํ‘œ๊ธฐ.

  • Promise.all๋กœ ํ˜„์žฌ ๋‚ ์”จ์™€ ์ผ๊ธฐ์˜ˆ๋ณด API๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ, ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜จ๋‹ค.
  • ๊ฐ๊ฐ API์—์„œ ์ถ”์ถœํ•œ ๋ฐ์ดํ„ฐ๋ฅผ state์— ์ €์žฅํ•œ๋‹ค.
  • ์ฒซ ์ ‘์† ์‹œ ๋กœ๋”ฉ ํ™”๋ฉด์„ ๋ณด์—ฌ์ฃผ๊ณ  ํ˜„์žฌ ์œ„์น˜ ๋ฐ์ดํ„ฐ ๋…ธ์ถœํ•œ๋‹ค.

Image


// ํ˜„์žฌ ์œ„์น˜๋ฅผ ๋ฐ›์•„์˜ค๋Š” ํ•จ์ˆ˜
function currentLocation() {
  return new Promise((resolve, reject) => {
    navigator.geolocation.getCurrentPosition(
      (position) => {
        const latitude = position.coords.latitude;
        const longitude = position.coords.longitude;
        resolve({ latitude, longitude });
      },
      (error) => {
        reject('error');
      }
    );
  });
}

// date ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•ด ๋ฐ์ดํ„ฐ ์ถ”์ถœ
const oneDay = 1000 * 60 * 60 * 24;
const offset = 1000 * 60 * 60 * 9;
const current = new Date().getTime() + offset;
const DesiredTime = ' 21:00:00';
const oneDaysLater = new Date(current + oneDay).toISOString().slice(0, 10) + DesiredTime;
const twoDaysLater = new Date(current + oneDay * 2).toISOString().slice(0, 10) + DesiredTime;
const threeDaysLater = new Date(current + oneDay * 3).toISOString().slice(0, 10) + DesiredTime;

// 3์ผ์น˜ ๋ฐ์ดํ„ฐ๋งŒ ์ถ”์ถœ (UTC ์‹œ๊ฐ„ ๊ณ„์‚ฐ ๊ณ ๋ ค)
const weatherData = resultForecast.list.filter((item) => {
  return item.dt_txt === oneDaysLater || item.dt_txt === twoDaysLater || item.dt_txt === threeDaysLater;
});





๐Ÿ•ฐ๏ธ 24์‹œ๊ฐ„์„ ์ธ์‹ํ•ด์„œ ์‹œ๊ฐ„๋Œ€๋ณ„๋กœ ๋ฐฐ๊ฒฝํ™”๋ฉด์— ๋ณ€ํ™” ์ถ”๊ฐ€.

  • intl.DateTimeFormat์„ ์ด์šฉํ•ด์„œ ํ˜„์žฌ ์‹œ๊ฐ„์„ ๋ฐ›์•„์˜จ๋‹ค.
  • styled-components์˜ props๋ฅผ ์ด์šฉํ•ด์„œ, ์‹œ๊ฐ„๋Œ€๋ณ„๋กœ ๋ฐฐ๊ฒฝํ™”๋ฉด์„ ๋ณ€๊ฒฝํ•œ๋‹ค.

Image Image


  // ํ˜„์žฌ ์‹œ๊ฐ„์„ ๋ฐ›์•„์™€์„œ props๋กœ ์ „๋‹ฌ
    const currentTime = new Intl.DateTimeFormat(
        'en', { hour: '2-digit', hourCycle: 'h23' }).format(new Date()
    )

    const GlobalStyle = createGlobalStyle`
    body {
    padding: clamp(1,5rem, 5vw, 3rem);
    background: ${(props) => (
        props.times > 6 && props.times <= 17
            ? 'linear-gradient(345.21deg, #89F7FE 2.6%, #66A6FF 85.16%)'
            : props.times > 17 && props.times <= 19
                ? 'linear-gradient(180deg, #3F51B1 -7.36%, #5A55AE 5.68%, #7B5FAC 17.73%, #8F6AAE 30.77%, #A86AA4 42.81%, #CC6B8E 54.86%, #F18271 67.9%, #F3A469 79.94%, #F7C978 92.99%)'
                : 'linear-gradient(167.44deg, #08244F 0%, #134CB5 47.38%, #0B42AB 100%)'
    )};
    }





์ฐธ์กฐ

About

๐ŸŒˆ ๊ณต๊ณตAPI๋กœ ์ œ๊ณตํ•˜๋Š” ๋‚ ์”จ ์œ„์ ฏ ์„œ๋น„์Šค

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published