refactor(client): refactor settings/accounts to use Composition API (#8604)

This commit is contained in:
Andreas Nedbal 2022-05-03 13:33:40 +02:00 committed by GitHub
parent 8a5a99f01b
commit 0e26fae3bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 70 additions and 71 deletions

View File

@ -1,7 +1,7 @@
<template> <template>
<div class="_formRoot"> <div class="_formRoot">
<FormSuspense :p="init"> <FormSuspense :p="init">
<FormButton primary @click="addAccount"><i class="fas fa-plus"></i> {{ $ts.addAccount }}</FormButton> <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 v-for="account in accounts" :key="account.id" class="_panel _button lcjjdxlm" @click="menu(account, $event)">
<div class="avatar"> <div class="avatar">
@ -20,90 +20,89 @@
</div> </div>
</template> </template>
<script lang="ts"> <script lang="ts" setup>
import { defineAsyncComponent, defineComponent } from 'vue'; import { defineAsyncComponent, defineExpose, ref } from 'vue';
import FormSuspense from '@/components/form/suspense.vue'; import FormSuspense from '@/components/form/suspense.vue';
import FormButton from '@/components/ui/button.vue'; import FormButton from '@/components/ui/button.vue';
import * as os from '@/os'; import * as os from '@/os';
import * as symbols from '@/symbols'; import * as symbols from '@/symbols';
import { getAccounts, addAccount, login } from '@/account'; import { getAccounts, addAccount as addAccounts, login, $i } from '@/account';
import { i18n } from '@/i18n';
export default defineComponent({ const storedAccounts = ref<any>(null);
components: { const accounts = ref<any>(null);
FormSuspense,
FormButton,
},
emits: ['info'], const init = async () => {
getAccounts().then(accounts => {
storedAccounts.value = accounts.filter(x => x.id !== $i!.id);
data() { console.log(storedAccounts.value);
return {
[symbols.PAGE_INFO]: {
title: this.$ts.accounts,
icon: 'fas fa-users',
bg: 'var(--bg)',
},
storedAccounts: getAccounts().then(accounts => accounts.filter(x => x.id !== this.$i.id)),
accounts: null,
init: async () => os.api('users/show', {
userIds: (await this.storedAccounts).map(x => x.id)
}).then(accounts => {
this.accounts = accounts;
}),
};
},
methods: { return os.api('users/show', {
menu(account, ev) { userIds: storedAccounts.value.map(x => x.id)
os.popupMenu([{ });
text: this.$ts.switch, }).then(response => {
icon: 'fas fa-exchange-alt', accounts.value = response;
action: () => this.switchAccount(account), console.log(accounts.value);
}, { });
text: this.$ts.remove, }
icon: 'fas fa-trash-alt',
danger: true, function menu(account, ev) {
action: () => this.removeAccount(account), os.popupMenu([{
}], ev.currentTarget ?? ev.target); 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();
}, },
}, 'closed');
}
addAccount(ev) { function createAccount() {
os.popupMenu([{ os.popup(defineAsyncComponent(() => import('@/components/signup-dialog.vue')), {}, {
text: this.$ts.existingAccount, done: res => {
action: () => { this.addExistingAccount(); }, addAccounts(res.id, res.i);
}, { switchAccountWithToken(res.i);
text: this.$ts.createAccount,
action: () => { this.createAccount(); },
}], ev.currentTarget ?? ev.target);
}, },
}, 'closed');
}
addExistingAccount() { async function switchAccount(account: any) {
os.popup(defineAsyncComponent(() => import('@/components/signin-dialog.vue')), {}, { const fetchedAccounts: any[] = await getAccounts();
done: res => { const token = fetchedAccounts.find(x => x.id === account.id).token;
addAccount(res.id, res.i); switchAccountWithToken(token);
os.success(); }
},
}, 'closed');
},
createAccount() { function switchAccountWithToken(token: string) {
os.popup(defineAsyncComponent(() => import('@/components/signup-dialog.vue')), {}, { login(token);
done: res => { }
addAccount(res.id, res.i);
this.switchAccountWithToken(res.i);
},
}, 'closed');
},
async switchAccount(account: any) { defineExpose({
const storedAccounts = await getAccounts(); [symbols.PAGE_INFO]: {
const token = storedAccounts.find(x => x.id === account.id).token; title: i18n.ts.accounts,
this.switchAccountWithToken(token); icon: 'fas fa-users',
}, bg: 'var(--bg)',
switchAccountWithToken(token: string) {
login(token);
},
} }
}); });
</script> </script>