forked from cerc-io/laconic-console
Use Moustache to create template and set config in page. Use babel plugins to process GQL (and fix GQL queries). Added service type.
26 lines
641 B
JavaScript
26 lines
641 B
JavaScript
//
|
|
// Copyright 2020 DxOS.org
|
|
//
|
|
|
|
import get from 'lodash.get';
|
|
import { useState } from 'react';
|
|
|
|
// TODO(burdon): Enable multiple sort order (e.g., id, version).
|
|
export const useSorter = (initSort, initAscend) => {
|
|
const [{ sort, ascend }, setSort] = useState({ sort: initSort, ascend: initAscend });
|
|
|
|
const sorter = (item1, item2) => {
|
|
const a = get(item1, sort);
|
|
const b = get(item2, sort);
|
|
const dir = ascend ? 1 : -1;
|
|
return (a < b) ? -1 * dir : (a > b) ? dir : 0;
|
|
};
|
|
|
|
const sortBy = field => () => setSort({ sort: field, ascend: (field === sort ? !ascend : true) });
|
|
|
|
return [
|
|
sorter,
|
|
sortBy
|
|
];
|
|
};
|