Skip to content
Snippets Groups Projects
Dashboard.vue 3.25 KiB
Newer Older
onny's avatar
onny committed
<!--
  - @copyright Copyright (c) 2021 Jonas Heinrich
onny's avatar
onny committed
  -
  - @author Jonas Heinrich <onny@project-insanity.org>
  -
  - @license GNU AGPL version 3 or any later version
  -
  - This program is free software: you can redistribute it and/or modify
  - it under the terms of the GNU Affero General Public License as
  - published by the Free Software Foundation, either version 3 of the
  - License, or (at your option) any later version.
  -
  - This program is distributed in the hope that it will be useful,
  - but WITHOUT ANY WARRANTY; without even the implied warranty of
  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  - GNU Affero General Public License for more details.
  -
  - You should have received a copy of the GNU Affero General Public License
  - along with this program. If not, see <http://www.gnu.org/licenses/>.
  -
  -->

<template>
	<DashboardWidget :items="items"
onny's avatar
onny committed
		:show-more-text="t('podcast', 'episodes')"
onny's avatar
onny committed
		:show-more-url="showMoreUrl"
		:loading="state === 'loading'">
		<template #empty-content>
			<EmptyContent
				v-if="emptyContentMessage"
				:icon="emptyContentIcon">
				<template #desc>
					{{ emptyContentMessage }}
				</template>
			</EmptyContent>
		</template>
	</DashboardWidget>
</template>

<script>
import axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'
import { showError } from '@nextcloud/dialogs'
import { DashboardWidget } from '@nextcloud/vue-dashboard'
import EmptyContent from '@nextcloud/vue/dist/Components/EmptyContent'

const requesttoken = axios.defaults.headers.requesttoken

export default {
	name: 'Dashboard',

	components: {
		DashboardWidget,
		EmptyContent,
	},

	data() {
		return {
			notifications: [],
onny's avatar
onny committed
			showMoreUrl: generateUrl('/apps/podcast/#/favorites'),
onny's avatar
onny committed
			state: 'loading',
			darkThemeColor: OCA.Accessibility.theme === 'dark' ? 'ffffff' : '000000',
		}
	},

	computed: {
		items() {
			return this.notifications.map((n) => {
				return {
					id: n.id,
onny's avatar
onny committed
					targetUrl: generateUrl('/apps/podcast/#/favorites'),
onny's avatar
onny committed
					avatarUrl: n.favicon,
					mainText: n.name,
					subText: n.tags.replaceAll(',', ', '),
				}
			})
		},
		emptyContentMessage() {
			if (this.state === 'error') {
onny's avatar
onny committed
				return t('podcast', 'Error fetching favorite episodes')
onny's avatar
onny committed
			} else if (this.state === 'ok') {
onny's avatar
onny committed
				return t('podcast', 'No episodes added yet!')
onny's avatar
onny committed
			}
			return ''
		},
		emptyContentIcon() {
			if (this.state === 'error') {
				return 'icon-close'
			} else if (this.state === 'ok') {
				return 'icon-checkmark'
			}
			return 'icon-checkmark'
		},
	},

	beforeMount() {
		this.fetchNotifications()
	},

	methods: {
		fetchNotifications() {
			const req = {}
			axios.defaults.headers.requesttoken = requesttoken
onny's avatar
onny committed
			axios.get(generateUrl('/apps/podcast/api/favorites'), req).then((response) => {
onny's avatar
onny committed
				this.processNotifications(response.data)
				this.state = 'ok'
			}).catch((error) => {
				clearInterval(this.loop)
				if (error.response && error.response.status === 401) {
onny's avatar
onny committed
					showError(t('podcast', 'Failed to fetch favorite podcast episodes'))
onny's avatar
onny committed
					this.state = 'error'
				} else {
					// there was an error in notif processing
					console.debug(error)
				}
			})
		},
		processNotifications(newNotifications) {
			console.log(newNotifications)
			this.notifications = newNotifications
		},
	},
}
</script>