134 lines
4.3 KiB
JavaScript
134 lines
4.3 KiB
JavaScript
import { groupBy } from "lodash";
|
|
import fakeData from "./testData/board300.json";
|
|
|
|
const sortByParentId = (arr) => {
|
|
// return arr.reduce((accumulator, currentValue) => {
|
|
// //Find the parent item.
|
|
// let item = accumulator.find((x) => x.id === currentValue.kanbanparent);
|
|
// //Get index of parent item
|
|
// let index = accumulator.indexOf(item);
|
|
|
|
// index = index !== -1 ? index + 1 : 0;
|
|
// accumulator.splice(index, 0, currentValue);
|
|
// return accumulator;
|
|
// }, []);
|
|
|
|
let parentId = "-1";
|
|
const sortedList = [];
|
|
const byParentsIdsList = groupBy(arr, "kanbanparent"); // Create a new array with objects indexed by parentId
|
|
//console.log("sortByParentId -> byParentsIdsList", byParentsIdsList);
|
|
|
|
while (byParentsIdsList[parentId]) {
|
|
sortedList.push(...byParentsIdsList[parentId]); //Spread in the whole list in case several items have the same parents.
|
|
parentId = byParentsIdsList[parentId][byParentsIdsList[parentId].length - 1].id; //Grab the ID from the last one.
|
|
}
|
|
|
|
if (byParentsIdsList["null"]) byParentsIdsList["null"].map((i) => sortedList.push(i));
|
|
|
|
//Validate that the 2 arrays are of the same length and no children are missing.
|
|
if (arr.length !== sortedList.length) {
|
|
arr.map((origItem) => {
|
|
if (!!!sortedList.find((s) => s.id === origItem.id)) {
|
|
sortedList.push(origItem);
|
|
console.log("DATA CONSISTENCY ERROR: ", origItem.ro_number);
|
|
}
|
|
return 1;
|
|
});
|
|
}
|
|
|
|
return sortedList;
|
|
};
|
|
|
|
export const createFakeBoardData = () => {
|
|
return fakeData;
|
|
};
|
|
|
|
export const createBoardData = (AllStatuses, Jobs, filter) => {
|
|
const { search, employeeId } = filter;
|
|
const lanes = AllStatuses.map((s) => {
|
|
return {
|
|
id: s,
|
|
title: s,
|
|
cards: []
|
|
};
|
|
});
|
|
|
|
const filteredJobs =
|
|
(search === "" || !search) && !employeeId
|
|
? Jobs
|
|
: Jobs.filter((j) => {
|
|
let include = false;
|
|
if (search && search !== "") {
|
|
include = CheckSearch(search, j);
|
|
}
|
|
|
|
if (!!employeeId) {
|
|
include =
|
|
include ||
|
|
j.employee_body === employeeId ||
|
|
j.employee_prep === employeeId ||
|
|
j.employee_csr === employeeId ||
|
|
j.employee_refinish === employeeId;
|
|
}
|
|
|
|
return include;
|
|
});
|
|
|
|
const DataGroupedByStatus = groupBy(filteredJobs, (d) => d.status);
|
|
|
|
Object.keys(DataGroupedByStatus).map((statusGroupKey) => {
|
|
try {
|
|
const lane = lanes.find((l) => l.id === statusGroupKey);
|
|
if (!lane?.cards) return null;
|
|
lane.cards = sortByParentId(DataGroupedByStatus[statusGroupKey]).map((job) => {
|
|
const { id, title, description, due_date, ...metadata } = job;
|
|
return {
|
|
id,
|
|
title,
|
|
description,
|
|
label: job.due_date || "",
|
|
metadata
|
|
};
|
|
});
|
|
} catch (error) {
|
|
console.log("Error while creating board card", error);
|
|
}
|
|
return null;
|
|
});
|
|
|
|
return { lanes };
|
|
};
|
|
|
|
const CheckSearch = (search, job) => {
|
|
return (
|
|
(job.ro_number || "").toLowerCase().includes(search.toLowerCase()) ||
|
|
(job.ownr_fn || "").toLowerCase().includes(search.toLowerCase()) ||
|
|
(job.ownr_co_nm || "").toLowerCase().includes(search.toLowerCase()) ||
|
|
(job.ownr_ln || "").toLowerCase().includes(search.toLowerCase()) ||
|
|
(job.status || "").toLowerCase().includes(search.toLowerCase()) ||
|
|
(job.v_make_desc || "").toLowerCase().includes(search.toLowerCase()) ||
|
|
(job.v_model_desc || "").toLowerCase().includes(search.toLowerCase()) ||
|
|
(job.clm_no || "").toLowerCase().includes(search.toLowerCase()) ||
|
|
(job.plate_no || "").toLowerCase().includes(search.toLowerCase())
|
|
);
|
|
};
|
|
|
|
// export const updateBoardOnMove = (board, card, source, destination) => {
|
|
// //Slice from source
|
|
|
|
// const sourceCardList = board.columns.find((x) => x.id === source.fromColumnId)
|
|
// .cards;
|
|
// sourceCardList.slice(source.fromPosition, 0);
|
|
|
|
// //Splice into destination.
|
|
// const destCardList = board.columns.find(
|
|
// (x) => x.id === destination.toColumnId
|
|
// ).cards;
|
|
// console.log("updateBoardOnMove -> destCardList", destCardList);
|
|
|
|
// destCardList.splice(destination.toPosition, 0, card);
|
|
// console.log("updateBoardOnMove -> destCardList", destCardList);
|
|
// console.log("board", board);
|
|
// return board;
|
|
// };
|