added tons of features
This commit is contained in:
31
actions/execute-action.helper.ts
Normal file
31
actions/execute-action.helper.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import {isRedirectError} from 'next/dist/client/components/redirect-error'
|
||||
|
||||
type Options<T> = {
|
||||
actionFn: () => Promise<T>
|
||||
successMessage?: string
|
||||
}
|
||||
|
||||
const executeAction = async <T>({
|
||||
actionFn,
|
||||
successMessage = 'The actions was successful'
|
||||
}: Options<T>): Promise<{success: boolean; message: string}> => {
|
||||
try {
|
||||
await actionFn()
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: successMessage
|
||||
}
|
||||
} catch (error) {
|
||||
if (isRedirectError(error)) {
|
||||
throw error
|
||||
}
|
||||
|
||||
return {
|
||||
success: false,
|
||||
message: 'An error has occurred during executing the action'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export {executeAction}
|
||||
Reference in New Issue
Block a user