<template>
	<AppNavigation>
		<template #list>
			<NcAppContentList>
				<router-link to="/">
					<NcAppNavigationNew :text="t('repod', 'Add a podcast')">
						<template #icon>
							<PlusIcon :size="20" />
						</template>
					</NcAppNavigationNew>
				</router-link>
				<Loading v-if="loading" />
				<NcAppNavigationList v-if="!loading">
					<Item v-for="subscriptionUrl of subscriptions"
						:key="subscriptionUrl"
						:url="subscriptionUrl" />
				</NcAppNavigationList>
			</NcAppContentList>
		</template>
		<template #footer>
			<Settings />
		</template>
	</AppNavigation>
</template>

<script>
import { NcAppContentList, NcAppNavigationList, NcAppNavigationNew } from '@nextcloud/vue'
import AppNavigation from '../Atoms/AppNavigation.vue'
import Item from './Item.vue'
import Loading from '../Atoms/Loading.vue'
import PlusIcon from 'vue-material-design-icons/Plus.vue'
import Settings from '../Settings/Settings.vue'
import { showError } from '@nextcloud/dialogs'

export default {
	name: 'Subscriptions',
	components: {
		AppNavigation,
		Item,
		Loading,
		NcAppContentList,
		NcAppNavigationList,
		NcAppNavigationNew,
		PlusIcon,
		Settings,
	},
	data() {
		return {
			loading: true,
		}
	},
	computed: {
		subscriptions() {
			return this.$store.state.subscriptions.subscriptions
		},
	},
	async mounted() {
		try {
			await this.$store.dispatch('subscriptions/fetch')
		} catch (e) {
			console.error(e)
			showError(t('repod', 'Could not fetch subscriptions'))
		} finally {
			this.loading = false
		}
	},
}
</script>
