Skip to content

Commit

Permalink
Fix save button disable bug
Browse files Browse the repository at this point in the history
  • Loading branch information
eceeeren committed Nov 21, 2024
1 parent ed9be83 commit 815eb60
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ <h4>
</a>
</div>
<div>
<button type="submit" (click)="updateAttachmentWithFile()" class="btn btn-primary" [disabled]="isPdfLoading() || totalPages() === 0 || !isFileChanged()">
<button
type="submit"
(click)="updateAttachmentWithFile()"
class="btn btn-primary"
[disabled]="(isPdfLoading() || totalPages() === 0 || !isFileChanged()) && !hiddenPagesChanged()"
>
<fa-icon [icon]="faSave" [ngbTooltip]="'entity.action.save' | artemisTranslate"></fa-icon>
<span jhiTranslate="entity.action.save"></span>
</button>
Expand Down
26 changes: 25 additions & 1 deletion src/main/webapp/app/lecture/pdf-preview/pdf-preview.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, ElementRef, OnDestroy, OnInit, computed, inject, signal, viewChild } from '@angular/core';
import { Component, ElementRef, OnDestroy, OnInit, computed, effect, inject, signal, viewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { AttachmentService } from 'app/lecture/attachment.service';
import { Attachment } from 'app/entities/attachment.model';
Expand Down Expand Up @@ -47,6 +47,7 @@ export class PdfPreviewComponent implements OnInit, OnDestroy {
allPagesSelected = computed(() => this.selectedPages().size === this.totalPages());
initialHiddenPages = signal<Set<number>>(new Set());
hiddenPages = signal<Set<number>>(new Set());
areHiddenPagesChanged = signal<boolean>(false);

// Injected services
private readonly route = inject(ActivatedRoute);
Expand Down Expand Up @@ -103,6 +104,15 @@ export class PdfPreviewComponent implements OnInit, OnDestroy {
this.attachmentUnitSub?.unsubscribe();
}

constructor() {
effect(
() => {
this.hiddenPagesChanged();
},
{ allowSignalWrites: true },
);
}

/**
* Triggers the file input to select files.
*/
Expand All @@ -124,6 +134,20 @@ export class PdfPreviewComponent implements OnInit, OnDestroy {
.filter((id) => id !== null);
}

/**
* Checks if there has been any change between the current set of hidden pages and the new set of hidden pages.
*
* @returns Returns true if the sets differ in size or if any element in `newHiddenPages` is not found in `hiddenPages`, otherwise false.
*/
hiddenPagesChanged() {
if (this.initialHiddenPages()!.size !== this.hiddenPages()!.size) return true;

for (const elem of this.initialHiddenPages()!) {
if (!this.hiddenPages()!.has(elem)) return true;
}
return false;
}

/**
* Updates the existing attachment file or creates a new hidden version of the attachment.
*/
Expand Down

0 comments on commit 815eb60

Please sign in to comment.