You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm having an issue where I keep getting at least two calendars being painted and it should be just the one, I've used the below code, trying to ensure it deletes any other instances but it isn't working.
import{BlockStack,Button,InlineStack,Text}from'@shopify/polaris';import{ChevronLeftIcon,ChevronRightIcon}from'@shopify/polaris-icons';importCalHeatmapfrom'cal-heatmap';importCalendarLabelfrom'cal-heatmap/plugins/CalendarLabel';importLegendLitefrom'cal-heatmap/plugins/LegendLite';importTooltipfrom'cal-heatmap/plugins/Tooltip';import{format}from'date-fns';import{useCallback,useEffect,useRef,useState}from'react';// Define constants at the top of the file, outside the componentconstCELL_WIDTH=11;constCELL_HEIGHT=11;constCELL_RADIUS=2;constCELL_GUTTER=4;constCELLS_PER_MONTH=4;constDOMAIN_GUTTER=4;exportdefaultfunctionStockCalendar({ chartData }){constcalRef=useRef(null);constcalInstanceRef=useRef(null);constcontainerRef=useRef(null);const[range,setRange]=useState(8);constcalculateRange=useCallback(()=>{if(containerRef.current){constcontainerWidth=containerRef.current.offsetWidth;// Calculate the width of one month (range) including the domainconstmonthWidth=CELL_WIDTH*CELLS_PER_MONTH+CELL_GUTTER*(CELLS_PER_MONTH-1)+DOMAIN_GUTTER;// Calculate how many full months can fitconstfullMonths=Math.floor(containerWidth/monthWidth);// Calculate the remaining widthconstremainingWidth=containerWidth-fullMonths*monthWidth;// Calculate the fraction of the next month that can fitconstfractionOfNextMonth=remainingWidth/monthWidth;// If we can fit at least 50% of the next month, include itconstnewRange=fractionOfNextMonth>=0.5 ? fullMonths+1 : fullMonths;// Subtract two from the calculated range, but ensure at least 1 month is shownconstfinalRange=Math.max(1,newRange-2);setRange(finalRange);}},[]);constinitializeCalendar=useCallback(()=>{if(calRef.current&&!calInstanceRef.current&&chartData.length>0){constcal=newCalHeatmap();cal.paint({itemSelector: calRef.current,range: range,data: {source: chartData.map((item)=>({date: newDate(item.key).toISOString().split('T')[0],inventoryQuantity: item.value})),type: 'json',x: 'date',y: 'inventoryQuantity'},date: {start: newDate(newDate().getFullYear()-1,8,21),// September 21st of the previous yearmin: newDate(newDate().getFullYear()-1,8,21),max: newDate(newDate().getFullYear(),8,20)// September 20th of the current year},scale: {color: {type: 'threshold',range: ['#f47560','#ede84e','#2CB67D'],domain: [1,11]}},domain: {type: 'month',gutter: DOMAIN_GUTTER,label: {text: 'MMM',textAlign: 'start',position: 'top'}},subDomain: {type: 'ghDay',radius: CELL_RADIUS,width: CELL_WIDTH,height: CELL_HEIGHT,gutter: CELL_GUTTER}},[[Tooltip,{text: function(date,value,dayjsDate){letstockStatus='Out of stock';if(value>0&&value<=10){stockStatus='Low in stock';}elseif(value>10){stockStatus='In stock';}return`${stockStatus} (${value||0} items) on ${dayjsDate.format('dddd, MMMM D, YYYY')}`;}}],[LegendLite,{itemSelector: '#day-legend',radius: 2,width: 11,height: 11,gutter: 4}],[CalendarLabel,{width: 30,textAlign: 'start',text: ()=>Array.from({length: 7},(_,i)=>i%2===0 ? '' : format(newDate(2023,0,i+1),'EEE')),padding: [25,0,0,0]}]]);calInstanceRef.current=cal;console.log('New CalHeatmap instance created');}},[chartData,range]);useEffect(()=>{console.log('StockCalendar effect running');calculateRange();initializeCalendar();constresizeObserver=newResizeObserver(()=>{calculateRange();});if(containerRef.current){resizeObserver.observe(containerRef.current);}// Cleanup functionreturn()=>{if(calInstanceRef.current){console.log('Destroying CalHeatmap instance');calInstanceRef.current.destroy();calInstanceRef.current=null;}resizeObserver.disconnect();};},[initializeCalendar,calculateRange]);consthandlePrevious=()=>calInstanceRef.current&&calInstanceRef.current.previous();consthandleNext=()=>calInstanceRef.current&&calInstanceRef.current.next();return(<divclassName="StockCalendar"ref={containerRef}><BlockStackgap={400}><divref={calRef}></div><InlineStackalign="space-between"blockAlign="center"><InlineStackgap={200}blockAlign="center"><Buttonicon={ChevronLeftIcon}onClick={handlePrevious}disabled={!calInstanceRef.current}accessibilityLabel="Previous"/><Buttonicon={ChevronRightIcon}onClick={handleNext}disabled={!calInstanceRef.current}accessibilityLabel="Next"/></InlineStack><InlineStackgap={200}align="end"blockAlign="center"><Textas="span"variant="bodyMd"tone="subdued">
Out of stock
</Text><divid="day-legend"style={{display: 'inline-block'}}></div><Textas="span"variant="bodyMd"tone="subdued">
In stock
</Text></InlineStack></InlineStack></BlockStack></div>);}
The text was updated successfully, but these errors were encountered:
I'm having an issue where I keep getting at least two calendars being painted and it should be just the one, I've used the below code, trying to ensure it deletes any other instances but it isn't working.
The text was updated successfully, but these errors were encountered: