Skip to content

Commit

Permalink
feat: add AppComponent inputs/outputs (#166)
Browse files Browse the repository at this point in the history
Add inputs and outputs so that a custom element user can interact
with NGE.

A netzgrafikDto input is introduced to get/set the data service's
current DTO. A operation output is introduced to notify about DTO
changes. The changes are described by a new Operation class.

Co-authored-by: Louis Greiner <greiner.louis@gmail.com>
  • Loading branch information
emersion and louisgreiner authored Jul 4, 2024
1 parent bd7f08b commit 9d2c6ee
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 5 deletions.
22 changes: 19 additions & 3 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import {Component} from "@angular/core";
import {Component, Input, Output} from "@angular/core";
import {AuthService} from "./services/auth/auth.service";
import {TrainrunService} from "./services/data/trainrun.service";
import {TrainrunSectionService} from "./services/data/trainrunsection.service";
import {DataService} from "./services/data/data.service";
import {environment} from "../environments/environment";
import packageJson from "../../package.json";
import {Observable} from "rxjs";
import {Observable, merge} from "rxjs";
import {ProjectDto} from "./api/generated";
import {NetzgrafikDto} from "./data-structures/business.data.structures";
import {Operation} from "./models/operation.model";

@Component({
selector: "sbb-root",
Expand Down Expand Up @@ -33,7 +38,7 @@ export class AppComponent {
return this.authService.claims?.email;
}

constructor(private authService: AuthService) {
constructor(private authService: AuthService, private dataService: DataService, private trainrunService: TrainrunService, private trainrunSectionService: TrainrunSectionService) {
if (!this.disableBackend) {
this.authenticated = authService.initialized;
}
Expand All @@ -44,4 +49,15 @@ export class AppComponent {
this.authService.logOut();
}
}

@Input()
get netzgrafikDto() {
return this.dataService.getNetzgrafikDto();
}
set netzgrafikDto(netzgrafikDto: NetzgrafikDto) {
this.dataService.loadNetzgrafikDto(netzgrafikDto);
}

@Output()
operation: Observable<Operation> = merge(this.trainrunService.operation, this.trainrunSectionService.operation);
}
43 changes: 43 additions & 0 deletions src/app/models/operation.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {Trainrun} from "./trainrun.model";
import {TrainrunSection} from "./trainrunsection.model";

enum OperationType {
create = "create",
update = "update",
delete = "delete"
}

export abstract class Operation {
readonly type: OperationType;

constructor(type: OperationType) {
this.type = type;
}
}

export class CreateTrainrunOperation extends Operation {
readonly trainrunSection: TrainrunSection;

constructor(trainrunSection: TrainrunSection) {
super(OperationType.create);
this.trainrunSection = trainrunSection;
}
}

export class UpdateTrainrunSectionsOperation extends Operation {
readonly trainrunSections: TrainrunSection[];

constructor(trainrunSections: TrainrunSection[]) {
super(OperationType.update);
this.trainrunSections = trainrunSections;
}
}

export class DeleteTrainrunOperation extends Operation {
readonly trainrun: Trainrun;

constructor(trainrun: Trainrun) {
super(OperationType.delete);
this.trainrun = trainrun;
}
}
6 changes: 5 additions & 1 deletion src/app/services/data/trainrun.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
TrainrunFrequency,
TrainrunTimeCategory,
} from "../../data-structures/business.data.structures";
import {Injectable} from "@angular/core";
import {EventEmitter, Injectable} from "@angular/core";
import {BehaviorSubject} from "rxjs";
import {NodeService} from "./node.service";
import {TrainrunSectionService} from "./trainrunsection.service";
Expand All @@ -23,6 +23,7 @@ import {FilterService} from "../ui/filter.service";
import {Transition} from "../../models/transition.model";
import {Port} from "../../models/port.model";
import {Connection} from "../../models/connection.model";
import {DeleteTrainrunOperation, Operation} from "../../models/operation.model";

@Injectable({
providedIn: "root",
Expand All @@ -34,6 +35,8 @@ export class TrainrunService {

trainrunsStore: { trainruns: Trainrun[] } = {trainruns: []}; // store the data in memory

readonly operation = new EventEmitter<Operation>();

private dataService: DataService = null;
private nodeService: NodeService = null;
private trainrunSectionService: TrainrunSectionService = null;
Expand Down Expand Up @@ -180,6 +183,7 @@ export class TrainrunService {
if (enforceUpdate) {
this.trainrunsUpdated();
}
this.operation.emit(new DeleteTrainrunOperation(trainrun));
}

getSelectedTrainrun(): Trainrun {
Expand Down
13 changes: 12 additions & 1 deletion src/app/services/data/trainrunsection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
TrainrunSectionDto,
} from "../../data-structures/business.data.structures";
import {Node} from "../../models/node.model";
import {Injectable, OnDestroy} from "@angular/core";
import {Injectable, OnDestroy, EventEmitter} from "@angular/core";
import {BehaviorSubject, Subject} from "rxjs";
import {TrainrunService} from "./trainrun.service";
import {NodeService} from "./node.service";
Expand All @@ -22,6 +22,7 @@ import {Transition} from "../../models/transition.model";
import {takeUntil} from "rxjs/operators";
import {FilterService} from "../ui/filter.service";
import {TrainrunSectionNodePair} from "../util/trainrun.iterator";
import {CreateTrainrunOperation, Operation, UpdateTrainrunSectionsOperation} from "../../models/operation.model";

interface DepartureAndArrivalTimes {
nodeFromDepartureTime: number;
Expand All @@ -46,6 +47,8 @@ export class TrainrunSectionService implements OnDestroy {
trainrunSections: [],
}; // store the data in memory

readonly operation = new EventEmitter<Operation>();

informSelectedTrainrunClickSubject =
new BehaviorSubject<InformSelectedTrainrunClick>({
trainrunSectionId: undefined,
Expand Down Expand Up @@ -668,6 +671,8 @@ export class TrainrunSectionService implements OnDestroy {

createTrainrunSection(sourceNodeId: number, targetNodeId: number, retrieveTravelTimeFromEdge: boolean = false) {
const trainrunSection: TrainrunSection = new TrainrunSection();
const initialTrainrunsLength = this.trainrunService.trainrunsStore.trainruns.length;

trainrunSection.setTrainrun(
this.trainrunService.getSelectedOrNewTrainrun(),
);
Expand Down Expand Up @@ -698,6 +703,12 @@ export class TrainrunSectionService implements OnDestroy {
this.propagateTimesForNewTrainrunSection(trainrunSection);
//this.trainrunSectionsUpdated();
this.trainrunService.trainrunsUpdated();

if (initialTrainrunsLength !== this.trainrunService.trainrunsStore.trainruns.length) {
this.operation.emit(new CreateTrainrunOperation(trainrunSection));
} else {
this.operation.emit(new UpdateTrainrunSectionsOperation(this.getAllTrainrunSectionsForTrainrun(trainrunSection.getTrainrunId())));
}
}

reconnectTrainrunSection(
Expand Down

0 comments on commit 9d2c6ee

Please sign in to comment.