Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
108 lines (95 loc) · 3.57 KB
/
Copy pathapp.js
File metadata and controls
108 lines (95 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Retrieve centralized storage or initialize cleanly
let getproperty = JSON.parse(localStorage.getItem("properties")) || [];
// --- RENDERING ENGINE ---
function renderTable() {
const tbody = document.getElementById("property-table-body");
if (!tbody) return; // Guard clause to prevent errors if elements don't exist on current page
tbody.innerHTML = "";
getproperty.forEach((property, index) => {
// Safely calculate the total units added in Step 2 of the form layout
const totalUnits = property.units ? property.units.length : 0;
const row = document.createElement("tr");
row.innerHTML = `
<td>
<strong>${escapeHTML(property.type)}</strong>
<br />
<small style="color: #8aa3b8; font-size: 11px;">${escapeHTML(property.address || "No Address Listed")}</small>
</td>
<td>${escapeHTML(property.owner)}</td>
<td><span class="status-badge ${property.status.toLowerCase().replace(/\s+/g, "-")}">${escapeHTML(property.status)}</span></td>
<td>${escapeHTML(property.rent)}</td>
<td>
<span class="unit-counter-badge">${totalUnits} ${totalUnits === 1 ? "Unit" : "Units"}</span>
</td>
<td>
<button class="action-view-btn" onclick="viewProperty(${index})">View Details</button>
</td>
`;
tbody.appendChild(row);
});
}
// --- NAVIGATION ENGINE ---
function viewProperty(index) {
const selectedProperty = getproperty[index];
localStorage.setItem("viewProperty", JSON.stringify(selectedProperty));
window.location.href = "view-property.html";
}
// --- Safe XSS String Escaper (Prevents database text injections breaking layouts) ---
function escapeHTML(str) {
if (!str) return "";
return str
.toString()
.replace(
/[&<>'"]/g,
(tag) =>
({
"&": "&",
"<": "<",
">": ">",
"'": "'",
'"': """,
})[tag] || tag,
);
}
// --- UNIFIED INTEGRATED SUBMISSION CONTROLLER ---
const propertyForm = document.getElementById("propertyForm");
if (propertyForm) {
propertyForm.addEventListener("submit", function (event) {
event.preventDefault();
const type = document.getElementById("type").value;
const owner = document.getElementById("owner").value.trim();
const status = document.getElementById("status").value;
const rent = "₦" + document.getElementById("rent").value.trim();
const expiry = document.getElementById("expiry").value;
const tenants = document.getElementById("tenants").value.trim();
const description = document.getElementById("description").value.trim();
const address = document.getElementById("address").value.trim();
const propertyId = crypto.randomUUID();
// Map through dynamic sub-units from Step 2
const unitsContainer = document.getElementById("unitsContainer");
const units = Array.from(unitsContainer.querySelectorAll(".unit-card")).map(
(card) => ({
name: card.querySelector('input[name="unit_name[]"]').value.trim(),
type: card.querySelector('select[name="unit_type[]"]').value,
}),
);
const newProperty = {
propertyId,
type,
owner,
status,
rent,
leaseExpiry: expiry,
tenants,
description,
address,
units,
};
getproperty.push(newProperty);
localStorage.setItem("properties", JSON.stringify(getproperty));
// Redirect back to the dashboard layout view
window.location.href = "dashboard.html";
});
}
// Execute render strictly when the fully qualified DOM is painted
document.addEventListener("DOMContentLoaded", renderTable);