Skip to content

Commit 424f665

Browse files
committed
SImplify data access
1 parent 64d22bf commit 424f665

File tree

4 files changed

+20
-16
lines changed

4 files changed

+20
-16
lines changed

client/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"connected-react-router": "^5.0.1",
88
"font-awesome": "^4.7.0",
99
"jest-junit": "^5.2.0",
10+
"lodash.get": "^4.4.2",
1011
"lodash.has": "^4.5.2",
1112
"lodash.mapvalues": "^4.6.0",
1213
"prettier": "^1.14.3",

client/src/components/book/Form.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import React, { Component } from 'react';
22
import { Field, reduxForm } from 'redux-form';
3+
import PropTypes from 'prop-types';
34

45
class Form extends Component {
6+
static propTypes = {
7+
handleSubmit: PropTypes.func.isRequired,
8+
error: PropTypes.string
9+
};
10+
511
renderField = data => {
612
data.input.className = 'form-control';
713

client/src/utils/dataAccess.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ENTRYPOINT } from '../config/entrypoint';
22
import { SubmissionError } from 'redux-form';
3+
import get from 'lodash/get';
34
import has from 'lodash/has';
45
import mapValues from 'lodash/mapValues';
56

@@ -39,10 +40,6 @@ export function mercureSubscribe(url) {
3940
}
4041

4142
export function normalize(data) {
42-
// Resolve IDs to absolute URLs
43-
if (has(data, '@id'))
44-
data['@id'] = new URL(data['@id'], ENTRYPOINT).toString();
45-
4643
if (has(data, 'hydra:member')) {
4744
// Normalize items in collections
4845
data['hydra:member'] = data['hydra:member'].map(item => normalize(item));
@@ -51,16 +48,13 @@ export function normalize(data) {
5148
}
5249

5350
// Flatten nested documents
54-
mapValues(data, value => {
55-
if (Array.isArray(value))
56-
return value.map(
57-
v => (has(v, '@id') ? new URL(v['@id'], ENTRYPOINT).toString() : v)
58-
);
59-
if (has(value, '@id')) return new URL(value['@id'], ENTRYPOINT).toString();
60-
return value;
61-
});
62-
63-
return data;
51+
return mapValues(
52+
data,
53+
value =>
54+
Array.isArray(value)
55+
? value.map(v => get(v, '@id', v))
56+
: get(value, '@id', value)
57+
);
6458
}
6559

6660
export function extractHubURL(response) {
@@ -70,7 +64,6 @@ export function extractHubURL(response) {
7064
const matches = linkHeader.match(
7165
/<([^>]+)>;\s+rel=(?:mercure|"[^"]*mercure[^"]*")/
7266
);
73-
if (!matches || !matches[1]) return null;
7467

75-
return new URL(matches[1], ENTRYPOINT);
68+
return matches && matches[1] ? new URL(matches[1], ENTRYPOINT) : null;
7669
}

client/yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5014,6 +5014,10 @@ lodash.debounce@^4.0.8:
50145014
version "4.0.8"
50155015
resolved "https://registry.yarnpkg.com/lodash.debounce/-/lodash.debounce-4.0.8.tgz#82d79bff30a67c4005ffd5e2515300ad9ca4d7af"
50165016

5017+
lodash.get@^4.4.2:
5018+
version "4.4.2"
5019+
resolved "https://registry.yarnpkg.com/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99"
5020+
50175021
lodash.has@^4.5.2:
50185022
version "4.5.2"
50195023
resolved "https://registry.yarnpkg.com/lodash.has/-/lodash.has-4.5.2.tgz#d19f4dc1095058cccbe2b0cdf4ee0fe4aa37c862"

0 commit comments

Comments
 (0)