fixed delete bug, code refactoring, improved UI

This commit is contained in:
GotPPay
2018-01-10 13:27:09 +01:00
parent 84c11f1b2f
commit 3214a2bea4
12 changed files with 268 additions and 85 deletions

View File

@@ -13,7 +13,7 @@ class App extends Component {
constructor(props){
super(props);
this.state={_id:'5a5016e775becaef2015da10',
this.state={_id:'5a232fb86ce046c749739455',
skillID:'',
skillName:'',
invocationName:'Saburly',
@@ -21,7 +21,9 @@ class App extends Component {
allIntents:[],
selectedIntent: {intentName:'',questions:[''],answer:''},
selectedIndex:-1,
launchRequest:false};
launchRequest:false,
waiting: false
};
getSkill(this.state._id).then(l=> l.text()).then(result=>{
let jResult = JSON.parse(result)[0];
@@ -38,9 +40,11 @@ class App extends Component {
this.handleAddIntentClick = this.handleAddIntentClick.bind(this);
this.handleSaveLaunchRequestClick = this.handleSaveLaunchRequestClick.bind(this);
this.createSkill = this.createSkill.bind(this);
this.sendSkill = this.sendSkill.bind(this);
}
render() {
if(this.state.launchRequest){
return (
<div className="App">
@@ -52,11 +56,14 @@ class App extends Component {
onLaunchRequestClick={this.handleLaunchRequestClick}
onIntentClick={this.handleIntentClick}
onAddIntentClick={this.handleAddIntentClick}
selectedIndex={this.state.selectedIndex}>
selectedIndex={this.state.selectedIndex}
waiting={this.state.waiting}>
</IntentList>
<LaunchRequest invocationName={this.state.invocationName}
invocationAnswer={this.state.invocationAnswer}
onSaveClick={this.handleSaveLaunchRequestClick}> </LaunchRequest>
onSaveClick={this.handleSaveLaunchRequestClick}
waiting={this.state.waiting}>
</LaunchRequest>
</div>
);
}else{
@@ -70,11 +77,13 @@ class App extends Component {
onLaunchRequestClick={this.handleLaunchRequestClick}
onIntentClick={this.handleIntentClick}
onAddIntentClick={this.handleAddIntentClick}
selectedIndex={this.state.selectedIndex}>
selectedIndex={this.state.selectedIndex}
waiting={this.state.waiting}>
</IntentList>
<IntentDetails selectedIntent={this.state.selectedIntent}
onDeleteIntentClick={this.handleDeleteIntentClick}
onSaveIntentClick={this.handleSaveIntentClick}>
onSaveIntentClick={this.handleSaveIntentClick}
waiting={this.state.waiting}>
</IntentDetails>
</div>
);
@@ -100,43 +109,27 @@ class App extends Component {
}
handleSaveLaunchRequestClick(name, answer){
this.setState({invocationName: name, invocationAnswer: answer});
console.log("handleSaveLaunchRequest");
updateSkill(this.createSkill(this.state.allIntents,name,answer)).then(l=>l.text()).then(result=>{
let jResult = JSON.parse(result);
if (jResult.result !== 0){
Popup.alert('Model was not saved. Please try again');
}else{
Popup.alert('Saved');
}
console.log(jResult.message);
}).catch(e=>{
console.log("Error :" + e);
Popup.alert('Model was not saved. Please try again');
});
this.setState({waiting:true, invocationName:name, invocationAnswer: answer});
this.sendSkill(this.state.allIntents,true,{waiting:false},{waiting:false},name,answer);
}
handleDeleteIntentClick(selectedIntent){
let id = this.state.allIntents.indexOf(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{
//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;
let newAllIntentsJSON = JSON.stringify(this.state.allIntents);
let newAllIntents = JSON.parse(newAllIntentsJSON);
newAllIntents.splice(id,1);
this.setState({allIntents: newAllIntents, selectedIntent: {intentName:'', questions:[''],answer:''}});
updateSkill(this.createSkill(newAllIntents)).then(l=>l.text()).then(result=>{
let jResult = JSON.parse(result);
if (jResult.result !== 0){
Popup.alert('Model was not saved. Please try again');
}else{
Popup.alert('Saved');
}
console.log(jResult.message);
}).catch(e=>{
console.log("error : " + e);
Popup.alert('Model was not saved. Please try again');
});
this.setState({waiting:true});
let newState = {allIntents: newAllIntents, selectedIntent: {intentName:'', questions:[''],answer:''}, waiting:false};
this.sendSkill(newAllIntents,true,newState,{waiting:false});
}catch(e){
console.log("error : " + e);
}
@@ -145,34 +138,46 @@ class App extends Component {
handleSaveIntentClick(selectedIntent){
let newAllIntents = this.state.allIntents;
let newAllIntentsJSON = JSON.stringify(this.state.allIntents);
let newAllIntents = JSON.parse(newAllIntentsJSON);
let newState = null;
if (this.state.selectedIndex === -1){
//new intent
newAllIntents.push(selectedIntent);
this.setState({allIntents: newAllIntents, selectedIntent: selectedIntent, selectedIndex: newAllIntents.length-1});
newState = {allIntents: newAllIntents, selectedIntent: selectedIntent, selectedIndex: newAllIntents.length-1, waiting:false};
}else{
newAllIntents[this.state.selectedIndex] = selectedIntent;
this.setState({allIntents: newAllIntents, selectedIntent: selectedIntent});
newState = {allIntents: newAllIntents, selectedIntent: selectedIntent, waiting: false};
}
updateSkill(this.createSkill(newAllIntents)).then(l=>l.text()).then(result=>{
let jResult = JSON.parse(result);
if (jResult.result !== 0){
Popup.alert('Model was not saved. Please try again');
}else{
Popup.alert('Saved');
}
console.log(jResult.message);
}).catch(e=>{
Popup.alert('Model was not saved. Please try again');
console.log("error : " + e);
});
this.setState({waiting:true});
this.sendSkill(newAllIntents, true, newState, {waiting:false});
}
handleAddIntentClick(){
this.setState({allIntents: this.state.allIntents, selectedIndex: -1,launchRequest:false,selectedIntent: {intentName:'',questions:[''], answer:''}});
}
sendSkill(newAllIntents, showPopUp, resolveState, rejectState, newName, newAnswer){
return new Promise((resolve,reject)=>{
updateSkill(this.createSkill(newAllIntents,newName,newAnswer)).then(l=>l.text()).then(result=>{
let jResult = JSON.parse(result);
if (jResult.result !== 0){
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=>{
if (showPopUp) Popup.alert('Model was not saved. Please try again');
this.setState(rejectState);
//reject(e);
});
});
}
}
export default App;