clientjs/packages/annotation-dashboard-explorunivers/src/store.js
author ymh <ymh.work@gmail.com>
Mon, 17 Sep 2018 01:35:46 +0200
changeset 4 df751568fda6
parent 0 5f4fcbc80b37
child 6 819a7ab4e3fb
permissions -rw-r--r--
Add dashboardId for calculating discussionId

import React, { Component } from 'react';

import thunkMiddleware from 'redux-thunk';
import { Provider } from 'react-redux';
import { createLogger } from 'redux-logger';
import { createStore, applyMiddleware, compose } from 'redux';

import { rootReducer, actions } from 'dashboard-components';

import { dashboardId } from './config.json';

const { fetchAnnotations } = actions;

let middlewares = [thunkMiddleware];

if (process.env.NODE_ENV !== 'production') {
  const loggerMiddleware = createLogger();
  middlewares = [...middlewares, loggerMiddleware];
}

/* eslint-disable no-underscore-dangle */
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
/* eslint-enable */

const store = createStore(
  rootReducer,
  composeEnhancers(applyMiddleware(...middlewares)),
);

export default WrappedComponent => class extends Component {
  componentWillMount() {
    const apiUrl = process.env.REACT_APP_API_URL;
    const discussionUrl = process.env.REACT_APP_DISCUSSION_URL;
    store.dispatch(fetchAnnotations(apiUrl, discussionUrl, dashboardId));
  }

  render() {
    return <Provider store={store}><WrappedComponent {...this.props} /></Provider>;
  }
};