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..084e98b 100644 --- a/src/components/App.js +++ b/src/components/App.js @@ -1,9 +1,12 @@ import React, { Component } from 'react'; - -import './App.css'; - import Header from './Header/Header'; import Compose from './Compose/Compose'; +import Post from './Post/Post'; +import axios from 'axios'; +import './App.css'; + +axios.defaults.headers.common['Content-Type'] = 'application/json'; + class App extends Component { constructor() { @@ -18,23 +21,87 @@ 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, date) { + axios.put(`http://localhost:9090/posts/${id}`, { text, date }).then(response => { + const updatedPost = response.data; - updatePost() {} + const updatedPosts = this.state.posts.map(post => { + if (post.id === updatedPost.id) { + return { post, ...updatedPost }; + } else { + return post; + } + }); - deletePost() {} + this.setState({ posts: updatedPosts }); + }); + } + + + deletePost(id) { + axios + .delete(`http://localhost:9090/posts/${id}`) + .then(res => { + this.setState({ + posts: this.state.posts.filter(p => p.id !== id) + }) + }) + } - createPost() {} + + createPost(text) { + axios + .post('http://localhost:9090/posts', { text }) + .then( results => { + this.setState({ + posts: this.state.posts.concat(results.data) + }) + }) + } + + searchPost = (text) => { + axios + .get(`http://localhost:9090/posts?q=${encodeURI(text)}`) + .then(res => { + this.setState({ + posts: res.data + }) + }) + } render() { - const { posts } = this.state; + const { posts } = this.state; return (
-
+
- + + {posts.map(post => ( + + ))}
); 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/Header/Search/Search.js b/src/components/Header/Search/Search.js index 5778c53..ccaa78a 100644 --- a/src/components/Header/Search/Search.js +++ b/src/components/Header/Search/Search.js @@ -11,7 +11,7 @@ export default class Search extends Component { return (
- + this.props.searchPostFn(e.target.value)}placeholder="Search Your Feed" />
diff --git a/src/components/Post/Edit/Edit.js b/src/components/Post/Edit/Edit.js index 29e4d05..058223a 100644 --- a/src/components/Post/Edit/Edit.js +++ b/src/components/Post/Edit/Edit.js @@ -19,7 +19,19 @@ export default class Edit extends Component { this.setState({ text: value }); } - updatePost() {} + updatePost() { + const date = new Date().toLocaleString('en-PH', { + day: 'numeric', + month: 'short', + year: 'numeric', + }); + + const { text } = this.state; + const { id, updatePostFn, hideEdit } = this.props; + updatePostFn(id,text, date); + hideEdit(); + + } render() { // More destructuring! diff --git a/src/components/Post/Post.js b/src/components/Post/Post.js index c7cb520..6ea170f 100644 --- a/src/components/Post/Post.js +++ b/src/components/Post/Post.js @@ -55,7 +55,7 @@ export default class Post extends Component { // This is destructuring! You can also think of it as being written as so: // const editing = this.state.editing // const showMasterMenu = this.state.showMasterMenu - const { editing, showMasterMenu } = this.state; + const { editing, showMasterMenu, id } = this.state; return ( // Main body of post @@ -70,7 +70,7 @@ export default class Post extends Component { style={{ display: showMasterMenu ? 'flex' : 'none' }} > Edit - Delete + this.props.deletePostFn(this.props.id)}>Delete @@ -83,7 +83,7 @@ export default class Post extends Component { DevMountain @DevMountain - - POST DATE GOES HERE + {this.props.date} {/* This is where the text goes. Notice the turnary statement. The turnary statement decides to display either the text OR the editor view @@ -97,9 +97,14 @@ export default class Post extends Component {
{// This has been pulled off of this.state via destructuring editing ? ( - + console.log(this.props.id), + ) : ( - POST TEXT GOES HERE + {this.props.text} )}