Skip to content

Commit a32ed97

Browse files
committed
Edit and delete inplace for scroll
1 parent ec00c3a commit a32ed97

6 files changed

+69
-27
lines changed

scilog/src/app/overview/overview-scroll/overview-scroll.component.spec.ts

+28-5
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,34 @@ describe('OverviewScrollComponent', () => {
171171
}));
172172
});
173173

174-
it('should test deleteLogbook', async () => {
175-
const reloadSpy = spyOn(component, 'reloadLogbooks');
176-
await component.deleteLogbook('123');
177-
expect(logbookDataSpy.deleteLogbook).toHaveBeenCalledOnceWith('123');
178-
expect(reloadSpy).toHaveBeenCalledTimes(1);
174+
it('should test deleteLogbook', fakeAsync(async () => {
175+
const reshapeOnResizeSpy = spyOn<any>(component, 'reshapeOnResize').and.callThrough();
176+
spyOn<any>(component, 'getLogbooks').and.returnValue([{id: '5'}]);
177+
component.logbooks = [
178+
[{id: '1'},{id: '2'}, {id: '3'}],
179+
[{id: '4'}]
180+
];
181+
component['pageSize'] = 3;
182+
component['endOfData'] = false;
183+
await component.deleteLogbook('3');
184+
expect(reshapeOnResizeSpy).toHaveBeenCalledOnceWith(
185+
2,
186+
[{id: '1'}, {id: '2'}, {id: '4'}]
187+
);
188+
expect(component.logbooks).toEqual([
189+
[{id: '1'}, {id: '2'}, {id: '4'}], [{id: '5'}]])
190+
}));
191+
192+
it('should test afterLogbookEdit', async () => {
193+
component.logbooks = [
194+
[{id: '1'},{id: '2'},{id: '3'}],
195+
[{id: '4'},{id: '5'},{id: '6'}]
196+
];
197+
component.afterLogbookEdit({id: '4', name: '9'} as unknown as Logbooks)
198+
expect(component.logbooks).toEqual([
199+
[{id: '1'},{id: '2'},{id: '3'}],
200+
[{id: '4', name: '9'},{id: '5'},{id: '6'}]
201+
]);
179202
});
180203

181204
})

