2020-06-26 11:09:59 +02:00
|
|
|
import {Component, OnInit, ViewChild} from '@angular/core';
|
2020-06-19 08:43:22 +02:00
|
|
|
import {ApiService} from '../services/api.service';
|
2020-05-22 09:36:50 +02:00
|
|
|
|
2020-06-26 11:09:59 +02:00
|
|
|
|
|
|
|
|
|
|
|
interface LoadSamplesOptions {
|
|
|
|
toPage?: number;
|
|
|
|
event?: Event;
|
|
|
|
firstPage?: boolean;
|
|
|
|
}
|
|
|
|
|
2020-05-22 09:36:50 +02:00
|
|
|
@Component({
|
|
|
|
selector: 'app-samples',
|
|
|
|
templateUrl: './samples.component.html',
|
|
|
|
styleUrls: ['./samples.component.scss']
|
|
|
|
})
|
2020-06-26 11:09:59 +02:00
|
|
|
|
|
|
|
// TODO: always show first page on sort change
|
|
|
|
|
2020-05-22 12:52:17 +02:00
|
|
|
export class SamplesComponent implements OnInit { // TODO: implement paging
|
2020-05-22 09:36:50 +02:00
|
|
|
|
2020-06-26 11:09:59 +02:00
|
|
|
@ViewChild('pageSizeSelection') pageSizeSelection: HTMLElement;
|
|
|
|
|
2020-05-22 12:52:17 +02:00
|
|
|
materials = {};
|
|
|
|
samples = [];
|
2020-06-26 11:09:59 +02:00
|
|
|
totalSamples = 0; // total number of samples
|
|
|
|
filters = {status: {new: true, validated: true}, pageSize: 25, toPage: 0, sort: 'added-asc'};
|
|
|
|
page = 1;
|
|
|
|
loadSamplesQueue = []; // arguments of queued up loadSamples() calls
|
|
|
|
activeKeys = [
|
|
|
|
{name: 'Number', key: 'number'},
|
|
|
|
// {name: 'Material number', key: ''},
|
|
|
|
{name: 'Material name', key: ''},
|
|
|
|
{name: 'Supplier', key: ''},
|
|
|
|
// {name: 'Material', key: ''},
|
|
|
|
// {name: 'GF', key: ''},
|
|
|
|
// {name: 'CF', key: ''},
|
|
|
|
// {name: 'M', key: ''},
|
|
|
|
{name: 'Type', key: 'type'},
|
|
|
|
{name: 'Color', key: 'color'},
|
|
|
|
{name: 'Batch', key: 'batch'},
|
|
|
|
{name: 'Added', key: 'added'},
|
|
|
|
];
|
2020-05-22 12:52:17 +02:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
private api: ApiService
|
|
|
|
) { }
|
2020-05-22 09:36:50 +02:00
|
|
|
|
|
|
|
ngOnInit(): void {
|
2020-06-19 08:43:22 +02:00
|
|
|
this.api.get('/materials?status=all', (mData: any) => {
|
2020-05-22 12:52:17 +02:00
|
|
|
this.materials = {};
|
|
|
|
mData.forEach(material => {
|
|
|
|
this.materials[material._id] = material;
|
|
|
|
});
|
2020-06-19 08:43:22 +02:00
|
|
|
this.loadSamples();
|
|
|
|
});
|
2020-06-26 11:09:59 +02:00
|
|
|
this.api.get('/samples/count', (data: {count: number}) => {
|
|
|
|
this.totalSamples = data.count;
|
|
|
|
});
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
|
|
|
|
2020-06-26 11:09:59 +02:00
|
|
|
loadSamples(options: LoadSamplesOptions = {}) { // set toPage to null to reload first page, queues calls
|
|
|
|
this.loadSamplesQueue.push(options);
|
|
|
|
if (this.loadSamplesQueue.length <= 1) { // nothing queued up
|
|
|
|
this.sampleLoader(this.loadSamplesQueue[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private sampleLoader(options: LoadSamplesOptions) { // actual loading of the sample, do not call directly
|
|
|
|
const query: string[] = [];
|
|
|
|
query.push('status=' + (this.filters.status.new && this.filters.status.validated ? 'all' : (this.filters.status.new ? 'new' : 'validated')));
|
|
|
|
if (this.samples[0]) { // do not include from-id when page size was changed
|
|
|
|
if (!options.firstPage && (!options.event || ((options.event.target as HTMLElement).id.indexOf(this.pageSizeSelection.id) < 0))) {
|
|
|
|
query.push('from-id=' + this.samples[0]._id);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.page = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (options.toPage) {
|
|
|
|
query.push('to-page=' + options.toPage);
|
|
|
|
}
|
|
|
|
query.push('page-size=' + this.filters.pageSize);
|
|
|
|
query.push('sort=' + this.filters.sort);
|
|
|
|
|
|
|
|
this.api.get('/samples?' + query.join('&'), sData => {
|
2020-06-19 08:43:22 +02:00
|
|
|
this.samples = sData as any;
|
|
|
|
this.samples.forEach(sample => {
|
|
|
|
sample.material_number = this.materials[sample.material_id].numbers.find(e => sample.color === e.color).number;
|
2020-05-22 12:52:17 +02:00
|
|
|
});
|
2020-06-26 11:09:59 +02:00
|
|
|
this.loadSamplesQueue.shift();
|
|
|
|
if (this.loadSamplesQueue.length > 0) { // execute next queue item
|
|
|
|
this.sampleLoader(this.loadSamplesQueue[0]);
|
|
|
|
}
|
2020-05-22 12:52:17 +02:00
|
|
|
});
|
2020-05-22 09:36:50 +02:00
|
|
|
}
|
|
|
|
|
2020-06-26 11:09:59 +02:00
|
|
|
loadPage(delta) {
|
|
|
|
if (!/[0-9]+/.test(delta) || (this.page <= 1 && delta < 0)) { // invalid delta
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.page += delta;
|
|
|
|
this.loadSamples({toPage: delta});
|
|
|
|
}
|
|
|
|
|
|
|
|
pages() {
|
|
|
|
return Math.ceil(this.totalSamples / this.filters.pageSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
setSort(string) {
|
|
|
|
this.filters.sort = string;
|
|
|
|
this.loadSamples({firstPage: true});
|
|
|
|
}
|
2020-05-22 09:36:50 +02:00
|
|
|
}
|