๊ด€๋ฆฌ ๋ฉ”๋‰ด

Daily Front_Minhhk

[REACT] StatesAirline Server ๋ณธ๋ฌธ

Code๊ฐœ๋ฐœ์ผ์ง€

[REACT] StatesAirline Server

Minhhk 2022. 12. 8. 14:18
๐Ÿ’ก Rest API ์— ์—ฐ๊ตฌ๋ฅผ ์ข€ ํ•ด๋ด์•ผ๊ฒ ๋‹ค

par1, part2 →

Search.js

import { useState } from 'react';

function Search({onSearch}) {
  const [textDestination, setTextDestination] = useState('');

  const handleChange = (e) => {
    setTextDestination(e.target.value.toUpperCase());
  };

  const handleKeyPress = (e) => {
    if (e.type === 'keypress' && e.code === 'Enter') {
      handleSearchClick();
    }
  };

  const handleSearchClick = () => {
    console.log('๊ฒ€์ƒ‰ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ฑฐ๋‚˜, ์—”ํ„ฐ๋ฅผ ์น˜๋ฉด search ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค');

    // TODO: ์ง€์‹œ์— ๋”ฐ๋ผ ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ props๋ฅผ ๋ฐ›์•„์„œ ์‹คํ–‰์‹œ์ผœ ๋ณด์„ธ์š”.
    // main.js ์—์„œ search ํ•จ์ˆ˜๋Š” ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ๋ฐ›์•„์˜จ๋‹ค -> ๊ฐ์ฒด๋กœ ํ‘œํ˜„
    // ์ถœ๋ฐœ์ง€ : "ICN" ๊ณ ์ • , ๋„์ฐฉ์ง€ input์—์„œ value={textDestination} -> ๋ฐ›์•„์˜จ ๊ฐ’ 
    onSearch({departure: "ICN", destination: textDestination})
  };

  return (
    <fieldset>
      <legend>๊ณตํ•ญ ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜๊ณ , ๊ฒ€์ƒ‰ํ•˜์„ธ์š”</legend>
      <span>์ถœ๋ฐœ์ง€</span>
      <input id="input-departure" type="text" disabled value="ICN"></input>
      <span>๋„์ฐฉ์ง€</span>
      <input
        id="input-destination"
        type="text"
        value={textDestination}
        onChange={handleChange}
        placeholder="CJU, BKK, PUS ์ค‘ ํ•˜๋‚˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”"
        onKeyPress={handleKeyPress}
      />
      <button id="search-btn" onClick={handleSearchClick}>
        ๊ฒ€์ƒ‰
      </button>
    </fieldset>
  );
}

export default Search;

main.js

import Head from 'next/head';
import { useEffect, useState } from 'react';
import { getFlight } from '../api/FlightDataApi';
import FlightList from './component/FlightList';
import LoadingIndicator from './component/LoadingIndicator';
import Search from './component/Search';
import Debug from './component/Debug';

