react-server-actions
Version:
A package for working with actions in React and Next.js
68 lines • 2.12 kB
JavaScript
// ** Action result constructors helpers
export const success = (formData, successData) => ({
success: true,
formData,
successData,
invalid: undefined,
error: undefined,
});
export const failure = (formData, invalid) => ({
success: false,
formData, // pass down the data even if there are errors to leave the form filled
successData: undefined,
invalid,
error: undefined,
});
export const error = (formData, error) => ({
formData, // pass down the data even if there are errors to leave the form filled
successData: undefined,
success: false,
invalid: undefined,
error: error instanceof Error ? error.message : JSON.stringify(error),
});
export function actionInvalid(formData, field, error) {
return {
invalid: {
[field]: error,
},
success: false,
error: undefined,
formData, // pass down the data even if there are errors to leave the form filled
successData: undefined,
};
}
export function actionError(formData, error) {
return {
invalid: undefined,
success: false,
error: error,
formData, // pass down the data even if there are errors to leave the form filled
successData: undefined,
};
}
// ** Action result typeguards
export const isFailureActionResult = (actionResult) => {
return (typeof actionResult === 'object' &&
actionResult !== null &&
'success' in actionResult &&
actionResult.success === false &&
'invalid' in actionResult &&
actionResult.invalid !== undefined);
};
export const isErrorActionResult = (actionResult) => {
return (typeof actionResult === 'object' &&
actionResult !== null &&
'success' in actionResult &&
actionResult.success === false &&
'error' in actionResult &&
actionResult.error !== undefined);
};
// ** Initial state helper
export const initialState = (formData) => ({
success: false,
formData,
successData: undefined,
invalid: undefined,
error: undefined,
});
//# sourceMappingURL=helpers.js.map