Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure the loading indicator and the refresh button animated at the correct time. #3363

Merged
merged 6 commits into from
Jan 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/frontend/app/shared/components/list/list.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@
</button>
</div>
<button id="app-list-refresh-button" mat-icon-button *ngIf="dataSource.refresh" [disabled]="(dataSource.isLoadingPage$ | async) || (isAddingOrSelecting$ | async)" (click)="refresh()">
<mat-icon class="refresh-icon" [ngClass]="{refreshing: isRefreshing}" aria-label="Refresh list data">refresh</mat-icon>
<mat-icon class="refresh-icon" [ngClass]="{refreshing: (isRefreshing$ | async)}" aria-label="Refresh list data">refresh</mat-icon>
</button>
</div>

</mat-card>
</div>
<div class="refresh-button__no-header" *ngIf="dataSource.refresh && !(hasControls$ | async) && (!(hasRowsOrIsFiltering$ | async) && noEntries)">
<button id="app-list-refresh-button" mat-mini-fab *ngIf="!(isAddingOrSelecting$ | async)" [disabled]="dataSource.isLoadingPage$ | async" (click)="refresh()">
<mat-icon class="refresh-icon" [ngClass]="{refreshing: isRefreshing}" aria-label="Refresh list data">refresh</mat-icon>
<mat-icon class="refresh-icon" [ngClass]="{refreshing: (isRefreshing$ | async)}" aria-label="Refresh list data">refresh</mat-icon>
</button>
</div>
<div class="list-component__body">
Expand Down
48 changes: 27 additions & 21 deletions src/frontend/app/shared/components/list/list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ import {
isObservable,
Observable,
of as observableOf,
queueScheduler,
Subscription,
} from 'rxjs';
import {
combineLatest,
debounceTime,
distinctUntilChanged,
filter,
Expand All @@ -41,9 +39,10 @@ import {
subscribeOn,
takeWhile,
tap,
throttleTime,
withLatestFrom,
} from 'rxjs/operators';

import { safeUnsubscribe } from '../../../core/utils.service';
import { ListFilter, ListPagination, ListSort, SetListViewAction } from '../../../store/actions/list.actions';
import { AppState } from '../../../store/app-state';
import { entityFactory } from '../../../store/helpers/entity-factory';
Expand All @@ -65,7 +64,6 @@ import {
ListViewTypes,
MultiFilterManager,
} from './list.component.types';
import { safeUnsubscribe } from '../../../core/utils.service';


@Component({
Expand Down Expand Up @@ -186,7 +184,7 @@ export class ListComponent<T> implements OnInit, OnChanges, OnDestroy, AfterView
isFiltering$: Observable<boolean>;
noRowsNotFiltering$: Observable<boolean>;
showProgressBar$: Observable<boolean>;
public isRefreshing = false;
isRefreshing$: Observable<boolean>;



Expand Down Expand Up @@ -492,30 +490,39 @@ export class ListComponent<T> implements OnInit, OnChanges, OnDestroy, AfterView
})
);

