From 7ea40e7f6f5b84ef5089fca3f06092b721e18c8e Mon Sep 17 00:00:00 2001 From: Max Ehrlicher-Schmidt Date: Sun, 13 Dec 2020 21:42:01 +0100 Subject: [PATCH] Add copy functionallity --- src/app/components/table/table.component.html | 7 ++++- src/app/components/table/table.component.ts | 18 ++++++++++++ src/app/graphqlOperations/bike.graphql | 6 ++++ .../pages/tables/bikes/bikes.component.html | 2 ++ src/app/pages/tables/bikes/bikes.component.ts | 5 ++++ src/app/services/bikes.service.ts | 16 +++++++++++ src/generated/graphql.ts | 28 +++++++++++++++++++ 7 files changed, 81 insertions(+), 1 deletion(-) diff --git a/src/app/components/table/table.component.html b/src/app/components/table/table.component.html index 7f67d27..c521f89 100644 --- a/src/app/components/table/table.component.html +++ b/src/app/components/table/table.component.html @@ -284,7 +284,12 @@ > deleteLöschen - diff --git a/src/app/components/table/table.component.ts b/src/app/components/table/table.component.ts index 214dccb..7ec5b1e 100644 --- a/src/app/components/table/table.component.ts +++ b/src/app/components/table/table.component.ts @@ -63,6 +63,9 @@ export class TableComponent implements AfterViewInit { @Input() tableDataGQLUpdateInputType: string; + @Input() + copyableRows = false; + @ViewChild(MatPaginator) paginator: MatPaginator; @ViewChild(MatSort) sort: MatSort; @@ -97,6 +100,7 @@ export class TableComponent implements AfterViewInit { @Output() createEvent = new EventEmitter(); @Output() lockEvent = new EventEmitter(); @Output() saveEvent = new EventEmitter(); + @Output() copyEvent = new EventEmitter(); @Output() cancelEvent = new EventEmitter(); @Output() deleteEvent = new EventEmitter(); @@ -159,6 +163,12 @@ export class TableComponent implements AfterViewInit { this.reloadingTable = false; this.isLoaded = true; for (const row of newTableDataSource) { + if (row.newObject) { + // its a copied object + row.id = this.getNewId(); + tempDataSource.push(flatten(row)); + continue; + } const oldRow = this.getRowById(row.id); /** make sure to not overwrite a row that is being edited */ if (!oldRow) { @@ -340,6 +350,14 @@ export class TableComponent implements AfterViewInit { this.saveEvent.emit(deepenRow); } + copy(row: any) { + const deepenRow = this.schemaService.filterObject( + this.tableDataGQLUpdateInputType, + deepen(row) + ); + this.copyEvent.emit(deepenRow); + } + cancel(row: any) { this.cancelEvent.emit(row); } diff --git a/src/app/graphqlOperations/bike.graphql b/src/app/graphqlOperations/bike.graphql index d96a3cf..e5af105 100644 --- a/src/app/graphqlOperations/bike.graphql +++ b/src/app/graphqlOperations/bike.graphql @@ -10,6 +10,12 @@ query GetCargoBikeById($id: ID!) { } } +query copyCargoBikeById($id: ID!) { + copyCargoBikeById(id: $id) { + ...CargoBikeFieldsForTable + } +} + query ReloadCargoBikeById($id: ID!) { cargoBikeById(id: $id) { ...CargoBikeFieldsForTable diff --git a/src/app/pages/tables/bikes/bikes.component.html b/src/app/pages/tables/bikes/bikes.component.html index 8c18b6a..4f26be9 100644 --- a/src/app/pages/tables/bikes/bikes.component.html +++ b/src/app/pages/tables/bikes/bikes.component.html @@ -2,6 +2,7 @@ [headline]="headline" [headlineIconName]="headlineIconName" [columnInfo]="columnInfo" + [copyableRows]="copyableRows" [dataService]="dataService" [tableDataGQLType]="tableDataGQLType" [tableDataGQLCreateInputType]="tableDataGQLCreateInputType" @@ -9,6 +10,7 @@ (createEvent)="create($event)" (lockEvent)="lock($event)" (saveEvent)="save($event)" + (copyEvent)="copy($event)" (cancelEvent)="cancel($event)" (deleteEvent)="delete($event)" > diff --git a/src/app/pages/tables/bikes/bikes.component.ts b/src/app/pages/tables/bikes/bikes.component.ts index 9b6d8e2..1dd25a5 100644 --- a/src/app/pages/tables/bikes/bikes.component.ts +++ b/src/app/pages/tables/bikes/bikes.component.ts @@ -132,6 +132,7 @@ export class BikesComponent implements OnInit { headline = 'Lastenräder'; headlineIconName = 'directions_bike'; + copyableRows = true; loadingRowIds: string[] = []; constructor(private bikesService: BikesService) {} @@ -152,6 +153,10 @@ export class BikesComponent implements OnInit { this.bikesService.updateBike({ bike: row }); } + copy(row: any) { + this.bikesService.copyBikeById({ id: row.id }); + } + cancel(row: any) { this.bikesService.unlockBike({ id: row.id }); } diff --git a/src/app/services/bikes.service.ts b/src/app/services/bikes.service.ts index 263205f..97d4839 100644 --- a/src/app/services/bikes.service.ts +++ b/src/app/services/bikes.service.ts @@ -16,6 +16,8 @@ import { DeleteCargoBikeMutationVariables, GetCargoBikeByIdGQL, GetCargoBikeByIdQueryVariables, + CopyCargoBikeByIdGQL, + CopyCargoBikeByIdQueryVariables } from 'src/generated/graphql'; @Injectable({ @@ -34,6 +36,7 @@ export class BikesService { private getCargoBikeByIdGQL: GetCargoBikeByIdGQL, private reloadCargoBikeByIdGQL: ReloadCargoBikeByIdGQL, private updateCargoBikeGQL: UpdateCargoBikeGQL, + private copyCargoBikeByIdGQL: CopyCargoBikeByIdGQL, private lockCargoBikeGQL: LockCargoBikeGQL, private unlockCargoBikeGQL: UnlockCargoBikeGQL, private createCargoBikeGQL: CreateCargoBikeGQL, @@ -73,6 +76,19 @@ export class BikesService { }); } + copyBikeById(variables: CopyCargoBikeByIdQueryVariables) { + + this.copyCargoBikeByIdGQL + .fetch(variables) + .subscribe((result) => { + const newBike = result.data.copyCargoBikeById; + newBike["newObject"] = true; + const currentTableData = this.tableData.getValue(); + this.tableData.next([newBike, ...this.tableData.getValue()]); + this.tableData.next(currentTableData); + }) + } + reloadBike(variables: ReloadCargoBikeByIdQueryVariables) { this.addLoadingRowId(variables.id); this.reloadCargoBikeByIdGQL diff --git a/src/generated/graphql.ts b/src/generated/graphql.ts index b047b52..f3218db 100644 --- a/src/generated/graphql.ts +++ b/src/generated/graphql.ts @@ -1813,6 +1813,16 @@ export type GetCargoBikeByIdQuery = { __typename?: 'Query', cargoBikeById?: Mayb & CargoBikeFieldsForPageFragment )> }; +export type CopyCargoBikeByIdQueryVariables = Exact<{ + id: Scalars['ID']; +}>; + + +export type CopyCargoBikeByIdQuery = { __typename?: 'Query', copyCargoBikeById?: Maybe<( + { __typename?: 'CargoBike' } + & CargoBikeFieldsForTableFragment + )> }; + export type ReloadCargoBikeByIdQueryVariables = Exact<{ id: Scalars['ID']; }>; @@ -3744,6 +3754,24 @@ export const GetCargoBikeByIdDocument = gql` export class GetCargoBikeByIdGQL extends Apollo.Query { document = GetCargoBikeByIdDocument; + constructor(apollo: Apollo.Apollo) { + super(apollo); + } + } +export const CopyCargoBikeByIdDocument = gql` + query copyCargoBikeById($id: ID!) { + copyCargoBikeById(id: $id) { + ...CargoBikeFieldsForTable + } +} + ${CargoBikeFieldsForTableFragmentDoc}`; + + @Injectable({ + providedIn: 'root' + }) + export class CopyCargoBikeByIdGQL extends Apollo.Query { + document = CopyCargoBikeByIdDocument; + constructor(apollo: Apollo.Apollo) { super(apollo); }