mirror of
https://github.com/myronblair/parkerslingshotrentals
synced 2026-06-30 17:50:31 -05:00
Fix: blocked dates enforce on date input; fix CORS + no-cache on availability endpoint
This commit is contained in:
+6
-5
@@ -2,7 +2,8 @@
|
|||||||
require_once __DIR__ . '/db.php';
|
require_once __DIR__ . '/db.php';
|
||||||
|
|
||||||
header('Content-Type: application/json');
|
header('Content-Type: application/json');
|
||||||
header('Access-Control-Allow-Origin: https://parkerslingshotrentals.com');
|
header('Access-Control-Allow-Origin: ' . SITE_URL);
|
||||||
|
header('Cache-Control: no-store, no-cache, must-revalidate');
|
||||||
|
|
||||||
$month = (int)($_GET['month'] ?? date('n'));
|
$month = (int)($_GET['month'] ?? date('n'));
|
||||||
$year = (int)($_GET['year'] ?? date('Y'));
|
$year = (int)($_GET['year'] ?? date('Y'));
|
||||||
@@ -12,7 +13,7 @@ $year = max(date('Y'), min(date('Y') + 2, $year));
|
|||||||
$start = sprintf('%04d-%02d-01', $year, $month);
|
$start = sprintf('%04d-%02d-01', $year, $month);
|
||||||
$end = date('Y-m-t', strtotime($start));
|
$end = date('Y-m-t', strtotime($start));
|
||||||
|
|
||||||
// Booked dates from confirmed/pending bookings
|
// Bookings that overlap this month — including those that start last month and end this month
|
||||||
$booked = db()->prepare(
|
$booked = db()->prepare(
|
||||||
"SELECT rental_date, end_date FROM bookings
|
"SELECT rental_date, end_date FROM bookings
|
||||||
WHERE status IN ('pending','confirmed')
|
WHERE status IN ('pending','confirmed')
|
||||||
@@ -30,7 +31,7 @@ foreach ($booked->fetchAll() as $row) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Admin-blocked dates
|
// Admin-blocked dates for this month
|
||||||
$blocked = db()->prepare(
|
$blocked = db()->prepare(
|
||||||
"SELECT block_date FROM blocked_dates WHERE block_date BETWEEN ? AND ?"
|
"SELECT block_date FROM blocked_dates WHERE block_date BETWEEN ? AND ?"
|
||||||
);
|
);
|
||||||
@@ -40,7 +41,7 @@ foreach ($blocked->fetchAll() as $row) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
echo json_encode([
|
echo json_encode([
|
||||||
'month' => $month,
|
'month' => $month,
|
||||||
'year' => $year,
|
'year' => $year,
|
||||||
'booked_dates' => array_values(array_unique($bookedDays)),
|
'booked_dates' => array_values(array_unique($bookedDays)),
|
||||||
]);
|
]);
|
||||||
|
|||||||
+19
-10
@@ -810,6 +810,7 @@
|
|||||||
<option value="weekend">Weekend — $299</option>
|
<option value="weekend">Weekend — $299</option>
|
||||||
</select>
|
</select>
|
||||||
<input type="date" name="date" required />
|
<input type="date" name="date" required />
|
||||||
|
<div id="date-unavail-msg" style="display:none;color:#f87171;font-size:.82rem;margin-top:.25rem">That date is already booked or unavailable. Please choose another.</div>
|
||||||
<textarea name="message" placeholder="Anything else we should know? (optional)"></textarea>
|
<textarea name="message" placeholder="Anything else we should know? (optional)"></textarea>
|
||||||
|
|
||||||
<!-- Square deposit card -->
|
<!-- Square deposit card -->
|
||||||
@@ -972,18 +973,26 @@
|
|||||||
// Initial load
|
// Initial load
|
||||||
loadCalendar(calMonth, calYear);
|
loadCalendar(calMonth, calYear);
|
||||||
|
|
||||||
// Keep calendar in sync when user types a date manually
|
// Keep calendar in sync when user types a date manually; validate against bookedSet
|
||||||
if (dateInput) {
|
if (dateInput) {
|
||||||
dateInput.addEventListener('change', () => {
|
dateInput.addEventListener('change', async () => {
|
||||||
if (dateInput.value) {
|
const val = dateInput.value;
|
||||||
const [y, m] = dateInput.value.split('-').map(Number);
|
if (!val) return;
|
||||||
if (m !== calMonth || y !== calYear) {
|
const [y, m] = val.split('-').map(Number);
|
||||||
calMonth = m; calYear = y;
|
if (m !== calMonth || y !== calYear) {
|
||||||
loadCalendar(calMonth, calYear);
|
calMonth = m; calYear = y;
|
||||||
}
|
await loadCalendar(calMonth, calYear);
|
||||||
selectedDate = dateInput.value;
|
|
||||||
renderCalendar(calMonth, calYear);
|
|
||||||
}
|
}
|
||||||
|
const unavailMsg = document.getElementById('date-unavail-msg');
|
||||||
|
if (bookedSet.has(val)) {
|
||||||
|
dateInput.value = '';
|
||||||
|
selectedDate = null;
|
||||||
|
if (unavailMsg) { unavailMsg.style.display = 'block'; setTimeout(() => unavailMsg.style.display = 'none', 5000); }
|
||||||
|
} else {
|
||||||
|
selectedDate = val;
|
||||||
|
if (unavailMsg) unavailMsg.style.display = 'none';
|
||||||
|
}
|
||||||
|
renderCalendar(calMonth, calYear);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user