diff --git a/src/components/Tenant/CreateTenant.jsx b/src/components/Tenant/CreateTenant.jsx index 03f3fda3..b759f5ce 100644 --- a/src/components/Tenant/CreateTenant.jsx +++ b/src/components/Tenant/CreateTenant.jsx @@ -29,7 +29,6 @@ const CreateTenant = () => { const location = useLocation(); const formData = location.state?.formData || null; - // Assume useCreateTenant also handles updates, so we destructure `updateTenant` as well. const { createTenant, updateTenant, loading, error, success } = useCreateTenant(); const [form, setForm] = useState(initialData); @@ -44,13 +43,12 @@ const CreateTenant = () => { if (formData) { const { contactName, contactNumber, logoImage, ...rest } = formData; - // A more robust way to split the name from the backend data let firstName = ""; let lastName = ""; if (contactName) { const nameParts = contactName.trim().split(" "); - firstName = nameParts.shift() || ""; // Take the first word - lastName = nameParts.join(" ") || ""; // Join the rest + firstName = nameParts.shift() || ""; + lastName = nameParts.join(" ") || ""; } setForm({ @@ -118,44 +116,55 @@ const CreateTenant = () => { setImagePreview(defaultAvatar); }; + const handleClearForm = () => { + setForm(initialData); + setImagePreview(defaultAvatar); + setImageFile(null); + }; + const handleSubmit = useCallback( async (e) => { e.preventDefault(); - // Prepare the data, ensuring firstName and lastName are included + // Determine the image to send to the API + // If there's a new file selected (imageFile), use it. + // If the image was reset (imagePreview is defaultAvatar), send null. + // Otherwise, use the existing logo image (imagePreview). + const finalLogoImage = imageFile + ? imageFile + : imagePreview === defaultAvatar + ? null + : imagePreview; + const submissionData = { - ...form, // This spreads all fields from your form state, including firstName and lastName - logoImage: imagePreview, + ...form, + logoImage: finalLogoImage, contactNumber: form.phone, contactName: `${form.firstName} ${form.lastName}`.trim(), }; let result; if (formData?.id) { - // This is the update path. Call the update function from the hook. result = await updateTenant(formData.id, submissionData); if (result) { alert("Tenant updated successfully!"); - // Navigate back to the profile page with the updated tenant data navigate("/tenant/profile", { state: { newTenant: result } }); } else { alert("Failed to update tenant. Please check the form and try again."); } } else { - // This is the creation path. Call the create function from the hook. result = await createTenant(submissionData); if (result) { alert("Tenant created successfully!"); - // Navigate to the subscription page with the new tenant's data navigate("/tenant/profile/subscription", { state: { formData: result } }); } else { alert("Failed to create tenant. Please check the form and try again."); } } }, - [form, imagePreview, formData, navigate, createTenant, updateTenant] + [form, imagePreview, imageFile, formData, navigate, createTenant, updateTenant] ); const RequiredLabel = ({ label }) => ( @@ -237,17 +246,19 @@ const CreateTenant = () => { onChange={handleChange} /> +
- +
+
{ />
- + -
-
- -
+ +
+ + +
+