Files
brewman/overlord/src/app/incentive/incentive.component.ts
T
tanshu 5ea09df272 Prettied, Linted and updated angular.json according to the latest schematic of Angular CLI.
Now all that is needed is to make it ready for strict compiling.
Removed eslint-plugin-prettier as it is not recommended and causes errors for both eslint and prettier

Bumped to v8.0.0
2020-10-10 08:45:05 +05:30

209 lines
5.5 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import { FormArray, FormBuilder, FormGroup } from '@angular/forms';
import { MatDialog } from '@angular/material/dialog';
import { ActivatedRoute, Router } from '@angular/router';
import * as moment from 'moment';
import { BehaviorSubject, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { AuthService } from '../auth/auth.service';
import { Account } from '../core/account';
import { Incentive } from '../core/incentive';
import { ToasterService } from '../core/toaster.service';
import { Voucher } from '../core/voucher';
import { VoucherService } from '../core/voucher.service';
import { ConfirmDialogComponent } from '../shared/confirm-dialog/confirm-dialog.component';
import { IncentiveDataSource } from './incentive-datasource';
@Component({
selector: 'app-incentive',
templateUrl: './incentive.component.html',
styleUrls: ['./incentive.component.css'],
})
export class IncentiveComponent implements OnInit {
public incentiveObservable = new BehaviorSubject<Incentive[]>([]);
dataSource: IncentiveDataSource;
form: FormGroup;
voucher: Voucher;
account: Account;
accBal: any;
displayedColumns = ['name', 'designation', 'department', 'daysWorked', 'points', 'amount'];
accounts: Observable<Account[]>;
constructor(
private route: ActivatedRoute,
private router: Router,
private fb: FormBuilder,
private dialog: MatDialog,
private toaster: ToasterService,
public auth: AuthService,
private ser: VoucherService,
) {
this.createForm();
this.listenToDateChange();
}
ngOnInit() {
this.route.data.subscribe((data: { voucher: Voucher }) => {
this.loadVoucher(data.voucher);
});
}
loadVoucher(voucher) {
this.voucher = voucher;
this.form.get('date').setValue(moment(this.voucher.date, 'DD-MMM-YYYY').toDate());
this.form.setControl(
'incentives',
this.fb.array(
this.voucher.incentives.map((x) =>
this.fb.group({
points: x.points,
}),
),
),
);
this.dataSource = new IncentiveDataSource(this.incentiveObservable);
this.incentiveObservable.next(this.voucher.incentives);
}
createForm() {
this.form = this.fb.group({
date: '',
incentives: this.fb.array([]),
});
}
listenToDateChange(): void {
this.form
.get('date')
.valueChanges.pipe(map((x) => moment(x).format('DD-MMM-YYYY')))
.subscribe((x) => {
if (x !== this.voucher.date && !this.voucher.id) {
return this.ser.getIncentive(x).subscribe((voucher: Voucher) => {
this.loadVoucher(voucher);
});
}
return '';
});
}
totalPoints() {
return this.voucher.incentives
.map((item) => item.daysWorked * item.points)
.reduce((sum, item) => sum + item);
}
pointValue() {
return Math.round((this.voucher.incentive * 100) / this.totalPoints()) / 100;
}
less(row: Incentive, i: number) {
if (row.points >= 1) {
row.points -= 1;
this.form
.get('incentives')
.get(`${i}`)
.setValue({ points: `${row.points}` });
}
}
change(row: Incentive, i: number) {
row.points = +this.form.get('incentives').get(`${i}`).get('points').value;
this.form
.get('incentives')
.get(`${i}`)
.setValue({ points: `${row.points}` });
}
more(row: Incentive, i: number) {
row.points += 1;
this.form
.get('incentives')
.get(`${i}`)
.setValue({ points: `${row.points}` });
}
amount(row) {
return row.points * row.daysWorked * this.pointValue();
}
canSave() {
if (!this.voucher.id) {
return true;
}
if (this.voucher.posted && this.auth.user.perms.indexOf('edit-posted-vouchers') !== -1) {
return true;
}
return (
this.voucher.user.id === this.auth.user.id ||
this.auth.user.perms.indexOf("edit-other-user's-vouchers") !== -1
);
}
post() {
this.ser.post(this.voucher.id).subscribe(
(result) => {
this.loadVoucher(result);
this.toaster.show('Success', 'Voucher Posted');
},
(error) => {
this.toaster.show('Danger', error);
},
);
}
save() {
const voucher: Voucher = this.getVoucher();
this.ser.saveOrUpdate(voucher).subscribe(
(result) => {
if (voucher.id === result.id) {
this.loadVoucher(result);
} else {
this.router.navigate(['/incentive', result.id]);
}
},
(error) => {
this.toaster.show('Danger', error);
},
);
}
getVoucher(): Voucher {
const formModel = this.form.value;
this.voucher.date = moment(formModel.date).format('DD-MMM-YYYY');
const array = this.form.get('incentives') as FormArray;
this.voucher.incentives.forEach((item, index) => {
item.points = array.controls[index].value.points;
});
return this.voucher;
}
delete() {
this.ser.delete(this.voucher.id).subscribe(
() => {
this.toaster.show('Success', '');
this.router.navigate(['/incentive'], { replaceUrl: true });
},
(error) => {
this.toaster.show('Danger', error);
},
);
}
confirmDelete(): void {
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
width: '250px',
data: { title: 'Delete Voucher?', content: 'Are you sure? This cannot be undone.' },
});
dialogRef.afterClosed().subscribe((result: boolean) => {
if (result) {
this.delete();
}
});
}
}