Files
hats-app/app/(dashboard)/task/data.ts
T
Ichsan Kurniawan aeda578c78 Add query-string helper and centralize filtering
Introduce getQueryStringFilteredAssignments in lib/fetchData and use it across task pages to append backend filter params to API requests. Move client/assignment filtering into shared helpers (getFilteredAssignments, getFilteredClients) and remove duplicated local filter logic from client/project pages. Expand searchable fields and adjust getFilteredAssignments signature to accept (list, query, sortBy). Update IClient type and add ProjectStatusSummary. Miscellaneous UI/UX fixes: use fullname in AssignmentTable tooltip, fix debounce usage, pass correct prop to FilterComponent in HomeTask, and normalize isEdit handling in project-assignment page.
2026-05-04 18:39:27 +07:00

98 lines
4.0 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.code?.toLowerCase().includes(query!) ||
c.description?.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, 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.client.name?.toLowerCase().includes(query!)) ||
(item.client.code?.toLowerCase().includes(query!)) ||
(item.end_user.name?.toLowerCase().includes(query!)) ||
(item.end_user.code?.toLowerCase().includes(query!)) ||
(item.im_name?.toLowerCase().includes(query)) ||
(item.im_code?.toLowerCase().includes(query)) ||
(item.im_description?.toLowerCase().includes(query)) ||
(item.project?.name?.toLowerCase().includes(query!)) ||
(item.project?.code?.toLowerCase().includes(query!)) ||
(item.project?.description?.toLowerCase().includes(query!)) ||
(item.wo_name?.toLowerCase().includes(query!)) ||
(item.wo_code?.toLowerCase().includes(query!))
);
// --- Status Filter ---
// const itemStatus = item.project_assignment_status?.name?.toLowerCase();
// const matchesStatus = activeStatuses.length === 0 || (itemStatus && activeStatuses.includes(itemStatus));
// return matchesSearch && matchesStatus;
return matchesSearch;
});
// 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;
}