PromucFlow_constructor/app/client/src/pages/Editor/ApiSidebar.tsx

137 lines
3.5 KiB
TypeScript
Raw Normal View History

2019-10-18 08:16:26 +00:00
import React from "react";
2019-10-21 15:12:45 +00:00
import { connect } from "react-redux";
import { RouteComponentProps } from "react-router";
import styled from "styled-components";
import { AppState } from "../../reducers";
import { ActionDataState } from "../../reducers/entityReducers/actionsReducer";
import { API_EDITOR_ID_URL, API_EDITOR_URL } from "../../constants/routes";
2019-10-30 10:23:20 +00:00
import { BaseButton } from "../../components/blueprint/ButtonComponent";
import { FormIcons } from "../../icons/FormIcons";
import { Spinner } from "@blueprintjs/core";
const ApiSidebarWrapper = styled.div`
height: 100%;
width: 100%;
flex-direction: column;
`;
const ApiItemsWrapper = styled.div`
flex: 1;
`;
2019-10-21 15:12:45 +00:00
const ApiItem = styled.div<{ isSelected: boolean }>`
height: 32px;
2019-10-21 15:12:45 +00:00
width: 100%;
padding: 5px 12px;
2019-10-21 15:12:45 +00:00
border-radius: 4px;
cursor: pointer;
font-size: 14px;
display: flex;
align-items: center;
margin-bottom: 2px;
2019-10-21 15:12:45 +00:00
background-color: ${props =>
props.isSelected ? props.theme.colors.paneCard : props.theme.colors.paneBG}
:hover {
background-color: ${props => props.theme.colors.paneCard};
}
`;
2019-10-22 14:54:23 +00:00
const HTTPMethod = styled.span<{ method: string | undefined }>`
2019-10-21 15:12:45 +00:00
flex: 1;
font-size: 12px;
2019-10-21 15:12:45 +00:00
color: ${props => {
switch (props.method) {
case "GET":
return "#29CCA3";
case "POST":
return "#F7C75B";
case "PUT":
return "#30A5E0";
case "DELETE":
return "#CE4257";
default:
return "#333";
}
}};
`;
const ActionName = styled.span`
flex: 3;
padding: 0 2px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
`;
const CreateNewButton = styled(BaseButton)`
&& {
border: none;
color: ${props => props.theme.colors.textOnDarkBG};
height: 32px;
&:hover {
color: ${props => props.theme.colors.paneBG};
svg {
path {
fill: ${props => props.theme.colors.paneBG};
}
}
}
svg {
height: 12px;
}
}
2019-10-21 15:12:45 +00:00
`;
interface ReduxStateProps {
actions: ActionDataState;
}
2019-11-01 07:11:32 +00:00
type Props = ReduxStateProps & RouteComponentProps<{ id: string }>;
2019-10-21 15:12:45 +00:00
class ApiSidebar extends React.Component<Props> {
handleCreateNew = () => {
const { history } = this.props;
history.push(API_EDITOR_URL);
};
2019-10-18 08:16:26 +00:00
render() {
2019-10-21 15:12:45 +00:00
const { actions, history, match } = this.props;
const activeActionId = match.params.id;
return (
<ApiSidebarWrapper>
{actions.isFetching && <Spinner size={30} />}
<ApiItemsWrapper>
{actions.data.map(action => (
<ApiItem
key={action.id}
onClick={() => history.push(API_EDITOR_ID_URL(action.id))}
isSelected={activeActionId === action.id}
>
<HTTPMethod method={action.actionConfiguration.httpMethod}>
{action.actionConfiguration.httpMethod}
</HTTPMethod>
<ActionName>{action.name}</ActionName>
</ApiItem>
))}
{!activeActionId && !actions.isFetching && (
<ApiItem isSelected>
<HTTPMethod method="" />
<ActionName>New Api</ActionName>
</ApiItem>
)}
</ApiItemsWrapper>
<CreateNewButton
text="Create new API"
icon={FormIcons.ADD_NEW_ICON()}
onClick={this.handleCreateNew}
/>
</ApiSidebarWrapper>
2019-10-21 15:12:45 +00:00
);
2019-10-18 08:16:26 +00:00
}
}
2019-10-21 15:12:45 +00:00
const mapStateToProps = (state: AppState): ReduxStateProps => ({
actions: state.entities.actions,
});
2019-11-01 07:11:32 +00:00
export default connect(mapStateToProps)(ApiSidebar);