Skip to content

Commit 0160acf

Browse files
committed
feat(patch-detail): improve connection handling and patch update logic -vibe-
1 parent b8d20f6 commit 0160acf

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

src/app/components/patch-parts/patch-detail-data.service.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { MatSnackBar } from "@angular/material/snack-bar";
1111
import { Router } from '@angular/router';
1212
import {
1313
BehaviorSubject,
14+
of,
1415
ReplaySubject,
1516
Subject
1617
} from 'rxjs';
@@ -236,13 +237,13 @@ export class PatchDetailDataService implements OnDestroy {
236237
takeUntil(this.destroyEvent$)
237238
)
238239
.subscribe(([_, patchConnections]) => {
240+
patchConnections = patchConnections || [];
239241
const selectedForConnection: {
240242
a: CVConnectionEntity | null;
241243
b: CVConnectionEntity | null
242244
} = this.selectedForConnection$.value;
243-
244245
const patch: Patch = this.singlePatchData$.value;
245-
246+
if (!selectedForConnection.a || !selectedForConnection.b || !patch) { return; }
246247
const newConnection: {
247248
patch: Patch;
248249
a: CVwithModule;
@@ -252,19 +253,13 @@ export class PatchDetailDataService implements OnDestroy {
252253
b: selectedForConnection.b.cv,
253254
patch
254255
};
255-
256-
const isAlreadyInList: boolean = !!patchConnections.find(connection => {
257-
return connection.a.id === newConnection.a.id && connection.b.id === newConnection.b.id;
258-
});
259-
256+
const isAlreadyInList: boolean = !!patchConnections.find(connection => connection.a.id === newConnection.a.id && connection.b.id === newConnection.b.id);
260257
if (!isAlreadyInList) {
261-
this.snackBar.open('✔ Connection confirmed', undefined, {duration: 1000});
262258
this.editorConnections$.next([
263259
...patchConnections,
264260
newConnection
265261
]);
266-
} else { this.snackBar.open('⚠ This connection has already been made', undefined, {duration: 2000}); }
267-
262+
}
268263
});
269264

270265
this.patchConnections$
@@ -285,12 +280,24 @@ export class PatchDetailDataService implements OnDestroy {
285280
this.savePatchEditing$
286281
.pipe(
287282
withLatestFrom(this.editorConnections$, this.singlePatchData$),
288-
switchMap(([a, patchConnections, patch]) => this.backend.update.patchConnections(patchConnections)
289-
.pipe(switchMap(x => this.backend.update.patch(patch)))),
283+
switchMap(([_, patchConnections, patch]) => {
284+
if (!patch) { return of(null); }
285+
if (patchConnections === null) { // connections not loaded yet, only patch update
286+
return this.backend.update.patch(patch);
287+
}
288+
if (patchConnections.length === 0) { // user cleared all connections
289+
return this.backend.delete.patchConnectionsForPatch(patch.id)
290+
.pipe(switchMap(() => this.backend.update.patch(patch)));
291+
}
292+
return this.backend.update.patchConnections(patchConnections)
293+
.pipe(switchMap(() => this.backend.update.patch(patch)));
294+
}),
290295
takeUntil(this.destroyEvent$)
291296
)
292-
.subscribe(value => {
293-
this.updateSinglePatchData$.next(this.singlePatchData$.value.id);
297+
.subscribe(() => {
298+
if (this.singlePatchData$.value) {
299+
this.updateSinglePatchData$.next(this.singlePatchData$.value.id);
300+
}
294301
});
295302

296303
this.deletePatch$

0 commit comments

Comments
 (0)