<template>
	<ul>
		<NcAppNavigationNewItem :loading="loading"
			:title="t('Add a RSS link')"
			@new-item="addSubscription">
			<template #icon>
				<Plus :size="20" />
			</template>
		</NcAppNavigationNewItem>
	</ul>
</template>

<script>
import { NcAppNavigationNewItem } from '@nextcloud/vue'
import Plus from 'vue-material-design-icons/Plus.vue'
import axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'
import { showError } from '@nextcloud/dialogs'

export default {
	name: 'AddRss',
	components: {
		NcAppNavigationNewItem,
		Plus,
	},
	data() {
		return {
			loading: false,
			feedUrl: '',
		}
	},
	methods: {
		async addSubscription(feedUrl) {
			this.loading = true

			try {
				await axios.post(generateUrl('/apps/gpoddersync/subscription_change/create'), { add: [feedUrl], remove: [] })
			} catch (e) {
				console.error(e)
				showError(t('Error while adding the feed'))
			}

			this.loading = false

			this.$store.dispatch('subscriptions/fetch')
		},
	},
}
</script>
