mirror of
https://github.com/fspc/workstand.git
synced 2025-02-23 17:23:24 -05:00
Initial values loaded.
This commit is contained in:
parent
5151d089a1
commit
6b5be33495
@ -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');
|
@ -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;
|
||||||
|
|
||||||
|
@ -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;
|
||||||
|
@ -8,8 +8,37 @@ 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) => {
|
class BikeTableComponent extends React.Component {
|
||||||
console.log(bikes);
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
|
||||||
|
this.state = {
|
||||||
|
bikeModal: {
|
||||||
|
open: false,
|
||||||
|
bike: undefined,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
this.handleOpen = this.handleOpen.bind(this);
|
||||||
|
this.renderBikes = this.renderBikes.bind(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
componentDidMount() {
|
||||||
|
this.props.fetchBikes();
|
||||||
|
}
|
||||||
|
|
||||||
|
handleOpen(id) {
|
||||||
|
this.setState({
|
||||||
|
...this.state,
|
||||||
|
bikeModal: {
|
||||||
|
...this.state.bikeModal,
|
||||||
|
open: true,
|
||||||
|
bike: id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
renderBikes(bikes) {
|
||||||
const bikeRows = bikes.map(bike => (
|
const bikeRows = bikes.map(bike => (
|
||||||
<TableRow selectable={false} key={bike.id}>
|
<TableRow selectable={false} key={bike.id}>
|
||||||
<TableRowColumn>{friendlySize(bike.size)}</TableRowColumn>
|
<TableRowColumn>{friendlySize(bike.size)}</TableRowColumn>
|
||||||
@ -18,57 +47,16 @@ const renderBikes = (bikes) => {
|
|||||||
<TableRowColumn>{bike.serial_number}</TableRowColumn>
|
<TableRowColumn>{bike.serial_number}</TableRowColumn>
|
||||||
<TableRowColumn>{bike.state}</TableRowColumn>
|
<TableRowColumn>{bike.state}</TableRowColumn>
|
||||||
<TableRowColumn>{bike.claimed_by}</TableRowColumn>
|
<TableRowColumn>{bike.claimed_by}</TableRowColumn>
|
||||||
<TableRowColumn><FlatButton label="Edit" primary /></TableRowColumn>
|
<TableRowColumn><FlatButton label="Edit" primary onTouchTap={(e) => this.handleOpen(bike.id)} /></TableRowColumn>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
));
|
));
|
||||||
|
|
||||||
return bikeRows;
|
return bikeRows;
|
||||||
}
|
}
|
||||||
|
|
||||||
class BikeTableComponent extends React.Component {
|
|
||||||
constructor(props) {
|
|
||||||
super(props);
|
|
||||||
|
|
||||||
this.state = {
|
|
||||||
bikes: [],
|
|
||||||
bikeModal: {
|
|
||||||
open: false,
|
|
||||||
bike: undefined,
|
|
||||||
editing: false,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
this.handleEditBike = this.handleEditBike.bind(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
componentDidMount() {
|
|
||||||
this.props.fetchBikes();
|
|
||||||
}
|
|
||||||
|
|
||||||
handleEditBike(bike) {
|
|
||||||
this.setState({
|
|
||||||
...this.state,
|
|
||||||
bikeModal: {
|
|
||||||
open: true,
|
|
||||||
bike,
|
|
||||||
editing: true,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
handleAddBike() {
|
|
||||||
this.setState({
|
|
||||||
...this.state,
|
|
||||||
bikeModal: {
|
|
||||||
open: true,
|
|
||||||
bike: {},
|
|
||||||
editing: false,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
||||||
|
@ -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 });
|
||||||
|
@ -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;
|
||||||
|
@ -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…
x
Reference in New Issue
Block a user