diff --git a/public/articles.json b/public/api/articles.json similarity index 100% rename from public/articles.json rename to public/api/articles.json diff --git a/src/actions/index.js b/src/actions/index.js index 8d6a4b9..66fda8e 100644 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -1,9 +1,9 @@ import github from '../apis/github'; +import api from '../apis/api'; export const getUser = (username) => async (dispatch, getState) => { const response = await github.get(`/users/${username}`); - console.log(response.data); dispatch({ type: 'GET_USER', @@ -20,6 +20,20 @@ export const getRepos = (username) => async (dispatch, getState) => { }); }; +export const getArticles = async (dispatch, getState) => { + const state = getState(); + + if (!state.articles || state.articles.length <= 0){ + const response = await api.get(`/articles`); // axios request for articles json file + + dispatch({ + type: 'GET_ARTICLES', + payload: response.data, + }); + return response; + } +}; + export const updateEmailBody = (event) => async (dispatch, getState) => { // Update the email body diff --git a/src/apis/api.js b/src/apis/api.js new file mode 100644 index 0000000..d68dda0 --- /dev/null +++ b/src/apis/api.js @@ -0,0 +1,7 @@ +import axios from 'axios'; + +// Axios request used to make requests to api.github.com + +export default axios.create({ + baseURL: '/api' // Change if a real api server is created +}); \ No newline at end of file diff --git a/src/components/Articles.js b/src/components/Articles.js index 0698d2d..d220e56 100644 --- a/src/components/Articles.js +++ b/src/components/Articles.js @@ -1,7 +1,9 @@ import React from 'react'; +import { connect } from 'react-redux'; import './css/Articles.css'; import Theater from './subcomponents/Theater'; import Article from './Article'; +import { getArticles} from '../actions/index'; import { Route, Link } from 'react-router-dom'; @@ -20,7 +22,7 @@ class Articles extends React.Component { return (