示例#1
0
  constructor(props) {
    super(props);

    this.state = {
      pluginsLoading: !!APP_PLUGINS.length,
    };

    this.auth = new Auth();
    this.pluginsRegistry = new PluginsRegistry(this);
    window.META_HOST_APP = this;

    axios.defaults.withCredentials = true;
    axios.defaults.headers.common['Content-Type'] = 'application/json';

    const cachedLang = localStorage.getItem(LOCAL_LANG);
    if (cachedLang) {
      languageSuccess(cachedLang);
    }

    axios.interceptors.response.use(
      function(response) {
        return response;
      },
      function(error) {
        const errorPrototype = Object.getPrototypeOf(error);

        // This is a canceled request error
        if (errorPrototype && errorPrototype.__CANCEL__) {
          return Promise.reject(error);
        }

        if (!error.response) {
          store.dispatch(noConnection(true));
        }

        /*
         * Authorization error
         */
        if (error.response.status == 401) {
          store.dispatch(setProcessSaved());
          logoutSuccess(this.auth);
          store.dispatch(push('/login?redirect=true'));
        } else if (error.response.status == 503) {
          store.dispatch(noConnection(true));
        } else if (error.response.status != 404) {
          if (localStorage.isLogged) {
            const errorMessenger = code => {
              switch (code) {
                case 500:
                  return 'Server error';
                case 400:
                  return 'Client error';
              }
            };
            const { data, status } = error.response;
            const errorTitle = errorMessenger(status);
            const message = data.message ? data.message : '';

            // eslint-disable-next-line no-console
            data.message && console.error(data.message);

            // Chart disabled notifications
            if (
              error.response.request.responseURL.includes('silentError=true')
            ) {
              return;
            }

            store.dispatch(
              addNotification(
                'Error: ' + message.split(' ', 4).join(' ') + '...',
                data.message,
                5000,
                'error',
                errorTitle
              )
            );
          }
        }

        //reset password errors
        if (error.response.request.responseURL.includes('resetPassword')) {
          return Promise.reject(error.response);
        }

        if (error.response.request.responseURL.includes('showError=true')) {
          const { data } = error.response;

          store.dispatch(
            addNotification(
              'Error: ' + data.message.split(' ', 4).join(' ') + '...',
              data.message,
              5000,
              'error',
              ''
            )
          );
        } else {
          return Promise.reject(error);
        }
      }.bind(this)
    );

    getAvailableLang().then(response => {
      const { defaultValue, values } = response.data;
      const valuesFlatten = values.map(item => Object.keys(item)[0]);
      const lang =
        valuesFlatten.indexOf(navigator.language) > -1
          ? navigator.language
          : defaultValue;

      languageSuccess(lang);
    });

    counterpart.setMissingEntryGenerator(() => '');

    if (APP_PLUGINS.length) {
      const plugins = APP_PLUGINS.map(plugin => {
        const waitForChunk = () =>
          import(`@plugins/${plugin}/index.js`)
            .then(module => module)
            .catch(() => {
              // eslint-disable-next-line no-console
              console.error(`Error loading plugin ${plugin}`);
            });

        return new Promise(resolve =>
          waitForChunk().then(file => {
            this.pluginsRegistry.addEntry(plugin, file);
            resolve({ name: plugin, file });
          })
        );
      });

      Promise.all(plugins).then(res => {
        const plugins = res.reduce((prev, current) => prev.concat(current), []);

        if (plugins.length) {
          store.dispatch(addPlugins(plugins));
        }

        plugins.forEach(({ file }) => {
          if (file.reducers && file.reducers.name) {
            store.attachReducers({
              plugins: {
                [`${file.reducers.name}`]: file.reducers.reducer,
              },
            });
          }
        });

        this.setState({
          pluginsLoading: false,
        });
      });
    }
  }
export function setMissingEntryGenerator(f) {
    counterpart.setMissingEntryGenerator(f);
}