Files
hats-app/app/(dashboard)/task/data.ts
T
Ichsan Kurniawan b5735a90c4 update logic, ui
2026-04-21 16:00:21 +07:00

90 lines
3.5 KiB
TypeScript

import { IProjectAssignment } from "./type.assignment";
import { IClient } from "./types";
export const STATUS_LIST = ["Not Started", "On Progress", "On Going", "Halted", "Issue", "SIT", "UAT", "PTR", "BAST", "Warranty", "End Of Support", "Canceled"]
export const COMPLETE_STATUS_LIST = ['BAST', 'WARRANTY', 'END OF SUPPORT']
export const ONPROGRESS_STATUS_LIST = ['ON PROGRESS', 'ON GOING', 'HALTED', 'ISSUE', 'SIT', 'UAT', 'PTR']
export const getFilteredClients = (clientLists: IClient[], query?: string, sortBy?: string) => {
let processedData = clientLists.filter((c) =>
c.name.toLowerCase().includes(query!) ||
c.end_user_name?.toLowerCase().includes(query!)
);
processedData = processedData.sort((a, b) => {
if (sortBy === 'latest') {
return new Date(b.created_at!).getTime() - new Date(a.created_at!).getTime();
}
if (sortBy === 'oldest') {
return new Date(a.created_at!).getTime() - new Date(b.created_at!).getTime();
}
if (sortBy === 'participants_desc') {
const aCount = a.users?.length || 0;
const bCount = b.users?.length || 0;
return bCount - aCount; // Highest count first
}
if (sortBy === 'participants_asc') {
const aCount = a.users?.length || 0;
const bCount = b.users?.length || 0;
return aCount - bCount; // Lowest count first
}
return 0; // 'default' - keeps original array order
});
return processedData;
}
export const getFilteredAssignments = (assignmentList: IProjectAssignment[], query?: string, status?: string, sortBy?: string) => {
if (assignmentList && assignmentList.length === 0) return []
const activeStatuses = status ? status.toLowerCase()?.split(',') : [];
// 1. FILTERING
let processedData = assignmentList.filter((item) => {
const matchesSearch = query === '' || (
(item.im_name?.toLowerCase().includes(query)) ||
(item.im_code?.toLowerCase().includes(query)) ||
(item.project?.name?.toLowerCase().includes(query!)) ||
(item.project?.code?.toLowerCase().includes(query!)) ||
(item.wo_name?.toLowerCase().includes(query!)) ||
(item.wo_code?.toLowerCase().includes(query!))
);
// --- Status Filter ---
const itemStatus = item.project_status?.name?.toLowerCase();
const matchesStatus = activeStatuses.length === 0 || (itemStatus && activeStatuses.includes(itemStatus));
return matchesSearch && matchesStatus;
});
// 2. SORTING
processedData = processedData.sort((a: IProjectAssignment, b: IProjectAssignment) => {
if (sortBy === 'latest') {
return new Date(b.created_at).getTime() - new Date(a.created_at).getTime();
}
if (sortBy === 'oldest') {
return new Date(a.created_at).getTime() - new Date(b.created_at).getTime();
}
if (sortBy === 'participants_desc') {
const aCount = a.project_assignment_work_order_detail?.length || 0;
const bCount = b.project_assignment_work_order_detail?.length || 0;
return bCount - aCount; // Highest count first
}
if (sortBy === 'participants_asc') {
const aCount = a.project_assignment_work_order_detail?.length || 0;
const bCount = b.project_assignment_work_order_detail?.length || 0;
return aCount - bCount; // Lowest count first
}
return 0; // 'default' - keeps original array order
});
return processedData;
}