Skip to content

Commit

Permalink
fix(core): subrecord lists update for external changes in real-time
Browse files Browse the repository at this point in the history
---------
This functionality has been developed for the project “codo”.
codo is developed under the projects “Landungsbrücken – Patenschaften in Hamburg stärken” and “openTransfer Patenschaften”. It is funded through the program “Menschen stärken Menschen” by the German Federal Ministry of Family Affairs, Senior Citizens, Women and Youth.
More information at https://github.com/codo-mentoring

“Landungsbrücken – Patenschaften in Hamburg stärken” is a project of BürgerStiftung Hamburg in cooperation with the Mentor.Ring Hamburg. With a mix of networking opportunities, capacity building and financial support the project strengthens Hamburg’s scene of mentoring projects since its founding in 2016.

The “Stiftung Bürgermut” foundation since 2007 supports the digital and real exchange of experiences and connections of active citizens. Within the federal program “Menschen stärken Menschen” the foundation as part of its program “openTransfer Patenschaften” offers support services for connecting, spreading and upskilling mentoring organisations across Germany.

Diese Funktion wurde entwickelt für das Projekt codo.
codo wird entwickelt im Rahmen der Projekte Landungsbrücken – Patenschaften in Hamburg stärken und openTransfer Patenschaften. Er ist gefördert durch das Bundesprogramm Menschen stärken Menschen des Bundesministeriums für Familie, Senioren, Frauen und Jugend.
Mehr Informationen unter https://github.com/codo-mentoring

“Landungsbrücken – Patenschaften in Hamburg stärken” ist ein Projekt der BürgerStiftung Hamburg in Kooperation mit dem Mentor.Ring Hamburg. Mit einer Mischung aus Vernetzungsangeboten, Qualifizierungsmaßnahmen und finanzieller Förderung stärkt das Projekt die Hamburger Szene der Patenschaftsprojekte seit der Gründung im Jahr 2016.

Die Stiftung Bürgermut fördert seit 2007 den digitalen und realen Erfahrungsaustausch und die Vernetzung von engagierten Bürger:innen. Innerhalb des Bundesprogramms „Menschen stärken Menschen” bietet die Stiftung im Rahmen ihres Programms openTransfer Patenschaften Unterstützungsleistungen zur Vernetzung, Verbreitung und Qualifizierung von Patenschafts- und Mentoringorganisationen bundesweit.

Co-authored-by: codo-mentoring <117934638+codo-mentoring@users.noreply.github.com>
  • Loading branch information
sleidig and codo-mentoring committed Nov 13, 2023
1 parent ab9c5d0 commit 7cf1d82
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import {
MatCheckboxModule,
} from "@angular/material/checkbox";
import { MatSlideToggleModule } from "@angular/material/slide-toggle";
import { applyUpdate } from "../../../entity/model/entity-update";

export interface TableRow<T extends Entity> {
record: T;
Expand Down Expand Up @@ -349,19 +350,12 @@ export class EntitySubrecordComponent<T extends Entity> implements OnChanges {
this.updateSubscription = this.entityMapper
.receiveUpdates(this.getEntityConstructor())
.pipe(untilDestroyed(this))
.subscribe(({ entity, type }) => {
if (type === "new") {
this.addToTable(entity);
} else if (type === "remove") {
this.removeFromDataTable(entity);
} else if (
type === "update" &&
!this.records.find((rec) => rec.getId() === entity.getId())
) {
this.addToTable(entity);
}
.subscribe((next) => {
this.records = applyUpdate(this.records, next, true);

if (!this.predicate(entity)) {
if (this.predicate(next.entity)) {
this.initDataSource();
} else {
// hide after a short delay to give a signal in the UI why records disappear by showing the changed values first
setTimeout(() => this.initDataSource(), 5000);
}
Expand Down Expand Up @@ -410,20 +404,6 @@ export class EntitySubrecordComponent<T extends Entity> implements OnChanges {
row.formGroup = null;
}

private removeFromDataTable(deleted: T) {
// use setter so datasource is also updated
this.records = this.records.filter(
(rec) => rec.getId() !== deleted.getId(),
);
this.initDataSource();
}

private addToTable(record: T) {
// use setter so datasource is also updated
this.records = [record].concat(this.records);
this.initDataSource();
}

/**
* Create a new entity.
* The entity is only written to the database when the user saves this record which is newly added in edit mode.
Expand Down
29 changes: 20 additions & 9 deletions src/app/core/entity/model/entity-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,34 @@ export interface UpdatedEntity<T extends Entity> {
* @param next An entity that should be updated as well as the type of update. This, as well as the entity
* may be undefined or null. In this event, the entities-array is returned as is.
* @param entities The entities to update, must be defined
* @param addIfMissing (Optional) whether to add an entity that comes through an update event but is not part of the array yet (default is to ignore)
* @return An array of the given entities with the update applied
*/
export function applyUpdate<T extends Entity>(
entities: T[],
next: UpdatedEntity<T>,
addIfMissing: boolean = false,
): T[] {
if (!next || !next.entity || !entities) {
return entities;
}
switch (next.type) {
case "new":
return [next.entity].concat(entities);
case "update":
return entities.map((e) =>
e.getId() === next.entity.getId() ? next.entity : e,
);
case "remove":
return entities.filter((e) => e.getId() !== next.entity.getId());

if (
next.type === "new" ||
(addIfMissing &&
next.type === "update" &&
!entities.find((e) => e.getId() === next.entity.getId()))
) {
return [next.entity].concat(entities);
}

if (next.type === "update") {
return entities.map((e) =>
e.getId() === next.entity.getId() ? next.entity : e,
);
}

if (next.type === "remove") {
return entities.filter((e) => e.getId() !== next.entity.getId());
}
}

0 comments on commit 7cf1d82

Please sign in to comment.