import React, {useContext, useEffect, useState} from 'react'; import {toast} from "react-toastify"; import {AuthContext} from "../App"; import {useForm} from "react-hook-form"; import {query} from "../auth"; import {Input} from "../custom"; function FileManualForm({id, title = "Upload Individual Entry", defaultData = {}, editType: edit = false}) { const {authState} = useContext(AuthContext) const [manualError, setManualError] = useState("") const {register, handleSubmit, errors, setError, reset, setValue} = useForm(); const manualUpload = async (data) => { const {status, json} = await query("/data/add", data).catch(() => { return {status: 500, json: {reason: "The upload server is offline!"}} }) console.debug("Manual Upload Response:", json) return {status, json} } function submitManual(data) { let req = data let split = data.date.split(/[/-]/); // noinspection JSCheckFunctionSignatures let date = new Date(split[2], Number(split[0]) - 1, split[1]) req.date = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() if (date > new Date()) return setError("date", "time", "Your date must be in the past.") if (date < new Date(1900, 0, 0)) return setError("date", "time", "Your date must be after 1900.") if (date.getDate() === 0) return setError("date", "format", "Invalid Date format (MM-DD-YYYY)") if (isNaN(parseFloat(data["precipitation"]))) { setError("precipitation", "format", "Invalid format. Precipitation must be a number. E.g 0.24") } req.precipitation = parseFloat(data["precipitation"]) req.id = id req.token = authState.token; (async () => { let {json, status} = await manualUpload(data) if (status !== 200) return setManualError(json.reason) reset() if (edit) window.location.reload() toast.success("Entry successfully uploaded") })(); } const formClasses = edit ? "bg-white shadow-lg p-4 overflow-hidden rounded-lg my-6 px-6 max-w-lg w-128" : "bg-white shadow-lg p-4 overflow-hidden rounded-lg my-6 px-6 w-11/12 lg:mx-2 lg:my-5 lg:px-5 lg:w-5/12" useEffect(() => { if (edit) { const date = new Date(defaultData.Date) const formattedDate = (date.getMonth() + 1).toString().padStart(2, "0") + "-" + date.getDate().toString().padStart(2, "0") + "-" + date.getFullYear() setValue([ {date: formattedDate}, {precipitation: defaultData.Precipitation}, {latitude: defaultData.Latitude}, {longitude: defaultData.Longitude}, {remarks: defaultData.Remarks}, ]) } }, [defaultData, edit, setValue]) return (
{title}
{manualError &&
{manualError}
}
); } export default FileManualForm;