From 8dfb5e226042151baea9decde41c6445b0086ea1 Mon Sep 17 00:00:00 2001 From: zmagod Date: Fri, 6 Oct 2017 15:22:01 +0200 Subject: [PATCH] implements @mlorb suggestions --- .../components/ActivityDateElement.jsx | 6 +- .../components/GlobalActivitiesModal.jsx | 92 ++++++++++--------- .../src/services/helpers/date_time_helper.js | 6 ++ 3 files changed, 58 insertions(+), 46 deletions(-) create mode 100644 app/javascript/src/services/helpers/date_time_helper.js diff --git a/app/javascript/src/components/Navigation/components/ActivityDateElement.jsx b/app/javascript/src/components/Navigation/components/ActivityDateElement.jsx index 5ef945ace..0dad1bf50 100644 --- a/app/javascript/src/components/Navigation/components/ActivityDateElement.jsx +++ b/app/javascript/src/components/Navigation/components/ActivityDateElement.jsx @@ -5,6 +5,8 @@ import Moment from "react-moment"; import { FormattedMessage } from "react-intl"; import styled from "styled-components"; + +import { isToday } from "../../../services/helpers/date_time_helper" import { WHITE_COLOR } from "../../../config/constants/colors"; const StyledLi = styled.li`margin-bottom: 1em;`; @@ -21,8 +23,8 @@ const StyledSpan = styled.span` border-radius: 0.25em; `; -const ActivityDateElement = (props: { date: Date, today?: boolean }) => { - const label = props.today ? ( +const ActivityDateElement = (props: { date: Date }) => { + const label = isToday(props.date) ? ( ) : ( {props.date} diff --git a/app/javascript/src/components/Navigation/components/GlobalActivitiesModal.jsx b/app/javascript/src/components/Navigation/components/GlobalActivitiesModal.jsx index b33328f04..0f95e6b69 100644 --- a/app/javascript/src/components/Navigation/components/GlobalActivitiesModal.jsx +++ b/app/javascript/src/components/Navigation/components/GlobalActivitiesModal.jsx @@ -46,10 +46,8 @@ const StyledModalBody = styled(Modal.Body)` `; type Props = { - more: boolean, showModal: boolean, - onCloseModal: Function, - activites: Array + onCloseModal: Function }; type State = { @@ -58,18 +56,30 @@ type State = { }; class GlobalActivitiesModal extends Component { - displayActivities: Function; - addMoreActivities: Function; - loadData: Function; - onCloseModalActions: Function; + static renderActivityDateElement( + key: number, + activity: Activity, + date: Date + ) { + return [ + , + + ]; + } constructor(props: Props) { super(props); this.state = { activities: [], more: false }; - this.displayActivities = this.displayActivities.bind(this); - this.addMoreActivities = this.addMoreActivities.bind(this); - this.onCloseModalActions = this.onCloseModalActions.bind(this); - this.loadData = this.loadData.bind(this); + (this: any).displayActivities = this.displayActivities.bind(this); + (this: any).addMoreActivities = this.addMoreActivities.bind(this); + (this: any).onCloseModalActions = this.onCloseModalActions.bind(this); + (this: any).loadData = this.loadData.bind(this); + (this: any).mapActivities = this.mapActivities.bind(this) + } + + onCloseModalActions() { + this.setState({ activities: [], more: false }); + this.props.onCloseModal(); } loadData() { @@ -81,9 +91,32 @@ class GlobalActivitiesModal extends Component { }); } - onCloseModalActions() { - this.setState({ activities: [], more: false }); - this.props.onCloseModal(); + mapActivities() { + return this.state.activities.map((activity, i, arr) => { + // @todo replace key={i} with key={activity.id} !!!!!!!!!!!!!! + // when the backend bug will be fixed + const newDate = new Date(activity.created_at); + // returns a label with "today" if the date of the activity is today + if (i === 0) { + return GlobalActivitiesModal.renderActivityDateElement( + i, + activity, + newDate + ); + } + // else checks if the previous activity is newer than current + // and displays a label with the date + const prevDate = new Date(arr[i - 1].created_at); + if (prevDate.getDate() > newDate.getDate()) { + return GlobalActivitiesModal.renderActivityDateElement( + i, + activity, + newDate + ); + } + // returns the default activity element + return ; + }); } displayActivities() { @@ -98,36 +131,7 @@ class GlobalActivitiesModal extends Component { ); } - return this.state.activities.map((activity, i, arr) => { - const dateNow = new Date(); - const newDate = new Date(activity.created_at); - if (i > 0) { - const prevDate = new Date(arr[i - 1].created_at); - if (prevDate.getDate() > newDate.getDate()) { - return [ - , - - ]; - } - } else { - if ( - newDate.getDate() === dateNow.getDate() && - newDate.getMonth() === dateNow.getMonth() && - newDate.getFullYear() === dateNow.getFullYear() - ) { - return [ - , - - ]; - } - return [ - , - - ]; - } - console.log("banana"); - return ; - }); + return this.mapActivities(); } addMoreActivities() { diff --git a/app/javascript/src/services/helpers/date_time_helper.js b/app/javascript/src/services/helpers/date_time_helper.js new file mode 100644 index 000000000..caf7017c0 --- /dev/null +++ b/app/javascript/src/services/helpers/date_time_helper.js @@ -0,0 +1,6 @@ +// @flow + +export function isToday(inputDate: Date): boolean { + const todaysDate = new Date(); + return inputDate.setHours(0, 0, 0, 0) == todaysDate.setHours(0, 0, 0, 0); +}