repod/src/components/Sidebar/Subscription.vue

151 lines
3.6 KiB
Vue
Raw Normal View History

2023-07-10 00:25:32 +02:00
<template>
<NcAppNavigationItem
:loading="loading"
:name="feed?.data?.title || url"
:to="toFeedUrl(url)">
2023-07-25 22:19:16 +02:00
<template #actions>
<NcActionButton
:aria-label="t('repod', 'Favorite')"
:model-value="feed?.isFavorite"
:name="t('repod', 'Favorite')"
:title="t('repod', 'Favorite')"
@update:modelValue="switchFavorite($event)">
<template #icon>
<StarPlusIcon v-if="!feed?.isFavorite" :size="20" />
<StarRemoveIcon v-if="feed?.isFavorite" :size="20" />
</template>
</NcActionButton>
<NcActionButton
:aria-label="t(`core`, 'Delete')"
:name="t(`core`, 'Delete')"
:title="t(`core`, 'Delete')"
@click="deleteSubscription">
2023-07-25 22:19:16 +02:00
<template #icon>
2024-03-16 18:35:31 +01:00
<DeleteIcon :size="20" />
2023-07-25 22:19:16 +02:00
</template>
</NcActionButton>
</template>
2024-01-30 18:11:50 +01:00
<template #icon>
<NcAvatar
:display-name="feed?.data?.author || feed?.data?.title"
2024-01-30 18:11:50 +01:00
:is-no-user="true"
:url="feed?.data?.imageUrl" />
<StarIcon v-if="feed?.isFavorite" class="star" :size="20" />
<AlertIcon v-if="failed" />
2024-01-30 18:11:50 +01:00
</template>
2023-07-10 00:25:32 +02:00
</NcAppNavigationItem>
</template>
<script lang="ts">
2023-07-29 17:53:51 +02:00
import { NcActionButton, NcAppNavigationItem, NcAvatar } from '@nextcloud/vue'
import { mapActions, mapState } from 'pinia'
import AlertIcon from 'vue-material-design-icons/Alert.vue'
2024-03-16 18:35:31 +01:00
import DeleteIcon from 'vue-material-design-icons/Delete.vue'
import type { PersonalSettingsPodcastDataInterface } from '../../utils/types.ts'
import StarIcon from 'vue-material-design-icons/Star.vue'
import StarPlusIcon from 'vue-material-design-icons/StarPlus.vue'
import StarRemoveIcon from 'vue-material-design-icons/StarRemove.vue'
2023-07-10 00:25:32 +02:00
import axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'
import { showError } from '../../utils/toast.ts'
import { t } from '@nextcloud/l10n'
import { toFeedUrl } from '../../utils/url.ts'
import { useSubscriptions } from '../../store/subscriptions.ts'
2023-07-10 00:25:32 +02:00
export default {
name: 'Subscription',
2023-07-10 00:25:32 +02:00
components: {
AlertIcon,
2024-03-16 18:35:31 +01:00
DeleteIcon,
2023-07-25 22:19:16 +02:00
NcActionButton,
2023-07-10 00:25:32 +02:00
NcAppNavigationItem,
2023-07-29 17:53:51 +02:00
NcAvatar,
StarIcon,
StarPlusIcon,
StarRemoveIcon,
2023-07-10 00:25:32 +02:00
},
props: {
url: {
type: String,
2023-07-10 00:25:32 +02:00
required: true,
},
},
2024-10-23 13:42:56 +02:00
data() {
return {
failed: false,
loading: true,
2023-07-10 00:25:32 +02:00
}
},
2024-10-23 13:42:56 +02:00
computed: {
...mapState(useSubscriptions, ['getSubByUrl', 'subs']),
feed() {
return this.getSubByUrl(this.url)
},
},
async mounted() {
try {
const podcastData =
await axios.get<PersonalSettingsPodcastDataInterface>(
generateUrl(
'/apps/gpoddersync/personal_settings/podcast_data?url={url}',
{
url: this.url,
},
),
)
this.addMetadatas(this.url, podcastData.data.data)
} catch (e) {
this.failed = true
console.error(e)
} finally {
this.loading = false
}
2024-10-23 13:42:56 +02:00
},
2023-07-25 22:19:16 +02:00
methods: {
...mapActions(useSubscriptions, ['fetch', 'addMetadatas', 'setFavorite']),
t,
toFeedUrl,
2023-07-25 22:19:16 +02:00
async deleteSubscription() {
if (
confirm(
t('repod', 'Are you sure you want to delete this subscription?'),
)
) {
try {
this.loading = true
await axios.post(
generateUrl('/apps/gpoddersync/subscription_change/create'),
{ add: [], remove: [this.url] },
)
} catch (e) {
console.error(e)
showError(t('repod', 'Error while removing the feed'))
} finally {
this.setFavorite(this.url, false)
this.loading = false
this.fetch()
}
2023-07-25 22:19:16 +02:00
}
},
switchFavorite(value: boolean) {
if (value) {
if (this.subs.filter((sub) => sub.isFavorite).length >= 10) {
showError(t('repod', 'You can only have 10 favorites'))
return
}
}
this.setFavorite(this.url, value)
},
2023-07-25 22:19:16 +02:00
},
2023-07-10 00:25:32 +02:00
}
</script>
<style scoped>
.star {
bottom: 2px;
color: yellow;
left: 22px;
position: absolute;
}
</style>