Skip to content

Commit

Permalink
complete supersquad
Browse files Browse the repository at this point in the history
  • Loading branch information
David Katz authored and David Katz committed Jun 17, 2017
1 parent 805fa5e commit 1e59001
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 7 deletions.
2 changes: 2 additions & 0 deletions supersquad/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<title>React App</title>
</head>
<body>
Expand Down
9 changes: 9 additions & 0 deletions supersquad/src/actions/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
export const ADD_CHARACTER = 'ADD_CHARACTER';
export const REMOVE_CHARACTER = 'REMOVE_CHARACTER';

export function addCharacterById(id) {
const action = {
type: ADD_CHARACTER,
id
}
return action;
}

export function removeCharacterById(id) {
const action = {
type: REMOVE_CHARACTER,
id
}
return action;
}
15 changes: 13 additions & 2 deletions supersquad/src/components/App.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import React, { Component } from 'react';
import CharacterList from './CharacterList';
import HeroList from './HeroList';
import SquadStats from './SquadStats';
import '../styles/index.css';

class App extends Component {
render() {
return (
<div>
<div className="App">
<h2>SuperSquad</h2>
<CharacterList />
<div className="col-md-4">
<CharacterList />
</div>
<div className="col-md-4">
<HeroList />
</div>
<div className="col-md-4">
<SquadStats />
</div>
</div>
)
}
Expand Down
7 changes: 4 additions & 3 deletions supersquad/src/components/CharacterList.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ class CharacterList extends Component {
return (
<div>
<h4>Characters</h4>
<ul>
<ul className="list-group">
{
this.props.characters.map(character => {
return (
<li key={character.id}>
<div>{character.name}</div>
<li key={character.id} className="list-group-item">
<div className="list-item">{character.name}</div>
<div
className="list-item right-button"
onClick={() => this.props.addCharacterById(character.id)}
>
+
Expand Down
40 changes: 40 additions & 0 deletions supersquad/src/components/HeroList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { removeCharacterById } from '../actions';

class HeroList extends Component {
render() {
return (
<div>
<h4>Your Hero Squad</h4>
<ul className="list-group">
{
this.props.heroes.map(hero => {
return (
<li key={hero.id} className="list-group-item">
<div className="list-item">
{hero.name}
</div>
<div
className="list-item right-button"
onClick={() => this.props.removeCharacterById(hero.id)}
>
x
</div>
</li>
)
})
}
</ul>
</div>
)
}
}

function mapStateToProps(state) {
return {
heroes: state.heroes
}
}

export default connect(mapStateToProps, { removeCharacterById })(HeroList);
49 changes: 49 additions & 0 deletions supersquad/src/components/SquadStats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React, { Component } from 'react';
import { connect } from 'react-redux';

class SquadStats extends Component {
strength() {
let strength = 0;
this.props.heroes.forEach(hero => strength += hero.strength);
return strength;
}

intelligence() {
let intelligence = 0;
this.props.heroes.forEach(hero => intelligence += hero.intelligence);
return intelligence;
}

speed() {
let speed = 0;
this.props.heroes.forEach(hero => speed += hero.speed);
return speed;
}

render() {
return (
<div>
<h4>Squad Stats</h4>
<ul className="list-group">
<li className="list-group-item">
<b>Overall Strength:</b> {this.strength()}
</li>
<li className="list-group-item">
<b>Overall Intelligence:</b> {this.intelligence()}
</li>
<li className="list-group-item">
<b>Overall Speed:</b> {this.speed()}
</li>
</ul>
</div>
)
}
}

function mapStateToProps(state) {
return {
heroes: state.heroes
}
}

export default connect(mapStateToProps, null)(SquadStats);
7 changes: 6 additions & 1 deletion supersquad/src/reducers/characters_reducer.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import characters_json from '../data/characters.json';
import { ADD_CHARACTER } from '../actions';
import { ADD_CHARACTER, REMOVE_CHARACTER } from '../actions';
import { createCharacter } from './helpers';


function characters(state = characters_json, action) {
switch(action.type) {
case ADD_CHARACTER:
let characters = state.filter(item => item.id !== action.id);
return characters;
case REMOVE_CHARACTER:
characters = [...state, createCharacter(action.id)];
return characters;
default:
return state;
}
Expand Down
5 changes: 4 additions & 1 deletion supersquad/src/reducers/heroes_reducer.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { ADD_CHARACTER } from '../actions';
import { ADD_CHARACTER, REMOVE_CHARACTER } from '../actions';
import { createCharacter } from './helpers';

function heroes(state = [], action) {
switch(action.type) {
case ADD_CHARACTER:
let heroes = [...state, createCharacter(action.id)];
return heroes;
case REMOVE_CHARACTER:
heroes = state.filter(item => item.id !== action.id);
return heroes;
default:
return state;
}
Expand Down
21 changes: 21 additions & 0 deletions supersquad/src/styles/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
body {
background-image: url('../assets/background.jpg');
height: 100%;
background-size: cover;
background-repeat: none;
opacity: 0.8;
}

.App {
text-align: center;
padding: 5%;
}

.right-button {
cursor: pointer;
float: right;
}

.list-item {
display: inline-block;
}

0 comments on commit 1e59001

Please sign in to comment.