<template>
	<fragment>
		<NcAppNavigation>
			<NcAppContentList>
				<router-link to="/discover">
					<NcAppNavigationNew :text="t('Add a podcast')">
						<template #icon>
							<Plus :size="20" />
						</template>
					</NcAppNavigationNew>
				</router-link>
				<NcLoadingIcon v-if="loading" />
				<ul v-if="!loading">
					<SubscriptionListItem v-for="subscriptionUrl of subscriptions"
						:key="subscriptionUrl"
						:subscription-url="subscriptionUrl" />
				</ul>
			</NcAppContentList>
		</NcAppNavigation>
		<NcAppContent>
			<router-view />
		</NcAppContent>
	</fragment>
</template>

<script>
import {
	NcAppContent,
	NcAppContentList,
	NcAppNavigation,
	NcAppNavigationNew,
	NcLoadingIcon,
} from '@nextcloud/vue'
import Plus from 'vue-material-design-icons/Plus.vue'
import SubscriptionListItem from '../components/SubscriptionListItem.vue'
import { showError } from '@nextcloud/dialogs'

export default {
	name: 'Index',
	components: {
		NcAppContent,
		NcAppContentList,
		NcAppNavigation,
		NcAppNavigationNew,
		NcLoadingIcon,
		Plus,
		SubscriptionListItem,
	},
	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('Could not fetch subscriptions'))
		} finally {
			this.loading = false
		}
	},
}
</script>