This commit is contained in:
Flatlogic Bot 2026-02-28 23:48:52 +00:00
parent 3f9d35d276
commit d2d6703960
2 changed files with 32 additions and 18 deletions

View File

@ -10,7 +10,6 @@ if (!$user_id) {
}
$startup_id = $_POST['startup_id'] ?? null;
$action = $_POST['action'] ?? 'follow'; // 'follow' or 'unfollow'
if (!$startup_id) {
echo json_encode(['success' => false, 'error' => 'Startup ID is required']);
@ -18,28 +17,38 @@ if (!$startup_id) {
}
try {
if ($action === 'follow') {
// Check if already following
$stmt = db()->prepare("SELECT 1 FROM startup_followers WHERE startup_id = ? AND user_id = ?");
$stmt->execute([$startup_id, $user_id]);
$is_following = (bool)$stmt->fetch();
if (!$is_following) {
// Follow
$stmt = db()->prepare("INSERT IGNORE INTO startup_followers (startup_id, user_id) VALUES (?, ?)");
$stmt->execute([$startup_id, $user_id]);
// Update denormalized count
$stmt = db()->prepare("UPDATE startups SET followers_count = (SELECT COUNT(*) FROM startup_followers WHERE startup_id = ?) WHERE id = ?");
$stmt->execute([$startup_id, $startup_id]);
$action_result = 'followed';
} else {
// Unfollow
$stmt = db()->prepare("DELETE FROM startup_followers WHERE startup_id = ? AND user_id = ?");
$stmt->execute([$startup_id, $user_id]);
// Update denormalized count
$stmt = db()->prepare("UPDATE startups SET followers_count = (SELECT COUNT(*) FROM startup_followers WHERE startup_id = ?) WHERE id = ?");
$stmt->execute([$startup_id, $startup_id]);
$action_result = 'unfollowed';
}
// Update denormalized count
$stmt = db()->prepare("UPDATE startups SET followers_count = (SELECT COUNT(*) FROM startup_followers WHERE startup_id = ?) WHERE id = ?");
$stmt->execute([$startup_id, $startup_id]);
// Get new count
$stmt = db()->prepare("SELECT followers_count FROM startups WHERE id = ?");
$stmt->execute([$startup_id]);
$new_count = $stmt->fetchColumn();
echo json_encode(['success' => true, 'new_count' => $new_count]);
echo json_encode([
'success' => true,
'action' => $action_result,
'new_count' => (int)$new_count,
'followers_count' => (int)$new_count // redundancy for both frontend versions
]);
} catch (Exception $e) {
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}

View File

@ -254,14 +254,19 @@ function getNextDividendInfo($createdAt) {
display: inline-flex;
align-items: center;
gap: 10px;
border: 1px solid var(--accent-primary);
border: 1px solid var(--border-color);
}
.follow-btn.following {
background: var(--accent-primary);
color: #000;
border-color: var(--accent-primary);
}
.follow-btn.not-following {
background: transparent;
color: var(--text-primary);
}
.follow-btn.not-following:hover {
border-color: var(--accent-primary);
color: var(--accent-primary);
}
.follow-btn:hover {
@ -688,12 +693,12 @@ function toggleFollow(startupId) {
const span = btn.querySelector('span');
const countSpan = document.getElementById('follower-count');
const formData = new FormData();
formData.append('startup_id', startupId);
fetch('api/follow_startup.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: 'startup_id=' + startupId
body: formData
})
.then(response => response.json())
.then(data => {
@ -711,8 +716,8 @@ function toggleFollow(startupId) {
icon.classList.add('fa-plus');
span.innerText = 'Follow';
}
if (countSpan) {
countSpan.innerText = data.followers_count.toLocaleString();
if (countSpan && (data.new_count !== undefined)) {
countSpan.innerText = data.new_count.toLocaleString();
}
} else {
alert('Error: ' + data.error);