import React, {useCallback, useContext, useEffect, useReducer, useState} from 'react'; import Navbar from "./Navbar"; import {AuthContext} from "../App"; import Griddle, {ColumnDefinition, plugins, RowDefinition} from "griddle-react"; import {query} from "../auth"; import "flatpickr/dist/themes/material_blue.css"; import "../styles/tables.css" import Flatpickr from "react-flatpickr"; import {Input} from "../custom"; import {toast} from "react-toastify"; import {AiFillDelete, AiFillEdit} from "react-icons/ai"; import Modal from "react-modal" import FileManualForm from "./FileManualForm"; const background = require('../assets/wave.png') const defaultData = () => [{ ID: "No Data Found", Date: "", Precipitation: "", Latitude: "", Longitude: "", Remarks: "", Edit: "", }] function MyData({history, ...props}) { const {authState} = useContext(AuthContext) const [data, setData] = useState(defaultData()) const [items, setItems] = useState(10) const [status, setStatus] = useState("") const initialDates = () => { const now = new Date() const lastYear = new Date(); lastYear.setFullYear(lastYear.getFullYear() - 1) return [lastYear, now] } const [dateRange, setDateRange] = useState([]); const [editModal, setEditModal] = useState({open: false, data: {}}) const [updateData, dispatchEdit] = useReducer((state, action) => action, []) useEffect(() => { const params = new URLSearchParams(props.location.search) let range = initialDates() if (params.get("after")) range[0] = new Date(params.get("after")) if (params.get("before")) range[1] = new Date(params.get("before")) setDateRange(range) }, [props.location]) useEffect(() => { if (!authState.loggedIn) history.push("/login?return=mydata") }, [authState, history]) const deleteRow = async (data) => { const {status, json} = await query("/data/delete", data).catch(e => { console.debug(e) return {status: 500, json: {reason: "The upload server is offline!"}} }) console.debug("Delete Row Response", json) return {status, json} } const initiateData = useCallback(async () => { if (dateRange.length !== 2 || !authState.username) return let after = dateRange[0] let before = dateRange[1] before.setDate(before.getDate() + 1) after.setDate(after.getDate() - 1) const now = new Date(); now.setDate(now.getDate() + 5) const hundred = new Date(1900, 0, 0) if (after > now || after < hundred || before > now || before < hundred) return toast.error("Invalid Date Range!") setStatus("Loading Data...") let {json, status} = await query("/data/query", { token: authState.token, after_date: after, before_date: before, username: authState.username, }).catch(() => ({status: 500, json: {reason: "The authentication server is offline!"}})) setStatus("") if (status !== 200) return toast.error(json.reason) let newData = defaultData() const formatDate = (date) => { return date.getFullYear() + "-" + (date.getMonth() + 1).toString().padStart(2, "0") + "-" + date.getDate().toString().padStart(2, "0") } json.entries.forEach((entry, index) => { const date = new Date(entry.date); const group = json.groups.find(gr => gr.id === entry.group_id) newData[index] = { ID: entry.id, Date: formatDate(date), Precipitation: entry.precipitation, Latitude: group.latitude, Longitude: group.longitude, Verified: group.validated ? "Yes" : "No", Remarks: entry.remarks, } }) setData(newData) before.setDate(before.getDate() - 1) after.setDate(after.getDate() + 1) window.history.pushState("Search", "Rain Track", "/mydata?after=" + formatDate(after) + "&before=" + formatDate(before)) }, [setData, authState.token, dateRange, authState.username]) useEffect(() => { (async () => { initiateData() })(); }, [setData, initiateData, dateRange, authState.username]) const styleConfig = { classNames: { Filter: "input h-8", Table: "table table-bordered table-hover table-striped lg:overflow-x-auto overflow-y-scroll", PreviousButton: "border border-gray-300 px-2 py-1 w-20 mr-2 rounded", NextButton: "border border-gray-300 px-2 py-1 w-20 rounded ml-2" } } useEffect(() => { if (updateData.length !== 0) { const row = data[updateData[1]] if (!row) return switch (updateData[0]) { case 0: setEditModal({open: true, data: row}) break; case 1: window.confirm("Are you sure you would like to delete this row?") && (async () => { let {json, status} = await deleteRow({token: authState.token, id: row.ID}) if (status !== 200) return toast.error(json.reason) toast.success("Entry successfully deleted") dispatchEdit([]) setData(data.filter(r => r !== row)) })() break; default: break; } } }, [updateData, data, authState]) return (
setDateRange(date)} options={{ dateFormat: "m-d-Y", mode: "range", wrap: true, position: "below" }}>
Select Date Range
{status}

}/>
setEditModal({open: false, data: {ID: 0}})} shouldCloseOnOverlayClick isOpen={editModal.open}>
); } export default MyData; const Layout = (setItems) => (({Table, Filter, Pagination}) => (
)) const EditComponent = ({griddleKey, dispatch}) => { return
dispatch([0, griddleKey])}> dispatch([1, griddleKey])}>
}