All files / components/performance-panel performance-panel.js

88% Statements 22/25
50% Branches 1/2
80% Functions 12/15
87.5% Lines 21/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178                  1x 4x     1x 4x     1x 4x     1x           4x 1x   1x                   3x 3x   3x                                                                       2x                           6x               3x                                         3x     3x                                     3x                               3x   3x                              
import * as React from 'react';
import { VictoryPie } from 'victory';
import { Box, Divider, Grid, Typography, useTheme } from '@material-ui/core';
import { Done as DoneIcon } from '@material-ui/icons';
import { Pie } from '@nivo/pie';
import { calculateScore, createPieChartData } from './helpers';
import { getWeek, isThisWeek, isToday, parseISO } from 'date-fns';
 
function PerformancePanel({ checkmarks, goal }) {
  const todayValues = checkmarks
    .filter((c) => isToday(parseISO(c.date)))
    .map((c) => c.value);
 
  const thisWeekValues = checkmarks
    .filter((c) => isThisWeek(parseISO(c.date)))
    .map((c) => c.value);
 
  const lastWeekValues = checkmarks
    .filter((c) => getWeek(parseISO(c.date)) === getWeek(new Date()) - 1)
    .map((c) => c.value);
 
  const dataList = [
    { label: "Last Week", data: createPieChartData(lastWeekValues) },
    { label: "This Week", data: createPieChartData(thisWeekValues) },
    { label: "Today", data: createPieChartData(todayValues) },
  ];
 
  const allTimeValues = checkmarks.map((d) => d.value);
  const allTimeScore = calculateScore(allTimeValues);
 
  return (
    <>
      <Typography component="h2" variant="h6" color="primary">
      Your performance
      </Typography>
 
      <FixedHeightDivider />
 
      <Grid container justifyContent="space-evenly">
        {dataList.map(({ label, data }) => {
          const completedValue = data[0].value;
          const hasReachedGoal = completedValue >= goal;
 
          return (
            <Grid item key={label}>
              <Label>{completedValue}%</Label>
 
              <ChartContainer>
                <PieChart data={data} />
 
                <CenteredBox>
                  {hasReachedGoal ? (
                    <DoneIcon fontSize="large" color="primary" />
                  ) : (
                    <GoalLabel>
                      Goal
                      <br />
                      <span>{goal}%</span>
                    </GoalLabel>
                  )}
                </CenteredBox>
              </ChartContainer>
 
              <Label variant="body2">{label}</Label>
            </Grid>
          );
        })}
      </Grid>
 
      <FixedHeightDivider />
 
      <Typography align="left" color="textSecondary">
        Overall All Time Performance: {allTimeScore}%
      </Typography>
    </>
  );
}
 
function FixedHeightDivider() {
  return (
    <Box
      clone
      sx={{
        alignSelf: 'stretch',
        height: '1px',
      }}
    >
      <Divider />
    </Box>
  );
}
 
function Label({ children, ...props }) {
  return (
    <Typography align="center" color="textSecondary" {...props}>
      {children}
    </Typography>
  );
}
 
function GoalLabel({ children }) {
  return (
    <Box
      clone
      sx={{
        marginTop: '5px',
        fontSize: 12,
        lineHeight: 1.2,
      }}
    >
      <Typography
        variant="subtitle2"
        color="textSecondary"
        component="label"
        align="center"
      >
        {children}
      </Typography>
    </Box>
  );
}
 
const CHART_SIZE = 95;
 
function CenteredBox({ children }) {
  return (
    <Box
      sx={{
        position: 'absolute',
        width: '100%',
        height: CHART_SIZE,
        display: 'flex',
        justifyContent: 'center',
        alignItems: 'center',
        top: 0,
        left: 0,
      }}
    >
      {children}
    </Box>
  );
}
 
function ChartContainer({ children }) {
  return (
    <Box
      sx={{
        position: 'relative',
        display: 'flex',
        justifyContent: 'center',
      }}
    >
      {children}
    </Box>
  );
}
 
function PieChart({ data }) {
  const {
    palette: { primary, grey },
  } = useTheme();
 
  return (
    <Pie
      data={data}
      height={CHART_SIZE}
      width={CHART_SIZE}
      margin={{ top: 5, right: 5, bottom: 5, left: 5 }}
      innerRadius={0.6}
      enableRadialLabels={false}
      enableSliceLabels={false}
      colors={[primary.main, grey[300]]}
    />
  );
}
 
export { PerformancePanel };