Skip to content

Commit

Permalink
simplify list component
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Jan 31, 2024
1 parent 0529c51 commit fa8379e
Showing 1 changed file with 17 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ export class <%= componentName %> implements OnInit {
protected modalService = inject(NgbModal);
<%_ } _%>

constructor() {
this.subscription = combineLatest([this.activatedRoute.queryParamMap, this.activatedRoute.data]).pipe(
tap(([params, data]) => this.fillComponentAttributeFromRoute(params, data)),
tap(() => this.load()),
).subscribe();

const { queryParamMap, data } = this.activatedRoute.snapshot;
this.fillComponentAttributeFromRoute(queryParamMap, data);
}

<%_ if (paginationInfiniteScroll) { _%>
reset(): void {
this.page = 1;
Expand Down Expand Up @@ -217,21 +227,13 @@ export class <%= componentName %> implements OnInit {
modalRef.closed
.pipe(
filter(reason => reason === ITEM_DELETED_EVENT),
switchMap(() => this.loadFromBackendWithRouteInformations())
).subscribe(
{
next: (res: EntityArrayResponseType) => {
this.onResponseSuccess(res);
}
});
tap(() => this.load())
).subscribe();
}

<%_ } _%>
load(): void {
if (this.subscription !== null) {
this.subscription.unsubscribe();
}
this.subscription = this.loadFromBackendWithRouteInformations().subscribe({
this.queryBackend().subscribe({
next: (res: EntityArrayResponseType) => {
this.onResponseSuccess(res);
}
Expand All @@ -248,16 +250,6 @@ export class <%= componentName %> implements OnInit {
}

<%_ } _%>
protected loadFromBackendWithRouteInformations(): Observable<EntityArrayResponseType> {
return combineLatest([this.activatedRoute.queryParamMap, this.activatedRoute.data])
.pipe(
tap(([params, data]) => this.fillComponentAttributeFromRoute(params, data)),
switchMap(() =>
this.queryBackend(<% if (!paginationNo) { %>this.page, <% } %>this.predicate, this.ascending<% if (jpaMetamodelFiltering && paginationPagination) { %>, this.filters.filterOptions<% } %><% if (searchEngineAny) { %>, this.currentSearch<% } %>)
)
);
}

protected fillComponentAttributeFromRoute(params: ParamMap, data: Data): void {
<%_ if (paginationPagination) { _%>
const page = params.get(PAGE_HEADER);
Expand Down Expand Up @@ -338,10 +330,11 @@ export class <%= componentName %> implements OnInit {
}

<%_ } _%>
protected queryBackend(<% if (!paginationNo) { %>page?: number, <% } %>predicate?: string, ascending?: boolean<% if (jpaMetamodelFiltering && paginationPagination) { %>, filterOptions?: IFilterOption[]<% } %><% if (searchEngineAny) { %>, currentSearch?: string<% } %>): Observable<EntityArrayResponseType> {
protected queryBackend(): Observable<EntityArrayResponseType> {
const { <% if (!paginationNo) { %>page, <% } %>predicate, ascending<% if (jpaMetamodelFiltering && paginationPagination) { %>, filters<% } %><% if (searchEngineAny) { %>, currentSearch<% } %> } = this;
this.isLoading = true;
<%_ if (!paginationNo) { _%>
const pageToLoad: number = page ?? 1;
const pageToLoad: number = page;
<%_ } _%>
const queryObject<% if ((jpaMetamodelFiltering && paginationPagination) || searchEngine) { %>: any <% } %> = {
<%_ if (!paginationNo) { _%>
Expand All @@ -357,7 +350,7 @@ export class <%= componentName %> implements OnInit {
sort: this.getSortQueryParam(predicate, ascending),
};
<%_ if (jpaMetamodelFiltering && paginationPagination) { _%>
filterOptions?.forEach(filterOption => {
filters.filterOptions.forEach(filterOption => {
queryObject[filterOption.name] = filterOption.values;
});
<%_ } _%>
Expand Down

0 comments on commit fa8379e

Please sign in to comment.