sort jobs list by selection sort option

This commit is contained in:
Keith Grant 2020-06-25 16:23:38 -07:00
parent 5610309a88
commit 9705f7bec6
2 changed files with 79 additions and 25 deletions

View 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;
}

View File

@ -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;
});
}