Skip to content

Commit

Permalink
Polish dashboard code
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Wurst <[email protected]>
  • Loading branch information
ChristophWurst committed Aug 10, 2020
1 parent 9649e46 commit 51df8a3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
28 changes: 23 additions & 5 deletions lib/Dashboard/MailWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -51,7 +67,7 @@ public function __construct(IL10N $l10n, IURLGenerator $urlGenerator, AccountSer
* @inheritDoc
*/
public function getId(): string {
return 'mail';
return Application::APP_ID;
}

/**
Expand Down Expand Up @@ -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)
);
}
}
8 changes: 4 additions & 4 deletions src/main-dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
10 changes: 8 additions & 2 deletions src/views/Dashboard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -63,7 +66,7 @@ export default {
},
data() {
return {
messages: null,
messages: [],
accounts,
fetchedAccounts: 0,
emptyImage: imagePath('mail', 'newsletter.svg'),
Expand All @@ -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) => {
Expand Down

0 comments on commit 51df8a3

Please sign in to comment.