-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: filter orders by date range, order number, sku and state
- Loading branch information
Showing
10 changed files
with
317 additions
and
3 deletions.
There are no files selected for viewing
Empty file.
16 changes: 16 additions & 0 deletions
16
...pp/pages/account-order-history/account-order-filters/account-order-filters.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<form [formGroup]="form" (ngSubmit)="submitForm()"> | ||
<div class="row"> | ||
<div class="col-md-10"> | ||
<formly-form [form]="form" [fields]="fields" /> | ||
</div> | ||
|
||
<div class="col-md-2 text-right d-flex flex-column justify-content-between pb-2"> | ||
<button type="reset" class="btn btn-secondary" (click)="resetForm()"> | ||
{{ 'account.order-history.filter.clear' | translate }} | ||
</button> | ||
<button type="submit" class="btn btn-primary"> | ||
{{ 'account.order-history.filter.apply' | translate }} | ||
</button> | ||
</div> | ||
</div> | ||
</form> |
33 changes: 33 additions & 0 deletions
33
...pages/account-order-history/account-order-filters/account-order-filters.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { ReactiveFormsModule } from '@angular/forms'; | ||
import { RouterTestingModule } from '@angular/router/testing'; | ||
import { FormlyForm } from '@ngx-formly/core'; | ||
import { TranslateModule } from '@ngx-translate/core'; | ||
import { MockComponent } from 'ng-mocks'; | ||
|
||
import { AccountOrderFiltersComponent } from './account-order-filters.component'; | ||
|
||
describe('Account Order Filters Component', () => { | ||
let component: AccountOrderFiltersComponent; | ||
let fixture: ComponentFixture<AccountOrderFiltersComponent>; | ||
let element: HTMLElement; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [MockComponent(FormlyForm), ReactiveFormsModule, RouterTestingModule, TranslateModule.forRoot()], | ||
declarations: [AccountOrderFiltersComponent], | ||
}).compileComponents(); | ||
}); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(AccountOrderFiltersComponent); | ||
component = fixture.componentInstance; | ||
element = fixture.nativeElement; | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(component).toBeTruthy(); | ||
expect(element).toBeTruthy(); | ||
expect(() => fixture.detectChanges()).not.toThrow(); | ||
}); | ||
}); |
213 changes: 213 additions & 0 deletions
213
src/app/pages/account-order-history/account-order-filters/account-order-filters.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
import { | ||
AfterViewInit, | ||
ChangeDetectionStrategy, | ||
Component, | ||
DestroyRef, | ||
EventEmitter, | ||
Injectable, | ||
Input, | ||
OnInit, | ||
Output, | ||
inject, | ||
} from '@angular/core'; | ||
import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; | ||
import { UntypedFormGroup } from '@angular/forms'; | ||
import { ActivatedRoute, Router } from '@angular/router'; | ||
import { NgbDateAdapter, NgbDateStruct } from '@ng-bootstrap/ng-bootstrap'; | ||
import { FormlyFieldConfig } from '@ngx-formly/core'; | ||
|
||
import { OrderListQuery } from 'ish-core/models/order-list-query/order-list-query.model'; | ||
|
||
@Injectable() | ||
export class OrderDateFilterAdapter extends NgbDateAdapter<string> { | ||
fromModel(value: string): NgbDateStruct { | ||
if (value) { | ||
const dateParts = value.split('-'); | ||
return { | ||
year: +dateParts[0], | ||
month: +dateParts[1], | ||
day: +dateParts[2], | ||
}; | ||
} | ||
} | ||
toModel(date: NgbDateStruct): string { | ||
if (date) { | ||
return `${date.year}-${date.month.toString().padStart(2, '0')}-${date.day.toString().padStart(2, '0')}`; | ||
} | ||
} | ||
} | ||
|
||
interface FormModel extends Record<string, unknown> { | ||
date?: { | ||
fromDate: string; | ||
toDate: string; | ||
}; | ||
orderNo?: string; | ||
sku?: string; | ||
state?: string; | ||
} | ||
|
||
type UrlModel = Partial<Record<'from' | 'to' | 'orderNo' | 'sku' | 'state', string | string[]>>; | ||
|
||
const filterableStates: OrderListQuery['statusCode'] = [ | ||
'NEW', | ||
'INPROGRESS', | ||
'CANCELLED', | ||
'NOTDELIVERABLE', | ||
'DELIVERED', | ||
'RETURNED', | ||
'PENDING', | ||
'COMPLETED', | ||
'MANUAL_INTERVENTION_NEEDED', | ||
'EXPORTED', | ||
'EXPORTFAILED', | ||
'CANCELLEDANDEXPORTED', | ||
]; | ||
|
||
function selectFirst(val: string | string[]): string { | ||
return Array.isArray(val) ? val[0] : val; | ||
} | ||
|
||
function selectAll(val: string | string[]): string { | ||
return Array.isArray(val) ? val.join(',') : val; | ||
} | ||
|
||
function selectArray(val: string | string[]): string[] { | ||
if (!val) { | ||
return; | ||
} | ||
return Array.isArray(val) ? val : [val]; | ||
} | ||
|
||
function removeEmpty<T extends Record<string, unknown>>(obj: T): T { | ||
return Object.keys(obj).reduce<Record<string, unknown>>((acc, key) => { | ||
if (Array.isArray(obj[key])) { | ||
if ((obj[key] as unknown[]).length > 0) { | ||
acc[key] = obj[key]; | ||
} | ||
} else if (obj[key]) { | ||
acc[key] = obj[key]; | ||
} | ||
return acc; | ||
}, {}) as T; | ||
} | ||
|
||
function urlToModel(params: UrlModel): FormModel { | ||
return removeEmpty<FormModel>({ | ||
date: { | ||
fromDate: selectFirst(params.from), | ||
toDate: selectFirst(params.to), | ||
}, | ||
orderNo: selectAll(params.orderNo), | ||
sku: selectAll(params.sku), | ||
state: selectFirst(params.state), | ||
}); | ||
} | ||
|
||
function modelToUrl(model: FormModel): UrlModel { | ||
return removeEmpty<UrlModel>({ | ||
from: model.date?.fromDate, | ||
to: model.date?.toDate, | ||
orderNo: model.orderNo?.split(',').map(s => s.trim()), | ||
sku: model.sku?.split(',').map(s => s.trim()), | ||
state: model.state | ||
?.split(',') | ||
.map(s => s.trim()) | ||
.filter(x => !!x) as OrderListQuery['statusCode'], | ||
}); | ||
} | ||
|
||
function urlToQuery(params: UrlModel): Partial<OrderListQuery> { | ||
return removeEmpty<Partial<OrderListQuery>>({ | ||
creationDateFrom: selectFirst(params.from), | ||
creationDateTo: selectFirst(params.to), | ||
documentNumber: selectArray(params.orderNo), | ||
lineItem_product: selectArray(params.sku), | ||
statusCode: selectArray(params.state) as OrderListQuery['statusCode'], | ||
}); | ||
} | ||
|
||
@Component({ | ||
selector: 'ish-account-order-filters', | ||
templateUrl: './account-order-filters.component.html', | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
providers: [{ provide: NgbDateAdapter, useClass: OrderDateFilterAdapter }], | ||
}) | ||
export class AccountOrderFiltersComponent implements OnInit, AfterViewInit { | ||
@Input() fragmentOnRouting: string; | ||
|
||
form = new UntypedFormGroup({}); | ||
|
||
fields: (FormlyFieldConfig & { key: keyof FormModel })[]; | ||
|
||
@Output() modelChange = new EventEmitter<Partial<OrderListQuery>>(); | ||
|
||
private destroyRef = inject(DestroyRef); | ||
|
||
constructor(private route: ActivatedRoute, private router: Router) {} | ||
|
||
ngOnInit() { | ||
this.fields = [ | ||
{ | ||
key: 'orderNo', | ||
type: 'ish-text-input-field', | ||
props: { | ||
placeholder: 'account.order-history.filter.label.order-no', | ||
label: 'account.order-history.filter.label.order-no', | ||
}, | ||
}, | ||
{ | ||
key: 'sku', | ||
type: 'ish-text-input-field', | ||
props: { | ||
placeholder: 'account.order-history.filter.label.sku', | ||
label: 'account.order-history.filter.label.sku', | ||
}, | ||
}, | ||
{ | ||
key: 'state', | ||
type: 'ish-select-field', | ||
templateOptions: { | ||
// keep-localization-pattern: ^account.order-history.filter.label.state.* | ||
options: filterableStates.map(s => ({ label: `account.order-history.filter.label.state.${s}`, value: s })), | ||
placeholder: 'account.order-history.filter.label.state', | ||
label: 'account.order-history.filter.label.state', | ||
}, | ||
}, | ||
{ | ||
key: 'date', | ||
type: 'ish-date-range-picker-field', | ||
props: { | ||
placeholder: 'common.placeholder.shortdate-caps', | ||
label: 'account.order-history.filter.label.date', | ||
minDays: -365 * 2, | ||
maxDays: 0, | ||
startDate: -30, | ||
}, | ||
}, | ||
]; | ||
} | ||
|
||
ngAfterViewInit(): void { | ||
this.route.queryParams.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(params => { | ||
this.form.patchValue(urlToModel(params)); | ||
this.modelChange.emit(urlToQuery(params)); | ||
}); | ||
} | ||
|
||
private navigate(queryParams: UrlModel) { | ||
this.router.navigate([], { | ||
relativeTo: this.route, | ||
queryParams, | ||
fragment: this.fragmentOnRouting, | ||
}); | ||
} | ||
|
||
submitForm() { | ||
this.navigate(modelToUrl(this.form.value)); | ||
} | ||
|
||
resetForm() { | ||
this.navigate(undefined); | ||
} | ||
} |
5 changes: 4 additions & 1 deletion
5
src/app/pages/account-order-history/account-order-history-page.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters