refactor(frontend): rename booklore-ui to frontend
- Frontend path: - rename the tracked Angular app tree from `booklore-ui/` to `frontend/` - preserve file contents during the move so this commit stays focused on the path change itself - Frontend docs: - update the moved frontend README and contribution guide where they refer to the renamed folder - keep those edits limited to path references introduced by the directory rename
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
import {QueryClient} from '@tanstack/angular-query-experimental';
|
||||
|
||||
import {AuthorSummary} from '../model/author.model';
|
||||
import {AUTHORS_QUERY_KEY} from './author-query-keys';
|
||||
|
||||
export function patchAuthorInCache(queryClient: QueryClient, authorId: number, fields: Partial<AuthorSummary>): void {
|
||||
queryClient.setQueryData<AuthorSummary[]>(AUTHORS_QUERY_KEY, current =>
|
||||
(current ?? []).map(author => author.id === authorId ? {...author, ...fields} : author)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export const AUTHORS_QUERY_KEY = ['authors'] as const;
|
||||
@@ -0,0 +1,56 @@
|
||||
import {inject, Injectable, signal} from '@angular/core';
|
||||
import {LocalStorageService} from '../../../shared/service/local-storage.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AuthorScalePreferenceService {
|
||||
|
||||
private readonly DEBOUNCE_MS = 1000;
|
||||
private readonly STORAGE_KEY = 'authorScalePreference';
|
||||
|
||||
private readonly localStorageService = inject(LocalStorageService);
|
||||
|
||||
private readonly _scaleFactor = signal(1.0);
|
||||
readonly scaleFactor = this._scaleFactor.asReadonly();
|
||||
|
||||
private saveTimeout: ReturnType<typeof setTimeout> | null = null;
|
||||
private lastPersistedScale = 1.0;
|
||||
|
||||
constructor() {
|
||||
const initialScale = this.loadScaleFromStorage();
|
||||
this._scaleFactor.set(initialScale);
|
||||
this.lastPersistedScale = initialScale;
|
||||
}
|
||||
|
||||
setScale(scale: number): void {
|
||||
this._scaleFactor.set(scale);
|
||||
if (scale === this.lastPersistedScale) {
|
||||
return;
|
||||
}
|
||||
this.scheduleSave(scale);
|
||||
}
|
||||
|
||||
private scheduleSave(scale: number): void {
|
||||
if (this.saveTimeout) {
|
||||
clearTimeout(this.saveTimeout);
|
||||
}
|
||||
|
||||
this.saveTimeout = setTimeout(() => {
|
||||
this.saveScalePreference(scale);
|
||||
}, this.DEBOUNCE_MS);
|
||||
}
|
||||
|
||||
private saveScalePreference(scale: number): void {
|
||||
this.localStorageService.set(this.STORAGE_KEY, scale);
|
||||
this.lastPersistedScale = scale;
|
||||
}
|
||||
|
||||
private loadScaleFromStorage(): number {
|
||||
const saved = this.localStorageService.get<number>(this.STORAGE_KEY);
|
||||
if (saved !== null && !isNaN(saved)) {
|
||||
return saved;
|
||||
}
|
||||
return 1.0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
import {computed, Injectable, signal} from '@angular/core';
|
||||
import {AuthorSummary} from '../model/author.model';
|
||||
|
||||
export interface AuthorCheckboxClickEvent {
|
||||
index: number;
|
||||
author: AuthorSummary;
|
||||
selected: boolean;
|
||||
shiftKey: boolean;
|
||||
}
|
||||
|
||||
@Injectable({providedIn: 'root'})
|
||||
export class AuthorSelectionService {
|
||||
private readonly _selectedAuthors = signal<Set<number>>(new Set());
|
||||
readonly selectedAuthors = this._selectedAuthors.asReadonly();
|
||||
readonly selectedCount = computed(() => this._selectedAuthors().size);
|
||||
|
||||
private currentAuthors: AuthorSummary[] = [];
|
||||
private lastSelectedIndex: number | null = null;
|
||||
|
||||
setCurrentAuthors(authors: AuthorSummary[]): void {
|
||||
this.currentAuthors = authors;
|
||||
}
|
||||
|
||||
handleCheckboxClick(event: AuthorCheckboxClickEvent): void {
|
||||
const {index, author, selected, shiftKey} = event;
|
||||
|
||||
if (!shiftKey || this.lastSelectedIndex === null) {
|
||||
if (selected) {
|
||||
this.select(author.id);
|
||||
} else {
|
||||
this.deselect(author.id);
|
||||
}
|
||||
this.lastSelectedIndex = index;
|
||||
} else {
|
||||
const start = Math.min(this.lastSelectedIndex, index);
|
||||
const end = Math.max(this.lastSelectedIndex, index);
|
||||
for (let i = start; i <= end; i++) {
|
||||
const a = this.currentAuthors[i];
|
||||
if (!a) continue;
|
||||
if (selected) {
|
||||
this.select(a.id);
|
||||
} else {
|
||||
this.deselect(a.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
selectAll(): void {
|
||||
this._selectedAuthors.update(current => {
|
||||
const next = new Set(current);
|
||||
for (const author of this.currentAuthors) {
|
||||
next.add(author.id);
|
||||
}
|
||||
return next;
|
||||
});
|
||||
}
|
||||
|
||||
deselectAll(): void {
|
||||
this._selectedAuthors.set(new Set());
|
||||
this.lastSelectedIndex = null;
|
||||
}
|
||||
|
||||
getSelectedIds(): number[] {
|
||||
return Array.from(this.selectedAuthors());
|
||||
}
|
||||
|
||||
private select(id: number): void {
|
||||
this._selectedAuthors.update(current => {
|
||||
const next = new Set(current);
|
||||
next.add(id);
|
||||
return next;
|
||||
});
|
||||
}
|
||||
|
||||
private deselect(id: number): void {
|
||||
this._selectedAuthors.update(current => {
|
||||
const next = new Set(current);
|
||||
next.delete(id);
|
||||
return next;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
import {computed, effect, inject, Injectable} from '@angular/core';
|
||||
import {HttpClient, HttpHeaders} from '@angular/common/http';
|
||||
import {lastValueFrom, Observable} from 'rxjs';
|
||||
import {map} from 'rxjs/operators';
|
||||
import {SseClient} from 'ngx-sse-client';
|
||||
import {API_CONFIG} from '../../../core/config/api-config';
|
||||
import {AuthorSummary, AuthorDetails, AuthorSearchResult, AuthorMatchRequest, AuthorUpdateRequest, AuthorPhotoResult} from '../model/author.model';
|
||||
import {AuthService} from '../../../shared/service/auth.service';
|
||||
import {injectQuery, queryOptions, QueryClient} from '@tanstack/angular-query-experimental';
|
||||
import {AUTHORS_QUERY_KEY} from './author-query-keys';
|
||||
import {patchAuthorInCache} from './author-query-cache';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AuthorService {
|
||||
|
||||
private http = inject(HttpClient);
|
||||
private authService = inject(AuthService);
|
||||
private sseClient = inject(SseClient);
|
||||
private queryClient = inject(QueryClient);
|
||||
private readonly baseUrl = `${API_CONFIG.BASE_URL}/api/v1/authors`;
|
||||
private readonly mediaBaseUrl = `${API_CONFIG.BASE_URL}/api/v1/media`;
|
||||
private readonly token = this.authService.token;
|
||||
|
||||
private authorsQuery = injectQuery(() => ({
|
||||
...this.getAuthorsQueryOptions(),
|
||||
enabled: !!this.token(),
|
||||
}));
|
||||
|
||||
allAuthors = computed(() => this.authorsQuery.data() ?? null);
|
||||
isAuthorsLoading = computed(() => !!this.token() && this.authorsQuery.isPending());
|
||||
|
||||
constructor() {
|
||||
effect(() => {
|
||||
const token = this.token();
|
||||
if (token === null) {
|
||||
this.queryClient.removeQueries({queryKey: AUTHORS_QUERY_KEY});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private getAuthorsQueryOptions() {
|
||||
return queryOptions({
|
||||
queryKey: AUTHORS_QUERY_KEY,
|
||||
queryFn: () => lastValueFrom(this.http.get<AuthorSummary[]>(this.baseUrl))
|
||||
});
|
||||
}
|
||||
|
||||
invalidateAuthors(): void {
|
||||
void this.queryClient.invalidateQueries({queryKey: AUTHORS_QUERY_KEY, exact: true});
|
||||
}
|
||||
|
||||
patchAuthorInCache(authorId: number, fields: Partial<AuthorSummary>): void {
|
||||
patchAuthorInCache(this.queryClient, authorId, fields);
|
||||
}
|
||||
|
||||
getAuthorDetails(authorId: number): Observable<AuthorDetails> {
|
||||
return this.http.get<AuthorDetails>(`${this.baseUrl}/${authorId}`);
|
||||
}
|
||||
|
||||
getAuthorByName(name: string): Observable<AuthorDetails> {
|
||||
return this.http.get<AuthorDetails>(`${this.baseUrl}/by-name`, {params: {name}});
|
||||
}
|
||||
|
||||
searchAuthorMetadata(authorId: number, query: string, region: string, asin?: string): Observable<AuthorSearchResult[]> {
|
||||
const params: Record<string, string> = {region};
|
||||
if (asin) {
|
||||
params['asin'] = asin;
|
||||
} else {
|
||||
params['q'] = query;
|
||||
}
|
||||
return this.http.get<AuthorSearchResult[]>(`${this.baseUrl}/${authorId}/search-metadata`, {params});
|
||||
}
|
||||
|
||||
matchAuthor(authorId: number, request: AuthorMatchRequest): Observable<AuthorDetails> {
|
||||
return this.http.post<AuthorDetails>(`${this.baseUrl}/${authorId}/match`, request);
|
||||
}
|
||||
|
||||
quickMatchAuthor(authorId: number, region: string = 'us'): Observable<AuthorDetails> {
|
||||
return this.http.post<AuthorDetails>(`${this.baseUrl}/${authorId}/quick-match`, null, {
|
||||
params: {region}
|
||||
});
|
||||
}
|
||||
|
||||
autoMatchAuthors(authorIds: number[]): Observable<AuthorSummary> {
|
||||
const token = this.authService.getInternalAccessToken();
|
||||
const headers = new HttpHeaders()
|
||||
.set('Content-Type', 'application/json')
|
||||
.set('Authorization', `Bearer ${token}`);
|
||||
|
||||
return this.sseClient.stream(
|
||||
`${this.baseUrl}/auto-match`,
|
||||
{keepAlive: false, reconnectionDelay: 1000, responseType: 'event'},
|
||||
{headers, body: authorIds, withCredentials: true},
|
||||
'POST'
|
||||
).pipe(
|
||||
map(event => {
|
||||
if (event.type === 'error') {
|
||||
throw new Error((event as ErrorEvent).message);
|
||||
}
|
||||
return JSON.parse((event as MessageEvent).data) as AuthorSummary;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
updateAuthor(authorId: number, request: AuthorUpdateRequest): Observable<AuthorDetails> {
|
||||
return this.http.put<AuthorDetails>(`${this.baseUrl}/${authorId}`, request);
|
||||
}
|
||||
|
||||
unmatchAuthors(authorIds: number[]): Observable<void> {
|
||||
return this.http.post<void>(`${this.baseUrl}/unmatch`, authorIds);
|
||||
}
|
||||
|
||||
deleteAuthors(authorIds: number[]): Observable<void> {
|
||||
return this.http.delete<void>(this.baseUrl, {body: authorIds});
|
||||
}
|
||||
|
||||
searchAuthorPhotos(authorId: number, query: string): Observable<AuthorPhotoResult[]> {
|
||||
return this.http.get<AuthorPhotoResult[]>(`${this.baseUrl}/${authorId}/search-photos`, {
|
||||
params: {q: query}
|
||||
});
|
||||
}
|
||||
|
||||
uploadAuthorPhotoFromUrl(authorId: number, imageUrl: string): Observable<void> {
|
||||
return this.http.post<void>(`${this.baseUrl}/${authorId}/photo/url`, null, {
|
||||
params: {url: imageUrl}
|
||||
});
|
||||
}
|
||||
|
||||
getUploadAuthorPhotoUrl(authorId: number): string {
|
||||
return `${this.baseUrl}/${authorId}/photo/upload`;
|
||||
}
|
||||
|
||||
getAuthorPhotoUrl(authorId: number): string {
|
||||
const token = this.authService.getInternalAccessToken();
|
||||
let url = `${this.mediaBaseUrl}/author/${authorId}/photo`;
|
||||
if (token) {
|
||||
url += `?token=${token}`;
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
getAuthorThumbnailUrl(authorId: number, cacheBuster?: number): string {
|
||||
const token = this.authService.getInternalAccessToken();
|
||||
let url = `${this.mediaBaseUrl}/author/${authorId}/thumbnail`;
|
||||
if (token) {
|
||||
url += `?token=${token}`;
|
||||
}
|
||||
if (cacheBuster) {
|
||||
url += (token ? '&' : '?') + 't=' + cacheBuster;
|
||||
}
|
||||
return url;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user