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; $startup_id = $_POST['startup_id'] ?? null;
$action = $_POST['action'] ?? 'follow'; // 'follow' or 'unfollow'
if (!$startup_id) { if (!$startup_id) {
echo json_encode(['success' => false, 'error' => 'Startup ID is required']); echo json_encode(['success' => false, 'error' => 'Startup ID is required']);
@ -18,28 +17,38 @@ if (!$startup_id) {
} }
try { 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 = db()->prepare("INSERT IGNORE INTO startup_followers (startup_id, user_id) VALUES (?, ?)");
$stmt->execute([$startup_id, $user_id]); $stmt->execute([$startup_id, $user_id]);
$action_result = 'followed';
// 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]);
} else { } else {
// Unfollow
$stmt = db()->prepare("DELETE FROM startup_followers WHERE startup_id = ? AND user_id = ?"); $stmt = db()->prepare("DELETE FROM startup_followers WHERE startup_id = ? AND user_id = ?");
$stmt->execute([$startup_id, $user_id]); $stmt->execute([$startup_id, $user_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]);
} }
// 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 // Get new count
$stmt = db()->prepare("SELECT followers_count FROM startups WHERE id = ?"); $stmt = db()->prepare("SELECT followers_count FROM startups WHERE id = ?");
$stmt->execute([$startup_id]); $stmt->execute([$startup_id]);
$new_count = $stmt->fetchColumn(); $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) { } catch (Exception $e) {
echo json_encode(['success' => false, 'error' => $e->getMessage()]); echo json_encode(['success' => false, 'error' => $e->getMessage()]);
} }

View File

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