Skip to content

Commit

Permalink
Only show current base shipments for multibase users (#1754)
Browse files Browse the repository at this point in the history
  • Loading branch information
fhenrich33 authored Nov 25, 2024
1 parent 02521e4 commit 5feb49c
Showing 1 changed file with 71 additions and 67 deletions.
138 changes: 71 additions & 67 deletions front/src/views/Transfers/ShipmentsOverview/ShipmentsOverviewView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import { SelectColumnFilter } from "components/Table/Filter";
import { BreadcrumbNavigation } from "components/BreadcrumbNavigation";
import { BaseOrgCell, BoxesCell, DirectionCell, StateCell } from "./components/TableCells";
import { useLoadAndSetGlobalPreferences } from "hooks/useLoadAndSetGlobalPreferences";
import { useBaseIdParam } from "hooks/useBaseIdParam";

function ShipmentsOverviewView() {
const { globalPreferences } = useContext(GlobalPreferencesContext);
const { isLoading: isGlobalStateLoading } = useLoadAndSetGlobalPreferences();
const { baseId } = useBaseIdParam();
// If forwarded from AgreementsOverview
const location = useLocation();

Expand All @@ -27,80 +29,82 @@ function ShipmentsOverviewView() {

// transform shipments data for UI
const graphqlToTableTransformer = (shipmentQueryResult: ShipmentsQuery | undefined) =>
shipmentQueryResult?.shipments.map((element) => {
if (globalPreferences?.availableBases) {
const availableBaseIds = globalPreferences.availableBases.map((base) =>
parseInt(base.id, 10),
);
const sourceBaseId = parseInt(element.sourceBase.id, 10);
const targetBaseId = parseInt(element.targetBase.id, 10);
shipmentQueryResult?.shipments
.filter((shipment) => shipment.sourceBase.id === baseId || shipment.targetBase.id === baseId)
.map((element) => {
if (globalPreferences?.availableBases) {
const availableBaseIds = globalPreferences.availableBases.map((base) =>
parseInt(base.id, 10),
);
const sourceBaseId = parseInt(element.sourceBase.id, 10);
const targetBaseId = parseInt(element.targetBase.id, 10);

const shipmentRow = {
id: element.id,
labelIdentifier: element.labelIdentifier,
direction: "To",
partnerBaseOrg: {
base: element.targetBase.name,
organisation: element.targetBase.organisation.name,
},
state: element.state,
boxes: 0,
lastUpdated: "",
href: element.id,
};

// calculating direction
if (availableBaseIds.includes(sourceBaseId)) {
shipmentRow.direction = "To";
shipmentRow.partnerBaseOrg = {
base: element.targetBase.name,
organisation: element.targetBase.organisation.name,
};
} else if (availableBaseIds.includes(targetBaseId)) {
shipmentRow.direction = "From";
shipmentRow.partnerBaseOrg = {
base: element.sourceBase.name,
organisation: element.sourceBase.organisation.name,
const shipmentRow = {
id: element.id,
labelIdentifier: element.labelIdentifier,
direction: "To",
partnerBaseOrg: {
base: element.targetBase.name,
organisation: element.targetBase.organisation.name,
},
state: element.state,
boxes: 0,
lastUpdated: "",
href: element.id,
};
}

// counting of boxes from details
const uniqueBoxIds = element.details.reduce((accumulator, detail) => {
if (detail.removedOn == null) {
const boxId = detail.box.labelIdentifier;
accumulator[boxId] = (accumulator[boxId] || 0) + 1;
// calculating direction
if (availableBaseIds.includes(sourceBaseId)) {
shipmentRow.direction = "To";
shipmentRow.partnerBaseOrg = {
base: element.targetBase.name,
organisation: element.targetBase.organisation.name,
};
} else if (availableBaseIds.includes(targetBaseId)) {
shipmentRow.direction = "From";
shipmentRow.partnerBaseOrg = {
base: element.sourceBase.name,
organisation: element.sourceBase.organisation.name,
};
}
return accumulator;
}, {});
shipmentRow.boxes = Object.keys(uniqueBoxIds).length;

// calculate last updated
const shipmentUpdateDateTimes = [
element.startedOn,
element.sentOn,
element.receivingStartedOn,
element.completedOn,
element.canceledOn,
].concat(
// append all DateTimes in the ShipmentDetails
element.details
.reduce(
(accumulator, detail) =>
accumulator.concat(detail.createdOn).concat(detail.removedOn),
[],
)
.filter((date) => Boolean(date)),
);
// counting of boxes from details
const uniqueBoxIds = element.details.reduce((accumulator, detail) => {
if (detail.removedOn == null) {
const boxId = detail.box.labelIdentifier;
accumulator[boxId] = (accumulator[boxId] || 0) + 1;
}
return accumulator;
}, {});
shipmentRow.boxes = Object.keys(uniqueBoxIds).length;

// get max date for last updates
shipmentRow.lastUpdated = new Intl.DateTimeFormat().format(
new Date(Math.max(...shipmentUpdateDateTimes.map((date) => new Date(date).getTime()))),
);
// calculate last updated
const shipmentUpdateDateTimes = [
element.startedOn,
element.sentOn,
element.receivingStartedOn,
element.completedOn,
element.canceledOn,
].concat(
// append all DateTimes in the ShipmentDetails
element.details
.reduce(
(accumulator, detail) =>
accumulator.concat(detail.createdOn).concat(detail.removedOn),
[],
)
.filter((date) => Boolean(date)),
);

return shipmentRow;
}
return undefined;
}) || [];
// get max date for last updates
shipmentRow.lastUpdated = new Intl.DateTimeFormat().format(
new Date(Math.max(...shipmentUpdateDateTimes.map((date) => new Date(date).getTime()))),
);

return shipmentRow;
}
return undefined;
}) || [];

// Set default filter if user was forwarded from AgreementsOverview
const initialState = useMemo(
Expand Down

0 comments on commit 5feb49c

Please sign in to comment.