Skip to content

Commit

Permalink
edit post
Browse files Browse the repository at this point in the history
  • Loading branch information
gayathrithedev committed Feb 10, 2019
1 parent 5e2d36c commit 7236664
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 10 deletions.
81 changes: 81 additions & 0 deletions src/components/Editpost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import React, { Component } from "react";
import fire from "../config/Fire";

class Editpost extends Component {
constructor(props) {
super(props);
this.state = {
key: "",
author: "",
title: "",
description: ""
};
this.onSubmit = this.onSubmit.bind(this);
}

componentDidMount() {
this.setState({
key: this.props.postData.key,
title: this.props.postData.title,
description: this.props.postData.description,
author: this.props.postData.author
});
}

onChange = e => {
const state = this.state;
state[e.target.name] = e.target.value;
this.setState({ post: state });
};

onSubmit(e) {
e.preventDefault();
const { author, title, description } = this.state;
const updateRef = fire
.firestore()
.collection("posts")
.doc(this.state.key);
updateRef
.set({
title,
description,
author
})
.then(docRef => {
this.setState({
title: "",
description: ""
});
})
.catch(error => {
console.error("Error adding document: ", error);
});
}

render() {
return (
<div>
im edit post component
<div className="post">
data
<form onSubmit={this.onSubmit}>
<input
type="text"
name="title"
defaultValue={this.props.postData.title}
onChange={this.onChange}
/>
<input
type="text"
name="description"
defaultValue={this.props.postData.description}
onChange={this.onChange}
/>
<button type="submit">update me</button>
</form>
</div>
</div>
);
}
}
export default Editpost;
46 changes: 36 additions & 10 deletions src/components/Showpost.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { Component } from "react";
import fire from "../config/Fire";
import Editpost from "./Editpost";

class Showpost extends Component {
constructor(props) {
Expand All @@ -8,7 +9,9 @@ class Showpost extends Component {
this.unsubscribe = null;
this.state = {
posts: [],
keyid: ""
keyid: "",
flag: false,
clsname: ""
};
}

Expand All @@ -33,6 +36,12 @@ class Showpost extends Component {
this.unsubscribe = this.ref.onSnapshot(this.onCollectionUpdate);
}

onChange = e => {
const state = this.state;
state[e.target.name] = e.target.value;
this.setState(state);
};

singlepost = key => {
this.setState({
keyid: key
Expand All @@ -53,6 +62,12 @@ class Showpost extends Component {
});
};

editPost = id => {
this.setState({
flag: !this.state.flag
});
};

render() {
var postkey = this.state.keyid;
return (
Expand All @@ -61,7 +76,6 @@ class Showpost extends Component {
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Author</th>
</tr>
</thead>
Expand All @@ -71,9 +85,7 @@ class Showpost extends Component {
return (
<tr onClick={() => this.singlepost(post.key)}>
<td>{post.title}</td>
<td>{post.description}</td>
<td>{post.author}</td>
<td>{post.key}</td>
</tr>
);
})}
Expand All @@ -85,12 +97,26 @@ class Showpost extends Component {
if (post.key === postkey)
return (
<div>
<tr>
<td>{post.title}</td>
<td>{post.description}</td>
<td>{post.author}</td>
<td>{post.key}</td>
</tr>
<form>
<input
type="text"
value={post.title}
onChange={this.onChange}
readOnly
/>
<input
type="text"
value={post.description}
onChange={this.onChange}
readOnly
/>
</form>
<button onClick={() => this.editPost(post.key)}>
edit{post.key}
</button>
<div className="edit">
{this.state.flag ? <Editpost postData={post} /> : null}
</div>
<button onClick={() => this.deletePost(post.key)}>
delete{post.key}
</button>
Expand Down

0 comments on commit 7236664

Please sign in to comment.