Update Product Prices built
This commit is contained in:
@ -0,0 +1,126 @@
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
import { FormArray, FormBuilder, FormGroup } from '@angular/forms';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
import * as moment from 'moment';
|
||||
|
||||
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 {
|
||||
dataSource: UpdateProductPricesDataSource;
|
||||
form: FormGroup;
|
||||
menuCategories: MenuCategory[];
|
||||
info: UpdateProductPrices;
|
||||
|
||||
/** 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 fb: FormBuilder,
|
||||
private toCsv: ToCsvService,
|
||||
private toaster: ToasterService,
|
||||
private ser: UpdateProductPricesService,
|
||||
) {
|
||||
this.createForm();
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
this.route.data.subscribe(
|
||||
(data: { menuCategories: MenuCategory[]; info: UpdateProductPrices }) => {
|
||||
this.menuCategories = data.menuCategories;
|
||||
this.loadData(data.info);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
loadData(info: UpdateProductPrices) {
|
||||
this.info = info;
|
||||
this.form.get('date').setValue(moment(this.info.date, 'DD-MMM-YYYY').toDate());
|
||||
this.form
|
||||
.get('menuCategory')
|
||||
.setValue(this.info.menuCategoryId !== undefined ? this.info.menuCategoryId : '');
|
||||
this.form.setControl(
|
||||
'prices',
|
||||
this.fb.array(
|
||||
this.info.items.map((x) =>
|
||||
this.fb.group({
|
||||
newPrice: x.newPrice,
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
this.dataSource = new UpdateProductPricesDataSource(this.info.items);
|
||||
}
|
||||
|
||||
show() {
|
||||
const info = this.getInfo();
|
||||
const route = ['update-product-prices'];
|
||||
if (info.menuCategoryId !== undefined) route.push(info.menuCategoryId);
|
||||
this.router.navigate(route, {
|
||||
queryParams: {
|
||||
date: info.date,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
createForm() {
|
||||
this.form = this.fb.group({
|
||||
date: '',
|
||||
menuCategory: '',
|
||||
prices: this.fb.array([]),
|
||||
});
|
||||
}
|
||||
|
||||
getInfo(): UpdateProductPrices {
|
||||
const formModel = this.form.value;
|
||||
const array = this.form.get('prices') as FormArray;
|
||||
this.info.items.forEach((item, index) => {
|
||||
item.newPrice = array.controls[index].value.newPrice;
|
||||
});
|
||||
|
||||
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('Danger', error.error);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user