90 lines
3.6 KiB
TypeScript
90 lines
3.6 KiB
TypeScript
import { toast } from 'react-toastify';
|
|
import * as THREE from 'three';
|
|
|
|
import * as Types from "../../../../types/world/worldTypes";
|
|
// import { deleteFloorItem } from '../../../../services/factoryBuilder/assest/floorAsset/deleteFloorItemApi';
|
|
import { Socket } from 'socket.io-client';
|
|
import { getFloorAssets } from '../../../../services/factoryBuilder/assest/floorAsset/getFloorItemsApi';
|
|
|
|
async function DeleteFloorItems(
|
|
itemsGroup: Types.RefGroup,
|
|
hoveredDeletableFloorItem: Types.RefMesh,
|
|
setFloorItems: Types.setFloorItemSetState,
|
|
setSimulationStates: any,
|
|
socket: Socket<any>
|
|
): Promise<void> {
|
|
|
|
////////// Deleting the hovered Floor GLTF from the scene (itemsGroup.current) and from the floorItems and also update it in the localstorage //////////
|
|
|
|
if (hoveredDeletableFloorItem.current) {
|
|
|
|
const email = localStorage.getItem('email')
|
|
const organization = (email!.split("@")[1]).split(".")[0];
|
|
|
|
const items = await getFloorAssets(organization);
|
|
const removedItem = items.find(
|
|
(item: { modeluuid: string }) => item.modeluuid === hoveredDeletableFloorItem.current?.uuid
|
|
);
|
|
|
|
if (!removedItem) {
|
|
return
|
|
}
|
|
|
|
//REST
|
|
|
|
// const response = await deleteFloorItem(organization, removedItem.modeluuid, removedItem.modelname);
|
|
|
|
//SOCKET
|
|
|
|
const data = {
|
|
organization: organization,
|
|
modeluuid: removedItem.modeluuid,
|
|
modelname: removedItem.modelname,
|
|
socketId: socket.id
|
|
}
|
|
|
|
const response = socket.emit('v2:model-asset:delete', data)
|
|
|
|
if (response) {
|
|
const updatedItems = items.filter(
|
|
(item: { modeluuid: string }) => item.modeluuid !== hoveredDeletableFloorItem.current?.uuid
|
|
);
|
|
|
|
const storedItems = JSON.parse(localStorage.getItem("FloorItems") || '[]');
|
|
const updatedStoredItems = storedItems.filter((item: { modeluuid: string }) => item.modeluuid !== hoveredDeletableFloorItem.current?.uuid);
|
|
localStorage.setItem("FloorItems", JSON.stringify(updatedStoredItems));
|
|
|
|
if (hoveredDeletableFloorItem.current) {
|
|
// Traverse and dispose of resources
|
|
hoveredDeletableFloorItem.current.traverse((child: THREE.Object3D) => {
|
|
if (child instanceof THREE.Mesh) {
|
|
if (child.geometry) child.geometry.dispose();
|
|
if (Array.isArray(child.material)) {
|
|
child.material.forEach((material) => {
|
|
if (material.map) material.map.dispose();
|
|
material.dispose();
|
|
});
|
|
} else if (child.material) {
|
|
if (child.material.map) child.material.map.dispose();
|
|
child.material.dispose();
|
|
}
|
|
}
|
|
});
|
|
|
|
// Remove the object from the scene
|
|
itemsGroup.current.remove(hoveredDeletableFloorItem.current);
|
|
}
|
|
setFloorItems(updatedItems);
|
|
|
|
setSimulationStates((prevEvents: (Types.ConveyorEventsSchema | Types.VehicleEventsSchema | Types.StaticMachineEventsSchema)[]) => {
|
|
const updatedEvents = (prevEvents || []).filter(event => event.modeluuid !== removedItem.modeluuid);
|
|
return updatedEvents;
|
|
});
|
|
|
|
toast.success("Model Removed!");
|
|
}
|
|
}
|
|
}
|
|
|
|
export default DeleteFloorItems;
|