You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mohemmionic5/Mohem/src/app/offersdiscount/home/home.component.ts

221 lines
7.1 KiB
TypeScript

import { AfterViewInit, Component, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { ModalController } from '@ionic/angular';
import { CommonService } from 'src/app/hmg-common/services/common/common.service';
import { TranslatorService } from 'src/app/hmg-common/services/translator/translator.service';
import { OfferDiscountService } from '../services/service';
import { IonInfiniteScroll } from '@ionic/angular';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss'],
})
export class HomeComponent implements AfterViewInit {
@ViewChild(IonInfiniteScroll) infiniteScroll: IonInfiniteScroll;
segment: any = '1';
categoriesObj: any;
categories: any = [];
offersData: any = [];
activeClass: any;
tempSearch: any = [];
searchText: String;
direction: any;
itemCounter = 1;
itemType: any;
isData = true;
allCategory: any = {
categoryID: 0,
content: '<svg xmlns="http://www.w3.org/2000/svg" width="33.925" height="25.841" viewBox="0 0 33.925 25.841"><g id="More_Select" data-name="More Select"><path d="m30 1h-24a1 1 0 0 0 -1 1v1h21a3 3 0 0 1 3 3v21h1a1 1 0 0 0 1-1v-24a1 1 0 0 0 -1-1z"/><path d="m26 5h-24a1 1 0 0 0 -1 1v24a1 1 0 0 0 1 1h24a1 1 0 0 0 1-1v-24a1 1 0 0 0 -1-1zm-4.747 9.344-8.728 8.726a1 1 0 0 1 -1.414 0l-4.364-4.363a1 1 0 0 1 1.414-1.414l3.657 3.656 8.021-8.019a1 1 0 0 1 1.414 1.414z"/></g></svg>',
isActive: true,
categoryName_en: "All",
categoryName_ar: "الجميع",
}
constructor(
public ts: TranslatorService,
public modalController: ModalController,
public cs: CommonService,
public offersService: OfferDiscountService,
public route: ActivatedRoute,
) {
this.route
.params.subscribe(val => {
setTimeout(() => {
this.ionEnter();
}, 100);
});
}
ngAfterViewInit(): void {
this.getOfferDiscount();
}
ionEnter() {
this.direction = TranslatorService.getCurrentDirection();
console.log(this.direction);
let activeClass = this.cs.sharedService.getSharedData(OfferDiscountService.selected_filters);
if (activeClass) {
this.filterOffers(activeClass);
} else {
this.getCategories();
this.tempSearch = JSON.parse(JSON.stringify(this.offersData));
}
}
getCategories() {
this.offersService.getCategories({}).subscribe((result) => {
this.categories = JSON.parse(result.Mohemm_ITG_ResponseItem).result.data; //result.result.data;
//this.allCategory.content = this.sanitizer.bypassSecurityTrustHtml();
this.categories.unshift(this.allCategory);
// for (let i = 0; i < this.categories.length; i++) {
// this.categories[i].content = this.sanitizer.bypassSecurityTrustHtml(this.categories[i].content);
// }
this.cs.sharedService.setSharedData(this.categories, OfferDiscountService.categories);
});
}
openDetails(offer) {
this.cs.sharedService.setSharedData(offer, OfferDiscountService.selected_offers);
var related = this.offersData.filter((res) => res.rowID != offer.rowID);
this.cs.sharedService.setSharedData(related, OfferDiscountService.related_offers);
this.cs.navigateForward('/offersdiscount/offer-details');
}
getOfferDiscount(pageNo?, categoryId?) {
this.cs.startLoading();
if (categoryId) {
this.offersService.getOffers({}, () => { }, this.ts.trPK('general', 'retry'), pageNo, categoryId).subscribe((res) => {
var data = JSON.parse(res['Mohemm_ITG_ResponseItem']);
this.cs.stopLoading();
if (data['result']) {
this.displayOffers(data['result']);
this.isData = true;
} else {
this.isData = false;
}
});
} else {
this.offersService.getOffers({}, () => { }, this.ts.trPK('general', 'retry')).subscribe((res) => {
var data = JSON.parse(res['Mohemm_ITG_ResponseItem']);
this.cs.stopLoading();
if (data['result']) {
this.displayOffers(data['result']);
this.isData = true;
} else { this.isData = false; }
});
}
}
displayOffers(data) {
if (data['data']) {
var parseJSON = data['data'];
var offers = JSON.parse(parseJSON);
console.log(offers);
this.offersData = this.filterActiveItems(offers);
console.log(this.offersData);
this.tempSearch = this.offersData;
}
}
filterActiveItems(offers) {
return offers.filter((res) => {
return res['IsActive'] == 'True';
});
}
filterOffers(key) {
if (this.direction === 'ltr') {
this.activeClass = key.categoryName_en;
} else {
this.activeClass = key.categoryName_ar
}
this.itemType = key.id;
this.getOfferDiscount(1, this.itemType)
}
checkDate(date) {
return new Date(date) >= new Date()
}
getDotted(temp) {
temp = this.stripHtml(temp);
return temp.substring(0, 70) + " ...";
}
stripHtml(html) {
var temporalDivElement = document.createElement("div");
temporalDivElement.innerHTML = html;
return temporalDivElement.textContent || temporalDivElement.innerText || "";
}
search(t) {
console.log(this.tempSearch);
if (t === '') {
this.offersData = this.tempSearch;
} else {
this.offersData = this.tempSearch.filter((post) => {
return (post.Title.toLowerCase().indexOf(t.toLowerCase()) > -1);
});
}
}
async openModel() {
// const modal = await this.modalController.create({
// component: FilterComponent,
// });
// modal.cssClass = "filter-modal"
// modal.present();
this.cs.navigateForward('/offersdiscount/filter-page');
}
loadData(event) {
const counter = 1;
this.itemCounter += counter;
console.log(this.itemType);
if (this.itemType) {
setTimeout(() => {
event.target.complete();
console.log(this.itemCounter);
this.offersService.getOffers({}, () => { }, this.ts.trPK('general', 'retry'), this.itemCounter, this.itemType).subscribe((res) => {
const data = JSON.parse(res['Mohemm_ITG_ResponseItem']);
if (data['result']) {
var allItem = JSON.parse(data.result['data']);
allItem = this.filterActiveItems(allItem);
if (allItem) {
allItem.forEach(element => {
this.offersData.push(element);
});
}
}
});
// event.target.disabled = true;
}, 1000);
} else {
setTimeout(() => {
event.target.complete();
console.log(this.itemCounter);
this.offersService.getOffers({}, () => { }, this.ts.trPK('general', 'retry'), this.itemCounter).subscribe((res) => {
const data = JSON.parse(res['Mohemm_ITG_ResponseItem']);
if (data['result']) {
const allItem = JSON.parse(data.result['data']);
if (allItem) {
allItem.forEach(element => {
this.offersData.push(element);
});
}
}
});
// event.target.disabled = true;
}, 1000);
}
}
}