/** * Photo Selector Component with Multi-Select * Feature: personal-user-enhancements * Requirements: 2.2 */ import React, { useState, useEffect } from 'react'; import { Button, message, Spin, Empty, Checkbox } from 'antd'; import { useModel, request } from '@umijs/max'; import styles from '../index.less'; interface PhotoSelectorProps { albumId: string; existingPhotoIds: string[]; onSuccess: () => void; onCancel: () => void; } interface Photo { id: string; url: string; thumbnailUrl: string; } const PhotoSelector: React.FC = ({ albumId, existingPhotoIds, onSuccess, onCancel, }) => { const { addPhotosToAlbum } = useModel('albums'); const [photos, setPhotos] = useState([]); const [loading, setLoading] = useState(false); const [submitting, setSubmitting] = useState(false); const [selectedPhotoIds, setSelectedPhotoIds] = useState([]); useEffect(() => { fetchUserPhotos(); }, []); const fetchUserPhotos = async () => { setLoading(true); try { // Fetch user's photos from gallery const response = await request('/api/v1/gallery/photos', { method: 'GET', params: { pageSize: 100 }, }); // Filter out photos already in the album const availablePhotos = response.items?.filter( (photo: Photo) => !existingPhotoIds.includes(photo.id) ) || []; setPhotos(availablePhotos); } catch (error) { message.error('Failed to load photos'); console.error('Error loading photos:', error); } finally { setLoading(false); } }; const handlePhotoToggle = (photoId: string) => { setSelectedPhotoIds((prev) => prev.includes(photoId) ? prev.filter((id) => id !== photoId) : [...prev, photoId] ); }; const handleSelectAll = () => { if (selectedPhotoIds.length === photos.length) { setSelectedPhotoIds([]); } else { setSelectedPhotoIds(photos.map((p) => p.id)); } }; const handleSubmit = async () => { if (selectedPhotoIds.length === 0) { message.warning('Please select at least one photo'); return; } setSubmitting(true); try { await addPhotosToAlbum(albumId, selectedPhotoIds); message.success(`${selectedPhotoIds.length} photo(s) added to album`); onSuccess(); } catch (error) { message.error('Failed to add photos to album'); console.error('Error adding photos:', error); } finally { setSubmitting(false); } }; if (loading) { return ; } if (photos.length === 0) { return ( ); } return (
0 && selectedPhotoIds.length < photos.length} onChange={handleSelectAll} > Select All ({selectedPhotoIds.length} selected)
{photos.map((photo) => { const isSelected = selectedPhotoIds.includes(photo.id); return (
handlePhotoToggle(photo.id)} > e.stopPropagation()} />
); })}
); }; export default PhotoSelector;