2024-03-11 15:30:05 +00:00
|
|
|
import sortBy from "lodash/sortBy";
|
|
|
|
// helpers
|
2024-03-19 14:38:35 +00:00
|
|
|
import { satisfiesDateFilter } from "@/helpers/filter.helper";
|
2024-03-20 08:14:08 +00:00
|
|
|
import { getDate } from "@/helpers/date-time.helper";
|
2024-03-11 15:30:05 +00:00
|
|
|
// types
|
|
|
|
import { ICycle, TCycleFilters } from "@plane/types";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description orders cycles based on their status
|
|
|
|
* @param {ICycle[]} cycles
|
|
|
|
* @returns {ICycle[]}
|
|
|
|
*/
|
|
|
|
export const orderCycles = (cycles: ICycle[]): ICycle[] => {
|
|
|
|
if (cycles.length === 0) return [];
|
|
|
|
|
2024-04-05 14:35:55 +00:00
|
|
|
const acceptedStatuses = ["current", "upcoming", "draft"];
|
2024-03-11 15:30:05 +00:00
|
|
|
const STATUS_ORDER: {
|
|
|
|
[key: string]: number;
|
|
|
|
} = {
|
|
|
|
current: 1,
|
|
|
|
upcoming: 2,
|
|
|
|
draft: 3,
|
|
|
|
};
|
|
|
|
|
2024-04-05 14:35:55 +00:00
|
|
|
let filteredCycles = cycles.filter((c) => acceptedStatuses.includes(c.status?.toLowerCase() ?? ""));
|
2024-03-11 15:30:05 +00:00
|
|
|
filteredCycles = sortBy(filteredCycles, [
|
2024-04-05 14:35:55 +00:00
|
|
|
(c) => STATUS_ORDER[c.status?.toLowerCase() ?? ""],
|
|
|
|
(c) => (c.status?.toLowerCase() === "upcoming" ? c.start_date : c.name.toLowerCase()),
|
2024-03-11 15:30:05 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
return filteredCycles;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description filters cycles based on the filter
|
|
|
|
* @param {ICycle} cycle
|
|
|
|
* @param {TCycleFilters} filter
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
export const shouldFilterCycle = (cycle: ICycle, filter: TCycleFilters): boolean => {
|
|
|
|
let fallsInFilters = true;
|
|
|
|
Object.keys(filter).forEach((key) => {
|
|
|
|
const filterKey = key as keyof TCycleFilters;
|
|
|
|
if (filterKey === "status" && filter.status && filter.status.length > 0)
|
2024-04-05 14:35:55 +00:00
|
|
|
fallsInFilters = fallsInFilters && filter.status.includes(cycle.status?.toLowerCase() ?? "");
|
2024-03-11 15:30:05 +00:00
|
|
|
if (filterKey === "start_date" && filter.start_date && filter.start_date.length > 0) {
|
2024-03-20 08:14:08 +00:00
|
|
|
const startDate = getDate(cycle.start_date);
|
2024-03-11 15:30:05 +00:00
|
|
|
filter.start_date.forEach((dateFilter) => {
|
2024-03-20 08:14:08 +00:00
|
|
|
fallsInFilters = fallsInFilters && !!startDate && satisfiesDateFilter(startDate, dateFilter);
|
2024-03-11 15:30:05 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
if (filterKey === "end_date" && filter.end_date && filter.end_date.length > 0) {
|
2024-03-20 08:14:08 +00:00
|
|
|
const endDate = getDate(cycle.end_date);
|
2024-03-11 15:30:05 +00:00
|
|
|
filter.end_date.forEach((dateFilter) => {
|
2024-03-20 08:14:08 +00:00
|
|
|
fallsInFilters = fallsInFilters && !!endDate && satisfiesDateFilter(endDate, dateFilter);
|
2024-03-11 15:30:05 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return fallsInFilters;
|
|
|
|
};
|