mirror of
https://github.com/ansible/awx.git
synced 2026-01-11 10:00:01 -03:30
sort jobs list by selection sort option
This commit is contained in:
parent
5610309a88
commit
9705f7bec6
78
awx/ui_next/src/components/JobList/sortJobs.js
Normal file
78
awx/ui_next/src/components/JobList/sortJobs.js
Normal file
@ -0,0 +1,78 @@
|
||||
const sortFns = {
|
||||
finished: byFinished,
|
||||
id: byId,
|
||||
name: byName,
|
||||
created_by__id: byCreatedBy,
|
||||
unified_job_template__project__id: byProject,
|
||||
started: byStarted,
|
||||
};
|
||||
|
||||
export default function sortJobs(jobs, orderBy) {
|
||||
const key = orderBy.replace('-', '');
|
||||
const fn = sortFns[key];
|
||||
if (!fn) {
|
||||
return jobs;
|
||||
}
|
||||
|
||||
return orderBy[0] === '-' ? jobs.sort(reverse(fn)) : jobs.sort(fn);
|
||||
}
|
||||
|
||||
function reverse(fn) {
|
||||
return (a, b) => fn(a, b) * -1;
|
||||
}
|
||||
|
||||
function byFinished(a, b) {
|
||||
if (!a.finished) {
|
||||
return 1;
|
||||
}
|
||||
if (!b.finished) {
|
||||
return -1;
|
||||
}
|
||||
return sort(new Date(a.finished), new Date(b.finished));
|
||||
}
|
||||
|
||||
function byStarted(a, b) {
|
||||
if (!a.started) {
|
||||
return -1;
|
||||
}
|
||||
if (!b.started) {
|
||||
return 1;
|
||||
}
|
||||
return sort(new Date(a.started), new Date(b.started));
|
||||
}
|
||||
|
||||
function byId(a, b) {
|
||||
return sort(a.id, b.id);
|
||||
}
|
||||
|
||||
function byName(a, b) {
|
||||
return sort(a.name, b.name);
|
||||
}
|
||||
|
||||
function byCreatedBy(a, b) {
|
||||
const nameA = a.summary_fields?.created_by?.username;
|
||||
const nameB = b.summary_fields?.created_by?.username;
|
||||
return sort(nameA, nameB);
|
||||
}
|
||||
|
||||
function byProject(a, b) {
|
||||
const projA = a.summary_fields?.project?.id;
|
||||
const projB = b.summary_fields?.project?.id;
|
||||
return sort(projA, projB);
|
||||
}
|
||||
|
||||
function sort(a, b) {
|
||||
if (!a) {
|
||||
return -1;
|
||||
}
|
||||
if (!b) {
|
||||
return 1;
|
||||
}
|
||||
if (a < b) {
|
||||
return -1;
|
||||
}
|
||||
if (a > b) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -2,6 +2,7 @@ import { useState, useEffect, useRef } from 'react';
|
||||
import { useLocation } from 'react-router-dom';
|
||||
import useThrottle from './useThrottle';
|
||||
import { parseQueryString } from '../../util/qs';
|
||||
import sortJobs from './sortJobs';
|
||||
|
||||
export default function useWsJobs(initialJobs, fetchJobsById, qsConfig) {
|
||||
const location = useLocation();
|
||||
@ -115,28 +116,3 @@ function updateJob(jobs, index, message) {
|
||||
};
|
||||
return [...jobs.slice(0, index), job, ...jobs.slice(index + 1)];
|
||||
}
|
||||
|
||||
function sortJobs(jobs, orderBy) {
|
||||
if (orderBy !== '-finished') {
|
||||
return jobs;
|
||||
}
|
||||
|
||||
return jobs.sort((a, b) => {
|
||||
if (!a.finished) {
|
||||
return -1;
|
||||
}
|
||||
if (!b.finished) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
const dateA = new Date(a.finished);
|
||||
const dateB = new Date(b.finished);
|
||||
if (dateA < dateB) {
|
||||
return 1;
|
||||
}
|
||||
if (dateA > dateB) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user