diff --git a/frontend/src/post/Post.js b/frontend/src/post/Post.js new file mode 100644 index 00000000..9c61bcc7 --- /dev/null +++ b/frontend/src/post/Post.js @@ -0,0 +1,12 @@ +import React, { Component } from 'react'; +import PropTypes from 'prop-types'; +import { Route, Link } from 'react-router-dom'; + +const Post = props => ( +
{props.post.author} - {props.post.title} - {props.post.body} - {props.post.voteScore}
+) +Post.PropTypes = { + post: PropTypes.object.isRequired +} + +export default Post diff --git a/frontend/src/post/action.js b/frontend/src/post/action.js new file mode 100644 index 00000000..6006eb49 --- /dev/null +++ b/frontend/src/post/action.js @@ -0,0 +1,17 @@ +export const GET_POSTS_BY_CATEGORY = 'GET_POSTS_BY_CATEGORY' + +const getPostsByCategory = posts => ({ + type: GET_POSTS_BY_CATEGORY, + posts + } +) + +export function fetchPostsByCategory(category) { + return function (dispatch) { + const url = `${process.env.REACT_APP_BACKEND}/${category}/posts` + return fetch( url, { headers: { 'Authorization': 'whatever-you-want' }, credentials: 'include' }) + .then((res) => res.json()) + .then(data => dispatch(getPostsByCategory(data)) + ); + }; +} \ No newline at end of file diff --git a/frontend/src/post/reducer.js b/frontend/src/post/reducer.js new file mode 100644 index 00000000..c706b273 --- /dev/null +++ b/frontend/src/post/reducer.js @@ -0,0 +1,26 @@ +import { combineReducers } from 'redux' + +import { + GET_POSTS_BY_CATEGORY, +} from './action' + +function getPostsByCategoriesReducer (state={ + isFetching: true, + posts: [] + }, action) { + const { posts } = action + console.log(posts) + switch (action.type) { + case GET_POSTS_BY_CATEGORY: + return Object.assign({}, state, { + isFetching: false, + posts + }) + default: + return state + } +} + +export default combineReducers({ + getPostsByCategoriesReducer +}) \ No newline at end of file