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

Add a button to create a new note to the dashboard #873

Merged
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
1 change: 0 additions & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
'name' => 'page#create',
'url' => '/new',
'verb' => 'GET',
'postfix' => 'new',
],
[
'name' => 'page#index',
Expand Down
2 changes: 1 addition & 1 deletion lib/AppInfo/SearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function (Note $note) : SearchResultEntry {
'',
$note->getTitle(),
$excerpt,
$this->url->linkToRouteAbsolute('notes.page.index') . 'note/'.$note->getId(),
$this->url->linkToRouteAbsolute('notes.page.indexnote', [ 'id' => $note->getId() ]),
'icon-notes-trans'
);
},
Expand Down
4 changes: 2 additions & 2 deletions lib/Controller/NotesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ public function index(int $pruneBefore = 0) : JSONResponse {
*/
public function dashboard() : JSONResponse {
return $this->helper->handleErrorResponse(function () {
$maxItems = 7;
$maxItems = 6;
$userId = $this->helper->getUID();
$notes = $this->notesService->getTopNotes($userId, $maxItems + 1);
$notes = $this->notesService->getTopNotes($userId);
$hasMoreNotes = count($notes) > $maxItems;
$notes = array_slice($notes, 0, $maxItems);
$items = array_map(function ($note) {
Expand Down
4 changes: 2 additions & 2 deletions lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function index() : TemplateResponse {
public function create() : RedirectResponse {
$note = $this->notesService->create($this->userSession->getUser()->getUID(), '', '');
$note->setContent('');
$url = $this->urlGenerator->linkToRoute('notes.page.index', [ 'id' => $note->getId() ]);
return new RedirectResponse($url);
$url = $this->urlGenerator->linkToRoute('notes.page.indexnote', [ 'id' => $note->getId() ]);
return new RedirectResponse($url . '?new');
}
}
4 changes: 2 additions & 2 deletions lib/Service/NotesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function getAll(string $userId) : array {
return [ 'notes' => $notes, 'categories' => $data['categories'] ];
}

public function getTopNotes(string $userId, int $count) : array {
public function getTopNotes(string $userId) : array {
$notes = $this->getAll($userId)['notes'];
usort($notes, function (Note $a, Note $b) {
$favA = $a->getFavorite();
Expand All @@ -48,7 +48,7 @@ public function getTopNotes(string $userId, int $count) : array {
return $favA > $favB ? -1 : 1;
}
});
return array_slice($notes, 0, $count);
return $notes;
}

public function get(string $userId, int $id) : Note {
Expand Down
11 changes: 6 additions & 5 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,12 @@ export default {
}
},

routeToNote(noteId, query) {
if (this.$route.name !== 'note' || this.$route.params.noteId !== noteId.toString()) {
routeToNote(id, query) {
const noteId = id.toString()
if (this.$route.name !== 'note' || this.$route.params.noteId !== noteId) {
this.$router.push({
name: 'note',
params: { noteId: noteId.toString() },
params: { noteId },
query,
})
}
Expand All @@ -228,13 +229,13 @@ export default {
return
}
this.loading.create = true
createNote(this.filter.category || '')
createNote(this.filter.category)
.then(note => {
this.routeToNote(note.id, { new: null })
})
.catch(() => {
})
.then(() => {
.finally(() => {
this.loading.create = false
})
},
Expand Down
2 changes: 1 addition & 1 deletion src/NotesService.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export const setTitle = (noteId, title) => {

export const createNote = category => {
return axios
.post(url('/notes'), { category })
.post(url('/notes'), { category: category || '' })
.then(response => {
_updateLocalNote(response.data)
return response.data
Expand Down
83 changes: 49 additions & 34 deletions src/components/Dashboard.vue
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
<template>
<DashboardWidget :items="items"
:loading="loading"
>
<template #default="{ item }">
<DashboardWidgetItem
:target-url="getItemTargetUrl(item)"
:main-text="item.title"
:sub-text="subtext(item)"
>
<template #avatar>
<div
class="note-item"
:class="{ 'note-item-favorite': item.favorite, 'note-item-no-favorites': !hasFavorites }"
/>
</template>
</DashboardWidgetItem>
</template>
<template #empty-content>
<EmptyContent icon="icon-notes">
<template #desc>
<p class="notes-empty-content-label">
{{ t('notes', 'No notes yet') }}
</p>
<p>
<a :href="createNoteUrl" class="button">{{ t('notes', 'New note') }}</a>
</p>
</template>
</EmptyContent>
</template>
</DashboardWidget>
<div class="dashboard-box">
<DashboardWidget :items="items" :loading="loading">
<template #default="{ item }">
<DashboardWidgetItem
:target-url="getItemTargetUrl(item)"
:main-text="item.title"
:sub-text="subtext(item)"
>
<template #avatar>
<div
class="note-item"
:class="{ 'note-item-favorite': item.favorite, 'note-item-no-favorites': !hasFavorites }"
/>
</template>
</DashboardWidgetItem>
</template>
<template #empty-content>
<EmptyContent icon="icon-notes">
<template #desc>
<p class="notes-empty-content-label">
{{ t('notes', 'No notes yet') }}
</p>
</template>
</EmptyContent>
</template>
</DashboardWidget>
<div v-if="!loading" class="buttons-footer">
<a :href="createNoteUrl" class="button">
{{ t('notes', 'New note') }}
</a>
</div>
</div>
</template>

<script>
import { DashboardWidget, DashboardWidgetItem } from '@nextcloud/vue-dashboard'
import { DashboardWidget, DashboardWidgetItem } from '@nextcloud/vue-dashboard' // TODO: should be refactored with next release of @nextcloud/vue : https://github.com/nextcloud/nextcloud-vue-dashboard/issues/407
import { EmptyContent } from '@nextcloud/vue'
import { generateUrl } from '@nextcloud/router'

Expand All @@ -51,6 +53,7 @@ export default {
data() {
return {
loading: true,
creating: false,
items: [],
hasMoreItems: false,
}
Expand All @@ -71,7 +74,7 @@ export default {

getItemTargetUrl() {
return (note) => {
return generateUrl('/apps/notes/note/' + note.id)
return generateUrl(`/apps/notes/note/${note.id}`)
}
},
},
Expand All @@ -90,13 +93,18 @@ export default {
},

subtext(item) {
return item.excerpt ? item.excerpt : categoryLabel(item.category)
return item.excerpt || categoryLabel(item.category)
},
},

}
</script>

<style scoped>
.dashboard-box {
position: relative;
height: 100%;
}

.note-item-favorite {
background: var(--icon-star-dark-FC0, var(--icon-star-dark-fc0));
}
Expand All @@ -118,4 +126,11 @@ export default {
.notes-empty-content-label {
margin-bottom: 20px;
}

salixor marked this conversation as resolved.
Show resolved Hide resolved
.buttons-footer {
position: absolute;
bottom: 1em;
width: 100%;
text-align: center;
salixor marked this conversation as resolved.
Show resolved Hide resolved
}
</style>