Skip to content

Refactoring example for cohort 3 #3

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

Open
wants to merge 1 commit 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
33 changes: 19 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"dependencies": {
"faker": "*",
"jquery": "*",
"ramda": "^0.25.0",
"react": "*",
"react-dom": "*",
"react-redux": "*",
Expand Down
24 changes: 0 additions & 24 deletions src/App.css

This file was deleted.

42 changes: 0 additions & 42 deletions src/App.js

This file was deleted.

8 changes: 0 additions & 8 deletions src/App.test.js

This file was deleted.

37 changes: 0 additions & 37 deletions src/actions/index.js

This file was deleted.

15 changes: 15 additions & 0 deletions src/components/clickable-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';

// Notice how this clickable list component doesn't know anything about pokemons.
// All it cares about is that it's a list, and to fire the onClickItem function
// passed to it when the user clicks an li.

export const ClickableList = ({ data, onClickItem }) => (
<ul>
{data.map((item, index) => (
<li key={index} onClick={() => onClickItem(item)}>
{item.title}
</li>
))}
</ul>
);
11 changes: 0 additions & 11 deletions src/components/pokemon-view.js

This file was deleted.

39 changes: 0 additions & 39 deletions src/components/pokemon-view.test.js

This file was deleted.

4 changes: 1 addition & 3 deletions src/constants/api-url.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
const POKEAPI_BASE_URL = 'https://pokeapi.co/api/v2/';

export const GET_ALL_POKEMON_URL = POKEAPI_BASE_URL + 'pokemon';
export const POKEAPI_BASE_URL = 'https://pokeapi.co/api/v2';
36 changes: 36 additions & 0 deletions src/features/app/app.actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Remember, thunks are just functions that return another function.
// It is not super obvious that fetchPokemons is a thunk, since it looks like it's just a function
// calling another function. However, pokemonRequest actually returns a function, see why in the expansions below.
// (2 & 3 are not part of the solution)

import { pokemonRequest } from '../../utils/pokemonRequest';
import { FETCH_POKEMONS } from '../pokemon-list/pokemon-list.types';

// Solution:
export const fetchPokemons = () =>
pokemonRequest({ type: FETCH_POKEMONS, endpoint: 'pokemon' });

// Solution 2. Adds return statement to see that does have a return value.
export const fetchPokemons2 = () => {
return pokemonRequest({ type: FETCH_POKEMONS, endpoint: 'pokemon' });
};

// Solution 3. Pastes values and pokemonRequest function to see how its a thunk.
export const fetchPokemons3 = () => {
return dispatch => {
dispatch({ type: FETCH_POKEMONS.START });
return fetch('https://pokeapi.co/api/v2/pokemon')
.then(res => res.json())
.then(
data => {
dispatch({ type: FETCH_POKEMONS.SUCCESS, payload: data });
},
error => {
dispatch({ type: FETCH_POKEMONS.FAILURE });
},
)
.catch(eror => {
dispatch({ type: FETCH_POKEMONS.FAILURE });
});
};
};
11 changes: 11 additions & 0 deletions src/features/app/app.component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react';
import { PokemonCard } from '../pokemon-card/pokemon-card.component';
import { PokemonListContainer } from '../pokemon-list/pokemon-list.container';

export const App = ({ hasFavouritePokemon, favouritePokemon }) => (
<div>
<h1>Pokemon App!</h1>
{hasFavouritePokemon && <PokemonCard pokemonName={favouritePokemon} />}
<PokemonListContainer />
</div>
);
16 changes: 16 additions & 0 deletions src/features/app/app.container.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { connect } from 'react-redux';
import { App } from './app.component';
import { isNil } from 'ramda';

// Check out ramda for some awesome utily functions!
// We could have also used "double bang" (!!) to get a boolean value
// (I try to avoid "truthy" and "falsy" values)

const mapStateToProps = ({ pokemon }) => ({
hasFavouritePokemon: !isNil(pokemon.favouritePokemon),
favouritePokemon: pokemon.favouritePokemon,
});

// Notice how my "connected" or "container" components don't have any markup, this is a best practice.

export const AppContainer = connect(mapStateToProps)(App);
12 changes: 12 additions & 0 deletions src/features/pokemon-card/pokemon-card.component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react';

export const PokemonCard = ({ pokemonName = 'Abdella', height = '183 cm' }) => (
<div style={{ width: '80vw', marginLeft: 'auto' }}>
<h2>{pokemonName}</h2>
<img
alt="pokemon"
src={`https://img.pokemondb.net/artwork/${pokemonName}.jpg`}
/>
<h4>Height: {height}</h4>
</div>
);
10 changes: 10 additions & 0 deletions src/features/pokemon-list/pokemon-list.actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { UPDATE_FAVOURITE_POKEMON } from './pokemon-list.types';

export function createUpdateFavouritePokemonAction(pokemonName) {
return {
type: UPDATE_FAVOURITE_POKEMON,
payload: {
pokemonName,
},
};
}
20 changes: 20 additions & 0 deletions src/features/pokemon-list/pokemon-list.container.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ClickableList } from '../../components/clickable-list';
import { connect } from 'react-redux';
import { createUpdateFavouritePokemonAction } from './pokemon-list.actions';

const mapStateToProps = state => ({
data: state.pokemon.list.map(item => ({
title: item.name,
})),
});

const mapDispatchToProps = dispatch => ({
onClickItem: item => dispatch(createUpdateFavouritePokemonAction(item.title)),
});

// Notice how my "connected" or "container" components don't have any markup, this is a best practice.

export const PokemonListContainer = connect(
mapStateToProps,
mapDispatchToProps,
)(ClickableList);
21 changes: 21 additions & 0 deletions src/features/pokemon-list/pokemon-list.reducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { UPDATE_FAVOURITE_POKEMON, FETCH_POKEMONS } from './pokemon-list.types';

// Some features need their own reducer, like this one!

const INITIAL_STATE = {
list: [],
favouritePokemon: null,
};

export const pokemonReducer = (state = INITIAL_STATE, { type, payload }) => {
switch (type) {
case UPDATE_FAVOURITE_POKEMON:
return { ...state, ...{ favouritePokemon: payload.pokemonName } };

case FETCH_POKEMONS.SUCCESS:
return { ...state, ...{ list: payload.results } };

default:
return state;
}
};
Loading