Show shelf names with suggested books

This commit is contained in:
Mouse Reeve 2020-11-06 20:36:52 -08:00
parent d31071ddb0
commit b763b7469b
4 changed files with 47 additions and 43 deletions

View file

@ -34,13 +34,15 @@ function rate_stars(e) {
function tabChange(e) {
var target = e.target.closest('li')
var identifier = target.getAttribute('data-id');
var parent_element = target.parentElement.closest('li').parentElement;
var tabs = target.parentElement.children;
var tabs = parent_element.getElementsByTagName('label');
for (i = 0; i < tabs.length; i++) {
if (tabs[i].getAttribute('data-id') == identifier) {
tabs[i].className += ' is-active';
var tab = tabs[i].parentElement;
if (tab.getAttribute('data-id') == identifier) {
tab.className += ' is-active';
} else {
tabs[i].className = tabs[i].className.replace('is-active', '');
tab.className = tab.className.replace('is-active', '');
}
}

View file

@ -4,22 +4,37 @@
<div class="columns">
<div class="column is-one-third">
<h2 class="title is-4">Suggested books</h2>
<h2 class="title is-5">Your books</h2>
{% if not suggested_books %}
<p>There are no books here right now! Try searching for a book to get started</p>
{% else %}
<div class="tabs is-small">
<ul>
{% for shelf in suggested_books %}
{% if shelf.books.count %}
{% with shelf_counter=forloop.counter %}
<li>
<p>{{ shelf.name }}</p>
<div class="tabs is-small is-toggle">
<ul>
{% for book in suggested_books %}
<li class="{% if forloop.first %}is-active{% endif %}" data-id="tab-book-{{ book.id }}">
{% for book in shelf.books %}
<li class="{% if shelf_counter == 1 and forloop.first %}is-active{% endif %}" data-id="tab-book-{{ book.id }}">
<label for="book-{{ book.id }}" onclick="tabChange(event)"><a>{% include 'snippets/book_cover.html' with book=book size="medium" %}</a></label>
</li>
{% endfor %}
</ul>
</div>
{% for book in suggested_books %}
</li>
{% endwith %}
{% endif %}
{% endfor %}
</ul>
</div>
{% for shelf in suggested_books %}
{% with shelf_counter=forloop.counter %}
{% for book in shelf.books %}
<div>
<input class="toggle-control" type="radio" name="recent-books" id="book-{{ book.id }}" {% if forloop.first %}checked{% endif %}>
<input class="toggle-control" type="radio" name="recent-books" id="book-{{ book.id }}" {% if shelf_counter == 1 and forloop.first %}checked{% endif %}>
<div class="toggle-content hidden">
<div class="block">
{% include 'snippets/book_titleby.html' with book=book %}
@ -29,6 +44,8 @@
</div>
</div>
{% endfor %}
{% endwith %}
{% endfor %}
{% endif %}
</div>

View file

@ -68,7 +68,7 @@
<div class="field">
<label class="label" for="start_date">
Started reading
<input type="date" name="start_date" class="input" id="id_start_date-{{ uuid }}" value="{% now "Y-m-d" %}">
<input type="date" name="start_date" class="input" id="start_id_start_date-{{ uuid }}" value="{% now "Y-m-d" %}">
</label>
</div>
</section>
@ -111,13 +111,13 @@
<div class="field">
<label class="label" for="start_date">
Started reading
<input type="date" name="start_date" class="input" id="id_start_date-{{ readthrough.id }}" value="{{ readthrough.start_date | date:"Y-m-d" }}">
<input type="date" name="start_date" class="input" id="finish_id_start_date-{{ uuid }}" value="{{ readthrough.start_date | date:"Y-m-d" }}">
</label>
</div>
<div class="field">
<label class="label" for="finish_date">
Finished reading
<input type="date" name="finish_date" class="input" id="id_finish_date-{{ readthrough.id }}" value="{% now "Y-m-d" %}">
<input type="date" name="finish_date" class="input" id="id_finish_date-{{ uuid }}" value="{% now "Y-m-d" %}">
</label>
</div>
</section>

View file

@ -60,34 +60,19 @@ def home_tab(request, tab):
except ValueError:
page = 1
count = 5
querysets = [
# recemt currently reading
models.Edition.objects.filter(
shelves__user=request.user,
shelves__identifier='reading'
),
# read
models.Edition.objects.filter(
shelves__user=request.user,
shelves__identifier='read'
).order_by('-updated_date')[:2],
# to-read
models.Edition.objects.filter(
shelves__user=request.user,
shelves__identifier='to-read'
),
# popular books
models.Edition.objects.annotate(
shelf_count=Count('shelves')
).order_by('-shelf_count')
]
max_books = 5
book_count = 0
preset_shelves = ['reading', 'read', 'to-read']
suggested_books = []
for queryset in querysets:
length = count - len(suggested_books)
suggested_books += list(queryset[:length])
if len(suggested_books) >= count:
break
for preset in preset_shelves:
limit = max_books - book_count
shelf = request.user.shelf_set.get(identifier=preset)
shelf_preview = {
'name': shelf.name,
'books': shelf.books.all()[:limit]
}
suggested_books.append(shelf_preview)
book_count += len(shelf_preview['books'])
activities = get_activity_feed(request.user, tab)
@ -99,7 +84,7 @@ def home_tab(request, tab):
data = {
'title': 'Updates Feed',
'user': request.user,
'suggested_books': set(suggested_books),
'suggested_books': suggested_books,
'activities': activities,
'review_form': forms.ReviewForm(),
'quotation_form': forms.QuotationForm(),