/* eslint-disable */ import React, { Component } from "react"; import PropTypes from "prop-types"; import { connect } from "react-redux"; import axios from "axios"; // Material UI and React Router import CircularProgress from "@material-ui/core/CircularProgress"; import Button from "@material-ui/core/Button"; import Grid from "@material-ui/core/Grid"; import Card from "@material-ui/core/Card"; import CardContent from "@material-ui/core/CardContent"; import TextField from "@material-ui/core/TextField"; import Typography from "@material-ui/core/Typography"; import withStyles from "@material-ui/styles/withStyles"; // component 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"; // Redux import { likePost, unlikePost, getLikes } from "../redux/actions/userActions"; const styles = { card: { marginBottom: 5 } }; class Home extends Component { state = { likes: [], loading: false, following: null, topics: null }; componentDidMount() { this.setState({ loading: true }); let userPromise = axios .get("/user") .then(res => { console.log(res.data.credentials.following); let list = []; res.data.credentials.following.forEach(element => { list.push(element.handle); }); this.setState({ following: list, topics: res.data.credentials.followedTopics }); }) .catch(err => console.log(err)); let allPosts; let postPromise = axios .get("/getallPosts") .then(res => { // console.log(res.data); // this.setState({ // posts: res.data // }); allPosts = res.data; // console.log(allPosts) return axios.get("/getAlert") }) .then((res) => { // console.log(res.data) res.data.forEach((adminAlert) => { allPosts.push(adminAlert); }) this.setState({ posts: allPosts }); }) .catch(err => console.log(err)); Promise.all([userPromise, postPromise]) .then(() => { this.setState({ loading: false }); }) .catch(error => { console.log(error); }); this.props.getLikes(); } componentWillReceiveProps(nextProps) { this.setState({ likes: nextProps.user.likes }); } flagPost = (event) => { // Flags a post let postId = event.target.dataset.key ? event.target.dataset.key : event.target.parentNode.dataset.key; console.log(postId); axios.post(`/hidePost`, {postId}) .then((res) => { console.log(res.data); }) .catch(err => { console.error(err); }); // event.preventDefault(); } handleClickLikeButton = (event) => { // Need the ternary if statement because the user can click on the text or body of the // Button and they are two different html elements let postId = event.target.dataset.key ? event.target.dataset.key : event.target.parentNode.dataset.key; console.log(postId); let doc = document.getElementById(postId); // console.log(postId); if (this.state.likes.includes(postId)) { this.props.unlikePost(postId, this.state.likes); doc.dataset.likes--; } else { this.props.likePost(postId, this.state.likes); doc.dataset.likes++; } doc.innerHTML = "Likes " + doc.dataset.likes; }; formatDate(dateString) { let newDate = new Date(Date.parse(dateString)); return newDate.toDateString(); } render() { const { UI: { loading } } = this.props; let authenticated = this.props.user.authenticated; let { classes } = this.props; let username = this.props.user.credentials.handle; console.log(username); var hiddenBool = true; if (username === "Admin") { hiddenBool = false; } console.log(hiddenBool); let postMarkup = this.state.posts ? ( this.state.following === undefined || this.state.following === null ? You aren't following anybody right now : this.state.posts.map(post => !post.hidden && this.state.following && (this.state.following.includes(post.userHandle) || post.userHandle === "Admin") ? ( {/* { this.state.imageUrl ? () : () } */} { post.profileImage ? () : () } {post.userHandle} {this.formatDate(post.createdAt)}
{post.microBlogTitle} {post.quoteBody}
{post.body}
Topics: {post.microBlogTopics.join(", ")}
{!hiddenBool && } Likes {post.likeCount} {/* */} {/* */} {/* Likes {post.likeCount} Comments {post.commentCount} */}
) : (

) ) ) : (

Loading post...

); return authenticated ? ( this.state.loading ? ( ) : ( {postMarkup} ) ) : loading ? ( ) : (
logo

Welcome to Twistter!

See the most interesting topics people are following right now.




Join today or sign in if you already have an account.


); } } 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 post = { userImage: "bing-url" }; const headers = { headers: { "Content-Type": "application/json" } }; axios .post(`/quoteWithoutPost/${this.props.microblog}`, post, headers) .then(res => { console.log(res.data); }) .catch(err => { console.error(err); }); event.preventDefault(); } handleOpenModal() { this.setState({ showModal: true }); } handleCloseModal() { this.setState({ showModal: false, characterCount: 250, value: "" }); } 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 = { quoteBody: this.state.value, userImage: "bing-url" }; 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 (
{/*