Chore: Prettier line length changed to 120 from 100 Fix: Hard coded the face as the primary color to make the buttons stand out
142 lines
4.3 KiB
TypeScript
142 lines
4.3 KiB
TypeScript
import { Component, OnInit } from '@angular/core';
|
|
import { FormArray, FormControl, FormGroup } from '@angular/forms';
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
import * as moment from 'moment';
|
|
import { map } from 'rxjs/operators';
|
|
|
|
import { MenuCategory } from '../core/menu-category';
|
|
import { ToasterService } from '../core/toaster.service';
|
|
import { ToCsvService } from '../shared/to-csv.service';
|
|
|
|
import { UpdateProductPrices } from './update-product-prices';
|
|
import { UpdateProductPricesDataSource } from './update-product-prices-datasource';
|
|
import { UpdateProductPricesService } from './update-product-prices.service';
|
|
|
|
@Component({
|
|
selector: 'app-update-product-prices',
|
|
templateUrl: './update-product-prices.component.html',
|
|
styleUrls: ['./update-product-prices.component.css'],
|
|
})
|
|
export class UpdateProductPricesComponent implements OnInit {
|
|
info: UpdateProductPrices = new UpdateProductPrices();
|
|
dataSource: UpdateProductPricesDataSource = new UpdateProductPricesDataSource(this.info.items);
|
|
form: FormGroup<{
|
|
date: FormControl<Date>;
|
|
menuCategory: FormControl<string>;
|
|
prices: FormArray<
|
|
FormGroup<{
|
|
newPrice: FormControl<number | null>;
|
|
}>
|
|
>;
|
|
}>;
|
|
|
|
menuCategories: MenuCategory[] = [];
|
|
|
|
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
|
|
displayedColumns = ['name', 'oldPrice', 'newPrice'];
|
|
|
|
constructor(
|
|
private route: ActivatedRoute,
|
|
private router: Router,
|
|
private toCsv: ToCsvService,
|
|
private toaster: ToasterService,
|
|
private ser: UpdateProductPricesService,
|
|
) {
|
|
// Create form
|
|
this.form = new FormGroup({
|
|
date: new FormControl(new Date(), { nonNullable: true }),
|
|
menuCategory: new FormControl('', { nonNullable: true }),
|
|
prices: new FormArray<
|
|
FormGroup<{
|
|
newPrice: FormControl<number | null>;
|
|
}>
|
|
>([]),
|
|
});
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.route.data
|
|
.pipe(
|
|
map((value) => {
|
|
const data = value as { menuCategories: MenuCategory[]; info: UpdateProductPrices };
|
|
data.menuCategories.unshift(new MenuCategory({ id: undefined, name: '-- All Categories --' }));
|
|
return data;
|
|
}),
|
|
)
|
|
.subscribe((data: { menuCategories: MenuCategory[]; info: UpdateProductPrices }) => {
|
|
this.menuCategories = data.menuCategories;
|
|
this.loadData(data.info);
|
|
});
|
|
}
|
|
|
|
loadData(info: UpdateProductPrices) {
|
|
this.info = info;
|
|
this.form.controls.date.setValue(moment(this.info.date, 'DD-MMM-YYYY').toDate());
|
|
this.form.controls.menuCategory.setValue(this.info.menuCategoryId !== undefined ? this.info.menuCategoryId : '');
|
|
this.form.controls.prices.clear();
|
|
|
|
this.info.items.forEach((x) =>
|
|
this.form.controls.prices.push(
|
|
new FormGroup({
|
|
newPrice: new FormControl<number | null>(x.newPrice),
|
|
}),
|
|
),
|
|
),
|
|
(this.dataSource = new UpdateProductPricesDataSource(this.info.items));
|
|
}
|
|
|
|
show() {
|
|
const info = this.getInfo();
|
|
const route = ['update-product-prices'];
|
|
if (info.menuCategoryId !== null) {
|
|
route.push(info.menuCategoryId as string);
|
|
}
|
|
this.router.navigate(route, {
|
|
queryParams: {
|
|
date: info.date,
|
|
},
|
|
});
|
|
}
|
|
|
|
getInfo(): UpdateProductPrices {
|
|
const formModel = this.form.value;
|
|
const array = this.form.controls.prices;
|
|
this.info.items.forEach((item, index) => {
|
|
item.newPrice = array.controls[index].value.newPrice ?? null;
|
|
});
|
|
|
|
return {
|
|
date: moment(formModel.date).format('DD-MMM-YYYY'),
|
|
menuCategoryId: formModel.menuCategory,
|
|
items: [...this.info.items],
|
|
};
|
|
}
|
|
|
|
exportCsv() {
|
|
const headers = {
|
|
Details: 'details',
|
|
};
|
|
const csvData = new Blob([this.toCsv.toCsv(headers, this.dataSource.data)], {
|
|
type: 'text/csv;charset=utf-8;',
|
|
});
|
|
const link = document.createElement('a');
|
|
link.href = window.URL.createObjectURL(csvData);
|
|
link.setAttribute('download', 'update-product-prices.csv');
|
|
document.body.appendChild(link);
|
|
link.click();
|
|
document.body.removeChild(link);
|
|
}
|
|
|
|
save() {
|
|
this.ser.save(this.getInfo()).subscribe(
|
|
(result: UpdateProductPrices) => {
|
|
this.toaster.show('Success', '');
|
|
this.loadData(result);
|
|
},
|
|
(error) => {
|
|
this.toaster.show('Error', error);
|
|
},
|
|
);
|
|
}
|
|
}
|