export default function Main() {
  const [condition, setCondition] = useState({
    departure: 'ICN'
  })
  const [flightList, setFlightList] = useState(json)
  const [isloading, setloading] = useState(true)
  // ๋กœ๋”ฉ์ƒํƒœ์ผ๋•Œ๋ฅผ ๊ตฌํ˜„ , ์ƒํƒœ๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค

  const search = ({ departure, destination }) => {
    if (condition.departure !== departure || condition.destination !== destination) {
      console.log('condition ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝ์‹œํ‚ต๋‹ˆ๋‹ค')

      // TODO:
      setCondition({departure, destination})
    }
  }

// useEffect๋กœ ์ƒํƒœ๋ฅผ ๋ณ€ํ™”ํ•˜๋Š” condition์„ FlightDataApi์˜ getflight๋กœ ๋„˜๊ฒจ์ค€๋‹ค

  useEffect(async() =>{
    setloading(true)
    setFlightList(await getFlight(condition))
    // ํ•„ํ„ฐ๋ง ์กฐ๊ฑด์ธ ๊ฐ์ฒด๋ฅผ ์ธ์ž๋กœ ๋„ฃ์–ด์ค€๋‹ค
    setloading(false)
  }, [condition])
  // ๊ฒ€์ƒ‰ ์กฐ๊ฑด์ด ๋ฐ”๋€” ๋•Œ ๋งˆ๋‹ค useEffect ์‹คํ–‰
  // useEffect์—์„œ ์–ด๋–ค ๊ฐ’์ด ๋ฐ”๋€”๋•Œ๋งŒ ํ˜ธ์ถœํ•˜๊ณ ์‹ถ๋‹ค๋ฉด ๋ฐฐ์—ด ์•ˆ์—
  // useEffect๋กœ ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ๋Š” ์ƒํƒœ์ธ condition์„ ๋„ฃ์–ด์ค€๋‹ค

 //useEffect .then

 useEffect(() =>{
	setloading(true)
 	getFlight(condition) // local์„œ๋ฒ„๊ฐ€ ์•„๋‹Œ ์ง„์งœ์„œ๋ฒ„์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜จ๋‹ค
   .then(filtered =>{
     setFlightList(filtered)
     setloading(false)
    })
 }, [condition])

  global.search = search // ์‹คํ–‰์—๋Š” ์ „ํ˜€ ์ง€์žฅ์ด ์—†์ง€๋งŒ, ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด ํ•„์š”ํ•œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ์ด ์ฝ”๋“œ๋Š” ์ง€์šฐ์ง€ ๋งˆ์„ธ์š”!

return (
    <div>
      <Head>
        <title>States Airline</title>
        <link rel="icon" href="/favicon.ico" />
      </Head>

      <main>
        <h1>์—ฌํ–‰๊ฐ€๊ณ  ์‹ถ์„ ๋•, States Airline</h1>
        <Search onSearch={search}/>
        <div className="table">
          <div className="row-header">
            <div className="col">์ถœ๋ฐœ</div>
            <div className="col">๋„์ฐฉ</div>
            <div className="col">์ถœ๋ฐœ ์‹œ๊ฐ</div>
            <div className="col">๋„์ฐฉ ์‹œ๊ฐ</div>
            <div className="col"></div>
          </div>
          {/* <FlightList list={flightList.filter(filterByCondition)} /> */}
          {isLoading ? <LoadingIndicator /> : <FlightList list={flightList} />}
        </div>

        <div className="debug-area">
          <Debug condition={condition} />
        </div>
        <img id="logo" alt="logo" src="codestates-logo.png" />
      </main>
    </div>
  );
}

FlightDataApi.js

import flightList from '../resource/flightList';
import fetch from 'node-fetch';

if (typeof window !== 'undefined') { // ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ๋ Œ๋”๋ง ๋˜๊ณ  ๋‚˜๋ฉด?
  // localStorage'์— ์ •๋ณด๋ฅผ ์ €์žฅ
  localStorage.setItem('flight', JSON.stringify(flightList));
}

// filterBy = ()๋Š” ๋””ํดํŠธ ๊ฐ’
export function getFlight(filterBy = {}) {
  // HINT: ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜๊ธฐ ์œ„ํ•ด, fetch๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ตฌํ˜„์€ ์™„์ „ํžˆ ์‚ญ์ œ๋˜์–ด๋„ ์ƒ๊ด€์—†์Šต๋‹ˆ๋‹ค.
  // TODO: ์•„๋ž˜ ๊ตฌํ˜„์„ REST API ํ˜ธ์ถœ๋กœ ๋Œ€์ฒดํ•˜์„ธ์š”.

  let emString = ''
  if(filterBy.departure){
    emString = emString + `departure=${filterBy.departure}&`
  }
  if(filterBy.destination){
    emString = emString + `destination=${filterBy.destination}`
  }
  let endpoint = `http://ec2-13-124-90-231.ap-northeast-2.compute.amazonaws.com:81/flight?${emString}`

  return fetch(endpoint)
    .then(res => res.json()) // ์‘๋‹ต๊ฐ์ฒด์—์„œ JSON ํ˜•์‹์—์„œ ๊บผ๋‚ด์˜ด

}

'Code๊ฐœ๋ฐœ์ผ์ง€' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Web Server/ mininode Server  (0) 2022.12.08
Web Server (SOP, CORS)  (0) 2022.12.08
[React] ํด๋ผ์ด์–ธํŠธ AJAX,, useEffect( )  (0) 2022.12.07
postman (OpenWeather API)  (0) 2022.12.05
REST API  (0) 2022.12.02