From 927048ec53fd75e37c5f48a1d673c0d0b469cad5 Mon Sep 17 00:00:00 2001 From: spencerpincott Date: Tue, 3 Jan 2023 21:42:41 -0500 Subject: [PATCH] Add checks for daily activities --- src/components/Checkbox.js | 15 +++++++++++ src/components/Icon.js | 5 ++++ src/routes/Day.js | 51 +++++++++++++++++++++++++++++++++----- 3 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 src/components/Checkbox.js diff --git a/src/components/Checkbox.js b/src/components/Checkbox.js new file mode 100644 index 0000000..80962b8 --- /dev/null +++ b/src/components/Checkbox.js @@ -0,0 +1,15 @@ +import React from "react" +import Icon from "components/Icon" +import { withTheme } from "@emotion/react" + +const Checkbox = ({ checked, name, tabindex, label, ...rest }) => ( +
+ {" "} +
+) + +export default withTheme(Checkbox) diff --git a/src/components/Icon.js b/src/components/Icon.js index 3ab8e03..dc99fc6 100644 --- a/src/components/Icon.js +++ b/src/components/Icon.js @@ -20,6 +20,8 @@ import { Smartphone, Sun, User, + Square, + CheckSquare, } from "react-feather" import { withTheme } from "@emotion/react" @@ -31,6 +33,7 @@ const IconBase = styled.div` border-radius: 12px; padding: 5px; transition: 0.1s all ease-in-out; + text-transform: capitalize; color: ${props => props.theme.colors.secondary}; &:focus { outline: none; @@ -69,6 +72,8 @@ const Icon = ({ name, tabindex, label, labelRight, size, ...rest }) => ( {name === "Smartphone" && } {name === "Sun" && } {name === "User" && } + {name === "Checked" && } + {name === "Unchecked" && } {label && labelRight && ( { - return props.theme.colors.tertiary}}; + color: ${props => props.theme.colors.tertiary}; } &:focus-within { box-shadow: 0 0 0 8px ${props => props.theme.colors.bodyBackground}, @@ -89,6 +89,7 @@ const Buttons = styled.div` flex-direction: row; margin-top: 20px; ` + const fadeKeyFrames = keyframes` from { opacity: 0; @@ -101,6 +102,11 @@ const LoadingSpinner = styled(BeatLoader)` opacity: 0; ` +const Activities = styled.div` + display: flex; + justify-content: space-between; +` + const AUTOSAVE_DELAY = 2000 class Day extends React.Component { @@ -110,6 +116,7 @@ class Day extends React.Component { saving: false, lastSavedAt: new Date(), lastEditedAt: new Date(), + activities: { }, } timeout = 0 @@ -159,16 +166,16 @@ class Day extends React.Component { .then((doc) => { if (doc.data()) { console.log("I have data!", doc.data().text) - this.setState({ text: doc.data().text, loading: false }) + this.setState({ text: doc.data().text, loading: false, activities: doc.data().activities }) } else { console.log("I don't have data!") - this.setState({ text: "", loading: false }) + this.setState({ text: "", loading: false, activities: { } }) } }) .catch((err) => { console.warn("entry not found in cache") // no doc was found, so reset the entry area to blank - this.setState({ loading: false, text: "" }) + this.setState({ loading: false, text: "", activities: { } }) // for cache first, server second fetching, dangerous with potential overwriting of data // docRef.get().then(doc => { // if (doc.data()) { @@ -244,14 +251,43 @@ class Day extends React.Component { }); } + setActivityCheck = (activity, checked) => { + const { firebase, authUser, year, month, day } = this.props + this.setState({ activities: { [activity]: checked } }); + firebase.db + .collection("entries") + .doc(`${year}${month}${day}-${authUser.uid}`) + .set( + { + activities: { + [activity]: checked, + } + }, + { + merge: true, + } + ); + } + render() { const { year, month, day, theme } = this.props const online = this.context - const { text, loading, saving, lastSavedAt, lastEditedAt } = this.state + const { text, loading, saving, lastSavedAt, lastEditedAt, activities } = this.state const currentDay = new Date(year, month - 1, day) if (!currentDay) return const hasSavedChanges = lastSavedAt >= lastEditedAt + const activityChecks = ["physio", "yoga", "run", "piano"].map(activity => + this.setActivityCheck(activity, !activities[activity])} + > + {" "} + + ); + return ( <> + + {activityChecks} + Record thoughts about the day