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 ํ์์์ ๊บผ๋ด์ด
}