1
0
mirror of https://github.com/aquatix/digimarks.git synced 2025-12-06 22:05:09 +01:00

Show and filter/search bookmarks

This commit is contained in:
2025-05-05 20:08:34 +02:00
parent 324c77f985
commit b364f865b1
4 changed files with 100 additions and 13 deletions

View File

@@ -362,10 +362,15 @@ def list_bookmarks(
session: SessionDep,
user_key: str,
offset: int = 0,
limit: Annotated[int, Query(le=100)] = 100,
limit: Annotated[int, Query(le=10000)] = 100,
) -> list[Bookmark]:
"""List all bookmarks in the database."""
bookmarks = session.exec(select(Bookmark).where(Bookmark.userkey == user_key).offset(offset).limit(limit)).all()
"""List all bookmarks in the database. By default gives back 100 items."""
bookmarks = session.exec(
select(Bookmark)
.where(Bookmark.userkey == user_key, Bookmark.status != Visibility.DELETED)
.offset(offset)
.limit(limit)
).all()
return bookmarks

View File

@@ -0,0 +1,63 @@
document.addEventListener('alpine:init', () => {
Alpine.store('digimarks', {
/** Main digimarks application, state etc */
// userKey: Alpine.$persist(0).as('userKey'),
userKey: -1,
/* cache consists of cache[userKey] = {'bookmarks': [], 'tags': [], ??} */
cache: Alpine.$persist({}).as('cache'),
bookmarks: [],
tags: [],
tryout: 'hey',
loading: false,
search: '',
async init() {
/** Initialise the application after loading */
// if (this.userKey in this.cache) {
// console.log('loading bookmarks from cache');
// this.bookmarks = this.cache[this.userKey]['bookmarks'] || [];
// }
/* await this.getBookmarks(); */
setInterval(() => {
// Update counter to next game (midnight UTC, fetched from API) every second
// this.countDownTimer();
}, 1000);
},
async loadCache() {
console.log('Loading bookmarks from cache for user "' + this.userKey + '"');
this.bookmarks = this.cache[this.userKey]['bookmarks'] || [];
},
async getBookmarks() {
/** Get the bookmarks from the backend */
this.loading = true;
console.log('Fetching latest bookmarks from backend for user "' + this.userKey + '"...');
let response = await fetch('/api/v1/' + this.userKey + '/bookmarks/?limit=10000');
let result = await response.json();
console.log(result);
this.bookmarks = result;
if (!(this.userKey in this.cache)) {
/* There is no cache for this userKey yet, create on */
console.log('caching');
this.cache[this.userKey] = {'bookmarks': []};
}
/* Cache the bookmarks to Local Storage */
this.cache[this.userKey]['bookmarks'] = result;
this.loading = false;
},
get filteredItems() {
// return this.cache[this.userKey]['bookmarks'].filter(
// i => i.title.includes(this.search)
// )
/* Use 'bookmarks' as it can already be pre-filtered */
return this.bookmarks.filter(
i => i.title.match(new RegExp(this.search, "i"))
)
}
})
});

View File

@@ -18,18 +18,17 @@
</head>
<body>
<div id="container" x-data="">
{% block pagecontent %}
<div id="container" x-data="">
{% block pagecontent %}
{% endblock %}
</div>
{% endblock %}
<!-- Scripts -->
<script src="/static/js/digimarks.js?v={{ version }}"></script>
<!-- Scripts -->
<script src="/static/js/digimarks.js?v={{ version }}"></script>
{% block extrajs %}{% endblock %}
</div>
{% block extrajs %}{% endblock %}
</body>
</html>

View File

@@ -3,8 +3,28 @@
{% block pageheader %}Bookmarks{% endblock %}
{% block pagecontent %}
Welcome user!
<div id="container"
x-init="$store.digimarks.userKey = '{{ user_key }}'; $store.digimarks.loadCache(); $store.digimarks.getBookmarks()"
x-data="">
<div x-init="userKey = '{{ user_key }}'"></div>
<nav class="menu">
<ul>
<li>digimarks</li>
<li>tags</li>
<li>add bookmark</li>
</ul>
</nav>
Welcome user <span x-text="$store.digimarks.userKey"></span>! Tryout: <b x-text="$store.digimarks.tryout"></b>
<input x-model="$store.digimarks.search" placeholder="Search...">
<div x-show="$store.digimarks.loading">
Loading...
</div>
<ul x-cloak>
<template x-for="bookmark in $store.digimarks.filteredItems" :key="bookmark.id">
<li><a x-text="bookmark.title" x-bind:href="bookmark.url" target="_blank"></a></li>
</template>
</ul>
</div>
{% endblock %}