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

Scalable frontend challenge #7

Open
wants to merge 4 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"react-redux": "^5.0.6",
"react-test-renderer": "^15.6.1",
"redux": "^3.7.2",
"redux-thunk": "^2.2.0",
"style-loader": "0.18.2",
"sw-precache-webpack-plugin": "0.11.3",
"url-loader": "0.5.9",
Expand Down
8 changes: 1 addition & 7 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
.block {
position: absolute;
width: 24px;
height: 24px;
background: red;
border: 1px solid #000;
}

37 changes: 23 additions & 14 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
import React, { Component } from "react";
import UserCreator from "./UserCreator";
import { connect } from "react-redux";
import RandomGif from "./RandomGif";
import RandomGifPair from "./RandomGifPair";
import RandomGifPairOfPairs from "./RandomGifPairOfPairs";
import { increment, toggleButton } from "./state/app";

class App extends Component {
render() {
const { users } = this.props;
const { count, increment, toggleButton, buttonEnabled } = this.props;

return (
<div>
<UserCreator />
<UserCreator />
<UserCreator />
<div id="userList">
{users.map(({ name }) =>
<div key={name} className="user">
{name}
</div>
)}
</div>
<h1>
{count}
</h1>
<RandomGif onLoad={increment} />
<RandomGifPair onLoad={increment} />
<RandomGifPairOfPairs onLoad={increment} />
<button style={{ background: buttonEnabled ? "green" : "red" }} onClick={toggleButton}>
Toggle
</button>
</div>
);
}
}

function mapStateToProps(state) {
return {
users: state.users
count: state.app.count,
buttonEnabled: state.app.buttonEnabled
};
}

export default connect(mapStateToProps)(App);
const mapDispatchToProps = {
increment: increment,
toggleButton: toggleButton
};

export default connect(mapStateToProps, mapDispatchToProps)(App);
15 changes: 15 additions & 0 deletions src/RandomGif/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import api from "../api";
export const LOADING = "LOADING";
export const LOADED = "LOADED";
export const SET_SRC = "SET_SRC";

const loading = () => ({ type: LOADING });
const loaded = () => ({ type: LOADED });

const setGifSrc = src => ({ type: SET_SRC, src });

export const getGif = () => (dispatch, getState) => {
dispatch(loading());

api.getRandomGifUrl().then(url => dispatch(setGifSrc(url))).then(() => dispatch(loaded()));
};
27 changes: 27 additions & 0 deletions src/RandomGif/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from "react";
import { connect } from "react-redux";
import { getGif, LOADED } from "./actions";
import reducer from "./reducer";
import isolate from "../utils/isolate";

const RandomGif = ({ loading, src, getGif }) =>
<div style={{ width: "100px" }}>
<button onClick={getGif} disabled={loading}>
{loading ? "Loading..." : "Get Gif"}
</button>
{src &&
<div>
<img alt="Gif" src={src} style={{ marginTop: "5px" }} />
</div>}
</div>;

const mapStateToProps = state => ({
loading: state.loading,
src: state.src
});

const actionCreators = { getGif };

export default isolate(reducer, {
[LOADED]: (action, props, getState) => props.onLoad()
})(connect(mapStateToProps, actionCreators)(RandomGif));
14 changes: 14 additions & 0 deletions src/RandomGif/reducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { LOADING, SET_SRC } from "./actions";

const initialState = { loading: false };

export default (state = initialState, action) => {
switch (action.type) {
case LOADING:
return { ...state, loading: true };
case SET_SRC:
return { ...state, src: action.src, loading: false };
default:
return state;
}
};
8 changes: 8 additions & 0 deletions src/RandomGifPair/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from "react";
import RandomGif from "../RandomGif";

export default ({ onLoad }) =>
<div>
<RandomGif onLoad={onLoad} />
<RandomGif onLoad={onLoad} />
</div>;
8 changes: 8 additions & 0 deletions src/RandomGifPairOfPairs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from "react";
import RandomGifPair from "../RandomGifPair";

export default ({ onLoad }) =>
<div>
<RandomGifPair onLoad={onLoad} />
<RandomGifPair onLoad={onLoad} />
</div>;
14 changes: 0 additions & 14 deletions src/UserCreator/actions.js

This file was deleted.

31 changes: 0 additions & 31 deletions src/UserCreator/index.js

This file was deleted.

14 changes: 0 additions & 14 deletions src/UserCreator/reducer.js

This file was deleted.

7 changes: 7 additions & 0 deletions src/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
getRandomGifUrl() {
return fetch(`https://api.giphy.com/v1/gifs/random?api_key=26ae311d361143eda202a3670a1b0c63`)
.then(response => response.json())
.then(json => json.data.fixed_width_small_url);
}
};
19 changes: 18 additions & 1 deletion src/state/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
export default function(state = [], action) {
const INCREMENT = "INCREMENT";
export function increment() {
return { type: INCREMENT };
}

const TOGGLE_BUTTON = "TOGGLE_BUTTON";
export function toggleButton() {
return { type: TOGGLE_BUTTON };
}

const initialState = { count: 0, buttonEnabled: false };

export default function(state = initialState, action) {
switch (action.type) {
case INCREMENT:
const amountToIncrement = state.buttonEnabled ? 2 : 1;
return { ...state, count: state.count + amountToIncrement };
case TOGGLE_BUTTON:
return { ...state, buttonEnabled: !state.buttonEnabled };
default:
return state;
}
Expand Down
17 changes: 0 additions & 17 deletions src/state/users.js

This file was deleted.

4 changes: 1 addition & 3 deletions src/store.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { combineReducers, createStore } from "redux";

import appReducer from "./state/app";
import usersReducer from "./state/users";

export default () =>
createStore(
combineReducers({
app: appReducer,
users: usersReducer
app: appReducer
}),
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);
48 changes: 48 additions & 0 deletions src/utils/isolate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React, { Component } from "react";
import { createStore, compose, applyMiddleware } from "redux";
import { Provider } from "react-redux";
import thunk from "redux-thunk";

export default function isolate(reducer, actionsToProps = {}) {
return function isolateComponent(WrappedComponent) {
return class IsolatedComponent extends Component {
constructor(props) {
super(props);

this.displayName = `Isolated(${WrappedComponent.displayName})`;

const composeEnhancers =
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
name: this.displayName
}) || compose;

const actionsToPropsMiddleware = store => next => action => {
const result = next(action);
if (actionsToProps[action.type]) {
actionsToProps[action.type](action, this.props, store.getState);
}
return result;
};

/*
* This might be worth taking is as a parameter
* even though initializing a new store per component might be a bit tedious,
* people could a function for that like I have in store.js to make the props API like
* <RandomGif store={createStore()} onLoad={increment} />
*/

this.store = createStore(
reducer,
composeEnhancers(applyMiddleware(thunk, actionsToPropsMiddleware))
);
}
render() {
return (
<Provider store={this.store}>
<WrappedComponent {...this.props} />
</Provider>
);
}
};
};
}
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5240,6 +5240,10 @@ reduce-function-call@^1.0.1:
dependencies:
balanced-match "^0.4.2"

redux-thunk@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/redux-thunk/-/redux-thunk-2.2.0.tgz#e615a16e16b47a19a515766133d1e3e99b7852e5"

redux@^3.7.2:
version "3.7.2"
resolved "https://registry.yarnpkg.com/redux/-/redux-3.7.2.tgz#06b73123215901d25d065be342eb026bc1c8537b"
Expand Down