[TIL] Day35 #StatesAirline

Beanxxยท2022๋…„ 6์›” 15์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
35/120
post-thumbnail

2022.06.15(Wed)

[TIL] Day35
[SEB FE] Day35

โ˜‘๏ธย [Pair] StatesAirline

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-06-15 แ„‹แ…ฉแ„’แ…ฎ 3 20 25
// Main.js
export default function Main() {
  // ํ•ญ๊ณตํŽธ ๊ฒ€์ƒ‰ ์กฐ๊ฑด์„ ๋‹ด๊ณ  ์žˆ๋Š” ์ƒํƒœ
  const [condition, setCondition] = useState({});
  
  // ํ•ญ๊ณตํŽธ ๋ชฉ๋ก ์ƒํƒœ
  const [flightList, setFlightList] = useState([]);
  
  // 
  const [isLoading, setIsLoading] = useState(true);

  // ์ฃผ์–ด์ง„ ๊ฒ€์ƒ‰ ํ‚ค์›Œ๋“œ์— ๋”ฐ๋ผ condition ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝ์‹œ์ผœ์ฃผ๋Š” ํ•จ์ˆ˜
  const search = ({ departure, destination }) => {
    if (
      condition.departure !== departure ||
      condition.destination !== destination
    ) {
      setCondition({ departure: departure, destination: destination });
      // key&value๊ฐ€ ๊ฐ™์œผ๋ฏ€๋กœ setCondition({departure, destination}) ๋ผ๊ณ  ์จ๋„ ๊ฐ™์Œ
    }
  };

  // condition ์ƒํƒœ๊ฐ€ ๋‹ฌ๋ผ์งˆ ๋•Œ ๋งˆ๋‹ค AJAX ์š”์ฒญ
  useEffect(() => {
    // ๋กœ๋”ฉ ํ™”๋ฉด O
    setIsLoading(true);
    // ์‘๋‹ต ์„ฑ๊ณต์‹œ ์‘๋‹ต์— ๋Œ€ํ•œ ํ•ญ๊ณตํŽธ ๋ชฉ๋ก์„ ๋ณ€๊ฒฝํ•˜๊ณ  ๋กœ๋”ฉํ™”๋ฉด X
    getFlight(condition).then((response) => {
      setFlightList(response),
        setIsLoading(false);
    });
  }, [condition]);

  return (
    <div className="container">
      <main>
        <Search
          onSearch={search}
          condition={condition}
          setCondition={setCondition}
        />
        <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>
          {/* ๋กœ๋”ฉ์ค‘์ด๋ฉด ๋กœ๋”ฉ ํ™”๋ฉด ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ  ์•„๋‹ˆ๋ฉด ํ•ญ๊ณตํŽธ ๋ชฉ๋ก์„ ๋ณด์—ฌ์คŒ */}
          {isLoading ? <LoadingIndicator /> : <FlightList list={flightList} />}
          {/* FlightList ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ณด๋ฉด list๋ฅผ props๋กœ ๋ฐ›์•„์™”์œผ๋ฏ€๋กœ ์—ฌ๊ธฐ์„œ list={flightList}๋กœ FlightList์— ์ „๋‹ฌํ•ด์ฃผ๋Š” ๊ฒƒ */}
        </div>
      </main>
    </div>
  );
}

