diff --git a/src/app/core/common-components/entity-subrecord/entity-subrecord/entity-subrecord.component.ts b/src/app/core/common-components/entity-subrecord/entity-subrecord/entity-subrecord.component.ts index 267027659e..e7b7b9d541 100644 --- a/src/app/core/common-components/entity-subrecord/entity-subrecord/entity-subrecord.component.ts +++ b/src/app/core/common-components/entity-subrecord/entity-subrecord/entity-subrecord.component.ts @@ -99,7 +99,7 @@ export class EntitySubrecordComponent implements OnChanges { @Input() clickMode: "popup" | "navigate" | "none" = "popup"; - @Output() sendBackData: EventEmitter = new EventEmitter + @Output() selectedData: EventEmitter = new EventEmitter @Input() showInactive = false; @Output() showInactiveChange = new EventEmitter(); @@ -484,17 +484,12 @@ export class EntitySubrecordComponent implements OnChanges { return this.screenWidthObserver.currentScreenSize() >= numericValue; } - selectedRows = [] selectRow (event: any, row:T[]) { - if (event.checked) { - this.selectedRows.push(row); - } else { - const index = this.selectedRows.indexOf(row); - if (index > -1) { - this.selectedRows.splice(index, 1); - } - } - this.sendBackData.emit(this.selectedRows) + const data = { + event: event, + row: row, + }; + this.selectedData.emit(data); } filterActiveInactive() { diff --git a/src/app/core/config/config-fix.ts b/src/app/core/config/config-fix.ts index f6757251dc..17166319ad 100644 --- a/src/app/core/config/config-fix.ts +++ b/src/app/core/config/config-fix.ts @@ -1140,5 +1140,9 @@ export const defaultJsonConfig = { } ] } + }, + + "flashMessage": { + "message": "Please select the row(s) you want to duplicate." } }; diff --git a/src/app/core/duplicate-records/duplicate-records.service.spec.ts b/src/app/core/duplicate-records/duplicate-records.service.spec.ts index 077fc94935..d7e0eb6293 100644 --- a/src/app/core/duplicate-records/duplicate-records.service.spec.ts +++ b/src/app/core/duplicate-records/duplicate-records.service.spec.ts @@ -88,6 +88,5 @@ describe('DuplicateRecordsService', () => { await service.duplicateRecord(originalData, schemaName); expect(transformDataSpy).toHaveBeenCalledWith(originalData, schemaName); expect(saveAllSpy).toHaveBeenCalled(); - }); }); diff --git a/src/app/core/duplicate-records/duplicate-records.service.ts b/src/app/core/duplicate-records/duplicate-records.service.ts index 0dcb695377..d1f64bf70f 100644 --- a/src/app/core/duplicate-records/duplicate-records.service.ts +++ b/src/app/core/duplicate-records/duplicate-records.service.ts @@ -3,6 +3,8 @@ import { EntityMapperService } from '../entity/entity-mapper/entity-mapper.servi import { EntityRegistry } from '../entity/database-entity.decorator'; import { EntitySchemaService } from '../entity/schema/entity-schema.service'; import { Entity } from '../entity/model/entity'; +import { MatSnackBar, MatSnackBarHorizontalPosition, MatSnackBarVerticalPosition } from '@angular/material/snack-bar'; +import { ConfigService } from '../config/config.service'; @Injectable({ providedIn: 'root' @@ -14,27 +16,43 @@ export class DuplicateRecordService { private entitymapperservice: EntityMapperService, private entityTypes: EntityRegistry, private entityService: EntitySchemaService, + private snackBar: MatSnackBar, + private config: ConfigService ) {} - async duplicateRecord(data: any, schemaName: string) { - const duplicateData = this.transformData(data, schemaName); - const results = await this.entitymapperservice.saveAll(duplicateData); - return results; + showFlashMessage(message: string) { + const horizontalPosition: MatSnackBarHorizontalPosition = 'center'; + const verticalPosition: MatSnackBarVerticalPosition = 'top'; + this.snackBar.open(message, 'Close', { + duration: 3000, + horizontalPosition: horizontalPosition, + verticalPosition: verticalPosition, + }); } - transformData(originalData: any, schemaName: string): any { - const data = []; - const copyData = []; + async duplicateRecord(sourceData: any, schemaName: string) { + if (!sourceData || sourceData.length === 0) { + const flashMessage = this.config.getConfig("flashMessage") as { message: string }; + this.showFlashMessage(flashMessage.message); + return; + } + const duplicateData = this.transformData(sourceData, schemaName); + return await this.entitymapperservice.saveAll(duplicateData); + } + + transformData(sourceData: any, schemaName: string): any { + const formattedData = []; + const duplicatedData = []; const entityConstructor = this.entityTypes.get(schemaName); const keys = [...entityConstructor.schema.keys()].filter(key => key !== '_id' && key !== '_rev'); - - originalData.map((item: { record: Entity; })=> { + + sourceData.map((item: { record: Entity; })=> { const dbEntity = this.entityService.transformEntityToDatabaseFormat(item.record); const entityformat = this.entityService.transformDatabaseToEntityFormat(dbEntity, entityConstructor.schema); - data.push(entityformat); + formattedData.push(entityformat); }) - - data.forEach((item)=> { + + formattedData.forEach((item)=> { const entity = new entityConstructor(); for (const key of keys) { if (key === 'name' || key === 'title' || key === 'subject') { @@ -42,9 +60,9 @@ export class DuplicateRecordService { } entity[key] = item[key]; } - copyData.push(entity); + duplicatedData.push(entity); }) - return copyData ; + return duplicatedData; } } diff --git a/src/app/core/entity-list/entity-list/entity-list.component.html b/src/app/core/entity-list/entity-list/entity-list.component.html index 6af795ada7..1dff5c3b22 100644 --- a/src/app/core/entity-list/entity-list/entity-list.component.html +++ b/src/app/core/entity-list/entity-list/entity-list.component.html @@ -128,7 +128,7 @@

{{ listName }}

[isLoading]="isLoading" [filter]="filterObj" [defaultSort]="listConfig?.defaultSort" - (sendBackData)="getDatafromsubRecord($event)" + (selectedData)="setSelectedRows($event)" [showInactive]="showInactive" > @@ -204,9 +204,9 @@

{{ listName }}