Browse Source

Initial values loaded.

feature/bike-tracking
Drew Larson 8 years ago
parent
commit
6b5be33495
  1. 3
      bikeshop_project/assets/js/bikes/actions.js
  2. 4
      bikeshop_project/assets/js/bikes/components/BikeForm/index.jsx
  3. 46
      bikeshop_project/assets/js/bikes/components/BikeModal/index.jsx
  4. 62
      bikeshop_project/assets/js/bikes/components/BikeTable/index.jsx
  5. 4
      bikeshop_project/assets/js/bikes/reducers.js
  6. 12
      bikeshop_project/assets/js/bikes/sagas.js
  7. 14
      bikeshop_project/assets/js/bikes/services/index.js

3
bikeshop_project/assets/js/bikes/actions.js

@ -5,3 +5,6 @@ export const setBikes = createAction('set bikes');
export const setBikesIsFetching = createAction('set bikes.isFetching'); export const setBikesIsFetching = createAction('set bikes.isFetching');
export const setBikesFetched = createAction('set bikes.fetched'); export const setBikesFetched = createAction('set bikes.fetched');
export const setBikesFetchFailed = createAction('set bikes.fetchFailed'); export const setBikesFetchFailed = createAction('set bikes.fetchFailed');
export const setBikeSaved = createAction('set bike.saved');
export const setBikeIsSaving = createAction('set bike.isSaving');
export const setBikeSaveFailed = createAction('set bike.isSaving');

4
bikeshop_project/assets/js/bikes/components/BikeForm/index.jsx

@ -326,7 +326,7 @@ BikeForm = reduxForm({
BikeForm = connect( BikeForm = connect(
state => ({ state => ({
initialValues: state.bike, // pull initial values from account reducer initialValues: state.bikes.entities['1'], // pull initial values from account reducer
}), }),
)(BikeForm); )(BikeForm);
@ -335,5 +335,7 @@ BikeForm.propTypes = {
handleClose: PropTypes.func, handleClose: PropTypes.func,
} }
export default BikeForm; export default BikeForm;

46
bikeshop_project/assets/js/bikes/components/BikeModal/index.jsx

@ -9,59 +9,41 @@ import BikeForm from '../BikeForm';
class BikeModal extends React.Component { class BikeModal extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = {
open: props.open,
bike: undefined,
editing: props.editing,
};
}
componentWillReceiveProps(newProps) {
this.setState({
...this.state,
open: newProps.open || false,
bike: newProps.bike || undefined,
editing: newProps.editing || false,
});
} }
handleClose() {
this.setState({ open: false });
};
render() { render() {
const title = this.state.bike && this.state.bike.stolen ? if (this.props.bike) {
(<div> const title = (
<h3>{this.props.editing ? 'Edit Bike' : 'Add Bike'}</h3> <div>
<h4>STOLEN</h4> <h3>{this.props.bike ? 'Edit Bike' : 'Add Bike'}</h3>
</div>) : </div>
<h3>Edit Bike</h3>; );
return (<div> return (<div>
<Dialog <Dialog
title={title} title={title}
open={this.state.open} open={this.props.open}
autoScrollBodyContent autoScrollBodyContent
> >
{ this.state.bike ? { this.props.bike ?
<BikeForm <BikeForm
bike={this.state.bike} enableReinitialize
editing={this.state.editing} bike={this.props.bike}
getBikes={this.props.getBikes} bikes={this.props.bikes}
handleClose={this.handleClose}
/> : /> :
<div>Unable to edit bike.</div> <div>Unable to edit bike.</div>
} }
</Dialog> </Dialog>
</div>); </div>);
} }
return null;
}
} }
BikeModal.propTypes = { BikeModal.propTypes = {
open: PropTypes.bool, open: PropTypes.bool,
bike: PropTypes.object, bike: PropTypes.object,
editing: PropTypes.bool,
}; };
export default BikeModal; export default BikeModal;

62
bikeshop_project/assets/js/bikes/components/BikeTable/index.jsx

