From 30cd7014debac81de6bfd478947037543054eea3 Mon Sep 17 00:00:00 2001 From: metal079 Date: Tue, 12 Mar 2024 22:16:41 -0500 Subject: [PATCH] Delete image history button --- src/app/home/options/options.component.html | 5 ++ src/app/home/options/options.component.ts | 74 ++++++++++++++++----- 2 files changed, 61 insertions(+), 18 deletions(-) diff --git a/src/app/home/options/options.component.html b/src/app/home/options/options.component.html index ccada39..b0a8f00 100644 --- a/src/app/home/options/options.component.html +++ b/src/app/home/options/options.component.html @@ -237,6 +237,11 @@ + + + diff --git a/src/app/home/options/options.component.ts b/src/app/home/options/options.component.ts index d952110..56f6e60 100644 --- a/src/app/home/options/options.component.ts +++ b/src/app/home/options/options.component.ts @@ -325,31 +325,37 @@ export class OptionsComponent { const db = await this.openDatabase(); const transaction = db.transaction(this.storeName, 'readonly'); const store = transaction.objectStore(this.storeName); - const request = store.getAll(); - + request.onsuccess = (event) => { this.userGeneratedImages = request.result; - - // Sort the userGeneratedImages array based on the timestamp in descending order - this.userGeneratedImages.sort((a, b) => { - const timestampA = a.timestamp ? a.timestamp.getTime() : 0; - const timestampB = b.timestamp ? b.timestamp.getTime() : 0; - return timestampB - timestampA; - }); - - // Calculate the total number of pages - this.totalPages = Math.ceil(this.userGeneratedImages.length / this.imagesPerPage); - - // Display the first page of images - this.searchImages(); - this.paginateImages(); + + if (this.userGeneratedImages.length > 0) { + // Sort the userGeneratedImages array based on the timestamp in descending order + this.userGeneratedImages.sort((a, b) => { + const timestampA = a.timestamp ? a.timestamp.getTime() : 0; + const timestampB = b.timestamp ? b.timestamp.getTime() : 0; + return timestampB - timestampA; + }); + + // Calculate the total number of pages + this.totalPages = Math.ceil(this.userGeneratedImages.length / this.imagesPerPage); + + // Display the first page of images + this.searchImages(); + this.paginateImages(); + } else { + // No images found, reset the pagination variables + this.currentPage = 1; + this.totalPages = 1; + this.paginatedImages = []; + } }; - + request.onerror = (event) => { console.error('Failed to retrieve images from IndexedDB', event); }; - + // Wait for the transaction to complete before proceeding await new Promise((resolve) => { transaction.oncomplete = () => { @@ -696,6 +702,38 @@ export class OptionsComponent { this.sortImages(); } + async deleteAllImages() { + try { + const db = await this.openDatabase(); + const transaction = db.transaction(this.storeName, 'readwrite'); + const store = transaction.objectStore(this.storeName); + + const request = store.clear(); + + request.onsuccess = (event) => { + console.log('All images deleted from IndexedDB'); + this.userGeneratedImages = []; + this.filteredImages = []; + this.paginatedImages = []; + this.currentPage = 1; + this.totalPages = 1; + }; + + request.onerror = (event) => { + console.error('Failed to delete images from IndexedDB', event); + }; + + // Wait for the transaction to complete before proceeding + await new Promise((resolve) => { + transaction.oncomplete = () => { + resolve(undefined); + }; + }); + } catch (error) { + console.error('Failed to open database', error); + } + } + // Example function called after successful Discord login onDiscordLoginSuccess(userData: any) { localStorage.setItem('discordUserData', JSON.stringify(userData));