Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,15 @@ <h1 class="mb-4">{{'bitstream-request-a-copy.header' | translate}}</h1>
</a>

<button
[dsBtnDisabled]="requestCopyForm.invalid"
[dsBtnDisabled]="submitting || requestCopyForm.invalid"
class="btn btn-default btn-primary"
(click)="onSubmit()">{{'bitstream-request-a-copy.submit' | translate}}</button>
(click)="onSubmit()">
@if (!submitting) {
{{'bitstream-request-a-copy.submit' | translate}}
} @else {
<i class="fas fa-spinner fa-spin me-1" aria-hidden="true"></i> {{'bitstream-request-a-copy.submitting' | translate}}
}
</button>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ import {
import { Store } from '@ngrx/store';
import { provideMockStore } from '@ngrx/store/testing';
import { TranslateModule } from '@ngx-translate/core';
import { of } from 'rxjs';
import {
NEVER,
of,
} from 'rxjs';

import { BitstreamRequestACopyPageComponent } from './bitstream-request-a-copy-page.component';

Expand Down Expand Up @@ -326,6 +329,33 @@ describe('BitstreamRequestACopyPageComponent', () => {
expect(itemRequestDataService.requestACopy).toHaveBeenCalledWith(itemRequest, 'payload');
expect(notificationsService.error).toHaveBeenCalled();
expect(location.back).not.toHaveBeenCalled();
expect(component.submitting).toBeFalse();
});
});

