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

179 lines
6.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-05 17:56:24 +01:00
import Popup from 'react-popup';
2017-11-30 17:43:24 +01:00
import {getSkill, updateSkill} from './lib/api'
2017-11-30 17:43:24 +01:00
class App extends Component {
constructor(props){
super(props);
2018-01-07 13:54:15 +00:00
this.state={_id:'5a5016e775becaef2015da10',
skillID:'',
2017-12-03 00:15:11 +01:00
skillName:'',
invocationName:'Saburly',
invocationAnswer:'We are saburly',
allIntents:[],
selectedIntent: {intentName:'',questions:[''],answer:''},
selectedIndex:-1,
launchRequest:false};
2017-11-30 17:43:24 +01:00
getSkill(this.state._id).then(l=> l.text()).then(result=>{
let jResult = JSON.parse(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,
allIntents: jResult.intents})
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);
2017-11-30 17:43:24 +01:00
}
render() {
if(this.state.launchRequest){
return (
<div className="App">
2018-01-05 17:56:24 +01:00
<Popup/>
<div className="App-header">
<h1> Tell All </h1>
</div>
<IntentList allIntents={this.state.allIntents}
onLaunchRequestClick={this.handleLaunchRequestClick}
onIntentClick={this.handleIntentClick}
onAddIntentClick={this.handleAddIntentClick}
selectedIndex={this.state.selectedIndex}>
</IntentList>
<LaunchRequest invocationName={this.state.invocationName}
invocationAnswer={this.state.invocationAnswer}
onSaveClick={this.handleSaveLaunchRequestClick}> </LaunchRequest>
2017-11-30 17:43:24 +01:00
</div>
);
}else{
return (
<div className="App">
2018-01-05 17:56:24 +01:00
<Popup/>
<div className="App-header">
<h1> Tell All </h1>
</div>
<IntentList allIntents={this.state.allIntents}
onLaunchRequestClick={this.handleLaunchRequestClick}
onIntentClick={this.handleIntentClick}
onAddIntentClick={this.handleAddIntentClick}
selectedIndex={this.state.selectedIndex}>
</IntentList>
<IntentDetails selectedIntent={this.state.selectedIntent}
onDeleteIntentClick={this.handleDeleteIntentClick}
onSaveIntentClick={this.handleSaveIntentClick}>
</IntentDetails>
</div>
);
}
}
createSkill(intents, name, answer){
return {
_id: this.state._id,
skillID: this.state.skillID,
intents: intents,
invocationName: (name===undefined) ? this.state.invocationName : name,
invocationAnswer: (answer===undefined)? this.state.invocationAnswer: answer
};
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(){
this.setState({selectedIndex: -2, launchRequest:true});
}
handleSaveLaunchRequestClick(name, answer){
this.setState({invocationName: name, invocationAnswer: answer});
2018-01-05 00:51:49 +01:00
console.log("handleSaveLaunchRequest");
updateSkill(this.createSkill(this.state.allIntents,name,answer)).then(l=>l.text()).then(result=>{
2017-12-03 17:38:58 +01:00
let jResult = JSON.parse(result);
2018-01-05 17:56:24 +01:00
if (jResult.result !== 0){
Popup.alert('Model was not saved. Please try again');
}else{
Popup.alert('Saved');
}
2018-01-05 00:51:49 +01:00
console.log(jResult.message);
}).catch(e=>{
console.log("Error :" + e);
2018-01-05 17:56:24 +01:00
Popup.alert('Model was not saved. Please try again');
2017-11-30 17:43:24 +01:00
});
}
handleDeleteIntentClick(selectedIntent){
let id = this.state.allIntents.indexOf(selectedIntent);
if (id!==-1){
try{
//I don't like this, state in database is different than component state, for some time
//TODO : move database operation in componentWillUpdate or componentDidUpdate
let newAllIntents = this.state.allIntents;
newAllIntents.splice(id,1);
this.setState({allIntents: newAllIntents, selectedIntent: {intentName:'', questions:[''],answer:''}});
updateSkill(this.createSkill(newAllIntents)).then(l=>l.text()).then(result=>{
2017-12-03 17:38:58 +01:00
let jResult = JSON.parse(result);
2018-01-05 17:56:24 +01:00
if (jResult.result !== 0){
Popup.alert('Model was not saved. Please try again');
}else{
Popup.alert('Saved');
}
2018-01-05 00:51:49 +01:00
console.log(jResult.message);
}).catch(e=>{
console.log("error : " + e);
2018-01-05 17:56:24 +01:00
Popup.alert('Model was not saved. Please try again');
2017-11-30 17:43:24 +01:00
});
}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 newAllIntents = this.state.allIntents;
if (this.state.selectedIndex === -1){
//new intent
newAllIntents.push(selectedIntent);
this.setState({allIntents: newAllIntents, selectedIntent: selectedIntent, selectedIndex: newAllIntents.length-1});
}else{
newAllIntents[this.state.selectedIndex] = selectedIntent;
this.setState({allIntents: newAllIntents, selectedIntent: selectedIntent});
}
2018-01-05 00:51:49 +01:00
updateSkill(this.createSkill(newAllIntents)).then(l=>l.text()).then(result=>{
let jResult = JSON.parse(result);
2018-01-05 17:56:24 +01:00
if (jResult.result !== 0){
Popup.alert('Model was not saved. Please try again');
}else{
Popup.alert('Saved');
}
2018-01-05 00:51:49 +01:00
console.log(jResult.message);
}).catch(e=>{
2018-01-05 17:56:24 +01:00
Popup.alert('Model was not saved. Please try again');
2018-01-05 00:51:49 +01:00
console.log("error : " + e);
});
2017-11-30 17:43:24 +01:00
}
handleAddIntentClick(){
this.setState({allIntents: this.state.allIntents, selectedIndex: -1,launchRequest:false,selectedIntent: {intentName:'',questions:[''], answer:''}});
2017-11-30 17:43:24 +01:00
}
}
export default App;