fix sorting jobs by finished date

This commit is contained in:
Keith Grant
2020-06-25 10:23:15 -07:00
parent 25fe090e67
commit 5610309a88
2 changed files with 33 additions and 3 deletions

View File

@@ -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 {

View File

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