diff --git a/src/domain/dataset.js b/src/domain/dataset.js index 603a64f22..1bfe913ba 100644 --- a/src/domain/dataset.js +++ b/src/domain/dataset.js @@ -17,7 +17,8 @@ import { } from "ramda"; const defaultState = { - datasets: {} + datasets: {}, + diffs:[] }; const defaultItemState = { owner: "", @@ -119,8 +120,10 @@ const valuesFor = (dataset, configuration) => { */ const setDataset = createAction("SET_DATASET"); const setFilteredDataset = createAction("SET_FILTERED_DATASET"); +const setDatasetDiff = createAction("SET_DATASET_DIFF"); const removeDataset = createAction("REMOVE_DATASET"); const removeFilteredDataset = createAction("REMOVE_FILTERED_DATASET"); +const removeDatasetDiff = createAction("REMOVE_DATASET_DIFF"); const setIsFetching = createAction("SET_IS_FETCHING"); // REDUCERS @@ -154,6 +157,25 @@ const reducer = handleActions( state.datasets[owner].filtered = filtered; return { ...state}; }, + [setDatasetDiff]: (state, { payload }) => { + const start = payload.start; + const end = payload.end; + const differences = payload.differences + const newDiff = { + 'start': start, + 'end': end, + 'differences': differences + } + + const idx = state.diffs.findIndex(d => d.start === start && d.end === end); + + if(idx === -1){ + state.diffs.push(newDiff); + } else { + state.diffs[idx] = newDiff; + } + return { ...state}; + }, [removeDataset]: (state, { payload }) => { const owner = payload.owner; if(state.datasets.hasOwnProperty(owner)) @@ -168,6 +190,17 @@ const reducer = handleActions( return { ...state } }, + [removeDatasetDiff]: (state, { payload }) => { + const start = payload.start; + const end = payload.end; + const idx = state.diffs.findIndex(d => d.start === start && d.end === end); + + if(idx !== -1){ + state.diffs.splice(idx, 1); + } + + return { ...state } + }, [setIsFetching]: (state, { payload }) => { const owner = payload.owner; const isFetching = !!payload.isFetching; @@ -183,6 +216,15 @@ const reducer = handleActions( const selectDataset = (state, owner) => state.dataset.datasets[owner] && state.dataset.datasets[owner].dataset ? state.dataset.datasets[owner].dataset : defaultItemState.dataset; const selectFilteredDataset = (state, owner) => state.dataset.datasets[owner] && state.dataset.datasets[owner].filtered ? state.dataset.datasets[owner].filtered : defaultItemState.filtered; +const selectDatasetDiff = (state, start, end) => { + let diff = null; + const idx = state.dataset.diffs.findIndex(d => d.start === start && d.end === end); + + if(idx !== -1){ + diff = state.dataset.diffs[idx].differences;; + } + return diff; +} const selectConfiguration = (state, owner) => state.dataset.datasets[owner] && state.dataset.datasets[owner].configuration ? state.dataset.datasets[owner].configuration : defaultItemState.configuration; const selectMergedConfiguration = (state) => { let fields = []; @@ -223,4 +265,4 @@ const getLastUpdated = (state, owner) => state.dataset.datasets[owner] && state. export default reducer; export { setDataset, selectDataset, removeDataset, setFilteredDataset, selectFilteredDataset, removeFilteredDataset, selectConfiguration, selectMergedConfiguration, selectValues, - selectMergedValues, getFieldId, configurationFor, setIsFetching, getIsFetching, getLastUpdated, valuesFor }; + selectMergedValues, getFieldId, configurationFor, setIsFetching, getIsFetching, getLastUpdated, valuesFor, setDatasetDiff, removeDatasetDiff, selectDatasetDiff }; diff --git a/src/domain/dataset.test.js b/src/domain/dataset.test.js index ebadb0a05..81473d94f 100644 --- a/src/domain/dataset.test.js +++ b/src/domain/dataset.test.js @@ -11,7 +11,10 @@ import { selectValues, selectMergedValues, setIsFetching, - getIsFetching + getIsFetching, + setDatasetDiff, + removeDatasetDiff, + selectDatasetDiff } from "./dataset"; import { combineReducers } from "redux"; @@ -313,5 +316,63 @@ describe("Dataset Reducer", () => { done(); }); }); + + describe("setDatasetDiffs", () => { + it("sets the diff between 2 datasets", (done) => { + const ds1Owner = uuidv4(); + const ds2Owner = uuidv4(); + const ds1 = [ + { 'uid': "uid1", 'role': { 'role': "role1", 'confidence': 80 } }, + { 'uid': "uid2", 'role': { 'role': "role1", 'confidence': 80 } } + ]; + const ds2 = [ + { 'uid': "uid1", 'role': { 'role': "role1", 'confidence': 80 } }, + { 'uid': "uid2", 'role': { 'role': "role2", 'confidence': 80 } } + ]; + const differences =[{ + key:"uid2", + fields: [{ + field: { 'path': ["role", "role"], 'displayName': "Role", 'groupable': false }, + startValue: "role1", + endValue: "role2" + }] + }]; + + const action = setDatasetDiff({ 'start': ds1Owner, 'end': ds2Owner, 'differences': differences }); + const result = reducer({}, action); + + expect(selectDatasetDiff(result, ds1Owner, ds2Owner)).to.deep.equal(differences); + + done(); + }); + + it("removes the diff between 2 datasets", (done) => { + const ds1Owner = uuidv4(); + const ds2Owner = uuidv4(); + const ds1 = [ + { 'uid': "uid1", 'role': { 'role': "role1", 'confidence': 80 } }, + { 'uid': "uid2", 'role': { 'role': "role1", 'confidence': 80 } } + ]; + const ds2 = [ + { 'uid': "uid1", 'role': { 'role': "role1", 'confidence': 80 } }, + { 'uid': "uid2", 'role': { 'role': "role2", 'confidence': 80 } } + ]; + const differences =[{ + key:"uid2", + fields: [{ + field: { 'path': ["role", "role"], 'displayName': "Role", 'groupable': false }, + startValue: "role1", + endValue: "role2" + }] + }]; + + const action = removeDatasetDiff({ 'start': ds1Owner, 'end': ds2Owner}); + const result = reducer({ diffs:[{ 'start': ds1Owner, 'end': ds2Owner, 'differences': differences }] }, action); + + expect(selectDatasetDiff(result, ds1Owner, ds2Owner)).to.equal(null); + + done(); + }); + }); }); });