Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dashboard widget to list relevant polls #2645

Merged
merged 4 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use OCP\Notification\IManager as NotificationManager;
use OCP\Group\Events\GroupDeletedEvent;
use OCP\User\Events\UserDeletedEvent;
use OCA\Polls\Dashboard\PollWidget;
use OCA\Polls\Event\CommentAddEvent;
use OCA\Polls\Event\CommentDeleteEvent;
use OCA\Polls\Event\OptionConfirmedEvent;
Expand Down Expand Up @@ -109,6 +110,7 @@ public function register(IRegistrationContext $context): void {
$context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class);
$context->registerEventListener(GroupDeletedEvent::class, GroupDeletedListener::class);
$context->registerSearchProvider(SearchProvider::class);
$context->registerDashboardWidget(PollWidget::class);
}

public function registerNotifications(NotificationManager $notificationManager): void {
Expand Down
65 changes: 65 additions & 0 deletions lib/Dashboard/PollWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* @copyright Copyright (c) 2022 Michael Longo <contact@tiller.fr>
*
* @author Michael Longo <contact@tiller.fr>
*
* @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/>.
*
*/

namespace OCA\Polls\Dashboard;

use OCP\Dashboard\IWidget;
use OCP\IL10N;
use OCP\IURLGenerator;

class PollWidget implements IWidget {
private IL10N $l10n;
private IURLGenerator $urlGenerator;

public function __construct(
IL10N $l10n,
IURLGenerator $urlGenerator
) {
$this->l10n = $l10n;
$this->urlGenerator = $urlGenerator;
}

public function getId(): string {
return 'polls';
}

public function getTitle(): string {
return $this->l10n->t('Polls');
}

public function getOrder(): int {
return 50;
}

public function getIconClass(): string {
return 'icon-polls';
}

public function getUrl(): ?string {
return $this->urlGenerator->linkToRouteAbsolute('polls.page.index');
}

public function load(): void {
\OCP\Util::addScript('polls', 'polls-dashboard');
}
}
7 changes: 7 additions & 0 deletions src/js/assets/scss/dashboard.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.icon-polls {
background-image: url(../../../../img/polls-black.svg);
}

body.dark .icon-polls {
background-image: url(../../../../img/polls.svg);
}
53 changes: 53 additions & 0 deletions src/js/init-dashboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* jshint esversion: 6 */
/**
* @copyright Copyright (c) 2022 Michael Longo <contact@tiller.fr>
*
* @author Michael Longo <contact@tiller.fr>
*
* @license AGPL-3.0-or-later
*
* 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/>.
*
*/

import Vue from 'vue'
import store from './store/index.js'
import { getRequestToken, getCurrentUser } from '@nextcloud/auth'
import { translate, translatePlural } from '@nextcloud/l10n'
import { generateFilePath } from '@nextcloud/router'

import Dashboard from './views/Dashboard.vue'
import './assets/scss/dashboard.scss'

Vue.config.debug = process.env.NODE_ENV !== 'production'
Vue.config.devTools = process.env.NODE_ENV !== 'production'

/* eslint-disable-next-line camelcase, no-undef */
__webpack_nonce__ = btoa(getRequestToken())
/* eslint-disable-next-line camelcase, no-undef */
__webpack_public_path__ = generateFilePath('polls', '', 'js/')

Vue.prototype.t = translate
Vue.prototype.n = translatePlural
Vue.prototype.getCurrentUser = getCurrentUser

document.addEventListener('DOMContentLoaded', () => {
OCA.Dashboard.register('polls', (el) => {
const View = Vue.extend(Dashboard)
new View({
propsData: {},
store,
}).$mount(el)
})
})
160 changes: 160 additions & 0 deletions src/js/views/Dashboard.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<!--
- @copyright Copyright (c) 2022 Michael Longo <contact@tiller.fr>
-
- @author Michael Longo <contact@tiller.fr>
-
- @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>
<div>
<NcDashboardWidget :items="relevantPolls"
empty-content-icon="icon-polls"
:empty-content-message="t('polls', 'No polls found for this category')"
:show-more-text="t('polls', 'Relevant polls')"
:loading="loading"
@hide="() => {}"
@markDone="() => {}">
<template #default="{ item }">
<a :href="pollLink(item)">
<div class="poll-item__item">
<div class="item__icon-spacer">
<TextPollIcon v-if="item.type === 'textPoll'" />
<DatePollIcon v-else />
</div>

<div class="item__title">
<div class="item__title__title">
{{ item.title }}
</div>

<div class="item__title__description">
{{ item.description ? item.description : t('polls', 'No description provided') }}
</div>
</div>
</div>
</a>
</template>
</NcDashboardWidget>
</div>
</template>

<script>
import { NcDashboardWidget } from '@nextcloud/vue'
import { showError } from '@nextcloud/dialogs'
import TextPollIcon from 'vue-material-design-icons/FormatListBulletedSquare.vue'
import DatePollIcon from 'vue-material-design-icons/CalendarBlank.vue'
import { mapGetters } from 'vuex'
import { generateUrl } from '@nextcloud/router'

export default {
name: 'Dashboard',
components: {
NcDashboardWidget,
TextPollIcon,
DatePollIcon,
},
data() {
return {
loading: false,
}
},
computed: {
...mapGetters({
filteredPolls: 'polls/filtered',
}),
relevantPolls() {
const list = [
...this.filteredPolls('relevant'),
]
return list.slice(0, 6)
},
pollLink() {
return (card) => generateUrl(`/apps/polls/vote/${card.id}`)
},
},
beforeMount() {
this.loading = true
this.$store.dispatch('polls/list').then(() => {
this.loading = false
return null
}).catch(() => {
showError(t('polls', 'Error loading poll list'))
})
},
}
</script>
<style lang="scss">
[class^='poll-item__'] {
display: flex;
flex: 1;
padding: 4px 8px;
border-bottom: 1px solid var(--color-border-dark);
}

[class^='item__'],
.poll-item__item .action-item {
display: flex;
align-items: center;
flex: 0 0 auto;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}

.item__title {
display: flex;
flex-direction: column;
flex: 1 0 155px;
align-items: stretch;
justify-content: center;

.item__title__title {
display: block;
}

.item__title__description {
opacity: 0.5;
display: block;
}
}

.poll-item__item {
border-radius: var(--border-radius-large);

&> .action-item {
display: flex;
}
&.active {
background-color: var(--color-primary-light);
}
&:hover {
background-color: var(--color-background-hover);
}
}

.item__icon-spacer {
width: 44px;
min-width: 44px;
}

[class^='item__type'] {
width: 44px;
min-width: 16px;
min-height: 16px;
}
</style>
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ webpackConfig.entry = {
main: path.join(__dirname, 'src/js/', 'main.js'),
userSettings: path.join(__dirname, 'src/js/', 'userSettings.js'),
adminSettings: path.join(__dirname, 'src/js/', 'adminSettings.js'),
dashboard: path.join(__dirname, 'src/js/', 'init-dashboard.js'),
}

webpackConfig.output = {
Expand Down