Refactor photo modification logic

This commit is contained in:
gpt-engineer-app[bot]
2025-10-02 17:46:45 +00:00
parent 6f579faa31
commit 2750d285cb
5 changed files with 224 additions and 29 deletions

View File

@@ -77,55 +77,128 @@ export function PhotoManagementDialog({
const deletePhoto = async (photoId: string) => {
if (!confirm('Are you sure you want to delete this photo?')) return;
const requestPhotoDelete = async (photoId: string, photo: Photo) => {
const reason = prompt('Please provide a reason for deleting this photo:');
if (!reason) return;
try {
const { error } = await supabase.from('photos').delete().eq('id', photoId);
// Get current user
const { data: { user } } = await supabase.auth.getUser();
if (!user) throw new Error('Not authenticated');
if (error) throw error;
// Create content submission
const { data: submission, error: submissionError } = await supabase
.from('content_submissions')
.insert([{
user_id: user.id,
submission_type: 'photo_delete',
content: {
photo_id: photoId,
entity_type: entityType,
entity_id: entityId,
reason: reason
}
}])
.select()
.single();
if (submissionError) throw submissionError;
// Create submission item
const { error: itemError } = await supabase
.from('submission_items')
.insert({
submission_id: submission.id,
item_type: 'photo_delete',
item_data: {
photo_id: photoId,
entity_type: entityType,
entity_id: entityId,
cloudflare_image_url: photo.cloudflare_image_url,
caption: photo.caption,
reason: reason
},
status: 'pending'
});
if (itemError) throw itemError;
await fetchPhotos();
toast({
title: 'Success',
description: 'Photo deleted',
title: 'Delete request submitted',
description: 'Your photo deletion request has been submitted for moderation',
});
onUpdate?.();
onOpenChange(false);
} catch (error) {
console.error('Error deleting photo:', error);
console.error('Error requesting photo deletion:', error);
toast({
title: 'Error',
description: 'Failed to delete photo',
description: 'Failed to submit deletion request',
variant: 'destructive',
});
}
};
const updatePhoto = async () => {
const requestPhotoEdit = async () => {
if (!editingPhoto) return;
try {
const { error } = await supabase
.from('photos')
.update({
caption: editingPhoto.caption,
})
.eq('id', editingPhoto.id);
// Get current user
const { data: { user } } = await supabase.auth.getUser();
if (!user) throw new Error('Not authenticated');
if (error) throw error;
// Get original photo data
const originalPhoto = photos.find(p => p.id === editingPhoto.id);
if (!originalPhoto) throw new Error('Original photo not found');
// Create content submission
const { data: submission, error: submissionError } = await supabase
.from('content_submissions')
.insert([{
user_id: user.id,
submission_type: 'photo_edit',
content: {
photo_id: editingPhoto.id,
entity_type: entityType,
entity_id: entityId
}
}])
.select()
.single();
if (submissionError) throw submissionError;
// Create submission item
const { error: itemError } = await supabase
.from('submission_items')
.insert({
submission_id: submission.id,
item_type: 'photo_edit',
item_data: {
photo_id: editingPhoto.id,
entity_type: entityType,
entity_id: entityId,
new_caption: editingPhoto.caption,
cloudflare_image_url: editingPhoto.cloudflare_image_url,
},
original_data: {
caption: originalPhoto.caption,
},
status: 'pending'
});
if (itemError) throw itemError;
await fetchPhotos();
setEditingPhoto(null);
toast({
title: 'Success',
description: 'Photo updated',
title: 'Edit request submitted',
description: 'Your photo edit has been submitted for moderation',
});
onUpdate?.();
onOpenChange(false);
} catch (error) {
console.error('Error updating photo:', error);
console.error('Error requesting photo edit:', error);
toast({
title: 'Error',
description: 'Failed to update photo',
description: 'Failed to submit edit request',
variant: 'destructive',
});
}
@@ -167,7 +240,7 @@ export function PhotoManagementDialog({
<Button variant="outline" onClick={() => setEditingPhoto(null)}>
Cancel
</Button>
<Button onClick={updatePhoto}>Save Changes</Button>
<Button onClick={requestPhotoEdit}>Submit for Review</Button>
</DialogFooter>
</DialogContent>
</Dialog>
@@ -223,16 +296,16 @@ export function PhotoManagementDialog({
className="flex-1 sm:flex-initial"
>
<Pencil className="w-4 h-4 mr-2" />
Edit
Request Edit
</Button>
<Button
size="sm"
variant="destructive"
onClick={() => deletePhoto(photo.id)}
onClick={() => requestPhotoDelete(photo.id, photo)}
className="flex-1 sm:flex-initial"
>
<Trash2 className="w-4 h-4 mr-2" />
Delete
Request Delete
</Button>
</div>
</div>