1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
}
}
}
|