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

Twitterlite refactored #11

Merged
merged 6 commits into from
Sep 23, 2018
Merged
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
4,412 changes: 2,206 additions & 2,206 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions sources/twitter-lite/public/bootstrap.min.css

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions sources/twitter-lite/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<link href="bootstrap.min.css" rel="stylesheet">
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Expand Down
26 changes: 13 additions & 13 deletions sources/twitter-lite/src/App.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import React, { Component } from 'react';
import './App.css';
import TwitterLite from './TwitterLite'
import SpecialContext from './SpecialContext'

import { connect } from 'react-redux'
import Slice from './Slice';

class App extends Component {
componentDidMount() {
setInterval(() => this.props.tweet())
}

const mapState = (state) => ({ slices: Array(Object.keys(state).length).fill(0) });

class App extends Component {
render() {
return (
<div>
{this.props.tweets.map((tweet, i) => <TwitterLite tweet={tweet} />)}
<div className='row'>
{this.props.slices.map((slice, idx) => {
return (
<div className='col-lg-4' key={idx}>
<Slice idx={idx} />
</div>
)
})}
</div>
);
}
}

function tweet() {
return { type: 'tweet', tweet: 'fabulous' }
}

export default connect(tweets => ({ tweets }), { tweet })(App);
export default connect(mapState)(App);
35 changes: 35 additions & 0 deletions sources/twitter-lite/src/Slice.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React, { Component } from 'react';
import { connect } from 'react-redux';

import TwitterLite from './TwitterLite';


const mapStateToProps = (state, props) => {
return {
slice: state[props.idx]
}
}


class Slice extends Component {
state = {};

// componentDidMount = () => {
// this.props.fillPairs(this.props.idx);
// }

render() {
const { slice, idx } = this.props;
return (
<ul className='list-group'>
{slice.map((tweet) => {
return (
<TwitterLite sliceId={idx} tweet={tweet} />
)
})}
</ul>
);
}
}

export default connect(mapStateToProps)(Slice);
8 changes: 8 additions & 0 deletions sources/twitter-lite/src/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as c from './constants'

export function addTweet (id) {
return {
type: `${c.ADD_TWEET}_${id}`,
tweet: 'fabulous'
}
}
4 changes: 4 additions & 0 deletions sources/twitter-lite/src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const ADD_TWEET = 'add-tweet'

export const NUMBER_OF_SLICES = 3;
// export const NUM_ENTRIES = 3500;
40 changes: 34 additions & 6 deletions sources/twitter-lite/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,49 @@ import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import 'fps-emit';
import { createStore } from 'redux'
import { createStore, combineReducers } from 'redux'
import { Provider } from 'react-redux'
import App from './App';
import SpecialContext from './SpecialContext'

const store = createStore((state = [], action) => {
if (action.type === 'tweet') {
return [...state, action.tweet]
import { addTweet } from './actions';

import * as c from './constants';

const highOrderSliceReducer = (sliceId = '') => (state = [], action) => {
switch (action.type) {
case `${c.ADD_TWEET}_${sliceId}`: {
return [...state, action.tweet];
}
default: {
return state
}
}
return state
})
}

const reducers = Array(c.NUMBER_OF_SLICES).fill(0).reduce((acc, curr, i) => {
acc[i] = highOrderSliceReducer(i);
return acc;
}, {});


const store = createStore(combineReducers(reducers));
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);

function addTweetInRandomSlice() {
const sliceId = Math.floor(Math.random() * c.NUMBER_OF_SLICES);
store.dispatch(addTweet(sliceId));
}

setInterval(addTweetInRandomSlice, 13)

setInterval(addTweetInRandomSlice, 21)

setInterval(addTweetInRandomSlice, 34)

setInterval(addTweetInRandomSlice, 55)