diff --git a/src/actions/user-actions.ts b/src/actions/user-actions.ts index ec961ba..d1444e2 100644 --- a/src/actions/user-actions.ts +++ b/src/actions/user-actions.ts @@ -51,6 +51,14 @@ export async function rejectUser( const userId = formData.get("userId") as string; const rejection_details = formData.get("rejection_details") as string; + if (!rejection_details?.trim()) { + return { + message: "Rejection details are required.", + fieldErrors: { rejection_details: ["Rejection details are required."] }, + payload: formData, + }; + } + const response = await apiClient.delete( `/api/auth/users/${userId}/reject/`, { data: { rejection_details } }, @@ -65,17 +73,17 @@ export async function rejectUser( }; } - if (response.status < 200 || response.status >= 300) { - const errorData = (response.data ?? {}) as ApiError; - throw new Error( - errorData.message || errorData.detail || "Failed to reject user", - ); - } - const error = (response.data ?? {}) as ApiError; + const message = + error.message || error.detail || "Failed to reject user."; + // The backend requires a non-empty reason; surface that as a field error so + // the textarea highlights, otherwise just toast the message. + const isReasonError = /rejection details/i.test(message); return { - message: error.message || error.detail || "An unexpected error occurred.", - fieldErrors: {}, + message, + fieldErrors: isReasonError + ? { rejection_details: [message] } + : {}, payload: formData, }; }