Created
February 6, 2019 06:42
-
-
Save dennyjohnk/7a043f64aa98fb370269fce1f6c56720 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import { Widget, addResponseMessage, addUserMessage, toggleInputDisabled } from './lib/index'; | |
import axios from 'axios'; | |
import { API_URL } from './constants'; | |
import { API_ROOT } from './api-config'; | |
import './lib/styles.css'; | |
export default class App extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
title: null, | |
subtitle: null, | |
senderPlaceHolder: null, | |
profileAvatar: 'https://i.imgur.com/vXzDKuh.gif', | |
botPrimaryColor: null, | |
botGradient1: 'red', | |
botGradient2: 'blue' | |
}; | |
} | |
/** Get chat configuration */ | |
componentWillMount() { | |
this.GetChatConfiguration(); | |
} | |
/** Ping server */ | |
componentDidMount() { | |
this.PingServer(); | |
} | |
GetChatConfiguration() { | |
var url_string = window.location.href; | |
var botId = url_string.split('botId=')[1]; | |
/* | |
check if no bot id | |
*/ | |
axios({ | |
method: 'get', | |
url: API_ROOT + API_URL.CONFIG, | |
params: { | |
botId: botId | |
}, | |
config: { headers: { 'Content-Type': 'application/json' } } | |
}) | |
.then(response => { | |
this.setState( | |
{ | |
title: response.data.botTitle, | |
subtitle: response.data.botSubTitle, | |
senderPlaceHolder: response.data.botPlaceholder, | |
botGradient1: response.data.botGradient1, | |
botGradient2: response.data.botGradient2, | |
botPrimaryColor: response.data.botPrimaryColor | |
}); | |
}) | |
.catch((error) => { | |
console.error(error); | |
}); | |
} | |
PingServer() { | |
axios({ | |
method: 'post', | |
url: API_ROOT + API_URL.PING, | |
withCredentials: true, | |
data: { | |
'domain': window.location.href | |
}, | |
config: { headers: { 'Content-Type': 'application/json' } } | |
}) | |
.then(response => { | |
for (var i = 0; i < response.data.length; i++) { | |
if (response.data[i].isBot) | |
addResponseMessage(response.data[i].text); | |
else | |
addUserMessage(response.data[i].text); | |
} | |
}); | |
} | |
GetMessage() { | |
axios.get('${API_ROOT}') | |
.then(function (response) { | |
return response.data; | |
}); | |
} | |
handleNewUserMessage = (newMessage) => { | |
axios({ | |
method: 'post', | |
url: API_ROOT + API_URL.MESSAGE, | |
withCredentials: true, | |
data: { | |
'domain': window.location.href, | |
'message': newMessage | |
}, | |
config: { headers: { 'Content-Type': 'application/json' } } | |
}) | |
.then(response => { | |
addResponseMessage(response.data); | |
}); | |
} | |
render() { | |
return ( | |
<Widget | |
title={this.state.title} | |
subtitle={this.state.subtitle} | |
senderPlaceHolder={this.state.senderPlaceHolder} | |
handleNewUserMessage={this.handleNewUserMessage} | |
profileAvatar={this.state.profileAvatar} | |
botGradient1={this.state.botGradient1} | |
botGradient2={this.state.botGradient2} | |
botPrimaryColor={this.state.botPrimaryColor} | |
badge={0} | |
/> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment