1- import React from 'react'
1+ import React , { useState } from 'react'
22import { FloorPlanEngine } from '@archilogic/floor-plan-sdk'
33import './FloorPlan.css'
44
@@ -10,22 +10,22 @@ interface FloorOptions {
1010 onLoad ?: ( floorPlan : FloorPlanEngine ) => void
1111}
1212
13- const FLOOR_PLAN_ID = `floor-plan-${ new Date ( ) . getTime ( ) } `
14- let floorPlan : FloorPlanEngine
15-
1613const FloorPanel : React . FC < FloorOptions > = props => {
14+ const [ floorPlan , setFloorPlan ] = useState ( undefined as FloorPlanEngine ) ;
15+ const floorPlanId = `floor-plan-${ new Date ( ) . getTime ( ) } `
1716 const tokenOptions = {
1817 publishableAccessToken : props . token
1918 }
2019
2120 const initFloorPlan = ( ) => {
22- const container = document . getElementById ( FLOOR_PLAN_ID )
21+ const container = document . getElementById ( floorPlanId )
2322 if ( container ) {
24- if ( ! props . loaded ) {
25- floorPlan = new FloorPlanEngine ( { container, options : props . startupOptions } )
26- floorPlan . loadScene ( props . id , tokenOptions ) . then ( ( ) => {
23+ if ( ! props . loaded && ! floorPlan ) {
24+ const fpe = new FloorPlanEngine ( { container, options : props . startupOptions } )
25+ fpe . loadScene ( props . id , tokenOptions ) . then ( ( ) => {
2726 if ( props . onLoad ) props . onLoad ( floorPlan )
2827 } )
28+ setFloorPlan ( fpe )
2929 } else {
3030 if ( props . onLoad ) props . onLoad ( floorPlan )
3131 }
@@ -35,7 +35,7 @@ const FloorPanel: React.FC<FloorOptions> = props => {
3535
3636 return (
3737 < div className = "plan-wrapper" >
38- < div id = { FLOOR_PLAN_ID } className = "plan-container" > </ div >
38+ < div id = { floorPlanId } className = "plan-container" > </ div >
3939 </ div >
4040 )
4141}
0 commit comments