refactor: make data list reusable component

Closes #98
This commit is contained in:
Jovan Krunić
2021-04-15 17:24:15 +02:00
parent c97ecd698c
commit a8f22c1e96
15 changed files with 472 additions and 354 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2020 StApps
* Copyright (C) 2018-2021 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
@@ -13,114 +13,54 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {Component} from '@angular/core';
import {
SCSearchQuery, SCThing,
} from '@openstapps/core';
import {DataListComponent} from './data-list.component';
import {SearchPageComponent} from './search-page.component';
/**
* TODO
* Presents a list of places for eating/drinking
*/
@Component({
selector: 'stapps-data-list',
templateUrl: 'data-list.html',
templateUrl: 'search-page.html',
})
export class FoodDataListComponent extends DataListComponent {
export class FoodDataListComponent extends SearchPageComponent {
/**
* Fetches items with set query configuration
*
* @param append If true fetched data gets appended to existing, override otherwise (default false)
* Sets the forced filter to present only places for eating/drinking
*/
protected async fetchAndUpdateItems(append = false): Promise<void> {
try {
// build query search options
const searchOptions: SCSearchQuery = {
from: this.from,
size: this.pageSize,
};
if (this.queryText && this.queryText.length > 0) {
// add query string
searchOptions.query = this.queryText;
}
if (this.sortQuery) {
// add query sorting
searchOptions.sort = [this.sortQuery];
}
searchOptions.filter = {
arguments: {
filters: [
{
arguments: {
field: 'categories',
value: 'canteen',
},
type: 'value',
},
{
arguments: {
field: 'categories',
value: 'student canteen',
},
type: 'value',
},
{
arguments: {
field: 'categories',
value: 'cafe',
},
type: 'value',
},
{
arguments: {
field: 'categories',
value: 'restaurant',
},
type: 'value',
},
],
operation: 'or',
initialize() {
this.forcedFilter = {
arguments: {
filters: [
{
arguments: {
field: 'categories',
value: 'canteen',
},
type: 'value',
},
type: 'boolean',
};
if (this.filterQuery !== undefined) {
searchOptions.filter = {
{
arguments: {
filters: [
searchOptions.filter,
this.filterQuery,
],
operation: 'and',
field: 'categories',
value: 'student canteen',
},
type: 'boolean',
};
}
const result = await this.dataProvider.search(searchOptions);
this.items = (async () => {
this.updateContextFilter(result.facets);
let items: SCThing[];
if(append) {
items = (await this.items).concat(result.data);
} else {
items = result.data;
}
return items;
})();
} catch (err) {
const alert: HTMLIonAlertElement = await this.alertController.create({
buttons: ['Dismiss'],
header: 'Error',
subHeader: err.message,
});
await alert.present();
}
type: 'value',
},
{
arguments: {
field: 'categories',
value: 'cafe',
},
type: 'value',
},
{
arguments: {
field: 'categories',
value: 'restaurant',
},
type: 'value',
},
],
operation: 'or',
},
type: 'boolean',
};
}
}