Refactor StorageAction and StorageMechanics: add currentMaterialType prop, enhance material type handling, and improve capacity change logic

This commit is contained in:
jeraldGolden 2025-05-09 23:15:48 +05:30
parent 45a10b8673
commit 42d465a977
2 changed files with 64 additions and 9 deletions

View File

@ -8,11 +8,12 @@ interface StorageActionProps {
min: number; min: number;
max: number; max: number;
defaultValue: string; defaultValue: string;
currentMaterialType: string;
handleCapacityChange: (value: string) => void; handleCapacityChange: (value: string) => void;
handleMaterialTypeChange: (value: string) => void; handleMaterialTypeChange: (value: string) => void;
} }
const StorageAction: React.FC<StorageActionProps> = ({ type, value, min, max, defaultValue, handleCapacityChange, handleMaterialTypeChange }) => { const StorageAction: React.FC<StorageActionProps> = ({ type, value, min, max, defaultValue, currentMaterialType, handleCapacityChange, handleMaterialTypeChange }) => {
return ( return (
<> <>
{type === 'store' && {type === 'store' &&
@ -42,7 +43,8 @@ const StorageAction: React.FC<StorageActionProps> = ({ type, value, min, max, de
onChange={handleCapacityChange} onChange={handleCapacityChange}
/> />
<LabledDropdown <LabledDropdown
defaultOption={""} label={"Material Type"}
defaultOption={currentMaterialType}
options={["Default material", "Material 1", "Material 2", "Material 3"]} options={["Default material", "Material 1", "Material 2", "Material 3"]}
onSelect={handleMaterialTypeChange} onSelect={handleMaterialTypeChange}
/> />

View File

@ -17,7 +17,11 @@ function StorageMechanics() {
const { getPointByUuid, updateAction } = useProductStore(); const { getPointByUuid, updateAction } = useProductStore();
const { selectedProduct } = useSelectedProduct(); const { selectedProduct } = useSelectedProduct();
const { setSelectedAction, clearSelectedAction } = useSelectedAction(); const { setSelectedAction, clearSelectedAction } = useSelectedAction();
const { setCurrentMaterials, clearCurrentMaterials, updateStorageUnitLoad, } = useStorageUnitStore(); const {
setCurrentMaterials,
clearCurrentMaterials,
updateStorageUnitLoad,
} = useStorageUnitStore();
const email = localStorage.getItem('email') const email = localStorage.getItem('email')
const organization = (email!.split("@")[1]).split(".")[0]; const organization = (email!.split("@")[1]).split(".")[0];
@ -72,7 +76,12 @@ function StorageMechanics() {
} }
if (validOption === "spawn") { if (validOption === "spawn") {
clearCurrentMaterials(selectedEventData.data.modelUuid);
updateStorageUnitLoad(selectedEventData.data.modelUuid, selectedPointData.action.storageCapacity);
} else { } else {
const materialType = selectedPointData.action.materialType || "default";
updateStorageUnitLoad(selectedEventData.data.modelUuid, selectedPointData.action.storageCapacity);
} }
}; };
@ -91,7 +100,7 @@ function StorageMechanics() {
}; };
const handleCapacityChange = (value: string) => { const handleCapacityChange = (value: string) => {
if (!selectedPointData) return; if (!selectedEventData || !selectedPointData) return;
const newCapacity = parseInt(value); const newCapacity = parseInt(value);
const event = updateAction(selectedProduct.productId, selectedPointData.action.actionUuid, { const event = updateAction(selectedProduct.productId, selectedPointData.action.actionUuid, {
@ -106,17 +115,58 @@ function StorageMechanics() {
event event
); );
if (activeOption === "spawn") { if (activeOption === "store") {
const materialType = selectedPointData.action.materialType || "default";
updateStorageUnitLoad(selectedEventData.data.modelUuid, newCapacity);
} }
} }
}; };
const createNewMaterial = (materialType: string): MaterialSchema | null => {
if (!selectedEventData || !selectedPointData) return null;
const materialId = THREE.MathUtils.generateUUID();
return {
materialId,
materialName: `${materialType}-${Date.now()}`,
materialType,
isActive: false,
isVisible: true,
isPaused: false,
isRendered: true,
startTime: performance.now(),
current: {
modelUuid: selectedEventData.data.modelUuid || '',
pointUuid: selectedPointData?.uuid || '',
actionUuid: selectedPointData?.action.actionUuid || ''
},
endTime: undefined,
previous: undefined,
next: null
};
};
const handleMaterialTypeChange = (value: string) => { const handleMaterialTypeChange = (value: string) => {
if (!selectedPointData) return; if (!selectedEventData || !selectedPointData) return;
const event = updateAction(selectedProduct.productId, selectedPointData.action.actionUuid, {
materialType: value,
});
if (event) {
updateBackend(
selectedProduct.productName,
selectedProduct.productId,
organization,
event
);
if (activeOption === "store") {
const capacity = selectedPointData.action.storageCapacity || 0;
}
}
}; };
// Get current values from store
const currentActionName = selectedPointData const currentActionName = selectedPointData
? selectedPointData.action.actionName ? selectedPointData.action.actionName
: "Action Name"; : "Action Name";
@ -125,6 +175,8 @@ function StorageMechanics() {
? selectedPointData.action.storageCapacity.toString() ? selectedPointData.action.storageCapacity.toString()
: "0"; : "0";
const currentMaterialType = selectedPointData?.action.materialType || "default";
const availableActions = { const availableActions = {
defaultOption: "store", defaultOption: "store",
options: ["store", "spawn"], options: ["store", "spawn"],
@ -156,6 +208,7 @@ function StorageMechanics() {
defaultValue="0" defaultValue="0"
min={0} min={0}
max={20} max={20}
currentMaterialType={currentMaterialType}
handleCapacityChange={handleCapacityChange} handleCapacityChange={handleCapacityChange}
handleMaterialTypeChange={handleMaterialTypeChange} handleMaterialTypeChange={handleMaterialTypeChange}
/> />