// Search.js
function Search({ onSearch, condition, setCondition }) {
  // ์ถœ๋ฐœ์ง€ ์ƒํƒœ
  const [textDeparture, setTextDeparture] = useState("");
  // ๋„์ฐฉ์ง€ ์ƒํƒœ
  const [textDestination, setTextDestination] = useState("");

  // ๋„์ฐฉ์ง€ event handler
  const handleChange = (e) => {
    setTextDestination(e.target.value.toUpperCase());
  };

  // ์ถœ๋ฐœ์ง€ event handler
  const handleChange2 = (e) => {
    setTextDeparture(e.target.value.toUpperCase());
  };

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

  const handleSearchClick = () => {
    onSearch({ departure: textDeparture, destination: textDestination });
  };

  return (
    <fieldset className="search-css">
      <legend>๊ณตํ•ญ ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜๊ณ , ๊ฒ€์ƒ‰ํ•˜์„ธ์š”</legend>
      <span>์ถœ๋ฐœ์ง€</span>
      <input
        id="input-departure"
        type="text"
        value={textDeparture}
        onChange={handleChange2}
        onKeyPress={handleKeyPress}
        placeholder="ICN, CJU ์ค‘ ํ•˜๋‚˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”"
      ></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;
// FlightList.js
function FlightList({ list = [] }) {
  if (list.length === 0) {
    return <div className="merge-col">๋ชฉ๋ก์ด ์—†์Šต๋‹ˆ๋‹ค</div>;
  }

  return list.map(
    ({ uuid, departure, destination, departure_times, arrival_times }) => {
      return (
        <Flight
          key={uuid}
          departure={departure}
          destination={destination}
          departureTimes={departure_times}
          arrivalTimes={arrival_times}
        />
      );
    }
  );
}

export default FlightList;
// FlightDataApi.js
import fetch from "node-fetch";

if (typeof window !== "undefined") {
  localStorage.setItem("flight", JSON.stringify(flightList));
}

export function getFlight(filterBy = {}) {
  console.log(filterBy);
  // ์ถœ๋ฐœ์ง€ ์„ ํƒ์„ ์•ˆํ•˜๊ณ  ์ฝ˜์†”์„ ์ฐ์œผ๋ฉด filterBy์— {departure: "ICN"}์ด ์ฐํž˜
  // ์ถœ๋ฐœ์ง€๋ฅผ ์„ ํƒํ•˜๊ณ  ์ฝ˜์†”์„ ์ฐ์œผ๋ฉด {departure: "ICN", destination: "~"}๊ฐ€ ์ฐํž˜
  
  // REST API ํ˜ธ์ถœ
  // ๋ชจ๋“  ๋ฐ์ดํ„ฐ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ด
  const url = `http://ec2-13-124-90-231.ap-northeast-2.compute.amazonaws.com:81/flight`;

  // ๊ฐ์ฒด์˜ key
  if (filterBy.destination === undefined) {
    return fetch(url).then((res) => res.json());
  }

  return fetch(
    `${url}?departure=${filterBy.departure}&destination=${filterBy.destination}`
  ).then((res) => res.json());
}

โ˜‘๏ธย Quiz

โžฐย Element: React ์•ฑ์˜ ๊ฐ€์žฅ ์ž‘์€ ๋‹จ์œ„
โžฐย Component: ์—ฌ๋Ÿฌ ์—˜๋ฆฌ๋จผํŠธ์™€ ํ•จ์ˆ˜๋“ค์ด ๋ชจ์—ฌ ์žˆ๋Š” ๊ธฐ๋Šฅ์  ๋‹จ์œ„ (ํ•˜๋‚˜์˜ element)


โœ…ย ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Lv.1 Programmers 3๋ฌธ์ œ ํ’€์ด(๋‘ ์ •์ˆ˜ ์‚ฌ์ด์˜ ํ•ฉ, ๋ฌธ์ž์—ด์„ ์ •์ˆ˜๋กœ ๋ฐ”๊พธ๊ธฐ, ์„œ์šธ์—์„œ ๊น€์„œ๋ฐฉ ์ฐพ๊ธฐ)
โœ…ย Youtube ๋ณ„์ฝ”๋”ฉ - useEffect ๊ฐ•์˜ ๋“ฃ๊ณ , ์ฝ”๋“œ ์—ฐ์Šต
โœ…ย ํ•œ์ž… ํฌ๊ธฐ๋กœ ์ž˜๋ผ ๋จน๋Š” ๋ฆฌ์•กํŠธ(React.js) - JS ๊ธฐ๋ณธ ๊ฐ•์˜ ๋“ฃ๊ธฐ

Ajax ์š”์ฒญํ•˜๋Š” ๋ถ€๋ถ„ fetch ์‚ฌ์šฉํ•˜๋Š” ์ชฝ๋„ ์–ด๋ ค์› ๊ณ , ์ปดํฌ๋„ŒํŠธ ๋ถ„๋ฆฌ๊ฐ€ ๋˜์–ด์žˆ์–ด์„œ ์—ฌ๊ธฐ์ €๊ธฐ ์–ด๋””์„œ ์–ด๋–ค ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ• ์ง€ ์–ด๋ ค์› ๋˜.. ์ฒ˜์Œ๋ถ€ํ„ฐ ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•ด๋ณด๋ฉด์„œ ํŒŒ์ผ ๊ตฌ์กฐ์™€ ๋ฐ์ดํ„ฐ๋ฅผ ์–ด๋–ค ์‹์œผ๋กœ ์ฃผ๊ณ ๋ฐ›๋Š”์ง€ ์—ฐ์Šต์ด ๋งŽ์ด ํ•„์š”ํ• ๋“ฏ ํ•˜๋‹ค.. fetch ์‚ฌ์šฉ ๋ถ€๋ถ„๋„ ๋ณต์Šต๋ณต์Šต ๐Ÿซ 

profile
FE developer

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