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

25 lines
791 B
JavaScript
Raw Normal View History

2021-05-10 13:22:07 +00:00
/**
* 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) {
2021-05-17 13:48:27 +00:00
return async (...args) => {
2021-05-11 16:05:01 +00:00
let result
2021-05-10 13:22:07 +00:00
try {
2021-05-17 13:48:27 +00:00
console.debug("Executing: ", func)
result = await func(...args)
console.debug("Executed successfully: ", func, " moving to a different page.")
2021-05-11 16:05:01 +00:00
history.push(destination)
return result
2021-05-10 13:22:07 +00:00
}
catch(e) {
2021-05-17 13:48:27 +00:00
console.debug("Execution failed: ", func, ", not doing anything.")
2021-05-11 16:05:01 +00:00
throw e
2021-05-10 13:22:07 +00:00
}
}
}