feat: add edit cashier component (#802)

Fixes: #762
This commit is contained in:
Benura Abeywardena
2020-07-12 12:32:55 +05:30
committed by GitHub
parent 1b0aa6c28e
commit a977cb6e0c
9 changed files with 265 additions and 4 deletions
@@ -0,0 +1,83 @@
<div class="container">
<mat-card>
<form [formGroup]="editCashierForm" (ngSubmit)="submit()">
<mat-card-content>
<div fxLayout="row wrap" fxLayoutGap="2%" fxLayout.lt-md="column">
<mat-form-field fxFlex="48%">
<mat-label>Office</mat-label>
<input matInput required autofocus [value]="cashierData.template.officeName" disabled>
</mat-form-field>
<mat-form-field fxFlex="48%">
<mat-label>Teller Name</mat-label>
<input matInput required autofocus [value]="cashierData.data.tellerName" disabled>
</mat-form-field>
<mat-form-field fxFlex="48%" *ngIf="isStaffId">
<mat-label>Cashier</mat-label>
<mat-select formControlName="staffId">
<mat-option *ngFor="let staff of cashierData.template.staffOptions" [value]="staff.id">
{{ staff.displayName }}
</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field fxFlex="48%" *ngIf="!isStaffId">
<mat-label>Cashier</mat-label>
<mat-select formControlName="staffId" *ngIf="!isStaffId">
<mat-option [value]="cashierData.data.staffId">
Select Option
</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field fxFlex="48%">
<mat-label>Description/Notes</mat-label>
<input matInput autofocus formControlName="description">
</mat-form-field>
<mat-form-field fxFlex="48%">
<mat-label>From</mat-label>
<input matInput [min]="minDate" [max]="maxDate" [matDatepicker]="fromDatePicker" required formControlName="startDate">
<mat-datepicker-toggle matSuffix [for]="fromDatePicker"></mat-datepicker-toggle>
<mat-datepicker #fromDatePicker></mat-datepicker>
<mat-error *ngIf="editCashierForm.controls.startDate.hasError('required')">
From Date <strong>is required</strong>
</mat-error>
</mat-form-field>
<mat-form-field fxFlex="48%">
<mat-label>To</mat-label>
<input matInput [min]="minDate" [max]="maxDate" [matDatepicker]="toDatePicker" required formControlName="endDate">
<mat-datepicker-toggle matSuffix [for]="toDatePicker"></mat-datepicker-toggle>
<mat-datepicker #toDatePicker></mat-datepicker>
<mat-error *ngIf="editCashierForm.controls.endDate.hasError('required')">
To Date <strong>is required</strong>
</mat-error>
</mat-form-field>
<div fxFlex="48%" style="padding-top: 10px;">
<mat-checkbox labelPosition="before" formControlName="isFullDay">
Full Day?
</mat-checkbox>
</div>
</div>
</mat-card-content>
<mat-card-actions fxLayout="row" fxLayout.xs="column" fxLayoutAlign="center" fxLayoutGap="5px">
<button type="button" mat-raised-button [routerLink]="['../']">Cancel</button>
<button mat-raised-button color="primary" [disabled]="!editCashierForm.valid">Submit</button>
</mat-card-actions>
</form>
</mat-card>
</div>
@@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { EditCashierComponent } from './edit-cashier.component';
describe('EditCashierComponent', () => {
let component: EditCashierComponent;
let fixture: ComponentFixture<EditCashierComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ EditCashierComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(EditCashierComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
@@ -0,0 +1,88 @@
/** Angular Imports. */
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { DatePipe } from '@angular/common';
/** Custom Services. */
import { OrganizationService } from 'app/organization/organization.service';
/**
* Edit Cashier component.
*/
@Component({
selector: 'mifosx-edit-cashier',
templateUrl: './edit-cashier.component.html',
styleUrls: ['./edit-cashier.component.scss']
})
export class EditCashierComponent implements OnInit {
/** Cashier Data. */
cashierData: any = new Object();
/** Edit cashier form. */
editCashierForm: FormGroup;
/** Is Staff ID present. */
isStaffId = true;
/** Minimum Date allowed. */
minDate = new Date(2000, 0, 1);
/** Maximum Date allowed. */
maxDate = new Date();
/**
*
* @param {FormBuilder} formBuilder Form Builder.
* @param {ActivatedRoute} route Activated Route.
* @param {Router} router Router.
* @param {DatePipe} datePipe Date Pipe.
* @param {OrganizationService} organizationService Organization Service.
*/
constructor(private formBuilder: FormBuilder,
private route: ActivatedRoute,
private router: Router,
private datePipe: DatePipe,
private organizationService: OrganizationService ) {
this.route.data.subscribe((data: { cashier: any, cashierTemplate: any }) => {
this.cashierData.data = data.cashier;
this.cashierData.template = data.cashierTemplate;
this.isStaffId = this.cashierData.template.staffOptions.some((element: any) => element.id === this.cashierData.data.staffId);
});
}
ngOnInit() {
this.setEditChargeForm();
}
/**
* Sets Edit Charge Form.
*/
setEditChargeForm() {
this.editCashierForm = this.formBuilder.group({
'staffId': [{value: this.cashierData.data.staffId, disabled: true}],
'description': [this.cashierData.data.description],
'startDate': [this.cashierData.data.startDate && new Date(this.cashierData.data.startDate), Validators.required],
'endDate': [this.cashierData.data.endDate && new Date(this.cashierData.data.endDate), Validators.required],
'isFullDay': [this.cashierData.data.isFullDay, Validators.required]
});
}
/**
* Submits edit cashier form.
*/
submit() {
const dateFormat = 'yyyy-MM-dd';
const startDate = this.editCashierForm.value.startDate;
const endDate = this.editCashierForm.value.endDate;
this.editCashierForm.patchValue({
'startDate': this.datePipe.transform(startDate, dateFormat),
'endDate': this.datePipe.transform(endDate, dateFormat)
});
const editCashierForm = this.editCashierForm.value;
editCashierForm.locale = 'en';
editCashierForm.dateFormat = dateFormat;
editCashierForm.staffId = this.cashierData.data.staffId;
this.organizationService.updateCashier(this.cashierData.data.tellerId, this.cashierData.data.id, editCashierForm).subscribe((response: any) => {
this.router.navigate(['../'], {relativeTo: this.route});
});
}
}
@@ -0,0 +1,31 @@
/** Angular Imports */
import { Injectable } from '@angular/core';
import { Resolve, ActivatedRouteSnapshot } from '@angular/router';
/** rxjs Imports */
import { Observable } from 'rxjs';
/** Custom Services */
import { OrganizationService } from 'app/organization/organization.service';
/**
* Cashier Template resolver.
*/
@Injectable()
export class EditCashierResolver implements Resolve<Object> {
/**
* @param {OrganizationService} organizationService Organization service.
*/
constructor(private organizationService: OrganizationService) {}
/**
* Returns the cashier template.
* @returns {Observable<any>}
*/
resolve(route: ActivatedRouteSnapshot): Observable<any> {
const tellerId = route.parent.paramMap.get('id');
return this.organizationService.getCashierTemplate(tellerId);
}
}
@@ -1,5 +1,5 @@
<div fxLayout="row" fxLayoutAlign="end" fxLayoutGap="2%" fxLayout.lt-md="column" class="container m-b-20">
<button mat-raised-button color="primary">
<button mat-raised-button color="primary" [routerLink]="['edit']">
<fa-icon icon="edit"></fa-icon>&nbsp;&nbsp;
Edit
</button>