insomnia/app/ui/components/sidebar/sidebar-children.js

115 lines
3.3 KiB
JavaScript
Raw Normal View History

2016-11-25 23:21:07 +00:00
import React, {PureComponent, PropTypes} from 'react';
import SidebarRequestRow from './sidebar-request-row';
import SidebarRequestGroupRow from './sidebar-request-group-row';
2016-11-25 23:09:17 +00:00
2016-11-25 23:21:07 +00:00
class SidebarChildren extends PureComponent {
_renderChildren (children) {
2016-11-25 23:09:17 +00:00
const {
handleCreateRequest,
handleCreateRequestGroup,
handleSetRequestGroupCollapsed,
handleDuplicateRequest,
2016-11-28 07:12:17 +00:00
handleDuplicateRequestGroup,
handleGenerateCode,
2016-11-25 23:09:17 +00:00
moveRequest,
moveRequestGroup,
handleActivateRequest,
activeRequest,
workspace
2016-11-25 23:09:17 +00:00
} = this.props;
const activeRequestId = activeRequest ? activeRequest._id : 'n/a';
return children.map(child => {
if (child.hidden) {
return null;
}
2016-11-25 23:09:17 +00:00
if (child.doc.type === 'Request') {
return (
<SidebarRequestRow
key={child.doc._id}
moveRequest={moveRequest}
handleActivateRequest={handleActivateRequest}
2016-11-28 07:12:17 +00:00
handleDuplicateRequest={handleDuplicateRequest}
handleGenerateCode={handleGenerateCode}
2016-11-25 23:09:17 +00:00
requestCreate={handleCreateRequest}
isActive={child.doc._id === activeRequestId}
request={child.doc}
workspace={workspace}
/>
);
2016-11-25 23:09:17 +00:00
}
// We have a RequestGroup!
const requestGroup = child.doc;
function hasActiveChild (children) {
for (const c of children) {
2016-11-25 23:21:07 +00:00
if (hasActiveChild(c.children || [])) {
return true;
2016-11-25 23:09:17 +00:00
} else if (c.doc._id === activeRequestId) {
return true;
}
}
// Didn't find anything, so return
return false;
}
const isActive = hasActiveChild(child.children);
const children = this._renderChildren(child.children);
2016-11-25 23:09:17 +00:00
return (
<SidebarRequestGroupRow
handleActivateRequest={handleActivateRequest}
key={requestGroup._id}
isActive={isActive}
moveRequestGroup={moveRequestGroup}
moveRequest={moveRequest}
handleSetRequestGroupCollapsed={handleSetRequestGroupCollapsed}
2016-11-28 07:12:17 +00:00
handleDuplicateRequestGroup={handleDuplicateRequestGroup}
2016-11-25 23:09:17 +00:00
isCollapsed={child.collapsed}
handleCreateRequest={handleCreateRequest}
handleCreateRequestGroup={handleCreateRequestGroup}
numChildren={child.children.length}
workspace={workspace}
requestGroup={requestGroup}
children={children}
/>
);
});
2016-11-25 23:09:17 +00:00
}
render () {
const {childObjects} = this.props;
2016-11-25 23:09:17 +00:00
return (
<ul className="sidebar__list sidebar__list-root">
{this._renderChildren(childObjects)}
2016-11-25 23:09:17 +00:00
</ul>
);
2016-11-25 23:09:17 +00:00
}
}
SidebarChildren.propTypes = {
// Required
handleActivateRequest: PropTypes.func.isRequired,
handleCreateRequest: PropTypes.func.isRequired,
handleCreateRequestGroup: PropTypes.func.isRequired,
handleSetRequestGroupCollapsed: PropTypes.func.isRequired,
handleDuplicateRequest: PropTypes.func.isRequired,
2016-11-28 07:12:17 +00:00
handleDuplicateRequestGroup: PropTypes.func.isRequired,
handleGenerateCode: PropTypes.func.isRequired,
2016-11-25 23:09:17 +00:00
moveRequest: PropTypes.func.isRequired,
moveRequestGroup: PropTypes.func.isRequired,
childObjects: PropTypes.arrayOf(PropTypes.object).isRequired,
2016-11-25 23:09:17 +00:00
workspace: PropTypes.object.isRequired,
// Optional
activeRequest: PropTypes.object
2016-11-25 23:09:17 +00:00
};
export default SidebarChildren;