Newer
Older
Andreas Nedbal
committed
<FormButton primary @click="addAccount"><i class="fas fa-plus"></i> {{ i18n.ts.addAccount }}</FormButton>
<div v-for="account in accounts" :key="account.id" class="_panel _button lcjjdxlm" @click="menu(account, $event)">
<div class="avatar">
<MkAvatar :user="account" class="avatar"/>
</div>
<div class="body">
<div class="name">
<MkUserName :user="account"/>
Andreas Nedbal
committed
<script lang="ts" setup>
import { defineAsyncComponent, defineExpose, ref } from 'vue';
import FormSuspense from '@/components/form/suspense.vue';
import FormButton from '@/components/ui/button.vue';
import * as os from '@/os';
import * as symbols from '@/symbols';
Andreas Nedbal
committed
import { getAccounts, addAccount as addAccounts, login, $i } from '@/account';
import { i18n } from '@/i18n';
Andreas Nedbal
committed
const storedAccounts = ref<any>(null);
const accounts = ref<any>(null);
Andreas Nedbal
committed
const init = async () => {
getAccounts().then(accounts => {
storedAccounts.value = accounts.filter(x => x.id !== $i!.id);
Andreas Nedbal
committed
console.log(storedAccounts.value);
Andreas Nedbal
committed
return os.api('users/show', {
userIds: storedAccounts.value.map(x => x.id)
});
}).then(response => {
accounts.value = response;
console.log(accounts.value);
});
Andreas Nedbal
committed
function menu(account, ev) {
os.popupMenu([{
text: i18n.ts.switch,
icon: 'fas fa-exchange-alt',
action: () => switchAccount(account),
}, {
text: i18n.ts.remove,
icon: 'fas fa-trash-alt',
danger: true,
action: () => removeAccount(account),
}], ev.currentTarget ?? ev.target);
}
function addAccount(ev) {
os.popupMenu([{
text: i18n.ts.existingAccount,
action: () => { addExistingAccount(); },
}, {
text: i18n.ts.createAccount,
action: () => { createAccount(); },
}], ev.currentTarget ?? ev.target);
}
function addExistingAccount() {
os.popup(defineAsyncComponent(() => import('@/components/signin-dialog.vue')), {}, {
done: res => {
addAccounts(res.id, res.i);
os.success();
Andreas Nedbal
committed
}, 'closed');
}
Andreas Nedbal
committed
function createAccount() {
os.popup(defineAsyncComponent(() => import('@/components/signup-dialog.vue')), {}, {
done: res => {
addAccounts(res.id, res.i);
switchAccountWithToken(res.i);
Andreas Nedbal
committed
}, 'closed');
}
async function switchAccount(account: any) {
const fetchedAccounts: any[] = await getAccounts();
const token = fetchedAccounts.find(x => x.id === account.id).token;
switchAccountWithToken(token);
}
function switchAccountWithToken(token: string) {
login(token);
}
defineExpose({
[symbols.PAGE_INFO]: {
title: i18n.ts.accounts,
icon: 'fas fa-users',
bg: 'var(--bg)',
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
}
});
</script>
<style lang="scss" scoped>
.lcjjdxlm {
display: flex;
padding: 16px;
> .avatar {
display: block;
flex-shrink: 0;
margin: 0 12px 0 0;
> .avatar {
width: 50px;
height: 50px;
}
}
> .body {
display: flex;
flex-direction: column;
justify-content: center;
width: calc(100% - 62px);
position: relative;
> .name {
font-weight: bold;
}
}
}
</style>