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..6a520b3 100644 --- a/src/components/App.js +++ b/src/components/App.js @@ -1,9 +1,13 @@ 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,27 +22,81 @@ class App extends Component { this.createPost = this.createPost.bind(this); } - componentDidMount() {} + componentDidMount() { + axios + .get(`http://localhost:9090/posts`) + .then(response => this.setState({ posts: response.data })); + } + + updatePost(id, text) { + axios.put(`http://localhost:9090/posts/${id}`, { text }).then(response => { + const updatedPost = response.data; + + const updatedPosts = this.state.posts.map(post => { + if (post.id === updatedPost.id) { + return { post, ...updatedPost }; + } else { + return post; + } + }); + + this.setState({ posts: updatedPosts }); + }); + } + + deletePost(id) { + axios.delete(`http://localhost:9090/posts/${id}`) + .then(response => { this.setState({ + posts: this.state.posts.filter(post => post.id !== id), + }); + }); + } - updatePost() {} + createPost(text) { + const date = new Date().toLocaleString('en-US', { + day: 'numeric', + month: 'short', + year: 'numeric', + }); - deletePost() {} + axios + .post('http://localhost:9090/posts', { text, date }) + .then(response => + this.setState({ posts: [response.data, ...this.state.posts] }) + ); + } - createPost() {} + searchTextUpdate = text => { + axios + .get(`http://localhost:9090/posts?q=${encodeURI(text)}`) + .then(response => this.setState({ posts: response.data})); + } render() { const { posts } = this.state; return (
-
+
- + + {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..807958c 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 @@ -48,4 +54,4 @@ export default class Compose extends Component { ); } -} +} \ No newline at end of file diff --git a/src/components/Header/Header.js b/src/components/Header/Header.js index 8bd5f2d..f2fc375 100644 --- a/src/components/Header/Header.js +++ b/src/components/Header/Header.js @@ -9,6 +9,7 @@ import Search from './Search/Search'; export default class Header extends Component { render() { + return (
@@ -20,7 +21,7 @@ export default class Header extends Component { {/* Displays the search bar */}
- + {/* Displays the profile icon */}
@@ -31,4 +32,4 @@ export default class Header extends Component {
); } -} +} \ No newline at end of file diff --git a/src/components/Header/Search/Search.js b/src/components/Header/Search/Search.js index 5778c53..29a0322 100644 --- a/src/components/Header/Search/Search.js +++ b/src/components/Header/Search/Search.js @@ -8,14 +8,17 @@ import { MdSearch } from 'react-icons/md'; export default class Search extends Component { render() { + return (
- - + this.props.searchTextFn(e.target.value)} + />
- ); + ) } -} +} \ No newline at end of file diff --git a/src/components/Post/Edit/Edit.js b/src/components/Post/Edit/Edit.js index 29e4d05..e58018e 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! @@ -57,4 +63,4 @@ export default class Edit extends Component {
); } -} +} \ No newline at end of file diff --git a/src/components/Post/Post.js b/src/components/Post/Post.js index c7cb520..713b0bb 100644 --- a/src/components/Post/Post.js +++ b/src/components/Post/Post.js @@ -56,6 +56,7 @@ export default class Post extends Component { // const editing = this.state.editing // const showMasterMenu = this.state.showMasterMenu const { editing, showMasterMenu } = this.state; + const { id, text, date, updatePostFn, deletePostFn } = this.props; return ( // Main body of post @@ -70,7 +71,7 @@ export default class Post extends Component { style={{ display: showMasterMenu ? 'flex' : 'none' }} > Edit - Delete + deletePostFn(id)}>Delete @@ -80,13 +81,14 @@ export default class Post extends Component { - DevMountain - @DevMountain + Boom.Camp + @boom.camp - - POST DATE GOES HERE + - {date} - {/* This is where the text goes. Notice the turnary statement. The turnary statement decides to display either the text OR the editor view + {/* This is where the text goes. Notice the ternary statement. + The ternary 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 ) { @@ -97,13 +99,18 @@ export default class Post extends Component {
{// This has been pulled off of this.state via destructuring editing ? ( - + ) : ( - POST TEXT GOES HERE + {text} )}
- {/* These are all of the cute little icons in the bottom left corner */} + {/* These are all of the little icons in the bottom left corner */}
@@ -112,4 +119,4 @@ export default class Post extends Component { ); } -} +} \ No newline at end of file