From e1b2743294dbafe073bba943b2638d833800a80c Mon Sep 17 00:00:00 2001 From: Devika Date: Wed, 25 Jun 2025 12:39:52 +0530 Subject: [PATCH] integrated the api to fetch the refernce keys in the Add Table Modal in ER Diagram --- src/components/AddTableModal.jsx | 98 +++++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 3 deletions(-) diff --git a/src/components/AddTableModal.jsx b/src/components/AddTableModal.jsx index 2284fa1..7e055d5 100644 --- a/src/components/AddTableModal.jsx +++ b/src/components/AddTableModal.jsx @@ -87,6 +87,10 @@ const AddTableModal = ({ const [columnTypesFromAPI, setColumnTypesFromAPI] = useState([]); const [loadingColumnTypes, setLoadingColumnTypes] = useState(false); + // Table keys from API + const [tableKeysFromAPI, setTableKeysFromAPI] = useState([]); + const [loadingTableKeys, setLoadingTableKeys] = useState(false); + // Validation and UI state const [errors, setErrors] = useState({}); const [isSubmitting, setIsSubmitting] = useState(false); @@ -184,6 +188,44 @@ const AddTableModal = ({ } }; + // Fetch table keys from API for a specific table + const fetchTableKeys = async (tableSlug) => { + if (!tableSlug) { + setTableKeysFromAPI([]); + return; + } + + setLoadingTableKeys(true); + try { + const response = await fetch('https://sandbox.kezel.io/api/qbt_table_key_list_get', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + token: "abdhsg", + org: "sN05Pjv11qvH", + tbl: tableSlug + }) + }); + const data = await response.json(); + + if (data.status === 200 && data.items) { + setTableKeysFromAPI(data.items); + } else { + console.error('Failed to fetch table keys:', data.message); + // Fallback to empty array + setTableKeysFromAPI([]); + } + } catch (error) { + console.error('Error fetching table keys:', error); + // Fallback to empty array + setTableKeysFromAPI([]); + } finally { + setLoadingTableKeys(false); + } + }; + const resetForm = () => { setFormData({ name: '', @@ -198,6 +240,8 @@ const AddTableModal = ({ setLoadingKeyTypes(false); setColumnTypesFromAPI([]); setLoadingColumnTypes(false); + setTableKeysFromAPI([]); + setLoadingTableKeys(false); setErrors({}); setIsSubmitting(false); setKeysViewMode('keys'); @@ -365,6 +409,7 @@ const AddTableModal = ({ targetTable: '', sourceKey: '', targetKey: '', + tableKey: '', // New field for table key from API relationType: '1:N' // 1:1, 1:N, N:M }; setRelations(prev => [...prev, newRelation]); @@ -376,6 +421,25 @@ const AddTableModal = ({ )); }; + // Handle target table selection change + const handleTargetTableChange = (relationId, tableId) => { + // Update the relation with the new target table + updateRelation(relationId, 'targetTable', tableId); + + // Clear the table key selection when target table changes + updateRelation(relationId, 'tableKey', ''); + + // Find the selected table to get its slug + const selectedTable = existingTables.find(table => table.id === tableId); + if (selectedTable && selectedTable.tbl) { + // Fetch table keys for the selected table + fetchTableKeys(selectedTable.tbl); + } else { + // Clear table keys if no table is selected + setTableKeysFromAPI([]); + } + }; + const removeRelation = (id) => { setRelations(prev => prev.filter(rel => rel.id !== id)); }; @@ -527,6 +591,7 @@ const AddTableModal = ({ target_table: rel.targetTable, source_key: rel.sourceKey, target_key: rel.targetKey, + table_key: rel.tableKey, relation_type: rel.relationType })) }; @@ -1025,12 +1090,12 @@ const AddTableModal = ({ {relations.map((relation) => ( - + Target Table + + + Table Key + + + {/* Relation Type @@ -1088,7 +1180,7 @@ const AddTableModal = ({ */} - + removeRelation(relation.id)} color="error"