From 4b8ae76132d2342f40cec703b3d5145ea075c451 Mon Sep 17 00:00:00 2001 From: vnugent Date: Wed, 13 Dec 2023 17:58:51 -0500 Subject: log time coming ui and lib updates --- front-end/src/store/userProfile.ts | 82 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 front-end/src/store/userProfile.ts (limited to 'front-end/src/store/userProfile.ts') diff --git a/front-end/src/store/userProfile.ts b/front-end/src/store/userProfile.ts new file mode 100644 index 0000000..a4ea469 --- /dev/null +++ b/front-end/src/store/userProfile.ts @@ -0,0 +1,82 @@ +import 'pinia' +import { MaybeRef, watch } from 'vue'; +import { ServerDataBuffer, ServerObjectBuffer, UserProfile, WebMessage, apiCall, useAxios, useDataBuffer, useUser } from '@vnuge/vnlib.browser'; +import { get, useToggle } from '@vueuse/core'; +import { PiniaPlugin, PiniaPluginContext, storeToRefs } from 'pinia' +import { defer } from 'lodash-es'; + +export interface OAuth2Application { + readonly Id: string, + readonly name: string, + readonly description: string, + readonly permissions: string[], + readonly client_id: string, + readonly Created: Date, + readonly LastModified: Date, +} + +export interface NewAppResponse { + readonly secret: string + readonly app: ServerDataBuffer> +} + +interface ExUserProfile extends UserProfile { + created: string | Date +} + +declare module 'pinia' { + export interface PiniaCustomProperties { + userProfile: ServerDataBuffer> + userName: string | undefined + refreshProfile(): void; + } +} + +export const profilePlugin = (accountsUrl:MaybeRef) :PiniaPlugin => { + + return ({ store }: PiniaPluginContext) => { + + const { loggedIn } = storeToRefs(store) + const { getProfile, userName } = useUser() + const axios = useAxios(null) + + const [onRefresh, refreshProfile] = useToggle() + + const updateUserProfile = async (profile: ServerObjectBuffer) => { + // Apply the buffer to the profile + const { data } = await axios.post>(get(accountsUrl), profile.buffer) + + //Get the new profile from the server + const newProfile = await getProfile() as ExUserProfile + + //Apply the new profile to the buffer + profile.apply(newProfile) + + return data; + } + + const userProfile = useDataBuffer({} as any, updateUserProfile) + + const loadProfile = async () => { + //Get the user profile + const profile = await getProfile() as ExUserProfile + //Apply the profile to the buffer + userProfile.apply(profile) + } + + watch([loggedIn, onRefresh], ([li]) => { + //If the user is logged in, load the profile buffer + if (li) { + apiCall(loadProfile) + } + }) + + defer(refreshProfile); + + return { + userProfile, + refreshProfile, + userName + } + } +} \ No newline at end of file -- cgit