Skip to content

Commit

Permalink
Add user-friendly error messages to admin panel
Browse files Browse the repository at this point in the history
- Add clear error messages for duplicate URLs
- Add success/error messages for all operations
- Keep modal open on error
- Improve message styling
  • Loading branch information
screenfluent committed Nov 18, 2024
1 parent 9624ebf commit 0b381a8
Showing 1 changed file with 58 additions and 3 deletions.
61 changes: 58 additions & 3 deletions public/admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@
}

$db = new Database();
$message = '';
$messageType = '';

// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
Expand All @@ -120,6 +122,14 @@
'is_requested' => isset($_POST['is_requested']) ? 1 : 0
];

// Check for duplicate URL before upload handling
$existing = $db->getImplementationByUrl($data['llms_txt_url']);
if ($existing) {
$message = "An implementation with this llms.txt URL already exists.";
$messageType = 'error';
break;
}

// Handle logo upload
if (isset($_FILES['logo']) && $_FILES['logo']['error'] === UPLOAD_ERR_OK) {
$file = $_FILES['logo'];
Expand All @@ -142,7 +152,13 @@
}
}

$db->addImplementation($data);
if ($db->addImplementation($data)) {
$message = "Implementation added successfully!";
$messageType = 'success';
} else {
$message = "Failed to add implementation. Please try again.";
$messageType = 'error';
}
break;

case 'edit':
Expand Down Expand Up @@ -178,11 +194,23 @@
}
}

$db->updateImplementation($_POST['id'], $data);
if ($db->updateImplementation($_POST['id'], $data)) {
$message = "Implementation updated successfully!";
$messageType = 'success';
} else {
$message = "Failed to update implementation. Please try again.";
$messageType = 'error';
}
break;

case 'delete':
$db->deleteImplementation($_POST['id']);
if ($db->deleteImplementation($_POST['id'])) {
$message = "Implementation deleted successfully!";
$messageType = 'success';
} else {
$message = "Failed to delete implementation. Please try again.";
$messageType = 'error';
}
break;
}
}
Expand Down Expand Up @@ -409,6 +437,22 @@
.actions {
white-space: nowrap;
}
.message {
padding: 10px 15px;
margin: 10px 0;
border-radius: 4px;
font-weight: 500;
}
.message.error {
background: #fee2e2;
color: #991b1b;
border: 1px solid #fecaca;
}
.message.success {
background: #dcfce7;
color: #166534;
border: 1px solid #bbf7d0;
}
</style>
</head>
<body>
Expand All @@ -418,6 +462,12 @@
<button class="add-new" onclick="showAddModal()">Add New</button>
</div>

<?php if ($message): ?>
<div class="message <?php echo $messageType; ?>">
<?php echo htmlspecialchars($message); ?>
</div>
<?php endif; ?>

<div class="table-wrapper">
<table>
<thead>
Expand Down Expand Up @@ -570,6 +620,11 @@ function showEditModal(impl) {
function closeModal() {
document.getElementById('modal').style.display = 'none';
}

// Keep modal open if there was an error
<?php if ($messageType === 'error' && isset($_POST['action'])): ?>
document.getElementById('modal').style.display = 'block';
<?php endif; ?>
</script>
</body>
</html>

0 comments on commit 0b381a8

Please sign in to comment.