Skip to content

Commit

Permalink
Merge 8422544 into 46c6285
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Lee committed Aug 27, 2019
2 parents 46c6285 + 8422544 commit ff54e37
Show file tree
Hide file tree
Showing 13 changed files with 356 additions and 35 deletions.
21 changes: 17 additions & 4 deletions src/__tests__/__snapshots__/App.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,24 @@ exports[`<App /> Should render the <App /> component. 1`] = `
<div
class="StyledBox-sc-13pk1d4-0 laNWCK"
>
<h3
class="StyledHeading-sc-1rdh4aw-0 ftOecI"
<div
class="StyledBox-sc-13pk1d4-0 gQLnBt"
>
Contracts
</h3>
<h3
class="StyledHeading-sc-1rdh4aw-0 ftOecI"
>
Contracts
</h3>
<div
class="StyledBox__StyledBoxGap-sc-13pk1d4-1 hyyRSo"
/>
<button
class="StyledButton-sc-323bzc-0 lnKzGb"
type="button"
>
Create
</button>
</div>
<div
class="StyledBox__StyledBoxGap-sc-13pk1d4-1 hPsznL"
/>
Expand Down
9 changes: 9 additions & 0 deletions src/__tests__/components/CreateContract.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { customRender } from '../../test-utils'
import CreateContract from '../../components/CreateContract'

describe('<CreateContract />', () => {
it('Should render the <CreateContract /> component.', () => {
const container = customRender({ component: CreateContract })
expect(container.asFragment()).toMatchSnapshot()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,24 @@ exports[`<ContractsDisplay /> Should render the <ContractsDisplay /> component.
<div
class="StyledBox-sc-13pk1d4-0 laNWCK"
>
<h3
class="StyledHeading-sc-1rdh4aw-0 ftOecI"
<div
class="StyledBox-sc-13pk1d4-0 gQLnBt"
>
Contracts
</h3>
<h3
class="StyledHeading-sc-1rdh4aw-0 ftOecI"
>
Contracts
</h3>
<div
class="StyledBox__StyledBoxGap-sc-13pk1d4-1 hyyRSo"
/>
<button
class="StyledButton-sc-323bzc-0 lnKzGb"
type="button"
>
Create
</button>
</div>
<div
class="StyledBox__StyledBoxGap-sc-13pk1d4-1 hPsznL"
/>
Expand Down
118 changes: 118 additions & 0 deletions src/__tests__/components/__snapshots__/CreateContract.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`<CreateContract /> Should render the <CreateContract /> component. 1`] = `
<DocumentFragment>
<div
class="StyledBox-sc-13pk1d4-0 ipWbUu"
>
<label
for="item-type"
>
Item Type
</label>
<select
id="item-type"
>
<option
value="{\\"name\\":\\"Ore\\",\\"value\\":2,\\"volume\\":1}"
>
Ore
</option>
<option
value="{\\"name\\":\\"Plasma\\",\\"value\\":3,\\"volume\\":0.4}"
>
Plasma
</option>
<option
value="{\\"name\\":\\"Space Junk\\",\\"value\\":1,\\"volume\\":0.1}"
>
Space Junk
</option>
<option
value="{\\"name\\":\\"Antibiotics\\",\\"value\\":2,\\"volume\\":0.1}"
>
Antibiotics
</option>
<option
value="{\\"name\\":\\"Tobacco\\",\\"value\\":3,\\"volume\\":0.2}"
>
Tobacco
</option>
<option
value="{\\"name\\":\\"Soil\\",\\"volume\\":2,\\"value\\":0.5}"
>
Soil
</option>
<option
value="{\\"name\\":\\"Ammunition\\",\\"value\\":3,\\"volume\\":0.8}"
>
Ammunition
</option>
<option
value="{\\"name\\":\\"Glass\\",\\"value\\":1,\\"volume\\":1}"
>
Glass
</option>
<option
value="{\\"name\\":\\"Silver\\",\\"value\\":4,\\"volume\\":0.5}"
>
Silver
</option>
<option
value="{\\"name\\":\\"Gold\\",\\"value\\":5,\\"volume\\":0.5}"
>
Gold
</option>
</select>
<label
for="item-volume"
>
Item Volume
</label>
<input
disabled=""
id="item-volume"
type="number"
value="1"
/>
<label
for="item-value"
>
Item Value
</label>
<input
disabled=""
id="item-value"
type="number"
value="2"
/>
<label
for="item-destination"
>
Destination
</label>
<select
id="item-destination"
>
<option
value="0"
>
Test Planet 1
</option>
<option
value="1"
>
Test Planet 2
</option>
<option
value="2"
>
Test Planet 3
</option>
</select>
<button>
Create Contract
</button>
</div>
</DocumentFragment>
`;
38 changes: 26 additions & 12 deletions src/components/ContractsDisplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,32 @@ import { Box, Heading, Text, Button } from 'grommet'
import { Checkmark, Close } from 'grommet-icons'
import { connect } from 'react-redux'
import { resetContract, setContract } from '../redux/actions/user'
import CreateContract from './CreateContract'
import { setIsCreatingContract } from '../redux/actions/ui'

