now can delete widgets
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
import { Button } from "@chakra-ui/react";
|
||||
|
||||
export function CancelButton ({onCancel}) {
|
||||
return (
|
||||
<Button onClick={onCancel} borderRadius="md">Cancel</Button>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { Button } from "@chakra-ui/react";
|
||||
|
||||
export function SaveButton ({onSave}) {
|
||||
return (
|
||||
<Button onClick={onSave} borderRadius="md" bg="primary" _hover={{bg: "primaryLt"}}>Save</Button>
|
||||
)
|
||||
}
|
||||
@@ -1,13 +1,26 @@
|
||||
import { useSlotRecipe, chakra } from "@chakra-ui/react"
|
||||
import { useSlotRecipe, chakra, Flex, CloseButton, Text } from "@chakra-ui/react"
|
||||
|
||||
export default function Widget({ title }) {
|
||||
export default function Widget({ id, title, isEditable, isDelete, DeleteWidget }) {
|
||||
console.log("rendering widget");
|
||||
const recipe = useSlotRecipe({ key: "widget" });
|
||||
const styles = recipe();
|
||||
|
||||
return (
|
||||
<>
|
||||
<chakra.div css={styles.header} className="widget-handle" >
|
||||
{title}
|
||||
{console.log("This widgets id is: ", id)}
|
||||
<chakra.div css={styles.header} cursor={isEditable ? "move":"default"}>
|
||||
{isDelete ? (
|
||||
<>
|
||||
<Flex flexDirection="row" justifyContent="space-between" alignItems="center">
|
||||
<Text className="widget-handle" w="100%">{title}</Text>
|
||||
<CloseButton onClick={() => DeleteWidget(id)} size="xs" _hover={{bg: "tomato", color: "white"}}/>
|
||||
</Flex>
|
||||
</>
|
||||
):(
|
||||
<>
|
||||
<Text className="widget-handle" >{title}</Text>
|
||||
</>
|
||||
)}
|
||||
</chakra.div>
|
||||
<chakra.div css={styles.canvas} >
|
||||
This is their widget content - drag or resize me!
|
||||
|
||||
@@ -6,10 +6,14 @@ import { DailyActivity } from "./DashboardCanvasNav/DailyActivity";
|
||||
import { Gas } from "./DashboardCanvasNav/Gas";
|
||||
import { Seismic } from "./DashboardCanvasNav/Seismic";
|
||||
import { RemoteSensing } from "./DashboardCanvasNav/RemoteSensing";
|
||||
import { useState, useEffect } from "react";
|
||||
import { useState, useEffect, useDebugValue } from "react";
|
||||
import { CancelButton } from "../components/CancelButton";
|
||||
import { SaveButton } from "../components/SaveButton";
|
||||
|
||||
export function DashboardCanvas() {
|
||||
console.log("rendering dashboardcanvas");
|
||||
const [isEditable, setIsEditable] = useState(false); // editable=true means static=false (vice versa)
|
||||
const [isDelete, setIsDelete] = useState(false);
|
||||
const [newCounter, setNewCounter] = useState(3);
|
||||
const [layout, setLayout] = useState([
|
||||
{ i: '0', x: 0, y: 0, w: 3, h: 3, minH: 3, minW: 1, static: !isEditable },
|
||||
@@ -18,15 +22,6 @@ export function DashboardCanvas() {
|
||||
]);
|
||||
const [originalLayout, setOriginalLayout] = useState([]);
|
||||
|
||||
useEffect(() => {
|
||||
setLayout(prevLayout =>
|
||||
prevLayout.map(item => ({
|
||||
...item,
|
||||
static: !isEditable // static false means resizable/editable
|
||||
}))
|
||||
);
|
||||
}, [isEditable]);
|
||||
|
||||
const onAddWidget = () => {
|
||||
const newId = newCounter.toString();
|
||||
setLayout(prev => [
|
||||
@@ -42,6 +37,16 @@ export function DashboardCanvas() {
|
||||
setNewCounter(prev => prev + 1);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
console.log("static change effect");
|
||||
setLayout(prevLayout =>
|
||||
prevLayout.map(item => ({
|
||||
...item,
|
||||
static: !isEditable // static false means resizable/editable
|
||||
}))
|
||||
);
|
||||
}, [isEditable]);
|
||||
|
||||
const onEditWidgets = () => {
|
||||
console.log("widgets are now editable");
|
||||
setOriginalLayout(layout); // Backup layout before editing
|
||||
@@ -49,23 +54,35 @@ export function DashboardCanvas() {
|
||||
}
|
||||
|
||||
const onSave = () => {
|
||||
setOriginalLayout([]) // Clear backup layout
|
||||
console.log("saving widgets...");
|
||||
setIsEditable(false);
|
||||
setIsDelete(false);
|
||||
setOriginalLayout([]) // Clear backup layout
|
||||
}
|
||||
|
||||
const onCancel = () => {
|
||||
console.log("canceling changes...");
|
||||
if (originalLayout.length) {
|
||||
setLayout(originalLayout.map(item => ({
|
||||
...item,
|
||||
static: true
|
||||
})));
|
||||
};
|
||||
setOriginalLayout([]);
|
||||
setIsEditable(false);
|
||||
setIsDelete(false);
|
||||
setOriginalLayout([]);
|
||||
}
|
||||
|
||||
const onDeleteWidgets = (id) => {
|
||||
setLayout(prev => prev.filter(w => w.i !== id));
|
||||
const onDeleteWidgets = () => {
|
||||
console.log("Start deleting widgets...");
|
||||
setOriginalLayout(layout);
|
||||
setIsEditable(true);
|
||||
setIsDelete(true);
|
||||
}
|
||||
|
||||
const DeleteWidget = (id) => {
|
||||
console.log("Deleting widget with id: ", id);
|
||||
setLayout(layout => layout.filter(w => w.i !== id));
|
||||
}
|
||||
|
||||
const onLayoutChange = (newLayout) => {
|
||||
@@ -105,19 +122,11 @@ export function DashboardCanvas() {
|
||||
</Tabs.Trigger>
|
||||
</Tabs.List>
|
||||
|
||||
{/* <HStack >
|
||||
<SearchBar />
|
||||
<SettingsButton visible={isEditable}
|
||||
onAddWidget={ onAddWidget }
|
||||
onEditWidgets={ onEditWidgets }
|
||||
onDeleteWidgets={ onDeleteWidgets }
|
||||
/>
|
||||
</HStack> */}
|
||||
<HStack >
|
||||
{isEditable ? (
|
||||
{isEditable || isDelete ? (
|
||||
<>
|
||||
<Button onClick={onSave} borderRadius="md" bg="primary" _hover={{bg: "primaryLt"}}>Save</Button>
|
||||
<Button onClick={onCancel} borderRadius="md">Cancel</Button>
|
||||
<SaveButton onSave={onSave} />
|
||||
<CancelButton onCancel={onCancel} />
|
||||
</>
|
||||
):(
|
||||
<>
|
||||
@@ -137,6 +146,9 @@ export function DashboardCanvas() {
|
||||
<WidgetCanvas
|
||||
layout={layout}
|
||||
onLayoutChange={onLayoutChange}
|
||||
isEditable={isEditable}
|
||||
isDelete={isDelete}
|
||||
DeleteWidget={DeleteWidget}
|
||||
/>
|
||||
</Tabs.Content>
|
||||
<Tabs.Content value="gas">
|
||||
|
||||
@@ -7,7 +7,8 @@ import GridLayout, { WidthProvider } from 'react-grid-layout';
|
||||
import Widget from '../../components/Widget.jsx';
|
||||
import { useSlotRecipe, chakra, Flex } from '@chakra-ui/react';
|
||||
|
||||
export default function WidgetCanvas({layout, onLayoutChange}) {
|
||||
export default function WidgetCanvas({layout, onLayoutChange, isEditable, isDelete, DeleteWidget}) {
|
||||
console.log("rendering widgetcanvas");
|
||||
// Define widgets initial position and size
|
||||
// TODO Add dynamic adding/deleting of widgets via options button
|
||||
const recipe = useSlotRecipe({ key: "widget" });
|
||||
@@ -24,26 +25,26 @@ export default function WidgetCanvas({layout, onLayoutChange}) {
|
||||
};
|
||||
|
||||
return (
|
||||
<chakra.div width="100%">
|
||||
<ResponsiveGridLayout
|
||||
className="layout"
|
||||
layout={layout}
|
||||
cols={12} // Total columns in grid
|
||||
rowHeight={30} //Height of a single row in px
|
||||
isBounded={true} // Total width of the grid
|
||||
margin={[10, 10]} // [horizontal, vertical] gap
|
||||
onLayoutChange={onLayoutChange}
|
||||
draggableHandle=".widget-handle" // Make only the header draggable
|
||||
onDragStart={handleDragStart}
|
||||
onDragStop={handleDragStop}
|
||||
>
|
||||
{layout.map((item) => (
|
||||
// this flex wrapper allows the widget to shrink down to the header size, no less
|
||||
<Flex key={item.i} css={styles.root} height="100%" >
|
||||
<Widget title="VDAP Widget" />
|
||||
</Flex>
|
||||
))}
|
||||
</ResponsiveGridLayout>
|
||||
</chakra.div>
|
||||
);
|
||||
<chakra.div width="100%">
|
||||
<ResponsiveGridLayout
|
||||
className="layout"
|
||||
layout={layout}
|
||||
cols={12} // Total columns in grid
|
||||
rowHeight={30} //Height of a single row in px
|
||||
isBounded={true} // Total width of the grid
|
||||
margin={[10, 10]} // [horizontal, vertical] gap
|
||||
onLayoutChange={onLayoutChange}
|
||||
draggableHandle=".widget-handle" // Make only the header draggable
|
||||
onDragStart={handleDragStart}
|
||||
onDragStop={handleDragStop}
|
||||
>
|
||||
{layout.map((item) => (
|
||||
// this flex wrapper allows the widget to shrink down to the header size, no less
|
||||
<Flex key={item.i} css={styles.root} height="100%" >
|
||||
<Widget id={item.i} title="VDAP Widget" isEditable={isEditable} isDelete={isDelete} DeleteWidget={DeleteWidget}/>
|
||||
</Flex>
|
||||
))}
|
||||
</ResponsiveGridLayout>
|
||||
</chakra.div>
|
||||
);
|
||||
}
|
||||
|
||||
+3
-1
@@ -70,7 +70,9 @@ const widgetRecipe = defineSlotRecipe({
|
||||
bg: "base200",
|
||||
cursor: "move",
|
||||
borderRadius: "sm",
|
||||
whiteSpace: "nowrap"
|
||||
whiteSpace: "nowrap",
|
||||
textAlign: "left",
|
||||
pl: "2"
|
||||
},
|
||||
canvas: {
|
||||
flex: "1 1 auto",
|
||||
|
||||
Reference in New Issue
Block a user