1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/frontend/src/hooks/useSort.js
Fredrik Strand Oseberg 85a7c55fdf Feat/group by projects (#308)
This PR adds support for projects as a first class citizen, and toggling features on in different environments.
2021-07-07 11:04:36 +02:00

88 lines
2.3 KiB
JavaScript

import { useState } from 'react';
import {
sortFeaturesByNameAscending,
sortFeaturesByNameDescending,
sortFeaturesByLastSeenAscending,
sortFeaturesByLastSeenDescending,
sortFeaturesByCreatedAtAscending,
sortFeaturesByCreatedAtDescending,
sortFeaturesByExpiredAtAscending,
sortFeaturesByExpiredAtDescending,
sortFeaturesByStatusAscending,
sortFeaturesByStatusDescending,
} from '../component/Reporting/utils';
import {
LAST_SEEN,
NAME,
CREATED,
EXPIRED,
STATUS,
REPORT,
} from '../component/Reporting/constants';
const useSort = () => {
const [sortData, setSortData] = useState({
sortKey: NAME,
ascending: true,
});
const handleSortName = features => {
if (sortData.ascending) {
return sortFeaturesByNameAscending(features);
}
return sortFeaturesByNameDescending(features);
};
const handleSortLastSeen = features => {
if (sortData.ascending) {
return sortFeaturesByLastSeenAscending(features);
}
return sortFeaturesByLastSeenDescending(features);
};
const handleSortCreatedAt = features => {
if (sortData.ascending) {
return sortFeaturesByCreatedAtAscending(features);
}
return sortFeaturesByCreatedAtDescending(features);
};
const handleSortExpiredAt = features => {
if (sortData.ascending) {
return sortFeaturesByExpiredAtAscending(features);
}
return sortFeaturesByExpiredAtDescending(features);
};
const handleSortStatus = features => {
if (sortData.ascending) {
return sortFeaturesByStatusAscending(features);
}
return sortFeaturesByStatusDescending(features);
};
const sort = features => {
switch (sortData.sortKey) {
case NAME:
return handleSortName(features);
case LAST_SEEN:
return handleSortLastSeen(features);
case CREATED:
return handleSortCreatedAt(features);
case EXPIRED:
case REPORT:
return handleSortExpiredAt(features);
case STATUS:
return handleSortStatus(features);
default:
return features;
}
};
return [sort, setSortData];
};
export default useSort;