2025-09-30 13:25:11 +01:00
|
|
|
import { Form, Formik } from "formik";
|
|
|
|
|
import FormGroup from "../components/FormGroup";
|
|
|
|
|
import type { SoundUploadValue } from "../../../types/types";
|
2025-10-01 15:21:07 +01:00
|
|
|
import { useSoundContext } from "../../../context/SoundContext";
|
|
|
|
|
import { toast } from "sonner";
|
2025-09-30 13:25:11 +01:00
|
|
|
|
|
|
|
|
const SoundUpload = () => {
|
2025-10-01 15:21:07 +01:00
|
|
|
const { dispatch } = useSoundContext();
|
2025-09-30 13:25:11 +01:00
|
|
|
const initialValues: SoundUploadValue = {
|
2025-10-01 15:21:07 +01:00
|
|
|
name: "",
|
2025-09-30 13:25:11 +01:00
|
|
|
soundFile: null,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleSubmit = (values: SoundUploadValue) => {
|
2025-10-01 15:21:07 +01:00
|
|
|
if (!values.soundFile) {
|
|
|
|
|
toast.warning("Please select an audio file");
|
|
|
|
|
} else {
|
|
|
|
|
dispatch({ type: "ADD", payload: values });
|
|
|
|
|
toast.success("Sound file upload successfully");
|
|
|
|
|
}
|
2025-09-30 13:25:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2025-10-01 15:21:07 +01:00
|
|
|
<Formik
|
|
|
|
|
initialValues={initialValues}
|
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
|
enableReinitialize
|
|
|
|
|
>
|
|
|
|
|
{({ setFieldValue, errors, setFieldError }) => (
|
2025-09-30 13:25:11 +01:00
|
|
|
<Form>
|
|
|
|
|
<FormGroup>
|
2025-10-01 15:21:07 +01:00
|
|
|
<label htmlFor="soundFile">Sound File</label>
|
2025-09-30 13:25:11 +01:00
|
|
|
<input
|
|
|
|
|
type="file"
|
|
|
|
|
name="soundFile"
|
|
|
|
|
id="sightingSoundinput"
|
2025-10-01 15:21:07 +01:00
|
|
|
accept="audio/mpeg"
|
2025-09-30 13:25:11 +01:00
|
|
|
onChange={(e) => {
|
|
|
|
|
if (
|
2025-10-01 15:21:07 +01:00
|
|
|
e.target?.files &&
|
|
|
|
|
e.target?.files[0]?.type === "audio/mpeg"
|
|
|
|
|
) {
|
|
|
|
|
setFieldValue("name", e.target.files[0].name);
|
|
|
|
|
setFieldValue("soundFile", e.target.files[0]);
|
|
|
|
|
} else {
|
|
|
|
|
setFieldError("soundFile", "Not an mp3 file");
|
|
|
|
|
toast.error("Not an mp3 file");
|
|
|
|
|
}
|
2025-09-30 13:25:11 +01:00
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</FormGroup>
|
2025-10-01 15:21:07 +01:00
|
|
|
{errors.soundFile && (
|
|
|
|
|
<p className="text-red-500 text-sm mt-1">Not an mp3 file</p>
|
|
|
|
|
)}
|
2025-09-30 13:25:11 +01:00
|
|
|
<button
|
|
|
|
|
type="submit"
|
|
|
|
|
className="w-1/4 text-white bg-green-700 hover:bg-green-800 font-small rounded-lg text-sm px-2 py-2.5"
|
2025-10-01 15:21:07 +01:00
|
|
|
disabled={errors.soundFile ? true : false}
|
2025-09-30 13:25:11 +01:00
|
|
|
>
|
|
|
|
|
Upload
|
|
|
|
|
</button>
|
|
|
|
|
</Form>
|
|
|
|
|
)}
|
|
|
|
|
</Formik>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default SoundUpload;
|