Okay, let's refactor this code block to be more concise and less repetitive (DRY - Don't Repeat Yourself).
The main redundancies are:
- The
toast function call itself.
- The
classNames: { content: "w-full" } option.
- The structure of the
description JSX.
- The potential typo
resp.message vs res.message.
Here are a few options, from simple improvements to a more extracted approach:
Option 1: Minor cleanup (Addressing typo and common options)
This version extracts the common options and addresses the likely typo (resp -> res).
// Define common toast options
const toastOptions = {
classNames: { content: "w-full" },
};
// Determine title and message based on success
const isSuccess = res.success;
const title = isSuccess ? "Upload Successful!" : "Something went wrong.";
// *** Assumption: 'resp.message' was a typo, using 'res.message'. Adjust if 'resp' is intentional. ***
const messageContent = res.message;
// Create the description JSX dynamically
const description = (
<div className="mt-2 p-4 rounded-md shadow bg-muted">
{/* Safely render message: stringify objects/arrays, else display as string */}
<p>
{messageContent !== null && typeof messageContent === 'object'
? JSON.stringify(messageContent)
: String(messageContent ?? '') /* Handle null/undefined */}
</p>
</div>
);
// Call toast once with dynamic values
toast(title, {
...toastOptions,
description: description,
});
// Reset form only on success, checking if form and method exist
if (isSuccess && form && typeof form.reset === 'function') {
form.reset();
}
Option 2: Using a Helper Function (More Scalable)
If this pattern appears elsewhere, a helper function is a good choice.
// Helper function (place this outside the component or in a utils file)
const showResultToast = (success, messageData, formResetFn) => {
const title = success ? "Upload Successful!" : "Something went wrong.";
const description = (
<div className="mt-2 p-4 rounded-md shadow bg-muted">
<p>
{messageData !== null && typeof messageData === 'object'
? JSON.stringify(messageData)
: String(messageData ?? '')}
</p>
</div>
);
toast(title, {
classNames: { content: "w-full" },
description: description,
});
// Execute reset function only if provided and it's a success case
if (success && formResetFn && typeof formResetFn === 'function') {
formResetFn();
}
};
// --- Usage within your component/logic ---
// *** Assumption: 'resp.message' was a typo, using 'res.message'. Adjust if 'resp' is intentional. ***
showResultToast(res.success, res.message, form?.reset); // Use optional chaining for safety
Explanation of Improvements:
- DRY Principle: Both options eliminate the repeated code for
toast options and the description structure.
- Readability: Separating the logic for determining the title, message, and description makes the core
toast call cleaner.
- Maintainability: If the toast styling or description structure needs to change, you only need to update it in one place (either Option 1's block or the helper function in Option 2).
- Robustness:
- Added a check
messageContent !== null && typeof messageContent === 'object' before JSON.stringify to prevent errors if messageContent is already a string, number, null, etc. It falls back to String(messageContent ?? '') for safe rendering.
- Added checks (
form && typeof form.reset === 'function' or form?.reset) to ensure form.reset exists before attempting to call it, preventing runtime errors if form is not available or doesn't have a reset method.
- Assumption Clarification: Explicitly noted the assumption about
resp.message likely being a typo for res.message. If resp is a different intended variable, the logic would need adjustment based on its meaning.
Choose Option 1 for a quick refactor within the existing block. Choose Option 2 if this logic is likely to be reused or if you prefer a more structured, functional approach.