import React from 'react';
import Polling from './Polling';
import TaskList from './TaskList';

const TaskListWithData = () => {
  return (
    <Polling interval={10000} url="scheduler/tasks">
      {({ data, error }) => {
        if (data) {
          data.sources.sort(sortTasks);
        }
        return <TaskList tasks={data} error={error} />;
      }}
    </Polling>
  );
};

export default TaskListWithData;

const statusScore = {
  running: 2,
  queued: 1,
  none: 0
};

function sortTasks(a, b) {
  const stateA = a.state;
  const stateB = b.state;
  return compareState(stateA, stateB);
}

function compareStatus(a, b) {
  const statusA = a ? a.status : 'none';
  const statusB = b ? b.status : 'none';
  return (statusScore[statusB] || 0) - (statusScore[statusA] || 0);
}

function compareState(stateA, stateB) {
  if (compareStatus(stateA, stateB) === 0) {
    const dateA = stateA ? stateA.date.getTime() : 0;
    const dateB = stateB ? stateB.date.getTime() : 0;
    return dateB - dateA;
  } else {
    return compareStatus(stateA, stateB);
  }
}

@@ -896,38 +896,29 @@
  stateA.date
-.getTime()
  : 
-0
+''
 ;%0A    const 
@@ -949,22 +949,13 @@
 date
-.getTime()
  : 
-0
+''
 ;%0A  
@@ -972,16 +972,29 @@
 ateB
- - 
+.localeCompare(
 dateA
+)
 ;%0A  

task list sort