From 6676ced2228f0a35a55633bb96b6f6c59f17e891 Mon Sep 17 00:00:00 2001 From: simensta Date: Wed, 18 Apr 2018 16:05:03 +0100 Subject: [PATCH] When creating a new collection for favorite subjects, add the selected subject (#258) --- src/ducks/subject.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/ducks/subject.js b/src/ducks/subject.js index aa80ba41..4c8af783 100644 --- a/src/ducks/subject.js +++ b/src/ducks/subject.js @@ -79,7 +79,7 @@ const subjectReducer = (state = initialState, action) => { subjectSet: action.id, queue: [], }); - + case SET_SUBJECT_ID: //Only used for emergencyLoadWorkInProgress() to bypass standard Subject ID selection. return Object.assign({}, state, { id: action.id, @@ -127,12 +127,21 @@ const createFavorites = (project) => { display_name, links, }; - apiClient.type('collections') + const newFavorites = apiClient.type('collections') .create(collection) .save() .catch(err => Promise.reject(err)); + return newFavorites; }; +function addSubjectToCollection(collection, subjectId) { + collection.addLink('subjects', [subjectId]); +} + +function removeSubjectToCollection(collection, subjectId) { + collection.removeLink('subjects', [subjectId]); +} + const toggleFavorite = () => { return (dispatch, getState) => { const projectID = getState().project.id; @@ -148,11 +157,11 @@ const toggleFavorite = () => { owner: user }).then(([collection]) => { if (collection && !favorite) { - collection.addLink('subjects', [subject.id.toString()]); + addSubjectToCollection(collection, subject.id); } else if (collection && favorite) { - collection.removeLink('subjects', [subject.id.toString()]); + removeSubjectToCollection(collection, subject.id); } else { - createFavorites(getState().project); + createFavorites(getState().project).then((collection) => addSubjectToCollection(collection, subject.id)); } }) }