Merge pull request #94 from ClaytonWWilson/engage_microblog

Engage microblog
This commit is contained in:
asankaran35 2019-11-27 21:09:32 -05:00 committed by GitHub
commit 0c9778949a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 381 additions and 20 deletions

View File

@ -1,6 +1,8 @@
/* eslint-disable prefer-arrow-callback */
/* eslint-disable promise/always-return */
const admin = require('firebase-admin');
const { db } = require('../util/admin');
exports.putPost = (req, res) => {
const newPost = {
@ -39,11 +41,11 @@ exports.getallPostsforUser = (req, res) => {
return res.status(200).json(posts);
})
.then(function() {
res.status(200).send("Successfully retrieved all user's posts from database.");
return;
return res.status(200).json("Successfully retrieved all user's posts from database.");
})
.catch(function(err) {
res.status(500).send("Failed to retrieve user's posts from database.", err);
return res.status(500).json("Failed to retrieve user's posts from database.", err);
});
};
@ -58,14 +60,186 @@ exports.getallPosts = (req, res) => {
return res.status(200).json(posts);
})
.then(function() {
res.status(200).send("Successfully retrieved every post from database.");
return;
return res.status(200).json("Successfully retrieved every post from database.");
})
.catch(function(err) {
res.status(500).send("Failed to retrieve posts from database.", err);
return res.status(500).json("Failed to retrieve posts from database.", err);
});
};
exports.quoteWithPost = (req, res) => {
let quoteData;
const quoteDoc = admin.firestore().collection('quote').
where('userHandle', '==', req.user.handle).
where('postId', '==', req.params.postId).limit(1);
const postDoc = db.doc(`/posts/${req.params.postId}`);
postDoc.get()
.then((doc) => {
if(doc.exists) {
quoteData = doc.data();
return quoteDoc.get();
}
else
{
return res.status(404).json({error: 'Post not found'});
}
})
.then((data) => {
if(data.empty) {
return admin.firestore().collection('quote').add({
postId : req.params.postId,
userHandle : req.user.handle,
quotePost : req.body.quotePost
})
.then(() => {
return admin.firestore().collection('posts').add({
quoteData,
quoteUser : req.user.handle,
quotePost : req.body.quotePost,
quotedAt : new Date().toISOString()
})
})
}
else {
return res.status(400).json({ error: 'Post has already been quoted.' });
}
})
.catch((err) => {
return res.status(500).json({error: err});
})
}
exports.quoteWithoutPost = (req, res) => {
let quoteData;
const quoteDoc = admin.firestore().collection('quote').
where('userHandle', '==', req.user.handle).
where('postId', '==', req.params.postId).limit(1);
const postDoc = db.doc(`/posts/${req.params.postId}`);
postDoc.get()
.then((doc) => {
if(doc.exists) {
quoteData = doc.data();
return quoteDoc.get();
}
else
{
return res.status(404).json({error: 'Post not found'});
}
})
.then((data) => {
if(data.empty) {
return admin.firestore().collection('quote').add({
postId : req.params.postId,
userHandle : req.user.handle,
})
.then(() => {
return admin.firestore().collection('posts').add({
quoteData,
quoteUser : req.user.handle,
quotedAt : new Date().toISOString()
})
})
}
else {
return res.status(400).json({ error: 'Post has already been quoted.' });
}
})
.catch((err) => {
return res.status(500).json({error: 'Something is wrong'});
})
}
exports.likePost = (req, res) => {
let postData;
const likeDoc = admin.firestore().collection('likes').where('userHandle', '==', req.user.handle)
.where('postId', '==', req.params.postId).limit(1);
const postDoc = db.doc(`/posts/${req.params.postId}`);
postDoc.get()
.then((doc) => {
if(doc.exists) {
postData = doc.data();
return likeDoc.get();
}
else
{
return res.status(404).json({error: 'Post not found'});
}
})
.then((data) => {
if (data.empty) {
return admin.firestore().collection('likes').add({
postId : req.params.postId,
userHandle: req.user.handle
})
.then(() => {
postData.likeCount++;
return postDoc.update({likeCount : postData.likeCount})
})
.then(() => {
return res.status(200).json(postData);
})
}
})
.catch((err) => {
return res.status(500).json({error: 'Something is wrong'});
})
}
exports.unlikePost = (req, res) => {
let postData;
const likeDoc = admin.firestore().collection('likes').where('userHandle', '==', req.user.handle)
.where('postId', '==', req.params.postId).limit(1);
const postDoc = db.doc(`/posts/${req.params.postId}`);
postDoc.get()
.then((doc) => {
if(doc.exists) {
postData = doc.data();
return likeDoc.get();
}
else
{
return res.status(404).json({error: 'Post not found'});
}
})
.then((data) => {
return db
.doc(`/likes/${data.docs[0].id}`)
.delete()
.then(() => {
postData.likeCount--;
return postDoc.update({ likeCount: postData.likeCount });
})
.then(() => {
res.status(200).json(postData);
});
})
.catch((err) => {
console.error(err);
return res.status(500).json({error: 'Something is wrong'});
})
}
exports.getFilteredPosts = (req, res) => {
admin.firestore().collection('posts').where('userHandle', '==', 'new user').where('microBlogTopics', '==')
};

