diff --git a/src/components/molecules/dragable-service/actions/add.js b/src/components/molecules/dragable-service/actions/add.js new file mode 100644 index 00000000..b0d1b36e --- /dev/null +++ b/src/components/molecules/dragable-service/actions/add.js @@ -0,0 +1,51 @@ +import { createAction } from 'redux-actions'; +import { fetch } from '../../../../utils'; +import { API_URL } from '../../../../backend_url'; +import { ADD_SERVICE } from '../constants'; + +export const reset = createAction(ADD_SERVICE, () => ({ + status: 'initial', +})); + +export const begin = createAction(ADD_SERVICE, () => ({ + status: 'pending', +})); + +export const success = createAction(ADD_SERVICE, service => ({ + service, + status: 'success', +})); + +export const fail = createAction(ADD_SERVICE, error => ({ + status: 'error', + error, +})); + +export const add = (clusterId, itemId) => + dispatch => { + dispatch(begin()); + fetch({ + url: `${API_URL}/clusters/${clusterId}/services`, + method: 'POST', + body: { + service_id: itemId, + }, + }) + .then(cluster => { + dispatch(success(cluster)); + return cluster; + }) + .catch(error => { + dispatch(fail(error.message)); + }); + }; + +const actions = { + reset, + begin, + success, + fail, + add, +}; + +export default actions; diff --git a/src/components/molecules/dragable-service/constants.js b/src/components/molecules/dragable-service/constants.js new file mode 100644 index 00000000..a2a3051d --- /dev/null +++ b/src/components/molecules/dragable-service/constants.js @@ -0,0 +1 @@ +export const ADD_SERVICE = 'ADD_SERVICE'; diff --git a/src/components/molecules/dragable-service/index.js b/src/components/molecules/dragable-service/index.js index 9e7f472d..e3555e4b 100644 --- a/src/components/molecules/dragable-service/index.js +++ b/src/components/molecules/dragable-service/index.js @@ -2,6 +2,7 @@ import React from 'react'; import { DragSource } from 'react-dnd'; import ItemTypes from './item-types'; import store from '../../../store'; +import actions from './actions/add'; const serviceSource = { @@ -16,7 +17,7 @@ const serviceSource = { const dropResult = monitor.getDropResult(); const cluster = store.getState().clusterDetail.cluster; if (dropResult) { - console.log(`You dropped ${item.id} into ${dropResult.name} with id ${cluster.id}!`); + store.dispatch(actions.add(cluster.id, item.id)); } }, }; diff --git a/src/components/molecules/dragable-service/reducers/add.js b/src/components/molecules/dragable-service/reducers/add.js new file mode 100644 index 00000000..1b3c5f37 --- /dev/null +++ b/src/components/molecules/dragable-service/reducers/add.js @@ -0,0 +1,13 @@ +import { ADD_SERVICE } from '../constants'; + +export default function applicationList( + state = { status: 'initial' }, + action +) { + switch (action.type) { + case ADD_SERVICE: + return action.payload; + default: + return state; + } +} diff --git a/src/components/molecules/dragable-service/reducers/index.js b/src/components/molecules/dragable-service/reducers/index.js new file mode 100644 index 00000000..ff961b4d --- /dev/null +++ b/src/components/molecules/dragable-service/reducers/index.js @@ -0,0 +1,9 @@ +import addService from './add'; + + +const reducers = [ + addService, +]; + + +export default reducers; diff --git a/src/components/pages/cluster/index.js b/src/components/pages/cluster/index.js index 173ccd05..cbfc0b71 100644 --- a/src/components/pages/cluster/index.js +++ b/src/components/pages/cluster/index.js @@ -23,6 +23,7 @@ import ServiceProvision from '../service-provision'; import Add from '../../atoms/add'; import Sidebar from '../../atoms/sidebar'; import AllServices from '../../organisms/service-list'; +import addService from '../../molecules/dragable-service/actions/add'; const mapStateToProps = (state) => { @@ -36,6 +37,7 @@ const mapStateToProps = (state) => { providerRemoveStatus: state.providerRemove.status, serviceRemove: state.clusterServiceRemove.service, serviceRemoveStatus: state.clusterServiceRemove.status, + /* addServiceStatus: state.addService.status,*/ }; if (state.clusterEdit.cluster) { data.cluster = state.clusterEdit.cluster; @@ -56,6 +58,7 @@ const ClusterDetail = React.createClass({ providerRemoveStatus: React.PropTypes.string, serviceRemove: React.PropTypes.object, serviceRemoveStatus: React.PropTypes.string, + addServiceStatus: React.PropTypes.string, }, getDefaultProps() { @@ -77,6 +80,7 @@ const ClusterDetail = React.createClass({ }, componentWillMount() { + console.log(store.getState()); store.dispatch(actions.get(this.props.params.clusterId)); store.dispatch(pluginActions.get()); }, @@ -92,6 +96,10 @@ const ClusterDetail = React.createClass({ } else if (nextProps.serviceRemoveStatus === 'success') { store.dispatch(actions.get(this.props.params.clusterId)); store.dispatch(serviceActionsRemove.reset()); + } else if (nextProps.addServiceStatus === 'success') { + store.dispatch(actions.get(this.props.params.clusterId)); + store.dispatch(addService.reset()); + console.log('here'); } }, diff --git a/src/reducers.js b/src/reducers.js index 17e65475..29a916c9 100644 --- a/src/reducers.js +++ b/src/reducers.js @@ -13,6 +13,7 @@ import service from './components/pages/service/reducers'; import serviceList from './components/pages/service-list/reducers'; import serviceProvision from './components/pages/service-provision/reducers'; import clusterServiceRemove from './components/organisms/cluster-service-list/reducers'; +import addService from './components/molecules/dragable-service/reducers'; const reducers = { @@ -73,6 +74,11 @@ application.map(reducer => { return undefined; }); +addService.map(reducer => { + reducers[reducer.name] = reducer; + return undefined; +}); + const rootReducer = combineReducers(reducers);