@@ -16,7 +16,7 @@ import {
1616 OnInit ,
1717 signal ,
1818} from '@angular/core' ;
19- import { toSignal } from '@angular/core/rxjs-interop' ;
19+ import { takeUntilDestroyed , toSignal } from '@angular/core/rxjs-interop' ;
2020import { ActivatedRoute , Router } from '@angular/router' ;
2121
2222import { MetadataTabsComponent , SubHeaderComponent } from '@osf/shared/components' ;
@@ -49,16 +49,15 @@ import {
4949 UpdateResourceSubjects ,
5050} from '@osf/shared/stores' ;
5151
52+ import { CedarMetadataDataTemplateJsonApi , CedarMetadataRecordData , CedarRecordDataBinding } from './models' ;
5253import {
53- CedarMetadataDataTemplateJsonApi ,
54- CedarMetadataRecord ,
55- CedarMetadataRecordData ,
56- CedarRecordDataBinding ,
57- } from './models' ;
58- import {
54+ CreateCedarMetadataRecord ,
55+ GetCedarMetadataRecords ,
56+ GetCedarMetadataTemplates ,
5957 GetCustomItemMetadata ,
6058 GetResourceMetadata ,
6159 MetadataSelectors ,
60+ UpdateCedarMetadataRecord ,
6261 UpdateCustomItemMetadata ,
6362 UpdateResourceDetails ,
6463 UpdateResourceLicense ,
@@ -131,11 +130,10 @@ export class MetadataComponent implements OnInit {
131130 updateResourceInstitutions : UpdateResourceInstitutions ,
132131 fetchResourceInstitutions : FetchResourceInstitutions ,
133132
134- // getUserInstitutions: GetUserInstitutions,
135- // getCedarRecords: GetCedarMetadataRecords,
136- // getCedarTemplates: GetCedarMetadataTemplates,
137- // createCedarRecord: CreateCedarMetadataRecord,
138- // updateCedarRecord: UpdateCedarMetadataRecord,
133+ getCedarRecords : GetCedarMetadataRecords ,
134+ getCedarTemplates : GetCedarMetadataTemplates ,
135+ createCedarRecord : CreateCedarMetadataRecord ,
136+ updateCedarRecord : UpdateCedarMetadataRecord ,
139137
140138 fetchSubjects : FetchSubjects ,
141139 fetchSelectedSubjects : FetchSelectedSubjects ,
@@ -214,13 +212,9 @@ export class MetadataComponent implements OnInit {
214212 this . actions . getCustomItemMetadata ( this . resourceId ) ;
215213 this . actions . getContributors ( this . resourceId , this . resourceType ( ) ) ;
216214 this . actions . fetchResourceInstitutions ( this . resourceId , this . resourceType ( ) ) ;
217- // this.actions.getCedarRecords(this.resourceId);
218- // this.actions.getCedarTemplates();
215+ this . actions . getCedarRecords ( this . resourceId , this . resourceType ( ) ) ;
216+ this . actions . getCedarTemplates ( ) ;
219217 this . actions . fetchSelectedSubjects ( this . resourceId , this . resourceType ( ) ) ;
220- // const user = this.currentUser();
221- // if (user?.id) {
222- // this.actions.getUserInstitutions(user.id);
223- // }
224218 }
225219 }
226220 onTabChange ( tabId : string | number ) : void {
@@ -259,41 +253,17 @@ export class MetadataComponent implements OnInit {
259253
260254 if ( ! this . resourceId || ! selectedRecord ) return ;
261255
262- const model = {
263- data : {
264- type : 'cedar_metadata_records' as const ,
265- attributes : {
266- metadata : data . data ,
267- is_published : false ,
268- } ,
269- relationships : {
270- template : {
271- data : {
272- type : 'cedar-metadata-templates' as const ,
273- id : data . id ,
274- } ,
275- } ,
276- target : {
277- data : {
278- type : 'nodes' as const ,
279- id : this . resourceId ,
280- } ,
281- } ,
282- } ,
283- } ,
284- } as CedarMetadataRecord ;
285-
286256 if ( selectedRecord . id ) {
287- // this.actions
288- // .updateCedarRecord(model , selectedRecord.id)
289- // .pipe(takeUntilDestroyed(this.destroyRef))
290- // .subscribe({
291- // next: () => {
292- // this.cedarFormReadonly.set(true);
293- // this.toastService.showSuccess('CEDAR record updated successfully');
294- // this.actions.getCedarRecords(projectId );
295- // },
296- // });
257+ this . actions
258+ . updateCedarRecord ( data , selectedRecord . id , this . resourceId , this . resourceType ( ) )
259+ . pipe ( takeUntilDestroyed ( this . destroyRef ) )
260+ . subscribe ( {
261+ next : ( ) => {
262+ this . cedarFormReadonly . set ( true ) ;
263+ this . toastService . showSuccess ( 'CEDAR record updated successfully' ) ;
264+ this . actions . getCedarRecords ( this . resourceId , this . resourceType ( ) ) ;
265+ } ,
266+ } ) ;
297267 }
298268 }
299269
@@ -302,7 +272,7 @@ export class MetadataComponent implements OnInit {
302272 }
303273
304274 openAddRecord ( ) : void {
305- this . router . navigate ( [ 'add' ] , { relativeTo : this . activeRoute } ) ;
275+ this . router . navigate ( [ '../ add' ] , { relativeTo : this . activeRoute } ) ;
306276 }
307277
308278 onTagsChanged ( tags : string [ ] ) : void {
@@ -541,30 +511,31 @@ export class MetadataComponent implements OnInit {
541511 }
542512
543513 private loadCedarRecord ( recordId : string ) : void {
544- // const records = this.cedarRecords();
545- // const templates = this.cedarTemplates();
546- // if (!records) {
547- // return;
548- // }
549- // const record = records.find((r) => r.id === recordId);
550- // if (!record) {
551- // return;
552- // }
553- // this.selectedCedarRecord.set(record);
554- // this.cedarFormReadonly.set(true);
555- // const templateId = record.relationships?.template?.data?.id;
556- // if (templateId && templates?.data) {
557- // const template = templates.data.find((t) => t.id === templateId);
558- // if (template) {
559- // this.selectedCedarTemplate.set(template);
560- // } else {
561- // this.selectedCedarTemplate.set(null);
562- // this.actions.getCedarTemplates();
563- // }
564- // } else {
565- // this.selectedCedarTemplate.set(null);
566- // this.actions.getCedarTemplates();
567- // }
514+ const records = this . cedarRecords ( ) ;
515+ const templates = this . cedarTemplates ( ) ;
516+ if ( ! records ) {
517+ return ;
518+ }
519+ const record = records . find ( ( r ) => r . id === recordId ) ;
520+ if ( ! record ) {
521+ return ;
522+ }
523+ this . selectedCedarRecord . set ( record ) ;
524+ this . cedarFormReadonly . set ( true ) ;
525+ const templateId = record . relationships ?. template ?. data ?. id ;
526+ console . log ( 'templateId:' , templateId ) ;
527+ if ( templateId && templates ?. data ) {
528+ const template = templates . data . find ( ( t ) => t . id === templateId ) ;
529+ if ( template ) {
530+ this . selectedCedarTemplate . set ( template ) ;
531+ } else {
532+ this . selectedCedarTemplate . set ( null ) ;
533+ this . actions . getCedarTemplates ( ) ;
534+ }
535+ } else {
536+ this . selectedCedarTemplate . set ( null ) ;
537+ this . actions . getCedarTemplates ( ) ;
538+ }
568539 }
569540
570541 private handleRouteBasedTabSelection ( ) : void {
0 commit comments