From 623d3b44837ebc34a68f26ee67f26fe84f4143d6 Mon Sep 17 00:00:00 2001 From: axolotle Date: Mon, 4 Sep 2023 16:38:54 +0200 Subject: [PATCH] add states with auto query getter for user and settings data --- composables/api.ts | 30 ------------------------ composables/states.ts | 54 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 30 deletions(-) diff --git a/composables/api.ts b/composables/api.ts index d5cf56a..083c47c 100644 --- a/composables/api.ts +++ b/composables/api.ts @@ -54,33 +54,3 @@ export function useApi( } return Promise.resolve(query()).then(() => result) } - -export interface UserData { - username: string - fullname: string - mail: string - mailalias: string[] - mailforward: string[] - groups: string[] - apps: Record -} - -export const useUserData = () => useState('userData') - -export async function useUserInfo() { - const userData = useUserData() - - if (!userData.value) { - const { data } = await useApi('/me') - userData.value = data.value as UserData - } - - const update = (data: Partial) => { - Object.assign(userData.value as UserData, data) - } - - return { - userData: userData as Ref, - update, - } -} diff --git a/composables/states.ts b/composables/states.ts index 290eb11..90883d7 100644 --- a/composables/states.ts +++ b/composables/states.ts @@ -15,3 +15,57 @@ export const useIsLoggedIn = () => { export const useRedirectUrl = () => useState('redirectUrl', () => null) + +// SETTINGS + +export interface Settings { + domain: string + public: boolean + portal_logo: string + portal_theme: string + portal_title: string + show_other_domains_apps: 0 | 1 + apps: Record +} + +const useSettingsState = () => useState('settings') + +export const useSettings = async () => { + const settings = useSettingsState() + + if (!settings.value) { + const { data } = await useApi('/public') + settings.value = data.value as Settings + + const colorMode = useColorMode() + colorMode.preference = settings.value.portal_theme + } + + return settings +} + +// USER + +export interface User { + username: string + fullname: string + mail: string + mailalias: string[] + mailforward: string[] + groups: string[] + apps: Record +} + +export const useUserState = () => useState('user', () => null) + +export const useUser = async () => { + const user = useUserState() + const isLoggedIn = useIsLoggedIn() + + if (!user.value && isLoggedIn.value) { + const { data } = await useApi('/me') + user.value = data.value + } + + return user as Ref +} \ No newline at end of file