describe('while the request is in flight', () => {
beforeEach(waitForAsync(() => {
init();
(itemRequestDataService.requestACopy as jasmine.Spy).and.returnValue(NEVER);
initTestbed();
}));
beforeEach(() => {
fixture = TestBed.createComponent(BitstreamRequestACopyPageComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should only submit the request once', () => {
component.name.patchValue('User Name');
component.email.patchValue('user@name.org');
component.allfiles.patchValue('false');
component.message.patchValue('I would like to request a copy');
component.captchaPayload.patchValue('payload');

component.onSubmit();
component.onSubmit();

expect(itemRequestDataService.requestACopy).toHaveBeenCalledTimes(1);
expect(component.submitting).toBeTrue();
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ export class BitstreamRequestACopyPageComponent implements OnInit, OnDestroy {
captchaEnabled$: Observable<boolean>;
challengeHref$: Observable<string>;

/**
* Whether the request is currently being sent; disables the submit button to prevent duplicate requests/emails
*/
submitting = false;

constructor(private location: Location,
private translateService: TranslateService,
private route: ActivatedRoute,
Expand Down Expand Up @@ -242,6 +247,10 @@ export class BitstreamRequestACopyPageComponent implements OnInit, OnDestroy {
* When the submission fails, the user will stay on the page and an error notification will be shown
*/
onSubmit() {
if (this.submitting || this.requestCopyForm.invalid) {
return;
}
this.submitting = true;
const itemRequest = new ItemRequest();
if (hasValue(this.bitstream)) {
itemRequest.bitstreamId = this.bitstream.uuid;
Expand All @@ -260,6 +269,7 @@ export class BitstreamRequestACopyPageComponent implements OnInit, OnDestroy {
this.notificationsService.success(this.translateService.get('bitstream-request-a-copy.submit.success'));
this.navigateBack();
} else {
this.submitting = false;
this.notificationsService.error(this.translateService.get('bitstream-request-a-copy.submit.error'));
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ <h3 class="mb-4">{{'deny-request-copy.header' | translate}}</h3>
@if (itemRequestRD && itemRequestRD.hasSucceeded) {
<div>
<p>{{'deny-request-copy.intro' | translate}}</p>
<ds-email-request-copy [subject]="subject$ | async" [message]="message$ | async" (send)="deny($event)"></ds-email-request-copy>
<ds-email-request-copy [subject]="subject$ | async" [message]="message$ | async" [sending]="sending" (send)="deny($event)"></ds-email-request-copy>
</div>
}
@if (!itemRequestRD || itemRequestRD?.isLoading) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ import {
TranslateModule,
TranslateService,
} from '@ngx-translate/core';
import { of } from 'rxjs';
import {
NEVER,
of,
} from 'rxjs';

import { getMockThemeService } from '../../shared/theme-support/test/theme-service.mock';
import { ThemeService } from '../../shared/theme-support/theme.service';
Expand Down Expand Up @@ -182,6 +185,24 @@ describe('DenyRequestCopyComponent', () => {
it('should not navigate', () => {
expect(router.navigateByUrl).not.toHaveBeenCalled();
});

it('should allow sending again', () => {
expect(component.sending).toBeFalse();
});
});

describe('when the request is still in flight', () => {
beforeEach(() => {
email = new RequestCopyEmail('subject', 'message');
(itemRequestService.deny as jasmine.Spy).and.returnValue(NEVER);
component.deny(email);
component.deny(email);
});

it('should only send the request once', () => {
expect(itemRequestService.deny).toHaveBeenCalledTimes(1);
expect(component.sending).toBeTrue();
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ export class DenyRequestCopyComponent implements OnInit {
*/
message$: Observable<string>;

/**
* Whether the email is currently being sent, used to prevent duplicate submissions
*/
sending = false;

constructor(
private router: Router,
private route: ActivatedRoute,
Expand Down Expand Up @@ -122,6 +127,10 @@ export class DenyRequestCopyComponent implements OnInit {
* @param email Subject and contents of the message to send back to the user requesting the item
*/
deny(email: RequestCopyEmail) {
if (this.sending) {
return;
}
this.sending = true;
this.itemRequestRD$.pipe(
getFirstSucceededRemoteDataPayload(),
switchMap((itemRequest: ItemRequest) => this.itemRequestService.deny(itemRequest.token, email)),
Expand All @@ -131,6 +140,7 @@ export class DenyRequestCopyComponent implements OnInit {
this.notificationsService.success(this.translateService.get('deny-request-copy.success'));
this.router.navigateByUrl('/');
} else {
this.sending = false;
this.notificationsService.error(this.translateService.get('deny-request-copy.error'), rd.errorMessage);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,17 @@
<ng-content></ng-content>
<div class="d-flex flex-row-reverse">
<button (click)="submit()"
[dsBtnDisabled]="!subject || subject.length === 0"
[dsBtnDisabled]="sending || !subject || subject.length === 0"
class="btn btn-primary"
title="{{'grant-deny-request-copy.email.send' | translate }}">
<i class="fas fa-envelope"></i> {{ 'grant-deny-request-copy.email.send' | translate }}
@if (!sending) {
<i class="fas fa-envelope"></i> {{ 'grant-deny-request-copy.email.send' | translate }}
} @else {
<i class="fas fa-spinner fa-spin me-1" aria-hidden="true"></i> {{ 'grant-deny-request-copy.email.sending' | translate }}
}
</button>
<button (click)="return()"
[dsBtnDisabled]="sending"
class="btn btn-outline-secondary me-1"
title="{{'grant-deny-request-copy.email.back' | translate }}">
<i class="fas fa-arrow-left"></i> {{ 'grant-deny-request-copy.email.back' | translate }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,12 @@ describe('EmailRequestCopyComponent', () => {
component.submit();
expect(component.send.emit).toHaveBeenCalledWith(new RequestCopyEmail('test-subject', 'test-message'));
});

it('submit should not emit while sending', () => {
spyOn(component.send, 'emit').and.stub();
component.subject = 'test-subject';
component.sending = true;
component.submit();
expect(component.send.emit).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ export class EmailRequestCopyComponent implements OnInit, OnDestroy {

protected readonly hasValue = hasValue;

/**
* Whether the email is currently being sent; disables the send button to prevent duplicate submissions
*/
@Input() sending = false;

constructor(protected location: Location) {
}

Expand Down Expand Up @@ -109,6 +114,9 @@ export class EmailRequestCopyComponent implements OnInit, OnDestroy {
* Submit the email
*/
submit() {
if (this.sending) {
return;
}
this.send.emit(new RequestCopyEmail(this.subject, this.message));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ export class ThemedEmailRequestCopyComponent extends ThemedComponent<EmailReques
@Input() validAccessPeriods$: Observable<string[]>;


protected inAndOutputNames: (keyof EmailRequestCopyComponent & keyof this)[] = ['send', 'subject', 'message', 'selectedAccessPeriod', 'validAccessPeriods$'];
/**
* Whether the email is currently being sent
*/
@Input() sending: boolean;

protected inAndOutputNames: (keyof EmailRequestCopyComponent & keyof this)[] = ['send', 'subject', 'message', 'selectedAccessPeriod', 'validAccessPeriods$', 'sending'];

protected getComponentName(): string {
return 'EmailRequestCopyComponent';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ <h3 class="mb-4">{{ 'grant-request-copy.header' | translate }}</h3>
<!-- Only send access periods for display if an access token was present -->
<ds-email-request-copy [subject]="subject$ | async"
[message]="message$ | async"
[sending]="sending"
(send)="grant($event)"
(selectedAccessPeriod)="selectAccessPeriod($event)"
[validAccessPeriods$]="validAccessPeriods$"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ import {
TranslateModule,
TranslateService,
} from '@ngx-translate/core';
import { of } from 'rxjs';
import {
NEVER,
of,
} from 'rxjs';

import { getMockThemeService } from '../../shared/theme-support/test/theme-service.mock';
import { ThemeService } from '../../shared/theme-support/theme.service';
Expand Down Expand Up @@ -177,6 +180,24 @@ describe('GrantRequestCopyComponent', () => {
it('should not navigate', () => {
expect(router.navigateByUrl).not.toHaveBeenCalled();
});

it('should allow sending again', () => {
expect(component.sending).toBeFalse();
});
});

describe('when the request is still in flight', () => {
beforeEach(() => {
email = new RequestCopyEmail('subject', 'message');
(itemRequestService.grant as jasmine.Spy).and.returnValue(NEVER);
component.grant(email);
component.grant(email);
});

it('should only send the request once', () => {
expect(itemRequestService.grant).toHaveBeenCalledTimes(1);
expect(component.sending).toBeTrue();
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ export class GrantRequestCopyComponent implements OnInit {
*/
sendAsAttachment = true;

/**
* Whether the email is currently being sent, used to prevent duplicate submissions
*/
sending = false;

/**
* Preview link to be sent to a request applicant
*/
Expand Down Expand Up @@ -153,6 +158,10 @@ export class GrantRequestCopyComponent implements OnInit {
* @param email Subject and contents of the message to send back to the user requesting the item
*/
grant(email: RequestCopyEmail) {
if (this.sending) {
return;
}
this.sending = true;
this.itemRequestRD$.pipe(
getFirstSucceededRemoteDataPayload(),
switchMap((itemRequest: ItemRequest) => this.itemRequestService.grant(itemRequest.token, email, this.suggestOpenAccess, this.accessPeriod)),
Expand All @@ -162,6 +171,7 @@ export class GrantRequestCopyComponent implements OnInit {
this.notificationsService.success(this.translateService.get('grant-request-copy.success'));
this.router.navigateByUrl('/');
} else {
this.sending = false;
this.notificationsService.error(this.translateService.get('grant-request-copy.error'), rd.errorMessage);
}
});
Expand Down
4 changes: 4 additions & 0 deletions src/assets/i18n/en.json5
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,8 @@

"bitstream-request-a-copy.submit": "Request copy",

"bitstream-request-a-copy.submitting": "Requesting copy...",

"bitstream-request-a-copy.submit.success": "The item request was submitted successfully.",

"bitstream-request-a-copy.submit.error": "Something went wrong with submitting the item request.",
Expand Down Expand Up @@ -2303,6 +2305,8 @@

"grant-deny-request-copy.email.send": "Send",

"grant-deny-request-copy.email.sending": "Sending...",

"grant-deny-request-copy.email.subject": "Subject",

"grant-deny-request-copy.email.subject.empty": "Please enter a subject",
Expand Down
Loading