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

243 lines
6.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";
2020-04-20 08:26:19 +00:00
import {
changeApi,
createNewApiAction,
initApiPane,
} from "actions/apiPaneActions";
import { RestAction } from "api/ActionAPI";
import { getPluginIdOfName } from "selectors/entitiesSelector";
import { PLUGIN_NAME } from "constants/ApiEditorConstants";
2020-01-24 09:54:40 +00:00
import EditorSidebar from "pages/Editor/EditorSidebar";
import { getNextEntityName } from "utils/AppsmithUtils";
2020-03-06 04:59:24 +00:00
import AnalyticsUtil from "utils/AnalyticsUtil";
import { API_EDITOR_URL_WITH_SELECTED_PAGE_ID } from "constants/routes";
2020-04-20 08:26:19 +00:00
import { API_PANE_V2, checkForFlag } from "utils/featureFlags";
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";
2020-04-22 09:15:24 +00:00
case "PATCH":
return "#8E8E8E";
2019-10-21 15:12:45 +00:00
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;
max-width: 100px;
`;
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;
2020-03-06 04:59:24 +00:00
deleteAction: (id: string, name: string) => void;
2020-04-20 08:26:19 +00:00
createNewApiAction: (pageId: 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-30 13:23:04 +00:00
return nextProps.actions !== this.props.actions;
2019-11-13 07:34:59 +00:00
}
2019-10-21 15:12:45 +00:00
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) => {
2020-01-30 13:23:04 +00:00
const action = this.props.actions.filter(a => a.config.id === itemId)[0];
const pageApiNames = this.props.actions
.filter(a => a.config.pageId === destinationPageId)
.map(a => a.config.name);
let name = action.config.name;
if (pageApiNames.indexOf(action.config.name) > -1) {
2020-01-27 13:53:33 +00:00
name = getNextEntityName(name, pageApiNames);
2019-11-13 07:34:59 +00:00
}
2020-01-30 13:23:04 +00:00
this.props.moveAction(
itemId,
destinationPageId,
name,
action.config.pageId,
);
2019-11-13 07:34:59 +00:00
};
2020-01-24 09:54:40 +00:00
handleCopy = (itemId: string, destinationPageId: string) => {
2020-01-30 13:23:04 +00:00
const action = this.props.actions.filter(a => a.config.id === itemId)[0];
const pageApiNames = this.props.actions
.filter(a => a.config.pageId === destinationPageId)
.map(a => a.config.name);
let name = `${action.config.name}Copy`;
2020-01-24 09:54:40 +00:00
if (pageApiNames.indexOf(name) > -1) {
2020-01-27 13:53:33 +00:00
name = getNextEntityName(name, pageApiNames);
2020-01-24 09:54:40 +00:00
}
this.props.copyAction(itemId, destinationPageId, name);
};
2020-03-11 13:59:46 +00:00
handleDelete = (itemId: string, itemName: string, pageName: string) => {
AnalyticsUtil.logEvent("DELETE_API_CLICK", {
apiId: itemId,
apiName: itemName,
pageName: pageName,
});
2020-03-06 04:59:24 +00:00
this.props.deleteAction(itemId, itemName);
2019-11-14 09:01:23 +00:00
};
2020-01-24 09:54:40 +00:00
renderItem = (action: RestAction) => {
return (
2020-03-06 04:59:24 +00:00
<ActionItem
onClick={() => {
AnalyticsUtil.logEvent("API_SELECT", {
apiId: action.id,
apiName: action.name,
});
}}
>
2020-01-24 09:54:40 +00:00
{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
};
handleCreateNewApiClick = (selectedPageId: string) => {
2020-04-20 08:26:19 +00:00
const { history, createNewApiAction } = this.props;
const { pageId, applicationId } = this.props.match.params;
2020-04-20 08:26:19 +00:00
const v2Flag = checkForFlag(API_PANE_V2);
if (v2Flag) {
history.push(
API_EDITOR_URL_WITH_SELECTED_PAGE_ID(
applicationId,
pageId,
selectedPageId,
),
);
} else {
createNewApiAction(selectedPageId);
}
};
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 },
},
2020-01-30 13:23:04 +00:00
actions,
pluginId,
2019-11-14 09:01:23 +00:00
} = this.props;
if (!pluginId) return null;
2020-01-30 13:23:04 +00:00
const data = actions.map(a => a.config);
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.handleCreateNewApiClick}
2020-01-24 09:54:40 +00:00
onItemSelected={this.handleApiChange}
moveItem={this.handleMove}
copyItem={this.handleCopy}
deleteItem={this.handleDelete}
createButtonTitle="Create new API"
2020-01-24 09:54:40 +00:00
/>
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 })),
2020-03-06 04:59:24 +00:00
deleteAction: (id: string, name: string) =>
dispatch(deleteAction({ id, name })),
2020-04-20 08:26:19 +00:00
createNewApiAction: (pageId: string) => dispatch(createNewApiAction(pageId)),
2019-10-21 15:12:45 +00:00
});
export default connect(mapStateToProps, mapDispatchToProps)(ApiSidebar);