bizmatch-project/bizmatch/src/app/services/search.service.ts

31 lines
1.3 KiB
TypeScript

import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import { SortByOptions } from '../../../../bizmatch-server/src/models/db.model';
import { BusinessListingCriteria, CommercialPropertyListingCriteria, UserListingCriteria } from '../../../../bizmatch-server/src/models/main.model';
import { getCriteriaProxy } from '../utils/utils';
@Injectable({
providedIn: 'root',
})
export class SearchService {
private criteriaSource = new Subject<{
criteria: BusinessListingCriteria | CommercialPropertyListingCriteria | UserListingCriteria;
sortBy?: SortByOptions;
}>();
currentCriteria = this.criteriaSource.asObservable();
constructor() {}
search(criteriaType: 'businessListings' | 'commercialPropertyListings' | 'brokerListings'): void {
const criteria = getCriteriaProxy(criteriaType, this);
const storedSortBy =
criteriaType === 'businessListings'
? sessionStorage.getItem('businessSortBy')
: criteriaType === 'commercialPropertyListings'
? sessionStorage.getItem('commercialSortBy')
: sessionStorage.getItem('professionalsSortBy');
const sortBy = storedSortBy && storedSortBy !== 'null' ? (storedSortBy as SortByOptions) : null;
this.criteriaSource.next({ criteria, sortBy });
}
}