Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { connect } from 'react-redux';
- import { replace } from 'react-router-redux';
- import React from 'react';
- import { PROGRAM_APPEASEMENT_DETAIL, PROGRAM_APPEASEMENTS } from 'config/routes';
- import toPath from 'core/lib/toPath';
- import * as OvationsApi from 'core/ovations-api';
- import * as appeasement from 'redux-modules/appeasement';
- import { clientContextManager } from 'redux-modules/root';
- import { InterstitialProps } from 'containers/Container';
- import Spinner from 'components/Spinner';
- import { DEFAULT_PAGE_SIZE } from 'config/global';
- const getCache = (props: InterstitialProps) => {
- const ctx = clientContextManager.getContext(props, props.match.params.clientId);
- return appeasement.selectors.getList(ctx.appeasement);
- };
- const getRequest = (props: InterstitialProps) => OvationsApi.Appeasement.deserializeRequest(props.location.search);
- const refreshCache = async (props: InterstitialProps, request: OvationsApi.Types.AppeasementSearchRequest) => {
- const { clientId, programId } = props.match.params;
- await props.dispatch(appeasement.actions.search(clientId, programId, request));
- };
- const getRedirect = (props: InterstitialProps, cacheHit: OvationsApi.Types.AppeasementDetail, request: OvationsApi.Types.AppeasementSearchRequest) => {
- const { clientId, programId } = props.match.params;
- const path = (cacheHit)
- ? toPath(PROGRAM_APPEASEMENT_DETAIL, { clientId, programId, appeasementId: cacheHit.id })
- : toPath(PROGRAM_APPEASEMENTS, props.match.params);
- return path + '?' + OvationsApi.Appeasement.serializeRequest(request);
- };
- const _getPage = (request: OvationsApi.Types.AppeasementSearchRequest) => request.page || 1;
- const _getFromCache = (props: InterstitialProps, request: OvationsApi.Types.AppeasementSearchRequest) => {
- const index = parseInt(props.match.params.index, 10);
- const cache = getCache(props);
- const indexOnPage = index - ((_getPage(request) - 1) * DEFAULT_PAGE_SIZE);
- return cache[indexOnPage];
- }
- export class AppeasementLoader extends React.Component<InterstitialProps> {
- async componentDidMount() {
- let request = getRequest(this.props);
- let cacheHit = _getFromCache(this.props, request);
- if (!cacheHit) {
- const index = parseInt(this.props.match.params.index, 10);
- const page = Math.floor(index / DEFAULT_PAGE_SIZE) + 1;
- request = { ...request, page };
- await refreshCache(this.props, request);
- cacheHit = _getFromCache(this.props, request);
- }
- this.props.dispatch(replace(getRedirect(this.props, cacheHit, request)));
- }
- render() {
- return <Spinner />;
- }
- }
- export default connect(state => state)(AppeasementLoader);
Add Comment
Please, Sign In to add comment