All files / src/hocs containerQuery.js

0% Statements 0/39
0% Branches 0/22
0% Functions 0/17
0% Lines 0/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115                                                                                                                                                                                                                                     
// @flow
 
import * as React from 'react';
import {mapValues, get, isPlainObject, isArray} from 'lodash';
import type {HOCProps} from './types';
 
type State = {
  value: any,
  recordValue: Object,
  originRootValue: Object,
  rootValue: Object,
  isFetching: boolean
}
 
// $FlowFixMe
export default function withContainerRouter(Com: React.ComponentType<*>) {
  return class ContainerWithRouter extends React.Component<HOCProps, State> {
    state = {
      value: {},
      recordValue: {},
      originRootValue: {},
      rootValue: {},
      isFetching: false
    };
    key: string;
    subscription: any;
    constructor(props: HOCProps) {
      super(props);
      this.key = props.refId.getPathArr()[0];
    }
 
    componentDidMount() {
      if (this.key) {
        this.queryData();
        this.subscribe();
      }
    }
 
    componentWillUnmount() {
      this.unsubscribe();
    }
    
    queryData = (props?: HOCProps): Promise<*> => {
      const {refId, fetch} = props || this.props;
      return fetch(this.key).then(data => {
        const rootValue = parseConnectionToNormal(data);
        this.setState({
          originRootValue: data,
          rootValue,
          recordValue: getRecordValue(rootValue, refId),
          value: getValue(data, refId.getPathArr()),
          isFetching: false
        });
      });
    }
 
    subscribe = () => {
      const {subscribe, refId} = this.props;
      const subscription = subscribe(this.key, (data) => {
        const rootValue = parseConnectionToNormal(data);
        this.setState({
          originRootValue: data,
          rootValue,
          recordValue: getRecordValue(rootValue, refId),
          value: getValue(data, refId.getPathArr()),
          isFetching: false
        });
      });
      this.subscription = subscription;
    }
 
    unsubscribe = () => {
      if (this.subscription) {
        this.subscription.unsubscribe();
      }
    }
 
    render() {
      const {value, recordValue} = this.state;
      return <Com {...this.props} value={value} recordValue={recordValue}/>;
    }
  };
}
 
export function getValue(value: Map<string, *>, idPathArr: Array<string>) {
  return idPathArr.reduce((result: any, key: string) => {
    if (isPlainObject(result)) {
      if ('edges' in result && 'pageInfo' in result) {
        return get(result, ['edges', key, 'node']);
      }
      return get(result, key);
    } else if (isArray(result)) {
      return get(result, key);
    } else {
      return result;
    }
  }, value);
}
 
export function parseConnectionToNormal(value: any) {
  if (isPlainObject(value)) {
    if (value.edges && value.pageInfo) {
      return value.edges.map(edge => parseConnectionToNormal(edge.node));
    }
    return mapValues(value, item => parseConnectionToNormal(item));
  } else if (isArray(value)) {
    return value.map(item => parseConnectionToNormal(item))
  } else {
    return value;
  }
}
 
function getRecordValue(rootValue, refId) {
  return get(rootValue, refId.getPathArr(), {});
}