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 bf710dd352
commit 794c3ec520

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)
});
}).then(response => {
accounts.value = response;
console.log(accounts.value);
});
}
function menu(account, ev) {
os.popupMenu([{ os.popupMenu([{
text: this.$ts.switch, text: i18n.ts.switch,
icon: 'fas fa-exchange-alt', icon: 'fas fa-exchange-alt',
action: () => this.switchAccount(account), action: () => switchAccount(account),
}, { }, {
text: this.$ts.remove, text: i18n.ts.remove,
icon: 'fas fa-trash-alt', icon: 'fas fa-trash-alt',
danger: true, danger: true,
action: () => this.removeAccount(account), action: () => removeAccount(account),
}], ev.currentTarget ?? ev.target); }], ev.currentTarget ?? ev.target);
}, }
addAccount(ev) { function addAccount(ev) {
os.popupMenu([{ os.popupMenu([{
text: this.$ts.existingAccount, text: i18n.ts.existingAccount,
action: () => { this.addExistingAccount(); }, action: () => { addExistingAccount(); },
}, { }, {
text: this.$ts.createAccount, text: i18n.ts.createAccount,
action: () => { this.createAccount(); }, action: () => { createAccount(); },
}], ev.currentTarget ?? ev.target); }], ev.currentTarget ?? ev.target);
}, }
addExistingAccount() { function addExistingAccount() {
os.popup(defineAsyncComponent(() => import('@/components/signin-dialog.vue')), {}, { os.popup(defineAsyncComponent(() => import('@/components/signin-dialog.vue')), {}, {
done: res => { done: res => {
addAccount(res.id, res.i); addAccounts(res.id, res.i);
os.success(); os.success();
}, },
}, 'closed'); }, 'closed');
}, }
createAccount() { function createAccount() {
os.popup(defineAsyncComponent(() => import('@/components/signup-dialog.vue')), {}, { os.popup(defineAsyncComponent(() => import('@/components/signup-dialog.vue')), {}, {
done: res => { done: res => {
addAccount(res.id, res.i); addAccounts(res.id, res.i);
this.switchAccountWithToken(res.i); switchAccountWithToken(res.i);
}, },
}, 'closed'); }, 'closed');
}, }
async switchAccount(account: any) { async function switchAccount(account: any) {
const storedAccounts = await getAccounts(); const fetchedAccounts: any[] = await getAccounts();
const token = storedAccounts.find(x => x.id === account.id).token; const token = fetchedAccounts.find(x => x.id === account.id).token;
this.switchAccountWithToken(token); switchAccountWithToken(token);
}, }
switchAccountWithToken(token: string) { function switchAccountWithToken(token: string) {
login(token); login(token);
}, }
defineExpose({
[symbols.PAGE_INFO]: {
title: i18n.ts.accounts,
icon: 'fas fa-users',
bg: 'var(--bg)',
} }
}); });
</script> </script>