2024-03-12 14:54:21 +00:00
|
|
|
import sortBy from "lodash/sortBy";
|
2024-04-11 15:58:59 +00:00
|
|
|
import { IModule, TModuleDisplayFilters, TModuleFilters, TModuleOrderByOptions } from "@plane/types";
|
2024-03-12 14:54:21 +00:00
|
|
|
// helpers
|
2024-03-20 08:14:08 +00:00
|
|
|
import { getDate } from "@/helpers/date-time.helper";
|
2024-04-11 15:58:59 +00:00
|
|
|
import { satisfiesDateFilter } from "@/helpers/filter.helper";
|
2024-03-12 14:54:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @description orders modules based on their status
|
|
|
|
* @param {IModule[]} modules
|
|
|
|
* @param {TModuleOrderByOptions | undefined} orderByKey
|
|
|
|
* @returns {IModule[]}
|
|
|
|
*/
|
|
|
|
export const orderModules = (modules: IModule[], orderByKey: TModuleOrderByOptions | undefined): IModule[] => {
|
|
|
|
let orderedModules: IModule[] = [];
|
|
|
|
if (modules.length === 0 || !orderByKey) return [];
|
|
|
|
|
|
|
|
if (orderByKey === "name") orderedModules = sortBy(modules, [(m) => m.name.toLowerCase()]);
|
|
|
|
if (orderByKey === "-name") orderedModules = sortBy(modules, [(m) => m.name.toLowerCase()]).reverse();
|
|
|
|
if (["progress", "-progress"].includes(orderByKey))
|
|
|
|
orderedModules = sortBy(modules, [
|
|
|
|
(m) => {
|
|
|
|
let progress = (m.completed_issues + m.cancelled_issues) / m.total_issues;
|
|
|
|
if (isNaN(progress)) progress = 0;
|
2024-03-22 13:06:09 +00:00
|
|
|
return orderByKey === "progress" ? progress : -progress;
|
2024-03-12 14:54:21 +00:00
|
|
|
},
|
|
|
|
"name",
|
|
|
|
]);
|
|
|
|
if (["issues_length", "-issues_length"].includes(orderByKey))
|
|
|
|
orderedModules = sortBy(modules, [
|
|
|
|
(m) => (orderByKey === "issues_length" ? m.total_issues : !m.total_issues),
|
|
|
|
"name",
|
|
|
|
]);
|
|
|
|
if (orderByKey === "target_date") orderedModules = sortBy(modules, [(m) => m.target_date]);
|
|
|
|
if (orderByKey === "-target_date") orderedModules = sortBy(modules, [(m) => !m.target_date]);
|
|
|
|
if (orderByKey === "created_at") orderedModules = sortBy(modules, [(m) => m.created_at]);
|
|
|
|
if (orderByKey === "-created_at") orderedModules = sortBy(modules, [(m) => !m.created_at]);
|
|
|
|
|
|
|
|
return orderedModules;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description filters modules based on the filters
|
|
|
|
* @param {IModule} module
|
|
|
|
* @param {TModuleDisplayFilters} displayFilters
|
|
|
|
* @param {TModuleFilters} filters
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
export const shouldFilterModule = (
|
|
|
|
module: IModule,
|
|
|
|
displayFilters: TModuleDisplayFilters,
|
|
|
|
filters: TModuleFilters
|
|
|
|
): boolean => {
|
|
|
|
let fallsInFilters = true;
|
|
|
|
Object.keys(filters).forEach((key) => {
|
|
|
|
const filterKey = key as keyof TModuleFilters;
|
|
|
|
if (filterKey === "status" && filters.status && filters.status.length > 0)
|
2024-04-05 14:35:55 +00:00
|
|
|
fallsInFilters = fallsInFilters && filters.status.includes(module.status?.toLowerCase() ?? "");
|
2024-03-12 14:54:21 +00:00
|
|
|
if (filterKey === "lead" && filters.lead && filters.lead.length > 0)
|
|
|
|
fallsInFilters = fallsInFilters && filters.lead.includes(`${module.lead_id}`);
|
|
|
|
if (filterKey === "members" && filters.members && filters.members.length > 0) {
|
|
|
|
const memberIds = module.member_ids;
|
|
|
|
fallsInFilters = fallsInFilters && filters.members.some((memberId) => memberIds.includes(memberId));
|
|
|
|
}
|
|
|
|
if (filterKey === "start_date" && filters.start_date && filters.start_date.length > 0) {
|
2024-03-20 08:14:08 +00:00
|
|
|
const startDate = getDate(module.start_date);
|
2024-03-12 14:54:21 +00:00
|
|
|
filters.start_date.forEach((dateFilter) => {
|
2024-03-20 08:14:08 +00:00
|
|
|
fallsInFilters = fallsInFilters && !!startDate && satisfiesDateFilter(startDate, dateFilter);
|
2024-03-12 14:54:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
if (filterKey === "target_date" && filters.target_date && filters.target_date.length > 0) {
|
2024-03-20 08:14:08 +00:00
|
|
|
const endDate = getDate(module.target_date);
|
2024-03-12 14:54:21 +00:00
|
|
|
filters.target_date.forEach((dateFilter) => {
|
2024-03-20 08:14:08 +00:00
|
|
|
fallsInFilters = fallsInFilters && !!endDate && satisfiesDateFilter(endDate, dateFilter);
|
2024-03-12 14:54:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (displayFilters.favorites && !module.is_favorite) fallsInFilters = false;
|
|
|
|
|
|
|
|
return fallsInFilters;
|
|
|
|
};
|