mirror of
https://github.com/YunoHost/yunohost-portal.git
synced 2024-09-03 20:06:23 +02:00
add states with auto query getter for user and settings data
This commit is contained in:
parent
46a76ff4bc
commit
623d3b4483
2 changed files with 54 additions and 30 deletions
|
@ -54,33 +54,3 @@ export function useApi<T>(
|
||||||
}
|
}
|
||||||
return Promise.resolve(query()).then(() => result)
|
return Promise.resolve(query()).then(() => result)
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface UserData {
|
|
||||||
username: string
|
|
||||||
fullname: string
|
|
||||||
mail: string
|
|
||||||
mailalias: string[]
|
|
||||||
mailforward: string[]
|
|
||||||
groups: string[]
|
|
||||||
apps: Record<string, { label: string; url: string }>
|
|
||||||
}
|
|
||||||
|
|
||||||
export const useUserData = () => useState<UserData | undefined>('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<UserData>) => {
|
|
||||||
Object.assign(userData.value as UserData, data)
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
userData: userData as Ref<UserData>,
|
|
||||||
update,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -15,3 +15,57 @@ export const useIsLoggedIn = () => {
|
||||||
|
|
||||||
export const useRedirectUrl = () =>
|
export const useRedirectUrl = () =>
|
||||||
useState<string | null>('redirectUrl', () => null)
|
useState<string | null>('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<string, { label: string; url: string }>
|
||||||
|
}
|
||||||
|
|
||||||
|
const useSettingsState = () => useState<Settings>('settings')
|
||||||
|
|
||||||
|
export const useSettings = async () => {
|
||||||
|
const settings = useSettingsState()
|
||||||
|
|
||||||
|
if (!settings.value) {
|
||||||
|
const { data } = await useApi<Settings>('/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<string, { label: string; url: string }>
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useUserState = () => useState<User | null>('user', () => null)
|
||||||
|
|
||||||
|
export const useUser = async <T extends User | null = User>() => {
|
||||||
|
const user = useUserState()
|
||||||
|
const isLoggedIn = useIsLoggedIn()
|
||||||
|
|
||||||
|
if (!user.value && isLoggedIn.value) {
|
||||||
|
const { data } = await useApi<T>('/me')
|
||||||
|
user.value = data.value
|
||||||
|
}
|
||||||
|
|
||||||
|
return user as Ref<T>
|
||||||
|
}
|
Loading…
Reference in a new issue