1 | import {Component, OnInit} from '@angular/core';
|
---|
2 | import {RouteInstanceService} from "../../../services/route-instance/route-instance.service";
|
---|
3 | import {Station} from "../../../model/Station";
|
---|
4 | import {RouteInstanceResponse} from "../../../model/responses/RouteInstanceResponse";
|
---|
5 | import {StationService} from "../../../services/station/station.service";
|
---|
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 {Subject, switchMap, tap} from "rxjs";
|
---|
11 | import {CommuteService} from "../../../services/commute/commute.service";
|
---|
12 | import {TicketService} from "../../../services/ticket/ticket.service";
|
---|
13 | import {TicketResponse} from "../../../model/responses/TicketResponse";
|
---|
14 | import {Router} from "@angular/router";
|
---|
15 | import {log} from "@angular-devkit/build-angular/src/builders/ssr-dev-server";
|
---|
16 |
|
---|
17 | @Component({
|
---|
18 | selector: 'app-start-commute',
|
---|
19 | standalone: true,
|
---|
20 | imports: [
|
---|
21 | MatButton,
|
---|
22 | MatFormField,
|
---|
23 | MatLabel,
|
---|
24 | MatOption,
|
---|
25 | MatSelect
|
---|
26 | ],
|
---|
27 | templateUrl: './start-commute.component.html',
|
---|
28 | styleUrl: './start-commute.component.css'
|
---|
29 | })
|
---|
30 | export class StartCommuteComponent implements OnInit {
|
---|
31 | tickets: TicketResponse[] = []
|
---|
32 | stations: Station[] = []
|
---|
33 | routeInstances: RouteInstanceResponse[] = []
|
---|
34 |
|
---|
35 | station: number | undefined;
|
---|
36 | instance: number | undefined;
|
---|
37 | ticket: number | undefined;
|
---|
38 |
|
---|
39 | private _stationSelected$ = new Subject<number>();
|
---|
40 | private _instanceSelected$ = new Subject<number>();
|
---|
41 | private _ticketSelected$ = new Subject<number>();
|
---|
42 | private _startCommute$ = new Subject<void>();
|
---|
43 |
|
---|
44 | constructor(private routeInstanceService: RouteInstanceService,
|
---|
45 | private stationService: StationService,
|
---|
46 | private commuteService: CommuteService,
|
---|
47 | private ticketService: TicketService,
|
---|
48 | private _router: Router,
|
---|
49 | ) {
|
---|
50 | }
|
---|
51 |
|
---|
52 | ngOnInit(): void {
|
---|
53 | this.ticketService.getTickets().subscribe({
|
---|
54 | next: response => {
|
---|
55 | this.tickets = response
|
---|
56 | console.log(this.tickets)
|
---|
57 | }
|
---|
58 | })
|
---|
59 |
|
---|
60 | this.stationService.getAllStations().subscribe({
|
---|
61 | next: response => {
|
---|
62 | this.stations = response;
|
---|
63 | console.log(this.stations)
|
---|
64 | }
|
---|
65 | })
|
---|
66 |
|
---|
67 | this._stationSelected$.pipe(
|
---|
68 | switchMap(station => this.routeInstanceService.getForStation(station))
|
---|
69 | ).subscribe({
|
---|
70 | next: response => {
|
---|
71 | this.routeInstances = response;
|
---|
72 | console.log(response);
|
---|
73 | }
|
---|
74 | })
|
---|
75 | // todo: finish logic
|
---|
76 | this._startCommute$.pipe(
|
---|
77 | switchMap(() => {
|
---|
78 | console.log('in switch map');
|
---|
79 | return this.commuteService.startCommute({
|
---|
80 | stationId: this.station!!,
|
---|
81 | routeInstanceId: this.instance!!,
|
---|
82 | ticketId: this.ticket!!
|
---|
83 | });
|
---|
84 | }
|
---|
85 | )
|
---|
86 | ).subscribe({
|
---|
87 | next: response => {
|
---|
88 | console.log(response)
|
---|
89 | },
|
---|
90 | error: err => console.log(err)
|
---|
91 | })
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | onStationPick(event: MatSelectChange) {
|
---|
96 | this.station = event.value
|
---|
97 | if (this.station) this._stationSelected$.next(this.station)
|
---|
98 | }
|
---|
99 |
|
---|
100 | onInstancePick(event: MatSelectChange) {
|
---|
101 | this.instance = event.value
|
---|
102 | if (this.instance) this._instanceSelected$.next(this.instance)
|
---|
103 | }
|
---|
104 |
|
---|
105 | startCommute() {
|
---|
106 | console.log('starting commute');
|
---|
107 | console.log('ticket', this.ticket);
|
---|
108 | console.log('instance', this.instance);
|
---|
109 | console.log('station', this.station);
|
---|
110 | if (this.station && this.ticket && this.instance)
|
---|
111 | this.commuteService.startCommute({
|
---|
112 | stationId: this.station!!,
|
---|
113 | routeInstanceId: this.instance!!,
|
---|
114 | ticketId: this.ticket!!
|
---|
115 | }).pipe(
|
---|
116 | tap(res => console.log(res))
|
---|
117 | ).subscribe(res => {
|
---|
118 | console.log(res)
|
---|
119 | this._router.navigate(['/commutes', res.id])
|
---|
120 | })
|
---|
121 | }
|
---|
122 |
|
---|
123 | onTicketSelect(event: MatSelectChange) {
|
---|
124 | this.ticket = event.value
|
---|
125 | if (this.ticket) this._ticketSelected$.next(this.ticket)
|
---|
126 | }
|
---|
127 | }
|
---|