Add a mat-toolbar element to the safe-page.component. Add a button with the 'add' icon to this toolbar.
...
<mat-toolbar>
<button mat-mini-fab color="primary">
<mat-icon aria-label="add new Safe Items">add</mat-icon>
</button>
</mat-toolbar>
...
Generate dialog component
ng g component views/user/containers/addSafeItemDialog --export --changeDetection OnPush --entryComponent
// ...
import { MatDialog } from '@angular/material';
// ...
private dialogService: MatDialog,
// ...
addSafeItem() {
this.dialogService.open(AddSafeItemDialogComponent);
}
Create form component:
ng g component views/user/components/SafeItemForm --export --changeDetection OnPush
add-safe-item-dialog.component.html
<cool-safe-item-form></cool-safe-item-form>
<h1>Please insert name and price of the item</h1>
<form (ngSubmit)="onSubmit()" #safeitemForm="ngForm">
<div>
<mat-form-field>
<input
autocomplete="section-item name"
#name="ngModel"
matInput
placeholder="name"
required
aria-required="true"
[(ngModel)]="model.name"
type="text"
name="name"
class="form-control"
id="name"
/>
<mat-error
*ngIf="(name.invalid || !name.pristine) && name.getError('required')"
>required</mat-error
>
</mat-form-field>
<mat-form-field>
<input
autocomplete="section-item price"
#price="ngModel"
matInput
required
placeholder="price"
pattern="[0-9]*"
aria-required="true"
[(ngModel)]="model.price"
type="text"
name="price"
class="form-control"
id="price"
/>
<span matPrefix>€ </span>
<span matSuffix>.00</span>
<mat-error
*ngIf="(price.invalid || !price.pristine) && price.getError('required')"
>required</mat-error
>
<mat-error *ngIf="price.invalid || !price.pristine "
></mat-error
>
</mat-form-field>
<button
[disabled]="!safeitemForm.form.valid"
mat-raised-button
color="primary"
type="submit"
>
Submit
</button>
</div>
</form>
form {
display: flex;
flex-direction: column;
}
mat-form-field {
margin-bottom: 10px;
}
import {
Component,
EventEmitter,
OnInit,
ChangeDetectionStrategy,
Input,
Output
} from "@angular/core";
import { SafeItem } from "~core/model";
@Component({
selector: "cool-safe-item-form",
templateUrl: "./safe-item-form.component.html",
styleUrls: ["./safe-item-form.component.scss"],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class SafeItemFormComponent implements OnInit {
@Output()
result: EventEmitter<SafeItem> = new EventEmitter();
model: SafeItem = {} as SafeItem;
constructor() {}
ngOnInit() {}
onSubmit() {
this.result.emit(this.model);
}
}
Implement the addItem function in safe.service.ts:
Unit Tests:
// TODO
addItem(safeId: string, item: SafeItem): any {
if (!this.items.has(safeId)) {
this.items.set(safeId, new BehaviorSubject<SafeItem[]>(null));
}
const oldItems = this.items.get(safeId).getValue();
if (!!oldItems) {
const newItems = [...oldItems, item];
this.items.get(safeId).next(newItems);
} else {
this.items.get(safeId).next([item]);
}
}
Hint: subscribe to the afterClosed Observable of the dialogRef.
safe.component.ts
addSafeItem() {
const dialogRef = this.dialogService.open(AddSafeItemDialogComponent, {
height: '400px',
width: '600px',
});
dialogRef
.afterClosed()
.pipe(withLatestFrom(this.safe$))
.subscribe(([result, safe]: [SafeItem, Safe]) => {
console.log(`Dialog result: ${result}`);
if (result) {
this.service.addItem(safe.id, result);
}
});
}
Add close function to the dialog: add-safe-item-dialog.component.ts
export class AddSafeItemDialogComponent implements OnInit {
constructor(public dialogRef: MatDialogRef<AddSafeItemDialogComponent>) {}
ngOnInit() {}
closeDialog(safeItem: SafeItem) {
this.dialogRef.close(safeItem);
}
}
add-safe-item-dialog.component.html
<cool-safe-item-form (result)="closeDialog($event)"></cool-safe-item-form>