WIP: Multi select
This commit is contained in:
+4
-9
@@ -2,22 +2,17 @@ package com.adityachandel.booklore.controller;
|
|||||||
|
|
||||||
import com.adityachandel.booklore.model.dto.BookDTO;
|
import com.adityachandel.booklore.model.dto.BookDTO;
|
||||||
import com.adityachandel.booklore.model.dto.BookViewerSettingDTO;
|
import com.adityachandel.booklore.model.dto.BookViewerSettingDTO;
|
||||||
import com.adityachandel.booklore.model.dto.BookWithNeighborsDTO;
|
import com.adityachandel.booklore.model.dto.request.ShelvesAssignmentRequest;
|
||||||
import com.adityachandel.booklore.model.dto.request.AssignShelvesRequest;
|
|
||||||
import com.adityachandel.booklore.model.dto.response.GoogleBooksMetadata;
|
|
||||||
import com.adityachandel.booklore.model.dto.request.SetMetadataRequest;
|
import com.adityachandel.booklore.model.dto.request.SetMetadataRequest;
|
||||||
|
import com.adityachandel.booklore.model.dto.response.GoogleBooksMetadata;
|
||||||
import com.adityachandel.booklore.service.BooksService;
|
import com.adityachandel.booklore.service.BooksService;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
import jakarta.validation.constraints.Max;
|
|
||||||
import jakarta.validation.constraints.Min;
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.data.domain.Page;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.time.Instant;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@RequestMapping("/v1/book")
|
@RequestMapping("/v1/book")
|
||||||
@@ -86,7 +81,7 @@ public class BookController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/assign-shelves")
|
@PostMapping("/assign-shelves")
|
||||||
public ResponseEntity<BookDTO> addBookToShelf(@RequestBody @Valid AssignShelvesRequest request) {
|
public ResponseEntity<List<BookDTO>> addBookToShelf(@RequestBody @Valid ShelvesAssignmentRequest request) {
|
||||||
return ResponseEntity.ok(booksService.addBookToShelf(request.getBookId(), request.getShelfIds()));
|
return ResponseEntity.ok(booksService.assignShelvesToBooks(request.getBookIds(), request.getShelvesToAssign(), request.getShelvesToUnassign()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,6 @@ package com.adityachandel.booklore.controller;
|
|||||||
|
|
||||||
import com.adityachandel.booklore.model.dto.BookDTO;
|
import com.adityachandel.booklore.model.dto.BookDTO;
|
||||||
import com.adityachandel.booklore.model.dto.ShelfDTO;
|
import com.adityachandel.booklore.model.dto.ShelfDTO;
|
||||||
import com.adityachandel.booklore.model.dto.request.AssignShelvesRequest;
|
|
||||||
import com.adityachandel.booklore.model.dto.request.ShelfCreateRequest;
|
import com.adityachandel.booklore.model.dto.request.ShelfCreateRequest;
|
||||||
import com.adityachandel.booklore.service.ShelfService;
|
import com.adityachandel.booklore.service.ShelfService;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
|
|||||||
-11
@@ -1,11 +0,0 @@
|
|||||||
package com.adityachandel.booklore.model.dto.request;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
public class AssignShelvesRequest {
|
|
||||||
private Long bookId;
|
|
||||||
private List<Long> shelfIds;
|
|
||||||
}
|
|
||||||
+12
@@ -0,0 +1,12 @@
|
|||||||
|
package com.adityachandel.booklore.model.dto.request;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class ShelvesAssignmentRequest {
|
||||||
|
private Set<Long> bookIds;
|
||||||
|
private Set<Long> shelvesToAssign;
|
||||||
|
private Set<Long> shelvesToUnassign;
|
||||||
|
}
|
||||||
@@ -15,10 +15,6 @@ import lombok.AllArgsConstructor;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.core.io.UrlResource;
|
import org.springframework.core.io.UrlResource;
|
||||||
import org.springframework.data.domain.Page;
|
|
||||||
import org.springframework.data.domain.PageImpl;
|
|
||||||
import org.springframework.data.domain.PageRequest;
|
|
||||||
import org.springframework.data.domain.Sort;
|
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@@ -33,8 +29,7 @@ import java.time.Instant;
|
|||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.time.format.DateTimeParseException;
|
import java.time.format.DateTimeParseException;
|
||||||
import java.util.Arrays;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@@ -220,25 +215,32 @@ public class BooksService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public BookDTO addBookToShelf(Long bookId, List<Long> shelfIds) {
|
public List<BookDTO> assignShelvesToBooks(Set<Long> bookIds, Set<Long> shelfIdsToAssign, Set<Long> shelfIdsToUnassign) {
|
||||||
Book book = bookRepository.findById(bookId)
|
List<Book> books = bookRepository.findAllById(bookIds);
|
||||||
.orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
List<Shelf> shelvesToAssign = shelfRepository.findAllById(shelfIdsToAssign);
|
||||||
|
List<Shelf> shelvesToUnassign = shelfRepository.findAllById(shelfIdsToUnassign);
|
||||||
List<Shelf> currentShelves = book.getShelves();
|
|
||||||
List<Shelf> shelvesToAdd = shelfRepository.findAllById(shelfIds);
|
|
||||||
|
|
||||||
currentShelves.stream()
|
|
||||||
.filter(shelf -> !shelfIds.contains(shelf.getId()))
|
|
||||||
.forEach(shelf -> shelf.getBooks().remove(book));
|
|
||||||
|
|
||||||
book.getShelves().clear();
|
|
||||||
book.getShelves().addAll(shelvesToAdd);
|
|
||||||
|
|
||||||
shelvesToAdd.forEach(shelf -> shelf.getBooks().add(book));
|
|
||||||
|
|
||||||
|
for (Book book : books) {
|
||||||
|
for (Shelf shelf : shelvesToUnassign) {
|
||||||
|
Optional<Shelf> shelfOpt = book.getShelves().stream().filter(s -> s.getId().equals(shelf.getId())).findAny();
|
||||||
|
shelfOpt.ifPresent(value -> book.getShelves().remove(value));
|
||||||
|
shelf.getBooks().remove(book);
|
||||||
|
shelfRepository.save(shelf);
|
||||||
|
}
|
||||||
|
for (Shelf shelf : shelvesToAssign) {
|
||||||
|
Optional<Shelf> shelfOpt = book.getShelves().stream().filter(s -> s.getId().equals(shelf.getId())).findAny();
|
||||||
|
if (shelfOpt.isEmpty()) {
|
||||||
|
book.getShelves().add(shelf);
|
||||||
|
}
|
||||||
|
Optional<Book> bookOpt = shelf.getBooks().stream().filter(b -> b.getId().equals(book.getId())).findAny();
|
||||||
|
if (bookOpt.isEmpty()) {
|
||||||
|
shelf.getBooks().add(book);
|
||||||
|
}
|
||||||
|
shelfRepository.save(shelf);
|
||||||
|
}
|
||||||
bookRepository.save(book);
|
bookRepository.save(book);
|
||||||
shelfRepository.saveAll(shelvesToAdd);
|
}
|
||||||
return BookTransformer.convertToBookDTO(book);
|
return books.stream().map(BookTransformer::convertToBookDTO).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ import {BookCardComponent} from './book/component/book-card/book-card.component'
|
|||||||
import {SpeedDialModule} from 'primeng/speeddial';
|
import {SpeedDialModule} from 'primeng/speeddial';
|
||||||
import {RouteReuseStrategy} from '@angular/router';
|
import {RouteReuseStrategy} from '@angular/router';
|
||||||
import {CustomReuseStrategy} from './custom-reuse-strategy';
|
import {CustomReuseStrategy} from './custom-reuse-strategy';
|
||||||
|
import {MenuModule} from 'primeng/menu';
|
||||||
|
|
||||||
@NgModule({
|
@NgModule({
|
||||||
declarations: [
|
declarations: [
|
||||||
@@ -69,6 +70,7 @@ import {CustomReuseStrategy} from './custom-reuse-strategy';
|
|||||||
DialogModule,
|
DialogModule,
|
||||||
BookCardComponent,
|
BookCardComponent,
|
||||||
SpeedDialModule,
|
SpeedDialModule,
|
||||||
|
MenuModule,
|
||||||
],
|
],
|
||||||
providers: [
|
providers: [
|
||||||
DialogService,
|
DialogService,
|
||||||
|
|||||||
@@ -1,14 +1,32 @@
|
|||||||
<div class="book-card">
|
<div class="book-card"
|
||||||
|
[class.selected]="book.selected"
|
||||||
|
(mouseover)="isHovered = true"
|
||||||
|
(mouseout)="isHovered = false">
|
||||||
<div class="cover-container">
|
<div class="cover-container">
|
||||||
<img [src]="coverImageSrc(book)" class="book-cover placeholder" alt="Cover of {{ book.metadata?.title }}" loading="lazy" />
|
<img
|
||||||
|
[src]="coverImageSrc(book)"
|
||||||
|
class="book-cover placeholder"
|
||||||
|
alt="Cover of {{ book.metadata?.title }}"
|
||||||
|
loading="lazy"/>
|
||||||
|
<p-checkbox
|
||||||
|
*ngIf="isHovered || book.selected"
|
||||||
|
[(ngModel)]="book.selected"
|
||||||
|
binary="true"
|
||||||
|
class="select-checkbox"
|
||||||
|
(ngModelChange)="toggleSelection(book.selected || false)"></p-checkbox>
|
||||||
<p-button [rounded]="true" icon="pi pi-info" class="read-btn" (click)="openBookInfo(book)"></p-button>
|
<p-button [rounded]="true" icon="pi pi-info" class="read-btn" (click)="openBookInfo(book)"></p-button>
|
||||||
<p-button [rounded]="true" icon="pi pi-book" class="info-btn" (click)="readBook(book)"></p-button>
|
<p-button [rounded]="true" icon="pi pi-book" class="info-btn" (click)="readBook(book)"></p-button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="book-info">
|
<div class="book-info">
|
||||||
<div class="book-title-container">
|
<div class="book-title-container">
|
||||||
<h4 class="book-title">{{ book.metadata?.title }}</h4>
|
<h4 class="book-title">{{ book.metadata?.title }}</h4>
|
||||||
<p-menu #menu [model]="items" [popup]="true" appendTo="body" />
|
<p-menu #menu [model]="items" [popup]="true" appendTo="body"></p-menu>
|
||||||
<p-button [text]="true" (click)="menu.toggle($event)" icon="pi pi-ellipsis-v" />
|
<p-button
|
||||||
|
[text]="true"
|
||||||
|
(click)="menu.toggle($event)"
|
||||||
|
icon="pi pi-ellipsis-v">
|
||||||
|
</p-button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,8 +1,13 @@
|
|||||||
.book-card {
|
.book-card {
|
||||||
background-color: var(--surface-card);
|
background-color: var(--surface-card);
|
||||||
border-radius: 8px 8px 0 0;
|
border-radius: 8px;
|
||||||
position: relative;
|
position: relative;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
|
transition: border 0.2s;
|
||||||
|
|
||||||
|
&.selected {
|
||||||
|
border: 2px solid var(--primary-color);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.cover-container {
|
.cover-container {
|
||||||
@@ -13,7 +18,6 @@
|
|||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: space-between;
|
justify-content: space-between;
|
||||||
flex-direction: row;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.book-title {
|
.book-title {
|
||||||
@@ -24,19 +28,18 @@
|
|||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
text-overflow: ellipsis;
|
text-overflow: ellipsis;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
padding: 6px 6px 3px;
|
padding: 6px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.book-cover {
|
.book-cover {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: auto;
|
height: auto;
|
||||||
max-height: 100%;
|
|
||||||
object-fit: cover;
|
|
||||||
border-radius: 8px 8px 0 0;
|
border-radius: 8px 8px 0 0;
|
||||||
transition: filter 0.2s ease-in-out;
|
transition: filter 0.2s ease-in-out;
|
||||||
}
|
}
|
||||||
|
|
||||||
.info-btn, .read-btn {
|
.info-btn,
|
||||||
|
.read-btn {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
left: 50%;
|
left: 50%;
|
||||||
transform: translateX(-50%);
|
transform: translateX(-50%);
|
||||||
@@ -45,15 +48,11 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.info-btn {
|
.info-btn {
|
||||||
top: 25%; /* 30% below the top of the image */
|
top: 25%;
|
||||||
}
|
}
|
||||||
|
|
||||||
.read-btn {
|
.read-btn {
|
||||||
bottom: 25%; /* 30% above the bottom of the image */
|
bottom: 25%;
|
||||||
}
|
|
||||||
|
|
||||||
.cover-container {
|
|
||||||
position: relative; /* Ensures buttons are positioned relative to the image */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.cover-container:hover .book-cover {
|
.cover-container:hover .book-cover {
|
||||||
@@ -65,3 +64,20 @@
|
|||||||
display: block;
|
display: block;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.select-checkbox {
|
||||||
|
position: absolute;
|
||||||
|
top: 8px;
|
||||||
|
right: 8px;
|
||||||
|
display: none; /* Hidden by default */
|
||||||
|
z-index: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Show checkbox on hover */
|
||||||
|
.cover-container:hover .select-checkbox {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Keep checkbox visible for selected books */
|
||||||
|
.book-card.selected .select-checkbox {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
import {Component, Input, OnInit} from '@angular/core';
|
import { Component, Input, OnInit } from '@angular/core';
|
||||||
import {Book} from '../../model/book.model';
|
import { Book } from '../../model/book.model';
|
||||||
import {Button} from 'primeng/button';
|
import { Button } from 'primeng/button';
|
||||||
import {Router} from '@angular/router';
|
import { Router } from '@angular/router';
|
||||||
import {MenuModule} from 'primeng/menu';
|
import { MenuModule } from 'primeng/menu';
|
||||||
import {MenuItem} from 'primeng/api';
|
import { MenuItem } from 'primeng/api';
|
||||||
import {DialogService} from "primeng/dynamicdialog";
|
import { DialogService } from 'primeng/dynamicdialog';
|
||||||
import {ShelfAssignerComponent} from "../shelf-assigner/shelf-assigner.component";
|
import { ShelfAssignerComponent } from '../shelf-assigner/shelf-assigner.component';
|
||||||
import {BookService} from '../../service/book.service';
|
import { BookService } from '../../service/book.service';
|
||||||
|
import { CheckboxModule } from 'primeng/checkbox';
|
||||||
|
import { FormsModule } from '@angular/forms';
|
||||||
|
import { NgIf } from '@angular/common';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-book-card',
|
selector: 'app-book-card',
|
||||||
@@ -14,30 +17,24 @@ import {BookService} from '../../service/book.service';
|
|||||||
styleUrls: ['./book-card.component.scss'],
|
styleUrls: ['./book-card.component.scss'],
|
||||||
imports: [
|
imports: [
|
||||||
Button,
|
Button,
|
||||||
MenuModule
|
MenuModule,
|
||||||
]
|
CheckboxModule,
|
||||||
|
FormsModule,
|
||||||
|
NgIf,
|
||||||
|
],
|
||||||
})
|
})
|
||||||
export class BookCardComponent implements OnInit {
|
export class BookCardComponent implements OnInit {
|
||||||
@Input() book!: Book;
|
@Input() book!: Book;
|
||||||
|
@Input() onBookSelect: (book: Book, selected: boolean) => void = () => {};
|
||||||
|
|
||||||
items: MenuItem[] | undefined;
|
items: MenuItem[] | undefined;
|
||||||
|
isHovered: boolean = false; // Track hover state
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private bookService: BookService, private router: Router,
|
private bookService: BookService,
|
||||||
private dialogService: DialogService) {
|
private router: Router,
|
||||||
}
|
private dialogService: DialogService
|
||||||
|
) {}
|
||||||
coverImageSrc(book: Book): string {
|
|
||||||
return this.bookService.getBookCoverUrl(book.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
readBook(book: Book) {
|
|
||||||
this.bookService.readBook(book);
|
|
||||||
}
|
|
||||||
|
|
||||||
openBookInfo(book: Book) {
|
|
||||||
this.router.navigate(['/library', book.libraryId, 'book', book.id, 'info']);
|
|
||||||
}
|
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
this.items = [
|
this.items = [
|
||||||
@@ -47,29 +44,46 @@ export class BookCardComponent implements OnInit {
|
|||||||
{
|
{
|
||||||
label: 'Edit shelf',
|
label: 'Edit shelf',
|
||||||
icon: 'pi pi-folder',
|
icon: 'pi pi-folder',
|
||||||
command: () => {
|
command: () => this.openShelfDialog(this.book),
|
||||||
this.openShelfDialog(this.book);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: 'View metadata',
|
label: 'View metadata',
|
||||||
icon: 'pi pi-info-circle',
|
icon: 'pi pi-info-circle',
|
||||||
command: () => {
|
command: () => this.openBookInfo(this.book),
|
||||||
this.openBookInfo(this.book);
|
},
|
||||||
}
|
],
|
||||||
}
|
},
|
||||||
]
|
|
||||||
}
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
private openShelfDialog(book: Book) {
|
coverImageSrc(book: Book): string {
|
||||||
|
return this.bookService.getBookCoverUrl(book.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
readBook(book: Book): void {
|
||||||
|
this.bookService.readBook(book);
|
||||||
|
}
|
||||||
|
|
||||||
|
openBookInfo(book: Book): void {
|
||||||
|
this.router.navigate(['/library', book.libraryId, 'book', book.id, 'info']);
|
||||||
|
}
|
||||||
|
|
||||||
|
toggleSelection(selected: boolean): void {
|
||||||
|
this.book.selected = selected;
|
||||||
|
this.onBookSelect(this.book, selected); // Notify the parent component
|
||||||
|
}
|
||||||
|
|
||||||
|
shouldShowCheckbox(): boolean {
|
||||||
|
return this.book.selected || this.isHovered;
|
||||||
|
}
|
||||||
|
|
||||||
|
private openShelfDialog(book: Book): void {
|
||||||
this.dialogService.open(ShelfAssignerComponent, {
|
this.dialogService.open(ShelfAssignerComponent, {
|
||||||
header: 'Update Shelves: ' + book.metadata?.title,
|
header: `Update Shelves: ${book.metadata?.title}`,
|
||||||
modal: true,
|
modal: true,
|
||||||
width: '30%',
|
width: '30%',
|
||||||
height: '70%',
|
height: '70%',
|
||||||
contentStyle: {overflow: 'auto'},
|
contentStyle: { overflow: 'auto' },
|
||||||
baseZIndex: 10,
|
baseZIndex: 10,
|
||||||
data: {
|
data: {
|
||||||
book: this.book
|
book: this.book
|
||||||
|
|||||||
@@ -10,10 +10,15 @@
|
|||||||
hideIcon="pi pi-times">
|
hideIcon="pi pi-times">
|
||||||
</p-speedDial>
|
</p-speedDial>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div *ngIf="isAnyBookSelected()">
|
||||||
|
<p-button icon="pi pi-ellipsis-v" label="Options" severity="primary" [rounded]="true" [outlined]="true" (click)="menu.toggle($event)"></p-button>
|
||||||
|
<p-menu #menu [model]="items" [popup]="true" appendTo="body"></p-menu>
|
||||||
|
<p-button icon="pi pi-times" label="Deselect All" severity="warning" [rounded]="true" [outlined]="true" (click)="deselectAllBooks()"></p-button>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="library-header-right">
|
<div class="library-header-right">
|
||||||
<p-dropdown
|
<p-dropdown optionLabel="name" placeholder="Select Sorting" />
|
||||||
optionLabel="name"
|
|
||||||
placeholder="Select Sorting"/>
|
|
||||||
<div class="zoom-controls">
|
<div class="zoom-controls">
|
||||||
<p-button icon="pi pi-minus" [rounded]="true" [outlined]="true" severity="info"></p-button>
|
<p-button icon="pi pi-minus" [rounded]="true" [outlined]="true" severity="info"></p-button>
|
||||||
<p-button icon="pi pi-plus" [rounded]="true" [outlined]="true" severity="info"></p-button>
|
<p-button icon="pi pi-plus" [rounded]="true" [outlined]="true" severity="info"></p-button>
|
||||||
@@ -26,7 +31,10 @@
|
|||||||
<virtual-scroller *ngIf="books.length > 0" class="virtual-scroller" #scroll [items]="books">
|
<virtual-scroller *ngIf="books.length > 0" class="virtual-scroller" #scroll [items]="books">
|
||||||
<div class="grid" #container>
|
<div class="grid" #container>
|
||||||
<div class="virtual-scroller-item" *ngFor="let book of scroll.viewPortItems">
|
<div class="virtual-scroller-item" *ngFor="let book of scroll.viewPortItems">
|
||||||
<app-book-card [book]="book"></app-book-card>
|
<app-book-card
|
||||||
|
[book]="book"
|
||||||
|
[onBookSelect]="handleBookSelect.bind(this)">
|
||||||
|
</app-book-card>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</virtual-scroller>
|
</virtual-scroller>
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
gap: 2rem;
|
gap: 2rem;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
.library-name {
|
.library-name {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
color: var(--text-color);
|
color: var(--text-color);
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ import {Book} from '../../model/book.model';
|
|||||||
import {Library} from '../../model/library.model';
|
import {Library} from '../../model/library.model';
|
||||||
import {Shelf} from '../../model/book.model';
|
import {Shelf} from '../../model/book.model';
|
||||||
import {ShelfService} from '../../service/shelf.service';
|
import {ShelfService} from '../../service/shelf.service';
|
||||||
|
import {ShelfAssignerComponent} from '../shelf-assigner/shelf-assigner.component';
|
||||||
|
import {DialogService} from 'primeng/dynamicdialog';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-books-browser',
|
selector: 'app-books-browser',
|
||||||
@@ -17,11 +19,12 @@ import {ShelfService} from '../../service/shelf.service';
|
|||||||
styleUrls: ['./books-browser.component.scss']
|
styleUrls: ['./books-browser.component.scss']
|
||||||
})
|
})
|
||||||
export class BooksBrowserComponent implements OnInit {
|
export class BooksBrowserComponent implements OnInit {
|
||||||
|
|
||||||
books$: Observable<Book[]> | undefined;
|
books$: Observable<Book[]> | undefined;
|
||||||
entity$: Observable<Library | Shelf | null> | undefined;
|
entity$: Observable<Library | Shelf | null> | undefined;
|
||||||
entityType$: Observable<string | null> | undefined;
|
entityType$: Observable<string | null> | undefined;
|
||||||
items: MenuItem[] | undefined;
|
items: MenuItem[] | undefined;
|
||||||
|
selectedBooks: Set<number> = new Set();
|
||||||
|
book: Book | undefined;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private activatedRoute: ActivatedRoute,
|
private activatedRoute: ActivatedRoute,
|
||||||
@@ -30,7 +33,8 @@ export class BooksBrowserComponent implements OnInit {
|
|||||||
private messageService: MessageService,
|
private messageService: MessageService,
|
||||||
private libraryService: LibraryService,
|
private libraryService: LibraryService,
|
||||||
private bookService: BookService,
|
private bookService: BookService,
|
||||||
private shelfService: ShelfService
|
private shelfService: ShelfService,
|
||||||
|
private dialogService: DialogService
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,75 +88,58 @@ export class BooksBrowserComponent implements OnInit {
|
|||||||
return of(null);
|
return of(null);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
//this.initializeMenuItems();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*private initializeMenuItems(): void {
|
handleBookSelect(book: Book, selected: boolean): void {
|
||||||
|
if (selected) {
|
||||||
|
this.selectedBooks.add(book.id);
|
||||||
|
this.book = book; // Set the selected book
|
||||||
|
this.setupMenu();
|
||||||
|
} else {
|
||||||
|
this.selectedBooks.delete(book.id);
|
||||||
|
this.book = undefined; // Clear the selected book
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setupMenu(): void {
|
||||||
this.items = [
|
this.items = [
|
||||||
{
|
{
|
||||||
icon: 'pi pi-trash',
|
label: 'Options',
|
||||||
tooltipOptions: {
|
items: [
|
||||||
tooltipLabel: 'Delete',
|
{
|
||||||
tooltipPosition: 'top'
|
label: 'Edit shelf',
|
||||||
|
icon: 'pi pi-folder',
|
||||||
|
command: () => this.openShelfDialog(),
|
||||||
|
}
|
||||||
|
],
|
||||||
},
|
},
|
||||||
command: () => {
|
|
||||||
this.entity$?.subscribe(entity => {
|
|
||||||
if (entity) {
|
|
||||||
this.confirmationService.confirm({
|
|
||||||
message: `Are you sure you want to delete ${entity.name}?`,
|
|
||||||
header: 'Confirmation',
|
|
||||||
icon: 'pi pi-exclamation-triangle',
|
|
||||||
acceptIcon: 'none',
|
|
||||||
rejectIcon: 'none',
|
|
||||||
rejectButtonStyleClass: 'p-button-text',
|
|
||||||
accept: () => {
|
|
||||||
if ('libraryId' in entity) { // Check if it's a Library
|
|
||||||
this.libraryService.deleteLibrary(entity.id).subscribe({
|
|
||||||
complete: () => {
|
|
||||||
this.router.navigate(['/']);
|
|
||||||
this.messageService.add({
|
|
||||||
severity: 'info',
|
|
||||||
summary: 'Success',
|
|
||||||
detail: 'Library was deleted'
|
|
||||||
});
|
|
||||||
},
|
|
||||||
error: () => {
|
|
||||||
this.messageService.add({
|
|
||||||
severity: 'error',
|
|
||||||
summary: 'Failed',
|
|
||||||
detail: 'Failed to delete library',
|
|
||||||
life: 3000
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else if ('id' in entity) { // Check if it's a Shelf
|
|
||||||
this.shelfService.deleteShelf(entity.id).subscribe({
|
|
||||||
complete: () => {
|
|
||||||
this.router.navigate(['/']);
|
|
||||||
this.messageService.add({
|
|
||||||
severity: 'info',
|
|
||||||
summary: 'Success',
|
|
||||||
detail: 'Shelf was deleted'
|
|
||||||
});
|
|
||||||
},
|
|
||||||
error: () => {
|
|
||||||
this.messageService.add({
|
|
||||||
severity: 'error',
|
|
||||||
summary: 'Failed',
|
|
||||||
detail: 'Failed to delete shelf',
|
|
||||||
life: 3000
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
];
|
];
|
||||||
}*/
|
}
|
||||||
|
|
||||||
|
deselectAllBooks(): void {
|
||||||
|
this.selectedBooks.clear();
|
||||||
|
this.books$?.subscribe(books => {
|
||||||
|
books.forEach(book => book.selected = false);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
isAnyBookSelected(): boolean {
|
||||||
|
return this.selectedBooks.size > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
openShelfDialog(): void {
|
||||||
|
this.dialogService.open(ShelfAssignerComponent, {
|
||||||
|
header: `Update Books Shelves`,
|
||||||
|
modal: true,
|
||||||
|
width: '30%',
|
||||||
|
height: '70%',
|
||||||
|
contentStyle: {overflow: 'auto'},
|
||||||
|
baseZIndex: 10,
|
||||||
|
data: {
|
||||||
|
isMultiBooks: true,
|
||||||
|
book: this.book,
|
||||||
|
bookIds: this.selectedBooks
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,9 +17,11 @@ export class ShelfAssignerComponent implements OnInit {
|
|||||||
|
|
||||||
shelves$: Observable<Shelf[]>;
|
shelves$: Observable<Shelf[]>;
|
||||||
book: Book;
|
book: Book;
|
||||||
selectedShelves: any[] = [];
|
selectedShelves: Shelf[] = [];
|
||||||
displayShelfDialog: boolean = false;
|
displayShelfDialog: boolean = false;
|
||||||
shelfName: string = '';
|
shelfName: string = '';
|
||||||
|
bookIds: Set<number> = new Set();
|
||||||
|
isMultiBooks: boolean = false;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private shelfService: ShelfService, private dynamicDialogConfig: DynamicDialogConfig,
|
private shelfService: ShelfService, private dynamicDialogConfig: DynamicDialogConfig,
|
||||||
@@ -27,9 +29,12 @@ export class ShelfAssignerComponent implements OnInit {
|
|||||||
private bookService: BookService) {
|
private bookService: BookService) {
|
||||||
this.shelves$ = this.shelfService.shelves$;
|
this.shelves$ = this.shelfService.shelves$;
|
||||||
this.book = this.dynamicDialogConfig.data.book;
|
this.book = this.dynamicDialogConfig.data.book;
|
||||||
|
this.bookIds = this.dynamicDialogConfig.data.bookIds;
|
||||||
|
this.isMultiBooks = this.dynamicDialogConfig.data.isMultiBooks;
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
|
if (!this.isMultiBooks) {
|
||||||
this.shelves$.subscribe(shelves => {
|
this.shelves$.subscribe(shelves => {
|
||||||
if (this.book.shelves) {
|
if (this.book.shelves) {
|
||||||
const bookShelfIds = this.book.shelves.map(shelf => shelf.id);
|
const bookShelfIds = this.book.shelves.map(shelf => shelf.id);
|
||||||
@@ -37,6 +42,7 @@ export class ShelfAssignerComponent implements OnInit {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
saveNewShelf() {
|
saveNewShelf() {
|
||||||
this.shelfService.createShelf(this.shelfName).subscribe(
|
this.shelfService.createShelf(this.shelfName).subscribe(
|
||||||
@@ -52,8 +58,9 @@ export class ShelfAssignerComponent implements OnInit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
updateBooksShelves() {
|
updateBooksShelves() {
|
||||||
const shelfIds = this.selectedShelves.map((shelf) => shelf.id);
|
if (!this.isMultiBooks) {
|
||||||
this.bookService.assignShelvesToBook(this.book, shelfIds).subscribe(
|
const shelfIds: (number | undefined)[] = this.selectedShelves.map((shelf) => shelf.id);
|
||||||
|
this.bookService.assignShelvesToBook(new Set([this.book.id]), new Set(shelfIds), this.getIdsToUnAssign(this.book, new Set(shelfIds))).subscribe(
|
||||||
() => {
|
() => {
|
||||||
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Book\'s shelves updated'});
|
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Book\'s shelves updated'});
|
||||||
this.dynamicDialogRef.close();
|
this.dynamicDialogRef.close();
|
||||||
@@ -63,6 +70,29 @@ export class ShelfAssignerComponent implements OnInit {
|
|||||||
this.dynamicDialogRef.close();
|
this.dynamicDialogRef.close();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
const shelfIds: (number | undefined)[] = this.selectedShelves.map((shelf) => shelf.id);
|
||||||
|
this.bookService.assignShelvesToBook(this.bookIds, new Set(shelfIds), new Set()).subscribe(
|
||||||
|
() => {
|
||||||
|
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Book\'s shelves updated'});
|
||||||
|
this.dynamicDialogRef.close();
|
||||||
|
},
|
||||||
|
(error) => {
|
||||||
|
this.messageService.add({severity: 'error', summary: 'Error', detail: 'Failed to update book\'s shelves'});
|
||||||
|
this.dynamicDialogRef.close();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getIdsToUnAssign(book: Book, idsToAssign: Set<number | undefined>): Set<number | undefined> {
|
||||||
|
const idsToUnassign = new Set<number>();
|
||||||
|
book.shelves?.forEach(shelf => {
|
||||||
|
if (!idsToAssign.has(shelf.id!)) {
|
||||||
|
idsToUnassign.add(shelf.id!);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return idsToUnassign;
|
||||||
}
|
}
|
||||||
|
|
||||||
createShelfDialog(): void {
|
createShelfDialog(): void {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ export interface Book {
|
|||||||
shelves?: Shelf[];
|
shelves?: Shelf[];
|
||||||
lastReadTime?: string;
|
lastReadTime?: string;
|
||||||
addedOn?: string;
|
addedOn?: string;
|
||||||
|
selected?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface BookMetadata {
|
export interface BookMetadata {
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
import {Injectable} from '@angular/core';
|
import {Injectable} from '@angular/core';
|
||||||
import {BehaviorSubject, filter, Observable, of, tap} from 'rxjs';
|
import {BehaviorSubject, Observable, of} from 'rxjs';
|
||||||
import {HttpClient} from '@angular/common/http';
|
import {HttpClient} from '@angular/common/http';
|
||||||
import {catchError, map, switchMap} from 'rxjs/operators';
|
import {catchError, map, switchMap} from 'rxjs/operators';
|
||||||
import {Book, BookMetadata, BookSetting, BookWithNeighborsDTO} from '../model/book.model';
|
import {Book, BookMetadata, BookSetting} from '../model/book.model';
|
||||||
import {ShelfService} from './shelf.service';
|
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root'
|
providedIn: 'root'
|
||||||
@@ -14,7 +13,7 @@ export class BookService {
|
|||||||
private books = new BehaviorSubject<Book[]>([]);
|
private books = new BehaviorSubject<Book[]>([]);
|
||||||
books$ = this.books.asObservable();
|
books$ = this.books.asObservable();
|
||||||
|
|
||||||
constructor(private http: HttpClient, private shelfService: ShelfService) {
|
constructor(private http: HttpClient) {
|
||||||
this.http.get<Book[]>(this.url).pipe(
|
this.http.get<Book[]>(this.url).pipe(
|
||||||
catchError(error => {
|
catchError(error => {
|
||||||
console.error('Error loading books:', error);
|
console.error('Error loading books:', error);
|
||||||
@@ -25,23 +24,26 @@ export class BookService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
assignShelvesToBook(book: Book, shelfIds: number[]): Observable<Book> {
|
assignShelvesToBook(bookIds: Set<number | undefined>, shelvesToAssign: Set<number | undefined>, shelvesToUnassign: Set<number | undefined>): Observable<Book[]> {
|
||||||
const requestPayload = {
|
const requestPayload = {
|
||||||
bookId: book.id,
|
bookIds: Array.from(bookIds),
|
||||||
shelfIds: shelfIds,
|
shelvesToAssign: Array.from(shelvesToAssign),
|
||||||
|
shelvesToUnassign: Array.from(shelvesToUnassign),
|
||||||
};
|
};
|
||||||
return this.http.post<Book>(`${this.url}/assign-shelves`, requestPayload).pipe(
|
return this.http.post<Book[]>(`${this.url}/assign-shelves`, requestPayload).pipe(
|
||||||
map((updatedBook) => {
|
map((updatedBooks) => {
|
||||||
const currentBooks = this.books.value;
|
const currentBooks = this.books.value;
|
||||||
|
updatedBooks.forEach(updatedBook => {
|
||||||
const bookIndex = currentBooks.findIndex(b => b.id === updatedBook.id);
|
const bookIndex = currentBooks.findIndex(b => b.id === updatedBook.id);
|
||||||
if (bookIndex !== -1) {
|
if (bookIndex !== -1) {
|
||||||
currentBooks[bookIndex] = updatedBook;
|
currentBooks[bookIndex] = updatedBook;
|
||||||
this.books.next([...currentBooks]);
|
|
||||||
}
|
}
|
||||||
return updatedBook;
|
});
|
||||||
|
this.books.next([...currentBooks]);
|
||||||
|
return updatedBooks;
|
||||||
}),
|
}),
|
||||||
catchError(error => {
|
catchError(error => {
|
||||||
console.error('Error assigning shelves to book:', error);
|
console.error('Error assigning shelves to books:', error);
|
||||||
throw error;
|
throw error;
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user