Skip to content

Commit

Permalink
🔀 Merge pull request #21 from alexlee-dev/feature/remove-cargo
Browse files Browse the repository at this point in the history
✨ Add ability to remove an item from the ship's cargo hold.
  • Loading branch information
Alex Lee committed Aug 21, 2019
2 parents 0f649b6 + bc0b89b commit e0da7d0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
8 changes: 8 additions & 0 deletions src/redux/actions/ship.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// * ACTION TYPES
const STORE_CARGO = 'STORE_CARGO'
const SET_SHIP = 'SET_SHIP'
const REMOVE_CARGO = 'REMOVE_CARGO'

// * ACTION GENERATORS
export const storeCargo = item => ({
Expand All @@ -12,6 +13,13 @@ export const storeCargo = item => ({

export const setShip = ship => ({ type: SET_SHIP, payload: { ship } })

export const removeCargo = item => ({
type: REMOVE_CARGO,
payload: {
item
}
})

// * PROMISES

// * THUNKS
19 changes: 14 additions & 5 deletions src/redux/reducers/ship.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ const shipDefaultState = {
export default (state = shipDefaultState, action) => {
switch (action.type) {
case 'STORE_CARGO':
const { item } = action.payload

return Object.assign({}, state, { cargo: [...state.cargo, item] })
return Object.assign({}, state, {
cargo: [...state.cargo, action.payload.item]
})
case 'SET_SHIP':
const { ship } = action.payload
return ship
return action.payload.ship
case 'REMOVE_CARGO':
const itemIndex = state.cargo.findIndex(
item => item.id === action.payload.item.id
)

const newCargo = Array.from(state.cargo)

newCargo.splice(itemIndex, 1)

return Object.assign({}, state, { cargo: newCargo })
default:
return state
}
Expand Down
32 changes: 24 additions & 8 deletions src/views/ship.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import { Box, Text, Button } from 'grommet'
import { Subtract } from 'grommet-icons'
import { removeCargo } from '../redux/actions/ship'

const ShipView = ({ cargo }) => {
const ShipView = ({ cargo, handleRemoveCargo }) => {
return (
<div>
<h2>Your Ship</h2>
<h3>Cargo</h3>
<ul>
{cargo.map(({ name }, i) => (
<li key={i}>{name}</li>
))}
</ul>
{cargo.map(item => (
<Box direction="row" gap="medium" key={item.id}>
<Text>{item.name}</Text>
<Button
hoverIndicator
icon={<Subtract />}
onClick={() => handleRemoveCargo(item)}
plain
/>
</Box>
))}
</div>
)
}

ShipView.propTypes = {
cargo: PropTypes.array.isRequired,
dispatch: PropTypes.func.isRequired
handleRemoveCargo: PropTypes.func.isRequired
}

const mapStateToProps = ({ ship }) => ({ cargo: ship.cargo })

export default connect(mapStateToProps)(ShipView)
const mapDispatchToProps = dispatch => ({
handleRemoveCargo: item => dispatch(removeCargo(item))
})

export default connect(
mapStateToProps,
mapDispatchToProps
)(ShipView)

0 comments on commit e0da7d0

Please sign in to comment.