diff --git a/src/features/cameras/DeleteCameraForm.jsx b/src/features/cameras/DeleteCameraForm.jsx index 7cc3e3ba..a980092d 100644 --- a/src/features/cameras/DeleteCameraForm.jsx +++ b/src/features/cameras/DeleteCameraForm.jsx @@ -8,8 +8,13 @@ import { FormWrapper, ButtonRow, HelperText } from '../../components/Form.jsx'; import { deleteCamera, fetchTask, selectDeleteCameraLoading } from '../tasks/tasksSlice.js'; import { SimpleSpinner, SpinnerOverlay } from '../../components/Spinner.jsx'; import { selectSelectedCamera } from '../projects/projectsSlice.js'; +import { + fetchCameraImageCount, + selectCameraImageCount, + selectCameraImageCountLoading, +} from '../cameras/wirelessCamerasSlice.js'; -const CameraId = styled('span', { +const BoldText = styled('span', { fontWeight: '$5', }); @@ -21,12 +26,19 @@ const DeleteCameraForm = ({ handleClose }) => { const [queuedForClose, setQueuedForClose] = useState(false); const deleteCameraLoading = useSelector(selectDeleteCameraLoading); const selectedCamera = useSelector(selectSelectedCamera); + const imageCount = useSelector(selectCameraImageCount); + const imageCountLoading = useSelector(selectCameraImageCountLoading); const dispatch = useDispatch(); - // TODO: extract into hook? useEffect(() => { - if (queuedForClose) handleClose(); - }, [queuedForClose, handleClose]); + if (imageCount === null && selectedCamera !== null && !imageCountLoading) { + dispatch(fetchCameraImageCount({ cameraId: selectedCamera })); + } + }, [imageCount, selectedCamera, dispatch]); + + useEffect(() => { + if (queuedForClose && !deleteCameraLoading.isLoading) handleClose(); + }, [deleteCameraLoading, queuedForClose, handleClose]); const handleDeleteCameraSubmit = (formVals) => { dispatch(deleteCamera(formVals)); @@ -41,9 +53,11 @@ const DeleteCameraForm = ({ handleClose }) => { } }, [deleteCameraLoading, dispatch]); + const imagesText = `${imageCount} ${imageCount === 1 ? ' image' : ' images'}`; + return (