Skip to content

Instantly share code, notes, and snippets.

@lele85
Last active February 21, 2017 16:40
Show Gist options
  • Save lele85/53a713aff8e9524ada973e5a1aa44abd to your computer and use it in GitHub Desktop.
Save lele85/53a713aff8e9524ada973e5a1aa44abd to your computer and use it in GitHub Desktop.
Currying
import _get from "lodash/get";
import _merge from "lodash/merge";
import _isEqual from "lodash/isEqual";
const not = (fun) => (...args) => !fun(...args);
const and = (fun1, fun2) => (...args) => fun1(...args) && fun2(...args);
const isSaving = (savingStatePath) => (state) => {
const savingState = _get(state, savingStatePath, false);
return savingState && savingState.state === "SAVING";
}
const hasUnsavedFields = (modelPath, editedFieldsPath) => (state) => {
const originalModel = _get(state, modelPath, {});
const editedFields = _get(state, editedFieldsPath, {});
const editedModel = _merge({}, originalModel, editedFields);
return !_isEqual(originalModel, editedModel);
};
const canNavigateOut = {
"show_edit": and(
not(isSaving("show.show_edit_save_state")),
not(hasUnsavedFields("show.show.model","show.show_edit"))
),
"another_route": not(
isSaving("settings.settings_save_state")
)
}
//Dynamically select logic from current route
canNavigateOut[currentRoute](state);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment