Refactor global variables page and add delete functionality

This commit is contained in:
anovazzi1 2024-02-05 18:50:58 -03:00
commit d0dcb32a7e
2 changed files with 22 additions and 1 deletions

View file

@ -12,16 +12,20 @@ import { useGlobalVariablesStore } from "../../../stores/globalVariables";
export default function AddNewVariableButton(): JSX.Element { export default function AddNewVariableButton(): JSX.Element {
const [key, setKey] = useState(""); const [key, setKey] = useState("");
const [value, setValue] = useState(""); const [value, setValue] = useState("");
const [open, setOpen] = useState(false);
const addGlobalVariable = useGlobalVariablesStore( const addGlobalVariable = useGlobalVariablesStore(
(state) => state.addGlobalVariable (state) => state.addGlobalVariable
); );
function handleSaveVariable() { function handleSaveVariable() {
registerGlobalVariable(key, value).then((_) => { registerGlobalVariable(key, value).then((_) => {
addGlobalVariable(key, value); addGlobalVariable(key, value);
setKey("");
setValue("");
setOpen(false);
}); });
} }
return ( return (
<BaseModal size="small"> <BaseModal open={open} setOpen={setOpen} size="small">
<BaseModal.Header <BaseModal.Header
description={"write a text variable to use anywhere on your flow"} description={"write a text variable to use anywhere on your flow"}
> >

View file

@ -1,11 +1,23 @@
import ShadTooltip from "../../components/ShadTooltipComponent";
import IconComponent from "../../components/genericIconComponent";
import PageLayout from "../../components/pageLayout"; import PageLayout from "../../components/pageLayout";
import { deleteGlobalVariable } from "../../controllers/API";
import { useGlobalVariablesStore } from "../../stores/globalVariables"; import { useGlobalVariablesStore } from "../../stores/globalVariables";
import AddNewVariableButton from "./components/addNewVariableButton"; import AddNewVariableButton from "./components/addNewVariableButton";
//TODO: improve UI
export default function GlobalVariablesPage() { export default function GlobalVariablesPage() {
const globalVariablesEntries = useGlobalVariablesStore( const globalVariablesEntries = useGlobalVariablesStore(
(state) => state.globalVariablesEntries (state) => state.globalVariablesEntries
); );
const removeGlobalVariable = useGlobalVariablesStore(
(state) => state.removeGlobalVariable
);
function handleDelete(key: string) {
deleteGlobalVariable(key).then((_) => removeGlobalVariable(key));
}
return ( return (
<PageLayout <PageLayout
title="Variables" title="Variables"
@ -16,6 +28,11 @@ export default function GlobalVariablesPage() {
{globalVariablesEntries.map((key, index) => ( {globalVariablesEntries.map((key, index) => (
<div className="flex w-full items-start" key={index}> <div className="flex w-full items-start" key={index}>
<span>{key}</span> <span>{key}</span>
<ShadTooltip content="Delete">
<button onClick={(_) => handleDelete(key)} className="ml-auto">
<IconComponent name="Trash2" />
</button>
</ShadTooltip>
</div> </div>
))} ))}
<AddNewVariableButton /> <AddNewVariableButton />