-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #343 from mekanix/feature/add-service-to-cluster
Drag and drop service onto cluster
- Loading branch information
Showing
27 changed files
with
422 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React from 'react'; | ||
import { DragSource } from 'react-dnd'; | ||
import ItemTypes from './item-types'; | ||
import store from '../../../store'; | ||
|
||
|
||
const serviceSource = { | ||
beginDrag(props) { | ||
return { | ||
id: props.id, | ||
}; | ||
}, | ||
|
||
endDrag(props, monitor) { | ||
const item = monitor.getItem(); | ||
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}!`); | ||
} | ||
}, | ||
}; | ||
|
||
|
||
const DragableService = React.createClass({ | ||
propTypes: { | ||
name: React.PropTypes.string.isRequired, | ||
id: React.PropTypes.string.isRequired, | ||
connectDragSource: React.PropTypes.func.isRequired, | ||
isDragging: React.PropTypes.bool.isRequired, | ||
}, | ||
|
||
render() { | ||
const { connectDragSource } = this.props; | ||
const { name, id } = this.props; | ||
return ( | ||
connectDragSource( | ||
<div>{name}: {id}</div> | ||
) | ||
); | ||
}, | ||
}); | ||
|
||
|
||
/* eslint-disable new-cap */ | ||
export default DragSource(ItemTypes.SERVICE, serviceSource, (connect, monitor) => ({ | ||
/* eslint-enable */ | ||
connectDragSource: connect.dragSource(), | ||
isDragging: monitor.isDragging(), | ||
}))(DragableService); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default { | ||
SERVICE: 'service', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
62 changes: 62 additions & 0 deletions
62
src/components/organisms/cluster-service-list/actions/remove.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { createAction } from 'redux-actions'; | ||
import { fetch } from '../../../../utils'; | ||
import { API_URL } from '../../../../backend_url'; | ||
import { CLUSTER_SERVICE_REMOVE } from '../constants'; | ||
|
||
|
||
export const reset = createAction(CLUSTER_SERVICE_REMOVE, () => ({ | ||
status: 'initial', | ||
})); | ||
|
||
export const begin = createAction(CLUSTER_SERVICE_REMOVE, () => ({ | ||
status: 'pending', | ||
})); | ||
|
||
export const success = createAction(CLUSTER_SERVICE_REMOVE, service => ({ | ||
service, | ||
status: 'success', | ||
})); | ||
|
||
export const fail = createAction(CLUSTER_SERVICE_REMOVE, error => ({ | ||
status: 'error', | ||
error, | ||
})); | ||
|
||
export const confirm = createAction(CLUSTER_SERVICE_REMOVE, id => ({ | ||
status: 'confirm', | ||
service: { | ||
id, | ||
}, | ||
})); | ||
|
||
|
||
export const remove = id => | ||
dispatch => { | ||
dispatch(begin()); | ||
fetch({ | ||
url: `${API_URL}/${id}`, | ||
method: 'DELETE', | ||
body: { | ||
id, | ||
}, | ||
}) | ||
.then(service => { | ||
dispatch(success(service)); | ||
return service; | ||
}) | ||
.catch(error => { | ||
dispatch(fail(error.message)); | ||
}); | ||
}; | ||
|
||
|
||
const actions = { | ||
reset, | ||
begin, | ||
success, | ||
fail, | ||
confirm, | ||
remove, | ||
}; | ||
|
||
export default actions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const CLUSTER_SERVICE_REMOVE = 'CLUSTER_SERVICE_REMOVE'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import React from 'react'; | ||
import { Link } from 'react-router'; | ||
import { DropTarget } from 'react-dnd'; | ||
import Service from '../../molecules/service'; | ||
import List from '../../molecules/transition-appear'; | ||
import removeActions from './actions/remove'; | ||
import ItemTypes from '../../molecules/dragable-service/item-types'; | ||
|
||
|
||
const clusterTarget = { | ||
drop() { | ||
return { | ||
name: 'Cluster', | ||
}; | ||
}, | ||
}; | ||
|
||
|
||
const ClusterServiceList = React.createClass({ | ||
propTypes: { | ||
services: React.PropTypes.array, | ||
children: React.PropTypes.node, | ||
clusterId: React.PropTypes.string.isRequired, | ||
connectDropTarget: React.PropTypes.func.isRequired, | ||
isOver: React.PropTypes.bool.isRequired, | ||
canDrop: React.PropTypes.bool.isRequired, | ||
}, | ||
|
||
getDefaultProps() { | ||
return { | ||
services: [], | ||
}; | ||
}, | ||
|
||
render() { | ||
const clusterUrl = `clusters/${this.props.clusterId}`; | ||
const serviceContent = this.props.services.map(service => { | ||
const url = `${clusterUrl}/services/${service.id}/provision`; | ||
const iconId = `${clusterUrl}/services/${service.id}`; | ||
return ( | ||
<Link to={url} key={service.id}> | ||
<Service | ||
name={service.name} | ||
iconId={iconId} | ||
close={removeActions.confirm} | ||
/> | ||
</Link> | ||
); | ||
}); | ||
const content = this.props.services ? serviceContent : this.props.children; | ||
const { canDrop, isOver, connectDropTarget } = this.props; | ||
const isActive = canDrop && isOver; | ||
|
||
let backgroundColor = 'white'; | ||
if (isActive) { | ||
backgroundColor = 'darkgreen'; | ||
} else if (canDrop) { | ||
backgroundColor = 'darkkhaki'; | ||
} | ||
|
||
return ( | ||
connectDropTarget( | ||
<div style={{ backgroundColor }}> | ||
<h3>Services</h3> | ||
<List> | ||
{content} | ||
</List> | ||
</div> | ||
) | ||
); | ||
}, | ||
}); | ||
|
||
|
||
/* eslint-disable new-cap */ | ||
export default DropTarget(ItemTypes.SERVICE, clusterTarget, (connect, monitor) => ({ | ||
/* eslint-enable */ | ||
connectDropTarget: connect.dropTarget(), | ||
isOver: monitor.isOver(), | ||
canDrop: monitor.canDrop(), | ||
}))(ClusterServiceList); |
9 changes: 9 additions & 0 deletions
9
src/components/organisms/cluster-service-list/reducers/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import clusterServiceRemove from './remove'; | ||
|
||
|
||
const reducers = [ | ||
clusterServiceRemove, | ||
]; | ||
|
||
|
||
export default reducers; |
17 changes: 17 additions & 0 deletions
17
src/components/organisms/cluster-service-list/reducers/remove.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { CLUSTER_SERVICE_REMOVE } from '../constants'; | ||
|
||
|
||
export default function clusterServiceRemove( | ||
state = { status: 'initial', service: {} }, | ||
action | ||
) { | ||
switch (action.type) { | ||
case CLUSTER_SERVICE_REMOVE: | ||
return { | ||
service: action.payload.service, | ||
status: action.payload.status, | ||
}; | ||
default: | ||
return state; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React from 'react'; | ||
import { connect } from 'react-redux'; | ||
import DragableService from '../../molecules/dragable-service'; | ||
import actions from '../../pages/service-list/actions'; | ||
import store from '../../../store'; | ||
|
||
|
||
const mapStateToProps = state => { | ||
const data = { | ||
services: state.serviceList.services, | ||
status: state.serviceList.status, | ||
}; | ||
return data; | ||
}; | ||
|
||
|
||
const ServiceList = React.createClass({ | ||
propTypes: { | ||
services: React.PropTypes.array, | ||
}, | ||
|
||
getDefaultProps() { | ||
return { | ||
services: [], | ||
}; | ||
}, | ||
|
||
componentWillMount() { | ||
store.dispatch(actions.get()); | ||
}, | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<h1>Services</h1> | ||
{ | ||
this.props.services.map(service => ( | ||
<DragableService key={service.id} id={service.id} name={service.name} /> | ||
)) | ||
} | ||
</div> | ||
); | ||
}, | ||
}); | ||
|
||
|
||
export default connect(mapStateToProps, actions)(ServiceList); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.