|
| 1 | +import { Component, OnInit } from '@angular/core'; |
| 2 | +import { AbstractPage } from '../../lib/abstract-page'; |
| 3 | +import { ICampus, IMeals, IMensaResponse } from '../../lib/interfaces'; |
| 4 | +import { default as moment } from 'moment/moment'; |
| 5 | +import { convertToArray, isInArray } from '../../lib/util'; |
| 6 | +import jquery from 'jquery'; |
| 7 | +import { IMensaRequestParams } from '../../services/webservice-wrapper/webservice-definition-interfaces'; |
| 8 | +import { TranslateService } from '@ngx-translate/core'; |
| 9 | +import { WebserviceWrapperService } from '../../services/webservice-wrapper/webservice-wrapper.service'; |
| 10 | + |
| 11 | +@Component({ |
| 12 | + selector: 'app-mensa2', |
| 13 | + templateUrl: './mensa2.page.html', |
| 14 | + styleUrls: ['./mensa2.page.scss'], |
| 15 | +}) |
| 16 | +export class Mensa2Page extends AbstractPage implements OnInit { |
| 17 | + isLoaded; |
| 18 | + networkError; |
| 19 | + selectedDate; |
| 20 | + currentDate = moment(); |
| 21 | + filterKeywords = []; |
| 22 | + |
| 23 | + ulfMeals: IMeals[] = []; |
| 24 | + displayedUlfMeals: IMeals[] = []; |
| 25 | + ulfMealForDate: boolean[] = []; |
| 26 | + ulfIconMapping = []; |
| 27 | + noUlfMealsForDate; |
| 28 | + campus: ICampus; |
| 29 | + |
| 30 | + constructor( |
| 31 | + private translate: TranslateService, |
| 32 | + private ws: WebserviceWrapperService |
| 33 | + ) { |
| 34 | + super({ optionalNetwork: true }); |
| 35 | + } |
| 36 | + |
| 37 | + ngOnInit() {} |
| 38 | + |
| 39 | + loadMenu(refresher?) { |
| 40 | + if (!(refresher && refresher.target)) { |
| 41 | + this.isLoaded = false; |
| 42 | + } |
| 43 | + |
| 44 | + this.ulfMeals = undefined; |
| 45 | + this.displayedUlfMeals = undefined; |
| 46 | + for (let i = 0; i < this.ulfMealForDate.length; i++) { |
| 47 | + this.ulfMealForDate[i] = false; |
| 48 | + } |
| 49 | + |
| 50 | + this.noUlfMealsForDate = true; |
| 51 | + this.networkError = false; |
| 52 | + |
| 53 | + this.ws |
| 54 | + .call( |
| 55 | + 'mensa', |
| 56 | + { |
| 57 | + campus_canteen_name: this.campus.canteen_name, |
| 58 | + } as IMensaRequestParams, |
| 59 | + { forceRefreshGroup: refresher !== undefined } |
| 60 | + ) |
| 61 | + .subscribe( |
| 62 | + (res: IMensaResponse) => { |
| 63 | + res.meal = res.meal.sort((a, b) => { |
| 64 | + if (a.title === 'Info') { |
| 65 | + return -1; |
| 66 | + } else { |
| 67 | + return 0; |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + if (this.campus.canteen_name === 'Griebnitzsee') { |
| 72 | + const ulfParam = 'UlfsCafe'; |
| 73 | + this.ws |
| 74 | + .call('mensa', { |
| 75 | + campus_canteen_name: ulfParam, |
| 76 | + } as IMensaRequestParams) |
| 77 | + .subscribe((resUlf: IMensaResponse) => { |
| 78 | + if (resUlf.meal) { |
| 79 | + this.ulfMeals = resUlf.meal; |
| 80 | + this.displayedUlfMeals = resUlf.meal; |
| 81 | + } |
| 82 | + if (resUlf.iconHashMap && resUlf.iconHashMap.entry) { |
| 83 | + this.ulfIconMapping = resUlf.iconHashMap.entry; |
| 84 | + } |
| 85 | + if (refresher && refresher.target) { |
| 86 | + refresher.target.complete(); |
| 87 | + } |
| 88 | + }); |
| 89 | + } |
| 90 | + }, |
| 91 | + () => { |
| 92 | + this.isLoaded = true; |
| 93 | + this.networkError = true; |
| 94 | + if (refresher && refresher.target) { |
| 95 | + refresher.target.complete(); |
| 96 | + } |
| 97 | + } |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + pickDate($event) { |
| 102 | + this.selectedDate = $event; |
| 103 | + this.noUlfMealsForDate = true; |
| 104 | + |
| 105 | + let i; |
| 106 | + let mealDate; |
| 107 | + |
| 108 | + if (this.displayedUlfMeals) { |
| 109 | + for (i = 0; i < this.displayedUlfMeals.length; i++) { |
| 110 | + if (this.displayedUlfMeals[i].date) { |
| 111 | + mealDate = moment(this.displayedUlfMeals[i].date); |
| 112 | + } else { |
| 113 | + mealDate = moment(); |
| 114 | + } |
| 115 | + |
| 116 | + if ($event.format('MM DD YYYY') === mealDate.format('MM DD YYYY')) { |
| 117 | + this.ulfMealForDate[i] = true; |
| 118 | + this.noUlfMealsForDate = false; |
| 119 | + } else { |
| 120 | + this.ulfMealForDate[i] = false; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + filterMenus(event) { |
| 127 | + const filter = convertToArray(event.detail.value); |
| 128 | + |
| 129 | + this.displayedUlfMeals = this.ulfMeals; |
| 130 | + |
| 131 | + if (filter && filter.length > 0) { |
| 132 | + if (this.displayedUlfMeals) { |
| 133 | + this.displayedUlfMeals = jquery.grep(this.displayedUlfMeals, (meal) => { |
| 134 | + if (meal.type) { |
| 135 | + let fulfillsConditions = false; |
| 136 | + for (const flt of filter) { |
| 137 | + if (isInArray(meal.type, flt)) { |
| 138 | + fulfillsConditions = true; |
| 139 | + break; |
| 140 | + } |
| 141 | + } |
| 142 | + return fulfillsConditions; |
| 143 | + } else { |
| 144 | + return false; |
| 145 | + } |
| 146 | + }); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + this.classifyMeals(); |
| 151 | + } |
| 152 | + |
| 153 | + classifyMeals() { |
| 154 | + let mealDate; |
| 155 | + |
| 156 | + if (this.displayedUlfMeals) { |
| 157 | + for (let i = 0; i < this.displayedUlfMeals.length; i++) { |
| 158 | + if (this.displayedUlfMeals[i].date) { |
| 159 | + mealDate = moment(this.displayedUlfMeals[i].date); |
| 160 | + } else { |
| 161 | + mealDate = moment(); |
| 162 | + } |
| 163 | + |
| 164 | + if ( |
| 165 | + this.currentDate.format('MM DD YYYY') === |
| 166 | + mealDate.format('MM DD YYYY') |
| 167 | + ) { |
| 168 | + this.ulfMealForDate[i] = true; |
| 169 | + this.noUlfMealsForDate = false; |
| 170 | + } else { |
| 171 | + this.ulfMealForDate[i] = false; |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + this.isLoaded = true; |
| 177 | + this.pickDate(this.selectedDate); |
| 178 | + } |
| 179 | +} |
0 commit comments