From 04a64d30262219bee116c9a59645570bc0de8103 Mon Sep 17 00:00:00 2001 From: Asara Date: Sat, 19 Oct 2019 23:13:17 -0400 Subject: [PATCH] Working blog post! --- src/actions/index.js | 11 +++++++++++ src/components/NewPost.js | 19 +++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/actions/index.js b/src/actions/index.js index 50f3a8c..c4a7f5f 100644 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -35,3 +35,14 @@ export const userLogin = (username, password) => async (dispatch) => { export const loadCookieToState = (data) => async (dispatch) => { dispatch({ type: 'LOAD_COOKIE', data }) }; + + +export const newBlogPost = (payload) => async (dispatch) => { + const response = await sudoscientist.post('/blog', payload) + if (response.status === 401) { + dispatch({ type: 'BLOG_POST_FAILED', payload: null }) + } + if (response.status === 200) { + dispatch({ type: 'BLOG_POST_CREATED', payload: response }) + } +}; diff --git a/src/components/NewPost.js b/src/components/NewPost.js index b57fcca..660de4c 100644 --- a/src/components/NewPost.js +++ b/src/components/NewPost.js @@ -1,22 +1,27 @@ import React from 'react'; +import { useSelector } from "react-redux"; import ReactMde from "react-mde"; import ReactMarkdown from 'react-markdown'; +import { connect } from 'react-redux'; import 'github-markdown-css' import "react-mde/lib/styles/css/react-mde-all.css" +import { newBlogPost } from '../actions'; const NewPost = (props) => { const [title, setTitle] = React.useState(""); const [content, setContent] = React.useState(""); const [tags, setTags] = React.useState(""); const [selectedTab, setSelectedTab] = React.useState("write"); + const username = useSelector(state => state.auth.username); const submitPost = () => { const payload = { title: title, content: content, tags: tags, + author: username } - console.log(payload) + props.newBlogPost(payload) } return( @@ -49,4 +54,14 @@ const NewPost = (props) => { ) } -export default NewPost; +const mapStateToProps = (state) => { + return { + user: state.auth, + }; +} + +export default connect( + mapStateToProps, + { newBlogPost } +)(NewPost); +