Skip to content

Commit

Permalink
Added time to comments, fixed a bunch of redux stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky1638 committed Sep 28, 2018
1 parent 5665592 commit 7121995
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
node_modules
dev.js
.vscode
.vscode
8 changes: 0 additions & 8 deletions .vscode/settings.json

This file was deleted.

12 changes: 11 additions & 1 deletion client/src/components/Comment.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import React from "react"
import moment from "moment"
import { AvatarImage, Text } from "components"
import { generateDaysAgoText } from "utils"

const calculateDate = date => {
const difference = moment().diff(moment(date))
return generateDaysAgoText(difference)
}

const Comment = ({ comment, ...otherProps }) => (
<div>
<div style={{ margin: "1em 0" }}>
<div className="f-aic" {...otherProps}>
<AvatarImage user={comment.user} />
<div style={{ marginLeft: 10 }}>
Expand All @@ -11,6 +18,9 @@ const Comment = ({ comment, ...otherProps }) => (
<div style={{ marginLeft: 15 }}>
<Text greyDark>{comment.rating}</Text>
</div>
<div style={{ marginLeft: 15 }}>
<Text grey>{calculateDate(comment.createdAt)}</Text>
</div>
</div>
<div>
<Text>{comment.content}</Text>
Expand Down
5 changes: 3 additions & 2 deletions client/src/components/RecipeCard.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from "react"
import styled from "styled-components"
import { Text } from "components"
import { Link } from "react-router-dom"

const StyledCard = styled("a")`
const StyledCard = styled(Link)`
padding: 0 0 1em 0;
margin: 0 1em 1em 1em;
min-width: 13em;
Expand All @@ -19,7 +20,7 @@ const RecipeCard = (props) => {
const { imgSrc, title, time, description, href } = props

return (
<StyledCard href={href}>
<StyledCard to={href}>
<div
title={title}
className="row"
Expand Down
23 changes: 20 additions & 3 deletions client/src/reducers/reducer_recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,30 @@ export default function(state = null, action) {
switch (action.type) {
case ADD_RECIPE:
if (state) return [...state, action.payload]
return action.payload
return [action.payload]
case FETCH_RECIPES:
return action.payload
case FETCH_RECIPE:
return state ? [...state, action.payload] : [action.payload]
case ADD_COMMENT:
return state ? [...state, action.payload] : [action.payload]
let newState = []
const recipeInState = state ? state.find(recipe => recipe.id === action.payload.id) : false

if (recipeInState) {
state.forEach(recipe => {
if (recipe.id === action.payload.id) {
newState.push(action.payload)
} else {
newState.push(recipe)
}
})
console.log("newState", newState)
return newState
} else if (state) {
console.log("hesfdj")
return [...state, action.payload]
}
console.log("action.payload", action.payload)
return [action.payload]
default:
return state
}
Expand Down
4 changes: 2 additions & 2 deletions client/src/scenes/Recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Slider } from "components"
import { fetchRecipe, getRecipeById } from "actions"
import { withRouter } from "react-router-dom"
import shortenText from "utils/shortenText"
import { shortenText } from "utils"
import _ from "lodash"
import { connect } from "react-redux"
import { isEmpty } from "lodash"
Expand Down Expand Up @@ -68,7 +68,7 @@ class Recipe extends React.Component {
componentWillMount() {
const { id } = this.props.match.params

this.props.fetchRecipe(id)
_.isEmpty(this.props.recipe) && this.props.fetchRecipe(id)
}

componentDidMount() {
Expand Down
11 changes: 11 additions & 0 deletions client/src/utils/generateDaysAgoText.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const generateDaysAgoText = timeInMilliseconds => {
const timeInSeconds = Math.floor(timeInMilliseconds / 1000)
if (timeInSeconds < 30) return "Just now"
else if (timeInSeconds < 60) return "<1 minute ago"
else if (timeInSeconds < 3599) return `${Math.floor(timeInSeconds / 60)} minute${timeInSeconds > 120 ? "s" : ""} ago`
else if (timeInSeconds < 86399) return `${Math.floor(timeInSeconds / 3600)} hour${timeInSeconds > 7200 ? "s" : ""} ago`
else if (timeInSeconds < 604799) return `${Math.floor(timeInSeconds / 86400)} day${timeInSeconds > 172800 ? "s" : ""} ago`
else return "A long time ago"
}

export default generateDaysAgoText
7 changes: 7 additions & 0 deletions client/src/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import shortenText from "./shortenText"
import generateDaysAgoText from "./generateDaysAgoText"

export {
shortenText,
generateDaysAgoText
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"cookie-session": "^2.0.0-beta.3",
"express": "^4.16.2",
"formik": "^0.11.11",
"moment": "^2.22.2",
"mongoose": "^5.0.7",
"multer": "^1.3.0",
"multer-s3": "^2.7.0",
Expand Down
17 changes: 15 additions & 2 deletions routes/commentRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,21 @@ module.exports = app => {
}
)
.then(() => {
Recipe.findOne({ where: { id: req.params.id }, include: [User, Comment] })
.then(foundRecipe => res.send(foundRecipe))
Recipe.findOne({
where: { id: req.params.recipeId },
include: [
{
model: User
},
{
model: Comment,
include: [User]
}
]
})
.then(foundRecipe => {
res.send(foundRecipe)
})
.catch(err => console.log(err))
})
.catch(err => console.log(err))
Expand Down

0 comments on commit 7121995

Please sign in to comment.