Files
bodyshop/client/src/components/production-board-kanban/production-board-kanban.utils.js

93 lines
2.9 KiB
JavaScript

import { groupBy } from "lodash";
// Function to sort an array of objects by parentId
const sortByParentId = (arr) => {
let parentId = "-1";
const sortedList = [];
const byParentsIdsList = groupBy(arr, "kanbanparent");
while (byParentsIdsList[parentId]) {
sortedList.push(...byParentsIdsList[parentId]);
parentId = byParentsIdsList[parentId][byParentsIdsList[parentId].length - 1].id;
}
if (byParentsIdsList["null"]) {
sortedList.push(...byParentsIdsList["null"]);
}
// Ensure all items are included in the sorted list
if (arr.length !== sortedList.length) {
arr.forEach((origItem) => {
if (!sortedList.some((s) => s.id === origItem.id)) {
sortedList.push(origItem);
}
});
}
return sortedList;
};
// Function to create board data based on statuses and jobs, with optional filtering
export const createBoardData = (statuses, Jobs, filter) => {
const { search, employeeId } = filter;
const lanes = statuses.map((status) => ({
id: status,
title: status,
cards: []
}));
const filteredJobs =
(search === "" || !search) && !employeeId ? Jobs : Jobs.filter((job) => checkFilter(search, employeeId, job));
const DataGroupedByStatus = groupBy(filteredJobs, "status");
Object.keys(DataGroupedByStatus).forEach((statusGroupKey) => {
try {
const lane = lanes.find((l) => l.id === statusGroupKey);
if (!lane) return;
lane.cards = sortByParentId(DataGroupedByStatus[statusGroupKey]).map((job) => {
const { id, title, description, due_date, ...metadata } = job;
return {
id,
title,
description,
label: due_date || "",
metadata
};
});
} catch (error) {
console.error("Error while creating board card", error);
}
});
return { lanes };
};
// Function to check if a job matches the search and/or employeeId filter
const checkFilter = (search, employeeId, job) => {
const lowerSearch = search?.toLowerCase() || "";
const matchesSearch =
lowerSearch &&
((job.ro_number || "").toLowerCase().includes(lowerSearch) ||
(job.ownr_fn || "").toLowerCase().includes(lowerSearch) ||
(job.ownr_co_nm || "").toLowerCase().includes(lowerSearch) ||
(job.ownr_ln || "").toLowerCase().includes(lowerSearch) ||
(job.status || "").toLowerCase().includes(lowerSearch) ||
(job.v_make_desc || "").toLowerCase().includes(lowerSearch) ||
(job.v_model_desc || "").toLowerCase().includes(lowerSearch) ||
(job.clm_no || "").toLowerCase().includes(lowerSearch) ||
(job.plate_no || "").toLowerCase().includes(lowerSearch));
const matchesEmployeeId =
employeeId &&
(job.employee_body === employeeId ||
job.employee_prep === employeeId ||
job.employee_csr === employeeId ||
job.employee_refinish === employeeId);
return matchesSearch || matchesEmployeeId;
};