Skip to content

Commit 2ba0aea

Browse files
Taran Vohramarkerikson
Taran Vohra
authored andcommitted
Twitterlite refactored (#11)
* setup * stockticker refactored * randomized slice and pair to update" * twitterlite refactored
1 parent 8a20689 commit 2ba0aea

File tree

8 files changed

+2307
-2225
lines changed

8 files changed

+2307
-2225
lines changed

package-lock.json

+2,206-2,206
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sources/twitter-lite/public/bootstrap.min.css

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sources/twitter-lite/public/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
-->
1111
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
1212
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
13+
<link href="bootstrap.min.css" rel="stylesheet">
1314
<!--
1415
Notice the use of %PUBLIC_URL% in the tags above.
1516
It will be replaced with the URL of the `public` folder during the build.

sources/twitter-lite/src/App.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
import React, { Component } from 'react';
22
import './App.css';
3-
import TwitterLite from './TwitterLite'
4-
import SpecialContext from './SpecialContext'
5-
63
import { connect } from 'react-redux'
4+
import Slice from './Slice';
75

8-
class App extends Component {
9-
componentDidMount() {
10-
setInterval(() => this.props.tweet())
11-
}
126

7+
const mapState = (state) => ({ slices: Array(Object.keys(state).length).fill(0) });
8+
9+
class App extends Component {
1310
render() {
1411
return (
15-
<div>
16-
{this.props.tweets.map((tweet, i) => <TwitterLite tweet={tweet} />)}
12+
<div className='row'>
13+
{this.props.slices.map((slice, idx) => {
14+
return (
15+
<div className='col-lg-4' key={idx}>
16+
<Slice idx={idx} />
17+
</div>
18+
)
19+
})}
1720
</div>
1821
);
1922
}
2023
}
2124

22-
function tweet() {
23-
return { type: 'tweet', tweet: 'fabulous' }
24-
}
2525

26-
export default connect(tweets => ({ tweets }), { tweet })(App);
26+
export default connect(mapState)(App);

sources/twitter-lite/src/Slice.jsx

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import React, { Component } from 'react';
2+
import { connect } from 'react-redux';
3+
4+
import TwitterLite from './TwitterLite';
5+
6+
7+
const mapStateToProps = (state, props) => {
8+
return {
9+
slice: state[props.idx]
10+
}
11+
}
12+
13+
14+
class Slice extends Component {
15+
state = {};
16+
17+
// componentDidMount = () => {
18+
// this.props.fillPairs(this.props.idx);
19+
// }
20+
21+
render() {
22+
const { slice, idx } = this.props;
23+
return (
24+
<ul className='list-group'>
25+
{slice.map((tweet) => {
26+
return (
27+
<TwitterLite sliceId={idx} tweet={tweet} />
28+
)
29+
})}
30+
</ul>
31+
);
32+
}
33+
}
34+
35+
export default connect(mapStateToProps)(Slice);

sources/twitter-lite/src/actions.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import * as c from './constants'
2+
3+
export function addTweet (id) {
4+
return {
5+
type: `${c.ADD_TWEET}_${id}`,
6+
tweet: 'fabulous'
7+
}
8+
}

sources/twitter-lite/src/constants.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const ADD_TWEET = 'add-tweet'
2+
3+
export const NUMBER_OF_SLICES = 3;
4+
// export const NUM_ENTRIES = 3500;

sources/twitter-lite/src/index.js

+34-6
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,49 @@ import React from 'react';
22
import ReactDOM from 'react-dom';
33
import './index.css';
44
import 'fps-emit';
5-
import { createStore } from 'redux'
5+
import { createStore, combineReducers } from 'redux'
66
import { Provider } from 'react-redux'
77
import App from './App';
88
import SpecialContext from './SpecialContext'
99

10-
const store = createStore((state = [], action) => {
11-
if (action.type === 'tweet') {
12-
return [...state, action.tweet]
10+
import { addTweet } from './actions';
11+
12+
import * as c from './constants';
13+
14+
const highOrderSliceReducer = (sliceId = '') => (state = [], action) => {
15+
switch (action.type) {
16+
case `${c.ADD_TWEET}_${sliceId}`: {
17+
return [...state, action.tweet];
18+
}
19+
default: {
20+
return state
21+
}
1322
}
14-
return state
15-
})
23+
}
24+
25+
const reducers = Array(c.NUMBER_OF_SLICES).fill(0).reduce((acc, curr, i) => {
26+
acc[i] = highOrderSliceReducer(i);
27+
return acc;
28+
}, {});
1629

30+
31+
const store = createStore(combineReducers(reducers));
1732
ReactDOM.render(
1833
<Provider store={store}>
1934
<App />
2035
</Provider>,
2136
document.getElementById('root')
2237
);
38+
39+
function addTweetInRandomSlice() {
40+
const sliceId = Math.floor(Math.random() * c.NUMBER_OF_SLICES);
41+
store.dispatch(addTweet(sliceId));
42+
}
43+
44+
setInterval(addTweetInRandomSlice, 13)
45+
46+
setInterval(addTweetInRandomSlice, 21)
47+
48+
setInterval(addTweetInRandomSlice, 34)
49+
50+
setInterval(addTweetInRandomSlice, 55)

0 commit comments

Comments
 (0)