Skip to content

Commit 46a9980

Browse files
Merge pull request #244 from ITISFoundation/fix_job_calls
fix: solved main loading issues
2 parents 44004a0 + 17b4e62 commit 46a9980

File tree

1 file changed

+91
-92
lines changed

1 file changed

+91
-92
lines changed

node/src/components/data/JobSelector.tsx

Lines changed: 91 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -116,95 +116,99 @@ export default function JobsSelector(props: JobSelectorPropsType) {
116116
setJobCollections(newJobCollections);
117117
};
118118

119-
async function updateJobCollections(functionUid: string, forceFetch = false) {
120-
console.info("Fetching jobCollections for function: ", functionUid);
121-
if (fetchedJobCollections.length > 0 && !forceFetch) {
122-
console.info("Job collections already fetched, skipping fetch.");
123-
setJobCollections(fetchedJobCollections);
124-
setLoading(false);
125-
return;
126-
}
127-
128-
const jobsC = (await getFunctionJobCollections(functionUid as string)) as FunctionJobCollection[];
119+
const updateJobCollections = useCallback(
120+
async (functionUid: string, forceFetch = false) => {
121+
console.info("Fetching jobCollections for function: ", functionUid);
122+
if (fetchedJobCollections.length > 0 && !forceFetch) {
123+
console.info("Job collections already fetched, skipping fetch.");
124+
setJobCollections(fetchedJobCollections);
125+
setLoading(false);
126+
return;
127+
}
129128

130-
const equalJC =
131-
fetchedJobCollections.length === jobsC.length &&
132-
fetchedJobCollections
133-
.map((jc, idx) => jc.jobCollection.jobIds.join(",") === jobsC[idx].jobIds.join(","))
134-
.every(v => v === true);
129+
const jobsC = (await getFunctionJobCollections(functionUid as string)) as FunctionJobCollection[];
135130

136-
if (equalJC) {
137-
console.info("Job collections already fetched, skipping fetch.");
138-
setJobCollections(fetchedJobCollections);
139-
setLoading(false);
140-
return;
141-
}
131+
const equalJC =
132+
fetchedJobCollections.length === jobsC.length &&
133+
fetchedJobCollections
134+
.map((jc, idx) => jc.jobCollection.jobIds.join(",") === jobsC[idx].jobIds.join(","))
135+
.every(v => v === true);
142136

143-
if (forceFetch) {
144-
setLoading(true);
145-
setJobCollections([]);
146-
setProgress(0);
147-
setJobProgress(0);
148-
jobsFetched.current = 0;
149-
colsFetched.current = 0;
150-
}
137+
if (equalJC) {
138+
console.info("Job collections already fetched, skipping fetch.");
139+
setJobCollections(fetchedJobCollections);
140+
setLoading(false);
141+
return;
142+
}
151143

152-
const totalSubs = jobsC.reduce((acc, jc) => acc + jc.jobIds.length, 0);
153-
colsFetched.current = 0;
154-
jobsFetched.current = 0;
155-
console.info("Fetched jobCollections: ", jobsC, totalSubs);
144+
if (forceFetch) {
145+
setLoading(true);
146+
setJobCollections([]);
147+
setProgress(0);
148+
setJobProgress(0);
149+
jobsFetched.current = 0;
150+
colsFetched.current = 0;
151+
}
156152

157-
if (jobsC.length === 0) {
158-
console.info("No job collections found for function: ", functionUid);
159-
setJobCollections([]);
160-
setFetchedJobCollections([]);
161-
setLoading(false);
162-
return;
163-
}
153+
const totalSubs = jobsC.reduce((acc, jc) => acc + jc.jobIds.length, 0);
154+
colsFetched.current = 0;
155+
jobsFetched.current = 0;
156+
console.info("Fetched jobCollections: ", jobsC, totalSubs);
157+
158+
if (jobsC.length === 0) {
159+
console.info("No job collections found for function: ", functionUid);
160+
setJobCollections([]);
161+
setFetchedJobCollections([]);
162+
setLoading(false);
163+
return;
164+
}
164165

165-
const newJobCollections: SelectedJobCollection[] = [];
166-
167-
for (let jcIdx = 0; jcIdx < jobsC.length; jcIdx += 1) {
168-
const jc = jobsC[jcIdx];
169-
const subJobs = [];
170-
for (let subJobIdx = 0; subJobIdx < jc.jobIds.length; subJobIdx += 1) {
171-
let job: FunctionJob;
172-
const id = jc.jobIds[subJobIdx];
173-
// check if job is already fetched in fetchedJobCollections
174-
const existingJob = fetchedJobCollections.find(
175-
j =>
176-
j.jobCollection.jobIds.includes(id) &&
177-
j.subJobs.some(sj => sj.job.uid === id && (sj.job.status === "FAILED" || sj.job.status === "SUCCESS")),
178-
);
179-
if (existingJob) {
180-
// console.info("Job already fetched: ", id, existingJob.subJobs.find((j) => j.job.uid === id));
181-
job = existingJob.subJobs.find(j => j.job.uid === id)?.job;
182-
} else {
183-
job = await getFunctionJob(id);
166+
const newJobCollections: SelectedJobCollection[] = [];
167+
168+
for (let jcIdx = 0; jcIdx < jobsC.length; jcIdx += 1) {
169+
const jc = jobsC[jcIdx];
170+
const subJobs = [];
171+
for (let subJobIdx = 0; subJobIdx < jc.jobIds.length; subJobIdx += 1) {
172+
let job: FunctionJob;
173+
const id = jc.jobIds[subJobIdx];
174+
// check if job is already fetched in fetchedJobCollections
175+
const existingJob = fetchedJobCollections.find(
176+
j =>
177+
j.jobCollection.jobIds.includes(id) &&
178+
j.subJobs.some(sj => sj.job.uid === id && (sj.job.status === "FAILED" || sj.job.status === "SUCCESS")),
179+
);
180+
if (existingJob) {
181+
// console.info("Job already fetched: ", id, existingJob.subJobs.find((j) => j.job.uid === id));
182+
job = existingJob.subJobs.find(j => j.job.uid === id)?.job;
183+
} else {
184+
job = await getFunctionJob(id);
185+
}
186+
jobsFetched.current += 1;
187+
const jobsProg = (jobsFetched.current / totalSubs) * 100;
188+
setJobProgress(jobsProg);
189+
subJobs.push({
190+
selected: false,
191+
job,
192+
});
184193
}
185-
jobsFetched.current += 1;
186-
const jobsProg = (jobsFetched.current / totalSubs) * 100;
187-
setJobProgress(jobsProg);
188-
subJobs.push({
194+
console.info("Fetched subJobs for jobCollection: ", progress, jobProgress, jobsFetched.current);
195+
colsFetched.current += jc.jobIds.length;
196+
setProgress((colsFetched.current / totalSubs) * 100);
197+
newJobCollections.push({
198+
jobCollection: jc,
189199
selected: false,
190-
job,
200+
subJobs,
191201
});
192202
}
193-
console.info("Fetched subJobs for jobCollection: ", progress, jobProgress, jobsFetched.current);
194-
colsFetched.current += jc.jobIds.length;
195-
setProgress((colsFetched.current / totalSubs) * 100);
196-
newJobCollections.push({
197-
jobCollection: jc,
198-
selected: false,
199-
subJobs,
200-
});
201-
}
202203

203-
setJobCollections(newJobCollections);
204-
setFetchedJobCollections(newJobCollections);
205-
updateJobContext(newJobCollections);
206-
setProgress(100);
207-
}
204+
setJobCollections(newJobCollections);
205+
setFetchedJobCollections(newJobCollections);
206+
updateJobContext(newJobCollections);
207+
setProgress(100);
208+
},
209+
// eslint-disable-next-line react-hooks/exhaustive-deps
210+
[fetchedJobCollections],
211+
);
208212

209213
const openJobCollection = (uid: string) => {
210214
const idx = jobCollections.findIndex(jc => jc.jobCollection.uid === uid);
@@ -279,6 +283,12 @@ export default function JobsSelector(props: JobSelectorPropsType) {
279283
updateJobContext(newJobCollections);
280284
}, [jobCollections, updateJobContext]);
281285

286+
const handleJobsUpdate = useCallback(async () => {
287+
setJobCollections([]);
288+
await updateJobCollections(selectedFunction?.uid as string);
289+
console.info("Updated JobCollections");
290+
}, [selectedFunction, updateJobCollections]);
291+
282292
useEffect(() => {
283293
if (jobCollections.length > 0 && loading === true) {
284294
onToggleAll(true);
@@ -288,23 +298,12 @@ export default function JobsSelector(props: JobSelectorPropsType) {
288298
}, [jobCollections, loading, onToggleAll, setIsSuMoGenerated, setLoading, updateJobContext]);
289299

290300
useEffect(() => {
291-
console.info("useEffect in JobsSelector triggered");
292-
if (selectedFunction === undefined || jobCollections.length === 0) {
301+
console.info("useEffect in JobsSelector triggered", selectedFunction, jobCollections);
302+
if (selectedFunction === undefined || jobCollections.length > 0) {
293303
return;
294304
}
295305
console.info("Function selected: ", selectedFunction.uid);
296-
(async () => {
297-
setJobCollections([]);
298-
await updateJobCollections(selectedFunction?.uid as string);
299-
console.info("Updated JobCollections");
300-
})();
301-
302-
console.info("Function selected: ", selectedFunction.uid);
303-
(async () => {
304-
setJobCollections([]);
305-
await updateJobCollections(selectedFunction?.uid as string);
306-
console.info("Updated JobCollections");
307-
})();
306+
handleJobsUpdate();
308307
// eslint-disable-next-line react-hooks/exhaustive-deps
309308
}, [selectedFunction]);
310309

0 commit comments

Comments
 (0)