File
Index
Properties
|
|
Methods
|
|
Accessors
|
|
Constructor
constructor(http: HttpClient, router: Router, authService: AuthService)
|
|
Parameters :
Name |
Type |
Optional |
http |
HttpClient
|
No
|
router |
Router
|
No
|
authService |
AuthService
|
No
|
|
Methods
addRestaurant
|
addRestaurant()
|
|
|
Private
_isLoggedIn
|
Type : boolean
|
Default value : false
|
|
menuItems
|
Type : MenuItem[]
|
|
restaurants
|
Type : Restaurant[]
|
|
Accessors
isLoggedIn
|
getisLoggedIn()
|
|
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Restaurant } from '../Restaurant';
import { Router } from '@angular/router';
import { AuthService } from '../auth.service';
import { MenuItem } from '../MenuItem';
@Component({
selector: 'app-add-restaurant',
templateUrl: './add-restaurant.component.html',
styleUrls: ['./add-restaurant.component.css']
})
export class AddRestaurantComponent {
name: string;
owner: string;
restaurants: Restaurant[];
menuItems:MenuItem[];
private _isLoggedIn: boolean = false; // private backing field for isLoggedIn
constructor(private http: HttpClient, private router: Router, private authService: AuthService) {
if (this.authService.isLoggedIn()) {
this._isLoggedIn = true;
this.authService.getRestaurants().subscribe(
(restaurants) => {
this.restaurants = restaurants;
},
(error) => {
console.log(error);
}
);
}
}
addRestaurant() {
const restaurantData = {
name: this.name,
owner: this.owner,
menuItems: this.menuItems // Initialize an empty array for menuItems
};
this.http.post<Restaurant>('http://localhost:3000/restaurants', restaurantData)
.subscribe(
response => {
console.log('Restaurant added:', response);
// Handle success, e.g., show a success message
// Optionally, you can add the newly added restaurant to the existing array
this.restaurants.push(response);
},
error => {
console.error('Error adding restaurant:', error);
// Handle error, e.g., show an error message
}
);
}
get isLoggedIn() {
return this._isLoggedIn; // return the private backing field
}
}
<div class="alert alert-primary" role="alert" *ngIf="!isLoggedIn">
<i class="fas fa-info-circle mr-2"></i> Please login to view available restaurants.
</div>
<div *ngIf="isLoggedIn">
<h2>Add Restaurant</h2>
<form (submit)="addRestaurant()">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" id="name" [(ngModel)]="name" name="name" class="form-control" required>
</div>
<div class="form-group">
<label for="owner">Owner:</label>
<input type="text" id="owner" [(ngModel)]="owner" name="owner" class="form-control" required>
</div>
<button type="submit" class="btn btn-primary">Add Restaurant</button>
</form>
</div>
/* style.css */
/* Center the form on the page */
.container {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 50px;
}
/* Add spacing between form elements */
.container .form-group {
margin-bottom: 20px;
}
/* Style the submit button */
.container .btn {
width: 200px;
margin-top: 10px;
}
/* Add margin to the alert message */
.alert {
margin-top: 20px;
margin-bottom: 20px;
}
/* Style the heading */
h2 {
margin-bottom: 30px;
}
/* Style the label */
label {
font-weight: bold;
}
/* Add spacing to the input fields */
input {
margin-bottom: 10px;
padding: 8px;
}
Legend
Html element with directive