File

src/app/menuitems/menuitems.component.ts

Implements

OnInit

Metadata

Index

Properties
Methods
Accessors

Constructor

constructor(route: ActivatedRoute, http: HttpClient, cartService: CartService)
Parameters :
Name Type Optional
route ActivatedRoute No
http HttpClient No
cartService CartService No

Methods

addToCart
addToCart(menuItem: MenuItem)
Parameters :
Name Type Optional
menuItem MenuItem No
Returns : void
fetchMenuItems
fetchMenuItems()
Returns : void
ngOnInit
ngOnInit()
Returns : void

Properties

Private _isLoggedIn
Type : boolean
Default value : false
cartId
Type : string
Default value : ''
cartItems
Type : CartItem[]
Default value : []
menuId
Type : string
Default value : ''
menuItems
Type : MenuItem[]
Default value : []
restaurantName
Type : string
role
Type : string | any
userId
Type : string

Accessors

isLoggedIn
getisLoggedIn()
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;
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""