From b69b23e14fd83df8003712d7ea2a3654f9a6de0b Mon Sep 17 00:00:00 2001 From: Sedrak Adoyan Date: Wed, 28 Feb 2024 00:40:01 +0300 Subject: [PATCH 01/10] fix: bug --- src/components/DataModelGeneration/index.tsx | 12 ++++++++++++ src/components/TableSelection/index.tsx | 10 +++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/components/DataModelGeneration/index.tsx b/src/components/DataModelGeneration/index.tsx index 57bf36e9..f4bc4e1e 100644 --- a/src/components/DataModelGeneration/index.tsx +++ b/src/components/DataModelGeneration/index.tsx @@ -75,6 +75,18 @@ const DataModelGeneration: FC = ({ }; const onFormSubmit = (data: DynamicForm, type: string) => { + Object.keys(data).forEach((k) => { + if (k === "type") return; + if (typeof data[k] === "object") { + Object.keys(data[k]).forEach((kk: string) => { + console.log(kk); + const descriptor = Object.getOwnPropertyDescriptor(data[k], kk)!; + Object.defineProperty(data[k], kk.replace(`${k}.`, ""), descriptor); + delete data[k][kk]; + }); + } + }); + onSubmit(data, type); if (resetOnSubmit) reset(); }; diff --git a/src/components/TableSelection/index.tsx b/src/components/TableSelection/index.tsx index b4b00f75..46f9be6a 100644 --- a/src/components/TableSelection/index.tsx +++ b/src/components/TableSelection/index.tsx @@ -40,18 +40,18 @@ const TableSelection: FC = ({ }); const isAllSelected = () => - Object.keys(schema[path]).every((tb) => value[tb] === true); + Object.keys(schema[path]).every((tb) => value[`${path}.${tb}`] === true); const onClear = () => { const newVal: DynamicForm = {}; - Object.keys(value).forEach((k) => (newVal[k] = false)); + Object.keys(value).forEach((k) => (newVal[`${path}.${k}`] = false)); onChange(newVal); }; const onSelectAll = (e: CheckboxChangeEvent) => { if (!e.target.checked) return onClear(); const newVal: DynamicForm = {}; - Object.keys(schema[path]).forEach((tb) => (newVal[tb] = true)); + Object.keys(schema[path]).forEach((tb) => (newVal[`${path}.${tb}`] = true)); onChange(newVal); }; @@ -69,9 +69,9 @@ const TableSelection: FC = ({
{ - onChange({ ...value, [tb]: e.target.checked }); + onChange({ ...value, [`${path}.${tb}`]: e.target.checked }); }} > Date: Wed, 28 Feb 2024 00:49:40 +0300 Subject: [PATCH 02/10] fix: remove console.log --- src/components/DataModelGeneration/index.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/DataModelGeneration/index.tsx b/src/components/DataModelGeneration/index.tsx index f4bc4e1e..319371c6 100644 --- a/src/components/DataModelGeneration/index.tsx +++ b/src/components/DataModelGeneration/index.tsx @@ -79,7 +79,6 @@ const DataModelGeneration: FC = ({ if (k === "type") return; if (typeof data[k] === "object") { Object.keys(data[k]).forEach((kk: string) => { - console.log(kk); const descriptor = Object.getOwnPropertyDescriptor(data[k], kk)!; Object.defineProperty(data[k], kk.replace(`${k}.`, ""), descriptor); delete data[k][kk]; From 8108af5f65a96526aac3114822aa4c23d135ce83 Mon Sep 17 00:00:00 2001 From: Sedrak Adoyan Date: Wed, 28 Feb 2024 01:04:51 +0300 Subject: [PATCH 03/10] refactor: DataModelGeneration submit function --- src/components/DataModelGeneration/index.tsx | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/components/DataModelGeneration/index.tsx b/src/components/DataModelGeneration/index.tsx index 319371c6..7dd6fb85 100644 --- a/src/components/DataModelGeneration/index.tsx +++ b/src/components/DataModelGeneration/index.tsx @@ -75,17 +75,14 @@ const DataModelGeneration: FC = ({ }; const onFormSubmit = (data: DynamicForm, type: string) => { - Object.keys(data).forEach((k) => { - if (k === "type") return; - if (typeof data[k] === "object") { - Object.keys(data[k]).forEach((kk: string) => { - const descriptor = Object.getOwnPropertyDescriptor(data[k], kk)!; - Object.defineProperty(data[k], kk.replace(`${k}.`, ""), descriptor); - delete data[k][kk]; - }); + Object.keys(data).forEach((db) => { + if (typeof data?.[db] === "object") { + data[db] = Object.keys(data[db]).reduce((res: any, table) => ({ + ...res, + [table.replace(`${db}.`, "")]: data[db][table], + })); } }); - onSubmit(data, type); if (resetOnSubmit) reset(); }; From 0d372ef5e146fbae18639f5f454d88f0a3bd77eb Mon Sep 17 00:00:00 2001 From: Sedrak Adoyan Date: Wed, 28 Feb 2024 01:05:35 +0300 Subject: [PATCH 04/10] refactor: rename var --- src/components/DataModelGeneration/index.tsx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/components/DataModelGeneration/index.tsx b/src/components/DataModelGeneration/index.tsx index 7dd6fb85..eeeb3ffb 100644 --- a/src/components/DataModelGeneration/index.tsx +++ b/src/components/DataModelGeneration/index.tsx @@ -75,11 +75,11 @@ const DataModelGeneration: FC = ({ }; const onFormSubmit = (data: DynamicForm, type: string) => { - Object.keys(data).forEach((db) => { - if (typeof data?.[db] === "object") { - data[db] = Object.keys(data[db]).reduce((res: any, table) => ({ + Object.keys(data).forEach((scope) => { + if (typeof data?.[scope] === "object") { + data[scope] = Object.keys(data[scope]).reduce((res: any, table) => ({ ...res, - [table.replace(`${db}.`, "")]: data[db][table], + [table.replace(`${scope}.`, "")]: data[scope][table], })); } }); From 94ab40f6d119b5f34a46a99a9ca9dae516c8563f Mon Sep 17 00:00:00 2001 From: Ivan Fokeev Date: Sun, 7 Apr 2024 22:55:20 +0300 Subject: [PATCH 05/10] fix: loading --- src/components/DataModelGeneration/index.tsx | 21 ++++++++++++-------- src/components/TableSelection/index.tsx | 4 +++- src/hooks/useOnboarding.ts | 8 +++++--- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/components/DataModelGeneration/index.tsx b/src/components/DataModelGeneration/index.tsx index eeeb3ffb..d7458f3b 100644 --- a/src/components/DataModelGeneration/index.tsx +++ b/src/components/DataModelGeneration/index.tsx @@ -3,6 +3,7 @@ import { useTranslation } from "react-i18next"; import cn from "classnames"; import { useResponsive } from "ahooks"; import { useForm } from "react-hook-form"; +import { init } from "i18next"; import Input from "@/components/Input"; import type { @@ -57,6 +58,7 @@ const DataModelGeneration: FC = ({ }) => { const { t } = useTranslation(["dataModelGeneration", "common"]); + console.log(initialValue); const { control, handleSubmit, watch, reset } = useForm({ values: initialValue, }); @@ -74,15 +76,18 @@ const DataModelGeneration: FC = ({ } }; + console.log("loading", loading); + const onFormSubmit = (data: DynamicForm, type: string) => { - Object.keys(data).forEach((scope) => { - if (typeof data?.[scope] === "object") { - data[scope] = Object.keys(data[scope]).reduce((res: any, table) => ({ - ...res, - [table.replace(`${scope}.`, "")]: data[scope][table], - })); - } - }); + console.log("formsubmit", data); + // Object.keys(data).forEach((scope) => { + // if (typeof data?.[scope] === "object") { + // data[scope] = Object.keys(data[scope]).reduce((res: any, table) => ({ + // ...res, + // [table.replace(`${scope}.`, "")]: data[scope][table], + // })); + // } + // }); onSubmit(data, type); if (resetOnSubmit) reset(); }; diff --git a/src/components/TableSelection/index.tsx b/src/components/TableSelection/index.tsx index 46f9be6a..415cdad7 100644 --- a/src/components/TableSelection/index.tsx +++ b/src/components/TableSelection/index.tsx @@ -39,6 +39,8 @@ const TableSelection: FC = ({ defaultValue: initialValue, }); + console.log(value); + const isAllSelected = () => Object.keys(schema[path]).every((tb) => value[`${path}.${tb}`] === true); @@ -66,7 +68,7 @@ const TableSelection: FC = ({
{Object.keys(schema[path]).map((tb) => ( -
+
{ () => teamData?.dataSources || [], [teamData] ) as DataSourceInfo[]; + const curDataSource = useMemo( () => - dataSources.find((d) => d.id === editId || d.id === dataSourceSetup?.id), + dataSources.find((d) => d.id === editId) || + dataSources.find((d) => d.id === dataSourceSetup?.id), [editId, dataSourceSetup?.id, dataSources] ); @@ -321,10 +323,10 @@ export default ({ editId }: Props) => { }, [curDataSource, dataSourceSetup?.id, dataSources, editId]); useEffect(() => { - if (step === 2 && dataSourceSetup?.id && !schema) { + if (step === 2 && curDataSource?.id && !schema) { execFetchTables(); } - }, [dataSourceSetup?.id, schema, step, execFetchTables]); + }, [curDataSource?.id, schema, step, execFetchTables]); useEffect(() => { if (fetchTablesQuery.data) { From 957e83b42ef1d40163a7118c05e466fd801328af Mon Sep 17 00:00:00 2001 From: Sedrak Adoyan Date: Mon, 8 Apr 2024 01:04:15 +0300 Subject: [PATCH 06/10] fix --- src/components/ApiSetup/index.tsx | 2 +- src/components/DataModelGeneration/index.tsx | 19 +++++++++++-------- src/pages/DataSources/index.tsx | 7 +------ src/stores/DataSourceStore.ts | 3 ++- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/components/ApiSetup/index.tsx b/src/components/ApiSetup/index.tsx index 745cbb72..7d7ddd2b 100644 --- a/src/components/ApiSetup/index.tsx +++ b/src/components/ApiSetup/index.tsx @@ -106,7 +106,7 @@ const ApiSetup: FC = ({ password = initialValue?.password, database = initialValue?.db, }) => { - const [hostname, port] = host.split(":"); + const [hostname, port] = host?.split(":"); const portOption = port ? `-P ${port}` : ""; const psqlPortOption = port ? `--port=${port}` : ""; diff --git a/src/components/DataModelGeneration/index.tsx b/src/components/DataModelGeneration/index.tsx index d7458f3b..ba5b9b15 100644 --- a/src/components/DataModelGeneration/index.tsx +++ b/src/components/DataModelGeneration/index.tsx @@ -79,15 +79,18 @@ const DataModelGeneration: FC = ({ console.log("loading", loading); const onFormSubmit = (data: DynamicForm, type: string) => { + Object.keys(data).forEach((scope) => { + if (typeof data?.[scope] === "object") { + data[scope] = Object.keys(data[scope]).reduce( + (res: any, table: any) => ({ + ...res, + [table.replace(`${scope}.`, "")]: data[scope][table], + }), + {} + ); + } + }); console.log("formsubmit", data); - // Object.keys(data).forEach((scope) => { - // if (typeof data?.[scope] === "object") { - // data[scope] = Object.keys(data[scope]).reduce((res: any, table) => ({ - // ...res, - // [table.replace(`${scope}.`, "")]: data[scope][table], - // })); - // } - // }); onSubmit(data, type); if (resetOnSubmit) reset(); }; diff --git a/src/pages/DataSources/index.tsx b/src/pages/DataSources/index.tsx index 87ecacd3..8342fb8c 100644 --- a/src/pages/DataSources/index.tsx +++ b/src/pages/DataSources/index.tsx @@ -255,6 +255,7 @@ const DataSourcesWrapper = () => { clean, nextStep, setFormStateData, + isOnboarding, } = DataSourceStore(); const { @@ -332,12 +333,6 @@ const DataSourcesWrapper = () => { await onDataSourceSetupSubmit(data, true, nextStep); }; - useEffect(() => { - if (connect) { - setIsOnboarding(true); - } - }, [connect, setIsOnboarding]); - useEffect(() => { if (!connect && editId && !generate) { setStep(1); diff --git a/src/stores/DataSourceStore.ts b/src/stores/DataSourceStore.ts index 3da53aaf..c2d40144 100644 --- a/src/stores/DataSourceStore.ts +++ b/src/stores/DataSourceStore.ts @@ -81,7 +81,8 @@ const dataSourceStore = create((set) => ({ ...prev, isOnboarding: value, })), - clean: () => set({ ...defaultState }), + clean: () => + set((prev) => ({ ...defaultState, isOnboarding: prev.isOnboarding })), })); export default dataSourceStore; From 763542a3d51cf304953e4b3aba5312317f51812d Mon Sep 17 00:00:00 2001 From: Sedrak Adoyan Date: Mon, 8 Apr 2024 02:03:01 +0300 Subject: [PATCH 07/10] fix: data model generation modal on page update --- src/components/DataSourceForm/index.tsx | 1 - src/hooks/useOnboarding.ts | 10 +++------- src/pages/DataSources/index.tsx | 2 -- src/stores/DataSourceStore.ts | 2 ++ 4 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/components/DataSourceForm/index.tsx b/src/components/DataSourceForm/index.tsx index a6ae021d..016a8deb 100644 --- a/src/components/DataSourceForm/index.tsx +++ b/src/components/DataSourceForm/index.tsx @@ -43,7 +43,6 @@ const DataSourceForm: FC = ({ }) => { const { t } = useTranslation(["dataSourceStepForm"]); const { step } = DataSourceStore(); - return ( { clean, setSchema, setFormStateData, + formState, } = DataSourceStore(); const [fetchTablesQuery, execFetchTables] = useFetchTablesQuery({ - variables: { id: dataSourceSetup?.id }, + variables: { id: editId }, pause: true, }); @@ -309,12 +310,7 @@ export default ({ editId }: Props) => { ...defaultFormState, ...prev.formState, step0: curDataSource.type, - step1: { - id: curDataSource?.id, - db_params: { ...curDataSource.dbParams }, - name: curDataSource.name, - ...prev.formState.step1, - }, + step1: curDataSource, }, branchId: currentBranch.id, } as Partial) diff --git a/src/pages/DataSources/index.tsx b/src/pages/DataSources/index.tsx index 8342fb8c..3af9e8c1 100644 --- a/src/pages/DataSources/index.tsx +++ b/src/pages/DataSources/index.tsx @@ -251,11 +251,9 @@ const DataSourcesWrapper = () => { isGenerate, setIsGenerate, setStep, - setIsOnboarding, clean, nextStep, setFormStateData, - isOnboarding, } = DataSourceStore(); const { diff --git a/src/stores/DataSourceStore.ts b/src/stores/DataSourceStore.ts index c2d40144..e84cefca 100644 --- a/src/stores/DataSourceStore.ts +++ b/src/stores/DataSourceStore.ts @@ -33,6 +33,7 @@ export interface DataSourceState extends DataSourceData { setIsGenerate: (value: boolean) => void; setIsOnboarding: (value: boolean) => void; clean: () => void; + reset: (set: (newValue: DataSourceState) => void) => void; } export const defaultFormState = { @@ -83,6 +84,7 @@ const dataSourceStore = create((set) => ({ })), clean: () => set((prev) => ({ ...defaultState, isOnboarding: prev.isOnboarding })), + reset: set as any, })); export default dataSourceStore; From a4b4964339038456b6c0770281582d705eb305a3 Mon Sep 17 00:00:00 2001 From: Sedrak Adoyan Date: Mon, 8 Apr 2024 22:50:05 +0300 Subject: [PATCH 08/10] fix: remove datasource state cleaning --- src/hooks/useOnboarding.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/hooks/useOnboarding.ts b/src/hooks/useOnboarding.ts index 913c5808..353b5daf 100644 --- a/src/hooks/useOnboarding.ts +++ b/src/hooks/useOnboarding.ts @@ -330,12 +330,6 @@ export default ({ editId }: Props) => { } }, [fetchTablesQuery.data, setSchema]); - useEffect(() => { - if (step === 0) { - clean(); - } - }, [clean, step]); - const loading = createMutation.fetching || checkConnectionMutation.fetching || From 8ee86374392d5892873c9185b2dfd47d0fd8df81 Mon Sep 17 00:00:00 2001 From: Sedrak Adoyan Date: Mon, 8 Apr 2024 22:56:53 +0300 Subject: [PATCH 09/10] fix: datasource form autocomplete --- src/hooks/useOnboarding.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/hooks/useOnboarding.ts b/src/hooks/useOnboarding.ts index 353b5daf..376550b3 100644 --- a/src/hooks/useOnboarding.ts +++ b/src/hooks/useOnboarding.ts @@ -310,7 +310,12 @@ export default ({ editId }: Props) => { ...defaultFormState, ...prev.formState, step0: curDataSource.type, - step1: curDataSource, + step1: { + id: curDataSource?.id, + db_params: { ...curDataSource.dbParams }, + name: curDataSource.name, + ...prev.formState.step1, + }, }, branchId: currentBranch.id, } as Partial) From 18b3d6fa80b3f1b858965c066a79ce5316b61e54 Mon Sep 17 00:00:00 2001 From: Sedrak Adoyan Date: Tue, 9 Apr 2024 00:02:45 +0300 Subject: [PATCH 10/10] fix: onboarding --- src/components/ApiSetup/index.tsx | 13 ++- src/components/DataSourceFormBody/index.tsx | 1 + src/hooks/useOnboarding.ts | 109 ++++++++++++-------- src/pages/Onboarding/index.tsx | 7 +- 4 files changed, 83 insertions(+), 47 deletions(-) diff --git a/src/components/ApiSetup/index.tsx b/src/components/ApiSetup/index.tsx index 7d7ddd2b..2aa6704e 100644 --- a/src/components/ApiSetup/index.tsx +++ b/src/components/ApiSetup/index.tsx @@ -106,7 +106,7 @@ const ApiSetup: FC = ({ password = initialValue?.password, database = initialValue?.db, }) => { - const [hostname, port] = host?.split(":"); + const [hostname, port] = host ? host?.split?.(":") : []; const portOption = port ? `-P ${port}` : ""; const psqlPortOption = port ? `--port=${port}` : ""; @@ -239,7 +239,16 @@ const ApiSetup: FC = ({
= ({ ); case 3: const initialValue = formState?.step3 || formData?.step3; + return ( { } }; - const prepareSqlApiData = ( - dataSourceName?: string, - dataSourceId?: string - ): { apiConfig: ApiSetupForm; credentialParams: CredentialParams } => { - const apiConfig: ApiSetupForm = prepareInitValues( - dataSourceId, - currentUser.id, - dataSourceName - ); - const credentialParams: CredentialParams = { - user_id: currentUser.id, - username: apiConfig.db_username, - password: apiConfig.password, - }; - - if (dataSourceId) { - credentialParams.datasource_id = dataSourceId; - } + const prepareSqlApiData = useCallback( + ( + dataSourceName?: string, + dataSourceId?: string + ): { apiConfig: ApiSetupForm; credentialParams: CredentialParams } => { + const apiConfig: ApiSetupForm = prepareInitValues( + dataSourceId, + currentUser.id, + dataSourceName + ); + const credentialParams: CredentialParams = { + user_id: currentUser.id, + username: apiConfig.db_username, + password: apiConfig.password, + }; - return { apiConfig, credentialParams }; - }; + if (dataSourceId) { + credentialParams.datasource_id = dataSourceId; + } - const tryInsertSqlCredentials = async ( - dataSourceId?: string, - dataSourceName?: string, - attemptsLeft: number = 5 - ): Promise => { - if (attemptsLeft === 0) { - return false; - } + return { apiConfig, credentialParams }; + }, + [currentUser.id] + ); - const { apiConfig, credentialParams } = prepareSqlApiData( - dataSourceName, - dataSourceId - ); - const res = await execInsertSqlCredentialsMutation({ - object: credentialParams, - }); + const tryInsertSqlCredentials = useCallback( + async ( + dataSourceId?: string, + dataSourceName?: string, + attemptsLeft: number = 5 + ): Promise => { + if (attemptsLeft === 0) { + return false; + } - const newCredentials = res.data?.insert_sql_credentials_one; - if (res.error || !newCredentials) { - return await tryInsertSqlCredentials( - dataSourceId, + const { apiConfig, credentialParams } = prepareSqlApiData( dataSourceName, - attemptsLeft - 1 + dataSourceId ); - } + const res = await execInsertSqlCredentialsMutation({ + object: credentialParams, + }); - return apiConfig; - }; + const newCredentials = res.data?.insert_sql_credentials_one; + if (res.error || !newCredentials) { + return await tryInsertSqlCredentials( + dataSourceId, + dataSourceName, + attemptsLeft - 1 + ); + } + + DataSourceStore.setState((prev) => ({ + ...prev, + formState: { + ...prev.formState, + step3: { ...apiConfig }, + }, + })); + + return apiConfig; + }, + [execInsertSqlCredentialsMutation, prepareSqlApiData] + ); const createOrUpdateDataSource = async (data: DataSourceSetupForm) => { let dataSourceId; @@ -335,6 +349,15 @@ export default ({ editId }: Props) => { } }, [fetchTablesQuery.data, setSchema]); + useEffect(() => { + if (step === 3) { + tryInsertSqlCredentials( + curDataSource?.id as string | undefined, + curDataSource?.name as string | undefined + ); + } + }, [curDataSource?.id, curDataSource?.name, step, tryInsertSqlCredentials]); + const loading = createMutation.fetching || checkConnectionMutation.fetching || diff --git a/src/pages/Onboarding/index.tsx b/src/pages/Onboarding/index.tsx index c48dffaa..14360da3 100644 --- a/src/pages/Onboarding/index.tsx +++ b/src/pages/Onboarding/index.tsx @@ -41,7 +41,7 @@ const Onboarding: React.FC = ({ onDataModelGenerationSubmit = () => {}, }) => { const responsive = useResponsive(); - const { currentUser } = CurrentUserStore(); + const { currentUser, teamData } = CurrentUserStore(); const header = ( { const [, setLocation] = useLocation(); const basePath = ONBOARDING; + const { teamData } = CurrentUserStore(); const step = useMemo(() => parseInt(pageStep || "0", 10) - 1, [pageStep]); const { @@ -91,7 +92,9 @@ const OnboardingWrapper = () => { } = DataSourceStore(); const { loading, onDataSourceSetupSubmit, onDataModelGenerationSubmit } = - useOnboarding({}); + useOnboarding({ + editId: teamData?.dataSources?.[0]?.id as string | undefined, + }); const onFinish = () => { clean();