Guest User

Untitled

a guest
Sep 20th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. import { connect } from 'react-redux';
  2. import { replace } from 'react-router-redux';
  3. import React from 'react';
  4.  
  5. import { PROGRAM_APPEASEMENT_DETAIL, PROGRAM_APPEASEMENTS } from 'config/routes';
  6. import toPath from 'core/lib/toPath';
  7. import * as OvationsApi from 'core/ovations-api';
  8. import * as appeasement from 'redux-modules/appeasement';
  9. import { clientContextManager } from 'redux-modules/root';
  10. import { InterstitialProps } from 'containers/Container';
  11. import Spinner from 'components/Spinner';
  12. import { DEFAULT_PAGE_SIZE } from 'config/global';
  13.  
  14. const getCache = (props: InterstitialProps) => {
  15. const ctx = clientContextManager.getContext(props, props.match.params.clientId);
  16. return appeasement.selectors.getList(ctx.appeasement);
  17. };
  18.  
  19. const getRequest = (props: InterstitialProps) => OvationsApi.Appeasement.deserializeRequest(props.location.search);
  20.  
  21. const refreshCache = async (props: InterstitialProps, request: OvationsApi.Types.AppeasementSearchRequest) => {
  22. const { clientId, programId } = props.match.params;
  23. await props.dispatch(appeasement.actions.search(clientId, programId, request));
  24. };
  25.  
  26. const getRedirect = (props: InterstitialProps, cacheHit: OvationsApi.Types.AppeasementDetail, request: OvationsApi.Types.AppeasementSearchRequest) => {
  27. const { clientId, programId } = props.match.params;
  28. const path = (cacheHit)
  29. ? toPath(PROGRAM_APPEASEMENT_DETAIL, { clientId, programId, appeasementId: cacheHit.id })
  30. : toPath(PROGRAM_APPEASEMENTS, props.match.params);
  31.  
  32. return path + '?' + OvationsApi.Appeasement.serializeRequest(request);
  33. };
  34.  
  35. const _getPage = (request: OvationsApi.Types.AppeasementSearchRequest) => request.page || 1;
  36.  
  37. const _getFromCache = (props: InterstitialProps, request: OvationsApi.Types.AppeasementSearchRequest) => {
  38. const index = parseInt(props.match.params.index, 10);
  39. const cache = getCache(props);
  40. const indexOnPage = index - ((_getPage(request) - 1) * DEFAULT_PAGE_SIZE);
  41. return cache[indexOnPage];
  42. }
  43.  
  44. export class AppeasementLoader extends React.Component<InterstitialProps> {
  45. async componentDidMount() {
  46. let request = getRequest(this.props);
  47. let cacheHit = _getFromCache(this.props, request);
  48. if (!cacheHit) {
  49. const index = parseInt(this.props.match.params.index, 10);
  50. const page = Math.floor(index / DEFAULT_PAGE_SIZE) + 1;
  51. request = { ...request, page };
  52. await refreshCache(this.props, request);
  53. cacheHit = _getFromCache(this.props, request);
  54. }
  55. this.props.dispatch(replace(getRedirect(this.props, cacheHit, request)));
  56. }
  57. render() {
  58. return <Spinner />;
  59. }
  60. }
  61.  
  62. export default connect(state => state)(AppeasementLoader);
Add Comment
Please, Sign In to add comment