-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUserMenu.js
More file actions
105 lines (98 loc) · 2.8 KB
/
UserMenu.js
File metadata and controls
105 lines (98 loc) · 2.8 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { computed, markRaw, onBeforeMount, reactive, toRefs, watch } from "vue";
import { useRouter } from 'vue-router'
import {dealUser, getUser, postLogout} from "../services/user";
import { Key, Setting, SwitchButton, UserFilled } from "@element-plus/icons-vue";
export default {
name: "UserMenu",
props: {
user: {
type: Object,
default: null
}
},
setup(props) {
const router = useRouter()
const normalizeUser = (input) => {
if (!input) {
return {
username: null,
email: null,
first_name: null,
last_name: null
}
}
if (input.data) {
return dealUser(input)
}
return {
username: input.username ?? null,
email: input.email ?? null,
first_name: input.first_name ?? null,
last_name: input.last_name ?? null
}
}
const state = reactive({
user :{
username: null,
email: null,
is_superuser: null
},
})
const logout = () => {
postLogout().then(res=>{
if (res.status == 200) {
sessionStorage.clear()
router.push({ path: '/'})
}
})
location.reload(true)
}
const accountTriggerIcon = markRaw(UserFilled)
const menuItems = computed(() => [
{
key: "settings",
label: "Settings",
href: "/account-setting",
icon: markRaw(Setting)
},
{
key: "access-tokens",
label: "Access Tokens",
href: "/access-tokens",
icon: markRaw(Key)
},
{
key: "sign-out",
label: "Sign Out",
actionKey: "logout",
icon: markRaw(SwitchButton)
}
])
const handleMenuAction = (actionKey) => {
if (actionKey === "logout") {
logout()
}
}
watch(
() => props.user,
(newUser) => {
if (newUser) {
state.user = normalizeUser(newUser)
}
},
{ immediate: true }
)
onBeforeMount(async () => {
if (!props.user) {
const res = await getUser()
state.user = normalizeUser(res)
}
})
return {
...toRefs(state),
accountTriggerIcon,
menuItems,
handleMenuAction
}
}
}