Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- @redux-toolkit
- UI
- https://www.daleseo.com/js-array-slice-splice/
- slice/splice/split
- JS#3์ผ์ฐจ๋ฌ๋ฆฌ์#์ด๋ฐ์ธ๋ฐ#์๊ฐ๊ธ๋ฐฉ~
- for~in/for~of
- ํท๊ฐ๋ฆฐ๋ค~
- ์๋ฐ์คํฌ๋ฆฝํธ#์กฐ๊ฑด๋ฌธ#๋ฌธ์์ด
- variable#function
- react
- js
- redux์ํ์ ์ง
- ์๋ฐ์คํฌ๋ฆฝํธ#JS#var#let#const#undefined#null
- https://developer-talk.tistory.com/299
- toString#String
- removeCookie
- ์๋ฐ์คํฌ๋ฆฝํธ
- https://lo-victoria.com/introduction-to-redux-toolkit-for-beginners
- CSS
- UX
- https://dasima.xyz/%EC%9E%90%EB%B0%94%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8-%EC%A0%9C%EA%B3%B1-math-pow-%EA%B3%84%EC%82%B0/
- Beesbeesbees
- ์๋ฐ์คํฌ๋ฆฝํธ#JS#slice#splice
- ๋ด์ฅ๊ณ ์ฐจํจ์
- children vs childrenNodes
- User Flow
- cmarket
- ๋ ธ๋๊ต๊ณผ์
- dom
- ใทใ
Archives
- Today
- Total
Daily Front_Minhhk
[REACT] StatesAirline Server ๋ณธ๋ฌธ
๐ก 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 |