Skip to content

Commit 3b4640f

Browse files
authored
fix(calendar): correct retrieval date from entries map in cells (#144)
* fix(calendar): correct retrieval date from entries map in cells * refactor: useFetchOnAuth
1 parent 5cace36 commit 3b4640f

File tree

9 files changed

+97
-19
lines changed

9 files changed

+97
-19
lines changed

src/components/calendar-month/CalendarCell.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isCalendarDay } from '@helpers';
12
import { useScreenSize } from '@hooks';
23
import { Button, Tooltip } from '@nextui-org/react';
34
import {
@@ -68,9 +69,9 @@ const CalendarCell = ({
6869
const screenSize = useScreenSize();
6970
const date = format(
7071
new Date(fullYear, monthNumber - 1, dateNumber),
71-
'yyyy-MM-dd'
72+
'yyyy-MM-d'
7273
);
73-
const occurrences = occurrencesByDate[date] || [];
74+
const occurrences = isCalendarDay(date) ? occurrencesByDate[date] || [] : [];
7475
const isMobile = screenSize < 768;
7576
const hasNote = notes.some((note) => note.day === date);
7677

src/components/header/Header.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
11
import { AuthModalButton } from '@components';
22
import { useFetchOnAuth, useScreenSize } from '@hooks';
3-
import { Button, Tooltip } from '@nextui-org/react';
3+
import { Button, Spinner, Tooltip } from '@nextui-org/react';
44
import { GithubLogo } from '@phosphor-icons/react';
55
import React from 'react';
66
import { Link } from 'react-router-dom';
77

88
import ThemeToggle from './ThemeToggle';
99

1010
const Header = () => {
11-
useFetchOnAuth();
11+
const { hasFetched } = useFetchOnAuth();
1212
const screenSize = useScreenSize();
1313

1414
return (
1515
<header className="border-b border-b-slate-300 bg-slate-200 dark:border-b-slate-800 dark:bg-black">
16+
{!hasFetched && (
17+
<div className="absolute bottom-0 left-0 right-0 top-0 z-50 flex h-full flex-1 items-center justify-center bg-slate-50 opacity-90 dark:bg-slate-950">
18+
<div className="flex -translate-y-[73px] items-center justify-center">
19+
<Spinner
20+
color="primary"
21+
size="lg"
22+
label="Please wait"
23+
classNames={{
24+
circle1: 'border-b-white',
25+
circle2: 'border-b-white',
26+
}}
27+
/>
28+
</div>
29+
</div>
30+
)}
1631
<div className="mx-auto flex w-full items-center justify-between gap-2 px-2 py-2 lg:px-16 lg:py-4">
1732
<div className="flex items-center gap-2">
1833
<Button

src/helpers/calendar.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,58 @@
1+
type Year = `${number}${number}${number}${number}`;
2+
3+
type Month =
4+
| '01'
5+
| '02'
6+
| '03'
7+
| '04'
8+
| '05'
9+
| '06'
10+
| '07'
11+
| '08'
12+
| '09'
13+
| '10'
14+
| '11'
15+
| '12';
16+
17+
type Day =
18+
| '01'
19+
| '02'
20+
| '03'
21+
| '04'
22+
| '05'
23+
| '06'
24+
| '07'
25+
| '08'
26+
| '09'
27+
| '10'
28+
| '11'
29+
| '12'
30+
| '13'
31+
| '14'
32+
| '15'
33+
| '16'
34+
| '17'
35+
| '18'
36+
| '19'
37+
| '20'
38+
| '21'
39+
| '22'
40+
| '23'
41+
| '24'
42+
| '25'
43+
| '26'
44+
| '27'
45+
| '28'
46+
| '29'
47+
| '30'
48+
| '31';
49+
50+
export type CalendarDay = `${Year}-${Month}-${Day}`;
51+
52+
export const isCalendarDay = (value: string): value is CalendarDay => {
53+
return /^\d{4}-\d{2}-\d{2}$/.test(value);
54+
};
55+
156
const SUNDAY = 0;
257
const MONDAY = 1;
358

src/hooks/useFetchOnAuth.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,17 @@ import React from 'react';
99

1010
const useFetchOnAuth = () => {
1111
const supabase = useSupabaseClient();
12-
const { fetchTraits, clearTraits } = useTraitsStore();
13-
const { fetchHabits, clearHabits } = useHabitsStore();
14-
const { fetchOccurrences, clearOccurrences } = useOccurrencesStore();
15-
const { fetchNotes, clearNotes } = useNotesStore();
12+
const { fetchTraits, fetchingTraits, clearTraits } = useTraitsStore();
13+
const { fetchHabits, fetchingHabits, clearHabits } = useHabitsStore();
14+
const { fetchOccurrences, fetchingOccurrences, clearOccurrences } =
15+
useOccurrencesStore();
16+
const { fetchNotes, fetchingNotes, clearNotes } = useNotesStore();
17+
18+
const hasFetched =
19+
!fetchingTraits &&
20+
!fetchingHabits &&
21+
!fetchingOccurrences &&
22+
!fetchingNotes;
1623

1724
React.useEffect(() => {
1825
if (!supabase.auth) {
@@ -49,6 +56,8 @@ const useFetchOnAuth = () => {
4956
clearOccurrences,
5057
clearNotes,
5158
]);
59+
60+
return { hasFetched };
5261
};
5362

5463
export default useFetchOnAuth;

src/models/occurrence.model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { CalendarDay } from '@helpers';
12
import type { CamelCasedPropertiesDeep } from 'type-fest';
23

34
import type { TablesInsert, Tables } from '../../supabase/database.types';
@@ -17,8 +18,7 @@ export type Occurrence = BaseOccurrence & {
1718
habit: HabitWithTrait | null;
1819
};
1920

20-
type OccurrenceDate = string;
21-
export type OccurrencesDateMap = Record<OccurrenceDate, Occurrence[]>;
21+
export type OccurrencesDateMap = Record<CalendarDay, Occurrence[]>;
2222

2323
export type OccurrencesInsert = CamelCasedPropertiesDeep<
2424
TablesInsert<'occurrences'>

src/stores/habits.store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const useHabitsStore = create<HabitsState>((set) => {
3737
return {
3838
habits: [],
3939
addingHabit: false,
40-
fetchingHabits: false,
40+
fetchingHabits: true,
4141
habitIdBeingUpdated: null,
4242
habitIdBeingDeleted: null,
4343

src/stores/notes.store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const { showSnackbar } = useSnackbarsStore.getState();
2727

2828
const useNotesStore = create<NotesState>((set) => ({
2929
notes: [],
30-
fetchingNotes: false,
30+
fetchingNotes: true,
3131
addingNote: false,
3232
updatingNote: false,
3333
deletingNote: false,

src/stores/occurrences.store.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from '@services';
1212
import { useHabitsStore, useSnackbarsStore, useTraitsStore } from '@stores';
1313
import { getErrorMessage } from '@utils';
14+
import { format } from 'date-fns';
1415
import { create } from 'zustand';
1516

1617
type OccurrenceFilters = {
@@ -48,7 +49,7 @@ const useOccurrencesStore = create<OccurrencesState>((set, get) => {
4849

4950
return {
5051
addingOccurrence: false,
51-
fetchingOccurrences: false,
52+
fetchingOccurrences: true,
5253
allOccurrences: [],
5354
occurrences: [],
5455
occurrencesByDate: {},
@@ -176,11 +177,8 @@ const useOccurrencesStore = create<OccurrencesState>((set, get) => {
176177
const nextOccurrencesByDate = occurrences.reduce(
177178
(acc, occurrence) => {
178179
const { timestamp } = occurrence;
179-
const date = new Date(timestamp);
180-
const dateNumber = date.getDate();
181-
const month = date.getMonth();
182-
const year = date.getFullYear();
183-
const day = `${year}-${month + 1}-${dateNumber}`;
180+
const day = format(new Date(timestamp), 'yyyy-MM-d');
181+
184182
if (!acc[day]) {
185183
acc[day] = [occurrence];
186184
} else {

src/stores/traits.store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const { showSnackbar } = useSnackbarsStore.getState();
2323

2424
const useTraitsStore = create<TraitsState>((set) => ({
2525
traits: testTraits,
26-
fetchingTraits: false,
26+
fetchingTraits: true,
2727
addingTrait: false,
2828
fetchTraits: async () => {
2929
try {

0 commit comments

Comments
 (0)