From 51df8a3e69f590b40a57a5f927b0bade5b46930b Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Mon, 10 Aug 2020 17:07:19 +0200 Subject: [PATCH] Polish dashboard code Signed-off-by: Christoph Wurst --- lib/Dashboard/MailWidget.php | 28 +++++++++++++++++++++++----- src/main-dashboard.js | 8 ++++---- src/views/Dashboard.vue | 10 ++++++++-- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/lib/Dashboard/MailWidget.php b/lib/Dashboard/MailWidget.php index e25c61302f..1c2fc833af 100644 --- a/lib/Dashboard/MailWidget.php +++ b/lib/Dashboard/MailWidget.php @@ -26,20 +26,36 @@ namespace OCA\Mail\Dashboard; +use OCA\Mail\AppInfo\Application; use OCA\Mail\Service\AccountService; use OCP\AppFramework\Services\IInitialState; use OCP\Dashboard\IWidget; use OCP\IL10N; use OCP\IURLGenerator; +use OCP\Util; class MailWidget implements IWidget { /** @var IL10N */ private $l10n; + /** @var IURLGenerator */ private $urlGenerator; - public function __construct(IL10N $l10n, IURLGenerator $urlGenerator, AccountService $accountService, IInitialState $initialState, $userId) { + /** @var AccountService */ + private $accountService; + + /** @var IInitialState */ + private $initialState; + + /** @var string|null */ + private $userId; + + public function __construct(IL10N $l10n, + IURLGenerator $urlGenerator, + AccountService $accountService, + IInitialState $initialState, + ?string $userId) { $this->l10n = $l10n; $this->urlGenerator = $urlGenerator; $this->accountService = $accountService; @@ -51,7 +67,7 @@ public function __construct(IL10N $l10n, IURLGenerator $urlGenerator, AccountSer * @inheritDoc */ public function getId(): string { - return 'mail'; + return Application::APP_ID; } /** @@ -86,9 +102,11 @@ public function getUrl(): ?string { * @inheritDoc */ public function load(): void { - \OCP\Util::addScript('mail', 'dashboard'); + Util::addScript(Application::APP_ID, 'dashboard'); - $accounts = $this->accountService->findByUserId($this->userId); - $this->initialState->provideInitialState('mail-accounts', $accounts); + $this->initialState->provideInitialState( + 'mail-accounts', + $this->accountService->findByUserId($this->userId) + ); } } diff --git a/src/main-dashboard.js b/src/main-dashboard.js index 3b8265ca3f..9eb22157ba 100644 --- a/src/main-dashboard.js +++ b/src/main-dashboard.js @@ -35,10 +35,10 @@ __webpack_public_path__ = generateFilePath('mail', '', 'js/') Vue.mixin(Nextcloud) document.addEventListener('DOMContentLoaded', function() { - OCA.Dashboard.register('mail', (el) => { + const register = OCA?.Dashboard?.register || (() => {}) + + register('mail', (el) => { const View = Vue.extend(Dashboard) - new View({ - propsData: {}, - }).$mount(el) + new View().$mount(el) }) }) diff --git a/src/views/Dashboard.vue b/src/views/Dashboard.vue index 0e86f0ed79..90f714f75e 100644 --- a/src/views/Dashboard.vue +++ b/src/views/Dashboard.vue @@ -51,8 +51,11 @@ import { loadState } from '@nextcloud/initial-state' import { generateUrl, imagePath } from '@nextcloud/router' import Avatar from '../components/Avatar' import { DashboardWidget, DashboardWidgetItem } from '@nextcloud/vue-dashboard' +import orderBy from 'lodash/fp/orderBy' +import prop from 'lodash/fp/prop' const accounts = loadState('mail', 'mail-accounts') +const orderByDateInt = orderBy(prop('dateInt'), 'desc') export default { name: 'Dashboard', @@ -63,7 +66,7 @@ export default { }, data() { return { - messages: null, + messages: [], accounts, fetchedAccounts: 0, emptyImage: imagePath('mail', 'newsletter.svg'), @@ -75,7 +78,10 @@ export default { return this.fetchedAccounts < this.accounts.length }, importantMessages() { - return this.messages ? this.messages.sort((a, b) => b.dateInt - a.dateInt).slice(0, 7) : [] + if (!this.messages) { + return [] + } + return orderByDateInt(this.messages).slice(0, 7) }, getWidgetItem() { return (item) => {