1
0
mirror of https://github.com/ansible/awx.git synced 2024-10-30 22:21:13 +03:00

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,
};
export default function sortJobs(jobs, orderBy) {
const key = orderBy.replace('-', '');
export default function sortJobs(jobs, params) {
const { order_by = '-finished', page_size = 20 } = params;
const key = order_by.replace('-', '');
const fn = sortFns[key];
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) {
@ -33,10 +35,10 @@ function byFinished(a, b) {
function byStarted(a, b) {
if (!a.started) {
return -1;
return 1;
}
if (!b.started) {
return 1;
return -1;
}
return sort(new Date(a.started), new Date(b.started));
}
@ -50,15 +52,13 @@ function byName(a, b) {
}
function byCreatedBy(a, b) {
const nameA = a.summary_fields?.created_by?.username;
const nameB = b.summary_fields?.created_by?.username;
return sort(nameA, nameB);
const nameA = a.summary_fields?.created_by?.id;
const nameB = b.summary_fields?.created_by?.id;
return sort(nameA, nameB) * -1;
}
function byProject(a, b) {
const projA = a.summary_fields?.project?.id;
const projB = b.summary_fields?.project?.id;
return sort(projA, projB);
return sort(a.unified_job_template, b.unified_job_template);
}
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)
);
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
@ -54,7 +55,7 @@ export default function useWsJobs(initialJobs, fetchJobsById, qsConfig) {
const jobId = lastMessage.unified_job_id;
const index = jobs.findIndex(j => j.id === jobId);
if (index > -1) {
setJobs(sortJobs(updateJob(jobs, index, lastMessage), params.order_by));
setJobs(sortJobs(updateJob(jobs, index, lastMessage), params));
} else {
enqueueJobId(lastMessage.unified_job_id);
}