1c980ba678
* Notifications generating on comment,answer,contract update * Notifications MVP * Submitted an answer => answered * Listen for unseen notifications * Fix userlink formatting, move page * Fix links * Remove redundant code * Cleanup * Cleanup * Refactor name * Comments * Cleanup & update notif only after data retrieval * Find initial new notifs on user change * Enforce auth rules in db * eslint update * Code review changes * Refactor reason
25 lines
837 B
TypeScript
25 lines
837 B
TypeScript
import { collection, query, where } from 'firebase/firestore'
|
|
import { Notification } from 'common/notification'
|
|
import { db } from 'web/lib/firebase/init'
|
|
import { getValues, listenForValues } from 'web/lib/firebase/utils'
|
|
|
|
function getNotificationsQuery(userId: string, unseenOnly?: boolean) {
|
|
const notifsCollection = collection(db, `/users/${userId}/notifications`)
|
|
if (unseenOnly) return query(notifsCollection, where('isSeen', '==', false))
|
|
return query(notifsCollection)
|
|
}
|
|
|
|
export function listenForNotifications(
|
|
userId: string,
|
|
setNotifications: (notifs: Notification[]) => void,
|
|
unseenOnly?: boolean
|
|
) {
|
|
return listenForValues<Notification>(
|
|
getNotificationsQuery(userId, unseenOnly),
|
|
(notifs) => {
|
|
notifs.sort((n1, n2) => n2.createdTime - n1.createdTime)
|
|
setNotifications(notifs)
|
|
}
|
|
)
|
|
}
|