๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Frontend Dev/๐Ÿฅ ์ฝ”๋“œ์Šคํ…Œ์ด์ธ  FE ๋ถ€ํŠธ์บ ํ”„

Section2 Unit9 [React] ํด๋ผ์ด์–ธํŠธ Ajax ์š”์ฒญ - StateAirline Client ๊ณผ์ œ ๋ฆฌ๋ทฐ

๋ฐ˜์‘ํ˜•


Section2 Unit9 [React] ํด๋ผ์ด์–ธํŠธ Ajax ์š”์ฒญ - StateAirline Client ๊ณผ์ œ ๋ฆฌ๋ทฐ

 

โญ๏ธ ๊ณผ์ œ. StateAirline Client

โœ”๏ธ Bare Minimum Requirement

โœ… Part 1 ํ…Œ์ŠคํŠธ ํ†ต๊ณผ

โœ… Part 2 ํ…Œ์ŠคํŠธ ํ†ต๊ณผ

 

โœ๏ธ ๊ตฌํ˜„ ๊ณผ์ • ๋…ธํŠธ

โœจ Search.js

import { useState } from "react";

function Search({ onSearch }) {
  const [textDeparture, setTextDeparture] = useState("ICN");
  const [textDestination, setTextDestination] = useState("");

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

  // const handleKeyDown = (e) => {
  //   if (e.type === "keydown" && e.code === "Enter") {
  //     handleSearchClick();
  //   }
  // };

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

    // TODO: ์ง€์‹œ์— ๋”ฐ๋ผ ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ props๋ฅผ ๋ฐ›์•„์„œ ์‹คํ–‰์‹œ์ผœ ๋ณด์„ธ์š”.
    onSearch({ departure: textDeparture, destination: textDestination });
  };

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

export default Search;

๐Ÿ’ฌ onSearch({ destination: textDestination }); ์ด๋ ‡๊ฒŒ ์ „๋‹ฌํ•ด๋„ ๋˜๋Š”๊ฑฐ ๋ชฐ๋ผ์„œ ํ•œ์ฐธ ํ—ค๋งธ๋‹ค...

๐Ÿ’ฌ form ํƒœ๊ทธ์— submit ์ด๋ฒคํŠธ๋ฅผ ์ ์šฉ์‹œ์ผœ ๊ธฐ์กด์˜ handleKeyDown ํ•จ์ˆ˜๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ๋ณ€๊ฒฝ (form ํƒœ๊ทธ ๋‚ด์—์„œ Enter๋ฅผ ๋ˆŒ๋ €์„ ๋•Œ submit ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฒคํŠธ๋ฅผ ๋ฐ”๋กœ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.) 

๐Ÿ’ฌ ๊ธฐ์กด์— ๋„์ฐฉ์ง€๋งŒ ์„ค์ •ํ•˜๋Š” ์ฝ”๋“œ์—์„œ, ์ถœ๋ฐœ์ง€๋„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ณ€๊ฒฝ

๐Ÿ’ฌ ์ถœ๋ฐœ์ง€์— ์•„๋ฌด๊ฒƒ๋„ ์ž…๋ ฅํ•˜์ง€ ์•Š์œผ๋ฉด ๋ฒ„ํŠผ์ด disabled ๋˜๋„๋ก button ํƒœ๊ทธ์— disabled ์†์„ฑ์„ ์กฐ๊ฑด๋ถ€ ๋ Œ๋”๋ง์œผ๋กœ ์ž‘์—…

 

๐Ÿ“Œ ๋„คํŠธ์›Œํฌ ์š”์ฒญ์€ ๋Š๋ฆฌ๊ณ  ๋น„์‹ธ๋‹ค!!

1. ์ „์ฒด ํ•ญ๊ณตํŽธ์„ ๋ฐ›์•„์˜จ ํ›„ ์‚ฌ์šฉ์ž ์„ค์ •ํ•œ ์กฐ๊ฑด๋Œ€๋กœ ์ „์ฒด ํ•ญ๊ณต๊ถŒ ๋ชฉ๋ก์„ ํ•„ํ„ฐ๋ง โŒ

2. ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ํ†ตํ•ด ์ „์ฒด๊ฐ€ ์•„๋‹Œ ์‚ฌ์šฉ์ž๊ฐ€ ์„ค์ •ํ•œ ์กฐ๊ฑด์— ํ•ด๋‹นํ•˜๋Š” ํ•ญ๊ณต๊ถŒ ๋ชฉ๋ก ๋ฐ›์•„์™€์„œ ๊ทธ๋Œ€๋กœ ๋ Œ๋”๋ง โญ•

 

// fetch๋กœ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
function getFlight(filterBy) {
  const departure = filterBy.departure;
  const destination = filterBy.destination;

  const data = fetch(`testurl.com/flight?departure=${departure}&destination=${destination}`)
    .then(res => res.json())

  return data;
}
useEffect(() => {
  setIsLoading(true);
  getFlight(condition)
    .then(data => setFlightList(data))
    .finally(() => {
      setIsLoading(false);
    })
}, [condition])

 


๐ŸŒ™  ์˜ค๋Š˜์˜ ํšŒ๊ณ 

 States Airline ๊ณผ์ œ๋ฅผ ๋งˆ๋ฌด๋ฆฌํ•˜๊ณ , ํ•„์š”ํ•  ๊ฒƒ ๊ฐ™์€ ๊ธฐ๋Šฅ๋“ค์„ ์ข€ ๋” ๊ตฌํ˜„ํ•ด๋ณด์•˜๋‹ค. ์‹ค์ œ๋กœ ํ™”๋ฉด์„ ๋ณด๋ฉด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์™€์„œ ๋ญ”๊ฐ€๋ฅผ ๋งŒ๋“œ๋‹ˆ ํ™•์‹คํžˆ ์žฌ๋ฏธ๊ฐ€ ์žˆ๋‹ค๊ณ  ๋Š๊ปด์กŒ๋‹ค. ๊ณผ์ œ๋ฅผ ์ œ์ถœํ•˜๊ณ , ๋‹ค์Œ ์œ ๋‹›์ธ node๋กœ server ๋งŒ๋“œ๋Š” ํŒŒํŠธ๋ฅผ ์กฐ๊ธˆ ๋ดค๋Š”๋ฐ, ์ด๊ฑด ์ •๋ง๋กœ ์ฒ˜์Œ ์ ‘ํ•˜๋Š” ๊ฐœ๋…์ด๋ผ ์ดํ•ด๋„ ์•ˆ๋˜๊ณ  ๋„ˆ๋ฌด ์–ด๋ ค์›Œ ๋ณด์˜€๋‹ค. ์˜ค๋Š˜๋ถ€ํ„ฐ ์˜ˆ์Šต์„ ํ•ด์•ผ์ง€… ๐Ÿซฅ

๋ฐ˜์‘ํ˜•