mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 15:51:12 -05:00
Fix moderation status update
This commit is contained in:
@@ -185,6 +185,12 @@ export function ModerationQueue() {
|
||||
action: 'approved' | 'rejected',
|
||||
moderatorNotes?: string
|
||||
) => {
|
||||
// Prevent multiple clicks on the same item
|
||||
if (actionLoading === item.id) {
|
||||
console.log('Action already in progress for item:', item.id);
|
||||
return;
|
||||
}
|
||||
|
||||
setActionLoading(item.id);
|
||||
try {
|
||||
const table = item.type === 'review' ? 'reviews' : 'content_submissions';
|
||||
@@ -209,7 +215,7 @@ export function ModerationQueue() {
|
||||
updateData.reviewer_notes = moderatorNotes;
|
||||
}
|
||||
|
||||
console.log('Updating item:', item.id, 'with data:', updateData);
|
||||
console.log('Updating item:', item.id, 'with data:', updateData, 'table:', table);
|
||||
|
||||
const { error, data } = await supabase
|
||||
.from(table)
|
||||
@@ -222,36 +228,54 @@ export function ModerationQueue() {
|
||||
throw error;
|
||||
}
|
||||
|
||||
console.log('Update successful:', data);
|
||||
console.log('Update response:', { data, rowsAffected: data?.length });
|
||||
|
||||
// Check if the update actually affected any rows
|
||||
if (!data || data.length === 0) {
|
||||
console.error('No rows were updated. This might be due to RLS policies or the item not existing.');
|
||||
throw new Error('Failed to update item - no rows affected. You might not have permission to moderate this content.');
|
||||
}
|
||||
|
||||
console.log('Update successful, rows affected:', data.length);
|
||||
|
||||
toast({
|
||||
title: `Content ${action}`,
|
||||
description: `The ${item.type} has been ${action}`,
|
||||
});
|
||||
|
||||
// Update the local state immediately
|
||||
// Only update local state if the database update was successful
|
||||
setItems(prev => prev.map(i =>
|
||||
i.id === item.id
|
||||
? { ...i, status: action }
|
||||
: i
|
||||
));
|
||||
|
||||
// Also refresh the queue after a short delay to ensure consistency
|
||||
setTimeout(() => {
|
||||
fetchItems(activeEntityFilter, activeStatusFilter);
|
||||
}, 500);
|
||||
|
||||
// Clear notes
|
||||
// Clear notes only after successful update
|
||||
setNotes(prev => {
|
||||
const newNotes = { ...prev };
|
||||
delete newNotes[item.id];
|
||||
return newNotes;
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
// Only refresh if we're viewing a filter that should no longer show this item
|
||||
if ((activeStatusFilter === 'pending' && (action === 'approved' || action === 'rejected')) ||
|
||||
(activeStatusFilter === 'flagged' && (action === 'approved' || action === 'rejected'))) {
|
||||
console.log('Item no longer matches filter, removing from view');
|
||||
}
|
||||
|
||||
} catch (error: any) {
|
||||
console.error('Error moderating content:', error);
|
||||
|
||||
// Revert any optimistic updates
|
||||
setItems(prev => prev.map(i =>
|
||||
i.id === item.id
|
||||
? { ...i, status: item.status } // Revert to original status
|
||||
: i
|
||||
));
|
||||
|
||||
toast({
|
||||
title: "Error",
|
||||
description: `Failed to ${action} content: ${error.message}`,
|
||||
description: error.message || `Failed to ${action} content`,
|
||||
variant: "destructive",
|
||||
});
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user