11import "./DragAndDrop.css" ;
2- import { getData } from "@/utils/store" ;
2+ import { getData , setData } from "@/utils/store" ;
33import { v4 as uuidv4 } from "uuid" ;
44import { useState , useEffect , useRef } from "react" ;
55import GridLayout from "react-grid-layout" ;
66import RenderWidget from "../Widgets/RenderWidget" ;
77import { alertError } from "@/lib/alertCustomized" ;
8- import MenuWidget from "./Menu/MenuWidget" ;
98import CommandBox from "../CommandBox" ;
109
1110const DragAndDrop = ( ) => {
@@ -50,14 +49,28 @@ const DragAndDrop = () => {
5049 const [ map , setMap ] = useState ( Array ( 30 ) . fill ( 0 ) ) ;
5150
5251 useEffect ( ( ) => {
53- const initialLayout = [
54- { i : "Clock_1" , x : 0 , y : 0 , w : 1 , h : 1 , type : "clock" } ,
55- { i : "Clock_2" , x : 2 , y : 0 , w : 2 , h : 1 , type : "clock" } ,
56- { i : "Music_3" , x : 4 , y : 0 , w : 1 , h : 1 , type : "music" } ,
57- ] ;
58- setLayout ( initialLayout ) ;
52+ const fetchLayout = async ( ) => {
53+ const savedLayout = await getData ( "layout" ) ;
54+ //console.log(savedLayout);
55+ if (
56+ savedLayout &&
57+ JSON . stringify ( savedLayout ) !== JSON . stringify ( layout )
58+ ) {
59+ setLayout ( savedLayout ) ;
60+ }
61+ } ;
62+ fetchLayout ( ) ;
5963 } , [ ] ) ;
6064
65+ useEffect ( ( ) => {
66+ setMap ( createMap ( layout ) ) ;
67+ const saveLayout = async ( ) => {
68+ //console.log("Saving layout", layout);
69+ await setData ( "layout" , layout ) ;
70+ } ;
71+ saveLayout ( ) ;
72+ } , [ layout ] ) ;
73+
6174 const createMap = ( layout ) => {
6275 let map = Array ( 30 ) . fill ( 0 ) ;
6376
@@ -73,10 +86,6 @@ const DragAndDrop = () => {
7386 return map ;
7487 } ;
7588
76- useEffect ( ( ) => {
77- setMap ( createMap ( layout ) ) ;
78- } , [ layout ] ) ;
79-
8089 const handleLayoutChange = ( newLayout ) => {
8190 let changed = false ;
8291 const fixedLayout = newLayout . map ( ( item ) => {
0 commit comments