fix(file-mover): fix file mover never updating (#651)
This commit is contained in:
@@ -26,7 +26,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="banner-content">
|
<div class="banner-content">
|
||||||
<span class="banner-text">
|
<span class="banner-text">
|
||||||
Preview and organize <strong>{{ filePreviews.length }} file{{ filePreviews.length !== 1 ? 's' : '' }}</strong>.
|
Preview and organize <strong>{{ filePreviews().length }} file{{ filePreviews().length !== 1 ? 's' : '' }}</strong>.
|
||||||
Files will be renamed based on metadata and naming patterns.
|
Files will be renamed based on metadata and naming patterns.
|
||||||
</span>
|
</span>
|
||||||
<span class="banner-warning">
|
<span class="banner-warning">
|
||||||
@@ -132,7 +132,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="table-container">
|
<div class="table-container">
|
||||||
<p-table
|
<p-table
|
||||||
[value]="filePreviews"
|
[value]="filePreviews()"
|
||||||
[scrollable]="true"
|
[scrollable]="true"
|
||||||
scrollHeight="flex"
|
scrollHeight="flex"
|
||||||
[virtualScroll]="true"
|
[virtualScroll]="true"
|
||||||
@@ -231,11 +231,11 @@
|
|||||||
(click)="cancel()"
|
(click)="cancel()"
|
||||||
></p-button>
|
></p-button>
|
||||||
<p-button
|
<p-button
|
||||||
[label]="loading ? 'Moving...' : 'Move Files'"
|
[label]="loading() ? 'Moving...' : 'Move Files'"
|
||||||
[icon]="loading ? 'pi pi-spin pi-spinner' : 'pi pi-check'"
|
[icon]="loading() ? 'pi pi-spin pi-spinner' : 'pi pi-check'"
|
||||||
severity="success"
|
severity="success"
|
||||||
(click)="saveChanges()"
|
(click)="saveChanges()"
|
||||||
[disabled]="loading || filePreviews.length === 0"
|
[disabled]="loading() || filePreviews().length === 0"
|
||||||
></p-button>
|
></p-button>
|
||||||
} @else {
|
} @else {
|
||||||
<p-button
|
<p-button
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import {Component, effect, inject, OnDestroy} from '@angular/core';
|
import {Component, effect, inject, OnDestroy, signal} from '@angular/core';
|
||||||
import {FormsModule} from '@angular/forms';
|
import {FormsModule} from '@angular/forms';
|
||||||
import {Button} from 'primeng/button';
|
import {Button} from 'primeng/button';
|
||||||
import {TableModule} from 'primeng/table';
|
import {TableModule} from 'primeng/table';
|
||||||
@@ -108,13 +108,13 @@ export class FileMoverComponent implements OnDestroy {
|
|||||||
bookCount: number;
|
bookCount: number;
|
||||||
}[] = [];
|
}[] = [];
|
||||||
defaultMovePattern = '';
|
defaultMovePattern = '';
|
||||||
loading = false;
|
loading = signal(false);
|
||||||
patternsCollapsed = true;
|
patternsCollapsed = true;
|
||||||
infoCollapsed = true;
|
infoCollapsed = true;
|
||||||
|
|
||||||
bookIds = new Set<number>();
|
bookIds = new Set<number>();
|
||||||
books: Book[] = [];
|
books: Book[] = [];
|
||||||
filePreviews: FilePreview[] = [];
|
filePreviews = signal<FilePreview[]>([]);
|
||||||
defaultTargetLibraryId: number | null = null;
|
defaultTargetLibraryId: number | null = null;
|
||||||
defaultTargetLibraryPathId: number | null = null;
|
defaultTargetLibraryPathId: number | null = null;
|
||||||
defaultAvailableLibraryPaths: LibraryPath[] = [];
|
defaultAvailableLibraryPaths: LibraryPath[] = [];
|
||||||
@@ -134,7 +134,7 @@ export class FileMoverComponent implements OnDestroy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
applyPattern(): void {
|
applyPattern(): void {
|
||||||
this.filePreviews = this.books.map(book => {
|
const previews = this.books.map(book => {
|
||||||
const fileName = book.fileName ?? '';
|
const fileName = book.fileName ?? '';
|
||||||
const fileSubPath = book.fileSubPath ? `${book.fileSubPath.replace(/\/+$/g, '')}/` : '';
|
const fileSubPath = book.fileSubPath ? `${book.fileSubPath.replace(/\/+$/g, '')}/` : '';
|
||||||
|
|
||||||
@@ -169,39 +169,46 @@ export class FileMoverComponent implements OnDestroy {
|
|||||||
this.updatePreviewPaths(preview, book);
|
this.updatePreviewPaths(preview, book);
|
||||||
return preview;
|
return preview;
|
||||||
});
|
});
|
||||||
|
this.filePreviews.set(previews);
|
||||||
}
|
}
|
||||||
|
|
||||||
onDefaultLibraryChange(): void {
|
onDefaultLibraryChange(): void {
|
||||||
this.defaultAvailableLibraryPaths = this.getLibraryPathsById(this.defaultTargetLibraryId);
|
this.defaultAvailableLibraryPaths = this.getLibraryPathsById(this.defaultTargetLibraryId);
|
||||||
this.defaultTargetLibraryPathId = this.defaultAvailableLibraryPaths.length > 0 ? this.defaultAvailableLibraryPaths[0].id ?? null : null;
|
this.defaultTargetLibraryPathId = this.defaultAvailableLibraryPaths.length > 0 ? this.defaultAvailableLibraryPaths[0].id ?? null : null;
|
||||||
|
|
||||||
this.filePreviews.forEach(preview => {
|
this.filePreviews.update(previews => {
|
||||||
if (!preview.isMoved) {
|
previews.forEach(preview => {
|
||||||
preview.targetLibraryId = this.defaultTargetLibraryId;
|
if (!preview.isMoved) {
|
||||||
preview.targetLibraryName = this.getLibraryNameById(this.defaultTargetLibraryId);
|
preview.targetLibraryId = this.defaultTargetLibraryId;
|
||||||
preview.availableLibraryPaths = this.defaultAvailableLibraryPaths;
|
preview.targetLibraryName = this.getLibraryNameById(this.defaultTargetLibraryId);
|
||||||
preview.targetLibraryPathId = this.defaultTargetLibraryPathId;
|
preview.availableLibraryPaths = this.defaultAvailableLibraryPaths;
|
||||||
preview.targetLibraryPath = this.defaultAvailableLibraryPaths.find(p => p.id === this.defaultTargetLibraryPathId)?.path || '';
|
preview.targetLibraryPathId = this.defaultTargetLibraryPathId;
|
||||||
|
preview.targetLibraryPath = this.defaultAvailableLibraryPaths.find(p => p.id === this.defaultTargetLibraryPathId)?.path || '';
|
||||||
|
|
||||||
const book = this.books.find(b => b.id === preview.bookId);
|
const book = this.books.find(b => b.id === preview.bookId);
|
||||||
if (book) {
|
if (book) {
|
||||||
this.updatePreviewPaths(preview, book);
|
this.updatePreviewPaths(preview, book);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
return [...previews];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
onDefaultLibraryPathChange(): void {
|
onDefaultLibraryPathChange(): void {
|
||||||
this.filePreviews.forEach(preview => {
|
this.filePreviews.update(previews => {
|
||||||
if (!preview.isMoved && preview.targetLibraryId === this.defaultTargetLibraryId) {
|
previews.forEach(preview => {
|
||||||
preview.targetLibraryPathId = this.defaultTargetLibraryPathId;
|
if (!preview.isMoved && preview.targetLibraryId === this.defaultTargetLibraryId) {
|
||||||
preview.targetLibraryPath = this.defaultAvailableLibraryPaths.find(p => p.id === this.defaultTargetLibraryPathId)?.path || '';
|
preview.targetLibraryPathId = this.defaultTargetLibraryPathId;
|
||||||
|
preview.targetLibraryPath = this.defaultAvailableLibraryPaths.find(p => p.id === this.defaultTargetLibraryPathId)?.path || '';
|
||||||
|
|
||||||
const book = this.books.find(b => b.id === preview.bookId);
|
const book = this.books.find(b => b.id === preview.bookId);
|
||||||
if (book) {
|
if (book) {
|
||||||
this.updatePreviewPaths(preview, book);
|
this.updatePreviewPaths(preview, book);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
return [...previews];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,6 +222,7 @@ export class FileMoverComponent implements OnDestroy {
|
|||||||
if (book) {
|
if (book) {
|
||||||
this.updatePreviewPaths(preview, book);
|
this.updatePreviewPaths(preview, book);
|
||||||
}
|
}
|
||||||
|
this.filePreviews.update(previews => [...previews]);
|
||||||
}
|
}
|
||||||
|
|
||||||
onLibraryPathChange(preview: FilePreview): void {
|
onLibraryPathChange(preview: FilePreview): void {
|
||||||
@@ -225,6 +233,7 @@ export class FileMoverComponent implements OnDestroy {
|
|||||||
if (book) {
|
if (book) {
|
||||||
this.updatePreviewPaths(preview, book);
|
this.updatePreviewPaths(preview, book);
|
||||||
}
|
}
|
||||||
|
this.filePreviews.update(previews => [...previews]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private updatePreviewPaths(preview: FilePreview, book: Book): void {
|
private updatePreviewPaths(preview: FilePreview, book: Book): void {
|
||||||
@@ -300,7 +309,7 @@ export class FileMoverComponent implements OnDestroy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get movedFileCount(): number {
|
get movedFileCount(): number {
|
||||||
return this.filePreviews.filter(p => p.isMoved).length;
|
return this.filePreviews().filter(p => p.isMoved).length;
|
||||||
}
|
}
|
||||||
|
|
||||||
sanitize(input: string | undefined): string {
|
sanitize(input: string | undefined): string {
|
||||||
@@ -341,11 +350,11 @@ export class FileMoverComponent implements OnDestroy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
saveChanges(): void {
|
saveChanges(): void {
|
||||||
this.loading = true;
|
this.loading.set(true);
|
||||||
|
|
||||||
const request: FileMoveRequest = {
|
const request: FileMoveRequest = {
|
||||||
bookIds: [...this.bookIds],
|
bookIds: [...this.bookIds],
|
||||||
moves: this.filePreviews.map(preview => ({
|
moves: this.filePreviews().map(preview => ({
|
||||||
bookId: preview.bookId,
|
bookId: preview.bookId,
|
||||||
targetLibraryId: preview.targetLibraryId,
|
targetLibraryId: preview.targetLibraryId,
|
||||||
targetLibraryPathId: preview.targetLibraryPathId
|
targetLibraryPathId: preview.targetLibraryPathId
|
||||||
@@ -356,17 +365,21 @@ export class FileMoverComponent implements OnDestroy {
|
|||||||
takeUntil(this.destroy$)
|
takeUntil(this.destroy$)
|
||||||
).subscribe({
|
).subscribe({
|
||||||
next: () => {
|
next: () => {
|
||||||
this.loading = false;
|
this.loading.set(false);
|
||||||
this.filePreviews.forEach(p => (p.isMoved = true));
|
this.filePreviews.update(previews => {
|
||||||
|
previews.forEach(p => (p.isMoved = true));
|
||||||
|
return [...previews];
|
||||||
|
});
|
||||||
|
const currentPreviews = this.filePreviews();
|
||||||
this.messageService.add({
|
this.messageService.add({
|
||||||
severity: 'success',
|
severity: 'success',
|
||||||
summary: 'Files Organized!',
|
summary: 'Files Organized!',
|
||||||
detail: `Successfully organized ${this.filePreviews.length} file${this.filePreviews.length === 1 ? '' : 's'}.`,
|
detail: `Successfully organized ${currentPreviews.length} file${currentPreviews.length === 1 ? '' : 's'}.`,
|
||||||
life: 3000
|
life: 3000
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
error: () => {
|
error: () => {
|
||||||
this.loading = false;
|
this.loading.set(false);
|
||||||
this.messageService.add({
|
this.messageService.add({
|
||||||
severity: 'error',
|
severity: 'error',
|
||||||
summary: 'Oops! Something went wrong',
|
summary: 'Oops! Something went wrong',
|
||||||
|
|||||||
Reference in New Issue
Block a user