src/app/menuitems/menuitems.component.ts
OnInit
selector | app-menuitems |
styleUrls | ./menuitems.component.css |
templateUrl | ./menuitems.component.html |
Properties |
Methods |
Accessors |
constructor(route: ActivatedRoute, http: HttpClient, cartService: CartService)
|
||||||||||||
Defined in src/app/menuitems/menuitems.component.ts:23
|
||||||||||||
Parameters :
|
addToCart | ||||||
addToCart(menuItem: MenuItem)
|
||||||
Defined in src/app/menuitems/menuitems.component.ts:52
|
||||||
Parameters :
Returns :
void
|
fetchMenuItems |
fetchMenuItems()
|
Defined in src/app/menuitems/menuitems.component.ts:44
|
Returns :
void
|
ngOnInit |
ngOnInit()
|
Defined in src/app/menuitems/menuitems.component.ts:32
|
Returns :
void
|
Private _isLoggedIn |
Type : boolean
|
Default value : false
|
Defined in src/app/menuitems/menuitems.component.ts:23
|
cartId |
Type : string
|
Default value : ''
|
Defined in src/app/menuitems/menuitems.component.ts:17
|
cartItems |
Type : CartItem[]
|
Default value : []
|
Defined in src/app/menuitems/menuitems.component.ts:21
|
menuId |
Type : string
|
Default value : ''
|
Defined in src/app/menuitems/menuitems.component.ts:16
|
menuItems |
Type : MenuItem[]
|
Default value : []
|
Defined in src/app/menuitems/menuitems.component.ts:15
|
restaurantName |
Type : string
|
Defined in src/app/menuitems/menuitems.component.ts:18
|
role |
Type : string | any
|
Defined in src/app/menuitems/menuitems.component.ts:20
|
userId |
Type : string
|
Defined in src/app/menuitems/menuitems.component.ts:19
|
isLoggedIn |
getisLoggedIn()
|
Defined in src/app/menuitems/menuitems.component.ts:76
|
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { HttpClient } from '@angular/common/http';
import { MenuItem } from '../MenuItem';
import { User } from '../User';
import { CartItem } from '../Cart';
import { CartService } from '../cart.service';
@Component({
selector: 'app-menuitems',
templateUrl: './menuitems.component.html',
styleUrls: ['./menuitems.component.css']
})
export class MenuItemsComponent implements OnInit {
menuItems: MenuItem[] = [];
menuId: string = '';
cartId: string = '';
restaurantName: string;
userId!: string;
role: string | any;
cartItems:CartItem[]=[];
private _isLoggedIn: boolean = false; // private backing field for isLoggedIn
constructor(private route: ActivatedRoute, private http: HttpClient,private cartService: CartService) {
const token = localStorage.getItem('token');
if (token) {
this._isLoggedIn = true;
}
}
ngOnInit() {
this.route.params.subscribe(params => {
this.restaurantName = params['restaurantName'];
this.menuId = params['menuId'];
this.userId = params['userId'];
this.cartId = params['cartId'];
this.fetchMenuItems();
});
}
fetchMenuItems() {
this.http.get<MenuItem[]>(`http://localhost:3000/restaurants/${this.restaurantName}/${this.userId}/menuitems`)
.subscribe(data => {
this.menuItems = data;
console.log(this.menuItems)
});
}
addToCart(menuItem: MenuItem) {
const requestBody = {
user:this.userId,
cart:this.cartId,
menuItem: menuItem._id,
quantity: menuItem.quantity
};
this.http.post(`http://localhost:3000/cart`, requestBody)
.subscribe(
response => {
console.log(this.userId);
console.log(menuItem)
alert('Item added to cart successfully')
console.log('Item added to cart successfully', response);
},
error => {
alert('Error Item added to cart ')
console.error('Error adding item to cart', error);
}
);
}
get isLoggedIn() {
return this._isLoggedIn; // return the private backing field
}
}
<div class="container">
<h1 class="mb-4">
<i class="bi bi-list"></i> Menu Items
</h1>
<table class="table">
<thead>
<tr>
<th><i class="bi bi-card-list"></i> Name</th>
<th><i class="bi bi-cash"></i> Price</th>
<th><i class="bi bi-graph-up"></i> Quantity</th>
<th></th>
</tr>
</thead>
<tbody>
<ng-container *ngIf="isLoggedIn">
<tr *ngFor="let menuItem of menuItems">
<td>{{ menuItem.name }}</td>
<td>{{ menuItem.price | currency : 'INR' }}</td>
<td>
<input class="form-control" type="number" [(ngModel)]="menuItem.quantity" min="1" max="10" step="1">
</td>
<td>
<button class="btn btn-primary" (click)="addToCart(menuItem)">
<i class="bi bi-cart-plus"></i> Add to Cart
</button>
</td>
</tr>
</ng-container>
</tbody>
</table>
<button class="btn btn-secondary mt-3" [routerLink]="['/cart', userId]">
<i class="bi bi-cart"></i> Show Cart
</button>
</div>
./menuitems.component.css
.container {
margin: 20px;
padding: 20px;
background-color: #f8f9fa;
border: 1px solid #dee2e6;
border-radius: 5px;
}
h1 {
font-size: 24px;
font-weight: bold;
margin-bottom: 20px;
}
ul.list-group {
margin-bottom: 20px;
}
.list-group-item {
display: flex;
align-items: center;
justify-content: space-between;
padding: 10px;
}
label {
margin-right: 10px;
}
input.form-control {
width: 70px;
}
.item-row {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 10px;
}
.item-name {
flex-grow: 1;
}
.item-price {
flex-shrink: 0;
margin-left: 10px;
}
.item-controls {
display: flex;
align-items: center;
}
.label {
margin-right: 10px;
}
button.btn {
margin-left: 10px;
}