-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔀 Merge pull request #18 from alexlee-dev/feature/views
✨ Ability to change views.
- Loading branch information
Showing
10 changed files
with
167 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
import { connect } from 'react-redux' | ||
import { views } from '../constants' | ||
import { setView } from '../redux/actions/ui' | ||
|
||
const ViewSelector = ({ handleSetView, view }) => { | ||
const handleChange = e => handleSetView(e.target.value) | ||
|
||
return ( | ||
<select onChange={handleChange}> | ||
<option>{view}</option> | ||
{Object.keys(views).map((viewName, i) => { | ||
if (viewName !== view) { | ||
return <option key={i}>{viewName}</option> | ||
} else { | ||
return null | ||
} | ||
})} | ||
</select> | ||
) | ||
} | ||
|
||
ViewSelector.propTypes = { | ||
handleSetView: PropTypes.func.isRequired, | ||
view: PropTypes.string.isRequired | ||
} | ||
|
||
const mapStateToProps = ({ ui }) => ({ view: ui.view }) | ||
|
||
const mapDispatchToProps = dispatch => ({ | ||
handleSetView: view => dispatch(setView(view)) | ||
}) | ||
|
||
export default connect( | ||
mapStateToProps, | ||
mapDispatchToProps | ||
)(ViewSelector) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// * ACTION TYPES | ||
const SET_VIEW = 'SET_VIEW' | ||
|
||
// * ACTION GENERATORS | ||
export const setView = view => ({ | ||
type: SET_VIEW, | ||
payload: { | ||
view | ||
} | ||
}) | ||
|
||
// * PROMISES | ||
|
||
// * THUNKS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const uiDefaultState = { | ||
view: 'Ship' | ||
} | ||
|
||
export default (state = uiDefaultState, action) => { | ||
switch (action.type) { | ||
case 'SET_VIEW': | ||
const { view } = action.payload | ||
return { ...state, view } | ||
default: | ||
return state | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
import { connect } from 'react-redux' | ||
import { views } from '../constants' | ||
|
||
const View = ({ view }) => { | ||
const CurrentView = views[view] | ||
return <CurrentView /> | ||
} | ||
|
||
View.propTypes = { | ||
dispatch: PropTypes.func.isRequired, | ||
view: PropTypes.string.isRequired | ||
} | ||
|
||
const mapStateToProps = ({ ui }) => ({ view: ui.view }) | ||
|
||
export default connect(mapStateToProps)(View) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
import { connect } from 'react-redux' | ||
|
||
const PlanetsView = ({ planets }) => { | ||
return ( | ||
<div> | ||
{planets.map(({ items, isHomePlanet, name }) => ( | ||
<div key={name}> | ||
<h2>{isHomePlanet ? name + ' - Home Planet' : name}</h2> | ||
<span>Items:</span> | ||
<pre>{JSON.stringify(items, null, 2)}</pre> | ||
</div> | ||
))} | ||
</div> | ||
) | ||
} | ||
|
||
PlanetsView.propTypes = { | ||
dispatch: PropTypes.func.isRequired, | ||
planets: PropTypes.array.isRequired | ||
} | ||
|
||
const mapStateToProps = ({ world }) => ({ planets: world.planets }) | ||
|
||
export default connect(mapStateToProps)(PlanetsView) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
import { connect } from 'react-redux' | ||
|
||
const ShipView = ({ cargo }) => { | ||
return ( | ||
<div> | ||
<h2>Your Ship</h2> | ||
<h3>Cargo</h3> | ||
<ul> | ||
{cargo.map(({ name }, i) => ( | ||
<li key={i}>{name}</li> | ||
))} | ||
</ul> | ||
</div> | ||
) | ||
} | ||
|
||
ShipView.propTypes = { | ||
cargo: PropTypes.array.isRequired, | ||
dispatch: PropTypes.func.isRequired | ||
} | ||
|
||
const mapStateToProps = ({ ship }) => ({ cargo: ship.cargo }) | ||
|
||
export default connect(mapStateToProps)(ShipView) |