Clarify query var transformation code

This commit is contained in:
Michael Shamoon 2022-03-27 23:47:53 -07:00
parent 92ec3fc060
commit e329d72e8b

View File

@ -128,43 +128,40 @@ export class DocumentListComponent implements OnInit, OnDestroy, AfterViewInit {
this.unmodifiedFilterRules = view.filter_rules this.unmodifiedFilterRules = view.filter_rules
}) })
const filterQueryVars: string[] = FILTER_RULE_TYPES.map( const allFilterRuleQueryParams: string[] = FILTER_RULE_TYPES.map(
(rt) => rt.filtervar (rt) => rt.filtervar
) )
this.route.queryParamMap this.route.queryParamMap
.pipe( .pipe(
filter((qp) => !this.route.snapshot.paramMap.has('id')), // only when not on saved view filter(() => !this.route.snapshot.paramMap.has('id')), // only when not on saved view
takeUntil(this.unsubscribeNotifier) takeUntil(this.unsubscribeNotifier)
) )
.subscribe((queryParams) => { .subscribe((queryParams) => {
const queryVarsFilterRules = [] // transform query params to filter rules
let filterRulesFromQueryParams: FilterRule[] = []
allFilterRuleQueryParams
.filter((frqp) => queryParams.has(frqp))
.forEach((filterQueryParamName) => {
const filterQueryParamValues: string[] = queryParams
.get(filterQueryParamName)
.split(',')
filterQueryVars.forEach((filterQueryVar) => { filterRulesFromQueryParams = filterRulesFromQueryParams.concat(
if (queryParams.has(filterQueryVar)) { // map all values to filter rules
const value = queryParams.get(filterQueryVar) filterQueryParamValues.map((val) => {
if (value.split(',').length > 1) { return {
value.split(',').forEach((splitVal) => {
queryVarsFilterRules.push({
rule_type: FILTER_RULE_TYPES.find( rule_type: FILTER_RULE_TYPES.find(
(rt) => rt.filtervar == filterQueryVar (rt) => rt.filtervar == filterQueryParamName
).id, ).id,
value: splitVal, value: val,
}) }
}) })
} else { )
queryVarsFilterRules.push({ })
rule_type: FILTER_RULE_TYPES.find(
(rt) => rt.filtervar == filterQueryVar
).id,
value: value,
})
}
}
})
this.list.activateSavedView(null) this.list.activateSavedView(null)
this.list.filterRules = queryVarsFilterRules this.list.filterRules = filterRulesFromQueryParams
this.list.reload() this.list.reload()
this.unmodifiedFilterRules = [] this.unmodifiedFilterRules = []
}) })