From 3b3f235aca605deb7d080b1cae535a6f1a7794a8 Mon Sep 17 00:00:00 2001 From: EJBarba Date: Mon, 24 Jun 2019 17:48:28 +0800 Subject: [PATCH] submission, no black diamond --- package.json | 1 + src/components/App.js | 50 +++++++++++++++++++++++++++---- src/components/Compose/Compose.js | 8 ++++- src/components/Post/Edit/Edit.js | 8 ++++- src/components/Post/Post.js | 42 +++++++++----------------- 5 files changed, 73 insertions(+), 36 deletions(-) diff --git a/package.json b/package.json index 490d2cc..ec796ee 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "private": true, "homepage": "https://boomcamp.github.io/react-3/", "dependencies": { + "axios": "^0.19.0", "faker": "^4.1.0", "json-server": "^0.15.0", "react": "^16.8.6", diff --git a/src/components/App.js b/src/components/App.js index b48f641..57a0f41 100644 --- a/src/components/App.js +++ b/src/components/App.js @@ -1,9 +1,14 @@ import React, { Component } from 'react'; +import axios from 'axios'; import './App.css'; + import Header from './Header/Header'; import Compose from './Compose/Compose'; +import Post from './Post/Post'; + +axios.defaults.headers.common['Content-Type'] = 'application/json'; class App extends Component { constructor() { @@ -18,13 +23,35 @@ class App extends Component { this.createPost = this.createPost.bind(this); } - componentDidMount() {} + componentDidMount() { + axios.get('https://practiceapi.devmountain.com/api/posts').then(results => { + this.setState({ posts: results.data }); + }); + } - updatePost() {} + updatePost(id, text) { + axios + .put(`https://practiceapi.devmountain.com/api/posts?id=${id}`, { text }) + .then(results => { + this.setState({ posts: results.data }); + }); + } - deletePost() {} + deletePost(id) { + axios + .delete(`https://practiceapi.devmountain.com/api/posts?id=${id}`) + .then(results => { + this.setState({ posts: results.data }); + }); + } - createPost() {} + createPost(text) { + axios + .post('https://practiceapi.devmountain.com/api/posts', { text }) + .then(results => { + this.setState({ posts: results.data }); + }); + } render() { const { posts } = this.state; @@ -34,11 +61,22 @@ class App extends Component {
- + + + {posts.map(post => ( + + ))}
); } } -export default App; +export default App; \ No newline at end of file diff --git a/src/components/Compose/Compose.js b/src/components/Compose/Compose.js index 57a3aed..01713fb 100644 --- a/src/components/Compose/Compose.js +++ b/src/components/Compose/Compose.js @@ -20,7 +20,13 @@ export default class Compose extends Component { this.setState({ text }); } - createPost() {} + createPost() { + const { text } = this.state; + const { createPostFn } = this.props; + + createPostFn( text ); + this.setState({ text: '' }); + } render() { // Destructuring diff --git a/src/components/Post/Edit/Edit.js b/src/components/Post/Edit/Edit.js index 29e4d05..6e9d321 100644 --- a/src/components/Post/Edit/Edit.js +++ b/src/components/Post/Edit/Edit.js @@ -19,7 +19,13 @@ export default class Edit extends Component { this.setState({ text: value }); } - updatePost() {} + updatePost() { + const { text } = this.state; + const { id, updatePostFn, hideEdit } = this.props; + + updatePostFn( id, text ); + hideEdit(); + } render() { // More destructuring! diff --git a/src/components/Post/Post.js b/src/components/Post/Post.js index c7cb520..c1933c7 100644 --- a/src/components/Post/Post.js +++ b/src/components/Post/Post.js @@ -56,54 +56,40 @@ export default class Post extends Component { // const editing = this.state.editing // const showMasterMenu = this.state.showMasterMenu const { editing, showMasterMenu } = this.state; + const { text, date } = this.props; return ( - // Main body of post
{/* Three dots in top right corner */}
- - {/* Drop-down menu. Remember that the "showMasterMenu" variable has been destructured off of this.state */} -
Edit - Delete + this.props.deletePostFn(this.id)}>Delete {/* Remember to destructure deletePostFn off of props or use this.props.deletePostFn */}
- - {/* This is where all the meta data of the post will go (who, when, where) */}
- - DevMountain - @DevMountain - - - POST DATE GOES HERE + Boom.Camp + @boom.camp + - {date}
- - {/* This is where the text goes. Notice the turnary statement. The turnary statement decides to display either the text OR the editor view - You can also think of it as being written as so: - if( this.state.editing === true ) { - - } else { - - } - */}
- {// This has been pulled off of this.state via destructuring - editing ? ( - + {editing ? ( + ) : ( - POST TEXT GOES HERE + {text} )}
- - {/* These are all of the cute little icons in the bottom left corner */}