clean up sorting order discrepancies

This commit is contained in:
Keith Grant
2020-06-26 12:00:11 -07:00
parent 9705f7bec6
commit a3e0ae66ba
2 changed files with 15 additions and 14 deletions

View File

@@ -7,14 +7,16 @@ const sortFns = {
started: byStarted, started: byStarted,
}; };
export default function sortJobs(jobs, orderBy) { export default function sortJobs(jobs, params) {
const key = orderBy.replace('-', ''); const { order_by = '-finished', page_size = 20 } = params;
const key = order_by.replace('-', '');
const fn = sortFns[key]; const fn = sortFns[key];
if (!fn) { if (!fn) {
return jobs; return jobs.slice(0, page_size);
} }
return orderBy[0] === '-' ? jobs.sort(reverse(fn)) : jobs.sort(fn); const sorted = order_by[0] === '-' ? jobs.sort(reverse(fn)) : jobs.sort(fn);
return sorted.slice(0, page_size);
} }
function reverse(fn) { function reverse(fn) {
@@ -33,10 +35,10 @@ function byFinished(a, b) {
function byStarted(a, b) { function byStarted(a, b) {
if (!a.started) { if (!a.started) {
return -1; return 1;
} }
if (!b.started) { if (!b.started) {
return 1; return -1;
} }
return sort(new Date(a.started), new Date(b.started)); return sort(new Date(a.started), new Date(b.started));
} }
@@ -50,15 +52,13 @@ function byName(a, b) {
} }
function byCreatedBy(a, b) { function byCreatedBy(a, b) {
const nameA = a.summary_fields?.created_by?.username; const nameA = a.summary_fields?.created_by?.id;
const nameB = b.summary_fields?.created_by?.username; const nameB = b.summary_fields?.created_by?.id;
return sort(nameA, nameB); return sort(nameA, nameB) * -1;
} }
function byProject(a, b) { function byProject(a, b) {
const projA = a.summary_fields?.project?.id; return sort(a.unified_job_template, b.unified_job_template);
const projB = b.summary_fields?.project?.id;
return sort(projA, projB);
} }
function sort(a, b) { function sort(a, b) {

View File

@@ -31,7 +31,8 @@ export default function useWsJobs(initialJobs, fetchJobsById, qsConfig) {
job => !jobs.find(j => j.id === job.id) job => !jobs.find(j => j.id === job.id)
); );
if (deduplicated.length) { if (deduplicated.length) {
setJobs([...deduplicated, ...jobs]); const params = parseQueryString(qsConfig, location.search);
setJobs(sortJobs([...deduplicated, ...jobs], params));
} }
})(); })();
}, [throttledJobsToFetch, fetchJobsById]); // eslint-disable-line react-hooks/exhaustive-deps }, [throttledJobsToFetch, fetchJobsById]); // eslint-disable-line react-hooks/exhaustive-deps
@@ -54,7 +55,7 @@ export default function useWsJobs(initialJobs, fetchJobsById, qsConfig) {
const jobId = lastMessage.unified_job_id; const jobId = lastMessage.unified_job_id;
const index = jobs.findIndex(j => j.id === jobId); const index = jobs.findIndex(j => j.id === jobId);
if (index > -1) { if (index > -1) {
setJobs(sortJobs(updateJob(jobs, index, lastMessage), params.order_by)); setJobs(sortJobs(updateJob(jobs, index, lastMessage), params));
} else { } else {
enqueueJobId(lastMessage.unified_job_id); enqueueJobId(lastMessage.unified_job_id);
} }