refactor: auth login event (#17934)
This commit is contained in:
@@ -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