2017-11-30 17:43:24 +01:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
|
import './css/App.css';
|
2018-01-05 17:56:24 +01:00
|
|
|
import './css/popup.css';
|
2017-11-30 17:43:24 +01:00
|
|
|
import IntentList from './components/IntentList';
|
|
|
|
|
import IntentDetails from './components/IntentDetails';
|
2017-12-02 22:48:45 +01:00
|
|
|
import LaunchRequest from './components/LaunchRequest';
|
2018-01-11 04:24:16 +01:00
|
|
|
import Contact from './components/Contact';
|
2018-01-05 17:56:24 +01:00
|
|
|
import Popup from 'react-popup';
|
2017-12-02 22:48:45 +01:00
|
|
|
import {getSkill, updateSkill} from './lib/api'
|
2018-01-11 04:24:16 +01:00
|
|
|
import {
|
|
|
|
|
NEW_INTENT_SELECTED_INDEX,
|
|
|
|
|
LAUNCH_REQUEST_SELECTED_INDEX,
|
2018-01-12 01:56:17 +01:00
|
|
|
CONTACT_SELECTED_INDEX,
|
|
|
|
|
RESULT_CODES} from './config/constants'
|
2017-11-30 17:43:24 +01:00
|
|
|
|
|
|
|
|
class App extends Component {
|
|
|
|
|
|
|
|
|
|
constructor(props){
|
|
|
|
|
super(props);
|
|
|
|
|
|
2018-01-14 01:00:35 +01:00
|
|
|
this.state={_id:'5a232fb86ce046c749739455',
|
2017-12-02 22:48:45 +01:00
|
|
|
skillID:'',
|
2017-12-03 00:15:11 +01:00
|
|
|
skillName:'',
|
2017-12-02 22:48:45 +01:00
|
|
|
invocationName:'Saburly',
|
|
|
|
|
invocationAnswer:'We are saburly',
|
|
|
|
|
allIntents:[],
|
|
|
|
|
selectedIntent: {intentName:'',questions:[''],answer:''},
|
2018-01-11 04:24:16 +01:00
|
|
|
selectedIndex:NEW_INTENT_SELECTED_INDEX,
|
|
|
|
|
contactEmail:'',
|
2018-01-10 13:27:09 +01:00
|
|
|
waiting: false
|
|
|
|
|
};
|
2017-11-30 17:43:24 +01:00
|
|
|
|
2018-01-14 01:00:35 +01:00
|
|
|
getSkill(this.state._id).then(l=>l.json()).then(result=>{
|
|
|
|
|
let jResult = result[0];
|
2017-12-02 22:48:45 +01:00
|
|
|
if (jResult===undefined) return;
|
2017-12-03 00:15:11 +01:00
|
|
|
this.setState({ skillID:jResult.skillID,skillName:jResult.skillName, invocationName: jResult.invocationName,
|
2017-12-02 22:48:45 +01:00
|
|
|
invocationAnswer: jResult.invocationAnswer,
|
2018-01-11 04:24:16 +01:00
|
|
|
allIntents: jResult.intents, contactEmail: jResult.contactEmail})
|
2017-11-30 17:43:24 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
this.handleIntentClick = this.handleIntentClick.bind(this);
|
2017-12-02 22:48:45 +01:00
|
|
|
this.handleLaunchRequestClick = this.handleLaunchRequestClick.bind(this);
|
2017-11-30 17:43:24 +01:00
|
|
|
this.handleDeleteIntentClick = this.handleDeleteIntentClick.bind(this);
|
|
|
|
|
this.handleSaveIntentClick = this.handleSaveIntentClick.bind(this);
|
|
|
|
|
this.handleAddIntentClick = this.handleAddIntentClick.bind(this);
|
2017-12-02 22:48:45 +01:00
|
|
|
this.handleSaveLaunchRequestClick = this.handleSaveLaunchRequestClick.bind(this);
|
|
|
|
|
this.createSkill = this.createSkill.bind(this);
|
2018-01-10 13:27:09 +01:00
|
|
|
this.sendSkill = this.sendSkill.bind(this);
|
2018-01-11 04:24:16 +01:00
|
|
|
this.handleContactClick = this.handleContactClick.bind(this);
|
|
|
|
|
this.handleSaveEmailClick = this.handleSaveEmailClick.bind(this);
|
2017-11-30 17:43:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
2018-01-14 01:00:35 +01:00
|
|
|
let rightPanel;
|
|
|
|
|
switch (this.state.selectedIndex) {
|
|
|
|
|
case LAUNCH_REQUEST_SELECTED_INDEX:
|
|
|
|
|
rightPanel = <LaunchRequest invocationName={this.state.invocationName}
|
|
|
|
|
invocationAnswer={this.state.invocationAnswer}
|
|
|
|
|
onSaveClick={this.handleSaveLaunchRequestClick}
|
|
|
|
|
waiting={this.state.waiting}/> ;
|
|
|
|
|
break;
|
|
|
|
|
case CONTACT_SELECTED_INDEX:
|
|
|
|
|
rightPanel = <Contact contactEmail={this.state.contactEmail}
|
|
|
|
|
onSaveEmailClick={this.handleSaveEmailClick}
|
|
|
|
|
waiting={this.state.waiting}/> ;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
rightPanel = <IntentDetails selectedIntent={this.state.selectedIntent}
|
|
|
|
|
onDeleteIntentClick={this.handleDeleteIntentClick}
|
|
|
|
|
onSaveIntentClick={this.handleSaveIntentClick}
|
|
|
|
|
waiting={this.state.waiting}/>;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-11 04:24:16 +01:00
|
|
|
return(
|
|
|
|
|
<div className="App">
|
|
|
|
|
<Popup/>
|
|
|
|
|
<div className="App-header">
|
|
|
|
|
<h1> Tell All </h1>
|
2017-11-30 17:43:24 +01:00
|
|
|
</div>
|
2018-01-11 04:24:16 +01:00
|
|
|
<IntentList allIntents={this.state.allIntents}
|
|
|
|
|
onLaunchRequestClick={this.handleLaunchRequestClick}
|
|
|
|
|
onContactClick={this.handleContactClick}
|
|
|
|
|
onIntentClick={this.handleIntentClick}
|
|
|
|
|
onAddIntentClick={this.handleAddIntentClick}
|
|
|
|
|
selectedIndex={this.state.selectedIndex}
|
2018-01-14 01:00:35 +01:00
|
|
|
waiting={this.state.waiting}/>
|
|
|
|
|
|
|
|
|
|
{rightPanel}
|
2018-01-11 04:24:16 +01:00
|
|
|
</div>
|
|
|
|
|
);
|
2017-12-02 22:48:45 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-11 04:24:16 +01:00
|
|
|
createSkill(intents, name, answer, email, updateOnAmazon){
|
2017-12-02 22:48:45 +01:00
|
|
|
return {
|
|
|
|
|
_id: this.state._id,
|
|
|
|
|
skillID: this.state.skillID,
|
|
|
|
|
intents: intents,
|
2018-01-11 04:24:16 +01:00
|
|
|
invocationName: name,
|
|
|
|
|
invocationAnswer: answer,
|
|
|
|
|
contactEmail: email,
|
|
|
|
|
updateOnAmazon: updateOnAmazon
|
2017-12-02 22:48:45 +01:00
|
|
|
};
|
2017-11-30 17:43:24 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-01 11:03:48 +01:00
|
|
|
handleIntentClick(selectedIntent, index){
|
2017-12-02 22:48:45 +01:00
|
|
|
this.setState({selectedIntent:selectedIntent, selectedIndex: index, launchRequest:false});
|
2017-11-30 17:43:24 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-02 22:48:45 +01:00
|
|
|
handleLaunchRequestClick(){
|
2018-01-11 04:24:16 +01:00
|
|
|
this.setState({selectedIndex: LAUNCH_REQUEST_SELECTED_INDEX});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleContactClick(){
|
|
|
|
|
this.setState({selectedIndex: CONTACT_SELECTED_INDEX})
|
2017-12-02 22:48:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleSaveLaunchRequestClick(name, answer){
|
2018-01-10 13:27:09 +01:00
|
|
|
this.setState({waiting:true, invocationName:name, invocationAnswer: answer});
|
2018-01-11 04:24:16 +01:00
|
|
|
this.sendSkill(this.state.allIntents,true,{waiting:false},{waiting:false},name,answer,this.state.contactEmail,true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleSaveEmailClick(email){
|
|
|
|
|
this.setState({waiting:true});
|
|
|
|
|
this.sendSkill(this.state.allIntents,true,{contactEmail: email, waiting:false},{waiting:false},this.state.invocationName,this.state.invocationAnswer,email,false);
|
2017-11-30 17:43:24 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-02 22:48:45 +01:00
|
|
|
handleDeleteIntentClick(selectedIntent){
|
2018-01-10 13:27:09 +01:00
|
|
|
let id = -1;
|
2018-01-15 14:52:34 +01:00
|
|
|
//TODO : Change comparsion method ! Same object with different proeprty sorting will not be same string
|
2018-01-10 13:27:09 +01:00
|
|
|
this.state.allIntents.map((intent,index)=>{
|
|
|
|
|
if ((id===-1) && (JSON.stringify(selectedIntent)===JSON.stringify(intent)))
|
|
|
|
|
id = index;
|
|
|
|
|
});
|
|
|
|
|
|
2017-12-02 22:48:45 +01:00
|
|
|
if (id!==-1){
|
|
|
|
|
try{
|
2018-01-10 13:27:09 +01:00
|
|
|
let newAllIntentsJSON = JSON.stringify(this.state.allIntents);
|
|
|
|
|
let newAllIntents = JSON.parse(newAllIntentsJSON);
|
2017-12-02 22:48:45 +01:00
|
|
|
newAllIntents.splice(id,1);
|
2018-01-10 13:27:09 +01:00
|
|
|
this.setState({waiting:true});
|
|
|
|
|
|
|
|
|
|
let newState = {allIntents: newAllIntents, selectedIntent: {intentName:'', questions:[''],answer:''}, waiting:false};
|
2018-01-11 04:24:16 +01:00
|
|
|
this.sendSkill(newAllIntents,true,newState,{waiting:false},this.state.invocationName,this.state.invocationAnswer,this.state.contactEmail,true);
|
2018-01-10 13:27:09 +01:00
|
|
|
|
2017-12-02 22:48:45 +01:00
|
|
|
}catch(e){
|
2018-01-05 00:51:49 +01:00
|
|
|
console.log("error : " + e);
|
2017-11-30 17:43:24 +01:00
|
|
|
}
|
2017-12-02 22:48:45 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
handleSaveIntentClick(selectedIntent){
|
2018-01-10 13:27:09 +01:00
|
|
|
let newAllIntentsJSON = JSON.stringify(this.state.allIntents);
|
|
|
|
|
let newAllIntents = JSON.parse(newAllIntentsJSON);
|
|
|
|
|
|
|
|
|
|
let newState = null;
|
2018-01-11 04:24:16 +01:00
|
|
|
if (this.state.selectedIndex === NEW_INTENT_SELECTED_INDEX){
|
2017-12-02 22:48:45 +01:00
|
|
|
//new intent
|
|
|
|
|
newAllIntents.push(selectedIntent);
|
2018-01-10 13:27:09 +01:00
|
|
|
newState = {allIntents: newAllIntents, selectedIntent: selectedIntent, selectedIndex: newAllIntents.length-1, waiting:false};
|
2017-12-02 22:48:45 +01:00
|
|
|
}else{
|
|
|
|
|
newAllIntents[this.state.selectedIndex] = selectedIntent;
|
2018-01-10 13:27:09 +01:00
|
|
|
newState = {allIntents: newAllIntents, selectedIntent: selectedIntent, waiting: false};
|
2017-12-02 22:48:45 +01:00
|
|
|
}
|
2018-01-10 13:27:09 +01:00
|
|
|
this.setState({waiting:true});
|
2018-01-11 04:24:16 +01:00
|
|
|
this.sendSkill(newAllIntents, true, newState, {waiting:false}, this.state.invocationName,this.state.invocationAnswer,this.state.contactEmail, true);
|
2017-11-30 17:43:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleAddIntentClick(){
|
2018-01-11 04:24:16 +01:00
|
|
|
this.setState({allIntents: this.state.allIntents, selectedIndex: NEW_INTENT_SELECTED_INDEX,launchRequest:false,selectedIntent: {intentName:'',questions:[''], answer:''}});
|
2017-11-30 17:43:24 +01:00
|
|
|
}
|
2018-01-10 13:27:09 +01:00
|
|
|
|
2018-01-11 04:24:16 +01:00
|
|
|
sendSkill(newAllIntents, showPopUp, resolveState, rejectState, newName, newAnswer, email, updateOnAmazon){
|
2018-01-10 13:27:09 +01:00
|
|
|
return new Promise((resolve,reject)=>{
|
2018-01-14 01:00:35 +01:00
|
|
|
updateSkill(this.createSkill(newAllIntents,newName,newAnswer,email,updateOnAmazon)).then(l=>l.json()).then(result=>{
|
|
|
|
|
if (result.result !== RESULT_CODES.OK){
|
|
|
|
|
console.log(result.result);
|
2018-01-10 13:27:09 +01:00
|
|
|
if (showPopUp) Popup.alert('Model was not saved. Please try again');
|
|
|
|
|
this.setState(rejectState);
|
|
|
|
|
//reject('Error code : ' + jResult.result);
|
|
|
|
|
}else{
|
|
|
|
|
if (showPopUp) Popup.alert('Saved');
|
|
|
|
|
this.setState(resolveState);
|
|
|
|
|
resolve();
|
|
|
|
|
}
|
|
|
|
|
}).catch(e=>{
|
2018-01-12 01:56:17 +01:00
|
|
|
console.log('error : ' + e);
|
2018-01-10 13:27:09 +01:00
|
|
|
if (showPopUp) Popup.alert('Model was not saved. Please try again');
|
|
|
|
|
this.setState(rejectState);
|
|
|
|
|
//reject(e);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-11-30 17:43:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default App;
|