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) { function tabChange(e) {
var target = e.target.closest('li') var target = e.target.closest('li')
var identifier = target.getAttribute('data-id'); 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++) { for (i = 0; i < tabs.length; i++) {
if (tabs[i].getAttribute('data-id') == identifier) { var tab = tabs[i].parentElement;
tabs[i].className += ' is-active'; if (tab.getAttribute('data-id') == identifier) {
tab.className += ' is-active';
} else { } 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="columns">
<div class="column is-one-third"> <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 %} {% if not suggested_books %}
<p>There are no books here right now! Try searching for a book to get started</p> <p>There are no books here right now! Try searching for a book to get started</p>
{% else %} {% 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"> <div class="tabs is-small is-toggle">
<ul> <ul>
{% for book in suggested_books %} {% for book in shelf.books %}
<li class="{% if forloop.first %}is-active{% endif %}" data-id="tab-book-{{ book.id }}"> <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> <label for="book-{{ book.id }}" onclick="tabChange(event)"><a>{% include 'snippets/book_cover.html' with book=book size="medium" %}</a></label>
</li> </li>
{% endfor %} {% endfor %}
</ul> </ul>
</div> </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> <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="toggle-content hidden">
<div class="block"> <div class="block">
{% include 'snippets/book_titleby.html' with book=book %} {% include 'snippets/book_titleby.html' with book=book %}
@ -29,6 +44,8 @@
</div> </div>
</div> </div>
{% endfor %} {% endfor %}
{% endwith %}
{% endfor %}
{% endif %} {% endif %}
</div> </div>

View file

@ -68,7 +68,7 @@
<div class="field"> <div class="field">
<label class="label" for="start_date"> <label class="label" for="start_date">
Started reading 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> </label>
</div> </div>
</section> </section>
@ -111,13 +111,13 @@
<div class="field"> <div class="field">
<label class="label" for="start_date"> <label class="label" for="start_date">
Started reading 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> </label>
</div> </div>
<div class="field"> <div class="field">
<label class="label" for="finish_date"> <label class="label" for="finish_date">
Finished reading 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> </label>
</div> </div>
</section> </section>

View file

@ -60,34 +60,19 @@ def home_tab(request, tab):
except ValueError: except ValueError:
page = 1 page = 1
count = 5 max_books = 5
querysets = [ book_count = 0
# recemt currently reading preset_shelves = ['reading', 'read', 'to-read']
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')
]
suggested_books = [] suggested_books = []
for queryset in querysets: for preset in preset_shelves:
length = count - len(suggested_books) limit = max_books - book_count
suggested_books += list(queryset[:length]) shelf = request.user.shelf_set.get(identifier=preset)
if len(suggested_books) >= count: shelf_preview = {
break '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) activities = get_activity_feed(request.user, tab)
@ -99,7 +84,7 @@ def home_tab(request, tab):
data = { data = {
'title': 'Updates Feed', 'title': 'Updates Feed',
'user': request.user, 'user': request.user,
'suggested_books': set(suggested_books), 'suggested_books': suggested_books,
'activities': activities, 'activities': activities,
'review_form': forms.ReviewForm(), 'review_form': forms.ReviewForm(),
'quotation_form': forms.QuotationForm(), 'quotation_form': forms.QuotationForm(),