1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-12-01 17:04:19 +00:00
pds-2021-g2-nest/nest_frontend/utils/goToOnSuccess.js

23 lines
688 B
JavaScript

/**
* Decorator which adds a redirect on success to an event handler.
*
* @param func - The function to decorate.
* @param history - The history to push the destination to.
* @param destination - The path of the destination.
* @returns {(function(): void)|*}
*/
export default function goToOnSuccess(func, history, destination) {
return (...args) => {
let result
try {
console.debug("Trying to run: ", func)
result = func(...args)
history.push(destination)
return result
}
catch(e) {
console.debug("Failed to run ", func, ", not doing anything.")
throw e
}
}
}