|
| 1 | +import {defineStore} from "pinia"; |
| 2 | +import { ProfileSettings } from "../../types/ProfileSettings"; |
| 3 | + |
| 4 | +export const useProfileSettingsStore = defineStore('profileSettings', { |
| 5 | + // because of the persist option. This option is defined in the pinia persisted state plugin |
| 6 | + // @types-ignore |
| 7 | + persist: true, |
| 8 | + state: () => ({ |
| 9 | + settings: null as ProfileSettings | null, |
| 10 | + loading: false, |
| 11 | + error: null as unknown | null, |
| 12 | + refreshed: "2021-08-01T12:00:00Z" |
| 13 | + }), |
| 14 | + getters: { |
| 15 | + getDisplayName(): string { |
| 16 | + return this.settings ? this.settings.displayName : ''; |
| 17 | + }, |
| 18 | + getUsername(): string { |
| 19 | + return this.settings ? this.settings.username : ''; |
| 20 | + }, |
| 21 | + getProfilePicture(): string | null { |
| 22 | + return this.settings ? this.settings.profilePicture : ''; |
| 23 | + }, |
| 24 | + isPrivateProfile(): boolean { |
| 25 | + return this.settings ? this.settings.privateProfile : false; |
| 26 | + }, |
| 27 | + isPreventIndex(): boolean { |
| 28 | + return this.settings ? this.settings.preventIndex : false; |
| 29 | + }, |
| 30 | + getDefaultStatusVisibility(): number { |
| 31 | + return this.settings ? this.settings.defaultStatusVisibility : 0; |
| 32 | + }, |
| 33 | + getPrivacyHideDays(): number { |
| 34 | + return this.settings ? this.settings.privacyHideDays : 0; |
| 35 | + }, |
| 36 | + getEmail(): string | null { |
| 37 | + return this.settings ? this.settings.email : null; |
| 38 | + }, |
| 39 | + isEmailVerified(): boolean { |
| 40 | + return this.settings ? this.settings.emailVerified : false; |
| 41 | + }, |
| 42 | + isProfilePictureSet(): boolean { |
| 43 | + return this.settings ? this.settings.profilePictureSet : false; |
| 44 | + }, |
| 45 | + getTwitter(): string | null { |
| 46 | + return this.settings ? this.settings.twitter : null; |
| 47 | + }, |
| 48 | + getMastodon(): string | null { |
| 49 | + return this.settings ? this.settings.mastodon : null; |
| 50 | + }, |
| 51 | + getMastodonVisibility(): number { |
| 52 | + return this.settings ? this.settings.mastodonVisibility : 0; |
| 53 | + } |
| 54 | + }, |
| 55 | + actions: { |
| 56 | + async fetchSettings() : Promise<void>{ |
| 57 | + // Fetch Data every 15 Minutes |
| 58 | + // ToDo: reduce interval |
| 59 | + // ToDo: refresh with settings update |
| 60 | + // ToDo: invalidate when logging out |
| 61 | + if (this.refreshed && (new Date().getTime() - new Date(this.refreshed).getTime()) < 60 * 15 * 1000) { |
| 62 | + return; |
| 63 | + } |
| 64 | + this.loading = true; |
| 65 | + try { |
| 66 | + this.settings = await fetch('/api/v1/settings/profile') |
| 67 | + .then((response: { json: () => any; }) => response.json()) |
| 68 | + .then((data: { data: any; }) => data.data); |
| 69 | + this.refreshed = new Date().toString(); |
| 70 | + } catch (error) { |
| 71 | + this.error = error; |
| 72 | + } finally { |
| 73 | + this.loading = false; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | +}); |
0 commit comments