/**
* Displays a set of item contracts.
*/
const ContractsDisplay = ({
contracts,
currentContract,
handleCreateInit,
handleResetContract,
handleSetContract
handleSetContract,
isCreatingContract
}) => {
return (
<Box gap="small" margin={{ vertical: 'medium' }}>
<Heading level="3">Contracts</Heading>
<Box align="center" direction="row" gap="medium">
<Heading level="3">Contracts</Heading>
<Button
hoverIndicator
label="Create"
onClick={() => handleCreateInit()}
plain
/>
</Box>
{isCreatingContract && <CreateContract />}
{currentContract && (
<Box direction="row" gap="small">
<Text>Current Contract: {currentContract.id}</Text>
Expand All @@ -42,13 +55,7 @@ const ContractsDisplay = ({
disabled={currentContract !== null}
hoverIndicator
icon={<Checkmark />}
onClick={e =>
handleSetContract(
contracts.find(
contract => contract.id === e.target.parentElement.value
)
)
}
onClick={() => handleSetContract(contract)}
plain
value={contract.id}
/>
Expand All @@ -61,20 +68,27 @@ const ContractsDisplay = ({
ContractsDisplay.propTypes = {
contracts: PropTypes.array.isRequired,
currentContract: PropTypes.object,
handleCreateInit: PropTypes.func.isRequired,
handleResetContract: PropTypes.func.isRequired,
handleSetContract: PropTypes.func.isRequired
handleSetContract: PropTypes.func.isRequired,
isCreatingContract: PropTypes.bool.isRequired
}

const mapStateToProps = ({ user, world }) => ({
const mapStateToProps = ({ ui, user, world }) => ({
contracts: world.contracts,
currentContract: user.contract
currentContract: user.contract,
isCreatingContract: ui.isCreatingContract
})

const mapDispatchToProps = dispatch => ({
handleCreateInit: () => {
dispatch(setIsCreatingContract(true))
},
handleResetContract: () => {
dispatch(resetContract())
},
handleSetContract: contract => {
console.log({ contract })
dispatch(setContract(contract))
}
})
Expand Down
87 changes: 87 additions & 0 deletions src/components/CreateContract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import React, { useState } from 'react'
import PropTypes from 'prop-types'
import { Box } from 'grommet'
import { itemList } from '../constants'
import { connect } from 'react-redux'
import { createContract } from '../redux/actions/world'
import { generateContract } from '../util'

/**
* Inputs for creating a contract.
*/
const CreateContract = ({ handleCreateContract, planets }) => {
const [itemType, setItemType] = useState(itemList[0])
const [destination, setDestination] = useState({
location: planets[0].location,
name: planets[0].name
})

return (
<Box width="medium">
<label htmlFor="item-type">Item Type</label>
<select
id="item-type"
onChange={e => setItemType(JSON.parse(e.target.value))}
>
{itemList.map(item => (
<option key={item.name} value={JSON.stringify(item)}>
{item.name}
</option>
))}
</select>
<label htmlFor="item-volume">Item Volume</label>
<input
disabled
id="item-volume"
type="number"
value={itemType ? itemType.volume : 0}
/>
<label htmlFor="item-value">Item Value</label>
<input
disabled
id="item-value"
type="number"
value={itemType ? itemType.value : 0}
/>
<label htmlFor="item-destination">Destination</label>
<select
id="item-destination"
onChange={e => {
const planet = planets.find(planet => planet.id === e.target.value)
setDestination({ location: planet.location, name: planet.name })
}}
>
{planets.map(planet => (
<option key={planet.id} value={planet.id}>
{planet.name}
</option>
))}
</select>
<button
onClick={() => {
handleCreateContract(
generateContract(planets, itemType.name, destination)
)
}}
>
Create Contract
</button>
</Box>
)
}

CreateContract.propTypes = {
handleCreateContract: PropTypes.func.isRequired,
planets: PropTypes.array.isRequired
}

const mapStateToProps = ({ world }) => ({ planets: world.planets })

const mapDispatchToProps = dispatch => ({
handleCreateContract: contract => dispatch(createContract(contract))
})

export default connect(
mapStateToProps,
mapDispatchToProps
)(CreateContract)
1 change: 1 addition & 0 deletions src/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const defaultState = {
travelDuration: null
},
ui: {
isCreatingContract: false,
view: 'Ship'
},
user: {
Expand Down
14 changes: 14 additions & 0 deletions src/redux/actions/ship.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ export const storeCargo = (item, quantity) => ({
// * PROMISES

// * THUNKS
/**
* Sets the ship to be traveling, sets the destination, and sets the ETA.
* @param {object} destination Destination object.
* @param {object} ship Ship object.
*/
export const departShip = (destination, ship) => dispatch => {
// * set isShipTraveling to true
dispatch(setShipTraveling(true))
Expand All @@ -85,6 +90,10 @@ export const departShip = (destination, ship) => dispatch => {
dispatch(setETA(eta.format('x')))
}

/**
* If the ship is carrying items to sell, will sell those items if necessary. Removes items from cargo if needed. Sets the ship location, destination, and isShipTraveling.
* @param {object} ship Ship object.
*/
export const landShip = ship => dispatch => {
const { cargo, destination } = ship
const sellableItems = cargo.items.filter(
Expand All @@ -107,6 +116,11 @@ export const landShip = ship => dispatch => {
dispatch(setShipTraveling(false))
}

/**
* Removes cash from the user, stores the cargo in the ship cargo, and removes the item from the planet.
* @param {object} item Item object.
* @param {number} quantity Quantity of the item.
*/
export const purchaseCargo = (item, quantity) => dispatch => {
// * dispatch an action to buy the items with the user's cash
dispatch(removeCash(item.price * quantity))
Expand Down
Loading

0 comments on commit ff54e37

Please sign in to comment.