source: bus-n-go-pavel-216049/bus-n-go-frontend/src/app/components/admin/admin-panel/admin-panel.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: 2.7 KB
Line 
1import {Component, OnInit} from '@angular/core';
2import {MatTableModule} from '@angular/material/table';
3import {AdminService} from "../../../services/admin/admin.service";
4import {AdminUsersResponse} from "../../../model/responses/AdminUsersResponse";
5import {MatCheckboxChange, MatCheckboxModule} from '@angular/material/checkbox';
6import {MatButton} from "@angular/material/button";
7import {PieChartComponent} from "../../charts/pie-chart/pie-chart.component";
8import {map, Observable} from "rxjs";
9import {PieChartValues} from "../../../model/PieChartValues";
10import {CommutesByHour, LineChartComponent} from "../../charts/line-chart.component";
11import {IncomeChartComponent, IncomeData} from "../../charts/stacked-bar-chart.component";
12
13@Component({
14 selector: 'app-admin-panel',
15 standalone: true,
16 imports: [MatTableModule, MatCheckboxModule, MatButton, PieChartComponent, LineChartComponent, IncomeChartComponent],
17 templateUrl: './admin-panel.component.html',
18 styleUrl: './admin-panel.component.css'
19})
20export class AdminPanelComponent implements OnInit {
21
22 allRoles = ['ROLE_ADMIN', 'ROLE_DRIVER', 'ROLE_CONDUCTOR', 'ROLE_PASSENGER']
23
24 users: AdminUsersResponse[] = []
25 usersColumns: string[] = ['id', 'name', 'email', 'phoneNumber', 'address', 'roles', 'update'];
26
27 constructor(private adminService: AdminService) {
28
29 }
30
31 ngOnInit() {
32 this.adminService.getAllUsers().subscribe({
33 next: data => {
34 this.users = data
35 console.log(data)
36 }
37 })
38 }
39
40 checked(role: string, roles: string[]): boolean {
41 return roles.includes(role)
42 }
43
44 updateRoles(id: number) {
45 console.log(id)
46 const user = this.users.find(u => u.id === id)
47 console.log(user)
48 if (!user) return;
49 this.adminService.updateRolesToUser(id, user.roles).subscribe({
50 next: value => {
51 console.log(value)
52 this.users = value
53 }
54 })
55 }
56
57 onSelectRole(id: number, event: MatCheckboxChange) {
58 console.log(event)
59 const role = event.source.name
60 if (!role) return;
61 this.users.filter(u => u.id === id).forEach(
62 u => {
63 var index = u.roles.indexOf(role);
64 if (index > -1) {
65 u.roles.splice(index, 1);
66 } else {
67 u.roles.push(role);
68 }
69 }
70 )
71 }
72
73 getFinesPerLine(): Observable<PieChartValues[]> {
74 return this.adminService.getFinesPerLine()
75 .pipe(
76 map(data => data.flatMap(finePerLine => Object({value: finePerLine.count, name: finePerLine.line.ime}))
77 )
78 );
79 }
80
81 getCommutesByHour(): Observable<CommutesByHour[]> {
82 return this.adminService.getCommutesByHour();
83 }
84
85 getTotalIncome(): Observable<IncomeData[]> {
86 return this.adminService.getTotalIncome()
87 }
88}
Note: See TracBrowser for help on using the repository browser.