Skip to content

Commit 7db949f

Browse files
feat(metadata): bug fixing, refactoring
1 parent 3fe169c commit 7db949f

16 files changed

Lines changed: 185 additions & 207 deletions

src/app/features/metadata/mappers/metadata.mapper.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ContributorsMapper, InstitutionsMapper, LicensesMapper } from '@osf/shared/mappers';
22

3-
import { CustomItemMetadataRecord, CustomMetadataJsonApiResponse, Metadata, MetadataJsonApi } from '../models';
3+
import { CustomItemMetadataRecord, CustomMetadataJsonApi, Metadata, MetadataJsonApi } from '../models';
44

55
export class MetadataMapper {
66
static fromMetadataApiResponse(response: MetadataJsonApi): Metadata {
@@ -31,11 +31,11 @@ export class MetadataMapper {
3131
};
3232
}
3333

34-
static fromCustomMetadataApiResponse(response: CustomMetadataJsonApiResponse): Partial<CustomItemMetadataRecord> {
34+
static fromCustomMetadataApiResponse(response: CustomMetadataJsonApi): Partial<CustomItemMetadataRecord> {
3535
return {
36-
language: response.data.attributes.language,
37-
resourceTypeGeneral: response.data.attributes.resource_type_general,
38-
funders: response.data.attributes.funders?.map((funder) => ({
36+
language: response.attributes.language,
37+
resourceTypeGeneral: response.attributes.resource_type_general,
38+
funders: response.attributes.funders?.map((funder) => ({
3939
funderName: funder.funder_name,
4040
funderIdentifier: funder.funder_identifier,
4141
funderIdentifierType: funder.funder_identifier_type,
@@ -45,6 +45,29 @@ export class MetadataMapper {
4545
})),
4646
};
4747
}
48+
49+
static toCustomMetadataApiRequest(id: string, metadata: Partial<CustomItemMetadataRecord>) {
50+
console.log('toCustomMetadataApiRequest', { id, metadata });
51+
return {
52+
data: {
53+
type: 'custom-item-metadata-records',
54+
id,
55+
attributes: {
56+
language: metadata.language,
57+
resource_type_general: metadata.resourceTypeGeneral,
58+
funders: metadata.funders?.map((funder) => ({
59+
funder_name: funder.funderName,
60+
funder_identifier: funder.funderIdentifier,
61+
funder_identifier_type: funder.funderIdentifierType,
62+
award_number: funder.awardNumber,
63+
award_uri: funder.awardUri,
64+
award_title: funder.awardTitle,
65+
})),
66+
},
67+
},
68+
};
69+
}
70+
4871
// static fromMetadataApiResponse(response: Record<string, unknown>): ProjectOverview {
4972
// const attributes = response['attributes'] as Record<string, unknown>;
5073
// const embeds = response['embeds'] as Record<string, unknown>;

src/app/features/metadata/metadata.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[title]="'project.overview.metadata.title' | translate"
77
/>
88
<osf-metadata-tabs
9-
[loading]="isMetadataLoading()"
9+
[loading]="isLoading() || isSubmitting()"
1010
[tabs]="tabs()"
1111
[selectedTab]="selectedTab()"
1212
[selectedCedarTemplate]="selectedCedarTemplate()!"

src/app/features/metadata/metadata.component.ts

Lines changed: 70 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import { CedarTemplateFormComponent } from '@osf/shared/components/shared-metada
1414
import {
1515
ContributorsDialogComponent,
1616
DescriptionDialogComponent,
17+
FundingDialogComponent,
1718
LicenseDialogComponent,
19+
ResourceInformationDialogComponent,
1820
} from '@osf/shared/components/shared-metadata/dialogs';
1921
import { SharedMetadataComponent } from '@osf/shared/components/shared-metadata/shared-metadata.component';
2022
import { MetadataResourceEnum, ResourceType } from '@osf/shared/enums';
@@ -40,6 +42,7 @@ import {
4042
GetCustomItemMetadata,
4143
GetResourceMetadata,
4244
MetadataSelectors,
45+
UpdateCustomItemMetadata,
4346
UpdateResourceDetails,
4447
UpdateResourceLicense,
4548
} from './store';
@@ -73,32 +76,31 @@ export class MetadataComponent implements OnInit {
7376
private resourceId = '';
7477

7578
tabs = signal<MetadataTabsModel[]>([]);
76-
protected readonly selectedTab = signal('osf');
79+
readonly selectedTab = signal('osf');
7780

7881
selectedCedarRecord = signal<CedarMetadataRecordData | null>(null);
7982
selectedCedarTemplate = signal<CedarMetadataDataTemplateJsonApi | null>(null);
8083
cedarFormReadonly = signal<boolean>(true);
81-
protected metadata = select(MetadataSelectors.getResourceMetadata);
82-
protected isMetadataLoading = select(MetadataSelectors.getLoading);
83-
protected customItemMetadata = select(MetadataSelectors.getCustomItemMetadata);
84-
protected fundersList = select(MetadataSelectors.getFundersList);
85-
protected contributors = select(ContributorsSelectors.getContributors);
86-
protected isContributorsLoading = select(ContributorsSelectors.isContributorsLoading);
87-
protected cedarRecords = select(MetadataSelectors.getCedarRecords);
88-
protected cedarTemplates = select(MetadataSelectors.getCedarTemplates);
89-
protected selectedSubjects = select(SubjectsSelectors.getSelectedSubjects);
90-
protected isSubjectsUpdating = select(SubjectsSelectors.areSelectedSubjectsLoading);
84+
metadata = select(MetadataSelectors.getResourceMetadata);
85+
isLoading = select(MetadataSelectors.getLoading);
86+
customItemMetadata = select(MetadataSelectors.getCustomItemMetadata);
87+
contributors = select(ContributorsSelectors.getContributors);
88+
isContributorsLoading = select(ContributorsSelectors.isContributorsLoading);
89+
cedarRecords = select(MetadataSelectors.getCedarRecords);
90+
cedarTemplates = select(MetadataSelectors.getCedarTemplates);
91+
selectedSubjects = select(SubjectsSelectors.getSelectedSubjects);
92+
isSubjectsUpdating = select(SubjectsSelectors.areSelectedSubjectsLoading);
9193
resourceType = signal<ResourceType>(this.activeRoute.parent?.snapshot.data['resourceType'] || ResourceType.Project);
94+
isSubmitting = select(MetadataSelectors.getSubmitting);
9295

9396
provider = environment.defaultProvider;
9497

95-
protected actions = createDispatchMap({
98+
actions = createDispatchMap({
9699
getResourceMetadata: GetResourceMetadata,
97100
updateMetadata: UpdateResourceDetails,
98101
updateResourceLicense: UpdateResourceLicense,
99102
getCustomItemMetadata: GetCustomItemMetadata,
100-
// updateCustomItemMetadata: UpdateCustomItemMetadata,
101-
// getFundersList: GetFundersList,
103+
updateCustomItemMetadata: UpdateCustomItemMetadata,
102104
getContributors: GetAllContributors,
103105
// getUserInstitutions: GetUserInstitutions,
104106
// getCedarRecords: GetCedarMetadataRecords,
@@ -145,6 +147,7 @@ export class MetadataComponent implements OnInit {
145147
});
146148

147149
effect(() => {
150+
console.log('customItemMetadata:', this.customItemMetadata());
148151
const metadata = this.metadata();
149152
if (this.resourceType() === ResourceType.Registration) {
150153
if (metadata) {
@@ -259,7 +262,6 @@ export class MetadataComponent implements OnInit {
259262
}
260263

261264
onTagsChanged(tags: string[]): void {
262-
console.log('Tags changed:', tags);
263265
this.actions.updateMetadata(this.resourceId, this.resourceType(), { tags });
264266
}
265267

@@ -316,39 +318,32 @@ export class MetadataComponent implements OnInit {
316318
}
317319

318320
openEditResourceInformationDialog(): void {
319-
// const dialogRef = this.dialogService.open(ResourceInformationDialogComponent, {
320-
// header: this.translateService.instant('project.metadata.resourceInformation.dialog.header'),
321-
// width: '500px',
322-
// focusOnShow: false,
323-
// closeOnEscape: true,
324-
// modal: true,
325-
// closable: true,
326-
// data: {
327-
// currentProject: this.currentProject(),
328-
// customItemMetadata: this.customItemMetadata(),
329-
// },
330-
// });
331-
// dialogRef.onClose
332-
// .pipe(
333-
// filter((result) => !!result && (result.resourceType || result.resourceLanguage)),
334-
// switchMap((result) => {
335-
// const projectId = this.currentProject()?.id;
336-
// if (projectId) {
337-
// const currentMetadata = this.customItemMetadata();
338-
// const updatedMetadata = {
339-
// ...currentMetadata,
340-
// language: result.resourceLanguage || currentMetadata?.language,
341-
// resource_type_general: result.resourceType || currentMetadata?.resource_type_general,
342-
// funder: currentMetadata?.funders,
343-
// };
344-
// return this.actions.updateCustomItemMetadata(projectId, updatedMetadata);
345-
// }
346-
// return EMPTY;
347-
// })
348-
// )
349-
// .subscribe({
350-
// next: () => this.toastService.showSuccess('project.metadata.resourceInformation.updated'),
351-
// });
321+
const currentCustomMetadata = this.customItemMetadata();
322+
const dialogRef = this.dialogService.open(ResourceInformationDialogComponent, {
323+
header: this.translateService.instant('project.metadata.resourceInformation.dialog.header'),
324+
width: '500px',
325+
focusOnShow: false,
326+
closeOnEscape: true,
327+
modal: true,
328+
closable: true,
329+
data: {
330+
customItemMetadata: currentCustomMetadata,
331+
},
332+
});
333+
dialogRef.onClose
334+
.pipe(
335+
filter((result) => !!result && (result.resourceTypeGeneral || result.language)),
336+
switchMap((result) => {
337+
const updatedMetadata = {
338+
...currentCustomMetadata,
339+
...result,
340+
};
341+
return this.actions.updateCustomItemMetadata(this.resourceId, updatedMetadata);
342+
})
343+
)
344+
.subscribe({
345+
next: () => this.toastService.showSuccess('project.metadata.resourceInformation.updated'),
346+
});
352347
}
353348

354349
openEditLicenseDialog(): void {
@@ -381,57 +376,33 @@ export class MetadataComponent implements OnInit {
381376
}
382377

383378
openEditFundingDialog(): void {
384-
// this.actions.getFundersList();
385-
// const dialogRef = this.dialogService.open(FundingDialogComponent, {
386-
// header: this.translateService.instant('project.metadata.funding.dialog.header'),
387-
// width: '600px',
388-
// focusOnShow: false,
389-
// closeOnEscape: true,
390-
// modal: true,
391-
// closable: true,
392-
// data: {
393-
// currentProject: this.currentProject(),
394-
// },
395-
// });
396-
// dialogRef.onClose
397-
// .pipe(
398-
// filter((result) => !!result && result.fundingEntries),
399-
// switchMap((result) => {
400-
// const projectId = this.currentProject()?.id;
401-
// if (projectId) {
402-
// const currentMetadata = this.customItemMetadata() || {
403-
// language: 'en',
404-
// resource_type_general: 'Dataset',
405-
// funders: [],
406-
// };
407-
// const updatedMetadata = {
408-
// ...currentMetadata,
409-
// funders: result.fundingEntries.map(
410-
// (entry: {
411-
// funderName?: string;
412-
// funderIdentifier?: string;
413-
// funderIdentifierType?: string;
414-
// awardNumber?: string;
415-
// awardUri?: string;
416-
// awardTitle?: string;
417-
// }) => ({
418-
// funder_name: entry.funderName || '',
419-
// funder_identifier: entry.funderIdentifier || '',
420-
// funder_identifier_type: entry.funderIdentifierType || '',
421-
// award_number: entry.awardNumber || '',
422-
// award_uri: entry.awardUri || '',
423-
// award_title: entry.awardTitle || '',
424-
// })
425-
// ),
426-
// };
427-
// return this.actions.updateCustomItemMetadata(projectId, updatedMetadata);
428-
// }
429-
// return EMPTY;
430-
// })
431-
// )
432-
// .subscribe({
433-
// next: () => this.toastService.showSuccess('project.metadata.funding.updated'),
434-
// });
379+
const currentCustomMetadata = this.customItemMetadata();
380+
381+
const dialogRef = this.dialogService.open(FundingDialogComponent, {
382+
header: this.translateService.instant('project.metadata.funding.dialog.header'),
383+
width: '600px',
384+
focusOnShow: false,
385+
closeOnEscape: true,
386+
modal: true,
387+
closable: true,
388+
data: {
389+
funders: currentCustomMetadata?.funders || [],
390+
},
391+
});
392+
dialogRef.onClose
393+
.pipe(
394+
filter((result) => !!result && result.fundingEntries),
395+
switchMap((result) => {
396+
const updatedMetadata = {
397+
...currentCustomMetadata,
398+
funders: result.fundingEntries,
399+
};
400+
return this.actions.updateCustomItemMetadata(this.resourceId, updatedMetadata);
401+
})
402+
)
403+
.subscribe({
404+
next: () => this.toastService.showSuccess('project.metadata.funding.updated'),
405+
});
435406
}
436407

437408
openEditAffiliatedInstitutionsDialog(): void {

src/app/features/metadata/models/funding-dialog.model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export interface FunderOption {
2323
}
2424

2525
export interface FundingDialogResult {
26-
fundingEntries: Funder;
26+
fundingEntries: Funder[];
2727
resourceId?: string;
2828
}
2929

src/app/features/metadata/services/metadata.service.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
CedarMetadataRecord,
1313
CedarMetadataRecordJsonApi,
1414
CedarMetadataTemplateJsonApi,
15+
CustomMetadataJsonApi,
1516
CustomMetadataJsonApiResponse,
1617
MetadataAttributesJsonApi,
1718
MetadataJsonApi,
@@ -35,17 +36,14 @@ export class MetadataService {
3536
getCustomItemMetadata(guid: string): Observable<CustomItemMetadataRecord> {
3637
return this.jsonApiService
3738
.get<CustomMetadataJsonApiResponse>(`${this.apiUrl}/custom_item_metadata_records/${guid}/`)
38-
.pipe(map((response) => MetadataMapper.fromCustomMetadataApiResponse(response)));
39+
.pipe(map((response) => MetadataMapper.fromCustomMetadataApiResponse(response.data)));
3940
}
4041

4142
updateCustomItemMetadata(guid: string, metadata: CustomItemMetadataRecord): Observable<CustomItemMetadataRecord> {
43+
const payload = MetadataMapper.toCustomMetadataApiRequest(guid, metadata);
44+
4245
return this.jsonApiService
43-
.put<CustomMetadataJsonApiResponse>(`${this.apiUrl}/custom_item_metadata_records/${guid}/`, {
44-
data: {
45-
type: 'custom-item-metadata-records',
46-
attributes: metadata,
47-
},
48-
})
46+
.put<CustomMetadataJsonApi>(`${this.apiUrl}/custom_item_metadata_records/${guid}/`, payload)
4947
.pipe(map((response) => MetadataMapper.fromCustomMetadataApiResponse(response)));
5048
}
5149

src/app/features/metadata/store/metadata.selectors.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ export class MetadataSelectors {
1919
return state.metadata?.isLoading || state.customMetadata?.isLoading || false;
2020
}
2121

22+
@Selector([MetadataState])
23+
static getSubmitting(state: MetadataStateModel) {
24+
return state.metadata?.isSubmitting || state.customMetadata?.isSubmitting || false;
25+
}
26+
2227
@Selector([MetadataState])
2328
static getError(state: MetadataStateModel) {
2429
return state.metadata?.error ?? null;

0 commit comments

Comments
 (0)