Skip to content

Commit

Permalink
editor: add process data record
Browse files Browse the repository at this point in the history
* Adds postprocessRecordEditor to update data after submit form.
* Adds preCreateRecord to update data before create record.
* Adds preUpdateRecord to update data before update record.

Co-Authored-by: Bertrand Zuchuat <bertrand.zuchuat@rero.ch>
  • Loading branch information
Garfield-fr committed Jan 9, 2020
1 parent 3a5c594 commit 94a955a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
25 changes: 25 additions & 0 deletions projects/ng-core-tester/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ const aggregations = (agg: object) => {
return of(agg);
};

const preProcessDocument = (record: any): any => {
// Update data record before transmit data to the form
return record;
};

const postProcessDocument = (record: any): any => {
// Update data record before transmit to the API
return record;
};

const preCreateDocument = (record: any): any => {
// Update data record before create record
return record;
};

const preUpdateDocument = (record: any): any => {
// Update data record before update record
return record;
};

export function matchedUrl(url: UrlSegment[]) {
const segments = [new UrlSegment(url[0].path, {})];

Expand Down Expand Up @@ -160,6 +180,7 @@ const routes: Routes = [
path: 'record/search',
children: [
{ path: ':type', component: RecordSearchComponent },
{ path: ':type/new', component: EditorComponent },
{ path: ':type/edit/:pid', component: EditorComponent },
{ path: ':type/detail/:pid', component: RecordDetailComponent }
],
Expand All @@ -175,6 +196,10 @@ const routes: Routes = [
aggregationsOrder: aggrDocumentOrder,
aggregationsExpand: aggrDocumentExpand,
aggregationsBucketSize: aggrBucketSize,
preprocessRecordEditor: preProcessDocument,
postprocessRecordEditor: postProcessDocument,
preCreateRecord: preCreateDocument,
preUpdateRecord: preUpdateDocument,
pagination: {
boundaryLinks: true,
maxSize: 5
Expand Down
46 changes: 43 additions & 3 deletions projects/rero/ng-core/src/lib/record/editor/editor.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,45 @@ export class EditorComponent implements OnInit, OnDestroy {
return record;
}

/**
* Postprocess the record before save
* @param record - Record object to postprocess
*/
private postprocessRecord(record: any) {
const config = this.recordUiService.getResourceConfig(this.recordType);

if (config.postprocessRecordEditor) {
return config.postprocessRecordEditor(record);
}
return record;
}

/**
* Pre Create Record
* @param record - Record object
*/
private preCreateRecord(record: any) {
const config = this.recordUiService.getResourceConfig(this.recordType);

if (config.preCreateRecord) {
return config.preCreateRecord(record);
}
return record;
}

/**
* Pre Update Record
* @param record - Record object
*/
private preUpdateRecord(record: any) {
const config = this.recordUiService.getResourceConfig(this.recordType);

if (config.preUpdateRecord) {
return config.preUpdateRecord(record);
}
return record;
}

/**
* Preprocess the record before passing it to the editor
* @param schema - object, JOSNSchema
Expand Down Expand Up @@ -244,9 +283,10 @@ export class EditorComponent implements OnInit, OnDestroy {
* @param event - object, JSON to POST on the backend
*/
submit(event) {
const data = removeEmptyValues(this.model);
let data = removeEmptyValues(this.model);
data = this.postprocessRecord(data);
if (data.pid != null) {
this.recordService.update(this.recordType, data).subscribe((record) => {
this.recordService.update(this.recordType, this.preUpdateRecord(data)).subscribe((record) => {
this.toastrService.success(
this.translateService.instant('Record Updated!'),
this.translateService.instant(this.recordType)
Expand All @@ -255,7 +295,7 @@ export class EditorComponent implements OnInit, OnDestroy {

});
} else {
this.recordService.create(this.recordType, data).subscribe(record => {
this.recordService.create(this.recordType, this.preCreateRecord(data)).subscribe(record => {
this.toastrService.success(
this.translateService.instant('Record Created with pid: ') +
record.metadata.pid,
Expand Down

0 comments on commit 94a955a

Please sign in to comment.