@ -8,67 +8,55 @@ import { friendlySize } from '../Size';
import BikeModal from '../BikeModal'; import BikeModal from '../BikeModal';
import { fetchBikes, setBike } from '../../actions'; import { fetchBikes, setBike } from '../../actions';
const renderBikes = (bikes) => {
console.log(bikes);
const bikeRows = bikes.map(bike => (
<TableRow selectable={false} key={bike.id}>
<TableRowColumn>{friendlySize(bike.size)}</TableRowColumn>
<TableRowColumn>{bike.colour}</TableRowColumn>
<TableRowColumn>{bike.make}</TableRowColumn>
<TableRowColumn>{bike.serial_number}</TableRowColumn>
<TableRowColumn>{bike.state}</TableRowColumn>
<TableRowColumn>{bike.claimed_by}</TableRowColumn>
<TableRowColumn><FlatButton label="Edit" primary /></TableRowColumn>
</TableRow>
));
return bikeRows;
}
class BikeTableComponent extends React.Component { class BikeTableComponent extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = { this.state = {
bikes: [],
bikeModal: { bikeModal: {
open: false, open: false,
bike: undefined, bike: undefined,
editing: false,
}, },
}; };
this.handleEditBike = this.handleEditBike.bind(this); this.handleOpen = this.handleOpen.bind(this);
this.renderBikes = this.renderBikes.bind(this);
} }
componentDidMount() { componentDidMount() {
this.props.fetchBikes(); this.props.fetchBikes();
} }
handleEditBike(bike) { handleOpen(id) {
this.setState({ this.setState({
...this.state, ...this.state,
bikeModal: { bikeModal: {
...this.state.bikeModal,
open: true, open: true,
bike, bike: id,
editing: true,
}, },
}); });
} }
handleAddBike() { renderBikes(bikes) {
this.setState({ const bikeRows = bikes.map(bike => (
...this.state, <TableRow selectable={false} key={bike.id}>
bikeModal: { <TableRowColumn>{friendlySize(bike.size)}</TableRowColumn>
open: true, <TableRowColumn>{bike.colour}</TableRowColumn>
bike: {}, <TableRowColumn>{bike.make}</TableRowColumn>
editing: false, <TableRowColumn>{bike.serial_number}</TableRowColumn>
}, <TableRowColumn>{bike.state}</TableRowColumn>
}); <TableRowColumn>{bike.claimed_by}</TableRowColumn>
} <TableRowColumn><FlatButton label="Edit" primary onTouchTap={(e) => this.handleOpen(bike.id)} /></TableRowColumn>
</TableRow>
));
return bikeRows;
}
render() { render() {
if (this.props.bikes.fetched) { if (this.props.bikes.fetched) {
const bikeRows = renderBikes(Object.values(this.props.bikes.entities)); const bikeRows = this.renderBikes(Object.values(this.props.bikes.entities));
return ( return (
<div className="mdl-grid"> <div className="mdl-grid">
<div className="mdl-cell mdl-cell--12-col"> <div className="mdl-cell mdl-cell--12-col">
@ -98,6 +86,11 @@ class BikeTableComponent extends React.Component {
</TableBody> </TableBody>
</Table> </Table>
</div> </div>
<BikeModal
open={this.state.bikeModal.open}
bike={this.state.bikeModal.bike}
bikes={this.props.bikes.entities}
/>
</div> </div>
); );
} }
@ -114,9 +107,6 @@ const mapDispatchToProps = dispatch => ({
fetchBikes: () => { fetchBikes: () => {
dispatch(fetchBikes()); dispatch(fetchBikes());
}, },
setBike: (id) => {
dispatch(setBike(id));
},
}); });
const BikeTable = connect(mapStateToProps, mapDispatchToProps)(BikeTableComponent); const BikeTable = connect(mapStateToProps, mapDispatchToProps)(BikeTableComponent);

4
bikeshop_project/assets/js/bikes/reducers.js

@ -17,7 +17,7 @@ export default handleActions({
[setBikesFetchFailed]: (state, action) => ({ [setBikesFetchFailed]: (state, action) => ({
...state, ...state,
fetchFailed: { fetchFailed: {
message: action.payload message: action.payload,
} },
}), }),
}, { entities: {}, isFetching: false, fetched: false, fetchFailed: undefined }); }, { entities: {}, isFetching: false, fetched: false, fetchFailed: undefined });

12
bikeshop_project/assets/js/bikes/sagas.js

@ -1,5 +1,6 @@
import { call, put, takeEvery, takeLatest } from 'redux-saga/effects'; import { call, put, takeEvery, takeLatest } from 'redux-saga/effects';
import { fetchBikes as fetchBikesAction, setBikes, setBikesIsFetching, setBikesFetched, setBikesFetchFailed } from './actions'; import { fetchBikes as fetchBikesAction, setBikes, setBikesIsFetching, setBikesFetched,
setBikesFetchFailed, setBikeSaved, setBikeSaveFailed, setBikeIsSaving } from './actions';
import { normalize } from 'normalizr'; import { normalize } from 'normalizr';
import * as schema from './schema'; import * as schema from './schema';
import Api from './services'; import Api from './services';
@ -24,4 +25,13 @@ function* watchFetchBikes() {
yield takeEvery(fetchBikesAction.toString(), fetchBikes); yield takeEvery(fetchBikesAction.toString(), fetchBikes);
} }
// function *saveBike(action) {
// try {
// yield put({ type: setBikeIsSaving.toString(), payload: true });
// const bike = yield call(Api.saveBike(action.payload));
// yield put({ type: setBikes.toString(), payload: normalize([state])})
// yield put({ type: setBikeSaved, payload: true})
// }
// }
export default watchFetchBikes; export default watchFetchBikes;

14
bikeshop_project/assets/js/bikes/services/index.js

@ -21,6 +21,20 @@ const Api = {
.then(data => data) .then(data => data)
.catch((error) => { .catch((error) => {
console.log('request failed', error); console.log('request failed', error);
throw error;
});
},
saveBike(id) {
return fetch(`/api/v1/bike/${id}`, {
credentials: 'same-origin',
method: 'PUT',
})
.then(checkStatus)
.then(parseJson)
.then(data => data)
.catch((error) => {
console.log('request failed', error);
throw error;
}); });
}, },
}; };

Loading…
Cancel
Save