Refactor integration to use Composition API (#8581)

* refactor(client): refactor integration to use Composition API

* fix(client): drop superfluous enable* constants

* refactor(client): deduplicate window opening for services
This commit is contained in:
Andreas Nedbal 2022-05-01 04:23:12 +02:00 committed by GitHub
parent 766559c6e9
commit 3dc027bcd5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 66 additions and 101 deletions

View File

@ -1,133 +1,98 @@
<template> <template>
<div class="_formRoot"> <div class="_formRoot">
<FormSection v-if="enableTwitterIntegration"> <FormSection v-if="instance.enableTwitterIntegration">
<template #label><i class="fab fa-twitter"></i> Twitter</template> <template #label><i class="fab fa-twitter"></i> Twitter</template>
<p v-if="integrations.twitter">{{ $ts.connectedTo }}: <a :href="`https://twitter.com/${integrations.twitter.screenName}`" rel="nofollow noopener" target="_blank">@{{ integrations.twitter.screenName }}</a></p> <p v-if="integrations.twitter">{{ i18n.ts.connectedTo }}: <a :href="`https://twitter.com/${integrations.twitter.screenName}`" rel="nofollow noopener" target="_blank">@{{ integrations.twitter.screenName }}</a></p>
<MkButton v-if="integrations.twitter" danger @click="disconnectTwitter">{{ $ts.disconnectService }}</MkButton> <MkButton v-if="integrations.twitter" danger @click="disconnectTwitter">{{ i18n.ts.disconnectService }}</MkButton>
<MkButton v-else primary @click="connectTwitter">{{ $ts.connectService }}</MkButton> <MkButton v-else primary @click="connectTwitter">{{ i18n.ts.connectService }}</MkButton>
</FormSection> </FormSection>
<FormSection v-if="enableDiscordIntegration"> <FormSection v-if="instance.enableDiscordIntegration">
<template #label><i class="fab fa-discord"></i> Discord</template> <template #label><i class="fab fa-discord"></i> Discord</template>
<p v-if="integrations.discord">{{ $ts.connectedTo }}: <a :href="`https://discord.com/users/${integrations.discord.id}`" rel="nofollow noopener" target="_blank">@{{ integrations.discord.username }}#{{ integrations.discord.discriminator }}</a></p> <p v-if="integrations.discord">{{ i18n.ts.connectedTo }}: <a :href="`https://discord.com/users/${integrations.discord.id}`" rel="nofollow noopener" target="_blank">@{{ integrations.discord.username }}#{{ integrations.discord.discriminator }}</a></p>
<MkButton v-if="integrations.discord" danger @click="disconnectDiscord">{{ $ts.disconnectService }}</MkButton> <MkButton v-if="integrations.discord" danger @click="disconnectDiscord">{{ i18n.ts.disconnectService }}</MkButton>
<MkButton v-else primary @click="connectDiscord">{{ $ts.connectService }}</MkButton> <MkButton v-else primary @click="connectDiscord">{{ i18n.ts.connectService }}</MkButton>
</FormSection> </FormSection>
<FormSection v-if="enableGithubIntegration"> <FormSection v-if="instance.enableGithubIntegration">
<template #label><i class="fab fa-github"></i> GitHub</template> <template #label><i class="fab fa-github"></i> GitHub</template>
<p v-if="integrations.github">{{ $ts.connectedTo }}: <a :href="`https://github.com/${integrations.github.login}`" rel="nofollow noopener" target="_blank">@{{ integrations.github.login }}</a></p> <p v-if="integrations.github">{{ i18n.ts.connectedTo }}: <a :href="`https://github.com/${integrations.github.login}`" rel="nofollow noopener" target="_blank">@{{ integrations.github.login }}</a></p>
<MkButton v-if="integrations.github" danger @click="disconnectGithub">{{ $ts.disconnectService }}</MkButton> <MkButton v-if="integrations.github" danger @click="disconnectGithub">{{ i18n.ts.disconnectService }}</MkButton>
<MkButton v-else primary @click="connectGithub">{{ $ts.connectService }}</MkButton> <MkButton v-else primary @click="connectGithub">{{ i18n.ts.connectService }}</MkButton>
</FormSection> </FormSection>
</div> </div>
</template> </template>
<script lang="ts"> <script lang="ts" setup>
import { defineComponent } from 'vue'; import { computed, defineExpose, onMounted, ref, watch } from 'vue';
import { apiUrl } from '@/config'; import { apiUrl } from '@/config';
import FormSection from '@/components/form/section.vue'; import FormSection from '@/components/form/section.vue';
import MkButton from '@/components/ui/button.vue'; import MkButton from '@/components/ui/button.vue';
import * as os from '@/os';
import * as symbols from '@/symbols'; import * as symbols from '@/symbols';
import { $i } from '@/account';
import { instance } from '@/instance';
import { i18n } from '@/i18n';
export default defineComponent({ const twitterForm = ref<Window | null>(null);
components: { const discordForm = ref<Window | null>(null);
FormSection, const githubForm = ref<Window | null>(null);
MkButton
},
emits: ['info'], const integrations = computed(() => $i!.integrations);
data() { function openWindow(service: string, type: string) {
return { return window.open(`${apiUrl}/${type}/${service}`,
[symbols.PAGE_INFO]: { `${service}_${type}_window`,
title: this.$ts.integration, 'height=570, width=520'
icon: 'fas fa-share-alt', );
bg: 'var(--bg)', }
},
apiUrl,
twitterForm: null,
discordForm: null,
githubForm: null,
enableTwitterIntegration: false,
enableDiscordIntegration: false,
enableGithubIntegration: false,
};
},
computed: { function connectTwitter() {
integrations() { twitterForm.value = openWindow('twitter', 'connect');
return this.$i.integrations; }
},
meta() { function disconnectTwitter() {
return this.$instance; openWindow('twitter', 'disconnect');
}, }
},
created() { function connectDiscord() {
this.enableTwitterIntegration = this.meta.enableTwitterIntegration; discordForm.value = openWindow('discord', 'connect');
this.enableDiscordIntegration = this.meta.enableDiscordIntegration; }
this.enableGithubIntegration = this.meta.enableGithubIntegration;
},
mounted() { function disconnectDiscord() {
document.cookie = `igi=${this.$i.token}; path=/;` + openWindow('discord', 'disconnect');
}
function connectGithub() {
githubForm.value = openWindow('github', 'connect');
}
function disconnectGithub() {
openWindow('github', 'disconnect');
}
onMounted(() => {
document.cookie = `igi=${$i!.token}; path=/;` +
` max-age=31536000;` + ` max-age=31536000;` +
(document.location.protocol.startsWith('https') ? ' secure' : ''); (document.location.protocol.startsWith('https') ? ' secure' : '');
this.$watch('integrations', () => { watch(integrations, () => {
if (this.integrations.twitter) { if (integrations.value.twitter) {
if (this.twitterForm) this.twitterForm.close(); if (twitterForm.value) twitterForm.value.close();
} }
if (this.integrations.discord) { if (integrations.value.discord) {
if (this.discordForm) this.discordForm.close(); if (discordForm.value) discordForm.value.close();
} }
if (this.integrations.github) { if (integrations.value.github) {
if (this.githubForm) this.githubForm.close(); if (githubForm.value) githubForm.value.close();
} }
}, {
deep: true
}); });
}, });
methods: { defineExpose({
connectTwitter() { [symbols.PAGE_INFO]: {
this.twitterForm = window.open(apiUrl + '/connect/twitter', title: i18n.ts.integration,
'twitter_connect_window', icon: 'fas fa-share-alt',
'height=570, width=520'); bg: 'var(--bg)',
},
disconnectTwitter() {
window.open(apiUrl + '/disconnect/twitter',
'twitter_disconnect_window',
'height=570, width=520');
},
connectDiscord() {
this.discordForm = window.open(apiUrl + '/connect/discord',
'discord_connect_window',
'height=570, width=520');
},
disconnectDiscord() {
window.open(apiUrl + '/disconnect/discord',
'discord_disconnect_window',
'height=570, width=520');
},
connectGithub() {
this.githubForm = window.open(apiUrl + '/connect/github',
'github_connect_window',
'height=570, width=520');
},
disconnectGithub() {
window.open(apiUrl + '/disconnect/github',
'github_disconnect_window',
'height=570, width=520');
},
} }
}); });
</script> </script>