diff options
author | vnugent <public@vaughnnugent.com> | 2023-12-13 17:58:51 -0500 |
---|---|---|
committer | vnugent <public@vaughnnugent.com> | 2023-12-13 17:58:51 -0500 |
commit | 4b8ae76132d2342f40cec703b3d5145ea075c451 (patch) | |
tree | 62b942b6181261566cd3245ee35cd15a138aabf2 /front-end/src/store/userProfile.ts | |
parent | b564708f29cf8a709c3e3d981477b2ec8440673e (diff) |
log time coming ui and lib updates
Diffstat (limited to 'front-end/src/store/userProfile.ts')
-rw-r--r-- | front-end/src/store/userProfile.ts | 82 |
1 files changed, 82 insertions, 0 deletions
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<OAuth2Application, WebMessage<string>> +} + +interface ExUserProfile extends UserProfile { + created: string | Date +} + +declare module 'pinia' { + export interface PiniaCustomProperties { + userProfile: ServerDataBuffer<ExUserProfile, WebMessage<string>> + userName: string | undefined + refreshProfile(): void; + } +} + +export const profilePlugin = (accountsUrl:MaybeRef<string>) :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<ExUserProfile>) => { + // Apply the buffer to the profile + const { data } = await axios.post<WebMessage<string>>(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 |