import { useNavigate } from 'react-router-dom';

import { UserContext } from '../Context.js';
import Article from '../components/Article.js';
import React, { useEffect, useState, useContext} from 'react';

import axios from 'axios';
axios.defaults.withCredentials = true;


function Button({history, children}){
  const navigate = useNavigate(); 
  return(
		<button onClick={() => {navigate('/search');}}>
			{children}
		</button>
	);
}

function Main() {
  const userContext = useContext(UserContext);
  const [articleList, setArticleList] = useState([])
  let listItem = [];
  listItem = articleList.map((article)=>{
    return(
      <Article 
      key={article._id}
      data={article}
    ></Article>
    )
  }
  )
  useEffect(() => {
    requestLoadArticle()
    .then((response) => {
      console.log(response)
      setArticleList(response.data)
    })
  }, []);

  return(
    <div className="App">
      <h1>메인 페이지 입니다.</h1>
        <div className="introduction">
          소개 내용을 담을 공간 
        </div>
        <Button>검색</Button>
        {listItem}
    </div>)
  ;
}

async function requestLoadArticle() {
	const response = await axios({
<<<<<<< HEAD
	  url: 'http://localhost:8080/article', // 통신할 웹문서
=======
	  url: 'http://localhost:8080/post/article', // 통신할 웹문서
>>>>>>> 2892e0e9c9dd1752080a370ba274deeca6254246
	  method: 'get', // 통신할 방식
	});
	return response;
  }

export default Main;