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

204 lines
5.8 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";
2019-11-25 05:07:27 +00:00
import { AppState } from "reducers";
import { ActionDataState } from "reducers/entityReducers/actionsReducer";
2020-01-24 09:54:40 +00:00
import { APIEditorRouteParams } from "constants/routes";
2019-11-25 05:07:27 +00:00
import { ApiPaneReduxState } from "reducers/uiReducers/apiPaneReducer";
2020-01-24 09:54:40 +00:00
import {
createActionRequest,
moveActionRequest,
copyActionRequest,
deleteAction,
} from "actions/actionActions";
2019-11-25 09:15:11 +00:00
import { changeApi, initApiPane } from "actions/apiPaneActions";
import { RestAction } from "api/ActionAPI";
import { getPluginIdOfName } from "selectors/entitiesSelector";
2020-01-24 09:54:40 +00:00
import { DEFAULT_API_ACTION, PLUGIN_NAME } from "constants/ApiEditorConstants";
import EditorSidebar from "pages/Editor/EditorSidebar";
import { getNextEntityName } from "utils/AppsmithUtils";
2019-10-21 15:12:45 +00:00
2019-11-14 09:01:23 +00:00
const HTTPMethod = styled.span<{ method?: string }>`
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";
}
}};
`;
2020-01-24 09:54:40 +00:00
const ActionItem = styled.div`
flex: 1;
display: flex;
align-items: center;
`;
2019-10-21 15:12:45 +00:00
const ActionName = styled.span`
flex: 3;
padding: 0 2px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
`;
2019-10-21 15:12:45 +00:00
interface ReduxStateProps {
actions: ActionDataState;
2019-11-13 07:34:59 +00:00
apiPane: ApiPaneReduxState;
pluginId: string | undefined;
2019-10-21 15:12:45 +00:00
}
2019-11-13 07:34:59 +00:00
interface ReduxDispatchProps {
2020-01-24 09:54:40 +00:00
createAction: (data: Partial<RestAction>) => void;
2019-11-25 09:15:11 +00:00
onApiChange: (id: string) => void;
initApiPane: (urlId?: string) => void;
2020-01-24 09:54:40 +00:00
moveAction: (
id: string,
pageId: string,
name: string,
originalPageId: string,
) => void;
copyAction: (id: string, pageId: string, name: string) => void;
deleteAction: (id: string) => void;
2019-11-13 07:34:59 +00:00
}
type Props = ReduxStateProps &
ReduxDispatchProps &
RouteComponentProps<APIEditorRouteParams>;
2019-11-13 07:34:59 +00:00
2020-01-24 09:54:40 +00:00
class ApiSidebar extends React.Component<Props> {
2019-11-25 09:15:11 +00:00
componentDidMount(): void {
this.props.initApiPane(this.props.match.params.apiId);
}
2020-01-24 09:54:40 +00:00
shouldComponentUpdate(nextProps: Readonly<Props>): boolean {
if (
Object.keys(nextProps.apiPane.drafts) !==
Object.keys(this.props.apiPane.drafts)
) {
return true;
2019-11-13 07:34:59 +00:00
}
2020-01-24 09:54:40 +00:00
return nextProps.actions.data !== this.props.actions.data;
2019-11-13 07:34:59 +00:00
}
2019-10-21 15:12:45 +00:00
handleCreateNew = () => {
2020-01-24 09:54:40 +00:00
const { actions } = this.props;
const { pageId } = this.props.match.params;
const pageApiNames = actions.data
.filter(a => a.pageId === pageId)
.map(a => a.name);
const newName = getNextEntityName("Api", pageApiNames);
this.props.createAction({ ...DEFAULT_API_ACTION, name: newName, pageId });
};
2020-01-24 09:54:40 +00:00
handleApiChange = (actionId: string) => {
this.props.onApiChange(actionId);
2019-11-13 07:34:59 +00:00
};
2020-01-24 09:54:40 +00:00
handleMove = (itemId: string, destinationPageId: string) => {
const action = this.props.actions.data.filter(a => a.id === itemId)[0];
const pageApiNames = this.props.actions.data
.filter(a => a.pageId === destinationPageId)
.map(a => a.name);
let name = action.name;
if (pageApiNames.indexOf(action.name) > -1) {
name = getNextEntityName(`${name}-`, pageApiNames);
2019-11-13 07:34:59 +00:00
}
2020-01-24 09:54:40 +00:00
this.props.moveAction(itemId, destinationPageId, name, action.pageId);
2019-11-13 07:34:59 +00:00
};
2020-01-24 09:54:40 +00:00
handleCopy = (itemId: string, destinationPageId: string) => {
const action = this.props.actions.data.filter(a => a.id === itemId)[0];
const pageApiNames = this.props.actions.data
.filter(a => a.pageId === destinationPageId)
.map(a => a.name);
let name = `${action.name}Copy`;
if (pageApiNames.indexOf(name) > -1) {
name = getNextEntityName(`${name}`, pageApiNames);
}
this.props.copyAction(itemId, destinationPageId, name);
};
2020-01-24 09:54:40 +00:00
handleDelete = (itemId: string) => {
this.props.deleteAction(itemId);
2019-11-14 09:01:23 +00:00
};
2020-01-24 09:54:40 +00:00
renderItem = (action: RestAction) => {
return (
<ActionItem>
{action.actionConfiguration ? (
<HTTPMethod method={action.actionConfiguration.httpMethod}>
{action.actionConfiguration.httpMethod}
</HTTPMethod>
) : (
<HTTPMethod />
)}
<ActionName>{action.name}</ActionName>
</ActionItem>
);
2019-11-25 09:15:11 +00:00
};
2019-10-18 08:16:26 +00:00
render() {
2019-11-14 09:01:23 +00:00
const {
2019-12-11 15:14:38 +00:00
apiPane: { isFetching, drafts },
2020-01-24 09:54:40 +00:00
match: {
params: { apiId },
},
2019-11-14 09:01:23 +00:00
actions: { data },
pluginId,
2019-11-14 09:01:23 +00:00
} = this.props;
if (!pluginId) return null;
2019-10-21 15:12:45 +00:00
return (
2020-01-24 09:54:40 +00:00
<EditorSidebar
isLoading={isFetching}
list={data}
selectedItemId={apiId}
draftIds={Object.keys(drafts)}
itemRender={this.renderItem}
onItemCreateClick={this.handleCreateNew}
onItemSelected={this.handleApiChange}
moveItem={this.handleMove}
copyItem={this.handleCopy}
deleteItem={this.handleDelete}
/>
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 => ({
pluginId: getPluginIdOfName(state, PLUGIN_NAME),
2019-10-21 15:12:45 +00:00
actions: state.entities.actions,
2019-11-13 07:34:59 +00:00
apiPane: state.ui.apiPane,
});
const mapDispatchToProps = (dispatch: Function): ReduxDispatchProps => ({
2020-01-24 09:54:40 +00:00
createAction: (data: Partial<RestAction>) =>
dispatch(createActionRequest(data)),
2019-11-25 09:15:11 +00:00
onApiChange: (actionId: string) => dispatch(changeApi(actionId)),
initApiPane: (urlId?: string) => dispatch(initApiPane(urlId)),
2020-01-24 09:54:40 +00:00
moveAction: (
id: string,
destinationPageId: string,
name: string,
originalPageId: string,
) =>
dispatch(
moveActionRequest({ id, destinationPageId, originalPageId, name }),
),
copyAction: (id: string, destinationPageId: string, name: string) =>
dispatch(copyActionRequest({ id, destinationPageId, name })),
deleteAction: (id: string) => dispatch(deleteAction({ id })),
2019-10-21 15:12:45 +00:00
});
export default connect(mapStateToProps, mapDispatchToProps)(ApiSidebar);