Skip to content

Commit

Permalink
Once source of truth for dates, move things into the date hook
Browse files Browse the repository at this point in the history
  • Loading branch information
cassidoo committed Feb 20, 2020
1 parent f7c7e15 commit 2a1922c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 42 deletions.
16 changes: 12 additions & 4 deletions src/AppContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ const appStateReducer = (state, action) => {

let currentDate = {
day: format(nd, "dd"),
dayDisplay: format(nd, "d"),
month: format(nd, "MM"),
year: format(nd, "y")
monthDisplay: format(nd, "MMM"),
year: format(nd, "y"),
weekday: format(nd, "EEEE")
};

switch (action.type) {
Expand Down Expand Up @@ -82,12 +85,17 @@ export function AppStateProvider({ children }) {
let initialState = loadState();

if (initialState === undefined) {
let nd = new Date();

initialState = {
items: [],
date: {
day: format(new Date(), "dd"),
month: format(new Date(), "MM"),
year: format(new Date(), "y")
day: format(nd, "dd"),
dayDisplay: format(nd, "d"),
month: format(nd, "MM"),
monthDisplay: format(nd, "MMM"),
year: format(nd, "y"),
weekday: format(nd, "EEEE")
}
};
}
Expand Down
36 changes: 6 additions & 30 deletions src/components/TodoDate.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,26 @@
import React, { useEffect } from "react";
import { format, parseISO, isBefore } from "date-fns";
import { useAppReducer, useAppState } from "../AppContext";
import React from "react";
import { useAppState } from "../AppContext";
import useDateCheck from "../hooks/useDateCheck";
import useReminderNotification from "../hooks/useReminderNotification";
import styles from "./TodoDate.module.scss";

// Current date at the top of the page
function TodoDate() {
const dispatch = useAppReducer();
const { date } = useAppState();

useDateCheck();
useReminderNotification();

let nd = new Date();

const currentNewDate = {
dayDisplay: format(nd, "d"),
day: format(nd, "dd"),
monthDisplay: format(nd, "MMM"),
month: format(nd, "MM"),
year: format(nd, "y"),
weekday: format(nd, "EEEE")
};

useEffect(() => {
const storedDateToCompare = parseISO(`${date.year}-${date.month}-${date.day}`);

const currentDate = `${currentNewDate.year}-${currentNewDate.month}-${currentNewDate.day}`;
const currentDateToCompare = parseISO(currentDate);

if (isBefore(storedDateToCompare, currentDateToCompare)) {
dispatch({ type: "RESET_ALL" });
}
});

return (
<div className={styles.date}>
<div className={styles.calendar}>
<div className={styles.day}>{currentNewDate.dayDisplay}</div>
<div className={styles.day}>{date.dayDisplay}</div>
<div className={styles.my}>
<div className={styles.month}>{currentNewDate.monthDisplay}</div>
<div className={styles.year}>{currentNewDate.year}</div>
<div className={styles.month}>{date.monthDisplay}</div>
<div className={styles.year}>{date.year}</div>
</div>
</div>
<div className="today">{currentNewDate.weekday}</div>
<div className="today">{date.weekday}</div>
</div>
);
}
Expand Down
15 changes: 7 additions & 8 deletions src/hooks/useDateCheck.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
import { useEffect } from "react";
import { format, parseISO, isBefore } from "date-fns";
import { useAppState } from "../AppContext";
import { useAppState, useAppReducer } from "../AppContext";
const { remote } = require("electron");

export default function useDateCheck() {
const { date } = useAppState();
const dispatch = useAppReducer();
const storedDate = parseISO(`${date.year}-${date.month}-${date.day}`);

useEffect(() => {
let nd = new Date();

const interval = setInterval(() => {
let nd = new Date();
let currentDate = parseISO(
`${format(nd, "y")}-${format(nd, "MM")}-${format(nd, "dd")}`
);
console.log(remote.getCurrentWindow());

if (isBefore(storedDate, currentDate)) {
if (remote.getCurrentWindow().showResetNotification) {
// Disabling this line so that it doesn't yell at us for not using
// the notification variable
// eslint-disable-next-line
let resetNotification = new Notification("todometer reset time!", {
new Notification("todometer reset time!", {
body: "It's a new day! Your todos are being reset."
});
}
dispatch({ type: "RESET_ALL" });
window.location.reload();
}
}, 1000);
return () => clearInterval(interval);
}, [storedDate]);
}, [storedDate, dispatch]);
}

0 comments on commit 2a1922c

Please sign in to comment.