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"; const SoundUpload = () => { const { state, dispatch } = useSoundContext(); const { mutation } = useCameraBlackboard(); 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, }); if (result.reason !== "OK") { toast.error("Cannot update sound settings"); } else { toast.success(`${values.name} file added`); } dispatch({ type: "ADD", payload: values }); }; return ( {({ setFieldValue, errors, setFieldError, values }) => (
{ if (e.target?.files && e.target?.files[0]?.type === "audio/mpeg") { 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"); } }} />
{!values.soundFile && (
No uploaded sound files
)}

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;