Skip to content

Commit

Permalink
fix loading notifications on app init (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
jongrim authored Feb 29, 2024
1 parent d467333 commit bbf2b91
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ async function setupUserProfile(id: string) {
store.userSettings = profile?.user_settings;
loadInProgress.value = false;
store.authState = "signedin";
if (!notificationSubscription.value) {
loadNotificationsAndSubscribe(profile.id);
}
}
function handleUserInit(session: Session) {
Expand All @@ -66,9 +69,6 @@ function handleUserInit(session: Session) {
setupUserProfile(session.user.id);
triggerUserAccessLoad(session.user.id);
getFlags(session.user.id);
if (!notificationSubscription.value) {
loadNotificationsAndSubscribe();
}
if (route.query.redirect && typeof route.query.redirect === "string") {
log({
Expand Down Expand Up @@ -129,28 +129,27 @@ onUnmounted(() => {
supabaseSubscription.value?.unsubscribe();
});
async function loadNotificationsAndSubscribe() {
if (!store.user?.id) return;
async function loadNotificationsAndSubscribe(id: string) {
const { data, error } = await supabase
.from("notifications")
.select("*")
.eq("read", false)
.eq("user_id", store.user.id);
.eq("user_id", id);
if (error) {
log({ error });
}
if (data) {
store.notifications = data;
}
const subscription = supabase
.channel(`public:notifications:user_id=eq.${store.user.id}`)
.channel(`public:notifications:user_id=eq.${id}`)
.on(
"postgres_changes",
{
event: "INSERT",
schema: "public",
table: "notifications",
filter: `user_id=eq.${store.user.id}`,
filter: `user_id=eq.${id}`,
},
(payload) => {
store.notifications = store.notifications.concat(
Expand All @@ -164,7 +163,7 @@ async function loadNotificationsAndSubscribe() {
event: "UPDATE",
schema: "public",
table: "notifications",
filter: `user_id=eq.${store.user.id}`,
filter: `user_id=eq.${id}`,
},
(payload) => {
store.notifications = store.notifications.map((notification) => {
Expand Down

0 comments on commit bbf2b91

Please sign in to comment.