Files
bodyshop/client/src/components/production-board-kanban/production-board-kanban.utils.js
Dave Richer a297bba193 - Check Point
Signed-off-by: Dave Richer <dave@imexsystems.ca>
2024-08-02 11:29:31 -04:00

91 lines
2.5 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, data, filter }) => {
const { search, employeeId } = filter;
const lanes = statuses.map((status) => ({
id: status,
title: status,
cards: []
}));
const filteredJobs =
(search === "" || !search) && !employeeId ? data : data.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,
job.ownr_fn,
job.ownr_co_nm,
job.ownr_ln,
job.status,
job.v_make_desc,
job.v_model_desc,
job.clm_no,
job.plate_no
].some((field) => field?.toLowerCase().includes(lowerSearch));
const matchesEmployeeId =
employeeId && [job.employee_body, job.employee_prep, job.employee_csr, job.employee_refinish].includes(employeeId);
return matchesSearch || matchesEmployeeId;
};