From 2b9caf8daf3d1754332793c69ffd63067af80d7d Mon Sep 17 00:00:00 2001 From: David Jay Date: Mon, 21 Nov 2016 18:00:50 -0500 Subject: [PATCH] Updating frontend to utilize new action delete endpoint. --- client/coral-framework/actions/items.js | 14 ++++++-------- client/coral-plugin-flags/FlagButton.js | 7 ++++--- client/coral-plugin-likes/LikeButton.js | 7 ++++--- init.js | 2 +- routes/api/index.js | 1 + .../coral-framework/store/itemActions.spec.js | 2 +- 6 files changed, 17 insertions(+), 16 deletions(-) diff --git a/client/coral-framework/actions/items.js b/client/coral-framework/actions/items.js index 943449d1f..419934c16 100644 --- a/client/coral-framework/actions/items.js +++ b/client/coral-framework/actions/items.js @@ -15,7 +15,7 @@ const getInit = (method, body) => { }; const init = {method, headers}; - if (method.toLowerCase() !== 'get') { + if (body) { init.body = JSON.stringify(body); } @@ -23,6 +23,9 @@ const getInit = (method, body) => { }; const responseHandler = response => { + if (response.status === 204) { + return; + } return response.ok ? response.json() : Promise.reject(`${response.status} ${response.statusText}`); }; /** @@ -241,14 +244,9 @@ export function postAction (item_id, action_type, user_id, item_type) { * */ -export function deleteAction (item_id, action_type, user_id, item_type) { +export function deleteAction (action_id) { return () => { - const action = { - action_type, - user_id - }; - - return fetch(`/api/v1/${item_type}/${item_id}/actions`, getInit('DELETE', action)) + return fetch(`/api/v1/actions/${action_id}`, {method: 'DELETE'}) .then(responseHandler); }; } diff --git a/client/coral-plugin-flags/FlagButton.js b/client/coral-plugin-flags/FlagButton.js index a4869c486..43e9891b7 100644 --- a/client/coral-plugin-flags/FlagButton.js +++ b/client/coral-plugin-flags/FlagButton.js @@ -10,12 +10,13 @@ const FlagButton = ({flag, id, postAction, deleteAction, addItem, updateItem, ad if (!flagged) { postAction(id, 'flag', '123', 'comments') .then((action) => { - addItem({...action, current_user:true}, 'actions'); - updateItem(action.item_id, action.action_type, action.id, 'comments'); + let id = `${action.action_type}_${action.item_id}`; + addItem({id, current_user: action, count: flag ? flag.count + 1 : 1}, 'actions'); + updateItem(action.item_id, action.action_type, id, 'comments'); }); addNotification('success', lang.t('flag-notif')); } else { - deleteAction(id, 'flag', '123', 'comments') + deleteAction(flagged.id) .then(() => { updateItem(id, 'flag', '', 'comments'); }); diff --git a/client/coral-plugin-likes/LikeButton.js b/client/coral-plugin-likes/LikeButton.js index 9bdf7b84d..1f3395a40 100644 --- a/client/coral-plugin-likes/LikeButton.js +++ b/client/coral-plugin-likes/LikeButton.js @@ -10,11 +10,12 @@ const LikeButton = ({like, id, postAction, deleteAction, addItem, updateItem}) = if (!liked) { postAction(id, 'like', '123', 'comments') .then((action) => { - addItem({id: action.id, current_user:true, count: like ? like.count + 1 : 1}, 'actions'); - updateItem(action.item_id, action.action_type, action.id, 'comments'); + let id = `${action.action_type}_${action.item_id}`; + addItem({id, current_user: action, count: like ? like.count + 1 : 1}, 'actions'); + updateItem(action.item_id, action.action_type, id, 'comments'); }); } else { - deleteAction(id, 'like', '123', 'comments') + deleteAction(liked.id) .then(() => { updateItem(like.id, 'count', like.count - 1, 'actions'); updateItem(like.id, 'current_user', false, 'actions'); diff --git a/init.js b/init.js index 619157bb2..768d02999 100644 --- a/init.js +++ b/init.js @@ -1,5 +1,5 @@ const Setting = require('./models/setting'); -const wordlist = require('../services/wordlist'); +const wordlist = require('./services/wordlist'); module.exports = () => Promise.all([ diff --git a/routes/api/index.js b/routes/api/index.js index c49e52c9d..7192a1324 100644 --- a/routes/api/index.js +++ b/routes/api/index.js @@ -9,5 +9,6 @@ router.use('/queue', require('./queue')); router.use('/settings', require('./settings')); router.use('/stream', require('./stream')); router.use('/user', require('./user')); +router.use('/actions', require('./actions')); module.exports = router; diff --git a/tests/client/coral-framework/store/itemActions.spec.js b/tests/client/coral-framework/store/itemActions.spec.js index 46650c592..0eb4165d3 100644 --- a/tests/client/coral-framework/store/itemActions.spec.js +++ b/tests/client/coral-framework/store/itemActions.spec.js @@ -173,7 +173,7 @@ describe('itemActions', () => { fetchMock.delete('*', {}); return actions.deleteAction('abc', 'flag', '123', 'comments')(store.dispatch) .then(response => { - expect(fetchMock.calls().matched[0][0]).to.equal('/api/v1/comments/abc/actions'); + expect(fetchMock.calls().matched[0][0]).to.equal('/api/v1/actions/abc'); expect(response).to.deep.equal({}); }); });