Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ <h2 class="mb-2">{{ 'shared.license.title' | translate }}</h2>
</p>
<osf-license
[licenses]="licenses()"
[selectedLicenseId]="createdPreprint()?.licenseId"
[selectedLicenseId]="createdPreprint()?.licenseId || defaultLicense()"
[selectedLicenseOptions]="createdPreprint()?.licenseOptions"
[fullWidthSelect]="true"
(createLicense)="createLicense($event)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { InputText } from 'primeng/inputtext';
import { Message } from 'primeng/message';
import { Tooltip } from 'primeng/tooltip';

import { ChangeDetectionStrategy, Component, inject, input, OnInit, output } from '@angular/core';
import { ChangeDetectionStrategy, Component, effect, inject, input, OnInit, output, signal } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';

import { formInputLimits } from '@osf/features/preprints/constants';
Expand Down Expand Up @@ -81,6 +81,24 @@ export class PreprintsMetadataStepComponent implements OnInit {
provider = input.required<PreprintProviderDetails | undefined>();
nextClicked = output<void>();
backClicked = output<void>();
defaultLicense = signal<string | undefined>(undefined);

constructor() {
effect(() => {
const licenses = this.licenses();
const preprint = this.createdPreprint();

if (licenses.length && preprint && !preprint.licenseId && preprint.defaultLicenseId) {
const defaultLicense = licenses.find((license) => license.id === preprint?.defaultLicenseId);
if (defaultLicense) {
this.defaultLicense.set(defaultLicense.id);
if (!defaultLicense.requiredFields.length) {
this.actions.saveLicense(defaultLicense.id);
}
}
}
});
}

ngOnInit() {
this.actions.fetchLicenses();
Expand Down
1 change: 1 addition & 0 deletions src/app/features/preprints/mappers/preprints.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export class PreprintsMapper {
articleDoiLink: response.links.doi,
embeddedLicense: null,
providerId: response.relationships?.provider?.data?.id,
defaultLicenseId: response.attributes.default_license_id,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface PreprintAttributesJsonApi {
why_no_prereg: StringOrNull;
prereg_links: string[];
prereg_link_info: PreregLinkInfo | null;
default_license_id: string;
}

export interface PreprintRelationshipsJsonApi {
Expand Down
1 change: 1 addition & 0 deletions src/app/features/preprints/models/preprint.models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface PreprintModel {
articleDoiLink?: string;
identifiers?: IdentifierModel[];
providerId: string;
defaultLicenseId?: string;
}

export interface PreprintFilesLinks {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ export class PreprintStepperState {
if (action.payload.isPublished) {
ctx.setState(patch({ hasBeenSubmitted: true }));
}

ctx.setState(patch({ preprint: patch({ isSubmitting: false, data: preprint }) }));
}),
catchError((error) => handleSectionError(ctx, 'preprint', error))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ <h2 class="mb-2">{{ 'shared.license.title' | translate }}</h2>
<osf-license
[licenses]="licenses()"
[fullWidthSelect]="true"
[selectedLicenseId]="selectedLicense()?.id"
[selectedLicenseId]="selectedLicense()?.id || control().get('id')?.value"
[selectedLicenseOptions]="selectedLicense()?.options"
(createLicense)="createLicense($event)"
(selectLicense)="selectLicense($event)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TranslatePipe } from '@ngx-translate/core';
import { Card } from 'primeng/card';
import { Message } from 'primeng/message';

import { ChangeDetectionStrategy, Component, effect, inject, input, untracked } from '@angular/core';
import { ChangeDetectionStrategy, Component, effect, inject, input } from '@angular/core';
import { FormGroup, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { ActivatedRoute } from '@angular/router';

Expand Down Expand Up @@ -64,18 +64,29 @@ export class RegistriesLicenseComponent {

effect(() => {
const licenses = this.licenses();
const selectedLicense = untracked(() => this.selectedLicense());
const selectedLicense = this.selectedLicense();
const defaultLicenseId = this.draftRegistration()?.defaultLicenseId;

if (!licenses.length || !selectedLicense) {
if (!licenses.length) {
return;
}

if (!licenses.find((license) => license.id === selectedLicense.id)) {
this.control().patchValue({
id: null,
});
this.control().markAsTouched();
this.control().updateValueAndValidity();
if (
defaultLicenseId &&
(!selectedLicense?.id || !licenses.find((license) => license.id === selectedLicense?.id))
) {
const defaultLicense = licenses.find((license) => license.id === defaultLicenseId);
if (defaultLicense) {
this.control().patchValue({
id: defaultLicense.id,
});
this.control().markAsTouched();
this.control().updateValueAndValidity();

if (!defaultLicense.requiredFields.length) {
this.actions.saveLicense(this.draftId, defaultLicense.id);
}
}
}
});
}
Expand Down
1 change: 1 addition & 0 deletions src/app/shared/mappers/registration/registration.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class RegistrationMapper {
},
providerId: response.relationships.provider?.data?.id || '',
hasProject: !!response.attributes.has_project,
defaultLicenseId: response.attributes?.default_license_id,
components: [],
currentUserPermissions: response.attributes.current_user_permissions,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface DraftRegistrationModel {
branchedFrom?: Partial<ProjectModel>;
providerId: string;
hasProject: boolean;
defaultLicenseId?: string;
components: Partial<ProjectModel>[];
currentUserPermissions: UserPermissions[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface DraftRegistrationAttributesJsonApi {
datetime_updated: string;
description: string;
has_project: boolean;
default_license_id?: string;
node_license: LicenseRecordJsonApi;
registration_metadata: Record<string, unknown>;
registration_responses: Record<string, unknown>;
Expand Down