From 7aa91492849d8a3d2f835c6c655fb7829571dc89 Mon Sep 17 00:00:00 2001 From: Riza Semih Koca Date: Sat, 2 Mar 2024 11:58:20 +0300 Subject: [PATCH] connectionform.tsx zustand --- .../components/connections/connectionform.tsx | 114 ++++++++++-------- 1 file changed, 65 insertions(+), 49 deletions(-) diff --git a/WebsiteApplication/client/components/connections/connectionform.tsx b/WebsiteApplication/client/components/connections/connectionform.tsx index b035e26..7e9b197 100644 --- a/WebsiteApplication/client/components/connections/connectionform.tsx +++ b/WebsiteApplication/client/components/connections/connectionform.tsx @@ -1,44 +1,66 @@ -import {Button, Card, CardBody, CardFooter, Input, Link, Tab, Tabs, Textarea, useDisclosure} from "@nextui-org/react"; +import {Card, CardBody, Input, Link, Tab, Tabs, Textarea} from "@nextui-org/react"; import {DatabaseCards} from "@/components/connections/databasecards"; import React from "react"; -import {Key} from "@react-types/shared"; import {parseDatabaseAndServer} from "@/lib/utils"; - +import {useUrlFormStore, useHostFormStore, useTabStore} from "@/lib/stores/connectionformstore"; export const ConnectionForm = () => { - const [selectedTab, setSelectedTab] = React.useState("host"); - const [databaseType, setDatabaseType] = React.useState("") - const [port, setPort] = React.useState("1234"); // Add state for port number - const [databaseName, setDatabaseName] = React.useState("") - const [host, setHost] = React.useState("") - const [url, setUrl] = React.useState(""); + const [selectedTab, setSelectedTab] = useTabStore(state => [state.selectedTab, state.setSelectedTab]); + const [defaultPort, setDefaultPort] = React.useState("1234"); + const [parsedServerFromUrl, setParsedServerFromUrl] = React.useState("null"); + const [parsedDatabaseFromUrl, setParsedDatabaseFromUrl] = React.useState("null"); + const [ + setHostDatabaseName, + setHostDatabaseType, + setHostUsername, + setHostPassword, + setHost, + setPort + ] = useHostFormStore(state => [ + state.setHostDatabaseName, + state.setHostDatabaseType, + state.setHostUsername, + state.setHostPassword, + state.setHost, + state.setPort + ]); - const handleUrlChange = (value:string) => { - setUrl(value); - const {database, server} = parseDatabaseAndServer(value) - setDatabaseName(database) - setHost(server) - } + const [ + Url, + setUrlDatabaseName, + setUrlDatabaseType, + setUrlUsername, + setUrlPassword, + setUrl + ] = useUrlFormStore(state => [ + state.Url, + state.setUrlDatabaseName, + state.setUrlDatabaseType, + state.setUrlUsername, + state.setUrlPassword, + state.setUrl + ]); - const updatePort = (databaseType: string) => { + const updateDefaultPort = (databaseType: string) => { switch (databaseType) { case "MYSQL": - setPort("3306"); + setDefaultPort("3306"); break; case "POSTGRES": - setPort("5432"); + setDefaultPort("5432"); break; case "MSSQL": - setPort("1433"); + setDefaultPort("1433"); break; case "ORACLE": - setPort("1521"); + setDefaultPort("1521"); break; default: - setPort("1234"); + setDefaultPort("1234"); break; } }; + return (
@@ -56,13 +78,14 @@ export const ConnectionForm = () => {
{ - setDatabaseType(selectedDatabase); - updatePort(selectedDatabase); // Update port based on selected database + setHostDatabaseType(selectedDatabase); + updateDefaultPort(selectedDatabase); // Update port based on selected database }} imageSizeClasses={"h-10 w-10"} baseCardClasses={"size-24"}/>
setHostDatabaseName(value)} className={"flex-2"} size="md" isRequired @@ -71,15 +94,17 @@ export const ConnectionForm = () => { type="text" /> setPort(value)} className={"flex-1"} isRequired label="Port" - placeholder={`eg. ${port}`} + placeholder={`eg. ${defaultPort}`} type="text" />
setHost(value)} className={"flex-1"} size="md" isRequired @@ -90,12 +115,14 @@ export const ConnectionForm = () => {
setHostUsername(value)} className={"flex-1"} isRequired label="Username" placeholder="Enter your username" type="text"/> setHostPassword(value)} className={"flex-1"} isRequired label="Password" @@ -117,8 +144,8 @@ export const ConnectionForm = () => { { - setDatabaseType(selectedDatabase); - updatePort(selectedDatabase); // Update port based on selected database + setUrlDatabaseType(selectedDatabase); + updateDefaultPort(selectedDatabase); // Update port based on selected database }} imageSizeClasses={"h-10 w-10"} baseCardClasses={"size-24"}/> @@ -128,23 +155,31 @@ export const ConnectionForm = () => { rows={3} isRequired label="Url" - value={url} - onValueChange={(value: string) => handleUrlChange(value)} - labelPlacement="outside" + value={Url} + onValueChange={(value: string) => { + setUrl(value); + const {database, server} = parseDatabaseAndServer(value); + setUrlDatabaseName(database); + setParsedDatabaseFromUrl(database); + setParsedServerFromUrl(server); + }} + placeholder="Enter your url eg. Server=myServerAddress;Port=1234;Database=myDataBase;Uid=myUsername;Pwd=myPassword;" className="flex-1" /> -

Database: {databaseName ?? "null"} Host: {host ?? "null"}

+

Database: {parsedDatabaseFromUrl ?? "not found"} Host: {parsedServerFromUrl ?? "not found"}

setUrlUsername(value)} className={"flex-1"} isRequired label="Username" placeholder="Enter your username" type="text"/> setUrlPassword(value)} className={"flex-1"} isRequired label="Password" @@ -162,25 +197,6 @@ export const ConnectionForm = () => { - {/**/} - {/*
*/} - {/* */} - {/* */} - {/* */} - {/*

*/} - {/* Already have an account?{" "}*/} - {/* setSelectedTab("host")}>*/} - {/* Host*/} - {/* */} - {/*

*/} - - {/**/} - {/*
*/}