refactor pages/follow.vue to composition API

This commit is contained in:
Johann150 2022-07-28 11:58:32 +02:00
parent bf16b3699e
commit 670c229cd0
Signed by untrusted user: Johann150
GPG key ID: 9EE6577A2A06F8F1

View file

@ -1,65 +1,75 @@
<template> <template>
<div class="mk-follow-page"> <!-- This page does not really have any content, it is mainly processing stuff -->
</div> <MkLoading v-if="state == 'loading'"/>
<MkError v-if="state == 'error'" :final="finalError" @retry="doIt"/>
</template> </template>
<script lang="ts"> <script lang="ts">
import { defineComponent } from 'vue'; import { } from 'vue';
import * as Acct from 'misskey-js/built/acct'; import * as Acct from 'misskey-js/built/acct';
import * as os from '@/os'; import * as os from '@/os';
import { mainRouter } from '@/router'; import { mainRouter } from '@/router';
import { i18n } from '@/i18n';
export default defineComponent({ let state: 'loading' | 'error' | 'done' = $ref('loading');
created() { let finalError: boolean = $ref(false);
const acct = new URL(location.href).searchParams.get('acct');
if (acct == null) return;
let promise; async function follow(user) {
const { canceled } = await os.confirm({
type: 'question',
text: i18n.t('followConfirm', { name: user.name || user.username }),
});
if (acct.startsWith('https://')) { if (canceled) {
promise = os.api('ap/show', { window.close();
uri: acct, return;
}); }
promise.then(res => {
if (res.type === 'User') {
this.follow(res.object);
} else if (res.type === 'Note') {
mainRouter.push(`/notes/${res.object.id}`);
} else {
os.alert({
type: 'error',
text: 'Not a user',
}).then(() => {
window.close();
});
}
});
} else {
promise = os.api('users/show', Acct.parse(acct));
promise.then(user => {
this.follow(user);
});
}
os.promiseDialog(promise, null, null, this.$ts.fetchingAsApObject); os.apiWithDialog('following/create', {
}, userId: user.id,
});
}
methods: { function doIt() {
async follow(user) { // this might be a retry
const { canceled } = await os.confirm({ state = 'loading';
type: 'question',
text: this.$t('followConfirm', { name: user.name || user.username }),
});
if (canceled) { const acct = new URL(location.href).searchParams.get('acct');
window.close(); if (acct == null) {
return; finalError = true;
state = 'error';
}
let promise;
if (acct.startsWith('https://')) {
promise = os.api('ap/show', {
uri: acct,
});
promise.then(res => {
if (res.type === 'User') {
follow(res.object);
} else if (res.type === 'Note') {
mainRouter.push(`/notes/${res.object.id}`);
} else {
os.alert({
type: 'error',
text: 'Not a user',
}).then(() => {
finalError = true;
state = 'error';
});
} }
});
os.apiWithDialog('following/create', { } else {
userId: user.id, promise = os.api('users/show', Acct.parse(acct));
}); promise.then(user => {
}, follow(user);
}, });
}); }
os.promiseDialog(promise, null, null, i18n.ts.fetchingAsApObject);
}
doIt();
</script> </script>