feat: add create employee component (#527)

Fixes: #157
This commit is contained in:
punwai
2020-01-01 11:24:44 +03:00
committed by Abhay Chawla
parent 0991df4b18
commit 8fc6dba1a9
8 changed files with 227 additions and 7 deletions
@@ -0,0 +1,77 @@
<div class="container">
<mat-card>
<form [formGroup]="employeeForm" (ngSubmit)="submit()">
<mat-card-content>
<div fxLayout="column">
<mat-form-field>
<mat-label>Office</mat-label>
<mat-select required formControlName="officeId">
<mat-option *ngFor="let office of officeData" [value]="office.id">
{{ office.name }}
</mat-option>
</mat-select>
<mat-error *ngIf="employeeForm.controls.officeId.hasError('required')">
Office is <strong>required</strong>
</mat-error>
</mat-form-field>
<mat-form-field>
<mat-label>First Name</mat-label>
<input matInput required formControlName="firstname">
<mat-error *ngIf="employeeForm.controls.firstname.hasError('required')">
First Name is <strong>required</strong>
</mat-error>
<mat-error *ngIf="employeeForm.controls.firstname.hasError('pattern')">
First Name <strong>cannot</strong> begin with a special character or number
</mat-error>
</mat-form-field>
<mat-form-field>
<mat-label>Last Name</mat-label>
<input matInput required formControlName="lastname">
<mat-error *ngIf="employeeForm.controls.lastname.hasError('required')">
Last Name is <strong>required</strong>
</mat-error>
<mat-error *ngIf="employeeForm.controls.lastname.hasError('pattern')">
Last Name <strong>cannot</strong> begin with a special character or number
</mat-error>
</mat-form-field>
<mat-checkbox labelPosition="before" formControlName="isLoanOfficer" class="loan-officer">
Is Loan Officer
</mat-checkbox>
<mat-form-field>
<mat-label>Mobile Number for SMS</mat-label>
<input matInput formControlName="mobileNo">
</mat-form-field>
<mat-form-field>
<mat-label>Joining Date</mat-label>
<input matInput [min]="minDate" [max]="maxDate" [matDatepicker]="joiningDatePicker" required formControlName="joiningDate">
<mat-datepicker-toggle matSuffix [for]="joiningDatePicker"></mat-datepicker-toggle>
<mat-datepicker #joiningDatePicker></mat-datepicker>
<mat-error *ngIf="employeeForm.controls.joiningDate.hasError('required')">
Joining Date is <strong>required</strong>
</mat-error>
</mat-form-field>
</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]="!employeeForm.valid">Submit</button>
</mat-card-actions>
</form>
</mat-card>
</div>
@@ -0,0 +1,7 @@
.container {
max-width: 37rem;
}
.loan-officer {
padding: 17.5px 0 17.5px 0;
}
@@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { CreateEmployeeComponent } from './create-employee.component';
describe('CreateEmployeeComponent', () => {
let component: CreateEmployeeComponent;
let fixture: ComponentFixture<CreateEmployeeComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ CreateEmployeeComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(CreateEmployeeComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
@@ -0,0 +1,87 @@
/** Angular Imports */
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { Router, ActivatedRoute } from '@angular/router';
import { DatePipe } from '@angular/common';
/** Custom Services */
import { OrganizationService } from '../../organization.service';
/**
* Create employee component.
*/
@Component({
selector: 'mifosx-create-employee',
templateUrl: './create-employee.component.html',
styleUrls: ['./create-employee.component.scss']
})
export class CreateEmployeeComponent implements OnInit {
/** Minimum joining date allowed. */
minDate = new Date(2000, 0, 1);
/** Maximum joining date allowed. */
maxDate = new Date();
/** Employee form. */
employeeForm: FormGroup;
/** Office data. */
officeData: any;
/**
* Retrieves the offices data from `resolve`.
* @param {FormBuilder} formBuilder Form Builder.
* @param {OrganizationService} organizationService Organization Service.
* @param {ActivatedRoute} route Activated Route.
* @param {Router} router Router for navigation.
* @param {DatePipe} datePipe Date Pipe to format date.
*/
constructor(private formBuilder: FormBuilder,
private organizationService: OrganizationService,
private route: ActivatedRoute,
private router: Router,
private datePipe: DatePipe) {
this.route.data.subscribe((data: { offices: any }) => {
this.officeData = data.offices;
});
}
/**
* Creates the employee form.
*/
ngOnInit() {
this.createEmployeeForm();
}
/**
* Creates the employee form.
*/
createEmployeeForm() {
this.employeeForm = this.formBuilder.group({
'officeId': ['', Validators.required],
'firstname': ['', [Validators.required, Validators.pattern('(^[A-z]).*')]],
'lastname': ['', [Validators.required, Validators.pattern('(^[A-z]).*')]],
'isLoanOfficer': [false],
'mobileNo': [''],
'joiningDate': ['', Validators.required],
});
}
/**
* Submits the employee form and creates employee,
* if successful redirects to employees.
*/
submit() {
const prevJoiningDate: Date = this.employeeForm.value.joiningDate;
// TODO: Update once language and date settings are setup
const dateFormat = 'yyyy-MM-dd';
this.employeeForm.patchValue({
joiningDate: this.datePipe.transform(prevJoiningDate, dateFormat)
});
const employee = this.employeeForm.value;
employee.locale = 'en';
employee.dateFormat = dateFormat;
this.organizationService.createEmployee(employee).subscribe((response: any) => {
this.router.navigate(['../'], { relativeTo: this.route });
});
}
}
@@ -5,9 +5,9 @@
Import Employees
</button>
<button mat-raised-button color="primary">
<button mat-raised-button color="primary" [routerLink]="['create']">
<fa-icon icon="plus"></fa-icon>&nbsp;&nbsp;
Create Employees
Create Employee
</button>
</div>
@@ -13,6 +13,7 @@ import { OrganizationComponent } from './organization.component';
import { LoanProvisioningCriteriaComponent } from './loan-provisioning-criteria/loan-provisioning-criteria.component';
import { OfficesComponent } from './offices/offices.component';
import { EmployeesComponent } from './employees/employees.component';
import { CreateEmployeeComponent } from './employees/create-employee/create-employee.component';
import { CurrenciesComponent } from './currencies/currencies.component';
import { SmsCampaignsComponent } from './sms-campaigns/sms-campaigns.component';
import { AdhocQueryComponent } from './adhoc-query/adhoc-query.component';
@@ -78,11 +79,24 @@ const routes: Routes = [
},
{
path: 'employees',
component: EmployeesComponent,
data: { title: extract('Manage Employees'), breadcrumb: 'Manage Employees' },
resolve: {
employees: EmployeesResolver
}
children: [
{
path: '',
component: EmployeesComponent,
resolve: {
employees: EmployeesResolver
}
},
{
path: 'create',
component: CreateEmployeeComponent,
data: { title: extract('Create Employee'), breadcrumb: 'Create Employee' },
resolve: {
offices: OfficesResolver
}
}
]
},
{
path: 'currencies',
+3 -1
View File
@@ -20,6 +20,7 @@ import { PasswordPreferencesComponent } from './password-preferences/password-pr
import { EntityDataTableChecksComponent } from './entity-data-table-checks/entity-data-table-checks.component';
import { WorkingDaysComponent } from './working-days/working-days.component';
import { CreateOfficeComponent } from './offices/create-office/create-office.component';
import { CreateEmployeeComponent } from './employees/create-employee/create-employee.component';
/**
* Organization Module
@@ -45,7 +46,8 @@ import { CreateOfficeComponent } from './offices/create-office/create-office.com
PasswordPreferencesComponent,
EntityDataTableChecksComponent,
WorkingDaysComponent,
CreateOfficeComponent
CreateOfficeComponent,
CreateEmployeeComponent
]
})
export class OrganizationModule { }
@@ -47,6 +47,14 @@ export class OrganizationService {
return this.http.get('/staff');
}
/**
* @param {any} employee Employee to be created.
* @returns {Observable<any>}
*/
createEmployee(employee: any): Observable<any> {
return this.http.post('/staff', employee);
}
/**
* @returns {Observable<any>} Currencies data
*/