From 4ec16ac78bcc749298473ee564dca6b085ac7907 Mon Sep 17 00:00:00 2001 From: alexsparkes Date: Thu, 7 Nov 2024 19:21:23 +0000 Subject: [PATCH] fix(stats): Streak logic --- src/components/Form/Settings/Text/Text.jsx | 17 +++------- src/features/stats/api/stats.js | 39 ++++++++++++++-------- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/components/Form/Settings/Text/Text.jsx b/src/components/Form/Settings/Text/Text.jsx index 4413d552..4228bfb6 100644 --- a/src/components/Form/Settings/Text/Text.jsx +++ b/src/components/Form/Settings/Text/Text.jsx @@ -1,25 +1,20 @@ import variables from 'config/variables'; -import { useState, useEffect } from 'react'; +import { useState } from 'react'; import { toast } from 'react-toastify'; import { Field, Label, Textarea, Input } from '@headlessui/react'; import { MdRefresh } from 'react-icons/md'; + import clsx from 'clsx'; + import EventBus from 'utils/eventbus'; function Text(props) { const [value, setValue] = useState(localStorage.getItem(props.name) || ''); - useEffect(() => { - if (props.code) { - // import('prismjs/themes/prism.css'); - const Prism = require('prismjs'); - Prism.highlightAll(); - } - }, [value, props.code]); - const handleChange = (e) => { let { value } = e.target; + // Alex wanted font to work with montserrat and Montserrat, so I made it work if (props.upperCaseFirst === true) { value = value.charAt(0).toUpperCase() + value.slice(1); } @@ -67,10 +62,6 @@ function Text(props) { rows={4} /> - ) : props.code ? ( -
-          {value}
-        
) : ( <> = 0; i--) { + const currentEventDate = new Date(events[i].timestamp).toDateString(); + const nextEventDate = new Date(events[i + 1].timestamp).toDateString(); - if (lastEventDate && lastEventDate !== currentEventDate) { const daysDifference = - (new Date(currentEventDate) - new Date(lastEventDate)) / (1000 * 60 * 60 * 24); + (new Date(nextEventDate) - new Date(currentEventDate)) / (1000 * 60 * 60 * 24); + if (daysDifference === 1) { - data.streak.current = (data.streak.current || 0) + 1; + currentStreak += 1; } else { - data.streak.current = 1; + if (currentStreak > highestStreak) { + highestStreak = currentStreak; + } + currentStreak = 1; } - } else { - data.streak.current = 1; } + + if (currentStreak > highestStreak) { + highestStreak = currentStreak; + } + + data.streak.current = currentStreak; + data.streak.highest = highestStreak; } static async postEvent(type, name = '', action = '') { @@ -109,7 +122,7 @@ export default class Stats { totalXp: 0, currentLevelXp: 0, nextLevelXp: this.calculateNextLevelXp(1), - streak: { current: 0 }, + streak: { current: 0, highest: 0 }, }; const timestamp = new Date().toISOString(); @@ -120,7 +133,7 @@ export default class Stats { const xpGained = this.calculateXpForEvent(type, statsData.streak.current); await this.addEvent({ type, name, action, timestamp, xpGained }); this.updateStatsData(statsData, xpGained); - this.calculateStreak(statsData, timestamp); + await this.calculateStreak(statsData); console.log(`Updated Stats Data: ${JSON.stringify(statsData)}`);