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

117 lines
3.4 KiB
JavaScript
Raw Normal View History

2017-08-10 01:56:27 +00:00
import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
import SidebarRequestRow from './sidebar-request-row';
import SidebarRequestGroupRow from './sidebar-request-group-row';
2017-07-31 21:14:16 +00:00
import * as models from '../../../models/index';
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,
handleCopyAsCurl,
2017-06-13 20:45:15 +00:00
moveDoc,
2016-11-25 23:09:17 +00:00
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;
}
2017-07-31 21:14:16 +00:00
if (child.doc.type === models.request.type) {
2016-11-25 23:09:17 +00:00
return (
<SidebarRequestRow
key={child.doc._id}
2017-06-13 20:45:15 +00:00
moveDoc={moveDoc}
2016-11-25 23:09:17 +00:00
handleActivateRequest={handleActivateRequest}
2016-11-28 07:12:17 +00:00
handleDuplicateRequest={handleDuplicateRequest}
handleGenerateCode={handleGenerateCode}
handleCopyAsCurl={handleCopyAsCurl}
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}
2017-06-13 20:45:15 +00:00
moveDoc={moveDoc}
2016-11-25 23:09:17 +00:00
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,
handleCopyAsCurl: PropTypes.func.isRequired,
2017-06-13 20:45:15 +00:00
moveDoc: 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;