Skip to content

Commit 5685a16

Browse files
committed
Merge branch 'master' of github.com:log-oscon/redux-wpapi into feature/integrations
* 'master' of github.com:log-oscon/redux-wpapi: Changes the filename pattern to camelcase Correct link in README.md Apply changes suggested by the linter Defers the RequestStatus change to another PullRequest Cleans up reducer tests, covered the happy path. Enhances CACHE_HIT Adds npm badge Add links to PR and Commit references Add changelog entry for this future PR Add CHANGELOG.md README.md Contributions Section
2 parents fa2bde5 + 9f0dfc3 commit 5685a16

17 files changed

+463
-245
lines changed

.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"extends": [ "airbnb" ],
3838

3939
"rules": {
40+
"no-underscore-dangle": 0,
4041
"no-unused-vars": [2, { "vars": "all", "args": "after-used" }],
4142
"quotes": [2, "single"],
4243
"strict": [2, "never"],

CHANGELOG.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Changelog
2+
3+
The following highlights the changes that have been rolled with each new release.
4+
5+
Some guidelines in reading this document:
6+
7+
* The `[new release]` section corresponds to all the changes that have been merged into master, but have not yet been packaged and released.
8+
* Every other release as it's section hose title is the tag of the release.
9+
* The changes on each release are a list of Pull Requests (PRs) which were merged into master. For every PR we have the short summary followed by a link to the actual merged PR page. Inside the PR are the detailed changes.
10+
* Being that these are the early days of the repository, we have some code changes that were added directly and without much detail, for these we have a link to the commit instead of the PR.
11+
12+
## [new release]
13+
14+
* Implements reducer tests ([#6](https://github.com/log-oscon/redux-wpapi/pull/6))
15+
* Draft on Contributions and the introduction of this `CHANGELOG.md` file ([#5](https://github.com/log-oscon/redux-wpapi/pull/5))
16+
* Fix FAILURE handling ([#3](https://github.com/log-oscon/redux-wpapi/pull/3))
17+
18+
## 0.1.2
19+
20+
* Fix module identification name in code ([#2](https://github.com/log-oscon/redux-wpapi/pull/2))
21+
* Correct README.md ([#1](https://github.com/log-oscon/redux-wpapi/pull/1))
22+
* Fix documentation render function ([b0bb5f4](https://github.com/log-oscon/redux-wpapi/commit/b0bb5f417d6943c981346cf74b912efa67a7c9b6))
23+
24+
## 0.1.1
25+
26+
* Include current page in the merge of selectQuery ([ca2c03c](https://github.com/log-oscon/redux-wpapi/commit/ca2c03cd4e337a58ef61e9e154223ff95acbd0de))
27+
28+
## 0.1.0
29+
30+
First version of redux-wpapi, already functional, but poorly documented.
31+
32+
Includes the reducer tests for the actions:
33+
34+
* `REDUX_WP_API_REQUEST` (get, create, update and delete)
35+
* `REDUX_WP_API_SUCCESS` (get)

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,21 @@ export default connect({
6969
request: selectQuery('HomePagePosts'),
7070
}, { wp })(HomePage);
7171
```
72+
73+
## Contributions
74+
75+
All contributions are welcome, and very much appreciated.
76+
77+
We are preparing some helper [documents](https://github.com/log-oscon/redux-wpapi/issues/4) to facilitate the process (hopefully), but for now we're following the these guidelines:
78+
79+
* Be reasonable
80+
* Give as much detailed information as you can
81+
* Keep it as short as possible
82+
* Let the code talk
83+
84+
### Must have's
85+
86+
Every Pull Request must have the following:
87+
88+
* Unit tests for any functionality that's exposed to the end user.
89+
* An entry in the [CHANGELOG.md](https://github.com/log-oscon/redux-wpapi/master/CHANGELOG.md) file.

src/ReduxWPAPI.js

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import capitalize from 'lodash/capitalize';
1111
import Immutable from 'immutable';
1212
import isArray from 'lodash/isArray';
1313
import isUndefined from 'lodash/isUndefined';
14-
import namedGroupRegex from './named-group-regexp';
14+
import namedGroupRegex from './namedGroupRegexp';
1515
import { selectQuery } from './selectors';
1616

1717
import {
@@ -61,7 +61,7 @@ export default class ReduxWPAPI {
6161
};
6262
},
6363

64-
// FIXME provisional method while waiting WP-API/node-wpapi#213
64+
// FIXME provisional method while WAITING WP-API/node-wpapi#213
6565
getIndexes(request) {
6666
/* eslint-disable no-param-reassign */
6767
// Internal mutations inside reduce function
@@ -194,23 +194,23 @@ export default class ReduxWPAPI {
194194
if (meta.operation === 'get') {
195195
let cache;
196196
let lastCacheUpdate;
197-
let cachePath;
197+
let data;
198198
const state = store.getState().wp;
199199
const indexes = this.settings.getIndexes(request, this.settings);
200200
const localID = this.getEntityLocalID(state, meta.aggregator, indexes);
201201
payload.uid = this.settings.getRequestUID(request, this.settings);
202202
payload.page = parseInt(this.settings.getRequestedPage(request, this.settings) || 1, 10);
203203

204204
if (localID) {
205-
cachePath = localID;
206-
cache = state.getIn(['entities', cachePath]);
205+
cache = state.getIn(['entities', localID]);
206+
data = [localID];
207207
}
208208

209209
if (cache) {
210210
lastCacheUpdate = cache.lastCacheUpdate;
211211
} else {
212-
cachePath = ['requestsByQuery', payload.uid];
213-
cache = state.getIn([...cachePath, payload.page]);
212+
cache = state.getIn(['requestsByQuery', payload.uid, payload.page]);
213+
data = cache.get('data');
214214
}
215215

216216
if (cache && (localID || (isUndefined(localID) && !cache.get('error')))) {
@@ -222,7 +222,7 @@ export default class ReduxWPAPI {
222222
uid: payload.uid,
223223
page: payload.page,
224224
lastCacheUpdate,
225-
cachePath,
225+
data,
226226
},
227227
});
228228

@@ -263,26 +263,28 @@ export default class ReduxWPAPI {
263263
reducer = (state = initialReducerState, action) => {
264264
switch (action.type) {
265265
case REDUX_WP_API_CACHE_HIT: {
266-
const { cachePath, page, uid } = action.payload;
267-
268-
const newState = state.mergeIn(['requestsByName', action.meta.name], { page, uid });
269-
if (newState.getIn(['requestsByQuery', uid, page])) {
270-
return newState;
271-
}
266+
const { data, page, uid } = action.payload;
267+
let newState = state.mergeIn(
268+
['requestsByName', action.meta.name],
269+
{ page, uid }
270+
);
272271

273-
return (
274-
newState.mergeIn(
275-
['requestsByQuery', uid, 1],
276-
{
272+
if (!newState.getIn(['requestsByQuery', uid, page])) {
273+
newState = (
274+
newState
275+
.mergeIn(['requestsByQuery', uid, page], {
277276
status: READY,
278277
operation: action.meta.operation,
279278
error: false,
280279
requestAt: action.payload.lastCacheUpdate,
281280
responseAt: action.payload.lastCacheUpdate,
282-
data: [cachePath],
283-
}
284-
)
285-
);
281+
})
282+
.setIn(['requestsByQuery', uid, 1, 'data'], data)
283+
);
284+
}
285+
286+
287+
return newState;
286288
}
287289

288290
case REDUX_WP_API_REQUEST: {

src/constants/requestStatus.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
export const WAITING = 'WAITING';
22
export const READY = 'READY';
33
export const ERROR = 'ERROR';
4+
5+
export default {
6+
WAITING,
7+
READY,
8+
ERROR,
9+
};
File renamed without changes.

test/data/collectionResponse.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
const collectionResponse = [
2+
{
3+
id: 2,
4+
slug: 'dumb2',
5+
dumbAttr: 'dumb2',
6+
_links: {
7+
self: [{ href: 'http://dumb.url/wp-json/namespace/any/2' }],
8+
collection: [{ href: 'http://dumb.url/wp-json/namespace/any' }],
9+
parent: [{
10+
embeddable: true,
11+
href: 'http://dumb.url/wp-json/namespace/any/1',
12+
}],
13+
author: [{
14+
embeddable: true,
15+
href: 'http://dumb.url/wp-json/wp/v2/users/1',
16+
}],
17+
},
18+
_embedded: {
19+
author: [{
20+
id: 1,
21+
name: 'admin',
22+
link: 'http://km.nos.dev/author/admin/',
23+
slug: 'admin',
24+
_links: {
25+
self: [{ href: 'http://km.nos.dev/wp-json/wp/v2/users/1' }],
26+
collection: [{ href: 'http://km.nos.dev/wp-json/wp/v2/users' }],
27+
},
28+
}],
29+
parent: [{
30+
id: 1,
31+
slug: 'dumb1',
32+
dumbAttr: 'dumb1',
33+
_links: {
34+
self: [{ href: 'http://dumb.url/wp-json/namespace/any/1' }],
35+
collection: [{ href: 'http://dumb.url/wp-json/namespace/any' }],
36+
parent: [{
37+
embeddable: true,
38+
href: 'http://dumb.url/wp-json/namespace/any/1',
39+
}],
40+
author: [{
41+
embeddable: true,
42+
href: 'http://dumb.url/wp-json/wp/v2/users/2',
43+
}],
44+
},
45+
}],
46+
},
47+
},
48+
{
49+
id: 1,
50+
slug: 'dumb1',
51+
dumbAttr: 'dumb1',
52+
_links: {
53+
self: [{ href: 'http://dumb.url/wp-json/namespace/any/1' }],
54+
collection: [{ href: 'http://dumb.url/wp-json/namespace/any' }],
55+
parent: [{
56+
embeddable: true,
57+
href: 'http://dumb.url/wp-json/namespace/any/1',
58+
}],
59+
author: [{
60+
embeddable: true,
61+
href: 'http://dumb.url/wp-json/wp/v2/users/2',
62+
}],
63+
},
64+
_embedded: {
65+
author: [{
66+
id: 2,
67+
name: 'edygar',
68+
link: 'http://km.nos.dev/author/edygar/',
69+
slug: 'edygar',
70+
_links: {
71+
self: [{ href: 'http://km.nos.dev/wp-json/wp/v2/users/2' }],
72+
collection: [{ href: 'http://km.nos.dev/wp-json/wp/v2/users' }],
73+
},
74+
}],
75+
},
76+
},
77+
];
78+
79+
collectionResponse._paging = {
80+
total: 2,
81+
totalPages: 1,
82+
};
83+
84+
export default collectionResponse;

test/data/queryBySlugResponse.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const queryBySlugResponse = [
2+
{
3+
id: 1,
4+
slug: 'dumb1-modified',
5+
dumbAttr: 'dumb1 - modified',
6+
_links: {
7+
self: [{ href: 'http://dumb.url/wp-json/namespace/any/1' }],
8+
collection: [{ href: 'http://dumb.url/wp-json/namespace/any' }],
9+
parent: [{
10+
embeddable: true,
11+
href: 'http://dumb.url/wp-json/namespace/any/1',
12+
}],
13+
author: [{
14+
embeddable: true,
15+
href: 'http://dumb.url/wp-json/wp/v2/users/2',
16+
}],
17+
},
18+
_embedded: {
19+
author: [{
20+
id: 2,
21+
name: 'edygar',
22+
link: 'http://km.nos.dev/author/edygar/',
23+
slug: 'edygar',
24+
_links: {
25+
self: [{ href: 'http://km.nos.dev/wp-json/wp/v2/users/2' }],
26+
collection: [{ href: 'http://km.nos.dev/wp-json/wp/v2/users' }],
27+
},
28+
}],
29+
},
30+
},
31+
];
32+
33+
queryBySlugResponse._paging = {
34+
total: 1,
35+
totalPages: 2,
36+
};
37+
38+
export default queryBySlugResponse;
39+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { REDUX_WP_API_CACHE_HIT } from '../../src/constants/actions';
2+
3+
export default {
4+
type: REDUX_WP_API_CACHE_HIT,
5+
payload: {
6+
uid: '/namespace/any',
7+
page: 1,
8+
lastCacheUpdate: Date.now(),
9+
data: [3, 1],
10+
},
11+
meta: {
12+
name: 'test',
13+
aggregator: 'any',
14+
operation: 'get',
15+
requestAt: Date.now(),
16+
},
17+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { REDUX_WP_API_CACHE_HIT } from '../../src/constants/actions';
2+
3+
export default {
4+
type: REDUX_WP_API_CACHE_HIT,
5+
payload: {
6+
uid: '/namespace/any?slug=dumb2',
7+
page: 1,
8+
lastCacheUpdate: Date.now(),
9+
data: [3],
10+
},
11+
meta: {
12+
name: 'test',
13+
aggregator: 'any',
14+
operation: 'get',
15+
requestAt: Date.now(),
16+
},
17+
};

0 commit comments

Comments
 (0)