2018-03-30 10:54:15 +02:00
import React , { Component } from 'react' ;
2017-12-01 11:03:48 +01:00
import { Button , SVGIcon , TextField } from 'react-md' ;
2018-03-28 15:51:10 +02:00
import AnswerSource from './AnswerSource.js' ;
2018-01-14 01:00:35 +01:00
import '../css/components/IntentDetails.css' ;
2018-01-19 22:42:37 +01:00
import '../css/Common.css' ;
2018-03-30 10:54:15 +02:00
import {
QUESTION _MAX _LENGTH ,
ANSWER _MAX _LENGTH ,
INTENT _NAME _MAX _LENGTH ,
INTENT _EXPLANATION _MAX _LENGTH ,
ANSWER _TYPE ,
} from '../config/constants' ;
2018-04-03 14:19:07 +02:00
import AnswerTextBox from './helper/AnswerTextBox.js' ;
2017-11-30 17:43:24 +01:00
class IntentDetails extends Component {
2018-03-30 10:54:15 +02:00
constructor ( props ) {
super ( props ) ;
2017-11-30 17:43:24 +01:00
2018-03-30 10:54:15 +02:00
this . state = { intent : props . selectedIntent } ;
2017-11-30 17:43:24 +01:00
2018-03-30 10:54:15 +02:00
this . addQuestion = this . addQuestion . bind ( this ) ;
this . deleteQuestion = this . deleteQuestion . bind ( this ) ;
this . handleQuestionEdit = this . handleQuestionEdit . bind ( this ) ;
this . handleIntentNameEdit = this . handleIntentNameEdit . bind ( this ) ;
this . handleIntentExplanationEdit = this . handleIntentExplanationEdit . bind (
this
) ;
2017-11-30 17:43:24 +01:00
}
2018-03-30 10:54:15 +02:00
componentWillReceiveProps ( props ) {
this . setState ( { intent : props . selectedIntent } ) ;
2017-11-30 17:43:24 +01:00
}
2018-03-30 10:54:15 +02:00
render ( ) {
2017-11-30 17:43:24 +01:00
return (
2018-01-14 01:00:35 +01:00
< div className = "RightPanelBox" >
2017-12-02 22:48:45 +01:00
< div className = "QuestionBox" >
2018-03-30 10:54:15 +02:00
< h5 className = "PanelSubTitle" >
{ ' ' }
In introduction , Alexa will help users to ask her the right questions about your business . For Example , she will say : "To ask us about our services, say : What do you do ? " . What do you do ? is defined in question field . Alexa will use first variation of question in intro .
< / h 5 >
< TextField
2018-01-19 22:42:37 +01:00
id = "intent explanation"
lineDirection = "center"
placeholder = "To ask us about our services, say "
className = "md-cell md-cell--bottom IntentDetailsInputBoxes"
onChange = { this . handleIntentExplanationEdit }
maxLength = { INTENT _EXPLANATION _MAX _LENGTH }
2018-03-30 10:54:15 +02:00
value = { this . state . intent . intentExplanation }
/ >
< br / >
2017-12-02 22:48:45 +01:00
< TextField
id = "intent name"
lineDirection = "center"
2018-01-16 13:38:05 +01:00
label = "Question name"
2018-01-14 01:00:35 +01:00
className = "md-cell md-cell--bottom IntentDetailsInputBoxes"
2017-12-02 22:48:45 +01:00
onChange = { this . handleIntentNameEdit }
maxLength = { INTENT _NAME _MAX _LENGTH }
2018-03-30 10:54:15 +02:00
value = { this . state . intent . intentName }
/ >
2017-12-02 22:48:45 +01:00
< / d i v >
2018-01-16 13:38:05 +01:00
< h5 className = "QuestionTitle" > Question variants < / h 5 >
2018-03-30 10:54:15 +02:00
{ this . state . intent . questions . map ( ( question , index ) => {
return (
< div key = { index } className = "QuestionBox" >
< TextField
id = "intent question"
lineDirection = "center"
placeholder = "Question"
className = "md-cell md-cell--bottom IntentDetailsInputBoxes"
maxLength = { QUESTION _MAX _LENGTH }
rightIcon = {
< SVGIcon
onClick = { ( ) => {
2018-04-05 15:40:10 +02:00
this . deleteQuestion ( index ) ;
2018-03-30 10:54:15 +02:00
} }
>
{ ' ' }
< path
fill = "#000000"
d = "M19,4H15.5L14.5,3H9.5L8.5,4H5V6H19M6,19A2,2 0 0,0 8,21H16A2,2 0 0,0 18,19V7H6V19Z"
/ >
{ ' ' }
< / S V G I c o n >
}
onChange = { e => {
this . handleQuestionEdit ( e , index ) ;
} }
value = { question }
/ >
< / d i v >
) ;
} ) }
< Button
className = "AddQuestionVariantButton"
icon
primary
onClick = { this . addQuestion }
disabled = { this . props . waiting }
>
add
< / B u t t o n >
< AnswerSource
2018-04-03 14:19:07 +02:00
className = "AnswerTypeButton"
onSaveAnswerType = { this . handleExternalSourceSave . bind ( this ) }
answerType = { this . state . intent . answerType }
/ >
< AnswerTextBox
2018-03-30 10:54:15 +02:00
answerType = { this . state . intent . answerType }
2018-04-03 14:19:07 +02:00
externalAnswerSource = { this . state . intent . externalAnswerSource }
handleAnswerSourceEdit = { this . handleAnswerSourceEdit . bind ( this ) }
handleAnswerEdit = { this . handleAnswerEdit . bind ( this ) }
answer = { this . state . intent . answer }
2018-03-30 10:54:15 +02:00
/ >
< Button
className = "IntentDetailsButton-firstInRow"
flat
primary
swapTheming
onClick = { ( ) => {
this . props . onSaveIntentClick ( this . state . intent ) ;
} }
disabled = { this . props . waiting }
>
Save
< / B u t t o n >
< Button
className = "IntentDetailsButton"
flat
primary
onClick = { ( ) => {
this . props . onDeleteIntentClick ( this . state . intent ) ;
} }
disabled = { this . props . waiting }
>
Delete
< / B u t t o n >
2017-11-30 17:43:24 +01:00
< / d i v >
) ;
}
2018-03-30 10:54:15 +02:00
addQuestion ( ) {
2017-11-30 17:43:24 +01:00
let newIntent = this . state . intent ;
2018-03-30 10:54:15 +02:00
newIntent . questions . push ( '' ) ;
this . setState ( { intent : newIntent } ) ;
2017-11-30 17:43:24 +01:00
}
2018-04-05 15:40:10 +02:00
deleteQuestion ( index ) {
2018-03-30 10:54:15 +02:00
if ( this . state . intent . questions . length > 1 ) {
let newIntent = this . state . intent ;
2018-04-05 15:40:10 +02:00
if ( index >= 0 && index < newIntent . questions . length ) newIntent . questions . splice ( index , 1 ) ;
2018-03-30 10:54:15 +02:00
this . setState ( { intent : newIntent } ) ;
}
2017-11-30 17:43:24 +01:00
}
2017-12-02 22:48:45 +01:00
2018-03-30 10:54:15 +02:00
handleQuestionEdit ( e , index ) {
2018-04-11 10:42:06 +02:00
const isQuestionInvalid = e . length >= QUESTION _MAX _LENGTH || ! /^[a-z,.' ]*$/i . test ( e ) ;
if ( isQuestionInvalid ) return ;
2017-12-02 22:48:45 +01:00
let newIntent = this . state . intent ;
newIntent . questions [ index ] = e ;
2018-03-30 10:54:15 +02:00
this . setState ( { intent : newIntent } ) ;
2017-12-02 22:48:45 +01:00
}
2018-03-30 10:54:15 +02:00
handleIntentExplanationEdit ( e , index ) {
2018-04-11 10:42:06 +02:00
const isExplanationInvalid = e . length >= INTENT _EXPLANATION _MAX _LENGTH || ! /^[a-z,.' ]*$/i . test ( e ) ;
if ( isExplanationInvalid )
2018-03-30 10:54:15 +02:00
return ;
2018-01-19 22:42:37 +01:00
let newIntent = this . state . intent ;
newIntent . intentExplanation = e ;
2018-03-30 10:54:15 +02:00
this . setState ( { intent : newIntent } ) ;
2018-01-19 22:42:37 +01:00
}
2018-03-30 10:54:15 +02:00
handleAnswerEdit ( e ) {
2018-04-11 10:42:06 +02:00
const isAnswerInvalid = e . length >= ANSWER _MAX _LENGTH || ! /^[a-z,.' ]*$/i . test ( e ) ;
if ( isAnswerInvalid ) return ;
2017-12-02 22:48:45 +01:00
let newIntent = this . state . intent ;
newIntent . answer = e ;
2018-03-30 10:54:15 +02:00
this . setState ( { intent : newIntent } ) ;
2017-12-02 22:48:45 +01:00
}
2018-03-30 10:54:15 +02:00
handleAnswerSourceEdit ( e ) {
2018-04-11 10:42:06 +02:00
const isAnswerSourceInvalid = e . length >= ANSWER _MAX _LENGTH ;
if ( isAnswerSourceInvalid ) return ;
2018-03-30 10:54:15 +02:00
let newIntent = this . state . intent ;
newIntent . externalAnswerSource = e ;
this . setState ( { intent : newIntent } ) ;
}
handleIntentNameEdit ( e ) {
2018-04-11 10:42:06 +02:00
const isIntentNameInvalid = e . length >= INTENT _NAME _MAX _LENGTH || ! /^[a-z]*$/i . test ( e ) ;
if ( isIntentNameInvalid ) return ;
2017-12-02 22:48:45 +01:00
let newIntent = this . state . intent ;
2018-01-29 21:32:24 +01:00
newIntent . intentName = e ;
2018-03-30 10:54:15 +02:00
this . setState ( { intent : newIntent } ) ;
}
handleExternalSourceSave ( answerType ) {
let newIntent = this . state . intent ;
newIntent . answerType = answerType ;
this . setState ( { intent : newIntent } ) ;
2017-12-02 22:48:45 +01:00
}
2017-11-30 17:43:24 +01:00
}
export default IntentDetails ;