source: bus-n-go-pavel-216049/bus-n-go-frontend/src/app/components/route-instances/start-route-instnace/start-route-instance.component.ts

Last change on this file was baf4cc4, checked in by ppaunovski <paunovskipavel@…>, 3 months ago

split group project and individual project into two separate folders

  • Property mode set to 100644
File size: 3.4 KB
RevLine 
[baf4cc4]1import {Component, OnInit} from '@angular/core';
2import {LineService} from "../../../services/line/line.service";
3import {BusService} from "../../../services/bus/bus.service";
4import {DirectionService} from "../../../services/direction/direction.service";
5import {Bus} from "../../../model/Bus";
6import {MatButton} from "@angular/material/button";
7import {MatFormField, MatLabel} from "@angular/material/form-field";
8import {MatOption} from "@angular/material/core";
9import {MatSelect, MatSelectChange} from "@angular/material/select";
10import {filter, Subject, switchMap, tap} from "rxjs";
11import {Line} from "../../../model/Line";
12import {Direction} from "../../../model/Direction";
13import {RouteInstanceService} from "../../../services/route-instance/route-instance.service";
14import {Router} from "@angular/router";
15import {AuthService} from "../../../services/auth/auth.service";
16import {log} from "@angular-devkit/build-angular/src/builders/ssr-dev-server";
17
18@Component({
19 selector: 'app-start-route-instnace',
20 standalone: true,
21 imports: [
22 MatButton,
23 MatFormField,
24 MatLabel,
25 MatOption,
26 MatSelect
27 ],
28 templateUrl: './start-route-instance.component.html',
29 styleUrl: './start-route-instance.component.css'
30})
31export class StartRouteInstanceComponent implements OnInit {
32 buses: Bus[] = []
33 lines: Line[] = []
34 directions: Direction[] = []
35 isDriverFree = false
36
37 bus: Bus | undefined
38 line: Line | undefined
39 direction: Direction | undefined
40
41 private _busSelected$ = new Subject<Bus>();
42 private _lineSelected$ = new Subject<Line>();
43
44 constructor(
45 private _lineService: LineService,
46 private _busService: BusService,
47 private _directionService: DirectionService,
48 private _routeInstanceService: RouteInstanceService,
49 private _router: Router,
50 private _authService: AuthService
51 ) {
52 }
53
54 ngOnInit(): void {
55
56 this._authService.isDriverFree().pipe(
57 tap(x => console.log(x)),
58 tap(isDriverFree => this.isDriverFree = isDriverFree),
59 filter(isDriverFree => isDriverFree === true),
60 switchMap(_ => this._busService.getAllFree())
61 ).subscribe({
62 next: response => {
63 console.log('free buses', response)
64 this.buses = response
65 }
66 })
67
68 this._busSelected$.pipe(
69 switchMap(bus => this._lineService.getAll())
70 ).subscribe({
71 next: response => {
72 this.lines = response
73 }
74 })
75 this._lineSelected$.pipe(
76 switchMap(line => this._lineService.getDirections(line))
77 ).subscribe({
78 next: response => {
79 this.directions = response.map(x => x.pravec)
80 console.log(response)
81 }
82 })
83 }
84
85 onBusPick($event: MatSelectChange) {
86 this.bus = this.buses.find(bus => bus.id === $event.value)
87 if (this.bus) this._busSelected$.next(this.bus)
88 }
89
90 onLinePick($event: MatSelectChange) {
91 this.line = this.lines.find(line => line.id === $event.value)
92 if (this.line) this._lineSelected$.next(this.line)
93 }
94
95 onDirectionPick($event: MatSelectChange) {
96 this.direction = this.directions.find(direction => direction.id === $event.value)
97 }
98
99 onSubmit() {
100 if (this.bus && this.line && this.direction)
101 this._routeInstanceService.start({
102 lineId: this.line.id,
103 busId: this.bus.id,
104 directionId: this.direction.id,
105 }).subscribe({
106 next: response => {
107 this._router.navigate(['/route-instances', response.id])
108 }
109 })
110 }
111}
Note: See TracBrowser for help on using the repository browser.