Skip to content

Commit

Permalink
Merge pull request #41 from sitek94/development
Browse files Browse the repository at this point in the history
Small fixes and refactoring
  • Loading branch information
sitek94 authored Dec 13, 2020
2 parents 9cf7563 + b1caadb commit e39abe4
Show file tree
Hide file tree
Showing 18 changed files with 127 additions and 264 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ src/api/firebase-config.js
# production
/build

# firebase
*.cache
.firebaserc

# misc
.DS_Store
.env
.env.development
.env.production
.env.local
.env.development.local
.env.test.local
Expand Down
16 changes: 16 additions & 0 deletions firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"hosting": {
"public": "build",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "habit-tracker",
"version": "0.1.0",
"version": "1.0.0-alpha.1",
"private": true,
"dependencies": {
"@emotion/react": "^11.1.1",
Expand Down
13 changes: 11 additions & 2 deletions src/api/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ import 'firebase/auth';
import 'firebase/database';
import 'firebase/firestore';

import firebaseConfig from './firebase-config';
const config = {
apiKey: process.env.REACT_APP_API_KEY,
authDomain: process.env.REACT_APP_AUTH_DOMAIN,
databaseURL: process.env.REACT_APP_DATABASE_URL,
projectId: process.env.REACT_APP_PROJECT_ID,
storageBucket: process.env.REACT_APP_STORAGE_BUCKET,
messagingSenderId: process.env.REACT_APP_MESSAGING_SENDER_ID,
appId: process.env.REACT_APP_APP_ID,
measurementId: process.env.REACT_APP_MEASUREMENT_ID,
};

class Firebase {
constructor() {
firebase.initializeApp(firebaseConfig);
firebase.initializeApp(config);

this.firebase = firebase;
this.auth = firebase.auth();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const variants = {
empty: {
icon: <EmptyCheckmarkIcon />,
label: 'empty',
color: 'default',
},
};

Expand All @@ -39,7 +40,7 @@ function Checkmark({ id, initialValue, habitId, date, disabled }) {
const debouncedUpdate = React.useRef(
debounce(({ id, newValue }) => {
updateCheckmarkInDb({ checkmarkId: id, value: newValue, habitId, date });
}, 300)
}, 200)
).current;

// Handles clicking on checkmark
Expand Down
49 changes: 0 additions & 49 deletions src/components/checkmark/checkmark-variants.js

This file was deleted.

1 change: 0 additions & 1 deletion src/components/checkmark/index.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/components/github-repo-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function GithubRepoLink(props) {
target="_blank"
label={t('githubRepo')}
rel="noopener noreferrer"
href="https://github.com/sitek94/pocket-globe-app"
href="https://github.com/sitek94/habit-tracker"
{...props}
>
<GitHubIcon />
Expand Down
11 changes: 5 additions & 6 deletions src/components/habit-row.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import PropTypes from 'prop-types';
import { Checkmark } from 'components/checkmark';
import { makeStyles, TableCell, TableRow, Typography } from '@material-ui/core';
import { getDay, isFuture } from 'date-fns';
import { getDay, parseISO } from 'date-fns';
import { EMPTY } from 'data/constants';

// Styles
Expand All @@ -17,7 +17,7 @@ const useStyles = makeStyles((theme) => ({
// Habit row
function HabitRow({ habit, dates, checkmarks }) {
const classes = useStyles();

const { id, name, frequency /* position */ } = habit;

return (
Expand All @@ -39,11 +39,10 @@ function HabitRow({ habit, dates, checkmarks }) {

{/* Dates */}
{dates.map((date) => {
const dateObj = new Date(date);
const dateObj = parseISO(date);

// Checkmark is disabled if the date is not tracked or date is in the future
const disabled =
!frequency.includes(getDay(dateObj)) || isFuture(dateObj);
// Checkmark is disabled if the date is not tracked
const disabled = !frequency.includes(getDay(dateObj));

// Find checkmark
const checkmark = checkmarks.find((d) => d.date === date);
Expand Down
4 changes: 2 additions & 2 deletions src/components/habits-table.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import { format, isToday } from 'date-fns';
import { format, isToday, parseISO } from 'date-fns';
import { getComparator } from 'utils/misc';
import { HabitRow } from './habit-row';
import { useLocale } from 'localization';
Expand Down Expand Up @@ -66,7 +66,7 @@ function HabitsTable({ habits, checkmarks, dates }) {

// Currently selected date range cells
const datesCells = dates.map((d) => {
const date = new Date(d);
const date = parseISO(d);

return {
date,
Expand Down
52 changes: 52 additions & 0 deletions src/components/performance-panel/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { COMPLETED } from 'data/constants';

/**
* Checkmark object
* @typedef Checkmark
*
* @property {string} id
* @property {string} date
* @property {string} habitId
* @property {number} value
*/

/**
* Calculate score
*
* Calculates the score for an array of checkmark values.
* The perfomance is the count of COMPLETED checkmarks divided
* by the total count.
*
* @param {number[]} values
*
* @returns {number} floored score between 0 and 100
*/
export function calculateScore(values) {
if (!values.length) return 0;

const completedCount = values.reduce((sum, value) => {
return value === COMPLETED ? sum + 1 : sum;
}, 0);

return Math.floor((completedCount / values.length) * 100);
}

/**
* Create pie chart data
*
* @returns an array of data for pie chart
*/
export function createPieChartData(values) {
const score = calculateScore(values);

return [
{
id: 'value',
value: score,
},
{
id: 'empty',
value: 100 - score,
},
];
}
1 change: 1 addition & 0 deletions src/components/performance-panel/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { PerformancePanel } from './performance-panel';
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,31 @@ import { Done as DoneIcon } from '@material-ui/icons';
import { Pie } from '@nivo/pie';
import {
calculateScore,
createScoreType,
getScoreTypeDataList,
isCheckmarkLastWeek,
isCheckmarkThisWeek,
isCheckmarkToday,
createPieChartData
} from './helpers';
import { useTranslation } from 'translations';
import { getWeek, isThisWeek, isToday, parseISO } from 'date-fns';

function UserScores({ checkmarks, goal }) {
function PerformancePanel({ checkmarks, goal }) {
const t = useTranslation();

// Score types that we track is 'last week', 'this week' and 'today'
const scoreTypeList = React.useMemo(
() => [
createScoreType(t('lastWeek'), isCheckmarkLastWeek),
createScoreType(t('thisWeek'), isCheckmarkThisWeek),
createScoreType(t('today'), isCheckmarkToday),
],
[t]
);
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);

// Use user's checkmarks and score types list to generate the data for pie chart
const scoreTypeDataList = getScoreTypeDataList(checkmarks, scoreTypeList);
const dataList = [
{ label: t('today'), data: createPieChartData(todayValues) },
{ label: t('thisWeek'), data: createPieChartData(thisWeekValues) },
{ label: t('lastWeek'), data: createPieChartData(lastWeekValues) },
];

// Calculate all time user score
const allTimeValues = checkmarks.map((d) => d.value);
Expand All @@ -43,7 +45,7 @@ function UserScores({ checkmarks, goal }) {

{/* Pie charts */}
<Grid container justifyContent="space-evenly">
{scoreTypeDataList.map(({ label, data }) => {
{dataList.map(({ label, data }) => {
const completedValue = data[0].value;
const hasReachedGoal = completedValue > goal;

Expand Down Expand Up @@ -183,4 +185,4 @@ function PieChart({ data }) {
);
}

export { UserScores };
export { PerformancePanel };
Loading

0 comments on commit e39abe4

Please sign in to comment.