Skip to content

Commit 361b840

Browse files
feat: view habit page; ability to view everyone's entries
1 parent 1585b3a commit 361b840

File tree

9 files changed

+478
-95
lines changed

9 files changed

+478
-95
lines changed

src/api/habits/mock-habits.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const mockHabits: { id: HabitIdT; data: DbHabitT }[] = [
2727
['2' as UserIdT]: {
2828
displayName: 'Sarah Johnson',
2929
username: 'sarahj',
30-
lastActivity: new Date('2024-12-08T00:00:00'),
30+
lastActivity: new Date('2025-01-11T00:00:00'),
3131
isOwner: false,
3232
},
3333
['3' as UserIdT]: {
@@ -119,6 +119,7 @@ export const mockHabitCompletions: Record<HabitIdT, AllCompletionsT> = {
119119
'2024-12-04': { numberOfCompletions: 2 },
120120
'2024-12-05': { numberOfCompletions: 3 },
121121
'2024-12-06': { numberOfCompletions: 1 },
122+
'2025-01-11': { numberOfCompletions: 1, note: 'Drank 8 glasses' },
122123
},
123124
},
124125
['3' as UserIdT]: {

src/api/habits/use-habit-completions.tsx

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ import {
99
type ParticipantCompletionsT,
1010
} from './types';
1111

12-
type Response = HabitCompletionWithDateInfoT[];
13-
type Variables = {
12+
type SingleUserHabitCompletionsResponse = HabitCompletionWithDateInfoT[];
13+
type SingleUserHabitCompletionsVariables = {
1414
habitId: HabitIdT;
1515
userId: UserIdT;
16+
numDays: number;
1617
};
1718

18-
export const useHabitCompletions = createQuery<Response, Variables, Error>({
19+
export const useHabitCompletions = createQuery<
20+
SingleUserHabitCompletionsResponse,
21+
SingleUserHabitCompletionsVariables,
22+
Error
23+
>({
1924
queryKey: ['habit-completions'],
2025
fetcher: async (variables) => {
2126
const completions = mockHabitCompletions[variables.habitId];
@@ -28,12 +33,48 @@ export const useHabitCompletions = createQuery<Response, Variables, Error>({
2833
}
2934

3035
const structuredCompletions: HabitCompletionWithDateInfoT[] =
31-
getStructuredCompletionData(participantCompletions, 7);
36+
getStructuredCompletionData(participantCompletions, variables.numDays);
3237

3338
return await addTestDelay(structuredCompletions);
3439
},
3540
});
3641

42+
type AllUsersHabitCompletionsResponse = {
43+
[key: UserIdT]: HabitCompletionWithDateInfoT[];
44+
};
45+
type AllUsersHabitCompletionsVariables = {
46+
habitId: HabitIdT;
47+
numDays: number;
48+
};
49+
50+
export const useAllUsersHabitCompletions = createQuery<
51+
AllUsersHabitCompletionsResponse,
52+
AllUsersHabitCompletionsVariables,
53+
Error
54+
>({
55+
queryKey: ['all-users-habit-completions'],
56+
fetcher: async (variables) => {
57+
const completions = mockHabitCompletions[variables.habitId];
58+
if (!completions) {
59+
throw new Error('Habit not found');
60+
}
61+
62+
const result: { [key: UserIdT]: HabitCompletionWithDateInfoT[] } = {};
63+
for (const userId in completions) {
64+
const participantCompletions = completions[userId as UserIdT];
65+
if (!participantCompletions) {
66+
throw new Error('Participant not found');
67+
}
68+
result[userId as UserIdT] = getStructuredCompletionData(
69+
participantCompletions,
70+
variables.numDays,
71+
);
72+
}
73+
74+
return await addTestDelay(result);
75+
},
76+
});
77+
3778
function getStructuredCompletionData(
3879
completionData: ParticipantCompletionsT,
3980
numDays: number,

src/api/habits/use-modify-entry.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,11 @@ export const useModifyHabitEntry = createMutation<Response, Variables, Error>({
9696
queryClient.invalidateQueries({
9797
queryKey: [
9898
'habit-completions',
99-
{ habitId: variables.habitId, userId: variables.userId },
99+
{ habitId: variables.habitId, userId: variables.userId, numDays: 7 },
100100
],
101101
});
102+
queryClient.invalidateQueries({
103+
queryKey: ['all-users-habit-completions', { habitId: variables.habitId }],
104+
});
102105
},
103106
});

0 commit comments

Comments
 (0)