2021-02-14 20:18:51 +01:00
|
|
|
<template>
|
|
|
|
<x-button
|
2022-01-30 13:40:14 +01:00
|
|
|
v-if="isButton"
|
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"
|
2022-05-22 17:08:37 +02:00
|
|
|
:disabled="disabled || undefined"
|
2021-02-14 20:18:51 +01:00
|
|
|
>
|
|
|
|
{{ buttonText }}
|
|
|
|
</x-button>
|
2022-01-30 13:40:34 +01:00
|
|
|
<BaseButton
|
2022-01-30 13:40:14 +01:00
|
|
|
v-else
|
2021-02-14 20:18:51 +01:00
|
|
|
v-tooltip="tooltipText"
|
|
|
|
@click="changeSubscription"
|
|
|
|
:class="{'is-disabled': disabled}"
|
|
|
|
>
|
|
|
|
<span class="icon">
|
2022-01-29 22:30:47 +01:00
|
|
|
<icon :icon="iconName"/>
|
2021-02-14 20:18:51 +01:00
|
|
|
</span>
|
|
|
|
{{ buttonText }}
|
2022-01-30 13:40:34 +01:00
|
|
|
</BaseButton>
|
2021-02-14 20:18:51 +01:00
|
|
|
</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'
|
|
|
|
|
2022-01-30 13:40:34 +01:00
|
|
|
import BaseButton from '@/components/base/BaseButton.vue'
|
|
|
|
|
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'
|
|
|
|
|
2022-01-30 13:40:14 +01:00
|
|
|
interface Props {
|
|
|
|
entity: string
|
|
|
|
entityId: number
|
2022-04-24 17:06:00 +02:00
|
|
|
subscription: SubscriptionModel | null
|
2022-01-30 23:27:22 +01:00
|
|
|
isButton?: boolean
|
2022-01-30 13:40:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const props = withDefaults(defineProps<Props>(), {
|
|
|
|
isButton: true,
|
2022-04-24 17:06:00 +02:00
|
|
|
subscription: null,
|
2021-12-04 15:47:32 +01:00
|
|
|
})
|
2021-02-14 20:18:51 +01:00
|
|
|
|
2022-04-24 17:06:00 +02:00
|
|
|
const subscriptionEntity = computed<string | null>(() => props.subscription?.entity ?? null)
|
2022-01-16 22:24: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
|
|
|
|
2022-05-23 07:23:59 +02:00
|
|
|
const {t} = useI18n({useScope: 'global'})
|
2021-12-04 15:47:32 +01:00
|
|
|
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>
|