Added feature: Split bill by Sales Category if no item is selected.

This commit is contained in:
2020-12-25 11:47:03 +05:30
parent 6257313792
commit 41d4ef1200
7 changed files with 204 additions and 45 deletions

View File

@ -0,0 +1,4 @@
.right {
display: flex;
justify-content: flex-end;
}

View File

@ -0,0 +1,23 @@
<h2 mat-dialog-title>Select Sale Categories</h2>
<mat-dialog-content>
<form [formGroup]="form" fxLayout="column">
<div formArrayName="saleCategories">
<div
fxLayout="row"
*ngFor="let sc of list; index as i"
[formGroupName]="i"
fxLayout="row"
fxLayoutAlign="space-around start"
fxLayout.lt-md="column"
fxLayoutGap="20px"
fxLayoutGap.lt-md="0px"
>
<mat-checkbox formControlName="saleCategory" fxFlex>{{ sc.name }}</mat-checkbox>
</div>
</div>
</form>
</mat-dialog-content>
<mat-dialog-actions align="end">
<button mat-button [mat-dialog-close]="false">Cancel</button>
<button mat-button (click)="accept()" color="primary">Ok</button>
</mat-dialog-actions>

View File

@ -0,0 +1,26 @@
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { SplitBillComponent } from './split-bill.component';
describe('SplitBillComponent', () => {
let component: SplitBillComponent;
let fixture: ComponentFixture<SplitBillComponent>;
beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [SplitBillComponent],
}).compileComponents();
}),
);
beforeEach(() => {
fixture = TestBed.createComponent(SplitBillComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@ -0,0 +1,46 @@
import { Component, Inject } from '@angular/core';
import { FormArray, FormBuilder, FormGroup } from '@angular/forms';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { Observable } from 'rxjs';
@Component({
selector: 'app-split-bill',
templateUrl: './split-bill.component.html',
styleUrls: ['./split-bill.component.css'],
})
export class SplitBillComponent {
list: { id: string; name: string; selected: boolean }[] = [];
form: FormGroup;
constructor(
public dialogRef: MatDialogRef<SplitBillComponent>,
private fb: FormBuilder,
@Inject(MAT_DIALOG_DATA)
public data: Observable<{ id: string; name: string; selected: boolean }[]>,
) {
this.form = this.fb.group({
saleCategories: this.fb.array([]),
});
this.data.subscribe((list: { id: string; name: string; selected: boolean }[]) => {
this.list = list;
this.form.setControl(
'saleCategories',
this.fb.array(
this.list.map((x) =>
this.fb.group({
saleCategory: x.selected,
}),
),
),
);
});
}
accept(): void {
const array = this.form.get('saleCategories') as FormArray;
this.list.forEach((item, index) => {
item.selected = array.controls[index].value.saleCategory;
});
this.dialogRef.close(this.list);
}
}