From 930708ffec6a19a32a31a5ac31a59792804206f0 Mon Sep 17 00:00:00 2001 From: Camerin Figueroa Date: Mon, 25 Oct 2021 18:13:04 -0400 Subject: [PATCH] Added some api and state actions --- public/{ => api}/articles.json | 0 src/actions/index.js | 16 +++++++++++++++- src/apis/api.js | 7 +++++++ src/components/Articles.js | 10 ++++++++-- .../{articleReducer.js => articlesReducer.js} | 4 ++-- src/reducers/index.js | 2 ++ 6 files changed, 34 insertions(+), 5 deletions(-) rename public/{ => api}/articles.json (100%) create mode 100644 src/apis/api.js rename src/reducers/{articleReducer.js => articlesReducer.js} (64%) 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 (
- "Articles" + {this.props.getArticles()}
); } @@ -49,5 +51,9 @@ class Articles extends React.Component { } } +const mapStateToProps = (state) => { + return { articles: state.articles.articles}; +} + +export default connect(mapStateToProps, { getArticles })(Articles); -export default Articles; \ No newline at end of file diff --git a/src/reducers/articleReducer.js b/src/reducers/articlesReducer.js similarity index 64% rename from src/reducers/articleReducer.js rename to src/reducers/articlesReducer.js index 989b804..7a07890 100644 --- a/src/reducers/articleReducer.js +++ b/src/reducers/articlesReducer.js @@ -1,4 +1,4 @@ -let articleReducer = (state={articles=[]}, action) => { +let articlesReducer = (state={articles: []}, action) => { switch(action.type) { case "GET_ARTICLES": return { ...state, articles: action.payload }; @@ -9,4 +9,4 @@ let articleReducer = (state={articles=[]}, action) => { }; -export default articleReducer; \ No newline at end of file +export default articlesReducer; \ No newline at end of file diff --git a/src/reducers/index.js b/src/reducers/index.js index 057fa09..c7375a8 100644 --- a/src/reducers/index.js +++ b/src/reducers/index.js @@ -4,10 +4,12 @@ import githubReducer from './githubReducer'; import contactModalReducer from "./contactModalReducer"; import introReducer from "./introReducer"; import navigationReducer from "./navigationReducer"; +import articlesReducer from "./articlesReducer"; export default combineReducers({ github: githubReducer, contactModal: contactModalReducer, intro: introReducer, navigation: navigationReducer, + articles: articlesReducer, });