Skip to content

Instantly share code, notes, and snippets.

@jsCommander
Created July 27, 2021 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsCommander/3ea7b352c31a6c4c3b72ba2e654ecb8e to your computer and use it in GitHub Desktop.
Save jsCommander/3ea7b352c31a6c4c3b72ba2e654ecb8e to your computer and use it in GitHub Desktop.
initPatient$ = createEffect(() =>
this.actions$.pipe(
ofType(ROUTER_NAVIGATION),
filter(
(action: RouterNavigationAction) =>
new URL(
window.location.protocol + window.location.hostname + action.payload.routerState.url
).pathname === '/patients/search'
),
mergeMap((action: RouterNavigationAction) =>
this.store.pipe(
select(lastNavigatedUrlFeatureKey),
filter((lastNavigatedUrl) => {
return !lastNavigatedUrl.includes(
new URL(
window.location.protocol +
window.location.hostname +
action.payload.routerState.url
).pathname
);
}),
first(),
mergeMap(() =>
this.store.pipe(
select('router'),
first(),
mergeMap((router) =>
this.store.pipe(
select(checkCaseColumnsExistsSelector),
filter((val) => val !== null),
first(),
mergeMap(() =>
this.store.pipe(
select(allowToInitPatientsSelector),
filter((val) => val),
first(),
mergeMap((caseColumnsExists) =>
this.store.pipe(
select(configFeatureKey),
filter((val) => Boolean(val)),
first(),
tap(() =>
this.store.dispatch(incrementPendingOperationCount())
),
mergeMapTo(
new Observable<GetByKpiResponse>(
(subscriber: Subscriber<GetByKpiResponse>) => {
this.patientsService
.getPatientsByKpi({
Offset: router.state.root.queryParams
.page
? '' +
(router.state.root.queryParams
.page -
1)
: '0',
Limit: router.state.root.queryParams
.pageSize
? '' +
router.state.root.queryParams
.pageSize
: this.DEFAULT_PAGE_SIZE,
'sort-field': 'fullName',
'sort-direction': '1',
})
.then((res: GetByKpiResponse) => {
if (
+router.state.root.queryParams
?.page
) {
if (
router.state.root.queryParams
?.page === '1'
) {
this.router.navigate(
['/patients/search'],
{
relativeTo: this
.route,
queryParams: {
page: null,
},
}
);
}
this.store.dispatch(
setPage({
page:
+router.state.root
.queryParams
.page - 1,
})
);
}
if (
router.state.root.queryParams
?.pageSize
) {
this.store.dispatch(
setPageSize({
pageSize: +router.state
.root.queryParams
.pageSize,
})
);
}
subscriber.next(res);
subscriber.complete();
})
.catch((err) => subscriber.error(err));
}
).pipe(
catchError((err) => {
console.error(err);
this.store.dispatch(
decrementPendingOperationCount()
);
this.store.dispatch(
setDirectories({ directories: {} })
);
return of(null);
})
)
),
map((res) =>
res
? patientsLoaded({
patients: denormalizePatients(res?.pd),
total: res?.length,
preservePage: true,
})
: patientsLoadError({
error: 'Ошибка при загрузке пациентов.',
})
)
)
)
)
)
)
)
)
)
)
)
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment