From c71fdf34cbef57f9002afca010b0e2d4fdaee0ef Mon Sep 17 00:00:00 2001 From: HF Date: Wed, 3 Aug 2022 01:41:01 +0200 Subject: [PATCH] add sorting --- src/components/ModWatchtools.jsx | 41 ++++++++++++++++++++++++++++---- src/components/Modtools.jsx | 2 +- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/components/ModWatchtools.jsx b/src/components/ModWatchtools.jsx index a61170b..54f0d44 100644 --- a/src/components/ModWatchtools.jsx +++ b/src/components/ModWatchtools.jsx @@ -14,6 +14,9 @@ const keepState = { iid: '', }; +/* + * parse interval in s/m/h to timestamp + */ function parseInterval(interval) { if (!interval) { return null; @@ -33,6 +36,20 @@ function parseInterval(interval) { return Date.now() - (num * factor); } +/* + * sorting function for array sort + */ +function compare(a, b) { + if (typeof a === 'string' && typeof b === 'string') { + return a.localeCompare(b); + } + if (a === 'N/A') a = 0; + if (b === 'N/A') b = 0; + if (a < b) return -1; + if (a > b) return 1; + return 0; +} + async function submitWatchAction( action, canvas, @@ -75,6 +92,7 @@ function ModWatchtools() { const [tlcoords, selectTLCoords] = useState(keepState.tlcoords); const [brcoords, selectBRCoords] = useState(keepState.brcoords); const [interval, selectInterval] = useState(keepState.interval); + const [sortBy, setSortBy] = useState(0); const [table, setTable] = useState(null); const [iid, selectIid] = useState(keepState.iid); const [resp, setResp] = useState(null); @@ -247,6 +265,7 @@ function ModWatchtools() { setSubmitting(false); setResp(ret.info); if (ret.rows) { + setSortBy(0); setTable({ columns: ret.columns, types: ret.types, @@ -277,6 +296,7 @@ function ModWatchtools() { setSubmitting(false); setResp(ret.info); if (ret.rows) { + setSortBy(0); setTable({ columns: ret.columns, types: ret.types, @@ -301,28 +321,39 @@ function ModWatchtools() { > - {columns.slice(1).map((col) => ( - {col} + {columns.slice(1).map((col, ind) => ( + setSortBy(ind + 1)} + >{col} ))} - {rows.map((row) => ( + {rows.sort((a, b) => compare(a[sortBy], b[sortBy])).map((row) => ( {row.slice(1).map((val, ind) => { const type = types[ind + 1]; switch (type) { case 'ts': { const date = new Date(val); + let minutes = date.getMinutes(); + if (minutes < 10) minutes = `0${minutes}`; return ( - {`${date.getHours()}:${date.getMinutes()}`} + {`${date.getHours()}:${minutes}`} ); } case 'clr': { const color = colors[val]; - const style = (color) ? { backgroundColor: color } : {}; + const style = (color) + ? { backgroundColor: color } + : undefined; return ({val}); } case 'coord': { diff --git a/src/components/Modtools.jsx b/src/components/Modtools.jsx index e381ce0..c9348b6 100644 --- a/src/components/Modtools.jsx +++ b/src/components/Modtools.jsx @@ -37,7 +37,7 @@ function Modtools() { className="modallink" style={(selectedPart === part) ? { fontWeight: 'bold', - } : {}} + } : undefined} onClick={() => selectPart(part)} >{part} {(ind !== parts.length - 1)