Skip to content
Snippets Groups Projects
Main.js 2.06 KiB
Newer Older
import { useNavigate } from 'react-router-dom';
import { UserContext } from '../Context.js';
import Article from '../components/Article.js';
Gwangbin's avatar
Gwangbin committed
import React, { useEffect, useState, useContext } from 'react';
import '../css/Main.css';
import axios from 'axios';
axios.defaults.withCredentials = true;

Gwangbin's avatar
Gwangbin committed
function Button({ history, children }) {
  const navigate = useNavigate();
  return (
    <button style={{ width: '40px', height: '30px' }} onClick={() => { navigate('/search'); }}>
      {children}
    </button>
  );
function Main() {
  const userContext = useContext(UserContext);
Gwangbin's avatar
Gwangbin committed
  const [articleList, setArticleList] = useState([]);
  let listItem = [];
Gwangbin's avatar
Gwangbin committed
  listItem = articleList.map((article) => {
    return (
      <Article
        key={article._id}
        data={article}
      ></Article>
Hyun Woo Jeong's avatar
Hyun Woo Jeong committed
  useEffect(() => {
Gwangbin's avatar
Gwangbin committed
    requestSortArticle("time")
      .then((response) => {
        console.log(response)
        setArticleList(response.data)
      })
  }, []);
Hyun Woo Jeong's avatar
Hyun Woo Jeong committed
  const handleSortChange = (event) => {
    requestSortArticle(event.target.value)
Gwangbin's avatar
Gwangbin committed
      .then((response) => {
        console.log(response)
        setArticleList(response.data)
      })
  };
Gwangbin's avatar
Gwangbin committed
  return (
    <div className="App">
Gwangbin's avatar
Gwangbin committed
      <h1>모두의 食道樂</h1>
      <div className="intro">
Gwangbin's avatar
Gwangbin committed
        가보고 싶은 식당을 찾아보고, 가본 식당을 기록해보세요!
      </div>
      <div style={{ display: 'flex' }}>
        <select id="addInputPrior" defaultValue={"time"} onChange={handleSortChange}>
          <option value="time">최신순</option>
          <option value="like">인기순</option>
        </select>
      </div>
      {listItem}
    </div>);
async function requestLoadArticle() {
Gwangbin's avatar
Gwangbin committed
  const response = await axios({
    url: 'http://localhost:8080/article', // 통신할 웹문서
    method: 'get', // 통신할 방식
  });
  return response;
}
Gwangbin's avatar
Gwangbin committed
async function requestSortArticle(crit) {
  const response = await axios({
    url: `http://localhost:8080/article/sort/${crit}`, // 통신할 웹문서
    method: 'get', // 통신할 방식
  });
  return response;
}
export default Main;