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

307 lines
8.3 KiB
JavaScript
Raw Normal View History

2018-01-19 22:42:37 +01:00
import React, {Component} from 'react';
2017-11-30 17:43:24 +01:00
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';
2018-01-19 22:42:37 +01:00
import {getSkill, updateSkill} from './lib/api';
2018-01-11 04:24:16 +01:00
import {
2018-01-19 22:42:37 +01:00
NEW_INTENT_SELECTED_INDEX,
LAUNCH_REQUEST_SELECTED_INDEX,
CONTACT_SELECTED_INDEX,
RESULT_CODES,
} from './config/constants';
2017-11-30 17:43:24 +01:00
class App extends Component {
2018-01-19 22:42:37 +01:00
constructor (props) {
super (props);
2017-11-30 17:43:24 +01:00
2018-01-19 22:42:37 +01:00
this.state = {
_id: '5a232fb86ce046c749739455',
skillID: '',
skillName: '',
invocationName: 'Saburly',
invocationAnswer: 'We are saburly',
allIntents: [],
selectedIntent: {
intentName: '',
intentExplanation: '',
questions: [''],
answer: '',
},
selectedIndex: NEW_INTENT_SELECTED_INDEX,
contactEmail: '',
waiting: false,
};
getSkill (this.state._id).then (l => l.json ()).then (result => {
if (result === undefined) return;
this.setState ({
skillID: result.skillID,
skillName: result.skillName,
invocationName: result.invocationName,
invocationAnswer: result.invocationAnswer,
allIntents: result.intents,
contactEmail: result.contactEmail,
});
});
this.handleIntentClick = this.handleIntentClick.bind (this);
this.handleLaunchRequestClick = this.handleLaunchRequestClick.bind (this);
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);
this.handleContactClick = this.handleContactClick.bind (this);
this.handleSaveEmailClick = this.handleSaveEmailClick.bind (this);
2017-11-30 17:43:24 +01:00
}
2018-01-19 22:42:37 +01:00
render () {
2018-01-14 01:00:35 +01:00
let rightPanel;
switch (this.state.selectedIndex) {
case LAUNCH_REQUEST_SELECTED_INDEX:
2018-01-19 22:42:37 +01:00
rightPanel = (
<LaunchRequest
invocationName={this.state.invocationName}
invocationAnswer={this.state.invocationAnswer}
onSaveClick={this.handleSaveLaunchRequestClick}
waiting={this.state.waiting}
/>
);
2018-01-14 01:00:35 +01:00
break;
case CONTACT_SELECTED_INDEX:
2018-01-19 22:42:37 +01:00
rightPanel = (
<Contact
contactEmail={this.state.contactEmail}
onSaveEmailClick={this.handleSaveEmailClick}
waiting={this.state.waiting}
/>
);
2018-01-14 01:00:35 +01:00
break;
default:
2018-01-19 22:42:37 +01:00
rightPanel = (
<IntentDetails
selectedIntent={this.state.selectedIntent}
onDeleteIntentClick={this.handleDeleteIntentClick}
onSaveIntentClick={this.handleSaveIntentClick}
waiting={this.state.waiting}
/>
);
2018-01-14 01:00:35 +01:00
}
2018-01-19 22:42:37 +01:00
return (
2018-01-11 04:24:16 +01:00
<div className="App">
2018-01-19 22:42:37 +01:00
<Popup />
2018-01-11 04:24:16 +01:00
<div className="App-header">
<h1> Tell All </h1>
2017-11-30 17:43:24 +01:00
</div>
2018-01-19 22:42:37 +01:00
<IntentList
allIntents={this.state.allIntents}
onLaunchRequestClick={this.handleLaunchRequestClick}
onContactClick={this.handleContactClick}
onIntentClick={this.handleIntentClick}
onAddIntentClick={this.handleAddIntentClick}
selectedIndex={this.state.selectedIndex}
waiting={this.state.waiting}
/>
2018-01-14 01:00:35 +01:00
{rightPanel}
2018-01-11 04:24:16 +01:00
</div>
);
}
2018-01-19 22:42:37 +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,
2018-01-19 22:42:37 +01:00
updateOnAmazon: updateOnAmazon,
};
2017-11-30 17:43:24 +01:00
}
2018-01-19 22:42:37 +01:00
handleIntentClick (selectedIntent, index) {
this.setState ({
selectedIntent: selectedIntent,
selectedIndex: index,
launchRequest: false,
});
2017-11-30 17:43:24 +01:00
}
2018-01-19 22:42:37 +01:00
handleLaunchRequestClick () {
this.setState ({selectedIndex: LAUNCH_REQUEST_SELECTED_INDEX});
2018-01-11 04:24:16 +01:00
}
2018-01-19 22:42:37 +01:00
handleContactClick () {
this.setState ({selectedIndex: CONTACT_SELECTED_INDEX});
}
2018-01-19 22:42:37 +01:00
handleSaveLaunchRequestClick (name, answer) {
this.setState ({
waiting: true,
invocationName: name,
invocationAnswer: answer,
});
this.sendSkill (
this.state.allIntents,
true,
{waiting: false},
{waiting: false},
name,
answer,
this.state.contactEmail,
true
);
2018-01-11 04:24:16 +01:00
}
2018-01-19 22:42:37 +01:00
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
}
2018-01-19 22:42:37 +01:00
handleDeleteIntentClick (selectedIntent) {
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-19 22:42:37 +01:00
this.state.allIntents.map ((intent, index) => {
if (
id === -1 &&
JSON.stringify (selectedIntent) === JSON.stringify (intent)
)
id = index;
});
2018-01-19 22:42:37 +01:00
if (id !== -1) {
try {
let newAllIntentsJSON = JSON.stringify (this.state.allIntents);
let newAllIntents = JSON.parse (newAllIntentsJSON);
newAllIntents.splice (id, 1);
this.setState ({waiting: true});
2018-01-19 22:42:37 +01:00
let newState = {
allIntents: newAllIntents,
selectedIntent: {intentName: '', questions: [''], answer: ''},
waiting: false,
};
this.sendSkill (
newAllIntents,
true,
newState,
{waiting: false},
this.state.invocationName,
this.state.invocationAnswer,
this.state.contactEmail,
true
);
} catch (e) {
console.log ('error : ' + e);
2017-11-30 17:43:24 +01:00
}
}
}
2018-01-19 22:42:37 +01:00
handleSaveIntentClick (selectedIntent) {
let newAllIntentsJSON = JSON.stringify (this.state.allIntents);
let newAllIntents = JSON.parse (newAllIntentsJSON);
let newState = null;
2018-01-19 22:42:37 +01:00
if (this.state.selectedIndex === NEW_INTENT_SELECTED_INDEX) {
//new intent
2018-01-19 22:42:37 +01:00
newAllIntents.push (selectedIntent);
newState = {
allIntents: newAllIntents,
selectedIntent: selectedIntent,
selectedIndex: newAllIntents.length - 1,
waiting: false,
};
} else {
newAllIntents[this.state.selectedIndex] = selectedIntent;
2018-01-19 22:42:37 +01:00
newState = {
allIntents: newAllIntents,
selectedIntent: selectedIntent,
waiting: false,
};
}
2018-01-19 22:42:37 +01:00
this.setState ({waiting: true});
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
}
2018-01-19 22:42:37 +01:00
handleAddIntentClick () {
this.setState ({
allIntents: this.state.allIntents,
selectedIndex: NEW_INTENT_SELECTED_INDEX,
launchRequest: false,
2018-01-23 01:51:36 +01:00
selectedIntent: {intentName: '', questions: [''], answer: '', intentExplanation:''},
2018-01-19 22:42:37 +01:00
});
2017-11-30 17:43:24 +01:00
}
2018-01-19 22:42:37 +01:00
sendSkill (
newAllIntents,
showPopUp,
resolveState,
rejectState,
newName,
newAnswer,
email,
updateOnAmazon
) {
return new Promise ((resolve, reject) => {
updateSkill (
this.createSkill (
newAllIntents,
newName,
newAnswer,
email,
updateOnAmazon
)
)
.then (l => l.json ())
.then (result => {
if (result.result !== RESULT_CODES.OK) {
console.log (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 => {
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;