-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0bf032d
commit 76b6087
Showing
23 changed files
with
625 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { createQuery } from 'react-query-kit'; | ||
|
||
import { habitColors } from '@/ui/colors'; | ||
|
||
import { addTestDelay } from '../common'; | ||
import { type UserIdT } from '../users'; | ||
import { mockHabits } from './mock-habits'; | ||
import { type HabitT } from './types'; | ||
|
||
type Variables = { id: HabitT['id'] }; | ||
type Response = HabitT; | ||
|
||
export const useHabit = createQuery<Response, Variables, Error>({ | ||
queryKey: ['habit'], | ||
fetcher: async ({ id }) => { | ||
const dbHabit = await addTestDelay( | ||
mockHabits.find((habit) => habit.id === id), | ||
); | ||
if (!dbHabit) throw new Error('Habit not found'); | ||
|
||
const { data } = dbHabit; | ||
return { | ||
id, | ||
...data, | ||
color: habitColors[data.colorName], | ||
participants: Object.fromEntries( | ||
Object.entries(data.participants).map( | ||
([participantId, participant]) => { | ||
if (!participant) | ||
throw new Error('Participant not found for habit ' + id); | ||
|
||
return [ | ||
participantId, | ||
{ | ||
id: participantId as UserIdT, | ||
displayName: participant.displayName, | ||
username: participant.username, | ||
lastActivity: new Date(participant.lastActivity), | ||
hasActivityToday: | ||
participant.lastActivity.toLocaleDateString('en-CA') === | ||
new Date().toLocaleDateString('en-CA'), | ||
isOwner: participant?.isOwner ?? false, | ||
}, | ||
]; | ||
}, | ||
), | ||
), | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './colors-schemas'; | ||
export * from './common'; | ||
export * from './habits'; | ||
export * from './notifications'; | ||
export * from './users'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './types'; | ||
export * from './use-notifications'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { type HabitIdT } from '../habits'; | ||
import { type UserIdT } from '../users'; | ||
import { | ||
type FriendNotificationT, | ||
type HabitNotificationT, | ||
type NotificationT, | ||
} from './types'; | ||
|
||
const now = new Date(); | ||
const getTimeAgoDate = (seconds: number) => | ||
new Date(now.getTime() - seconds * 1000); | ||
|
||
export const mockNotifications: (FriendNotificationT | HabitNotificationT)[] = [ | ||
{ | ||
type: 'friendRequest', | ||
senderId: '3' as UserIdT, | ||
receiverId: '1' as UserIdT, | ||
sentAt: getTimeAgoDate(20), // 20 seconds ago | ||
}, | ||
{ | ||
type: 'friendRequest', | ||
senderId: '6' as UserIdT, | ||
receiverId: '1' as UserIdT, | ||
sentAt: getTimeAgoDate(3600), // 1 hour ago | ||
}, | ||
{ | ||
type: 'habitInvite', | ||
habitId: '4' as HabitIdT, | ||
senderId: '2' as UserIdT, | ||
receiverId: '1' as UserIdT, | ||
sentAt: getTimeAgoDate(1800), // 30 minutes ago | ||
}, | ||
{ | ||
type: 'nudge', | ||
habitId: '1' as HabitIdT, | ||
senderId: '4' as UserIdT, | ||
receiverId: '1' as UserIdT, | ||
sentAt: getTimeAgoDate(172800), // 2 days ago | ||
}, | ||
]; | ||
|
||
export const setMockNotifications = (newNotifications: NotificationT[]) => { | ||
mockNotifications.length = 0; | ||
mockNotifications.push(...newNotifications); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { createQuery } from 'react-query-kit'; | ||
|
||
import { addTestDelay } from '../common'; | ||
import { mockNotifications } from './mock-notifications'; | ||
import { type FriendNotificationT, type HabitNotificationT } from './types'; | ||
|
||
type Response = (FriendNotificationT | HabitNotificationT)[]; | ||
type Variables = void; | ||
|
||
export const useNotifications = createQuery<Response, Variables, Error>({ | ||
queryKey: ['notifications'], | ||
fetcher: async () => { | ||
const myId = '1'; | ||
const notifications = await addTestDelay( | ||
mockNotifications.filter( | ||
(notification) => notification.receiverId === myId, | ||
), | ||
); | ||
return notifications; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.