1 | import {Component, OnInit} from '@angular/core';
|
---|
2 | import {LineService} from "../../../services/line/line.service";
|
---|
3 | import {BusService} from "../../../services/bus/bus.service";
|
---|
4 | import {DirectionService} from "../../../services/direction/direction.service";
|
---|
5 | import {Bus} from "../../../model/Bus";
|
---|
6 | import {MatButton} from "@angular/material/button";
|
---|
7 | import {MatFormField, MatLabel} from "@angular/material/form-field";
|
---|
8 | import {MatOption} from "@angular/material/core";
|
---|
9 | import {MatSelect, MatSelectChange} from "@angular/material/select";
|
---|
10 | import {filter, Subject, switchMap, tap} from "rxjs";
|
---|
11 | import {Line} from "../../../model/Line";
|
---|
12 | import {Direction} from "../../../model/Direction";
|
---|
13 | import {RouteInstanceService} from "../../../services/route-instance/route-instance.service";
|
---|
14 | import {Router} from "@angular/router";
|
---|
15 | import {AuthService} from "../../../services/auth/auth.service";
|
---|
16 | import {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 | })
|
---|
31 | export 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 | }
|
---|