# 항공권 목록 필터링
export default function Main() {
const [condition, setCondition] = useState({
departure: 'ICN',
});
const [flightList, setFlightList] = useState(getFlight);
const [isLoading,setIsLoading] = useState(true);
const search = ({ departure, destination }) => {
if (
condition.departure !== departure ||
condition.destination !== destination
) {
setCondition({departure,destination})
}
};
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 = () => {
onSearch({departure : "ICN",destination : textDestination})
};
# AJAX 요청
const [flightList, setFlightList] = useState();
const [isLoading,setIsLoading] = useState(true);
useEffect(() => {
setIsLoading(true);
getFlight(condition)
.then(data =>{
setFlightList(data);
setIsLoading(false);
})
}, [condition])
useEffect(async() =>{
setloading(true)
setFlightList(await getFlight(condition))
setloading(false)
}, [condition])
{isLoading
? <LoadingIndicator />
: <FlightList list={flightList} />
}
export function getFlight(filterBy = {}) {
let filtered = Boolean(filterBy.destination);
return(
filtered
? fetch(`http://ec2-13-124-90-231.ap-northeast-2.compute.amazonaws.com:81/flight?departure=ICN&destination=${filterBy.destination}`)
.then((res) => res.json())
:fetch(`http://ec2-13-124-90-231.ap-northeast-2.compute.amazonaws.com:81/flight`)
.then((res) => res.json())
)
let queryString = '';
if (filterBy.departure) {
queryString = queryString + `departure=${filterBy.departure}&`;
}
if (filterBy.destination) {
queryString = queryString + `destination=${filterBy.destination}`;
}
let endpoint = `http://ec2-13-124-90-231.ap-northeast-2.compute.amazonaws.com:81/flight?${queryString}`;
return fetch(endpoint).then((resp) => resp.json());