Skip to content

Commit 1ac8301

Browse files
committed
fix(resources): fixed some code
1 parent 0c1ccfe commit 1ac8301

File tree

6 files changed

+37
-55
lines changed

6 files changed

+37
-55
lines changed

src/app/features/moderation/models/moderator-json-api.model.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,4 @@ export interface ModeratorAddRequestModel {
2727
full_name?: string;
2828
email?: string;
2929
};
30-
// relationships: {
31-
// users?: {
32-
// data?: RelationshipUsersData;
33-
// };
34-
// };
35-
}
36-
37-
interface RelationshipUsersData {
38-
id?: string;
39-
type?: 'users';
4030
}

src/app/features/registry/components/add-resource-dialog/add-resource-dialog.component.ts

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,23 @@ import { DynamicDialogConfig, DynamicDialogRef } from 'primeng/dynamicdialog';
88
import { finalize, take } from 'rxjs';
99

1010
import { ChangeDetectionStrategy, Component, inject, signal } from '@angular/core';
11-
import {
12-
AbstractControl,
13-
FormControl,
14-
FormGroup,
15-
ReactiveFormsModule,
16-
ValidationErrors,
17-
ValidatorFn,
18-
Validators,
19-
} from '@angular/forms';
20-
21-
import { ResourceFormComponent } from '@osf/features/registry/components';
22-
import { resourceTypeOptions } from '@osf/features/registry/constants';
23-
import { AddResource } from '@osf/features/registry/models/resources/add-resource.model';
24-
import { ConfirmAddResource } from '@osf/features/registry/models/resources/confirm-add-resource.model';
11+
import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
12+
13+
import { LoadingSpinnerComponent } from '@osf/shared/components';
14+
import { InputLimits } from '@osf/shared/constants';
15+
import { RegistryResourceType } from '@osf/shared/enums';
16+
import { SelectOption } from '@osf/shared/models';
17+
import { CustomValidators } from '@osf/shared/utils';
18+
19+
import { resourceTypeOptions } from '../../constants';
20+
import { AddResource, ConfirmAddResource } from '../../models';
2521
import {
2622
ConfirmAddRegistryResource,
2723
PreviewRegistryResource,
2824
RegistryResourcesSelectors,
2925
SilentDelete,
30-
} from '@osf/features/registry/store/registry-resources';
31-
import { LoadingSpinnerComponent } from '@shared/components';
32-
import { InputLimits } from '@shared/constants';
33-
import { RegistryResourceType } from '@shared/enums';
34-
import { SelectOption } from '@shared/models';
35-
36-
export const doiValidator: ValidatorFn = (control: AbstractControl): ValidationErrors | null => {
37-
const value = control.value;
38-
if (!value) return null;
39-
40-
const DOIRegex = /\b(10\.\d{4,}(?:\.\d+)*\/\S+(?:(?!["&'<>])\S))\b/; // value for example: 10.1234/abcd1234 or https://doi.org/10.1234/abcd1234
41-
const isValid = DOIRegex.test(value);
42-
return isValid ? null : { invalidDoi: true };
43-
};
26+
} from '../../store/registry-resources';
27+
import { ResourceFormComponent } from '../resource-form/resource-form.component';
4428

4529
@Component({
4630
selector: 'osf-add-resource-dialog',
@@ -62,7 +46,7 @@ export class AddResourceDialogComponent {
6246
protected isResourceConfirming = signal(false);
6347

6448
protected form = new FormGroup({
65-
pid: new FormControl<string | null>('', [Validators.required, doiValidator]),
49+
pid: new FormControl<string | null>('', [CustomValidators.requiredTrimmed(), CustomValidators.doiValidator]),
6650
resourceType: new FormControl<string | null>('', [Validators.required]),
6751
description: new FormControl<string | null>(''),
6852
});

src/app/features/registry/components/edit-resource-dialog/edit-resource-dialog.component.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import { finalize, take } from 'rxjs';
99
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
1010
import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
1111

12-
import { doiValidator } from '@osf/features/registry/components';
13-
import { ResourceFormComponent } from '@osf/features/registry/components/resource-form/resource-form.component';
14-
import { RegistryResource } from '@osf/features/registry/models';
15-
import { AddResource } from '@osf/features/registry/models/resources/add-resource.model';
16-
import { RegistryResourcesSelectors, UpdateResource } from '@osf/features/registry/store/registry-resources';
17-
import { LoadingSpinnerComponent } from '@shared/components';
12+
import { LoadingSpinnerComponent } from '@osf/shared/components';
13+
import { CustomValidators } from '@osf/shared/utils';
14+
15+
import { AddResource, RegistryResource } from '../../models';
16+
import { RegistryResourcesSelectors, UpdateResource } from '../../store/registry-resources';
17+
import { ResourceFormComponent } from '../resource-form/resource-form.component';
1818

1919
@Component({
2020
selector: 'osf-edit-resource-dialog',
@@ -33,7 +33,7 @@ export class EditResourceDialogComponent {
3333
private resource: RegistryResource = this.dialogConfig.data.resource as RegistryResource;
3434

3535
protected form = new FormGroup({
36-
pid: new FormControl<string | null>('', [Validators.required, doiValidator]),
36+
pid: new FormControl<string | null>('', [CustomValidators.requiredTrimmed(), CustomValidators.doiValidator]),
3737
resourceType: new FormControl<string | null>('', [Validators.required]),
3838
description: new FormControl<string | null>(''),
3939
});

src/app/features/registry/components/resource-form/resource-form.component.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
(click)="handleCancel()"
3333
/>
3434
}
35-
<p-button class="btn-full-width" [label]="primaryButtonLabel() | translate" (onClick)="handleSubmit()" />
35+
<p-button
36+
class="btn-full-width"
37+
[label]="primaryButtonLabel() | translate"
38+
(onClick)="handleSubmit()"
39+
[disabled]="formGroup().invalid"
40+
/>
3641
</div>
3742
</form>

src/app/features/registry/pages/registry-metadata-add/registry-metadata-add.component.spec.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { provideStore, Store } from '@ngxs/store';
33
import { TranslatePipe } from '@ngx-translate/core';
44
import { MockComponent, MockPipe } from 'ng-mocks';
55

6-
import { of, throwError } from 'rxjs';
6+
import { of } from 'rxjs';
77

88
import { provideHttpClient } from '@angular/common/http';
99
import { provideHttpClientTesting } from '@angular/common/http/testing';
@@ -400,8 +400,7 @@ describe('RegistryMetadataAddComponent', () => {
400400
});
401401

402402
it('should successfully create cedar record', () => {
403-
const dispatchSpy = jest.spyOn(store, 'dispatch').mockReturnValue(of({}));
404-
const routerSpy = jest.spyOn(router, 'navigate');
403+
const dispatchSpy = jest.spyOn(store, 'dispatch').mockReturnValue(of());
405404

406405
store.reset({
407406
registryMetadata: {
@@ -422,7 +421,6 @@ describe('RegistryMetadataAddComponent', () => {
422421

423422
it('should handle submission success', (done) => {
424423
const routerSpy = jest.spyOn(router, 'navigate');
425-
const dispatchSpy = jest.spyOn(store, 'dispatch').mockReturnValue(of({}));
426424

427425
store.reset({
428426
registryMetadata: {
@@ -436,7 +434,6 @@ describe('RegistryMetadataAddComponent', () => {
436434

437435
component.onSubmit(mockSubmissionData);
438436

439-
// Use setTimeout to allow the subscription to complete
440437
setTimeout(() => {
441438
expect(component.isSubmitting()).toBe(false);
442439
expect(toastService.showSuccess).toHaveBeenCalledWith(
@@ -450,11 +447,8 @@ describe('RegistryMetadataAddComponent', () => {
450447
});
451448

452449
it('should handle submission error', (done) => {
453-
const dispatchSpy = jest.spyOn(store, 'dispatch').mockReturnValue(throwError(() => new Error('Test error')));
454-
455450
component.onSubmit(mockSubmissionData);
456451

457-
// Use setTimeout to allow the subscription to complete
458452
setTimeout(() => {
459453
expect(component.isSubmitting()).toBe(false);
460454
expect(toastService.showError).toHaveBeenCalledWith('project.overview.metadata.failedToCreateCedarRecord');

src/app/shared/utils/custom-form-validators.helper.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,13 @@ export class CustomValidators {
6262

6363
return endDate > startDate ? null : { dateRangeInvalid: true };
6464
};
65+
66+
static doiValidator: ValidatorFn = (control: AbstractControl): ValidationErrors | null => {
67+
const value = control.value;
68+
if (!value) return null;
69+
70+
const DOIRegex = /\b(10\.\d{4,}(?:\.\d+)*\/\S+(?:(?!["&'<>])\S))\b/;
71+
const isValid = DOIRegex.test(value);
72+
return isValid ? null : { invalidDoi: true };
73+
};
6574
}

0 commit comments

Comments
 (0)