feat(typesContext.tsx): add support for saving components to local storage

- Added import statements for necessary types and utility functions
- Added `saveComponent` function to the `typesContextType` interface
- Implemented `saveComponent` function to save a component to local storage

fix(nodeToolbarComponent/index.tsx): fix typo in case statement
- Fixed typo in case statement for "Download" option

feat(nodeToolbarComponent/index.tsx): add logic to handle "SaveAll" option
- Added logic to handle the "SaveAll" option in the switch statement
- Added condition to check if user is authenticated
- Added condition to check if user is auto-logged in
- Added console logs for debugging purposes

feat(entities/index.ts): add type definition for local storage user data
- Added `localStorageUserType` type definition to represent the structure of user data stored in local storage

feat(typesContext/index.ts): add type definition for node data
- Added `NodeDataType` type definition to represent the structure of node data

feat(utils.ts): add utility function to check if local storage key exists
- Added `checkLocalStorageKey` function to check if a given key exists in local storage
This commit is contained in:
anovazzi1 2023-09-25 20:59:04 -03:00
commit 00e25c9495
5 changed files with 53 additions and 1 deletions

View file

@ -8,7 +8,10 @@ import {
import { Node, ReactFlowInstance } from "reactflow";
import { getAll, getHealth } from "../controllers/API";
import { APIKindType } from "../types/api";
import { localStorageUserType } from "../types/entities";
import { NodeDataType } from "../types/flow";
import { typesContextType } from "../types/typesContext";
import { checkLocalStorageKey } from "../utils/utils";
import { alertContext } from "./alertContext";
import { AuthContext } from "./authContext";
@ -28,6 +31,7 @@ const initialValue: typesContextType = {
fetchError: false,
setFilterEdge: (filter) => {},
getFilterEdge: [],
saveComponent: (component: NodeDataType, key: string) => {},
};
export const typesContext = createContext<typesContextType>(initialValue);
@ -102,9 +106,23 @@ export function TypesProvider({ children }: { children: ReactNode }) {
.filter((edge) => edge.source !== idx && edge.target !== idx)
);
}
function saveComponent(component: NodeDataType, id: string) {
if (checkLocalStorageKey(id)) {
let savedComponents = localStorage.getItem(id)!;
let savedComponentsJSON: localStorageUserType =
JSON.parse(savedComponents);
let components = savedComponentsJSON.components;
components[component.type];
savedComponentsJSON.components = components;
localStorage.setItem(id, JSON.stringify(savedComponentsJSON));
}
}
return (
<typesContext.Provider
value={{
saveComponent,
types,
setTypes,
reactFlowInstance,

View file

@ -8,6 +8,7 @@ import {
SelectItem,
SelectTrigger,
} from "../../../../components/ui/select-custom";
import { AuthContext } from "../../../../contexts/authContext";
import { TabsContext } from "../../../../contexts/tabsContext";
import EditNodeModal from "../../../../modals/EditNodeModal";
import { nodeToolbarPropsType } from "../../../../types/components";
@ -41,6 +42,7 @@ export default function NodeToolbarComponent({
).length
);
const updateNodeInternals = useUpdateNodeInternals();
const { isAuthenticated, autoLogin, userData } = useContext(AuthContext);
function canMinimize() {
let countHandles: number = 0;
@ -65,10 +67,21 @@ export default function NodeToolbarComponent({
setShowNode((prev) => !prev);
updateNodeInternals(data.id);
break;
case "SaveAll":
case "Download":
//TODO add logic to save node on backend and update toolbar
downloadNode(createFlowComponent(data));
break;
case "SaveAll":
if (isAuthenticated) {
if (autoLogin) {
console.log("save all");
} else {
}
}
console.log(isAuthenticated);
console.log(userData);
console.log(autoLogin);
break;
}
};
@ -178,6 +191,15 @@ export default function NodeToolbarComponent({
Save{" "}
</div>{" "}
</SelectItem>
<SelectItem value={"Download"}>
<div className="flex">
<IconComponent
name="Download"
className="relative top-0.5 mr-2 h-4 w-4"
/>{" "}
Download{" "}
</div>{" "}
</SelectItem>
{isMinimal && (
<SelectItem value={"show"}>
<div className="flex">

View file

@ -1,6 +1,12 @@
import { NodeDataType } from "../flow";
export type sidebarNavigationItemType = {
name: string;
href: string;
icon: React.ForwardRefExoticComponent<React.SVGProps<SVGSVGElement>>;
current: boolean;
};
export type localStorageUserType = {
components: { [key: string]: NodeDataType };
};

View file

@ -1,6 +1,7 @@
import { Edge, Node, ReactFlowInstance } from "reactflow";
import { AlertItemType } from "../alerts";
import { APIClassType, APIDataType } from "../api";
import { NodeDataType } from "../flow";
const types: { [char: string]: string } = {};
const template: { [char: string]: APIClassType } = {};
@ -20,6 +21,7 @@ export type typesContextType = {
setFetchError: (newState: boolean) => void;
setFilterEdge: (newState) => void;
getFilterEdge: any[];
saveComponent: (component: NodeDataType, key: string) => void;
};
export type alertContextType = {

View file

@ -525,3 +525,7 @@ export function tabsArray(codes: string[], method: number) {
},
];
}
export function checkLocalStorageKey(key: string): boolean {
return localStorage.getItem(key) !== null;
}