|
1 | 1 | import { defineStore, storeToRefs } from 'pinia';
|
2 |
| -import { ref } from 'vue'; |
| 2 | +import { computed, reactive } from 'vue'; |
3 | 3 |
|
| 4 | +import type { TmdbClientAuthentication } from '~/models/tmdb/tmdb-client.model'; |
4 | 5 | import type { TraktClientAuthentication } from '~/models/trakt/trakt-authentication.model';
|
5 | 6 |
|
| 7 | +import type { TvdbClientAuthentication } from '~/models/tvdb/tvdb-client.model'; |
| 8 | + |
6 | 9 | import { storage } from '~/utils/browser/browser-storage.utils';
|
7 | 10 |
|
8 |
| -export const useSettingsStore = defineStore('settings', () => { |
9 |
| - const auth = ref<TraktClientAuthentication>(); |
10 |
| - const isAuthenticated = ref(false); |
| 11 | +type SettingsAuth = { |
| 12 | + trakt?: TraktClientAuthentication; |
| 13 | + tvdb?: TvdbClientAuthentication; |
| 14 | + tmdb?: TmdbClientAuthentication; |
| 15 | +}; |
11 | 16 |
|
12 |
| - const setAuthenticated = (authenticated: boolean = false) => { |
13 |
| - isAuthenticated.value = authenticated; |
| 17 | +type SettingsAuthenticated = { |
| 18 | + trakt?: boolean; |
| 19 | + tvdb?: boolean; |
| 20 | + tmdb?: boolean; |
| 21 | +}; |
| 22 | + |
| 23 | +export const useSettingsStore = defineStore('settings', () => { |
| 24 | + const auth = reactive<SettingsAuth>({}); |
| 25 | + const authenticated = reactive<SettingsAuthenticated>({}); |
| 26 | + const isAuthenticated = computed(() => Object.values(authenticated).every(Boolean)); |
| 27 | + |
| 28 | + const setAuthenticated = ({ trakt, tvdb, tmdb }: SettingsAuth = {}) => { |
| 29 | + if (trakt) authenticated.trakt = !!trakt.access_token; |
| 30 | + if (tvdb) authenticated.tvdb = !!tvdb.accessToken; |
| 31 | + if (tmdb) authenticated.tmdb = !!tmdb.accessToken; |
14 | 32 | };
|
15 | 33 |
|
16 |
| - const syncSetAuth = (_auth: TraktClientAuthentication) => storage.sync.set('settings.auth', _auth); |
| 34 | + const syncSetAuth = (_auth: SettingsAuth) => storage.sync.set('settings.auth', _auth); |
17 | 35 | const syncClearAuth = () => storage.sync.remove('settings.auth');
|
18 | 36 | const syncRestoreAuth = () =>
|
19 |
| - storage.sync.get<TraktClientAuthentication>('settings.auth').then(_auth => { |
20 |
| - auth.value = _auth; |
21 |
| - setAuthenticated(!!_auth?.access_token); |
| 37 | + storage.sync.get<SettingsAuth>('settings.auth').then(_auth => { |
| 38 | + Object.assign(auth, _auth); |
| 39 | + setAuthenticated(_auth); |
22 | 40 | return _auth;
|
23 | 41 | });
|
24 | 42 |
|
25 |
| - const setAuth = (_auth?: TraktClientAuthentication) => { |
26 |
| - auth.value = _auth; |
27 |
| - setAuthenticated(!!_auth?.access_token); |
28 |
| - console.info('settings-store', 'Auth changed', _auth); |
29 |
| - if (_auth?.access_token) { |
30 |
| - syncSetAuth(_auth).then(() => console.info('settings-store', 'Auth saved', _auth)); |
| 43 | + const setAuth = (_auth: SettingsAuth = {}) => { |
| 44 | + if (_auth.trakt) auth.trakt = _auth.trakt; |
| 45 | + if (_auth.tvdb) auth.tvdb = _auth.tvdb; |
| 46 | + if (_auth.tmdb) auth.tmdb = _auth.tmdb; |
| 47 | + console.info('settings-store', 'Auth changed', { ...auth }); |
| 48 | + setAuthenticated(_auth); |
| 49 | + if (Object.keys(_auth).length > 0) { |
| 50 | + syncSetAuth(auth).then(() => console.info('settings-store', 'Auth saved', { ...auth })); |
31 | 51 | } else {
|
32 | 52 | syncClearAuth().then(() => console.info('settings-store', 'Auth cleared'));
|
33 | 53 | }
|
34 | 54 | };
|
35 | 55 |
|
36 |
| - return { auth, setAuth, isAuthenticated, setAuthenticated, syncRestoreAuth }; |
| 56 | + return { auth, setAuth, authenticated, isAuthenticated, syncRestoreAuth }; |
37 | 57 | });
|
38 | 58 |
|
39 | 59 | export const useSettingsStoreRefs = () => storeToRefs(useSettingsStore());
|
0 commit comments