-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Streaks component need npm i react-calendar-heatmap and npm i react-tooltip * remove dead code, add some comments
- Loading branch information
Showing
6 changed files
with
249 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
|
||
import React, {useState, useEffect} from 'react'; | ||
import CalendarHeatmap from 'react-calendar-heatmap'; | ||
import ReactTooltip from 'react-tooltip'; | ||
import classes from './Streaks.module.css'; | ||
import './react-calendar-heatmap/dist/styles.css'; | ||
import {getLongDateStringWithSlashes} from '../../functions/dateFormatter'; | ||
import {getAllEncounters} from '../../services'; | ||
import {unmarshalEncounters} from '../../functions/dataUnmarshaller'; | ||
import {Avatar, Card} from '@mui/material'; | ||
|
||
const today = new Date(); | ||
|
||
function Streaks(encounter) { | ||
const [dates, setDates]=useState([]); | ||
async function getDate() { | ||
const encountersResult = await getAllEncounters(); | ||
|
||
const unmarshalledEncounters = encountersResult.map((encounter) => | ||
unmarshalEncounters(encounter), | ||
); | ||
setDates(unmarshalledEncounters.map((a) =>a.date)); | ||
} | ||
useEffect(()=>{ | ||
try { | ||
getDate(); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
}, [encounter]); | ||
// count the occurrences of the date | ||
const occurrences = dates.reduce(function(acc, curr) { | ||
return acc[curr] ? ++acc[curr] : acc[curr] = 1, acc; | ||
}, {}); | ||
// set useful Array | ||
const resultDate = Object.keys(occurrences); | ||
const resultCount = Object.values(occurrences); | ||
// use for the values props of the CalendarHeatmap | ||
const data = getRange(resultCount.length).map((d) => ( | ||
{ | ||
date: resultDate[d], | ||
count: resultCount[d], | ||
})); | ||
return ( | ||
<div> | ||
<header> | ||
<h2>Streaks</h2> | ||
<div className={classes.description}> | ||
<div>Encounter for last 5 months</div> | ||
<div className={classes.descriptionScales}> | ||
<div className={classes.scales}> | ||
<div>Low </div> | ||
<div className={classes.streaks0} /> | ||
<div className={classes.streaks1} /> | ||
<div className={classes.streaks2} /> | ||
<div className={classes.streaks3} /> | ||
<div className={classes.streaks4} /> | ||
<div className={classes.streaks5} /> | ||
<div> High</div> | ||
</div> | ||
</div> | ||
</div> | ||
</header> | ||
<div> | ||
<ReactTooltip/> | ||
<CalendarHeatmap | ||
startDate={shiftDate(today, -150)} | ||
endDate={today} | ||
values={data} | ||
classForValue={(value) => { | ||
if (!value) { | ||
return 'color-streaks-0'; | ||
} | ||
return `${value.count}` <= 10 ? | ||
`color-streaks-${value.count}` : | ||
`color-streaks-10`; | ||
}} | ||
tooltipDataAttrs={(value) => { | ||
if (!value.date) { | ||
return {'data-tip': | ||
`None encounter at this date`}; | ||
} | ||
return { | ||
'data-tip': `${getLongDateStringWithSlashes(value.date)} | ||
has count: | ||
${value.count }`, | ||
}; | ||
} | ||
} | ||
showWeekdayLabels={true} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
} | ||
// use for the startDate props of the CalendarHeatmap | ||
function shiftDate(date, numDays) { | ||
const newDate = new Date(date); | ||
newDate.setDate(newDate.getDate() + numDays); | ||
return newDate; | ||
} | ||
function getRange(count) { | ||
return Array.from({length: count}, (_, i) => i); | ||
} | ||
export default Streaks; |
69 changes: 69 additions & 0 deletions
69
forgettable-frontend/src/components/Streaks/Streaks.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
.scales { | ||
display: flex; | ||
margin-left: auto; | ||
margin-right: 0; | ||
} | ||
.description { | ||
width: 915px; | ||
margin-left: 29px; | ||
color: #313131; | ||
display: flex; | ||
justify-content: space-between; | ||
} | ||
|
||
.descriptionScales { | ||
display: flex; | ||
} | ||
|
||
.streaks0 { | ||
background: #eeeeee; | ||
fill: #eeeeee; | ||
width: 14px; | ||
height: 14px; | ||
margin: 5px; | ||
border-radius: 50%; | ||
} | ||
|
||
.streaks1 { | ||
background:#F3D58A ; | ||
fill: #F3D58A; | ||
width: 14px; | ||
height: 14px; | ||
margin: 5px; | ||
border-radius: 50%; | ||
} | ||
|
||
.streaks2 { | ||
width: 14px; | ||
height: 14px; | ||
margin: 5px; | ||
border-radius: 50%; | ||
fill: #ecc769; | ||
background: #ecc769; | ||
} | ||
|
||
.streaks3 { | ||
width: 14px; | ||
height: 14px; | ||
margin: 5px; | ||
border-radius: 50%; | ||
fill: #ebb429; | ||
background: #ebb429; | ||
} | ||
|
||
.streaks4 { | ||
width: 14px; | ||
height: 14px; | ||
margin: 5px; | ||
border-radius: 50%; | ||
fill: #d89d07; | ||
background: #d89d07; | ||
} | ||
.streaks5 { | ||
width: 14px; | ||
height: 14px; | ||
margin: 5px; | ||
border-radius: 50%; | ||
fill: #d80707; | ||
background:#d80707; | ||
} |
59 changes: 59 additions & 0 deletions
59
forgettable-frontend/src/components/Streaks/react-calendar-heatmap/dist/styles.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* react-calendar-heatmap styles | ||
* | ||
* All of the styles in this file are optional and configurable! | ||
* The github and gitlab color scales are provided for reference. | ||
*/ | ||
|
||
.react-calendar-heatmap text { | ||
font-size: 5px; | ||
fill: #aaa; | ||
} | ||
|
||
.react-calendar-heatmap .react-calendar-heatmap-small-text { | ||
font-size: 5px; | ||
} | ||
|
||
.react-calendar-heatmap rect:hover { | ||
stroke: #555; | ||
stroke-width: 1px; | ||
} | ||
|
||
|
||
.color-streaks-0 { | ||
fill: #eeeeee; | ||
} | ||
|
||
.color-streaks-1 { | ||
fill: #F3D58A; | ||
} | ||
|
||
.color-streaks-2 { | ||
fill: #ecc769; | ||
} | ||
|
||
.color-streaks-3 { | ||
fill: #ebb429; | ||
} | ||
|
||
.color-streaks-4 { | ||
fill: #e9a804; | ||
} | ||
.color-streaks-5 { | ||
fill: #d89d07; | ||
} | ||
.color-streaks-6 { | ||
fill: #8f6804; | ||
} | ||
.color-streaks-7 { | ||
fill: #7b5902; | ||
} | ||
.color-streaks-8 { | ||
fill: #4b3600; | ||
} | ||
.color-streaks-9 { | ||
fill: #463200; | ||
} | ||
.color-streaks-10 { | ||
fill: #d80707; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters