From 5610309a88a8b704950cc28455ecf8c34eaeac7c Mon Sep 17 00:00:00 2001 From: Keith Grant Date: Thu, 25 Jun 2020 10:23:15 -0700 Subject: [PATCH] fix sorting jobs by finished date --- .../src/components/JobList/JobList.jsx | 2 +- .../src/components/JobList/useWsJobs.js | 34 +++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/awx/ui_next/src/components/JobList/JobList.jsx b/awx/ui_next/src/components/JobList/JobList.jsx index 029e42d0cd..6e6d57653b 100644 --- a/awx/ui_next/src/components/JobList/JobList.jsx +++ b/awx/ui_next/src/components/JobList/JobList.jsx @@ -68,7 +68,7 @@ function JobList({ i18n, defaultParams, showTypeColumn = false }) { [location.search] // eslint-disable-line react-hooks/exhaustive-deps ); - const jobs = useWsJobs(results, fetchJobsById, !!defaultParams); + const jobs = useWsJobs(results, fetchJobsById, QS_CONFIG); const isAllSelected = selected.length === jobs.length && selected.length > 0; const { diff --git a/awx/ui_next/src/components/JobList/useWsJobs.js b/awx/ui_next/src/components/JobList/useWsJobs.js index e1d8d2d033..0733e7bfc6 100644 --- a/awx/ui_next/src/components/JobList/useWsJobs.js +++ b/awx/ui_next/src/components/JobList/useWsJobs.js @@ -1,7 +1,10 @@ import { useState, useEffect, useRef } from 'react'; +import { useLocation } from 'react-router-dom'; import useThrottle from './useThrottle'; +import { parseQueryString } from '../../util/qs'; -export default function useWsJobs(initialJobs, fetchJobsById, filtersApplied) { +export default function useWsJobs(initialJobs, fetchJobsById, qsConfig) { + const location = useLocation(); const [jobs, setJobs] = useState(initialJobs); const [lastMessage, setLastMessage] = useState(null); const [jobsToFetch, setJobsToFetch] = useState([]); @@ -38,6 +41,8 @@ export default function useWsJobs(initialJobs, fetchJobsById, filtersApplied) { if (!lastMessage || !lastMessage.unified_job_id) { return; } + const params = parseQueryString(qsConfig, location.search); + const filtersApplied = Object.keys(params).length > 4; if ( filtersApplied && !['completed', 'failed', 'error'].includes(lastMessage.status) @@ -48,7 +53,7 @@ export default function useWsJobs(initialJobs, fetchJobsById, filtersApplied) { const jobId = lastMessage.unified_job_id; const index = jobs.findIndex(j => j.id === jobId); if (index > -1) { - setJobs(updateJob(jobs, index, lastMessage)); + setJobs(sortJobs(updateJob(jobs, index, lastMessage), params.order_by)); } else { enqueueJobId(lastMessage.unified_job_id); } @@ -110,3 +115,28 @@ 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; + }); +}