Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 9 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
65 changes: 58 additions & 7 deletions src/components/App.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -18,23 +22,70 @@ 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() {}
updatePost(id, text,date) {
axios.put(`http://localhost:9090/posts/${id}`,{text,date})
.then(Response =>{
const newpost =Response.data;
console.log(newpost);
const new_post = this.state.posts.map(post =>{
if(post.id === newpost.id){
return { post,...newpost };
} else {
return post;
}
});
this.setState({ posts: new_post });
});
}

deletePost() {}
deletePost(id) {
axios.delete(`http://localhost:9090/posts/${id}`)
.then(Response => {
this.setState({
posts: this.state.posts.filter(post => post.id !== id),
});
});
}

createPost() {}
createPost(text) {
axios.post(`http://localhost:9090/posts/`,{text})
.then(Response =>{
this.setState({ posts:[Response.data,...this.state.posts] })
});
}

searchPost = (text) => {
axios.get(`http://localhost:9090/posts?q=${encodeURI(text)}`)
.then(Response =>{
this.setState({
posts: Response.data
});
});
}
render() {
const { posts } = this.state;

return (
<div className="App__parent">
<Header />
<Header SearchPostFn={this.searchPost}/>

<section className="App__content">
<Compose />
<Compose createPostFn={this.createPost}/>
{posts.map(post =>(
<Post key={post.id}
text={post.text}
date={post.date}
id={post.id}
updatePostFn={this.updatePost}
deletePostFn={this.deletePost}/>
))}
</section>
</div>
);
Expand Down
8 changes: 7 additions & 1 deletion src/components/Compose/Compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/components/Header/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default class Header extends Component {

{/* Displays the search bar */}
<div className="Header__right">
<Search />
<Search SearchPostFn={this.props.SearchPostFn}/>

{/* Displays the profile icon */}
<div className="Header__profile">
Expand Down
22 changes: 11 additions & 11 deletions src/components/Header/Search/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import { MdSearch } from 'react-icons/md';
//////////////////////////////////////////////////////// THIS COMPONENT IS BEING RENDERED IN THE *HEADER* COMPONENT

export default class Search extends Component {
render() {
return (
<section className="Search__parent">
<div className="Search__content">
<input placeholder="Search Your Feed" />
render() {
return (
<section className="Search__parent">
<div className="Search__content">
<input placeholder="Search Your Feed" onChange={e => this.props.SearchPostFn(e.target.value)}/>

<MdSearch id="Search__icon" />
</div>
</section>
);
}
}
<MdSearch id="Search__icon" />
</div>
</section>
);
}
}
9 changes: 8 additions & 1 deletion src/components/Post/Edit/Edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ export default class Edit extends Component {
this.setState({ text: value });
}

updatePost() {}
updatePost() {
const { text } =this.state;
const { id, updatePostFn, hideEdit, date } = this.props;

updatePostFn(id,text,date);

hideEdit();
}

render() {
// More destructuring!
Expand Down
13 changes: 9 additions & 4 deletions src/components/Post/Post.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 } = this.props;

return (
// Main body of post
Expand All @@ -70,7 +71,7 @@ export default class Post extends Component {
style={{ display: showMasterMenu ? 'flex' : 'none' }}
>
<span onClick={this.showEdit}>Edit</span>
<span>Delete</span>
<span onClick={() => this.props.deletePostFn(this.props.id)}>Delete</span>
</div>
</div>

Expand All @@ -83,7 +84,7 @@ export default class Post extends Component {
<span className="Post__name">DevMountain</span>
<span className="Post__handle">@DevMountain</span>

<span className="Post__date">- POST DATE GOES HERE</span>
<span className="Post__date">{date}</span>
</div>

{/* This is where the text goes. Notice the turnary statement. The turnary statement decides to display either the text OR the editor view
Expand All @@ -97,9 +98,13 @@ export default class Post extends Component {
<div className="Post__content">
{// This has been pulled off of this.state via destructuring
editing ? (
<Edit text="" hideEdit={this.hideEdit} />
<Edit
hideEdit={this.hideEdit}
id={this.props.id}
text={text} date={date}
updatePostFn={this.props.updatePostFn} />
) : (
<span className="Post__text">POST TEXT GOES HERE</span>
<span className="Post__text">{text}</span>
)}
</div>

Expand Down