Skip to content

Commit

Permalink
feat: activate manager view for manager/admin user
Browse files Browse the repository at this point in the history
  • Loading branch information
katyastan committed Aug 29, 2024
1 parent b0dbc80 commit 4a18fba
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 7 deletions.
19 changes: 18 additions & 1 deletion src/app/api/ordersService/orders.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ordersDummyData } from './orders.data';
export class OrdersService {
private httpClient = inject(HttpClient);
public allOrders = signal<Order[]>([]);
public allUsers = signal<User[]>([]);

public getOrders(): Observable<Order[]> {
return this.httpClient.get<Order[]>(ENDPOINTS.ORDER).pipe(
Expand All @@ -23,6 +24,15 @@ export class OrdersService {
);
}

public getAllOrders(): Observable<Order[]> {
return this.httpClient.get<Order[]>(`${ENDPOINTS.ORDER}?all=true`).pipe(
shareReplay(1),
// Temporar solution with hardcoded data
// tap((orders) => this.allOrders.set(orders)),
tap(() => this.allOrders.set(ordersDummyData)),
);
}

public makeOrder(order: OrderRequest): Observable<OrderId> {
return this.httpClient.post<OrderId>(ENDPOINTS.ORDER, order);
}
Expand All @@ -32,6 +42,13 @@ export class OrdersService {
}

public getAllUsers(): Observable<User[]> {
return this.httpClient.get<User[]>(ENDPOINTS.USERS);
return this.httpClient.get<User[]>(ENDPOINTS.USERS).pipe(
shareReplay(1),
tap((users) => this.allUsers.set(users)),
);
}

public findUserById(id: number): User | null {
return this.allUsers()?.find((user) => user.id === id) ?? null;
}
}
3 changes: 3 additions & 0 deletions src/app/orders/components/order/order.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
Price: <b>{{ currentPrice | currency: 'USD' }}</b>
</div>
<span>Order ID: {{ order.id }}</span>
@if (user) {
<div>User: {{ user.email }}</div>
}
</div>
</div>

Expand Down
5 changes: 4 additions & 1 deletion src/app/orders/components/order/order.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { DialogModule } from 'primeng/dialog';
import { firstValueFrom, Subscription } from 'rxjs';

import { CarriageService } from '@/app/api/carriagesService/carriage.service';
import { Order } from '@/app/api/models/order';
import { Order, User } from '@/app/api/models/order';
import { OrdersService } from '@/app/api/ordersService/orders.service';
import { ProfileService } from '@/app/api/profileService/profile.service';
import { StationsService } from '@/app/api/stationsService/stations.service';
import { UserMessageService } from '@/app/shared/services/userMessage/user-message.service';

Expand All @@ -24,10 +25,12 @@ import { TripTimelineComponent } from '../../../home/components/trip-timeline/tr
})
export class OrderComponent implements OnInit, OnDestroy {
@Input() public order!: Order;
@Input() public user!: User | null;
private cdr = inject(ChangeDetectorRef);
public carriageService = inject(CarriageService);
public stationsService = inject(StationsService);
public ordersService = inject(OrdersService);
public profileService = inject(ProfileService);
private subsciption = new Subscription();
public carriageName = '';
public currentPrice = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<div>
<div>
@for (order of orders; track order.id) {
<app-order [order]="order"></app-order>
@let user = ordersService.findUserById(order.userId) ?? null;
<app-order [order]="order" [user]="user"></app-order>
}
</div>
</div>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
import { ChangeDetectionStrategy, Component, inject, Input } from '@angular/core';

import { Order } from '@/app/api/models/order';
import { Order, User } from '@/app/api/models/order';
import { OrdersService } from '@/app/api/ordersService/orders.service';

import { OrderComponent } from '../order/order.component';

Expand All @@ -14,4 +15,6 @@ import { OrderComponent } from '../order/order.component';
})
export class OrdersListComponent {
@Input() public orders!: Order[];
@Input() public users!: User[];
public ordersService = inject(OrdersService);
}
6 changes: 5 additions & 1 deletion src/app/orders/pages/orders/orders.component.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<div class="wrapper">
@let orders = ordersService.allOrders();
@if (orders.length) {
@let users = ordersService.allUsers();
@if (isManager) {
<div>Manager view availble!</div>
<app-orders-list [orders]="orders" [users]="users"></app-orders-list>
} @else {
<app-orders-list [orders]="orders"></app-orders-list>
}
</div>
15 changes: 14 additions & 1 deletion src/app/orders/pages/orders/orders.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ChangeDetectionStrategy, Component, inject, OnDestroy, OnInit } from '@
import { Subscription } from 'rxjs';

import { OrdersService } from '@/app/api/ordersService/orders.service';
import { ProfileService } from '@/app/api/profileService/profile.service';
import { StationsService } from '@/app/api/stationsService/stations.service';

import { OrdersListComponent } from '../../components/orders-list/orders-list.component';
Expand All @@ -20,10 +21,22 @@ import { OrdersListComponent } from '../../components/orders-list/orders-list.co
export class OrdersComponent implements OnInit, OnDestroy {
public ordersService = inject(OrdersService);
public stationsService = inject(StationsService);
public profileService = inject(ProfileService);
public isManager = false;
private subsciption = new Subscription();

public ngOnInit(): void {
this.subsciption.add(this.ordersService.getOrders().subscribe());
this.subsciption.add(
this.profileService.getProfile().subscribe((profile) => {
if (profile.role === 'manager') {
this.isManager = true;
this.ordersService.getAllOrders().subscribe();
this.ordersService.getAllUsers().subscribe();
} else {
this.ordersService.getOrders().subscribe();
}
}),
);
this.subsciption.add(this.stationsService.getStations().subscribe());
}

Expand Down

0 comments on commit 4a18fba

Please sign in to comment.