All files / src/components index.js

0% Statements 0/66
0% Branches 0/48
0% Functions 0/11
0% Lines 0/64

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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200                                                                                                                                                                                                                                                                                                                                                                                                               
// @flow
 
import * as React from 'react';
import Provider from './Provider';
import Generator from './Generator';
import hocs from '../hocs';
import {createEmptyData} from 'canner-helpers';
import {Parser, Traverser} from 'canner-compiler';
import {createClient, MemoryConnector} from 'canner-graphql-interface';
import {isEmpty, isPlainObject} from 'lodash';
// i18n
import en from 'react-intl/locale-data/en';
import zh from 'react-intl/locale-data/zh';
import {IntlProvider, addLocaleData} from 'react-intl';
import hocsLocales from '../hocs/components/locale';
import pluginsLocales from '@canner/antd-locales';
addLocaleData([...en, ...zh]);
 
// type
import type {CMSProps, LoadedSchema} from './types';
 
type Props = CMSProps;
type State = {
  dataChanged: Object
};
 
class CannerCMS extends React.Component<Props, State> {
  imageServiceConfigs: Object
  client: Client;
  provider: ?Provider;
  componentTree: any;
  schema: any;
 
  static defaultProps = {
    schema: {
      schema: {}
    },
    dataDidChange: () => {},
    afterDeploy: () => {},
    baseUrl: '/',
    intl: {
      locale: 'en',
      defaultLocale: 'en',
      messages: {
        'en': {}
      }
    },
    routerParams: {},
    routes: []
  }
 
  constructor(props: Props) {
    super(props);
    const {schema, visitors} = props.schema;
    this.componentTree = compile(schema, visitors);
    this.schema = Object.keys(schema).reduce((result: any, key: string) => {
      let v = {...schema[key]};
      if (v.type === 'array') {
        // v.items = v.items.items;
        v.items.id = {
          type: 'id'
        }
      }
      result[key] = v;
      return result;
    }, {});
    this.client = genClient({...props.schema, schema: schema});
    this.state = {
      dataChanged: {}
    }
  }
 
  dataDidChange = (dataChanged: Object) => {
    const {dataDidChange} = this.props;
    if (dataDidChange) {
      dataDidChange(dataChanged);
    }
    this.setState({
      dataChanged
    });
  }
 
  deploy = (key: string, id?: string): Promise<*> => {
    if (this.provider) {
      return this.provider.deploy(key, id);
    }
    return Promise.resolve();
  }
 
  reset = (key: string, id?: string): Promise<*> => {
    if (this.provider) {
      return this.provider.reset(key, id);
    }
    return Promise.resolve();
  }
 
  render() {
    const {
      baseUrl,
      routes,
      routerParams,
      goTo,
      afterDeploy,
      intl = {},
      hideButtons,
      schema: {storages, dict = {}}
    } = this.props;
    const currentLocale = intl.locale || 'en';
    return (
      <IntlProvider
        locale={currentLocale}
        defaultLocale={intl.defaultLocale || currentLocale}
        messages={{
          ...(pluginsLocales[currentLocale] || {}),
          ...(hocsLocales[currentLocale] || {}),
          ...(dict[currentLocale] || {}),
          ...((intl.messages || {})[currentLocale] || {})
        }}
      >
        <Provider
          ref={provider => this.provider = provider}
          client={this.client}
          schema={this.schema}
          dataDidChange={this.dataDidChange}
          afterDeploy={afterDeploy}
          rootKey={routes[0]}
        >
          <Generator
            storages={storages}
            schema={this.schema}
            componentTree={this.componentTree || {}}
            hocs={hocs}
            goTo={goTo}
            baseUrl={baseUrl}
            routes={routes}
            routerParams={routerParams || {}}
            hideButtons={hideButtons}
          />
        </Provider>
      </IntlProvider>
    );
  }
}
 
function compile(schema, visitors) {
  const parser = new Parser();
  const tree = parser.parse(schema);
  const traverser = new Traverser(tree);
  visitors.forEach(visitor => {
    traverser.addVisitor(visitor);
  });
  const componentTree = traverser.traverse();
  return componentTree;
}
 
export function genClient(schema: LoadedSchema) {
  const {
    resolvers,
    connector,
    graphqlClient,
  } = schema;
 
  const options: Object = {
    schema: schema.schema
  };
 
  if (connector) {
    if (isPlainObject(connector)) {
      if (!isEmpty(connector)) {
        options.connectors = connector
      }
    } else {
      options.connector = connector;
    }
  }
 
  if (graphqlClient) {
    if (isPlainObject(graphqlClient)) {
      if (!isEmpty(connector)) {
        options.graphqlClients = graphqlClient;
      }
    } else {
      options.graphqlClient = graphqlClient;
    }
  }
 
  if (isEmpty(connector) && isEmpty(graphqlClient)) {
    options.connector = new MemoryConnector({
      defaultData: createEmptyData(schema.schema)
    });
  }
 
  if (!isEmpty(resolvers)) {
    options.resolvers = resolvers
  }
  return createClient(options);
}
 
export default CannerCMS;