Create APi Call in Create Tenant.
This commit is contained in:
parent
cb27f8c259
commit
de854e87f3
@ -29,7 +29,6 @@ const CreateTenant = () => {
|
|||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const formData = location.state?.formData || null;
|
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 { createTenant, updateTenant, loading, error, success } = useCreateTenant();
|
||||||
|
|
||||||
const [form, setForm] = useState(initialData);
|
const [form, setForm] = useState(initialData);
|
||||||
@ -44,13 +43,12 @@ const CreateTenant = () => {
|
|||||||
if (formData) {
|
if (formData) {
|
||||||
const { contactName, contactNumber, logoImage, ...rest } = formData;
|
const { contactName, contactNumber, logoImage, ...rest } = formData;
|
||||||
|
|
||||||
// A more robust way to split the name from the backend data
|
|
||||||
let firstName = "";
|
let firstName = "";
|
||||||
let lastName = "";
|
let lastName = "";
|
||||||
if (contactName) {
|
if (contactName) {
|
||||||
const nameParts = contactName.trim().split(" ");
|
const nameParts = contactName.trim().split(" ");
|
||||||
firstName = nameParts.shift() || ""; // Take the first word
|
firstName = nameParts.shift() || "";
|
||||||
lastName = nameParts.join(" ") || ""; // Join the rest
|
lastName = nameParts.join(" ") || "";
|
||||||
}
|
}
|
||||||
|
|
||||||
setForm({
|
setForm({
|
||||||
@ -118,44 +116,55 @@ const CreateTenant = () => {
|
|||||||
setImagePreview(defaultAvatar);
|
setImagePreview(defaultAvatar);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleClearForm = () => {
|
||||||
|
setForm(initialData);
|
||||||
|
setImagePreview(defaultAvatar);
|
||||||
|
setImageFile(null);
|
||||||
|
};
|
||||||
|
|
||||||
const handleSubmit = useCallback(
|
const handleSubmit = useCallback(
|
||||||
async (e) => {
|
async (e) => {
|
||||||
e.preventDefault();
|
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 = {
|
const submissionData = {
|
||||||
...form, // This spreads all fields from your form state, including firstName and lastName
|
...form,
|
||||||
logoImage: imagePreview,
|
logoImage: finalLogoImage,
|
||||||
contactNumber: form.phone,
|
contactNumber: form.phone,
|
||||||
contactName: `${form.firstName} ${form.lastName}`.trim(),
|
contactName: `${form.firstName} ${form.lastName}`.trim(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let result;
|
let result;
|
||||||
if (formData?.id) {
|
if (formData?.id) {
|
||||||
// This is the update path. Call the update function from the hook.
|
|
||||||
result = await updateTenant(formData.id, submissionData);
|
result = await updateTenant(formData.id, submissionData);
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
alert("Tenant updated successfully!");
|
alert("Tenant updated successfully!");
|
||||||
// Navigate back to the profile page with the updated tenant data
|
|
||||||
navigate("/tenant/profile", { state: { newTenant: result } });
|
navigate("/tenant/profile", { state: { newTenant: result } });
|
||||||
} else {
|
} else {
|
||||||
alert("Failed to update tenant. Please check the form and try again.");
|
alert("Failed to update tenant. Please check the form and try again.");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// This is the creation path. Call the create function from the hook.
|
|
||||||
result = await createTenant(submissionData);
|
result = await createTenant(submissionData);
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
alert("Tenant created successfully!");
|
alert("Tenant created successfully!");
|
||||||
// Navigate to the subscription page with the new tenant's data
|
|
||||||
navigate("/tenant/profile/subscription", { state: { formData: result } });
|
navigate("/tenant/profile/subscription", { state: { formData: result } });
|
||||||
} else {
|
} else {
|
||||||
alert("Failed to create tenant. Please check the form and try again.");
|
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 }) => (
|
const RequiredLabel = ({ label }) => (
|
||||||
@ -237,17 +246,19 @@ const CreateTenant = () => {
|
|||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="col-md-6">
|
<div className="col-md-6">
|
||||||
<RequiredLabel label="Billing Address" />
|
<RequiredLabel label="Onboarding Date" />
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="date"
|
||||||
name="billingAddress"
|
name="onBoardingDate"
|
||||||
className="form-control form-control-sm"
|
className="form-control form-control-sm"
|
||||||
value={form.billingAddress}
|
value={form.onBoardingDate}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="col-md-6">
|
<div className="col-md-6">
|
||||||
<RequiredLabel label="Organization Name" />
|
<RequiredLabel label="Organization Name" />
|
||||||
<input
|
<input
|
||||||
@ -319,26 +330,29 @@ const CreateTenant = () => {
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="col-md-6">
|
<div className="col-md-6">
|
||||||
<label className="form-label small mb-1">Domain Name</label>
|
<RequiredLabel label="Domain Name" />
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
name="domainName"
|
name="domainName"
|
||||||
className="form-control form-control-sm"
|
className="form-control form-control-sm"
|
||||||
value={form.domainName}
|
value={form.domainName}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<div className="col-md-6">
|
|
||||||
<RequiredLabel label="Onboarding Date" />
|
|
||||||
<input
|
|
||||||
type="date"
|
|
||||||
name="onBoardingDate"
|
|
||||||
className="form-control form-control-sm"
|
|
||||||
value={form.onBoardingDate}
|
|
||||||
onChange={handleChange}
|
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div className="col-12">
|
||||||
|
<RequiredLabel label="Billing Address" />
|
||||||
|
<textarea
|
||||||
|
name="billingAddress"
|
||||||
|
className="form-control form-control-sm"
|
||||||
|
rows="2"
|
||||||
|
value={form.billingAddress}
|
||||||
|
onChange={handleChange}
|
||||||
|
required
|
||||||
|
></textarea>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div className="col-12">
|
<div className="col-12">
|
||||||
<label className="form-label small mb-1">Description</label>
|
<label className="form-label small mb-1">Description</label>
|
||||||
<textarea
|
<textarea
|
||||||
@ -406,13 +420,26 @@ const CreateTenant = () => {
|
|||||||
>
|
>
|
||||||
{loading ? "Saving..." : formData?.id ? "Update" : "Save & Continue"}
|
{loading ? "Saving..." : formData?.id ? "Update" : "Save & Continue"}
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
|
{!formData?.id && (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="btn btn-sm btn-warning ms-2 px-4"
|
||||||
|
onClick={handleClearForm}
|
||||||
|
>
|
||||||
|
Clear
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{formData?.id && (
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className="btn btn-sm btn-secondary ms-2 px-4"
|
className="btn btn-sm btn-secondary ms-2 px-4"
|
||||||
onClick={() => navigate("/tenant")}
|
onClick={() => navigate("/tenant/profile")}
|
||||||
>
|
>
|
||||||
Cancel
|
Cancel
|
||||||
</button>
|
</button>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -98,7 +98,7 @@ const Tenant = () => {
|
|||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="table-responsive p-5 pt-3">
|
<div className="table-responsive p-3 pt-3">
|
||||||
<table className="table text-start align-middle">
|
<table className="table text-start align-middle">
|
||||||
<thead>
|
<thead>
|
||||||
<tr className="fs-6">
|
<tr className="fs-6">
|
||||||
@ -121,19 +121,37 @@ const Tenant = () => {
|
|||||||
return (
|
return (
|
||||||
<tr key={tenant.id} style={{ height: "50px" }} className="align-middle">
|
<tr key={tenant.id} style={{ height: "50px" }} className="align-middle">
|
||||||
<td>
|
<td>
|
||||||
<div className="d-flex align-items-center gap-2">
|
<div className={`d-flex align-items-center ${tenant.logoImage ? "gap-2" : ""}`}>
|
||||||
{tenant.logoImage ? (
|
{tenant.logoImage ? (
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
width: "36px",
|
||||||
|
height: "36px",
|
||||||
|
borderRadius: "50%",
|
||||||
|
border: "2px solid #ccc",
|
||||||
|
display: "flex",
|
||||||
|
alignItems: "center",
|
||||||
|
justifyContent: "center",
|
||||||
|
}}
|
||||||
|
>
|
||||||
<img
|
<img
|
||||||
src={tenant.logoImage}
|
src={tenant.logoImage}
|
||||||
alt={`${tenant.name} Logo`}
|
alt={`${tenant.name} Logo`}
|
||||||
style={{ width: "40px", height: "40px", borderRadius: "8px", objectFit: "cover" }}
|
style={{
|
||||||
|
width: "32px",
|
||||||
|
height: "32px",
|
||||||
|
borderRadius: "50%",
|
||||||
|
objectFit: "cover",
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<Avatar firstName={firstName} lastName={lastName} />
|
<Avatar firstName={firstName} lastName={lastName} />
|
||||||
)}
|
)}
|
||||||
<span>{tenant.contactName}</span>
|
<span className="ms-0">{tenant.contactName}</span>
|
||||||
</div>
|
</div>
|
||||||
</td>
|
</td>
|
||||||
|
|
||||||
<td><i className="bx bx-envelope text-primary me-2"></i>{tenant.email}</td>
|
<td><i className="bx bx-envelope text-primary me-2"></i>{tenant.email}</td>
|
||||||
<td><i className="bx bx-phone text-success me-2"></i>{tenant.contactNumber}</td>
|
<td><i className="bx bx-phone text-success me-2"></i>{tenant.contactNumber}</td>
|
||||||
<td><i className="bx bx-globe text-info me-2"></i>{tenant.domainName}</td>
|
<td><i className="bx bx-globe text-info me-2"></i>{tenant.domainName}</td>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user