Skip to content

Commit

Permalink
feat(actions): actions dropdown shows action metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
slvnperron committed May 12, 2018
1 parent 1b5f643 commit 21af29d
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 17 deletions.
1 change: 1 addition & 0 deletions packages/core/botpress/package.json
Expand Up @@ -102,6 +102,7 @@
"react-emoji": "^0.4.4",
"react-fontawesome": "^1.2.0",
"react-ga": "^2.1.2",
"react-highlight-words": "^0.11.0",
"react-markdown": "^2.4.5",
"react-redux": "^5.0.6",
"react-router": "^4.2.0",
Expand Down
@@ -1,6 +1,6 @@
import React, { Component } from 'react'
import { Modal, Button, Radio, OverlayTrigger, Tooltip, Panel, Well } from 'react-bootstrap'
import Select from 'react-select'
import SelectActionDropdown from './SelectActionDropdown'
import axios from 'axios'
import _ from 'lodash'

Expand All @@ -15,7 +15,7 @@ export default class ActionModalForm extends Component {

this.state = {
actionType: 'message',
availableFunctions: [],
avActions: [],
functionInputValue: '',
messageValue: '',
functionParams: {}
Expand Down Expand Up @@ -50,7 +50,7 @@ export default class ActionModalForm extends Component {

fetchAvailableFunctions() {
return axios.get('/api/flows/available_actions').then(({ data }) => {
this.setState({ availableFunctions: data })
this.setState({ avActions: data })
})
}

Expand All @@ -66,27 +66,26 @@ export default class ActionModalForm extends Component {
})
}

renderSectionCode() {
const { availableFunctions } = this.state
renderSectionAction() {
const { avActions } = this.state

const tooltip = (
<Tooltip id="notSeeingFunction">
Functions are registered in the code on the server-side. Please make sure that the file containing the functions
has been properly registered using `bp.registerActions()`. This file should return an object containing
functions.
<Tooltip id="notSeeingAction">
Actions are registered on the server-side. Read about how to register new actions by searching for
`bp.registerActions()`.
</Tooltip>
)

const tooltip2 = (
<Tooltip id="whatIsThis">
You can provide function calls extra parameters if they have been coded to support them. You can generally
ignore this for most functions. Please see the documentation to know more.
You can change how the Action is executed by providing it parameters. Some parameters are required, some are
optional.
</Tooltip>
)

const help = (
<OverlayTrigger placement="bottom" overlay={tooltip}>
<span className={style.tip}>Missing your function?</span>
<span className={style.tip}>Can't see your action?</span>
</OverlayTrigger>
)

Expand Down Expand Up @@ -114,13 +113,12 @@ export default class ActionModalForm extends Component {
<div>
<h5>Function to invoke {help}</h5>
<div className={style.section}>
<Select
name="functionToInvoke"
<SelectActionDropdown
value={this.state.functionInputValue}
options={availableFunctions.map(x => ({ label: x.name, value: x.name }))}
options={avActions}
onChange={val => {
this.setState({ functionInputValue: val && val.value })
const fn = availableFunctions.find(fn => fn.name === (val && val.value))
const fn = avActions.find(fn => fn.name === (val && val.value))
const defaultParams = _.get(fn, 'metadata.params')
const confirmationText = 'Should your params be overwritten via default ones?'
if (Object.keys(this.state.functionParams).length > 0 && defaultParams && confirm(confirmationText)) {
Expand Down Expand Up @@ -208,7 +206,7 @@ export default class ActionModalForm extends Component {
</Radio>
</div>

{this.state.actionType === 'message' ? this.renderSectionMessage() : this.renderSectionCode()}
{this.state.actionType === 'message' ? this.renderSectionMessage() : this.renderSectionAction()}
</Modal.Body>
<Modal.Footer>
<Button onClick={onClose}>Cancel</Button>
Expand Down
@@ -0,0 +1,52 @@
import React, { Component } from 'react'
import Select from 'react-select'
import _ from 'lodash'
import classnames from 'classnames'
import Highlighter from 'react-highlight-words'

const style = require('./actionDropdown.scss')

export default class SelectActionDropdown extends Component {
renderOption = option => {
const highlight = txt => <Highlighter searchWords={[this._inputValue]} textToHighlight={txt} />

if (option.metadata) {
const category = option.metadata.category ? (
<span className={style.category}>{highlight(option.metadata.category)}</span>
) : null
const title = option.metadata.title ? (
<span className={style.title}>{highlight(option.metadata.title)}</span>
) : null

return (
<div>
<span>
{category}
{title}
<span className={style.name}>> {highlight(option.label)}</span>
</span>
</div>
)
}

return highlight(option.label)
}

render() {
const options = this.props.options.map(x => ({
label: x.name,
value: x.name,
metadata: x.metadata
}))

return (
<Select
onInputChange={inputValue => (this._inputValue = inputValue)}
onChange={this.props.onChange}
options={options}
value={this.props.value}
optionRenderer={this.renderOption}
/>
)
}
}
@@ -0,0 +1,13 @@
.category {
font-weight: bold;
margin-right: 5px;
}

.title {
margin-right: 5px;
}

.name {
color: grey;
font-size: 90%;
}
11 changes: 11 additions & 0 deletions packages/core/botpress/yarn.lock
Expand Up @@ -3437,6 +3437,10 @@ he@1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/he/-/he-1.1.1.tgz#93410fd21b009735151f8868c2f271f3427e23fd"

highlight-words-core@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/highlight-words-core/-/highlight-words-core-1.2.0.tgz#232bec301cbf2a4943d335dc748ce70e9024f3b1"

history@^4.7.2:
version "4.7.2"
resolved "https://registry.yarnpkg.com/history/-/history-4.7.2.tgz#22b5c7f31633c5b8021c7f4a8a954ac139ee8d5b"
Expand Down Expand Up @@ -6048,6 +6052,13 @@ react-ga@^2.1.2:
prop-types "^15.6.0"
react "^15.6.2 || ^16.0"

react-highlight-words@^0.11.0:
version "0.11.0"
resolved "https://registry.yarnpkg.com/react-highlight-words/-/react-highlight-words-0.11.0.tgz#4f3c2039a8fd275f3ab795e59946b0324d8e6bee"
dependencies:
highlight-words-core "^1.2.0"
prop-types "^15.5.8"

react-input-autosize@^2.1.2:
version "2.2.1"
resolved "https://registry.yarnpkg.com/react-input-autosize/-/react-input-autosize-2.2.1.tgz#ec428fa15b1592994fb5f9aa15bb1eb6baf420f8"
Expand Down

0 comments on commit 21af29d

Please sign in to comment.