Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saved new item when dialog called from main menu #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion angular-app/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<button mat-menu-item routerLink="/items">Lists</button>
<button mat-menu-item (click)="openCreateItemDialog()">Create New Item</button>
</mat-menu>

<main>
<router-outlet></router-outlet>
</main>
16 changes: 10 additions & 6 deletions angular-app/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,25 @@ import { RouterModule } from '@angular/router';
import { MatDialog } from '@angular/material/dialog';
import { CreateItemComponent } from './create-item/create-item.component';
import { Router } from '@angular/router';
import { ItemComponent } from './item/item.component';
import { Item } from './item/item';

@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, CommonModule, MatMenuModule, MatButtonModule, RouterModule],
imports: [RouterOutlet, CommonModule, MatMenuModule, MatButtonModule, RouterModule, ItemComponent],
templateUrl: './app.component.html',
styleUrl: './app.component.css'
})
export class AppComponent implements OnInit {
title = 'mangodb-angualr';
title = 'mangodb-angular';
items: any[] = [];
newItem = { name: '', description: '' };
isLoading : boolean = false;
showCreateForm: boolean = false; // Toggle for the create form

constructor(private dialog: MatDialog, private router: Router) {}
item : Item | null = null;

constructor(private dialog: MatDialog, private router: Router, private itemService: ItemService) {}

ngOnInit(): void {
}
Expand All @@ -35,8 +38,9 @@ export class AppComponent implements OnInit {
});

dialogRef.afterClosed().subscribe((result) => {
//redirecting to the items page
this.router.navigate(['/items']);
this.itemService.addItem(result).subscribe((item) => {
this.router.navigate(['/items']);
});
});
}
}
6 changes: 2 additions & 4 deletions angular-app/src/app/item/item.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, inject, OnInit, ViewChild } from '@angular/core';
import { Component, EventEmitter, inject, Input, OnInit, Output, SimpleChanges, ViewChild } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import { ItemService } from '../services/item.service';
import { CommonModule } from '@angular/common';
Expand Down Expand Up @@ -41,7 +41,6 @@ import {
export class ItemComponent implements OnInit {
title = 'mangodb-angualr';
items: Item[] = [];
newItem = { name: '', description: '' };
isLoading : boolean = false;
showCreateForm: boolean = false; // Toggle for the create form
itemToEdit: any = {
Expand All @@ -55,7 +54,7 @@ export class ItemComponent implements OnInit {

@ViewChild(MatPaginator) paginator!: MatPaginator;
@ViewChild(MatSort) sort!: MatSort;

constructor(private itemService: ItemService) {}

ngOnInit(): void {
Expand All @@ -73,7 +72,6 @@ export class ItemComponent implements OnInit {
}

addOrUpdateItem(newItem: Item): void {
console.log(newItem);
if (newItem._id) {
this.itemService.updateItem(newItem._id, newItem).subscribe((item) => {
this.loadItems();
Expand Down