const hasChangedPage$ = this.dataSource.pagination$.pipe(
map(pag => pag.currentPage),
const loadingHasChanged$ = this.dataSource.isLoadingPage$.pipe(
distinctUntilChanged(),
pairwise(),
map(([oldPage, newPage]) => oldPage !== newPage),
map(([oldLoading, newLoading]) =>
oldLoading !== newLoading
),
startWith(true)
);

const isMaxedResult$ = this.dataSource.pagination$.pipe(
map(pag => !!pag.maxedResults),
const hasFilterChangedSinceLastLoading$ = loadingHasChanged$.pipe(
filter(hasChanged => hasChanged),
withLatestFrom(this.dataSource.pagination$),
pairwise(),
map(([oldData, newData]) => [oldData[1], newData[1]]),
map(([oldPage, newPage]) =>
oldPage.currentPage !== newPage.currentPage ||
oldPage.clientPagination.filter !== newPage.clientPagination.filter ||
oldPage.params !== newPage.params
),
startWith(true)
);

const canShowLoading$ = observableCombineLatest([hasChangedPage$, isMaxedResult$]).pipe(
map(([hasChangedPage, isLoadingMaxedResults]) => hasChangedPage || isLoadingMaxedResults),
startWith(true),
distinctUntilChanged()
this.isRefreshing$ = observableCombineLatest(hasFilterChangedSinceLastLoading$, this.dataSource.isLoadingPage$).pipe(
map(([hasChanged, loading]) => !hasChanged && loading),
startWith(false)
);

this.showProgressBar$ = this.dataSource.isLoadingPage$.pipe(
startWith(true),
combineLatest(canShowLoading$),
map(([loading, canShowLoading]) => loading && canShowLoading && !this.isRefreshing),
withLatestFrom(this.isRefreshing$),
map(([loading, isRefreshing]) => !isRefreshing && loading),
distinctUntilChanged(),
throttleTime(100, queueScheduler, { leading: true, trailing: true }),
startWith(true),
);

}

ngAfterViewInit() {
Expand Down Expand Up @@ -591,7 +598,6 @@ export class ListComponent<T> implements OnInit, OnChanges, OnDestroy, AfterView

public refresh() {
if (this.dataSource.refresh) {
this.isRefreshing = true;
this.dataSource.refresh();
this.dataSource.isLoadingPage$.pipe(
tap(isLoading => {
Expand All @@ -600,7 +606,7 @@ export class ListComponent<T> implements OnInit, OnChanges, OnDestroy, AfterView
}
}),
takeWhile(isLoading => isLoading)
).subscribe(null, null, () => this.isRefreshing = false);
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ function mergePaginationSections(
seedPagination: PaginationEntityState,
defaultState: PaginationEntityState
) {
const currentClientPagination = spreadClientPagination(currentPagination.clientPagination);
const seedClientPagination = spreadClientPagination(seedPagination.clientPagination);
return {
...currentClientPagination,
...currentPagination.clientPagination,
totalResults: seedClientPagination.totalResults,
currentPage: getCurrentPage(currentPagination, seedPagination)
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { SetClientPageSize } from '../../actions/pagination.actions';
import { PaginationEntityState } from '../../types/pagination.types';
import { spreadClientPagination } from './pagination-reducer.helper';

export function paginationSetClientPageSize(state: PaginationEntityState, action: SetClientPageSize) {
return {
...state,
error: false,
clientPagination: {
...spreadClientPagination(state.clientPagination),
...state.clientPagination,
pageSize: action.pageSize
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { SetClientPage } from '../../actions/pagination.actions';
import { PaginationEntityState } from '../../types/pagination.types';
import { spreadClientPagination } from './pagination-reducer.helper';

export function paginationSetClientPage(state: PaginationEntityState, action: SetClientPage) {
return {
...state,
error: false,
clientPagination: {
...spreadClientPagination(state.clientPagination),
...state.clientPagination,
currentPage: action.pageNumber
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { SetResultCount } from '../../actions/pagination.actions';
import { PaginationEntityState } from '../../types/pagination.types';
import { spreadClientPagination } from './pagination-reducer.helper';

export function paginationSetResultCount(state: PaginationEntityState, action: SetResultCount) {
if (state.totalResults === action.count && state.clientPagination.totalResults === action.count) {
Expand All @@ -11,7 +10,7 @@ export function paginationSetResultCount(state: PaginationEntityState, action: S
error: false,
totalResults: action.count,
clientPagination: {
...spreadClientPagination(state.clientPagination),
...state.clientPagination,
totalResults: action.count
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { PaginationEntityState } from '../../types/pagination.types';
import { spreadClientPagination } from './pagination-reducer.helper';

export function paginationSuccess(state: PaginationEntityState, action): PaginationEntityState {
const { apiAction, response, result } = action;
Expand All @@ -26,7 +25,7 @@ export function paginationSuccess(state: PaginationEntityState, action): Paginat
pageCount: totalPages,
totalResults,
clientPagination: {
...spreadClientPagination(state.clientPagination),
...state.clientPagination,
totalResults
}
};
Expand Down