2021-02-14 20:18:51 +01:00
|
|
|
<template>
|
|
|
|
<x-button
|
2022-01-04 19:58:06 +01:00
|
|
|
variant="secondary"
|
2022-01-29 22:30:47 +01:00
|
|
|
:icon="iconName"
|
2021-02-14 20:18:51 +01:00
|
|
|
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">
|
2022-01-29 22:30:47 +01:00
|
|
|
<icon :icon="iconName"/>
|
2021-02-14 20:18:51 +01:00
|
|
|
</span>
|
|
|
|
{{ buttonText }}
|
|
|
|
</a>
|
|
|
|
</template>
|
|
|
|
|
2021-12-04 15:47:32 +01:00
|
|
|
<script lang="ts" setup>
|
2022-01-19 23:07:02 +01:00
|
|
|
import {computed, shallowRef} from 'vue'
|
2021-12-04 15:47:32 +01:00
|
|
|
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,
|
2022-01-19 23:07:02 +01:00
|
|
|
type: Object,
|
2022-01-18 22:26:27 +01:00
|
|
|
validator(value) {
|
|
|
|
return value instanceof SubscriptionModel || value === null
|
2022-01-19 23:07:02 +01:00
|
|
|
},
|
2021-12-04 15:47:32 +01:00
|
|
|
},
|
|
|
|
entityId: {
|
|
|
|
required: true,
|
2022-01-16 22:24:51 +01:00
|
|
|
type: Number,
|
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
|
|
|
|
2022-01-16 22:24:51 +01:00
|
|
|
const subscriptionEntity = computed<string>(() => props.subscription.entity)
|
|
|
|
|
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,
|
2022-01-16 22:24:51 +01:00
|
|
|
parent: subscriptionEntity.value,
|
2021-12-04 15:47:32 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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'))
|
2022-01-29 22:30:47 +01:00
|
|
|
const iconName = computed(() => props.subscription !== null ? ['far', 'bell-slash'] : 'bell')
|
2021-12-04 15:47:32 +01:00
|
|
|
const disabled = computed(() => {
|
|
|
|
if (props.subscription === null) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-01-16 22:24:51 +01:00
|
|
|
return subscriptionEntity.value !== props.entity
|
2021-12-04 15:47:32 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
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>
|