Skip to content

Commit

Permalink
Merge pull request #117 from Patreon/nion-upgrades
Browse files Browse the repository at this point in the history
adds `PUT` action and adds `appendKey` meta option
  • Loading branch information
jbyttow committed Jun 30, 2020
2 parents b4c32ff + 9fdcfb6 commit e3a2b54
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@nion/nion",
"version": "3.0.3",
"version": "3.0.4",
"license": "MIT",
"browser": "es/index.js",
"main": "lib/index.js",
Expand Down
7 changes: 6 additions & 1 deletion src/actions/index.js
Expand Up @@ -126,6 +126,10 @@ const postAction = (dataKey, options) => {
return apiAction('POST', dataKey, options)
}

const putAction = (dataKey, options) => {
return apiAction('PUT', dataKey, options)
}

const patchAction = (dataKey, options) => {
return apiAction('PATCH', dataKey, options)
}
Expand Down Expand Up @@ -166,15 +170,16 @@ const updateEntityAction = ({ type, id }, attributes) => {
}

export const get = getAction

export const post = postAction
export const put = putAction
export const patch = patchAction
export { deleteAction as delete }
export const bootstrap = bootstrapAction
export const updateEntity = updateEntityAction

export default {
get,
put,
post,
patch,
delete: deleteAction,
Expand Down
14 changes: 14 additions & 0 deletions src/decorator/index.js
Expand Up @@ -152,6 +152,7 @@ const processDeclarations = (inputDeclarations, ...rest) => {
body,
meta: {
append: get(actionOptions, 'append'),
appendKey: get(actionOptions, 'appendKey'),
},
})(dispatch)
}
Expand All @@ -172,6 +173,19 @@ const processDeclarations = (inputDeclarations, ...rest) => {
endpoint,
meta: {
append: get(actionOptions, 'append'),
appendKey: get(actionOptions, 'appendKey'),
},
})(dispatch)
}

dispatchProps[key]['PUT'] = (params, actionOptions = {}) => {
const endpoint = getUrl(declaration, params)
return nionActions.put(dataKey, {
declaration,
endpoint,
meta: {
append: get(actionOptions, 'append'),
appendKey: get(actionOptions, 'appendKey'),
},
})(dispatch)
}
Expand Down
21 changes: 21 additions & 0 deletions src/hook/useNion.js
Expand Up @@ -43,6 +43,7 @@ function useNion(declaration, deps = []) {
endpoint,
meta: {
append: get(actionOptions, 'append'),
appendKey: get(actionOptions, 'appendKey'),
},
})(dispatch)
},
Expand All @@ -59,6 +60,24 @@ function useNion(declaration, deps = []) {
body,
meta: {
append: get(actionOptions, 'append'),
appendKey: get(actionOptions, 'appendKey'),
},
})(dispatch)
},
[coercedDeclaration, dispatch],
)

const putResource = useCallback(
(body = {}, params, actionOptions) => {
const endpoint = getUrl(coercedDeclaration, params)

return nionActions.put(coercedDeclaration.dataKey, {
endpoint,
declaration: coercedDeclaration,
body,
meta: {
append: get(actionOptions, 'append'),
appendKey: get(actionOptions, 'appendKey'),
},
})(dispatch)
},
Expand Down Expand Up @@ -135,6 +154,7 @@ function useNion(declaration, deps = []) {
const actions = useMemo(
() => ({
get: getResources,
put: putResource,
post: postResource,
patch: patchResource,
delete: deleteResource,
Expand All @@ -143,6 +163,7 @@ function useNion(declaration, deps = []) {
}),
[
getResources,
putResource,
postResource,
patchResource,
deleteResource,
Expand Down
25 changes: 22 additions & 3 deletions src/reducers/references.js
Expand Up @@ -38,9 +38,28 @@ const refsReducer = (state = initialState, action) => {
return state
case NION_API_BOOTSTRAP:
case NION_API_SUCCESS:
// If the result of a paginated nextPage request, we're going to want to append the
// retrieved entities to the end of the current entities list
if (action.meta.isNextPage || action.meta.append) {
if (
action.meta.appendKey &&
action.payload.requestType !== 'jsonApi'
) {
const appendKey = action.meta.appendKey
const previousEntities = get(
state[action.meta.dataKey],
appendKey,
)
return state.merge(
{
[action.meta.dataKey]: {
[appendKey]: previousEntities.concat(
action.payload.responseData.entryRef,
),
},
},
{ deep: true },
)
} else if (action.meta.isNextPage || action.meta.append) {
// If the result of a paginated nextPage request, we're going to want to append the
// retrieved entities to the end of the current entities list
const nextPageRef = action.payload.responseData.entryRef
const oldEntities = get(
state[action.meta.dataKey],
Expand Down
26 changes: 25 additions & 1 deletion src/reducers/references.test.js
Expand Up @@ -102,6 +102,29 @@ describe('nion: reducers', () => {
expect(isCollection).toEqual(true)
})

it('handles a NION_API_SUCCESS with meta.appendKey', () => {
const reducer = new Reducer()
const dataKey = 'messageKey'
const appendKey = 'messages'

const action = makeAction(types.NION_API_SUCCESS, dataKey, {
entryRef: {
messages: [{ type: 'message', id: 123 }],
},
})
reducer.applyAction(action)

const nextAction = makeAction(types.NION_API_SUCCESS, dataKey, {
entryRef: [{ type: 'message', id: 456 }],
appendKey: appendKey,
})
reducer.applyAction(nextAction)

const newMessage = get(reducer.state[dataKey][appendKey], 1)
expect(newMessage.type).toEqual('message')
expect(newMessage.id).toEqual(456)
})

it('adds links', () => {
const reducer = new Reducer()
const dataKey = 'users'
Expand Down Expand Up @@ -260,7 +283,7 @@ describe('nion: reducers', () => {
function makeAction(
actionType,
dataKey,
{ entryRef, ref, isNextPage, append, refToDelete },
{ entryRef, ref, isNextPage, append, appendKey, refToDelete },
) {
return {
type: actionType,
Expand All @@ -275,6 +298,7 @@ function makeAction(
dataKey,
isNextPage,
append,
appendKey,
refToDelete,
},
}
Expand Down
7 changes: 6 additions & 1 deletion types/index.d.ts
Expand Up @@ -27,8 +27,13 @@ export interface NionRef {
type: string
}

export interface GetActionOptions {
append?: boolean
appendKey?: string
}

export interface Actions<T> {
get(params?: any, actionOptions?: { append?: boolean }): Promise<T>
get(params?: any, actionOptions?: GetActionOptions): Promise<T>
delete(params?: any): Promise<T>
patch(body?: any, params?: any): Promise<T>
post(body?: any, params?: any): Promise<T>
Expand Down

0 comments on commit e3a2b54

Please sign in to comment.