Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Solo chatbox #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.18.0",
"immutable": "^3.8.2",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-scripts": "1.1.0"
"react-redux": "^5.1.0",
"react-scripts": "1.1.0",
"redux": "^4.0.1",
"redux-immutable": "^4.0.0",
"reselect": "^4.0.0",
"styled-components": "^4.0.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
}
15 changes: 15 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,32 @@ body,
padding: 0;
}

body * {
box-sizing: border-box;
}

.app {
display: flex;
flex-direction: column;
position: relative;
padding-bottom: 40px;
}

.chatArea {
flex-grow: 1;
flex: 1;
overflow: auto;
margin: 10px;
border: 1px solid #ccc;
background-color: #fafafa;
}
.chatArea > p {
padding: 12px;
margin: 0;
}
.chatArea > p:nth-of-type(even) {
background: #eee;
}

.messageBar {
display: flex;
Expand Down
117 changes: 107 additions & 10 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,114 @@
import React, { Component } from "react";
import MessageBar from "./MessageBar";
import ChatArea from "./ChatArea";
import { connect } from 'react-redux';
import axios from 'axios';

import { commands } from './constants';
import MessageBar from "./components/MessageBar";
import ChatArea from "./components/ChatArea";
import ErrorMessage from "./components/ErrorMessage";
import "./App.css";
import { IDGenerator } from './utils';
import { addMessage, updateMessage, stopApplication } from './actions';

class App extends Component {
render() {
return (
<div className="app">
<ChatArea />
<MessageBar />
</div>
);
constructor(props) {
super(props);
this.state = {
errorMessage: ''
}
}
searchForCharacter = (messageId, query) => {
axios.get(`https://swapi.co/api/people/?search=${query}`).then(data => {
if(data) {
const { results } = data.data;
if (Array.isArray(results) && results.length) {
this.props.updateMessage(messageId, `Search results for \`${query}\`: ${results[0].name}`);
}
else {
this.props.updateMessage(messageId, `No results for \`${query}\``);
}
}
}, err => {
this.props.updateMessage(messageId, `There is an error when searching \`${query}\``);
});
}
messageConverter = (message) => {
let messageResults = null;
const messageArray = message.split(' ');
const messageCommand = messageArray.shift();
const id = IDGenerator();

const firstChar = messageCommand[0];
if (firstChar === '/') {
message = messageCommand.substring(1);
if (commands.indexOf(message) > -1) {
switch(message) {
case 'time':
messageResults = `Current time: ${new Date().toString()}`;
break;
case 'starwars':
const query = messageArray.join(' ');
if (!query.replace(/ /g,'')) {
messageResults = null;
this.setState({ errorMessage: 'Please enter a query' });
break;
}
this.searchForCharacter(id, query);
messageResults = `Searching character by name: ${query}`;
break;
case 'goodbye':
this.props.stopApplication();
messageResults = 'Bye! See you again.';
break;
default:
messageResults = message;
break;
}
}
else {
messageResults = `Command \`${message}\` does not exist.`;
}
}
else {
messageResults = message;
}
if (!messageResults) return null;
return { id, content: messageResults };
}
onSubmit = (message) => {
this.setState({ errorMessage: '' });
const finalMessage = this.messageConverter(message);
if (finalMessage) {
this.props.addMessage(finalMessage);
return true;
}
return false;

}
render() {
return (
<div className="app">
<ChatArea onSubmit={this.onSubmit} />
<MessageBar onSubmit={this.onSubmit} />
<ErrorMessage message={this.state.errorMessage} />
</div>
);
}
}

function mapDispatchToProps(dispatch) {
return {
addMessage(message) {
dispatch(addMessage(message));
},
updateMessage(messageId, content) {
dispatch(updateMessage(messageId, content));
},
stopApplication() {
dispatch(stopApplication());
},
};
}

export default App;
export default connect(null, mapDispatchToProps)(App);

9 changes: 0 additions & 9 deletions src/ChatArea.js

This file was deleted.

16 changes: 0 additions & 16 deletions src/MessageBar.js

This file was deleted.

9 changes: 0 additions & 9 deletions src/SendButton.js

This file was deleted.

13 changes: 0 additions & 13 deletions src/TextInput.js

This file was deleted.

11 changes: 11 additions & 0 deletions src/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { STOP_APPLICATION, ADD_MESSAGE, UPDATE_MESSAGE } from './constants';

export function stopApplication() {
return { type: STOP_APPLICATION };
}
export function addMessage(message) {
return { type: ADD_MESSAGE, message };
}
export function updateMessage(messageId, content) {
return { type: UPDATE_MESSAGE, messageId, content };
}
39 changes: 39 additions & 0 deletions src/components/ChatArea.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React, { Component } from "react";
import PropTypes from 'prop-types';
import { createStructuredSelector } from 'reselect';
import { connect } from 'react-redux';
import { selectApplicationStopped, selectMessages } from '../selectors';
import { stopApplication } from '../actions';

class ChatArea extends Component {
render() {
const { messages } = this.props;
if (typeof messages === 'undefined') return null;
const messagesDisplay = messages.map((item, itemIndex) => <p key={`message-${itemIndex}`}>{ item.content }</p>)
return <div className="chatArea">
{ messagesDisplay }
</div>
}
}

ChatArea.defaultProps = {
contents: []
};

ChatArea.propTypes = {
contents: PropTypes.array
};

function mapDispatchToProps(dispatch) {
return {
stopApplication() {
dispatch(stopApplication());
}
};
}

const mapStateToProps = createStructuredSelector({
applicationStopped: selectApplicationStopped(),
messages: selectMessages()
});
export default connect(mapStateToProps, mapDispatchToProps)(ChatArea);
25 changes: 25 additions & 0 deletions src/components/ErrorMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from "react";
import styled from 'styled-components';
import PropTypes from 'prop-types';

const StyledMessage = styled.div`
color: red;
font-size: 12px;
padding-left: 12px;
position: absolute;
bottom: 30px;
`;
const ErrorMessage = props => {
const { message } = props;
return <StyledMessage>{ message }</StyledMessage>
}

ErrorMessage.defaultProps = {
message: ' '
};

ErrorMessage.propTypes = {
message: PropTypes.string
};
export default ErrorMessage;

Loading