Skip to content

Commit

Permalink
Merge pull request #772 from tolkamps1/EPICSYSTEM-7-activity-posts-page
Browse files Browse the repository at this point in the history
[EPICSYSTEM-7] Add filters to activity posts page.
  • Loading branch information
tolkamps1 authored Feb 22, 2024
2 parents bbd9f4e + 695d957 commit a13f831
Show file tree
Hide file tree
Showing 7 changed files with 724 additions and 163 deletions.
109 changes: 82 additions & 27 deletions src/app/activity/activity-component-resolver.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@ import { Observable } from 'rxjs/Observable';

import { SearchService } from 'app/services/search.service';
import { TableTemplateUtils } from 'app/shared/utils/table-template-utils';
import { Constants } from 'app/shared/utils/constants';
import { Project } from 'app/models/project';
import { ProjectService } from 'app/services/project.service';
import * as _ from 'lodash';

@Injectable()
export class ActivityComponentResolver implements Resolve<Observable<object>> {
public filterForURL: object = {};
public filterForAPI: object = {};

public activityTypes: Array<object>;
public projects: Array<Project> = [];

constructor(
private searchService: SearchService,
private projectService: ProjectService,
private tableTemplateUtils: TableTemplateUtils
) { }

Expand All @@ -17,35 +28,79 @@ export class ActivityComponentResolver implements Resolve<Observable<object>> {
if (activity) {
return this.searchService.getItem(activity, 'RecentActivity');
} else {
// this.filterForUrl.dateAddedStart = route.params.dateAddedStart == null || route.params.dateAddedStart === '' ? '' : route.params.dateAddedStart;
// this.filterForUrl.dateAddedEnd = route.params.dateAddedEnd == null || route.params.dateAddedEnd === '' ? '' : route.params.dateAddedEnd;

let filter = {};
let filterForApi = {};
if (route.params.type != null) {
filter['type'] = route.params.type;
let typeFiltersFromRoute = route.params.type.split(',');
let typeFiltersForApi = [];
if (typeFiltersFromRoute.includes('publicCommentPeriod')) { typeFiltersForApi.push('Public Comment Period'); }
if (typeFiltersFromRoute.includes('news')) { typeFiltersForApi.push('News'); }
if (typeFiltersForApi.length > 0) { filterForApi = { 'type': typeFiltersForApi.toString() }; }
}
return this.projectService.getAll(1, 1000, '+name')
.switchMap((res: any) => {
this.projects = res.data || [];
this.activityTypes = Constants.activityTypes;

this.setFiltersFromParams(route.params);

let tableParams = this.tableTemplateUtils.getParamsFromUrl(route.params, this.filterForURL);
if (tableParams.sortBy === '') {
tableParams.sortBy = '-dateAdded';
this.tableTemplateUtils.updateUrl(tableParams.sortBy, tableParams.currentPage, tableParams.pageSize, this.filterForURL, tableParams.keywords);
}
return this.searchService.getSearchResults(
tableParams.keywords || '',
'RecentActivity',
null,
tableParams.currentPage,
tableParams.pageSize,
tableParams.sortBy,
{},
true,
this.filterForAPI,
'');
});
}
}

paramsToDateFilters(params, name) {
delete this.filterForURL[name];
delete this.filterForAPI[name];

if (params[name]) {
this.filterForURL[name] = params[name];
this.filterForAPI[name] = params[name];
}
}

paramsToToggleFilters(params, name) {
delete this.filterForURL[name];
delete this.filterForAPI[name];

let tableParams = this.tableTemplateUtils.getParamsFromUrl(route.params, filter);
if (tableParams.sortBy === '') {
tableParams.sortBy = '-dateAdded';
this.tableTemplateUtils.updateUrl(tableParams.sortBy, tableParams.currentPage, tableParams.pageSize, filter, tableParams.keywords);
if (params[name]) {
this.filterForURL[name] = params[name];
this.filterForAPI[name] = params[name];
}
}

paramsToCollectionFilters(params, name, collection, identifyBy) {
delete this.filterForURL[name];
delete this.filterForAPI[name];

if (params[name] && collection) {
let confirmedValues = [];
// look up each value in collection
const values = params[name].split(',');
values.forEach(value => {
const record = _.find(collection, [ identifyBy, value ]);
if (record) {
confirmedValues.push(value);
}
});
if (confirmedValues.length) {
this.filterForURL[name] = confirmedValues.join(',');
this.filterForAPI[name] = confirmedValues.join(',');
}
return this.searchService.getSearchResults(
tableParams.keywords || '',
'RecentActivity',
null,
tableParams.currentPage,
tableParams.pageSize,
tableParams.sortBy,
{},
true,
filterForApi, '');
}
}

setFiltersFromParams(params) {
this.paramsToCollectionFilters(params, 'project', this.projects, '_id');
this.paramsToCollectionFilters(params, 'type', this.activityTypes, 'code');
this.paramsToToggleFilters(params, 'complianceAndEnforcement');
this.paramsToDateFilters(params, 'dateAddedStart');
this.paramsToDateFilters(params, 'dateAddedEnd');
}
}
Loading

0 comments on commit a13f831

Please sign in to comment.