Skip to content

Commit 3574a93

Browse files
committed
Fix loading single resource
1 parent 1248937 commit 3574a93

File tree

16 files changed

+93
-26
lines changed

16 files changed

+93
-26
lines changed

client/src/api/client.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,14 @@ export const normalizeResponse = (response) => {
6666
}));
6767
};
6868

69-
export const normalizeErrors = (response) => {
70-
throw get(response, 'response.data.errors')
69+
export const normalizeEndpointError = (err) => {
70+
const error = get(err, 'response.data.errors[0]');
71+
// eslint-disable-next-line no-throw-literal
72+
throw { message: error.detail || error.title || err.message };
73+
};
74+
75+
export const normalizeErrors = (err) => {
76+
throw get(err, 'response.data.errors')
7177
.reduce((errors, error) => {
7278
const attribute = /\/data\/[a-z]*\/(.*)$/.exec(get(error, 'source.pointer'))[1];
7379
set(errors, attribute.split('/'), error.title);

client/src/components/Auth/Login.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import LoginForm from './LoginForm';
88
import { login } from '../../store/auth';
99

1010
export class Login extends Component {
11+
// eslint-disable-next-line class-methods-use-this
1112
componentWillMount() {
1213
localStorage.clear();
1314
}
1415

1516
onSubmit = values => this.props.login(values)
1617
.then(this.props.redirect)
17-
.catch(({ response }) => {
18-
throw new SubmissionError({ _error: response.data.errors.join() });
18+
.catch(({ message }) => {
19+
throw new SubmissionError({ _error: message });
1920
});
2021

2122
render() {

client/src/components/Categories/CategoryList.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { Component, PropTypes } from 'react';
22
import { find, keyBy } from 'lodash';
33

4+
import { Loading } from '../UI';
45
import { withResourceList } from '../../hocs';
56
import CategoryForm from './CategoryForm';
67

@@ -12,6 +13,10 @@ export class CategoryList extends Component {
1213
render() {
1314
const { resourceList, onSubmit, onDelete } = this.props;
1415

16+
if (resourceList.empty && resourceList.loading) {
17+
return (<Loading />);
18+
}
19+
1520
return (
1621
<div>
1722
{resourceList.data.map(category =>
@@ -24,7 +29,6 @@ export class CategoryList extends Component {
2429
/>
2530
</div>,
2631
)}
27-
{resourceList.empty && resourceList.loading && <p>Loading...</p>}
2832
<CategoryForm
2933
isNew={true}
3034
form="category-new"

client/src/components/Posts/PostEdit.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { push } from 'react-router-redux';
33
import { connect } from 'react-redux';
44
import { get, find, omit } from 'lodash';
55

6-
import { EditHeader } from '../UI';
6+
import { ErrorAlert, Loading, EditHeader } from '../UI';
77
import { withResource } from '../../hocs';
88
import PostForm from './PostForm';
99
import {
@@ -23,7 +23,15 @@ export class PostEdit extends Component {
2323
}
2424

2525
render() {
26-
const { isNew, resource, onSubmit, categories } = this.props;
26+
const { isNew, error, loading, resource, onSubmit, categories } = this.props;
27+
28+
if (error) {
29+
return (<ErrorAlert {...error} />);
30+
}
31+
32+
if (loading) {
33+
return (<Loading/>);
34+
}
2735

2836
return (
2937
<div>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import React from 'react';
2+
import { Alert } from 'reactstrap';
3+
4+
const ErrorAlert = ({ message }) => <Alert color="danger">{message}</Alert>;
5+
6+
export default ErrorAlert;

client/src/components/UI/ListTable.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { Component } from 'react';
22
import { Table } from 'reactstrap';
33

4-
import { Pagination } from './';
4+
import { Loading, Pagination } from './';
55

66
const columnKey = (column, postfix) => `${column.accessor || column.header}-${postfix}`;
77

@@ -20,6 +20,11 @@ export default (props) => {
2020
}
2121
};
2222

23+
24+
if (resourceList.empty && resourceList.loading) {
25+
return (<Loading />);
26+
}
27+
2328
return (
2429
<div>
2530
<Table>
@@ -51,14 +56,11 @@ export default (props) => {
5156
)}
5257
</tbody>
5358
</Table>
54-
5559
<Pagination
5660
resourceList={resourceList}
5761
onPageNumber={onPageNumber}
5862
onPageSize={onPageSize}>
5963
</Pagination>
60-
61-
{resourceList.empty && resourceList.loading && <p>Loading...</p>}
6264
</div>
6365
);
6466
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react';
2+
3+
const Loading = () => (<div>Loading...</div>);
4+
5+
export default Loading;

client/src/components/UI/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
export CardSingle from './CardSingle';
22
export EditHeader from './EditHeader';
3+
export ErrorAlert from './ErrorAlert';
34
export ListTable from './ListTable';
5+
export Loading from './Loading';
46
export Pagination from './Pagination';

client/src/components/Users/UserEdit.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { push } from 'react-router-redux';
33
import { connect } from 'react-redux';
44
import { get, find, omit } from 'lodash';
55

6-
import { EditHeader } from '../UI';
6+
import { ErrorAlert, Loading, EditHeader } from '../UI';
77
import { withResource } from '../../hocs';
88
import UserForm from './UserForm';
99

@@ -17,7 +17,15 @@ export class UserEdit extends Component {
1717
}
1818

1919
render() {
20-
const { isNew, resource, onSubmit } = this.props;
20+
const { isNew, error, loading, resource, onSubmit } = this.props;
21+
22+
if (error) {
23+
return (<ErrorAlert {...error} />);
24+
}
25+
26+
if (loading) {
27+
return (<Loading />);
28+
}
2129

2230
return (
2331
<div>

client/src/hocs/withResource.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
updateResource,
1212
deleteResource,
1313
getOne,
14+
getError,
1415
} from '../store/api';
1516

1617
const withResource = (resourceType, resourceMeta) => (WrappedComponent) => {
@@ -33,14 +34,20 @@ const withResource = (resourceType, resourceMeta) => (WrappedComponent) => {
3334

3435
const mapStateToProps = (state, props) => ({
3536
isNew: !props.params.id,
37+
loading: props.params.id && !getOne(state, resourceType, props.params.id),
38+
error: props.params.id && getError(state, resourceType),
3639
resource: getOne(state, resourceType, props.params.id),
3740
});
3841

3942
const mapDispatchToProps = dispatch => ({
40-
fetchResource: (payload, meta) => dispatch(fetchOne(resourceType, payload, { ...resourceMeta, ...meta })),
41-
createResource: (payload, meta) => dispatch(createResource(resourceType, payload, { ...resourceMeta, ...meta })),
42-
updateResource: (payload, meta) => dispatch(updateResource(resourceType, payload, { ...resourceMeta, ...meta })),
43-
deleteResource: (payload, meta) => dispatch(deleteResource(resourceType, payload, { ...resourceMeta, ...meta })),
43+
fetchResource: (payload, meta) =>
44+
dispatch(fetchOne(resourceType, payload, { ...resourceMeta, ...meta })),
45+
createResource: (payload, meta) =>
46+
dispatch(createResource(resourceType, payload, { ...resourceMeta, ...meta })),
47+
updateResource: (payload, meta) =>
48+
dispatch(updateResource(resourceType, payload, { ...resourceMeta, ...meta })),
49+
deleteResource: (payload, meta) =>
50+
dispatch(deleteResource(resourceType, payload, { ...resourceMeta, ...meta })),
4451
});
4552

4653
return connect(mapStateToProps, mapDispatchToProps)(enhance(WrappedComponent));

0 commit comments

Comments
 (0)