-
Notifications
You must be signed in to change notification settings - Fork 29
feat(es2): Load cedar template by ID instead of via the list endpoint #1022
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: hotfix/26.12.2
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,16 +5,13 @@ import { TranslatePipe } from '@ngx-translate/core'; | |
| import { Button } from 'primeng/button'; | ||
|
|
||
| import { DatePipe } from '@angular/common'; | ||
| import { ChangeDetectionStrategy, Component, computed, effect, inject } from '@angular/core'; | ||
| import { ChangeDetectionStrategy, Component, computed, DestroyRef, effect, inject, signal } from '@angular/core'; | ||
| import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; | ||
| import { Router, RouterLink } from '@angular/router'; | ||
|
|
||
| import { UserSelectors } from '@core/store/user'; | ||
| import { | ||
| GetCedarMetadataRecords, | ||
| GetCedarMetadataTemplates, | ||
| GetCustomItemMetadata, | ||
| MetadataSelectors, | ||
| } from '@osf/features/metadata/store'; | ||
| import { CedarMetadataDataTemplateJsonApi } from '@osf/features/metadata/models'; | ||
| import { GetCedarMetadataRecords, GetCustomItemMetadata, MetadataSelectors } from '@osf/features/metadata/store'; | ||
| import { AffiliatedInstitutionsViewComponent } from '@osf/shared/components/affiliated-institutions-view/affiliated-institutions-view.component'; | ||
| import { ContributorsListComponent } from '@osf/shared/components/contributors-list/contributors-list.component'; | ||
| import { FundersListComponent } from '@osf/shared/components/funders-list/funders-list.component'; | ||
|
|
@@ -27,6 +24,7 @@ import { TruncatedTextComponent } from '@osf/shared/components/truncated-text/tr | |
| import { CurrentResourceType, ResourceType } from '@osf/shared/enums/resource-type.enum'; | ||
| import { LanguageLabelPipe } from '@osf/shared/pipes/language-label.pipe'; | ||
| import { ResourceTypeGeneralLabelPipe } from '@osf/shared/pipes/resource-type-general-label.pipe'; | ||
| import { MetadataService } from '@osf/shared/services/metadata.service'; | ||
| import { CollectionsSelectors, GetProjectSubmissions } from '@osf/shared/stores/collections'; | ||
| import { | ||
| ContributorsSelectors, | ||
|
|
@@ -74,6 +72,8 @@ import { OverviewSupplementsComponent } from '../overview-supplements/overview-s | |
| }) | ||
| export class ProjectOverviewMetadataComponent { | ||
| private readonly router = inject(Router); | ||
| private readonly destroyRef = inject(DestroyRef); | ||
| private readonly metadataService = inject(MetadataService); | ||
|
|
||
| readonly currentProject = select(ProjectOverviewSelectors.getProject); | ||
| readonly isAnonymous = select(ProjectOverviewSelectors.isProjectAnonymous); | ||
|
|
@@ -97,10 +97,11 @@ export class ProjectOverviewMetadataComponent { | |
| readonly isProjectSubmissionsLoading = select(CollectionsSelectors.getCurrentProjectSubmissionsLoading); | ||
| readonly activeFlags = select(UserSelectors.getActiveFlags); | ||
| readonly cedarRecords = select(MetadataSelectors.getCedarRecords); | ||
| private readonly cedarTemplatesResponse = select(MetadataSelectors.getCedarTemplates); | ||
| readonly cedarTemplates = computed(() => this.cedarTemplatesResponse()?.data ?? null); | ||
| readonly isCedarMode = computed(() => this.activeFlags().includes(COLLECTION_SUBMISSION_WITH_CEDAR)); | ||
|
|
||
| private readonly cedarTemplatesMap = signal<Map<string, CedarMetadataDataTemplateJsonApi>>(new Map()); | ||
| readonly cedarTemplates = computed(() => [...this.cedarTemplatesMap().values()]); | ||
|
|
||
| readonly resourceType = CurrentResourceType.Projects; | ||
| readonly dateFormat = 'MMM d, y, h:mm a'; | ||
|
|
||
|
|
@@ -116,14 +117,14 @@ export class ProjectOverviewMetadataComponent { | |
| getBibliographicContributors: GetBibliographicContributors, | ||
| loadMoreBibliographicContributors: LoadMoreBibliographicContributors, | ||
| getCedarRecords: GetCedarMetadataRecords, | ||
| getCedarTemplates: GetCedarMetadataTemplates, | ||
| }); | ||
|
|
||
| constructor() { | ||
| effect(() => { | ||
| const project = this.currentProject(); | ||
|
|
||
| if (project?.id) { | ||
| this.cedarTemplatesMap.set(new Map()); | ||
| this.actions.getBibliographicContributors(project.id, ResourceType.Project); | ||
| this.actions.getInstitutions(project.id); | ||
| this.actions.getIdentifiers(project.id); | ||
|
|
@@ -132,10 +133,28 @@ export class ProjectOverviewMetadataComponent { | |
| this.actions.getProjectSubmissions(project.id); | ||
| this.actions.getLicense(project.licenseId); | ||
| this.actions.getCedarRecords(project.id, ResourceType.Project); | ||
| this.actions.getCedarTemplates(); | ||
| this.actions.getCustomItemMetadata(project.id); | ||
| } | ||
| }); | ||
|
|
||
| effect(() => { | ||
| const records = this.cedarRecords(); | ||
| if (!records?.length) return; | ||
|
|
||
| const currentMap = this.cedarTemplatesMap(); | ||
| const missingIds = [ | ||
| ...new Set(records.map((r) => r.relationships?.template?.data?.id).filter((id): id is string => !!id)), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need |
||
| ].filter((id) => !currentMap.has(id)); | ||
|
|
||
| missingIds.forEach((id) => { | ||
| this.metadataService | ||
| .getMetadataCedarTemplate(id) | ||
| .pipe(takeUntilDestroyed(this.destroyRef)) | ||
| .subscribe((response) => { | ||
| this.cedarTemplatesMap.update((map) => new Map(map).set(id, response.data)); | ||
| }); | ||
| }); | ||
| }); | ||
|
Comment on lines
+149
to
+157
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move cedar template loading into NGXS: dispatch
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also why do we need to get each template by ID instead of via the list?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Also why do we need to get each template by ID instead of via the list?" This is standard procedure when working with the v2 API. If we have an ID for something, then we use the detail endpoint with the ID to get the item. This prevents us from having to request more data than we need for the individual item, and it prevents us from having to iterate through items to find the thing we need. |
||
| } | ||
|
|
||
| onCustomCitationUpdated(citation: string): void { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -6,6 +6,7 @@ import { inject, Injectable } from '@angular/core'; | |||||
| import { ENVIRONMENT } from '@core/provider/environment.provider'; | ||||||
| import { CedarRecordsMapper, MetadataMapper, RorMapper } from '@osf/features/metadata/mappers'; | ||||||
| import { | ||||||
| CedarMetadataDataTemplateJsonApi, | ||||||
| CedarMetadataRecord, | ||||||
| CedarMetadataRecordJsonApi, | ||||||
| CedarMetadataTemplateJsonApi, | ||||||
|
|
@@ -103,6 +104,12 @@ export class MetadataService { | |||||
| ); | ||||||
| } | ||||||
|
|
||||||
| getMetadataCedarTemplate(id: string): Observable<{ data: CedarMetadataDataTemplateJsonApi }> { | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use this interface. |
||||||
| return this.jsonApiService.get<{ data: CedarMetadataDataTemplateJsonApi }>( | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| `${this.apiDomainUrl}/_/cedar_metadata_templates/${id}/` | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| getMetadataCedarRecords( | ||||||
| resourceId: string, | ||||||
| resourceType: ResourceType, | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use one shared action
GetCedarMetadataTemplatesByIdshere and inproject-overview-metadata— same cache in store, different selectors for reading. Move template loading out of the component: dispatch ids, read from store, nosubscribe.