habitica-self-host/website/client/src/components/settings/api.vue

172 lines
5.4 KiB
Vue
Raw Normal View History

2019-10-12 14:33:05 +00:00
<template>
<div class="row standard-page">
<div class="col-6">
<h2>{{ $t('API') }}</h2><p>{{ $t('APIText') }}</p><div class="section">
<h6>{{ $t('userId') }}</h6><pre class="prettyprint">{{ user.id }}</pre><h6>{{ $t('APIToken') }}</h6><div class="d-flex align-items-center mb-3">
<button
class="btn btn-secondary"
@click="showApiToken = !showApiToken"
>
{{ $t(`${showApiToken ? 'hide' : 'show'}APIToken`) }}
</button><pre
v-if="showApiToken"
class="prettyprint ml-4 mb-0"
>{{ apiToken }}</pre>
</div><p v-html="$t('APITokenWarning', { hrefTechAssistanceEmail })"></p>
</div><div class="section">
<h3>{{ $t('thirdPartyApps') }}</h3><ul>
<li>
<a
target="_blank"
href="https://www.beeminder.com/habitica"
>{{ $t('beeminder') }}</a><br>{{ $t('beeminderDesc') }}
</li><li>
<a
target="_blank"
href="https://chrome.google.com/webstore/detail/habitrpg-chat-client/hidkdfgonpoaiannijofifhjidbnilbb"
>{{ $t('chromeChatExtension') }}</a><br>{{ $t('chromeChatExtensionDesc') }}
</li><li>
<a
target="_blank"
:href="`https://oldgods.net/habitica/habitrpg_user_data_display.html?uuid=` + user._id"
>{{ $t('dataTool') }}</a><br>{{ $t('dataToolDesc') }}
</li><li v-html="$t('otherExtensions')">
<br>{{ $t('otherDesc') }}
</li>
</ul><hr>
</div>
</div><div class="col-6">
<h2>{{ $t('webhooks') }}</h2><p v-html="$t('webhooksInfo')"></p><table class="table table-striped">
<thead v-if="user.webhooks.length">
<tr><th>{{ $t('enabled') }}</th><th>{{ $t('webhookURL') }}</th><th></th></tr>
</thead><tbody>
<tr v-for="(webhook, index) in user.webhooks">
<td>
<input
v-model="webhook.enabled"
type="checkbox"
@change="saveWebhook(webhook, index)"
>
</td><td>
<input
v-model="webhook.url"
class="form-control"
type="url"
>
</td><td>
<a
class="btn btn-warning checklist-icons"
@click="deleteWebhook(webhook, index)"
><span
class="glyphicon glyphicon-trash"
:tooltip="$t('delete')"
>Delete</span></a><a
class="btn btn-success checklist-icons"
@click="saveWebhook(webhook, index)"
>Update</a>
</td>
</tr><tr>
<td colspan="2">
<div class="form-horizontal">
<div class="form-group col-sm-10">
<input
v-model="newWebhook.url"
class="form-control"
type="url"
:placeholder="$t('webhookURL')"
>
</div><div class="col-sm-2">
<button
class="btn btn-sm btn-primary"
type="submit"
@click="addWebhook(newWebhook.url)"
>
{{ $t('add') }}
</button>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</template>
<style scoped>
.section {
margin-top: 2em;
}
</style>
<script>
2019-10-01 13:38:48 +00:00
import { mapState } from '@/libs/store';
2019-10-01 14:00:06 +00:00
import uuid from '@/../../common/script/libs/uuid';
// @TODO: env.EMAILS.TECH_ASSISTANCE_EMAIL
const TECH_ASSISTANCE_EMAIL = 'admin@habitica.com';
export default {
data () {
return {
newWebhook: {
url: '',
},
hrefTechAssistanceEmail: `<a href="mailto:${TECH_ASSISTANCE_EMAIL}">${TECH_ASSISTANCE_EMAIL}</a>`,
showApiToken: false,
};
},
computed: {
2019-10-09 18:08:36 +00:00
...mapState({ user: 'user.data', credentials: 'credentials' }),
apiToken () {
return this.credentials.API_TOKEN;
},
},
2019-10-11 18:35:49 +00:00
mounted () {
window.addEventListener('message', this.receiveMessage, false);
},
destroy () {
window.removeEventListener('message', this.receiveMessage);
},
methods: {
2018-06-29 16:35:22 +00:00
receiveMessage (eventFrom) {
if (eventFrom.origin !== 'https://www.spritely.app') return;
2018-06-29 16:35:22 +00:00
const creds = {
userId: this.user._id,
apiToken: this.credentials.API_TOKEN,
};
eventFrom.source.postMessage(creds, eventFrom.origin);
2018-06-29 16:35:22 +00:00
},
async addWebhook (url) {
2019-10-09 18:08:36 +00:00
const webhookInfo = {
id: uuid(),
type: 'taskActivity',
options: {
created: false,
updated: false,
deleted: false,
scored: true,
},
url,
enabled: true,
};
2019-10-09 18:08:36 +00:00
const webhook = await this.$store.dispatch('user:addWebhook', { webhookInfo });
this.user.webhooks.push(webhook);
this.newWebhook.url = '';
},
async saveWebhook (webhook, index) {
delete webhook._editing;
2019-10-09 18:08:36 +00:00
const updatedWebhook = await this.$store.dispatch('user:updateWebhook', { webhook });
this.user.webhooks[index] = updatedWebhook;
},
async deleteWebhook (webhook, index) {
delete webhook._editing;
2019-10-09 18:08:36 +00:00
await this.$store.dispatch('user:deleteWebhook', { webhook });
this.user.webhooks.splice(index, 1);
},
},
};
</script>