From 9705f7bec63499af3f34878a99df8320bd218d20 Mon Sep 17 00:00:00 2001 From: Keith Grant Date: Thu, 25 Jun 2020 16:23:38 -0700 Subject: [PATCH] sort jobs list by selection sort option --- .../src/components/JobList/sortJobs.js | 78 +++++++++++++++++++ .../src/components/JobList/useWsJobs.js | 26 +------ 2 files changed, 79 insertions(+), 25 deletions(-) create mode 100644 awx/ui_next/src/components/JobList/sortJobs.js diff --git a/awx/ui_next/src/components/JobList/sortJobs.js b/awx/ui_next/src/components/JobList/sortJobs.js new file mode 100644 index 0000000000..9b761f1c90 --- /dev/null +++ b/awx/ui_next/src/components/JobList/sortJobs.js @@ -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; +} diff --git a/awx/ui_next/src/components/JobList/useWsJobs.js b/awx/ui_next/src/components/JobList/useWsJobs.js index 0733e7bfc6..c06ea7c88f 100644 --- a/awx/ui_next/src/components/JobList/useWsJobs.js +++ b/awx/ui_next/src/components/JobList/useWsJobs.js @@ -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; - }); -}