From d686b32d9d00aaa6f3596767ddca709f24c80e9d Mon Sep 17 00:00:00 2001 From: Bouncey Date: Thu, 8 Nov 2018 13:43:36 +0000 Subject: [PATCH] fix: Reimplement the HeatMap --- client/package-lock.json | 19 +++ client/package.json | 1 + .../components/profile/components/HeatMap.js | 106 +++++++++----- .../components/profile/components/heatmap.css | 18 +++ client/static/css/cal-heatmap.css | 133 ------------------ 5 files changed, 108 insertions(+), 169 deletions(-) delete mode 100644 client/static/css/cal-heatmap.css diff --git a/client/package-lock.json b/client/package-lock.json index 21487535cb..3673bbb66f 100644 --- a/client/package-lock.json +++ b/client/package-lock.json @@ -14378,6 +14378,25 @@ "prop-types": "^15.6.0" } }, + "react-calendar-heatmap": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/react-calendar-heatmap/-/react-calendar-heatmap-1.7.0.tgz", + "integrity": "sha512-stPZSm07l3mZmj/MXOD+feHM2ZWY4Jb8fn06O+OeMFMasRCuiB2ZjBgCnCLb0BZ4hHh8vHNQ9v7wapiMcVgU0g==", + "requires": { + "prop-types": "^15.6.2" + }, + "dependencies": { + "prop-types": { + "version": "15.6.2", + "resolved": "https://registry.npmjs.org/prop-types/-/prop-types-15.6.2.tgz", + "integrity": "sha512-3pboPvLiWD7dkI3qf3KbUe6hKFKa52w+AE0VCqECtf+QHAKgOL37tTaNCnuX1nAAQ4ZhyP+kYVKf8rLmJ/feDQ==", + "requires": { + "loose-envify": "^1.3.1", + "object-assign": "^4.1.1" + } + } + } + }, "react-dev-utils": { "version": "4.2.3", "resolved": "https://registry.npmjs.org/react-dev-utils/-/react-dev-utils-4.2.3.tgz", diff --git a/client/package.json b/client/package.json index 80703370d1..1092c73986 100644 --- a/client/package.json +++ b/client/package.json @@ -40,6 +40,7 @@ "prismjs": "^1.15.0", "query-string": "^6.1.0", "react": "^16.4.2", + "react-calendar-heatmap": "^1.7.0", "react-dom": "^16.4.2", "react-freecodecamp-search": "^2.0.2", "react-ga": "^2.5.3", diff --git a/client/src/components/profile/components/HeatMap.js b/client/src/components/profile/components/HeatMap.js index 550c7ba6da..6c5d44d53e 100644 --- a/client/src/components/profile/components/HeatMap.js +++ b/client/src/components/profile/components/HeatMap.js @@ -1,9 +1,15 @@ -import React, { Component } from 'react'; +import React from 'react'; import PropTypes from 'prop-types'; import { Helmet } from 'react-helmet'; +import CalendarHeatMap from 'react-calendar-heatmap'; +import startOfMonth from 'date-fns/start_of_month'; +import startOfDay from 'date-fns/start_of_day'; +import format from 'date-fns/format'; import FullWidthRow from '../../helpers/FullWidthRow'; +import Spacer from '../../helpers/Spacer'; +import 'react-calendar-heatmap/dist/styles.css'; import './heatmap.css'; const propTypes = { @@ -14,44 +20,72 @@ const propTypes = { }) }; -class HeatMap extends Component { - constructor(props) { - super(props); +const now = Date.now(); +const today = new Date(now); +const sixMonthsInMillseconds = 15780000000; +const sixMonthsAgoInMilliseconds = now - sixMonthsInMillseconds; +const sixMonthsAgo = startOfMonth(sixMonthsAgoInMilliseconds); - this.renderMap = this.renderMap.bind(this); - } +function HeatMap({ calendar, streak }) { + const dateValueMap = Object.keys(calendar) + .map(ts => Number(ts * 1000) || null) + .filter(Boolean) + .reduce((map, current) => { + const startOfCurrent = format(startOfDay(current), 'YYYY-MM-DD'); + if (startOfCurrent in map) { + map[startOfCurrent] = map[startOfCurrent] + 1; + } else { + map[startOfCurrent] = 1; + } + return map; + }, {}); - render() { - const { streak = {} } = this.props; - return ( - - -