From 4a43dda852aa3410adf01a327cd2707585410054 Mon Sep 17 00:00:00 2001 From: GotPPay Date: Tue, 3 Apr 2018 14:19:07 +0200 Subject: [PATCH] fixed PR #15 comments --- backend/config/constants.js | 4 +- backend/helpers/externalSource.js | 8 --- backend/models/alexa.js | 34 ++++++---- web/src/App.js | 37 ++++------ web/src/components/AnswerSource.js | 68 ++++--------------- web/src/components/IntentDetails.js | 53 +++------------ web/src/components/helper/AnswerSourceForm.js | 57 ++++++++++++++++ web/src/components/helper/AnswerTextBox.js | 44 ++++++++++++ web/src/components/{modal => helper}/Modal.js | 0 web/src/config/constants.js | 4 +- web/src/css/components/IntentDetails.scss | 6 +- 11 files changed, 164 insertions(+), 151 deletions(-) create mode 100644 web/src/components/helper/AnswerSourceForm.js create mode 100644 web/src/components/helper/AnswerTextBox.js rename web/src/components/{modal => helper}/Modal.js (100%) diff --git a/backend/config/constants.js b/backend/config/constants.js index 3dd7834..ce392dd 100644 --- a/backend/config/constants.js +++ b/backend/config/constants.js @@ -65,8 +65,8 @@ constants.stringConstraints = { constants.answerType = { PREDEFINED: 0, - EXTERNAL_SOURCE_WP_JSON : 1, - EXTERNAL_SOURCE_RSS : 2 + EXTERNAL_SOURCE_WP_TITLES : 1, + EXTERNAL_SOURCE_WP_NEWS : 2 } module.exports = constants; diff --git a/backend/helpers/externalSource.js b/backend/helpers/externalSource.js index 7c87b00..f271d96 100644 --- a/backend/helpers/externalSource.js +++ b/backend/helpers/externalSource.js @@ -3,14 +3,6 @@ let Parser = require ('rss-parser'); let parser = new Parser (); -getDataFromRSSFeed = function (url) { - //let feed = await parser.parseURL(url); - //console.log(feed.title); - //feed.items.forEach(item => { - // console.log(item.title + ':' + item.link) - //}); -} - getDataFromWPJSON = function (sourceUrl, page = 1, maxPosts = 10) { return new Promise ((resolve, reject) => { var options = { diff --git a/backend/models/alexa.js b/backend/models/alexa.js index 40604d6..e501f6a 100644 --- a/backend/models/alexa.js +++ b/backend/models/alexa.js @@ -7,6 +7,7 @@ let predefinedSourceHelper = require ('../helpers/externalSource'); var handlers = {}; var destinationEmail; +let skillName; module.exports = { run: function (req, res) { @@ -33,6 +34,7 @@ module.exports = { .then (activeSkill => { handlers = {}; destinationEmail = activeSkill.contactEmail; + skillName = activeSkill.invocationName; let listOfPossibleQuestions = ''; activeSkill.intents.forEach (intent => { @@ -77,7 +79,7 @@ module.exports = { reject: null } - let answer = new Promise((resolve,reject)=>{ + let answerPromise = new Promise ((resolve, reject) => { answerPromiseProps = { resolve:resolve, reject:reject @@ -88,30 +90,32 @@ module.exports = { case constants.answerType.PREDEFINED: answerPromiseProps.resolve(intent.answer); break; - case constants.answerType.EXTERNAL_SOURCE_WP_JSON: + case constants.answerType.EXTERNAL_SOURCE_WP_TITLES: predefinedSourceHelper.getAnswerFromWP(intent.externalAnswerSource).then(answer=>{ answerPromiseProps.resolve(answer); }).catch(error=>{ answerPromiseProps.reject(error); }); break; - case constants.answerType.EXTERNAL_SOURCE_RSS: + case constants.answerType.EXTERNAL_SOURCE_WP_NEWS: answer = 'Not implemented yet' answerPromiseProps.resolve(answer); break; } - answer.then(answer=>{ - this.response - .speak (answer) - .listen (constants.voiceResponseStrings.GENERIC_CONTINUE); //Phrase from listen doesn't work !!! - this.emit (':responseReady'); - }).catch(error=>{ - this.response - .speak (error) - .listen (constants.voiceResponseStrings.GENERIC_CONTINUE); //Phrase from listen doesn't work !!! - this.emit (':responseReady'); - }); + answerPromise + .then (answer => { + this.response + .speak (answer) + .listen (constants.voiceResponseStrings.GENERIC_CONTINUE); //Phrase from listen doesn't work !!! + this.emit (':responseReady'); + }) + .catch (error => { + this.response + .speak (error) + .listen (constants.voiceResponseStrings.GENERIC_CONTINUE); //Phrase from listen doesn't work !!! + this.emit (':responseReady'); + }); }; }); @@ -213,7 +217,7 @@ module.exports = { if (this.attributes['LaunchRequestYesNo']) { this.attributes['LaunchRequestYesNo'] = false; } - this.response.speak ('Thank you for using Saburly'); + this.response.speak (`Thank you for using ${skillName}`); this.emit (':responseReady'); }; diff --git a/web/src/App.js b/web/src/App.js index bed0a50..7d1e21b 100644 --- a/web/src/App.js +++ b/web/src/App.js @@ -253,30 +253,21 @@ class App extends Component { } //Check for same question variants and same question name in other intents + //all intents with the same intentName, or some of the questions are the same + //will be kept in filteredIntents. After filterring, there should be only one + //intent left, the selected one + + let filteredIntents = this.state.allIntents.filter(intent=>{ + let result = (selectedIntent.intentName === intent.intentName); + let filteredQuestions = intent.questions.filter(question=>{ + return (selectedIntent.questions.indexOf(question)!==-1); + }); + return (result || filteredQuestions.length > 0); + }); - for (let i = 0; i < this.state.allIntents.length; i++) { - if (i !== this.state.selectedIndex) { - if (selectedIntent.intentName === this.state.allIntents[i].intentName) { - Popup.alert ('Question name already exists'); - return; - } - - for (let j = 0; j < selectedIntent.questions.length; j++) { - for (let k = 0; k < this.state.allIntents[i].questions.length; k++) { - if ( - selectedIntent.questions[j] === - this.state.allIntents[i].questions[k] - ) { - Popup.alert ( - 'Question variant already exists (in question :' + - this.state.allIntents[i].intentName + - ')' - ); - return; - } - } - } - } + if (filteredIntents.length > 1){ + Popup.alert('Question name or question variant already exists'); + return; } let newAllIntentsJSON = JSON.stringify (this.state.allIntents); diff --git a/web/src/components/AnswerSource.js b/web/src/components/AnswerSource.js index 63aef82..9f6710a 100644 --- a/web/src/components/AnswerSource.js +++ b/web/src/components/AnswerSource.js @@ -1,9 +1,7 @@ import React, {Component} from 'react'; -import {Button, SelectionControlGroup} from 'react-md'; -import Modal from './modal/Modal'; -import { - ANSWER_TYPE -} from '../config/constants'; +import {Button} from 'react-md'; +import AnswerSourceForm from './helper/AnswerSourceForm'; +import '../css/components/IntentDetails.css'; class AnswerSource extends Component { constructor (props) { @@ -30,67 +28,27 @@ class AnswerSource extends Component { onSave(){ this.onClose(); - this.props.onSaveHandle(this.state.answerType); + this.props.onSaveAnswerType(this.state.answerType); } onSourceChange(value, event){ - this.setState({answerType:parseInt(value)}); + this.setState({answerType:parseInt(value,10)}); } render () { let modal; if (this.state.isModalOpen) { - modal = ( - - Cancel - , - , - ]} - > - - - ); + modal = } return (
- {modal} diff --git a/web/src/components/IntentDetails.js b/web/src/components/IntentDetails.js index 45b9270..ff5827f 100644 --- a/web/src/components/IntentDetails.js +++ b/web/src/components/IntentDetails.js @@ -10,6 +10,7 @@ import { INTENT_EXPLANATION_MAX_LENGTH, ANSWER_TYPE, } from '../config/constants'; +import AnswerTextBox from './helper/AnswerTextBox.js'; class IntentDetails extends Component { constructor (props) { @@ -20,13 +21,10 @@ class IntentDetails extends Component { this.addQuestion = this.addQuestion.bind (this); this.deleteQuestion = this.deleteQuestion.bind (this); this.handleQuestionEdit = this.handleQuestionEdit.bind (this); - this.handleAnswerEdit = this.handleAnswerEdit.bind (this); - this.handleAnswerSourceEdit = this.handleAnswerSourceEdit.bind (this); this.handleIntentNameEdit = this.handleIntentNameEdit.bind (this); this.handleIntentExplanationEdit = this.handleIntentExplanationEdit.bind ( this ); - this.handleExternalSourceSave = this.handleExternalSourceSave.bind (this); } componentWillReceiveProps (props) { @@ -34,43 +32,6 @@ class IntentDetails extends Component { } render () { - let answerBox; - switch (this.state.intent.answerType) { - case ANSWER_TYPE.PREDEFINED: - answerBox = ( -
- -
- ); - break; - case ANSWER_TYPE.EXTERNAL_SOURCE_WP_JSON: - case ANSWER_TYPE.EXTERNAL_SOURCE_RSS: - answerBox = ( -
- -
- ); - break; - } - return (
@@ -140,11 +101,17 @@ class IntentDetails extends Component { add - {answerBox} + , + , + ]} + > + + ); + } +} + +export default AnswerSourceForm; \ No newline at end of file diff --git a/web/src/components/helper/AnswerTextBox.js b/web/src/components/helper/AnswerTextBox.js new file mode 100644 index 0000000..0dd630c --- /dev/null +++ b/web/src/components/helper/AnswerTextBox.js @@ -0,0 +1,44 @@ +import React, {Component} from 'react'; +import {TextField} from 'react-md'; +import '../../css/components/IntentDetails.css'; +import '../../css/Common.css'; +import { + ANSWER_MAX_LENGTH, + ANSWER_TYPE, +} from '../../config/constants'; + +class AnswerTextBox extends Component { + + render () { + //theese are defaults for ANSWER_TYPE.PREDEFINED + let labelText="Answer"; + let valueText=this.props.answer; + let onChangeValue=this.props.handleAnswerEdit; + switch(this.props.answerType){ + case ANSWER_TYPE.EXTERNAL_SOURCE_WP_TITLES: + case ANSWER_TYPE.EXTERNAL_SOURCE_WP_NEWS: + labelText="Answer source"; + valueText=this.props.externalAnswerSource; + onChangeValue=this.props.handleAnswerSourceEdit + break; + } + + + return( +
+ +
+ ); + } +} + +export default AnswerTextBox; diff --git a/web/src/components/modal/Modal.js b/web/src/components/helper/Modal.js similarity index 100% rename from web/src/components/modal/Modal.js rename to web/src/components/helper/Modal.js diff --git a/web/src/config/constants.js b/web/src/config/constants.js index 5a3b6f6..4c93a6d 100644 --- a/web/src/config/constants.js +++ b/web/src/config/constants.js @@ -24,8 +24,8 @@ export const CONTACT_SELECTED_INDEX = -3; export const ANSWER_TYPE = { PREDEFINED: 0, - EXTERNAL_SOURCE_WP_JSON : 1, - EXTERNAL_SOURCE_RSS : 2 + EXTERNAL_SOURCE_WP_TITLES : 1, + EXTERNAL_SOURCE_WP_NEWS : 2 } export const RESULT_CODES = { diff --git a/web/src/css/components/IntentDetails.scss b/web/src/css/components/IntentDetails.scss index a307b93..e33114f 100644 --- a/web/src/css/components/IntentDetails.scss +++ b/web/src/css/components/IntentDetails.scss @@ -27,7 +27,7 @@ margin-right: 10%; } -.QuestionTypeButton{ - float: left; - margin-left: 30px; +.AnswerTypeButton{ + float: right; + margin-right: 30px; } \ No newline at end of file