View File

@ -70,7 +70,7 @@ app.post("/removeSub", fbAuth, removeSub);
/*------------------------------------------------------------------*
* handlers/post.js *
*------------------------------------------------------------------*/
const { getallPostsforUser, getallPosts, putPost } = require("./handlers/post");
const { getallPostsforUser, getallPosts, putPost, likePost, unlikePost, quoteWithPost, quoteWithoutPost} = require("./handlers/post");
app.get("/getallPostsforUser", fbAuth, getallPostsforUser);
@ -79,6 +79,14 @@ app.get("/getallPosts", getallPosts);
// Adds one post to the database
app.post("/putPost", fbAuth, putPost);
app.get("/like/:postId", fbAuth, likePost);
app.get("/unlike/:postId", fbAuth, unlikePost);
app.post("/quoteWithPost/:postId", fbAuth, quoteWithPost);
app.post("/quoteWithoutPost/:postId", fbAuth, quoteWithoutPost);
/*------------------------------------------------------------------*
* handlers/topic.js *
*------------------------------------------------------------------*/

View File

@ -41,5 +41,5 @@
"last 1 safari version"
]
},
"proxy": "https://us-central1-twistter-e4649.cloudfunctions.net/api"
"proxy": "http://localhost:5001/twistter-e4649/us-central1/api"
}

View File

