mirror of
https://github.com/immich-app/immich.git
synced 2025-12-22 17:24:56 +03:00
refactor: auth login event (#17934)
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import type { LoginResponseDto } from '@immich/sdk';
|
||||
|
||||
type Listener<EventMap extends Record<string, unknown[]>, K extends keyof EventMap> = (...params: EventMap[K]) => void;
|
||||
|
||||
class EventManager<EventMap extends Record<string, unknown[]>> {
|
||||
@@ -50,6 +52,7 @@ class EventManager<EventMap extends Record<string, unknown[]>> {
|
||||
|
||||
export const eventManager = new EventManager<{
|
||||
'user.login': [];
|
||||
'auth.login': [LoginResponseDto];
|
||||
'auth.logout': [];
|
||||
'language.change': [{ name: string; code: string; rtl?: boolean }];
|
||||
}>();
|
||||
|
||||
@@ -1,24 +1,27 @@
|
||||
import { eventManager } from '$lib/managers/event-manager.svelte';
|
||||
import { handlePromiseError } from '$lib/utils';
|
||||
import { handleError } from '$lib/utils/handle-error';
|
||||
import { getNotifications, updateNotification, updateNotifications, type NotificationDto } from '@immich/sdk';
|
||||
import { t } from 'svelte-i18n';
|
||||
import { get } from 'svelte/store';
|
||||
|
||||
class NotificationStore {
|
||||
notifications = $state<NotificationDto[]>([]);
|
||||
|
||||
constructor() {
|
||||
// TODO replace this with an `auth.login` event
|
||||
this.refresh().catch(() => {});
|
||||
|
||||
eventManager.on('auth.login', () => handlePromiseError(this.refresh()));
|
||||
eventManager.on('auth.logout', () => this.clear());
|
||||
}
|
||||
|
||||
get hasUnread() {
|
||||
return this.notifications.length > 0;
|
||||
async refresh() {
|
||||
try {
|
||||
this.notifications = await getNotifications({ unread: true });
|
||||
} catch (error) {
|
||||
const translate = get(t);
|
||||
handleError(error, translate('errors.failed_to_load_notifications'));
|
||||
}
|
||||
}
|
||||
|
||||
refresh = async () => {
|
||||
this.notifications = await getNotifications({ unread: true });
|
||||
};
|
||||
|
||||
markAsRead = async (id: string) => {
|
||||
this.notifications = this.notifications.filter((notification) => notification.id !== id);
|
||||
await updateNotification({ id, notificationUpdateDto: { readAt: new Date().toISOString() } });
|
||||
|
||||
Reference in New Issue
Block a user