Skip to content

Commit

Permalink
feat(admin-ui): Persist custom order filter params in url
Browse files Browse the repository at this point in the history
Relates to #561
  • Loading branch information
michaelbromley committed Nov 19, 2020
1 parent 70e14f2 commit 8eb6246
Showing 1 changed file with 27 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ export class OrderListComponent
label: _('order.filter-preset-open'),
config: {
active: false,
states: this.orderStates.filter(s => s !== 'Delivered' && s !== 'Cancelled' && s !== 'Shipped'),
states: this.orderStates.filter(
s => s !== 'Delivered' && s !== 'Cancelled' && s !== 'Shipped',
),
},
},
{
Expand Down Expand Up @@ -100,10 +102,11 @@ export class OrderListComponent
merge(this.searchTerm.valueChanges, this.activePreset$.pipe(skip(1)))
.pipe(debounceTime(250), takeUntil(this.destroy$))
.subscribe(() => this.refresh());
const queryParamMap = this.route.snapshot.queryParamMap;
this.customFilterForm = new FormGroup({
states: new FormControl([]),
placedAtStart: new FormControl(),
placedAtEnd: new FormControl(),
states: new FormControl(queryParamMap.getAll('states') ?? []),
placedAtStart: new FormControl(queryParamMap.get('placedAtStart')),
placedAtEnd: new FormControl(queryParamMap.get('placedAtEnd')),
});
}

Expand All @@ -118,8 +121,14 @@ export class OrderListComponent
}

applyCustomFilters() {
const formValue = this.customFilterForm.value;
this.setQueryParam({
filter: 'custom',
states: formValue.states,
placedAtStart: formValue.placedAtStart,
placedAtEnd: formValue.placedAtEnd,
});
this.customFilterForm.markAsPristine();
this.refresh();
}

// tslint:disable-next-line:no-shadowed-variable
Expand All @@ -138,26 +147,29 @@ export class OrderListComponent
};
}
} else if (activeFilterPreset === 'custom') {
const formValue = this.customFilterForm?.value ?? {};
if (formValue.states?.length) {
const queryParams = this.route.snapshot.queryParamMap;
const states = queryParams.getAll('states') ?? [];
const placedAtStart = queryParams.get('placedAtStart');
const placedAtEnd = queryParams.get('placedAtEnd');
if (states.length) {
filter.state = {
in: formValue.states,
in: states,
};
}
if (formValue.placedAtStart && formValue.placedAtEnd) {
if (placedAtStart && placedAtEnd) {
filter.orderPlacedAt = {
between: {
start: formValue.placedAtStart,
end: formValue.placedAtEnd,
start: placedAtStart,
end: placedAtEnd,
},
};
} else if (formValue.placedAtStart) {
} else if (placedAtStart) {
filter.orderPlacedAt = {
after: formValue.placedAtStart,
after: placedAtStart,
};
} else if (formValue.placedAtEnd) {
} else if (placedAtEnd) {
filter.orderPlacedAt = {
before: formValue.placedAtEnd,
before: placedAtEnd,
};
}
}
Expand Down

0 comments on commit 8eb6246

Please sign in to comment.