diff --git a/src/App.js b/src/App.js
index 932a66d..414a697 100644
--- a/src/App.js
+++ b/src/App.js
@@ -4,11 +4,23 @@ import ChatArea from "./ChatArea";
import "./App.css";
class App extends Component {
+ constructor(props) {
+ super(props);
+ this.state = { messages: [] };
+ }
+ sendMessageHandler(newMessage) {
+ this.setState({
+ messages: [
+ ...this.state.messages,
+ newMessage
+ ]
+ });
+ }
render() {
return (
-
-
+
+
);
}
diff --git a/src/ChatArea.js b/src/ChatArea.js
index c4e314c..9df5d61 100644
--- a/src/ChatArea.js
+++ b/src/ChatArea.js
@@ -2,7 +2,18 @@ import React, { Component } from "react";
class ChatArea extends Component {
render() {
- return ;
+ var data = this.props.data || [];
+ return (
+
+ {data.map(function (message, i) {
+ return (
+
+ {message}
+
+ )
+ })}
+
+ )
}
}
diff --git a/src/MessageBar.js b/src/MessageBar.js
index 05a6028..5f928bd 100644
--- a/src/MessageBar.js
+++ b/src/MessageBar.js
@@ -1,13 +1,77 @@
import React, { Component } from "react";
import TextInput from "./TextInput";
import SendButton from "./SendButton";
+import PropTypes from "prop-types";
class MessageBar extends Component {
+ static propTypes = {
+ onSendMessage: PropTypes.func
+ }
+
+ constructor(props) {
+ super(props);
+ this.state = {
+ message: '',
+ isDisabled: false
+ };
+ }
+
+ cleanUp() {
+ this.setState({
+ message: ''
+ });
+ }
+
+ async onSendMessageHandler() {
+ if (this.state.isDisabled) {
+ return;
+ }
+
+ if (this.state.message === '/time') {
+ this.props.onSendMessage && this.props.onSendMessage((new Date()).toString());
+ } else if (this.state.message === '/goodbye') {
+ this.setState({
+ isDisabled: true
+ });
+ this.props.onSendMessage && this.props.onSendMessage('Goodbye. Have a nice day.');
+
+ } else if (this.state.message.startsWith('/starwars')) {
+ var character = this.state.message.split(' ')[1];
+ var people = await fetch('https://swapi.co/api/people/?search=' + character);
+ var json = await people.json();
+ var characterName = '';
+ characterName = json.results && json.results[0] && json.results[0].name;
+ if (characterName) {
+ this.props.onSendMessage && this.props.onSendMessage(characterName);
+ } else {
+ this.props.onSendMessage && this.props.onSendMessage('Your character is not available.');
+ }
+ } else if (this.state.message.startsWith('/')) {
+ this.props.onSendMessage && this.props.onSendMessage('Sorry. This command is not available.');
+ } else {
+ this.state.message && this.props.onSendMessage && this.props.onSendMessage(this.state.message);
+ }
+
+ this.cleanUp();
+ this.refs.messageInput.cleanUp();
+ }
+
+ onChangeHandler(e) {
+ this.setState({
+ message: e.target.value
+ });
+ }
+
render() {
return (
-
-
+
+
);
}
diff --git a/src/SendButton.js b/src/SendButton.js
index dd4ae98..272ce84 100644
--- a/src/SendButton.js
+++ b/src/SendButton.js
@@ -1,8 +1,14 @@
import React, { Component } from "react";
+import PropTypes from 'prop-types';
class SendButton extends Component {
+ static propTypes = {
+ onSendMessage: PropTypes.func,
+ isDisabled: PropTypes.bool
+ }
+
render() {
- return ;
+ return ;
}
}
diff --git a/src/TextInput.js b/src/TextInput.js
index 4b2acd0..7ce3523 100644
--- a/src/TextInput.js
+++ b/src/TextInput.js
@@ -1,10 +1,39 @@
import React, { Component } from "react";
+import PropTypes from 'prop-types';
class MessageBar extends Component {
+ static propTypes = {
+ onChange: PropTypes.func,
+ onSendMessage: PropTypes.func,
+ }
+
+ constructor(props) {
+ super(props);
+ this.state = { message: '' };
+ }
+
+ cleanUp() {
+ this.setState({ message: '' });
+ }
+
+ onChangeHanlder(e) {
+ this.props.onChange && this.props.onChange(e);
+ this.setState({ message: e.target.value });
+ }
+
+ onKeyPressHanlder(e) {
+ if (e.key === 'Enter') {
+ this.props.onSendMessage && this.props.onSendMessage();
+ }
+ }
+
render() {
return (
-
+
);
}