Skip to content

Commit

Permalink
#1362 added flash message for select row
Browse files Browse the repository at this point in the history
  • Loading branch information
Brajesh Kumar authored and Brajesh Kumar committed Nov 2, 2023
1 parent 653f4af commit 741692b
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class EntitySubrecordComponent<T extends Entity> implements OnChanges {

@Input() clickMode: "popup" | "navigate" | "none" = "popup";

@Output() sendBackData: EventEmitter<any[]> = new EventEmitter<any[]>
@Output() selectedData: EventEmitter<any> = new EventEmitter<any>

@Input() showInactive = false;
@Output() showInactiveChange = new EventEmitter<boolean>();
Expand Down Expand Up @@ -484,17 +484,12 @@ export class EntitySubrecordComponent<T extends Entity> 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() {
Expand Down
4 changes: 4 additions & 0 deletions src/app/core/config/config-fix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1140,5 +1140,9 @@ export const defaultJsonConfig = {
}
]
}
},

"flashMessage": {
"message": "Please select the row(s) you want to duplicate."
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,5 @@ describe('DuplicateRecordsService', () => {
await service.duplicateRecord(originalData, schemaName);
expect(transformDataSpy).toHaveBeenCalledWith(originalData, schemaName);
expect(saveAllSpy).toHaveBeenCalled();

});
});
46 changes: 32 additions & 14 deletions src/app/core/duplicate-records/duplicate-records.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -14,37 +16,53 @@ 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') {
item[key] = `Copy of ${item[key]}`;
}
entity[key] = item[key];
}
copyData.push(entity);
duplicatedData.push(entity);
})

return copyData ;
return duplicatedData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ <h2>{{ listName }}</h2>
[isLoading]="isLoading"
[filter]="filterObj"
[defaultSort]="listConfig?.defaultSort"
(sendBackData)="getDatafromsubRecord($event)"
(selectedData)="setSelectedRows($event)"
[showInactive]="showInactive"
></app-entity-subrecord>
</ng-template>
Expand Down Expand Up @@ -204,9 +204,9 @@ <h2>{{ listName }}</h2>
</button>
<button
mat-menu-item
[appDuplicateRecords]="duplicatesdata"
[appDuplicateRecords]="selectedRows"
angulartics2On="click"
(click)="clearDuplicatesData()"
(click)="clearSelectedRows()"
[entityType]="entityConstructor?.ENTITY_TYPE"
angularticsAction="duplicates records"
>
Expand Down
18 changes: 13 additions & 5 deletions src/app/core/entity-list/entity-list/entity-list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class EntityListComponent<T extends Entity>

@Output() elementClick = new EventEmitter<T>();
@Output() addNewClick = new EventEmitter();
@Input() duplicatesdata : T[];
@Input() selectedRows : T[] = [];

@ViewChild(EntitySubrecordComponent) entityTable: EntitySubrecordComponent<T>;

Expand Down Expand Up @@ -305,10 +305,18 @@ export class EntityListComponent<T extends Entity>
this.addNewClick.emit();
}

getDatafromsubRecord(data: any) {
this.duplicatesdata = data
setSelectedRows(data: any) {
if (data.event.checked) {
this.selectedRows.push(data.row);
} else {
const index = this.selectedRows.indexOf(data.row);
if (index > -1) {
this.selectedRows.splice(index, 1);
}
}
}
clearDuplicatesData() {
this.duplicatesdata = null;

clearSelectedRows() {
this.selectedRows = [];
}
}

0 comments on commit 741692b

Please sign in to comment.