@ -15,9 +15,14 @@ import '../App.css';
import logo from '../images/twistter-logo.png';
import noImage from '../images/no-img.png';
import Writing_Microblogs from '../Writing_Microblogs';
import ReactModal from 'react-modal';
class Home extends Component {
state = {};
state = {
};
componentDidMount() {
axios
@ -27,10 +32,14 @@ class Home extends Component {
this.setState({
posts: res.data
})
this.setState({posts: (this.state.posts).sort((a,b) =>
-a.createdAt.localeCompare(b.createdAt))
})
})
.catch(err => console.log(err));
}
render() {
let authenticated = this.props.user.authenticated;
@ -45,14 +54,17 @@ class Home extends Component {
}
</Typography>
<Typography variant="h7"><b>{post.userHandle}</b></Typography>
<Typography variant="body2" color={"textSecondary"}>{post.createdAt}</Typography>
<Typography variant="body2" color={"textSecondary"}>{post.createdAt.substring(0,10) +
" " + post.createdAt.substring(11,19)}</Typography>
<br />
<Typography variant="body1"><b>{post.microBlogTitle}</b></Typography>
<Typography variant="body2">{post.body}</Typography>
<br />
<Typography variant="body2"><b>Topics:</b> {post.microBlogTopics}</Typography>
<Typography variant="body2"><b>Topics:</b> {post.microBlogTopics.join("," + " ")}</Typography>
<br />
<Typography variant="body2" color={"textSecondary"}>Likes {post.likeCount} Comments {post.commentCount}</Typography>
<Typography variant="body2" color={"textSecondary"}>Likes {post.likeCount}</Typography>
<Like microBlog = {post.postId}></Like>
<Quote microblog = {post.postId}></Quote>
</CardContent>
</Card>
)
@ -96,6 +108,7 @@ class Home extends Component {
}
}
const mapStateToProps = (state) => ({
user: state.user
})
@ -104,4 +117,167 @@ Home.propTypes = {
user: PropTypes.object.isRequired
}
class Quote extends Component {
constructor(props) {
super(props);
this.state = {
characterCount: 250,
showModal: false,
value: ""
}
this.handleSubmitWithoutPost = this.handleSubmitWithoutPost.bind(this);
this.handleOpenModal = this.handleOpenModal.bind(this);
this.handleCloseModal = this.handleCloseModal.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmitWithoutPost(event) {
const headers = {
headers: { "Content-Type": "application/json" }
};
axios.post(`/quoteWithoutPost/${this.props.microblog}`, headers)
.then((res) => {
console.log(res.data);
})
.catch(err => {
console.error(err);
});
event.preventDefault();
}
handleOpenModal() {
this.setState({ showModal: true });
}
handleCloseModal() {
this.setState({ showModal: false });
}
handleChangeforPost(event) {
this.setState({ value: event.target.value });
}
handleChangeforCharacterCount(event) {
const charCount = event.target.value.length;
const charRemaining = 250 - charCount;
this.setState({ characterCount: charRemaining });
}
handleSubmit(event) {
const quotedPost = {
quotePost: this.state.value,
};
const headers = {
headers: { "Content-Type": "application/json" }
};
axios.post(`/quoteWithPost/${this.props.microblog}`, quotedPost, headers)
.then((res) => {
console.log(res.data);
})
.catch(err => {
console.error(err);
});
event.preventDefault();
this.setState({ showModal: false, characterCount: 250, value: "" });
}
render() {
return (
<div>
<button onClick={this.handleOpenModal}>Quote with Post</button>
<ReactModal
isOpen={this.state.showModal}
style={{content: {height: "50%", width: "25%", marginTop: "auto", marginLeft: "auto", marginRight: "auto", marginBottom : "auto"}}}
>
<div style={{ width: "200px", marginLeft: "50px" }}>
<form>
<textarea
value={this.state.value}
required
maxLength="250"
placeholder="Write Quoted Post here..."
onChange={e => {
this.handleChangeforPost(e);
this.handleChangeforCharacterCount(e);
}}
cols={40}
rows={20}
/>
<div style={{ fontSize: "14px", marginRight: "-100px" }}>
<p2>Characters Left: {this.state.characterCount}</p2>
</div>
<button onClick={this.handleSubmit}>Share Quoted Post</button>
<button onClick={this.handleCloseModal}>Cancel</button>
</form>
</div>
</ReactModal>
<button onClick={this.handleSubmitWithoutPost}>Quote without Post</button>
</div>
)
}
}
class Like extends Component {
constructor(props) {
super(props)
this.state = {
like: false
}
this.handleClick = this.handleClick.bind(this);
}
handleClick(){
this.setState({
like: !this.state.like
});
if(this.state.like == false)
{
axios.get(`/like/${this.props.microBlog}`)
.then((res) => {
console.log(res.data);
})
.catch((err) => {
console.log(err);
})
}
else
{
axios.get(`/unlike/${this.props.microBlog}`)
.then((res) => {
console.log(res.data);
})
.catch((err) => {
console.log(err);
})
}
}
render() {
const label = this.state.like ? 'Unlike' : 'Like'
return(
<div>
<button onClick={this.handleClick}>{label}</button>
</div>
)
}
}
export default connect(mapStateToProps)(Home);

View File

@ -131,7 +131,10 @@ class user extends Component {
// console.log(res.data);
this.setState({
posts: res.data
});
})
this.setState({posts: (this.state.posts).sort((a,b) =>
-a.createdAt.localeCompare(b.createdAt))
})
})
.catch(err => console.log(err));
}
@ -200,19 +203,19 @@ class user extends Component {
<Typography variant="body2" color={"textSecondary"}>
{post.createdAt}
</Typography>
<Typography variant="h7"><b>{post.userHandle}</b></Typography>
<Typography variant="body2" color={"textSecondary"}>{post.createdAt.substring(0,10) +
" " + post.createdAt.substring(11,19)}</Typography>
<br />
<Typography variant="body1">
<b>{post.microBlogTitle}</b>
</Typography>
<Typography variant="body2">{post.body}</Typography>
<br />
<Typography variant="body2">
<b>Topics:</b> {post.microBlogTopics}
</Typography>
<Typography variant="body2"><b>Topics:</b> {post.microBlogTopics.join("," + " ")}</Typography>
<br />
<Typography variant="body2" color={"textSecondary"}>
Likes {post.likeCount} Comments {post.commentCount}
</Typography>
<Typography variant="body2" color={"textSecondary"}>Likes {post.likeCount}</Typography>
</CardContent>
</Card>
))