Last active
March 11, 2019 21:53
-
-
Save arackaf/070ac026486a7d0d1e7b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function subjectsReducer(state = initialSubjectsState(), action = {}){ | |
switch(action.type){ | |
case LOAD_SUBJECTS_RESULTS: | |
return Object.assign({}, state, { list: subjectsToHash(action.subjects) }); | |
case EDIT_SUBJECT: | |
var editingSubject = state.list[action._id], | |
newSubjectParent, | |
eligibleParents = flattenedSubjects(state.list).filter(s => s._id !== action._id && (!new RegExp(`,${action._id},`).test(s.path))); | |
if (editingSubject.path == null){ | |
newSubjectParent = null; | |
} else { | |
let hierarchy = editingSubject.path.split(','); | |
newSubjectParent = hierarchy[hierarchy.length - 2]; | |
} | |
return Object.assign({}, state, { editSubjectsPacket: Object.assign({}, state.editSubjectsPacket, { newSubjectName: editingSubject.name, newSubjectParent, editingSubject, eligibleParents }) }); | |
case UPDATE_SUBJECT_RESULTS: | |
let changedSubjects = subjectsToHash(action.affectedSubjects); | |
return Object.assign({}, state, { list: Object.assign({}, state.list, changedSubjects) }); | |
} | |
return state; | |
} | |
function subjectsToHash(subjects){ | |
let hash = {}; | |
subjects.forEach(s => hash[s._id] = s); | |
return hash; | |
} | |
function flattenedSubjects(subjects){ | |
return Object.keys(subjects).map(k => subjects[k]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment