Skip to content

Commit efbe749

Browse files
committed
Moved AddTweet.jsx to /containers. Implement basic functionality for AddTweet
1 parent 5eb6e48 commit efbe749

File tree

6 files changed

+71
-7
lines changed

6 files changed

+71
-7
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.vscode
2-
node_modules
2+
node_modules
3+
.env

ReactMiniProjects/tweeter/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ The DB of choice for this project will be PostgreSQL. I wanted to learn some SQL
2121
#### Nice to haves
2222

2323
Storybook - UI Component Library
24+
2425
Sort by trending - have some sort of trending algorithm
26+
2527
Users, authentication, restricting number of upvotes to a single time
28+
2629
Editing Tweets by author

ReactMiniProjects/tweeter/client/src/App.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import React, { Component } from 'react';
22
import './App.css';
3+
import AddTweet from './containers/AddTweet';
34
import SortableTweetList from './containers/SortableTweetList';
45

56
class App extends Component {
67
render() {
78
return (
89
<div className="App">
10+
<AddTweet />
911
<SortableTweetList />
1012
</div>
1113
);

ReactMiniProjects/tweeter/client/src/components/AddTweet.jsx

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import { connect } from 'react-redux';
4+
5+
import { addTweet } from '../actions/actions';
6+
7+
const mapDispatchToProps = dispatch => ({
8+
onSubmit: tweet => dispatch(addTweet(tweet))
9+
});
10+
11+
class AddTweet extends React.Component {
12+
state = {
13+
text: ''
14+
};
15+
16+
inputRef = React.createRef();
17+
18+
onInputChange = () => {
19+
const { value } = this.inputRef.current;
20+
this.setState({
21+
text: value
22+
});
23+
};
24+
25+
onFormSubmit = e => {
26+
e.preventDefault();
27+
28+
if (!this.inputRef.current.value.trim()) return;
29+
30+
this.props.onSubmit({
31+
text: this.state.text,
32+
createdAt: new Date(),
33+
likes: 0,
34+
id: Math.floor(Math.random() * 1000)
35+
});
36+
37+
this.inputRef.current.value = '';
38+
this.setState({
39+
text: ''
40+
});
41+
};
42+
43+
render() {
44+
return (
45+
<div>
46+
<form onSubmit={this.onFormSubmit}>
47+
<input type="text" ref={this.inputRef} onChange={this.onInputChange} />
48+
<button type="submit" disabled={this.state.text.length <= 0}>
49+
Submit
50+
</button>
51+
</form>
52+
</div>
53+
);
54+
}
55+
}
56+
57+
AddTweet.propTypes = {
58+
onSubmit: PropTypes.func.isRequired
59+
};
60+
61+
export default connect(
62+
undefined,
63+
mapDispatchToProps
64+
)(AddTweet);

ReactMiniProjects/tweeter/server/.env

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)