summaryrefslogtreecommitdiff
path: root/packages/client/src/pages/settings/accounts.vue
blob: a744a031d4f62fa038c115fcd922b5c5657bedbb (plain)
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
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
139
<template>
<div class="_formRoot">
	<FormSuspense :p="init">
		<FormButton primary @click="addAccount"><i class="fas fa-plus"></i> {{ $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"/>
				</div>
				<div class="acct">
					<MkAcct :user="account"/>
				</div>
			</div>
		</div>
	</FormSuspense>
</div>
</template>

<script lang="ts">
import { defineComponent } 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';
import { getAccounts, addAccount, login } from '@/account';

export default defineComponent({
	components: {
		FormSuspense,
		FormButton,
	},

	emits: ['info'],

	data() {
		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: {
		menu(account, ev) {
			os.popupMenu([{
				text: this.$ts.switch,
				icon: 'fas fa-exchange-alt',
				action: () => this.switchAccount(account),
			}, {
				text: this.$ts.remove,
				icon: 'fas fa-trash-alt',
				danger: true,
				action: () => this.removeAccount(account),
			}], ev.currentTarget ?? ev.target);
		},

		addAccount(ev) {
			os.popupMenu([{
				text: this.$ts.existingAccount,
				action: () => { this.addExistingAccount(); },
			}, {
				text: this.$ts.createAccount,
				action: () => { this.createAccount(); },
			}], ev.currentTarget ?? ev.target);
		},

		addExistingAccount() {
			os.popup(import('@/components/signin-dialog.vue'), {}, {
				done: res => {
					addAccount(res.id, res.i);
					os.success();
				},
			}, 'closed');
		},

		createAccount() {
			os.popup(import('@/components/signup-dialog.vue'), {}, {
				done: res => {
					addAccount(res.id, res.i);
					this.switchAccountWithToken(res.i);
				},
			}, 'closed');
		},

		async switchAccount(account: any) {
			const storedAccounts = await getAccounts();
			const token = storedAccounts.find(x => x.id === account.id).token;
			this.switchAccountWithToken(token);
		},

		switchAccountWithToken(token: string) {
			login(token);
		},
	}
});
</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>