Add global variable functionality and display on Global Variables page

This commit is contained in:
anovazzi1 2024-02-05 18:30:41 -03:00
commit 4a1c9f8dae
3 changed files with 71 additions and 62 deletions

View file

@ -7,7 +7,6 @@ import { classNames } from "../../utils/utils";
import IconComponent from "../genericIconComponent"; import IconComponent from "../genericIconComponent";
import { Input } from "../ui/input"; import { Input } from "../ui/input";
export default function InputComponent({ export default function InputComponent({
autoFocus = false, autoFocus = false,
onBlur, onBlur,
@ -27,8 +26,13 @@ export default function InputComponent({
const [pwdVisible, setPwdVisible] = useState(false); const [pwdVisible, setPwdVisible] = useState(false);
const refInput = useRef<HTMLInputElement>(null); const refInput = useRef<HTMLInputElement>(null);
const [showOptions, setShowOptions] = useState<boolean>(false); const [showOptions, setShowOptions] = useState<boolean>(false);
const [filteredOpts, setFilteredValue] = useState<string[]>(["key", "key2", "key3", "key4", "key5"]); const [filteredOpts, setFilteredValue] = useState<string[]>([
"key",
"key2",
"key3",
"key4",
"key5",
]);
// Clear component state // Clear component state
useEffect(() => { useEffect(() => {
@ -37,16 +41,15 @@ export default function InputComponent({
} }
}, [disabled]); }, [disabled]);
const filteredOptions = filteredOpts.filter((option) =>
const filteredOptions = filteredOpts.filter((option) => option.toLowerCase().includes(value.toLowerCase())) option.toLowerCase().includes(value.toLowerCase())
);
function onInputLostFocus(event): void { function onInputLostFocus(event): void {
if (onBlur) onBlur(event); if (onBlur) onBlur(event);
setShowOptions(false); setShowOptions(false);
} }
return ( return (
<div className="relative w-full"> <div className="relative w-full">
{isForm ? ( {isForm ? (
@ -136,8 +139,7 @@ export default function InputComponent({
false ? "mb-2 w-[250px]" : "absolute w-full" false ? "mb-2 w-[250px]" : "absolute w-full"
)} )}
> >
{filteredOptions.map( {filteredOptions.map((option, id) => (
(option, id) => (
<Listbox.Option <Listbox.Option
key={id} key={id}
className={({ active }) => className={({ active }) =>
@ -155,9 +157,7 @@ export default function InputComponent({
<> <>
<span <span
className={classNames( className={classNames(
selected selected ? "font-semibold" : "font-normal",
? "font-semibold"
: "font-normal",
"block truncate " "block truncate "
)} )}
data-testid={`${option}-${id ?? ""}-option`} data-testid={`${option}-${id ?? ""}-option`}
@ -165,7 +165,6 @@ export default function InputComponent({
{option} {option}
</span> </span>
{selected ? ( {selected ? (
<span <span
className={classNames( className={classNames(
@ -187,8 +186,7 @@ export default function InputComponent({
</> </>
)} )}
</Listbox.Option> </Listbox.Option>
) ))}
)}
</Listbox.Options> </Listbox.Options>
</Transition> </Transition>
</div> </div>
@ -199,7 +197,6 @@ export default function InputComponent({
</> </>
)} )}
<span <span
className={ className={
password password
@ -214,7 +211,6 @@ export default function InputComponent({
/> />
</span> </span>
{password && ( {password && (
<button <button
type="button" type="button"

View file

@ -5,14 +5,20 @@ import { Label } from "../../../components/ui/label";
import { Textarea } from "../../../components/ui/textarea"; import { Textarea } from "../../../components/ui/textarea";
import { registerGlobalVariable } from "../../../controllers/API"; import { registerGlobalVariable } from "../../../controllers/API";
import BaseModal from "../../../modals/baseModal"; import BaseModal from "../../../modals/baseModal";
import { useGlobalVariablesStore } from "../../../stores/globalVariables";
//TODO IMPLEMENT FORM LOGIC //TODO IMPLEMENT FORM LOGIC
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 addGlobalVariable = useGlobalVariablesStore(
(state) => state.addGlobalVariable
);
function handleSaveVariable() { function handleSaveVariable() {
registerGlobalVariable(key, value).then((_) => {}); registerGlobalVariable(key, value).then((_) => {
addGlobalVariable(key, value);
});
} }
return ( return (
<BaseModal size="small"> <BaseModal size="small">

View file

@ -3,16 +3,23 @@ import { useGlobalVariablesStore } from "../../stores/globalVariables";
import AddNewVariableButton from "./components/addNewVariableButton"; import AddNewVariableButton from "./components/addNewVariableButton";
export default function GlobalVariablesPage() { export default function GlobalVariablesPage() {
const globalVariables = useGlobalVariablesStore( const globalVariablesEntries = useGlobalVariablesStore(
(state) => state.globalVariables (state) => state.globalVariablesEntries
); );
return ( return (
<PageLayout <PageLayout
title="Variables" title="Variables"
description="set your own personal varaibles and use it on your flow" description="set your own personal varaibles and use it on your flow"
> >
{Object.keys(globalVariables).length > 0 ? ( {globalVariablesEntries.length > 0 ? (
<div></div> <div className="flex h-full w-full flex-col justify-around">
{globalVariablesEntries.map((key, index) => (
<div className="flex w-full items-start" key={index}>
<span>{key}</span>
</div>
))}
<AddNewVariableButton />
</div>
) : ( ) : (
<div className="flex h-full w-full flex-col items-center justify-center align-middle"> <div className="flex h-full w-full flex-col items-center justify-center align-middle">
<div> <div>