Files
old-tellall/web/src/App.js

196 lines
7.5 KiB
JavaScript
Raw Normal View History

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';
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';
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',
skillID:'',
2017-12-03 00:15:11 +01:00
skillName:'',
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:'',
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];
if (jResult===undefined) return;
2017-12-03 00:15:11 +01:00
this.setState({ skillID:jResult.skillID,skillName:jResult.skillName, invocationName: jResult.invocationName,
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);
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);
this.handleSaveLaunchRequestClick = this.handleSaveLaunchRequestClick.bind(this);
this.createSkill = this.createSkill.bind(this);
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>
);
}
2018-01-11 04:24:16 +01:00
createSkill(intents, name, answer, email, updateOnAmazon){
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-11-30 17:43:24 +01:00
}
2017-12-01 11:03:48 +01:00
handleIntentClick(selectedIntent, index){
this.setState({selectedIntent:selectedIntent, selectedIndex: index, launchRequest:false});
2017-11-30 17:43:24 +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})
}
handleSaveLaunchRequestClick(name, answer){
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
}
handleDeleteIntentClick(selectedIntent){
let id = -1;
this.state.allIntents.map((intent,index)=>{
if ((id===-1) && (JSON.stringify(selectedIntent)===JSON.stringify(intent)))
id = index;
});
if (id!==-1){
try{
let newAllIntentsJSON = JSON.stringify(this.state.allIntents);
let newAllIntents = JSON.parse(newAllIntentsJSON);
newAllIntents.splice(id,1);
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);
}catch(e){
2018-01-05 00:51:49 +01:00
console.log("error : " + e);
2017-11-30 17:43:24 +01:00
}
}
}
handleSaveIntentClick(selectedIntent){
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){
//new intent
newAllIntents.push(selectedIntent);
newState = {allIntents: newAllIntents, selectedIntent: selectedIntent, selectedIndex: newAllIntents.length-1, waiting:false};
}else{
newAllIntents[this.state.selectedIndex] = selectedIntent;
newState = {allIntents: newAllIntents, selectedIntent: selectedIntent, waiting: false};
}
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-11 04:24:16 +01:00
sendSkill(newAllIntents, showPopUp, resolveState, rejectState, newName, newAnswer, email, updateOnAmazon){
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);
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);
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;