|
| 1 | +import NoteControl from "./noteControl"; |
| 2 | +import TaskControl from "./taskControl"; |
| 3 | +import NotesControl from "./notesControl"; |
| 4 | +import TasksControl from "./tasksControl"; |
| 5 | +import { useEffect, useState } from "react"; |
| 6 | +import { getJottings, getSharedJottings } from "../../libs/Datastore/requests"; |
| 7 | +import { useInterval } from "../../libs/delay"; |
| 8 | +import { compareArrays } from "../../libs/arrayExtensions"; |
| 9 | + |
| 10 | +export default function JottingsControl(props) { |
| 11 | + const [notes, setNotes] = useState(null); |
| 12 | + const [tasks, setTasks] = useState(null); |
| 13 | + |
| 14 | + const getJotsToShow = (response, jotType) => { |
| 15 | + if ( |
| 16 | + response[1].status === "rejected" && |
| 17 | + response[0].status === "fulfilled" |
| 18 | + ) |
| 19 | + return response[0].value[jotType]; |
| 20 | + else if ( |
| 21 | + response[1].status === "fulfilled" && |
| 22 | + response[0].status === "fulfilled" |
| 23 | + ) |
| 24 | + return [...response[0].value[jotType], ...response[1].value[jotType]]; |
| 25 | + else if ( |
| 26 | + response[0].status === "rejected" && |
| 27 | + response[1].status === "fulfilled" |
| 28 | + ) |
| 29 | + return response[1].value[jotType]; |
| 30 | + |
| 31 | + return -1; |
| 32 | + }; |
| 33 | + |
| 34 | + const makeJottingsRequests = async (interval) => { |
| 35 | + const ownAbortController = new AbortController(); |
| 36 | + const sharedAbortController = new AbortController(); |
| 37 | + |
| 38 | + if (notes === -1 || tasks === -1) { |
| 39 | + console.info("Interval cleared due to errors"); |
| 40 | + clearInterval(interval); |
| 41 | + } |
| 42 | + |
| 43 | + try { |
| 44 | + const response = Promise.allSettled([ |
| 45 | + getJottings(ownAbortController), |
| 46 | + getSharedJottings(sharedAbortController), |
| 47 | + ]); |
| 48 | + |
| 49 | + console.info("Requests to owned and shared jottings started"); |
| 50 | + console.debug("Response", await response); |
| 51 | + |
| 52 | + const notesToShow = getJotsToShow(await response, "notes"); |
| 53 | + const tasksToShow = getJotsToShow(await response, "tasks"); |
| 54 | + |
| 55 | + console.info("Response received, data computed"); |
| 56 | + console.debug("notesToShow", notesToShow); |
| 57 | + console.debug("tasksToShow", tasksToShow); |
| 58 | + |
| 59 | + const notesToShowIsNewData = |
| 60 | + notes == null || compareArrays(notes, notesToShow); |
| 61 | + |
| 62 | + if (notesToShowIsNewData) { |
| 63 | + setNotes(notesToShow); |
| 64 | + console.info("UI updated"); |
| 65 | + } |
| 66 | + if (tasksToShow != tasks) { |
| 67 | + setTasks(tasksToShow); |
| 68 | + } |
| 69 | + } catch (e) { |
| 70 | + console.error("Request Error:", e); |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | + // componentDidMount() - Load the jottings and recurringly update them with requests |
| 75 | + const updateData = useInterval(() => { |
| 76 | + console.group("Interval Cycle"); |
| 77 | + makeJottingsRequests(updateData).then(() => |
| 78 | + console.groupEnd("Interval Cycle") |
| 79 | + ); |
| 80 | + }, 10000); |
| 81 | + |
| 82 | + return ( |
| 83 | + <> |
| 84 | + <NotesControl notesState={[notes, setNotes]} /> |
| 85 | + <TasksControl tasksState={[tasks, setTasks]} /> |
| 86 | + |
| 87 | + <NoteControl notes={notes} /> |
| 88 | + <TaskControl tasks={tasks} /> |
| 89 | + </> |
| 90 | + ); |
| 91 | +} |
0 commit comments