source: bus-n-go-pavel-216049/bus-n-go-frontend/src/app/components/commutes/start-commute/start-commute.component.ts@ baf4cc4

Last change on this file since baf4cc4 was baf4cc4, checked in by ppaunovski <paunovskipavel@…>, 4 weeks ago

split group project and individual project into two separate folders

  • Property mode set to 100644
File size: 3.9 KB
Line 
1import {Component, OnInit} from '@angular/core';
2import {RouteInstanceService} from "../../../services/route-instance/route-instance.service";
3import {Station} from "../../../model/Station";
4import {RouteInstanceResponse} from "../../../model/responses/RouteInstanceResponse";
5import {StationService} from "../../../services/station/station.service";
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 {Subject, switchMap, tap} from "rxjs";
11import {CommuteService} from "../../../services/commute/commute.service";
12import {TicketService} from "../../../services/ticket/ticket.service";
13import {TicketResponse} from "../../../model/responses/TicketResponse";
14import {Router} from "@angular/router";
15import {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})
30export 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}
Note: See TracBrowser for help on using the repository browser.