11import NoteControl from "./noteControl" ;
22import TaskControl from "./taskControl" ;
3+ import LabelControl from "./labelControl" ;
34import NotesControl from "./notesControl" ;
45import TasksControl from "./tasksControl" ;
6+ import LabelsControl from "./labelsControl" ;
57import { useEffect , useState } from "react" ;
6- import { getJottings , getSharedJottings } from "../../libs/Datastore/requests" ;
8+ import { getJottings , getSharedJottings , getLabels , getLabel } from "../../libs/Datastore/requests" ;
79import { useInterval } from "../../libs/delay" ;
810import { compareArrays } from "../../libs/arrayExtensions" ;
11+ import UrlService from "../../libs/UrlService" ;
12+ import { useRouter } from "next/router" ;
913
1014export default function JottingsControl ( props ) {
1115 const [ notes , setNotes ] = useState ( null ) ;
1216 const [ tasks , setTasks ] = useState ( null ) ;
13-
17+ const [ labels , setLabels ] = useState ( null ) ;
18+
19+ const urlParser = new URLSearchParams ( location . search ) ;
20+
1421 const getJotsToShow = ( response , jotType ) => {
1522 if (
1623 response [ 1 ] . status === "rejected" &&
@@ -41,27 +48,26 @@ export default function JottingsControl(props) {
4148 }
4249
4350 try {
44- const response = Promise . allSettled ( [
45- getJottings ( ownAbortController ) ,
46- getSharedJottings ( sharedAbortController ) ,
47- ] ) ;
51+ let promisesToSettle = [ getJottings ( ownAbortController ) , getSharedJottings ( sharedAbortController ) ] ;
52+
53+ const response = Promise . allSettled ( promisesToSettle ) ;
4854
4955 console . info ( "Requests to owned and shared jottings started" ) ;
5056 console . debug ( "Response" , await response ) ;
5157
5258 const notesToShow = getJotsToShow ( await response , "notes" ) ;
5359 const tasksToShow = getJotsToShow ( await response , "tasks" ) ;
5460
55- console . info ( "Response received, data computed" ) ;
56- console . debug ( "notesToShow" , notesToShow ) ;
57- console . debug ( "tasksToShow" , tasksToShow ) ;
61+ console . log ( "Response received, data computed" ) ;
62+ console . info ( "notesToShow" , notesToShow ) ;
63+ console . info ( "tasksToShow" , tasksToShow ) ;
5864
5965 const notesToShowIsNewData =
6066 notes == null || compareArrays ( notes , notesToShow ) ;
6167
6268 if ( notesToShowIsNewData ) {
6369 setNotes ( notesToShow ) ;
64- console . info ( "UI updated" ) ;
70+ console . log ( "UI updated" ) ;
6571 }
6672 if ( tasksToShow != tasks ) {
6773 setTasks ( tasksToShow ) ;
@@ -71,21 +77,50 @@ export default function JottingsControl(props) {
7177 }
7278 } ;
7379
80+ const makeLabelsRequest = async ( interval ) => {
81+ const abortController = new AbortController ( ) ;
82+
83+ if ( labels === - 1 ) {
84+ console . error ( "[makeLabelsRequest] Interval cleared due to errors" ) ;
85+ clearInterval ( interval ) ;
86+ }
87+
88+ try {
89+ const response = await getLabels ( abortController ) ;
90+ console . log ( "Requests to labels started" ) ;
91+ console . log ( "labels" , response ) ;
92+
93+ const labelsToShowIsNewData = labels == null || compareArrays ( labels , response ) ;
94+
95+ if ( labelsToShowIsNewData ) {
96+ setLabels ( response ) ;
97+ console . info ( "labels" , labels ) ;
98+ console . log ( "UI updated" ) ;
99+ }
100+ } catch ( e ) {
101+ console . error ( "Request Error:" , e ) ;
102+ setLabels ( - 1 ) ;
103+ }
104+ } ;
105+
74106 // componentDidMount() - Load the jottings and recurringly update them with requests
75107 const updateData = useInterval ( ( ) => {
76108 console . group ( "Interval Cycle" ) ;
77- makeJottingsRequests ( updateData ) . then ( ( ) =>
78- console . groupEnd ( "Interval Cycle" )
79- ) ;
109+ Promise . all ( [
110+ makeJottingsRequests ( updateData ) ,
111+ makeLabelsRequest ( updateData )
112+ ] ) . then ( ( ) => console . groupEnd ( "Interval Cycle" ) )
80113 } , 10000 ) ;
81114
82115 return (
83116 < >
117+ < LabelsControl labelsState = { [ labels , setLabels ] } />
84118 < NotesControl notesState = { [ notes , setNotes ] } />
85119 < TasksControl tasksState = { [ tasks , setTasks ] } />
86-
120+
87121 < NoteControl notes = { notes } />
88122 < TaskControl tasks = { tasks } />
123+ < LabelControl labels = { labels } />
89124 </ >
90125 ) ;
91126}
0 commit comments