Skip to content

Commit 6a8ce4d

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

File tree

9 files changed

+491
-108
lines changed

9 files changed

+491
-108
lines changed

src/api/habits/mock-habits.tsx

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,31 @@ export const mockHabits: { id: HabitIdT; data: DbHabitT }[] = [
2020
participants: {
2121
['1' as UserIdT]: {
2222
displayName: 'John Doe',
23-
username: 'johndoe',
23+
username: 'john_doe',
2424
lastActivity: new Date('2024-12-01T00:00:00'),
2525
isOwner: true,
2626
},
2727
['2' as UserIdT]: {
28-
displayName: 'Sarah Johnson',
29-
username: 'sarahj',
30-
lastActivity: new Date('2024-12-08T00:00:00'),
28+
displayName: 'Jane Doe',
29+
username: 'jane_doe',
30+
lastActivity: new Date('2025-01-11T00:00:00'),
3131
isOwner: false,
3232
},
3333
['3' as UserIdT]: {
34-
displayName: 'Mike Wilson',
35-
username: 'mikew',
34+
displayName: 'Apple Smith',
35+
username: 'apple',
3636
lastActivity: new Date('2024-12-08T00:00:00'),
3737
isOwner: false,
3838
},
3939
['4' as UserIdT]: {
40-
displayName: 'Emily Brown',
41-
username: 'emilyb',
40+
displayName: 'Bob Johnson',
41+
username: 'bob_johnson',
4242
lastActivity: new Date('2024-12-07T00:00:00'),
4343
isOwner: false,
4444
},
4545
['5' as UserIdT]: {
46-
displayName: 'Chris Lee',
47-
username: 'chrisl',
46+
displayName: 'Lorem Ipsum',
47+
username: 'lorem_ipsum',
4848
lastActivity: new Date('2024-12-03T00:00:00'),
4949
isOwner: false,
5050
},
@@ -64,8 +64,8 @@ export const mockHabits: { id: HabitIdT; data: DbHabitT }[] = [
6464
icon: 'dumbbell',
6565
participants: {
6666
['1' as UserIdT]: {
67-
displayName: 'Jane Smith',
68-
username: 'janesmith',
67+
displayName: 'John Doe',
68+
username: 'john_doe',
6969
lastActivity: new Date('2024-01-15T00:00:00'),
7070
isOwner: true,
7171
},
@@ -85,8 +85,8 @@ export const mockHabits: { id: HabitIdT; data: DbHabitT }[] = [
8585
icon: 'book',
8686
participants: {
8787
['1' as UserIdT]: {
88-
displayName: 'Alex Chen',
89-
username: 'alexchen',
88+
displayName: 'John Doe',
89+
username: 'john_doe',
9090
lastActivity: new Date('2024-12-01T00:00:00'),
9191
isOwner: true,
9292
},
@@ -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)