Feature: Exit guard if bill is dirty is sales to prevent accidentally backing out of a bill with data.

This commit is contained in:
Amritanshu Agrawal 2021-06-29 08:58:07 +05:30
parent fd2659eee5
commit 0d41c0e345
6 changed files with 53 additions and 3 deletions

View File

@ -396,4 +396,9 @@ export class BillService {
}
return false;
}
public isDirty(): boolean {
const newKot = this.bill.kots.find((k) => k.id === undefined) as Kot;
return newKot.inventories.length !== 0;
}
}

View File

@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { CanDeactivateBillGuard } from './can-deactivate-bill.guard';
describe('CanDeactivateBillGuard', () => {
let guard: CanDeactivateBillGuard;
beforeEach(() => {
TestBed.configureTestingModule({});
guard = TestBed.inject(CanDeactivateBillGuard);
});
it('should be created', () => {
expect(guard).toBeTruthy();
});
});

View File

@ -0,0 +1,27 @@
import { Injectable } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { CanDeactivate } from '@angular/router';
import { Observable, of as observableOf } from 'rxjs';
import { map } from 'rxjs/operators';
import { ConfirmDialogComponent } from '../shared/confirm-dialog/confirm-dialog.component';
import { BillsComponent } from './bills/bills.component';
@Injectable({
providedIn: 'root',
})
export class CanDeactivateBillGuard implements CanDeactivate<BillsComponent> {
constructor(private dialog: MatDialog) {}
canDeactivate(component: BillsComponent): Observable<boolean> | boolean {
if (!component.bs.isDirty()) {
return observableOf(true);
}
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
width: '250px',
data: { title: 'Abandon Bill?', content: 'Current items will be lost.' },
});
return dialogRef.afterClosed().pipe(map((result: boolean) => result));
}
}

View File

@ -28,7 +28,7 @@ export class PaxComponent implements OnInit {
}
accept(): void {
const { pax } = this.form.value;
const pax = +this.form.value.pax;
this.dialogRef.close(pax);
}
}

View File

@ -6,6 +6,7 @@ import { AuthGuard } from '../auth/auth-guard.service';
import { BillResolver } from './bills/bill-resolver.service';
import { BillsComponent } from './bills/bills.component';
import { UpdateTableResolver } from './bills/update-table-resolver.service';
import { CanDeactivateBillGuard } from './can-deactivate-bill.guard';
import { SalesHomeComponent } from './home/sales-home.component';
import { MenuCategoriesResolver } from './menu-categories/menu-categories-resolver.service';
import { MenuCategoriesComponent } from './menu-categories/menu-categories.component';
@ -41,7 +42,7 @@ const routes: Routes = [
path: 'bill',
component: BillsComponent,
canActivate: [AuthGuard],
// canDeactivate: [CanDeactivateGuard],
canDeactivate: [CanDeactivateBillGuard],
data: {
permission: 'Sales',
},

View File

@ -24,6 +24,7 @@ import { BillNumberComponent } from './bill-number/bill-number.component';
import { BillTypeComponent } from './bill-type/bill-type.component';
import { BillService } from './bill.service';
import { BillsComponent } from './bills/bills.component';
import { CanDeactivateBillGuard } from './can-deactivate-bill.guard';
import { DiscountComponent } from './discount/discount.component';
import { SalesHomeComponent } from './home/sales-home.component';
import { MenuCategoriesComponent } from './menu-categories/menu-categories.component';
@ -39,7 +40,7 @@ import { SplitBillComponent } from './split-bill/split-bill.component';
import { TablesDialogComponent } from './tables-dialog/tables-dialog.component';
@NgModule({
providers: [BillService],
providers: [BillService, CanDeactivateBillGuard],
declarations: [
BillsComponent,
BillNumberComponent,