ibms-dome/ibms_react/src/stores/forgeSlice.js

61 lines
1.7 KiB
JavaScript

import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { forgeInit, modelLoad } from "@UTIL";
export const fetchForge = createAsyncThunk(
"forge/fetchForge",
async ({ viewerEl, urn }, { dispatch, fulfillWithValue, rejectWithValue }) => {
const res = await forgeInit(viewerEl);
if (res.ok) {
await modelLoad(res.viewer, urn, dispatch);
return fulfillWithValue(res.viewer);
} else {
return rejectWithValue(res.errCode);
}
},
);
const forgeSlice = createSlice({
name: "forge",
initialState: {
viewer: null,
model: null,
dataVizExtn: null,
DataVizCore: null,
structureInfo: null,
errCode: 0,
urn_3D: "",
},
reducers: {
loadDataVizExtn: (state, { payload }) => {
state.dataVizExtn = payload;
state.DataVizCore = window.Autodesk.DataVisualization.Core;
// heatmap
state.structureInfo = new window.Autodesk.DataVisualization.Core.ModelStructureInfo(state.model);
},
loadStructureInfo: (state, { payload }) => {
state.structureInfo = payload
},
loadModel: (state, { payload }) => {
state.model = payload
},
shutdownForge: (state, action) => {
state.viewer?.finish();
state.viewer = null;
window.Autodesk?.Viewing.shutdown();
},
},
extraReducers: (builder) => {
builder
.addCase(fetchForge.fulfilled, (state, action) => {
state.viewer = action.payload;
})
.addCase(fetchForge.rejected, (state, action) => {
state.errCode = action.payload;
});
},
});
const { reducer, actions } = forgeSlice;
export const { shutdownForge, loadDataVizExtn, loadModel } = actions;
export default reducer;