Organize code
This commit is contained in:
+99
@@ -0,0 +1,99 @@
|
||||
<div class="cover-search-container">
|
||||
<div class="search-section">
|
||||
<form [formGroup]="searchForm" (ngSubmit)="onSearch()">
|
||||
<div class="form-grid">
|
||||
<div class="form-field">
|
||||
<label for="title">Title *</label>
|
||||
<input
|
||||
pInputText
|
||||
id="title"
|
||||
formControlName="title"
|
||||
placeholder="Enter book title"
|
||||
/>
|
||||
@if (searchForm.get('title')?.invalid && searchForm.get('title')?.touched) {
|
||||
<small class="error-message">
|
||||
Title is required
|
||||
</small>
|
||||
}
|
||||
</div>
|
||||
|
||||
<div class="form-field">
|
||||
<label for="author">Author</label>
|
||||
<input
|
||||
pInputText
|
||||
id="author"
|
||||
formControlName="author"
|
||||
placeholder="Enter author name"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="form-field">
|
||||
<label> </label>
|
||||
<div class="button-group">
|
||||
<p-button
|
||||
type="submit"
|
||||
outlined
|
||||
label="Search Covers"
|
||||
icon="pi pi-search"
|
||||
[loading]="loading"
|
||||
[disabled]="searchForm.invalid">
|
||||
</p-button>
|
||||
|
||||
<p-button
|
||||
type="button"
|
||||
label="Clear"
|
||||
icon="pi pi-times"
|
||||
(click)="onClear()"
|
||||
styleClass="p-button-secondary p-button-outlined">
|
||||
</p-button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
@if (hasSearched) {
|
||||
<div class="results-section">
|
||||
@if (loading) {
|
||||
<div class="loading-container">
|
||||
<p-progressSpinner></p-progressSpinner>
|
||||
<p>Searching for book covers...</p>
|
||||
</div>
|
||||
}
|
||||
|
||||
@if (!loading && coverImages.length === 0) {
|
||||
<div class="no-results">
|
||||
<i class="pi pi-info-circle"></i>
|
||||
<p>No cover images found. Try adjusting your search terms.</p>
|
||||
</div>
|
||||
}
|
||||
|
||||
@if (!loading && coverImages.length > 0) {
|
||||
<div class="covers-grid">
|
||||
@for (cover of coverImages; track cover.url; let i = $index) {
|
||||
<div class="cover-item">
|
||||
<p-image
|
||||
[src]="cover.url"
|
||||
[alt]="'Book cover ' + (i + 1)"
|
||||
appendTo="body"
|
||||
[preview]="true">
|
||||
</p-image>
|
||||
@if (cover.width && cover.height) {
|
||||
<div class="resolution-overlay">
|
||||
{{cover.width}} x {{cover.height}}
|
||||
</div>
|
||||
}
|
||||
<p-button
|
||||
class="select-button"
|
||||
(click)="selectAndSave(cover)"
|
||||
label="Pick & Save"
|
||||
severity="info"
|
||||
title="Select & Save this cover">
|
||||
</p-button>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
+148
@@ -0,0 +1,148 @@
|
||||
.cover-search-container {
|
||||
margin: 0 auto;
|
||||
|
||||
.search-section {
|
||||
background: var(--surface-card);
|
||||
border: 1px solid var(--surface-border);
|
||||
border-radius: 8px;
|
||||
padding: 0 1.5rem 0 1.5rem;
|
||||
margin-bottom: 2rem;
|
||||
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
||||
|
||||
h2 {
|
||||
margin: 0 0 1.5rem 0;
|
||||
color: var(--text-color);
|
||||
}
|
||||
|
||||
.form-grid {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 1fr auto;
|
||||
gap: 1rem;
|
||||
align-items: end;
|
||||
|
||||
.form-field {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.5rem;
|
||||
|
||||
label {
|
||||
font-weight: 500;
|
||||
color: var(--text-color);
|
||||
}
|
||||
|
||||
.error-message {
|
||||
color: var(--red-500);
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.button-group {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
}
|
||||
|
||||
.results-section {
|
||||
padding: 0 1.5rem 0 1.5rem;
|
||||
|
||||
.loading-container {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
padding: 3rem;
|
||||
background: var(--surface-card);
|
||||
border-radius: 8px;
|
||||
border: 1px solid var(--surface-border);
|
||||
|
||||
p {
|
||||
margin-top: 1rem;
|
||||
color: var(--text-color-secondary);
|
||||
}
|
||||
}
|
||||
|
||||
.no-results {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
padding: 3rem;
|
||||
color: var(--text-color-secondary);
|
||||
background: var(--surface-card);
|
||||
border-radius: 8px;
|
||||
border: 1px solid var(--surface-border);
|
||||
|
||||
i {
|
||||
font-size: 2rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
}
|
||||
|
||||
.covers-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
|
||||
gap: 1.5rem;
|
||||
padding: 1rem 0;
|
||||
|
||||
.cover-item {
|
||||
position: relative;
|
||||
border: 1px solid transparent;
|
||||
border-radius: 8px;
|
||||
padding: 4px;
|
||||
transition: all 0.3s ease;
|
||||
|
||||
&:hover {
|
||||
border-color: var(--primary-color);
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
|
||||
}
|
||||
|
||||
.resolution-overlay {
|
||||
position: absolute;
|
||||
top: 8px;
|
||||
right: 8px;
|
||||
background: rgba(0, 0, 0, 0.8);
|
||||
color: white;
|
||||
padding: 4px 8px;
|
||||
border-radius: 4px;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 600;
|
||||
backdrop-filter: blur(4px);
|
||||
z-index: 1;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.select-button {
|
||||
position: absolute !important;
|
||||
bottom: 8px;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
width: 120px;
|
||||
opacity: 0;
|
||||
transition: opacity 0.3s ease;
|
||||
z-index: 2;
|
||||
cursor: pointer;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
&:hover .select-button {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.cover-search-container .search-section .form-grid {
|
||||
grid-template-columns: 1fr;
|
||||
|
||||
.form-field .button-group {
|
||||
justify-content: stretch;
|
||||
|
||||
::ng-deep .p-button {
|
||||
flex: 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+116
@@ -0,0 +1,116 @@
|
||||
import {Component, inject, Input, OnInit} from '@angular/core';
|
||||
import {Toast} from 'primeng/toast';
|
||||
import {MessageService} from 'primeng/api';
|
||||
import {FormBuilder, FormGroup, FormsModule, ReactiveFormsModule, Validators} from '@angular/forms';
|
||||
import {BookCoverService, CoverFetchRequest, CoverImage} from '../../../../shared/services/book-cover.service';
|
||||
import {finalize} from 'rxjs/operators';
|
||||
import {Button} from 'primeng/button';
|
||||
import {InputText} from 'primeng/inputtext';
|
||||
import {ProgressSpinner} from 'primeng/progressspinner';
|
||||
import {DynamicDialogConfig, DynamicDialogRef} from 'primeng/dynamicdialog';
|
||||
import {BookService} from '../../../book/service/book.service';
|
||||
import {Image} from 'primeng/image';
|
||||
|
||||
@Component({
|
||||
selector: 'app-cover-search',
|
||||
templateUrl: './cover-search.component.html',
|
||||
imports: [
|
||||
Button,
|
||||
ReactiveFormsModule,
|
||||
FormsModule,
|
||||
InputText,
|
||||
ProgressSpinner,
|
||||
Image
|
||||
],
|
||||
styleUrls: ['./cover-search.component.scss']
|
||||
})
|
||||
export class CoverSearchComponent implements OnInit {
|
||||
@Input() bookId!: number;
|
||||
searchForm: FormGroup;
|
||||
coverImages: CoverImage[] = [];
|
||||
loading = false;
|
||||
hasSearched = false;
|
||||
|
||||
private fb = inject(FormBuilder);
|
||||
private bookCoverService = inject(BookCoverService);
|
||||
private dynamicDialogConfig = inject(DynamicDialogConfig);
|
||||
protected dynamicDialogRef = inject(DynamicDialogRef);
|
||||
protected bookService = inject(BookService);
|
||||
private messageService = inject(MessageService);
|
||||
|
||||
constructor() {
|
||||
this.searchForm = this.fb.group({
|
||||
title: ['', Validators.required],
|
||||
author: ['']
|
||||
});
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
this.bookId = this.dynamicDialogConfig.data.bookId;
|
||||
let book = this.bookService.getBookByIdFromState(this.bookId);
|
||||
|
||||
if (book) {
|
||||
this.searchForm.patchValue({
|
||||
title: book.metadata?.title || '',
|
||||
author: book.metadata?.authors && book.metadata?.authors.length > 0 ? book.metadata?.authors[0] : ''
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
onSearch() {
|
||||
if (this.searchForm.valid) {
|
||||
this.loading = true;
|
||||
const request: CoverFetchRequest = {
|
||||
title: this.searchForm.value.title,
|
||||
author: this.searchForm.value.author
|
||||
};
|
||||
|
||||
this.bookCoverService.fetchBookCovers(request)
|
||||
.pipe(finalize(() => this.loading = false))
|
||||
.subscribe({
|
||||
next: (images) => {
|
||||
console.log('API response received:', images);
|
||||
this.coverImages = images.sort((a, b) => a.index - b.index);
|
||||
this.hasSearched = true;
|
||||
},
|
||||
error: (error) => {
|
||||
console.error('Error fetching covers:', error);
|
||||
this.coverImages = [];
|
||||
this.hasSearched = true;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
console.log('Form invalid', {
|
||||
formErrors: this.searchForm.errors,
|
||||
titleErrors: this.searchForm.get('title')?.errors
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
selectAndSave(image: CoverImage) {
|
||||
this.bookService.uploadCoverFromUrl(this.bookId, image.url)
|
||||
.subscribe({
|
||||
next: () => {
|
||||
this.messageService.add({
|
||||
severity: 'success',
|
||||
summary: 'Cover Updated',
|
||||
detail: 'Cover image updated successfully.'
|
||||
});
|
||||
this.dynamicDialogRef.close();
|
||||
},
|
||||
error: err => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Cover Update Failed',
|
||||
detail: err?.message || 'Failed to update cover image.'
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
onClear() {
|
||||
this.searchForm.reset();
|
||||
this.coverImages = [];
|
||||
this.hasSearched = false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user