2021-02-14 20:18:51 +01:00
|
|
|
<template>
|
|
|
|
<x-button
|
|
|
|
type="secondary"
|
|
|
|
:icon="icon"
|
|
|
|
v-tooltip="tooltipText"
|
|
|
|
@click="changeSubscription"
|
2021-10-17 18:52:05 +02:00
|
|
|
:disabled="disabled || null"
|
2021-02-14 20:18:51 +01:00
|
|
|
v-if="isButton"
|
|
|
|
>
|
|
|
|
{{ buttonText }}
|
|
|
|
</x-button>
|
|
|
|
<a
|
|
|
|
v-tooltip="tooltipText"
|
|
|
|
@click="changeSubscription"
|
|
|
|
:class="{'is-disabled': disabled}"
|
|
|
|
v-else
|
|
|
|
>
|
|
|
|
<span class="icon">
|
|
|
|
<icon :icon="icon"/>
|
|
|
|
</span>
|
|
|
|
{{ buttonText }}
|
|
|
|
</a>
|
|
|
|
</template>
|
|
|
|
|
2021-12-04 15:47:32 +01:00
|
|
|
<script lang="ts" setup>
|
|
|
|
import {computed, shallowRef} from 'vue'
|
|
|
|
import {useI18n} from 'vue-i18n'
|
|
|
|
|
2021-02-14 20:18:51 +01:00
|
|
|
import SubscriptionService from '@/services/subscription'
|
|
|
|
import SubscriptionModel from '@/models/subscription'
|
|
|
|
|
2021-12-04 15:47:32 +01:00
|
|
|
import {success} from '@/message'
|
|
|
|
|
|
|
|
const props = defineProps({
|
|
|
|
entity: {
|
|
|
|
required: true,
|
|
|
|
type: String,
|
|
|
|
},
|
|
|
|
subscription: {
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
entityId: {
|
|
|
|
required: true,
|
2021-02-14 20:18:51 +01:00
|
|
|
},
|
2021-12-04 15:47:32 +01:00
|
|
|
isButton: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true,
|
2021-02-14 20:18:51 +01:00
|
|
|
},
|
2021-12-04 15:47:32 +01:00
|
|
|
})
|
2021-02-14 20:18:51 +01:00
|
|
|
|
2021-12-04 15:47:32 +01:00
|
|
|
const emit = defineEmits(['change'])
|
2021-02-14 20:18:51 +01:00
|
|
|
|
2021-12-04 15:47:32 +01:00
|
|
|
const subscriptionService = shallowRef(new SubscriptionService())
|
2021-02-14 20:18:51 +01:00
|
|
|
|
2021-12-04 15:47:32 +01:00
|
|
|
const {t} = useI18n()
|
|
|
|
const tooltipText = computed(() => {
|
|
|
|
if (disabled.value) {
|
|
|
|
return t('task.subscription.subscribedThroughParent', {
|
|
|
|
entity: props.entity,
|
|
|
|
parent: props.subscription.entity,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return props.subscription !== null ?
|
|
|
|
t('task.subscription.subscribed', {entity: props.entity}) :
|
|
|
|
t('task.subscription.notSubscribed', {entity: props.entity})
|
|
|
|
})
|
|
|
|
|
|
|
|
const buttonText = computed(() => props.subscription !== null ? t('task.subscription.unsubscribe') : t('task.subscription.subscribe'))
|
|
|
|
const icon = computed(() => props.subscription !== null ? ['far', 'bell-slash'] : 'bell')
|
|
|
|
const disabled = computed(() => {
|
|
|
|
if (props.subscription === null) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return props.subscription.entity !== props.entity
|
|
|
|
})
|
|
|
|
|
|
|
|
function changeSubscription() {
|
|
|
|
if (disabled.value) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (props.subscription === null) {
|
|
|
|
subscribe()
|
|
|
|
} else {
|
|
|
|
unsubscribe()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function subscribe() {
|
|
|
|
const subscription = new SubscriptionModel({
|
|
|
|
entity: props.entity,
|
|
|
|
entityId: props.entityId,
|
|
|
|
})
|
|
|
|
await subscriptionService.value.create(subscription)
|
|
|
|
emit('change', subscription)
|
|
|
|
success({message: t('task.subscription.subscribeSuccess', {entity: props.entity})})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function unsubscribe() {
|
|
|
|
const subscription = new SubscriptionModel({
|
|
|
|
entity: props.entity,
|
|
|
|
entityId: props.entityId,
|
|
|
|
})
|
|
|
|
await subscriptionService.value.delete(subscription)
|
|
|
|
emit('change', null)
|
|
|
|
success({message: t('task.subscription.unsubscribeSuccess', {entity: props.entity})})
|
2021-02-14 20:18:51 +01:00
|
|
|
}
|
|
|
|
</script>
|