mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 08:31:12 -05:00
Fix moderation status update
This commit is contained in:
@@ -185,6 +185,12 @@ export function ModerationQueue() {
|
|||||||
action: 'approved' | 'rejected',
|
action: 'approved' | 'rejected',
|
||||||
moderatorNotes?: string
|
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);
|
setActionLoading(item.id);
|
||||||
try {
|
try {
|
||||||
const table = item.type === 'review' ? 'reviews' : 'content_submissions';
|
const table = item.type === 'review' ? 'reviews' : 'content_submissions';
|
||||||
@@ -209,7 +215,7 @@ export function ModerationQueue() {
|
|||||||
updateData.reviewer_notes = moderatorNotes;
|
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
|
const { error, data } = await supabase
|
||||||
.from(table)
|
.from(table)
|
||||||
@@ -222,36 +228,54 @@ export function ModerationQueue() {
|
|||||||
throw error;
|
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({
|
toast({
|
||||||
title: `Content ${action}`,
|
title: `Content ${action}`,
|
||||||
description: `The ${item.type} has been ${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 =>
|
setItems(prev => prev.map(i =>
|
||||||
i.id === item.id
|
i.id === item.id
|
||||||
? { ...i, status: action }
|
? { ...i, status: action }
|
||||||
: i
|
: i
|
||||||
));
|
));
|
||||||
|
|
||||||
// Also refresh the queue after a short delay to ensure consistency
|
// Clear notes only after successful update
|
||||||
setTimeout(() => {
|
|
||||||
fetchItems(activeEntityFilter, activeStatusFilter);
|
|
||||||
}, 500);
|
|
||||||
|
|
||||||
// Clear notes
|
|
||||||
setNotes(prev => {
|
setNotes(prev => {
|
||||||
const newNotes = { ...prev };
|
const newNotes = { ...prev };
|
||||||
delete newNotes[item.id];
|
delete newNotes[item.id];
|
||||||
return newNotes;
|
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);
|
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({
|
toast({
|
||||||
title: "Error",
|
title: "Error",
|
||||||
description: `Failed to ${action} content: ${error.message}`,
|
description: error.message || `Failed to ${action} content`,
|
||||||
variant: "destructive",
|
variant: "destructive",
|
||||||
});
|
});
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
Reference in New Issue
Block a user