@@ -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+
3778function getStructuredCompletionData (
3879 completionData : ParticipantCompletionsT ,
3980 numDays : number ,
0 commit comments