Created
January 9, 2020 19:43
-
-
Save praveen001/ea2e4c64d260ed57cd0e3a4bd8b67819 to your computer and use it in GitHub Desktop.
Undo/Redo with Immer in Redux
This file contains 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
import { ADD_TODO, REMOVE_TODO } from '../actions/todos'; | |
const changes = {}; | |
let currentVersion = -1; | |
const noOfVersionsSupported = 100; | |
const undoableActions = [ADD_TODO, REMOVE_TODO]; | |
export default function(state = initialState, action) { | |
return produce(state, draft => { | |
// ... code removed for brevity | |
}, (patches, inversePatches) => { | |
if (undoableActions.indexOf(action.type) !== -1) { | |
currentVersion++; | |
changes[currentVersion] = { | |
redo: patches, | |
undo: inversePatches | |
} | |
delete changes[currentVersion + 1]; | |
delete changes[currentVersion - noOfVersionsSupported]; | |
} | |
}); | |
} |
This file contains 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
let state = { | |
name: "Micheal", | |
age: 32 | |
}; | |
state = produce(state, draft => { | |
draft.age = 40; | |
}); |
This file contains 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
state = produce(state, draft => { | |
draft.age = 40; | |
}, (patches, inversePatches) => { | |
console.log(patches); | |
// [ { op: 'replace', path: [ 'age' ], value: 40 } ] | |
console.log(inversePatches); | |
// [ { op: 'replace', path: [ 'age' ], value: 32 } ] | |
}); |
This file contains 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
import produce from 'immer'; | |
import { ADD_TODO, REMOVE_TODO } from '../actions/todos'; | |
const initialState = { | |
todos: [ | |
{ | |
desc: "Writing an article" | |
}, | |
{ | |
desc: "Dont use todo application for example" | |
} | |
] | |
} | |
export default function(state = initialState, action) { | |
return produce(state, draft => { | |
switch (action.type) { | |
case ADD_TODO: | |
draft.todos.push(action.payload.todo); | |
break; | |
case REMOVE_TODO: | |
draft.todos.splice(action.payload.index, 1); | |
break; | |
} | |
}); | |
} |
This file contains 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
const changes = {}; | |
let currentVersion = -1; | |
export default function(state = initialState, action) { | |
return produce(state, draft => { | |
// ... code removed for brevity | |
}, (patches, inversePatches) => { | |
currentVersion++; | |
changes[currentVersion] = { | |
redo: patches, | |
undo: inversePatches | |
} | |
}); | |
} |
This file contains 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
const changes = {}; | |
let currentVersion = -1; | |
const noOfVersionsSupported = 100; | |
export default function(state = initialState, action) { | |
return produce(state, draft => { | |
// ... code removed for brevity | |
}, (patches, inversePatches) => { | |
currentVersion++; | |
changes[currentVersion] = { | |
redo: patches, | |
undo: inversePatches | |
} | |
delete changes[currentVersion + 1]; | |
delete changes[currentVersion - noOfVersionsSupported]; | |
}); | |
} |
This file contains 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
import produce, { applyPatches } from 'immer'; | |
import { ADD_TODO, REMOVE_TODO, UNDO, REDO } from '../actions/todos'; | |
// ... code removed for brevity | |
export default function(state = initialState, action) { | |
return produce(state, draft => { | |
switch (action.type) { | |
// ... code removed for brevity | |
case UNDO: | |
return applyPatches(state, changes[currentVersion--].undo); | |
case REDO: | |
return applyPatches(state, changes[++currentVersion].redo); | |
} | |
}, (patches, inversePatches) => { | |
// ... code removed for brevity | |
}); | |
} |
This file contains 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
export default function(state = initialState, action) { | |
return produce(state, draft => { | |
switch (action.type) { | |
// ... code removed for brevity | |
case UNDO: | |
return produce( | |
applyPatches(state, changes[currentVersion--].undo), | |
newDraft => { | |
newDraft.canUndo = changes.hasOwnProperty(currentVersion); | |
newDraft.canRedo = true; | |
} | |
); | |
case REDO: | |
return produce( | |
applyPatches(state, changes[++currentVersion].redo), | |
newDraft => { | |
newDraft.canUndo = true; | |
newDraft.canRedo = changes.hasOwnProperty(currentVersion + 1); | |
} | |
); | |
} | |
if (undoableActions.indexOf(action.type) !== -1) { | |
draft.canUndo = true; | |
draft.canRedo = false; | |
} | |
}, (patches, inversePatches) => { | |
// ... code removed for brevity | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment