From ccc7976b395a4b8f1f2f530ac3c4bba9e9290eab Mon Sep 17 00:00:00 2001 From: Dominik Hryshaiev Date: Fri, 25 Oct 2024 13:37:21 +0200 Subject: [PATCH 1/2] fix(occurrences): fetch on range change --- src/components/Providers.tsx | 2 +- src/components/calendar/CalendarCell.tsx | 68 ++++++++++--------- src/components/calendar/CalendarMonthGrid.tsx | 2 +- src/components/calendar/OccurrenceChip.tsx | 13 +--- src/components/common/Snackbars/Snackbars.tsx | 2 +- src/stores/occurrences.store.ts | 9 ++- 6 files changed, 45 insertions(+), 51 deletions(-) diff --git a/src/components/Providers.tsx b/src/components/Providers.tsx index a9e8527..6bd6e0c 100644 --- a/src/components/Providers.tsx +++ b/src/components/Providers.tsx @@ -15,7 +15,7 @@ const LowerProviders = ({ children }: ProviderProps) => { return {children}; }; -const PotentialSupabaseProvider = ({ children }: { children: ReactNode }) => { +const PotentialSupabaseProvider = ({ children }: ProviderProps) => { if (!Object.keys(supabaseClient).length) { return children; } diff --git a/src/components/calendar/CalendarCell.tsx b/src/components/calendar/CalendarCell.tsx index b994130..1fa0ed8 100644 --- a/src/components/calendar/CalendarCell.tsx +++ b/src/components/calendar/CalendarCell.tsx @@ -4,16 +4,16 @@ import { useOccurrencesStore } from '@stores'; import { useUser } from '@supabase/auth-helpers-react'; import clsx from 'clsx'; import { format } from 'date-fns'; -import { motion } from 'framer-motion'; +import { AnimatePresence, motion } from 'framer-motion'; import React from 'react'; import OccurrenceChip from './OccurrenceChip'; type CalendarCellProps = { dateNumber: number; - monthIndex: number; + monthNumber: number; fullYear: number; - onClick: (dateNumber: number, monthIndex: number, fullYear: number) => void; + onClick: (dateNumber: number, monthNumber: number, fullYear: number) => void; onNavigateBack?: () => void; onNavigateForward?: () => void; rangeStatus: 'below-range' | 'in-range' | 'above-range'; @@ -21,7 +21,7 @@ type CalendarCellProps = { const CalendarCell = ({ dateNumber, - monthIndex, + monthNumber, fullYear, onNavigateBack, onNavigateForward, @@ -35,11 +35,11 @@ const CalendarCell = ({ const today = new Date(); const isToday = today.getDate() === dateNumber && - today.getMonth() + 1 === monthIndex && + today.getMonth() + 1 === monthNumber && today.getFullYear() === fullYear; const screenSize = useScreenSize(); const date = format( - new Date(fullYear, monthIndex - 1, dateNumber), + new Date(fullYear, monthNumber - 1, dateNumber), 'yyyy-MM-dd' ); const occurrences = occurrencesByDate[date] || []; @@ -47,7 +47,7 @@ const CalendarCell = ({ const groupedOccurrences = Object.groupBy(occurrences, (o) => o.habitId); const handleClick = React.useCallback(() => { - if (fetchingOccurrences || !user?.id) { + if (fetchingOccurrences || !user) { return null; } @@ -61,18 +61,18 @@ const CalendarCell = ({ } } - return onClick(dateNumber, monthIndex, fullYear); + return onClick(dateNumber, monthNumber, fullYear); }, [ isToday, dateNumber, fetchingOccurrences, fullYear, - monthIndex, + monthNumber, onClick, onNavigateBack, onNavigateForward, rangeStatus, - user?.id, + user, ]); React.useEffect(() => { @@ -120,7 +120,9 @@ const CalendarCell = ({ const cellRootClassName = clsx( 'flex h-28 flex-1 flex-col border-r-3 border-neutral-500 last-of-type:border-r-0 hover:bg-neutral-200 dark:border-neutral-400 dark:hover:bg-neutral-800', rangeStatus === 'below-range' && 'cursor-w-resize', - rangeStatus === 'above-range' && 'cursor-e-resize' + rangeStatus === 'above-range' && 'cursor-e-resize', + isToday && + 'bg-neutral-200 hover:bg-neutral-300 dark:bg-neutral-800 dark:hover:bg-neutral-700' ); const cellHeaderClassName = clsx( @@ -132,10 +134,6 @@ const CalendarCell = ({
- {Object.entries(groupedOccurrences).map( - ([habitId, habitOccurrences]) => { - return ( - - - - ); - } - )} + + {Object.entries(groupedOccurrences).map( + ([habitId, habitOccurrences]) => { + if (!habitOccurrences) { + return null; + } + + return ( + + + + ); + } + )} +
); diff --git a/src/components/calendar/CalendarMonthGrid.tsx b/src/components/calendar/CalendarMonthGrid.tsx index 52b6d60..c860432 100644 --- a/src/components/calendar/CalendarMonthGrid.tsx +++ b/src/components/calendar/CalendarMonthGrid.tsx @@ -52,7 +52,7 @@ const Month = ( { - const { occurrenceIdBeingDeleted } = useOccurrencesStore(); - const [{ id, habit }] = occurrences; + const [{ habit }] = occurrences; const { name: habitName, iconPath, trait } = habit || {}; const { color: traitColor } = trait || {}; const screenSize = useScreenSize(); const iconUrl = getHabitIconUrl(iconPath); - const isBeingDeleted = occurrenceIdBeingDeleted === id; - const chipStyle = { backgroundColor: colorOverride || traitColor, }; @@ -42,10 +38,6 @@ const OccurrenceChip = ({ return null; } - if (isBeingDeleted) { - return ; - } - return (