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..63be748 100644 --- a/src/components/App.js +++ b/src/components/App.js @@ -1,40 +1,101 @@ -import React, { Component } from 'react'; +import React, { Component } from "react"; +import "./App.css"; +import axios from "axios"; -import './App.css'; +import Header from "./Header/Header"; +import Compose from "./Compose/Compose"; +import Post from "./Post/Post"; -import Header from './Header/Header'; -import Compose from './Compose/Compose'; +axios.defaults.headers.common["Content-Type"] = "application/json"; class App extends Component { constructor() { super(); this.state = { - posts: [], + posts: [] }; this.updatePost = this.updatePost.bind(this); this.deletePost = this.deletePost.bind(this); this.createPost = this.createPost.bind(this); + this.searchPost = this.searchPost.bind(this); } - componentDidMount() {} + componentDidMount() { + axios + .get("https://practiceapi.devmountain.com/api/posts") + .then(response => this.setState({ posts: response.data })); + } + + updatePost(id, text) { + axios + .put(`https://practiceapi.devmountain.com/api/posts?id=${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; + } + }); - updatePost() {} + this.setState({ posts: updatedPosts }); + }); + } - deletePost() {} + deletePost(id) { + axios + .delete(`https://practiceapi.devmountain.com/api/posts?id=${id}`) + .then(response => { + this.setState({ + posts: this.state.posts.filter(post => post.id !== id) + }); + }); + } - createPost() {} + createPost(text) { + axios + .post("https://practiceapi.devmountain.com/api/posts", { text }) + .then(results => { + this.setState({ posts: results.data }); + }); + } + + searchPost(text) { + if (text) { + axios + .get( + `https://practiceapi.devmountain.com/api/posts/filter?text=${text}` + ) + .then(response => { + this.setState({ + posts: response.data + }); + }); + } + } render() { const { posts } = this.state; return (
-
+
- + + {posts.map(post => ( + + ))}
); diff --git a/src/components/Compose/Compose.js b/src/components/Compose/Compose.js index 57a3aed..73f8492 100644 --- a/src/components/Compose/Compose.js +++ b/src/components/Compose/Compose.js @@ -20,8 +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 const { text } = this.state; diff --git a/src/components/Header/Header.js b/src/components/Header/Header.js index 8bd5f2d..4c552f4 100644 --- a/src/components/Header/Header.js +++ b/src/components/Header/Header.js @@ -20,7 +20,7 @@ export default class Header extends Component { {/* Displays the search bar */}
- + {/* Displays the profile icon */}
diff --git a/src/components/Header/Search/Search.js b/src/components/Header/Search/Search.js index 5778c53..12a22c8 100644 --- a/src/components/Header/Search/Search.js +++ b/src/components/Header/Search/Search.js @@ -1,8 +1,8 @@ -import React, { Component } from 'react'; +import React, { Component } from "react"; -import './Search.css'; +import "./Search.css"; -import { MdSearch } from 'react-icons/md'; +import { MdSearch } from "react-icons/md"; //////////////////////////////////////////////////////// THIS COMPONENT IS BEING RENDERED IN THE *HEADER* COMPONENT @@ -11,7 +11,10 @@ export default class Search extends Component { return (
- + this.props.searchPost(e.target.value)} + />
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..7d37e13 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 { text, date, id, 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,10 +81,10 @@ export default class Post extends Component { - DevMountain - @DevMountain + Vince Ludovice + @vince.ludovice - - 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 @@ -97,9 +98,14 @@ export default class Post extends Component {
{// This has been pulled off of this.state via destructuring editing ? ( - + ) : ( - POST TEXT GOES HERE + {text} )}