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