Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
Initial commit for previous intent feature
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffladiray committed Feb 19, 2019
1 parent df21630 commit 81fcee4
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 8 deletions.
8 changes: 7 additions & 1 deletion backend/src/middlewares/openNLX.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,13 @@ class OpenNLXMiddleware {
const { botId, versionId } = data.intents[0];
const version = versionId || "default";
const bots = this.mainControllers.getBots();
const intents = await bots.getIntents(botId, versionId);
let intents = await bots.getIntents(botId, versionId);
intents = intents.map((int) => {
const intentWithPrevious = int;
intentWithPrevious.previous = int.previousId;
delete intentWithPrevious.previousId;
return intentWithPrevious;
});
this.openNLX.deleteAllIntents(botId, version);
this.openNLX.setIntents(botId, version, intents);
}
Expand Down
34 changes: 30 additions & 4 deletions front/src/shared/components/intentDetail.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
import React from "react";
import PropTypes from "prop-types";
import Zrmc, {
Button,
DialogManager,
Icon,
List,
ListItem,
ListItemMeta,
MenuItem,
Select,
TextField,
Button,
Icon,
} from "zrmc";
import { ExpansionPanel } from "zoapp-ui";
import ActionsList from "../components/actionsList";
Expand All @@ -25,6 +27,7 @@ const IntentDetail = ({
newActions,
displayCondition,
displayHelp,
getIntentNameById,
onSelect,
onAction,
onHelp,
Expand All @@ -37,6 +40,7 @@ const IntentDetail = ({
}) => {
const { name, input, output } = intent;
const topic = intent.topic && intent.topic.length > 0 ? intent.topic : "*";
const { previousId } = intent;
let help = "";
if (displayHelp > -1) {
help = <HelpPanel index={displayHelp} onHelp={onHelp} />;
Expand Down Expand Up @@ -139,7 +143,7 @@ const IntentDetail = ({
}
}}
>
NONE
{getIntentNameById(previousId) || "NONE"}
</Button>
</ListItemMeta>
</ListItem>
Expand All @@ -154,10 +158,12 @@ IntentDetail.defaultProps = {
onNewActionsChange: () => {},
onSelectActionsComponent: () => {},
onDeleteActionClick: () => {},
getIntentNameById: () => {},
};

IntentDetail.propTypes = {
intent: PropTypes.shape({}).isRequired,
getIntentNameById: PropTypes.func.isRequired,
onSelect: PropTypes.func.isRequired,
onAction: PropTypes.func.isRequired,
onHelp: PropTypes.func,
Expand Down Expand Up @@ -191,7 +197,7 @@ export const displayActionEditor = (
let condition = "";
let text = parameters;
let content = null;
if (actionDef === "Topic" || actionDef === "Previous") {
if (actionDef === "Topic") {
content = (
<div>
<TextField
Expand All @@ -204,6 +210,26 @@ export const displayActionEditor = (
/>
</div>
);
} else if (actionDef === "Previous") {
content = (
<Select
label="Select an intent"
style={{ width: "100%" }}
onSelected={(t) => {
setInput({ value: t !== "default" ? t : null });
}}
>
{parameters.options.map((int) => (
<MenuItem
key={int.id}
value={int.id || "default"}
selected={parameters.previousId === int.id}
>
{int.name}
</MenuItem>
))}
</Select>
);
} else {
if (type === "condition") {
text = parameters.text ? parameters.text : "";
Expand Down
44 changes: 42 additions & 2 deletions front/src/shared/containers/builder/intentContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,15 @@ class IntentContainer extends Component {
this.props.appUpdateIntent(this.props.selectedBotId, intent);
}
} else if (editAction === "Previous") {
// console.log("TODO", "IntentContainer.onPrevious ");
const previousId = this.actionField.value;
const { selectedIntent } = this.props;
const currentPreviousId = selectedIntent.previousId
? selectedIntent.previousId
: null;
if (previousId !== currentPreviousId) {
const intent = { ...selectedIntent, previousId };
this.props.appUpdateIntent(this.props.selectedBotId, intent);
}
}
this.reset();
return true;
Expand Down Expand Up @@ -308,6 +316,11 @@ class IntentContainer extends Component {
title = "Set previous intent";
action = "Set";
actionDef = "Previous";
const { previousId } = intent;
parameters = {
previousId,
options: this.props.intents,
};
} else if (state === "addCondition") {
title = `Add ${title} item`;
action = "Add";
Expand Down Expand Up @@ -441,6 +454,12 @@ class IntentContainer extends Component {
newActions={this.props.newActions}
displayHelp={this.state.displayHelp}
displayCondition={this.state.displayCondition}
getIntentNameById={(intentId) => {
const { name: intentName } = this.props.intents.find(
(int) => int.id === intentId,
) || { name: null };
return intentName;
}}
onSelect={this.handleActions}
onAction={this.handleDoActions}
onHelp={this.handleHelp}
Expand All @@ -459,9 +478,16 @@ class IntentContainer extends Component {
IntentContainer.defaultProps = {
selectedIntent: null,
selectedBotId: null,
intents: null,
};

IntentContainer.propTypes = {
intents: PropTypes.arrayOf(
PropTypes.shape({
name: PropTypes.string,
id: PropTypes.string,
}),
),
selectedBotId: PropTypes.string,
selectedIntent: PropTypes.shape({
id: PropTypes.string,
Expand All @@ -485,9 +511,23 @@ const mapStateToProps = (state) => {
? state.app.intents[selectedIntentIndex]
: null;
}

// intents list as props of intentDetail can be avoided, but is mandatory in order to
// display full list of intent when connecting previousId
// Pretty ugly and laggy, since redone multiples times
const intents = state.app.intents.reduce(
(acc, cur) => {
if (cur.id !== selectedIntent.id) {
return acc.concat({ id: cur.id, name: cur.name });
}
return acc;
},
[{ id: null, name: "NONE" }],
);

const selectedBotId = state.app ? state.app.selectedBotId : null;
const { newActions } = state.app;
return { selectedIntent, selectedBotId, newActions };
return { selectedIntent, selectedBotId, newActions, intents };
};

const mapDispatchToProps = (dispatch) => ({
Expand Down
4 changes: 3 additions & 1 deletion front/tests/shared/containers/intentContainer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ describe("containers/IntentContainerBase", () => {
appDeleteIntentAction: () => {},
apiSendIntentRequest: () => {},
appSetNewActions: () => {},
getIntentNameById: () => "title",
intents: [{ id: "1", name: "TestIntent" }],
selectedIntent: {
id: "Nq421",
botId: "HZAw",
Expand Down Expand Up @@ -505,7 +507,7 @@ describe("containers/IntentContainerBase", () => {
type: undefined,
action: "Set",
actionDef: "Previous",
parameters: undefined,
parameters: { previousId: undefined, options: defaultProps.intents },
function: () => {},
handleEditAction: () => {},
handleChangeAction: () => {},
Expand Down

0 comments on commit 81fcee4

Please sign in to comment.