import { Form, Formik } from "formik"; import FormGroup from "../components/FormGroup"; import type { SoundUploadValue } from "../../../types/types"; import { useSoundContext } from "../../../context/SoundContext"; import { toast } from "sonner"; import { useCameraBlackboard } from "../../../hooks/useCameraBlackboard"; import { useFileUpload } from "../../../hooks/useFileUpload"; const SoundUpload = () => { const { state, dispatch } = useSoundContext(); const { mutation } = useCameraBlackboard(); const { mutation: fileMutation } = useFileUpload({ queryKey: state.sightingSound ? [state.sightingSound] : undefined, }); const initialValues: SoundUploadValue = { name: "", soundFile: null, soundFileName: "", soundUrl: "", }; const handleSubmit = async (values: SoundUploadValue) => { if (!values.soundFile) { toast.warning("Please select an audio file"); return; } const alreadyExists = state?.soundOptions?.some((soundOption) => soundOption.name === values.name); if (state.soundOptions?.includes(values) || alreadyExists) { toast.warning("Sound already in list"); return; } const updatedValues = { ...state, soundOptions: [...(state.soundOptions ?? []), values], }; const result = await mutation.mutateAsync({ operation: "INSERT", path: "soundSettings", value: updatedValues, }); await fileMutation.mutateAsync(values.soundFile); if (result.reason !== "OK") { toast.error("Cannot update sound settings"); } dispatch({ type: "ADD", payload: values }); }; return ( {({ setFieldValue, errors, setFieldError }) => (
{ if (e.target?.files && e.target?.files[0]?.type === "audio/mpeg") { if (e.target.files[0].size / (1024 * 1024) >= 1) { toast.error("File is too large. Max size is 1MB"); return; } const url = URL.createObjectURL(e.target.files[0]); setFieldValue("soundUrl", url); setFieldValue("name", e.target.files[0].name); setFieldValue("soundFileName", e.target.files[0].name); setFieldValue("soundFile", e.target.files[0]); } else { setFieldError("soundFile", "Not an mp3 file"); toast.error("Not an mp3 file"); } }} />

Uploaded Sound files will appear in the drop downs once they are uploaded. They can be used for any Sighting,{" "} Hotlist or NPED hits.

{errors.soundFile &&

Not an mp3 file

}
)}
); }; export default SoundUpload;