scilog/src/app/overview/overview-scroll/overview-scroll.component.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export class OverviewScrollComponent {
116116

117117
private async reshapeOnResize(oldPageSize: number, logbooks: Logbooks[] = undefined) {
118118
const pageDiff = this.pageSize - oldPageSize;
119-
const _logbooks = logbooks ?? this.logbooks.flat();
119+
const _logbooks = [...(logbooks ?? this.logbooks.flat())];
120120
if (!this.endOfData)
121121
pageDiff > 0 ?
122122
_logbooks.push(...(await this.getLogbooks(oldPageSize, pageDiff))) :
@@ -161,9 +161,17 @@ export class OverviewScrollComponent {
161161
this.logbookEdit.emit(logbook);
162162
}
163163

164+
afterLogbookEdit(logbook: Logbooks) {
165+
this.logbooks.forEach(group =>
166+
group.forEach((log, i) => {if (log.id === logbook.id) group[i] = logbook})
167+
);
168+
}
169+
164170
async deleteLogbook(logbookId: string) {
165171
await this.dataService.deleteLogbook(logbookId);
166-
await this.reloadLogbooks();
172+
const logbooks = this.logbooks.flat().filter(logbook => logbook.id !== logbookId);
173+
await this.reshapeOnResize(
174+
this.pageSize - 1, logbooks);
167175
}
168176

169177
logbookSelected(logbookId: string) {

scilog/src/app/overview/overview-table/overview-table.component.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,10 @@ describe('OverviewTableComponent', () => {
119119
expect(reloadSpy).toHaveBeenCalledOnceWith(false);
120120
});
121121

122+
it('should test afterLogbookEdit', async () => {
123+
const reloadSpy = spyOn(component, 'reloadLogbooks');
124+
await component.afterLogbookEdit();
125+
expect(reloadSpy).toHaveBeenCalledOnceWith(false);
126+
});
127+
122128
})

scilog/src/app/overview/overview-table/overview-table.component.ts

+4
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ export class OverviewTableComponent implements OnInit {
9393
this.logbookEdit.emit(logbook);
9494
}
9595

96+
async afterLogbookEdit() {
97+
await this.reloadLogbooks(false);
98+
}
99+
96100
async deleteLogbook(logbookId: string) {
97101
await this.dataService.deleteLogbook(logbookId);
98102
await this.reloadLogbooks(false);

scilog/src/app/overview/overview.component.spec.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ describe('OverviewComponent', () => {
3434

3535
cookiesSpy = jasmine.createSpyObj("CookieService", ["lastLogbook"]);
3636
cookiesSpy.lastLogbook.and.returnValue([]);
37-
const tableSpy = jasmine.createSpyObj("OverviewTableComponent", ['reloadLogbooks']);
38-
const scrollSpy = jasmine.createSpyObj("OverviewScrollComponent", ['reloadLogbooks']);
37+
const tableSpy = jasmine.createSpyObj("OverviewTableComponent", ['reloadLogbooks', 'afterLogbookEdit']);
38+
const scrollSpy = jasmine.createSpyObj("OverviewScrollComponent", ['reloadLogbooks', 'afterLogbookEdit']);
3939

4040
beforeEach(waitForAsync(() => {
4141
TestBed.configureTestingModule({
@@ -65,16 +65,16 @@ describe('OverviewComponent', () => {
6565
});
6666

6767
[
68-
['logbook-module', scrollSpy, 'add', true],
69-
['logbook-module', scrollSpy, 'edit', false],
70-
['logbook-headline', tableSpy, 'add', true],
71-
['logbook-headline', tableSpy, 'edit', false],
68+
['logbook-module', scrollSpy.reloadLogbooks, 'add', true],
69+
['logbook-module', scrollSpy.afterLogbookEdit, 'edit', false],
70+
['logbook-headline', tableSpy.reloadLogbooks, 'add', true],
71+
['logbook-headline', tableSpy.afterLogbookEdit, 'edit', false],
7272
].forEach((t, i) => {
7373
it(`should test reloadData ${i}`, async () => {
74-
t[1].reloadLogbooks.calls.reset();
75-
component.matCardType = t[0] as MatCardType;
76-
await component['reloadData'](t[2] as 'edit' | 'add');
77-
expect(t[1].reloadLogbooks).toHaveBeenCalledOnceWith(t[3]);
74+
t[1].calls.reset();
75+
component.matCardType = t[0];
76+
await component['reloadData']({id: '1'}, t[2]);
77+
expect(t[1]).toHaveBeenCalledTimes(1);
7878
});
7979
});
8080

scilog/src/app/overview/overview.component.ts

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component, OnInit, ViewChild } from '@angular/core';
22
import { Logbooks } from '@model/logbooks';
3-
import { from, Subscription, switchMap } from 'rxjs';
3+
import { Subscription } from 'rxjs';
44
import { UserPreferencesService } from '@shared/user-preferences.service';
55
import { CollectionConfig, WidgetItemConfig } from '@model/config';
66
import { MatLegacyDialog as MatDialog, MatLegacyDialogConfig as MatDialogConfig } from '@angular/material/legacy-dialog';
@@ -102,13 +102,16 @@ export class OverviewComponent implements OnInit {
102102
let dialogRef: any;
103103
dialogRef = this.dialog.open(AddLogbookComponent, dialogConfig);
104104

105-
this.subscriptions.push(dialogRef.afterClosed().pipe(
106-
switchMap(() => from(this.reloadData('edit')))
107-
).subscribe());
105+
this.subscriptions.push(dialogRef.afterClosed()
106+
.subscribe(async (result: Logbooks) => {
107+
await this.reloadData(result, 'edit');
108+
}));
108109
}
109110

110-
private async reloadData(action: 'edit' | 'add') {
111-
await this.overviewComponent.reloadLogbooks(!(action === 'edit'));
111+
async reloadData(logbook: Logbooks, action: 'edit' | 'add') {
112+
if (!logbook) return
113+
if (action === 'edit') await this.overviewComponent.afterLogbookEdit(logbook);
114+
if (action === 'add') await this.overviewComponent.reloadLogbooks();
112115
}
113116

114117
addCollectionLogbook(contentType: string) {
@@ -126,10 +129,8 @@ export class OverviewComponent implements OnInit {
126129
default:
127130
break;
128131
}
129-
this.subscriptions.push(dialogRef.afterClosed().subscribe(async result => {
130-
if (typeof result != "undefined") {
131-
await this.reloadData('add');
132-
}
132+
this.subscriptions.push(dialogRef.afterClosed().subscribe(async (result: Logbooks) => {
133+
await this.reloadData(result, 'add');
133134
}));
134135
}
135136

0 commit comments

Comments
 (0)