diff --git a/console/src/features/DataRelationships/DatabaseRelationshipsTab.tsx b/console/src/features/DataRelationships/DatabaseRelationshipsTab.tsx index 41a0a250537..44c0afea120 100644 --- a/console/src/features/DataRelationships/DatabaseRelationshipsTab.tsx +++ b/console/src/features/DataRelationships/DatabaseRelationshipsTab.tsx @@ -230,6 +230,7 @@ export const DatabaseRelationshipsTab = ({ }} onComplete={onComplete} driver={driver} + onClose={closeForm} /> ) : null} diff --git a/console/src/features/DataRelationships/components/DatabaseRelationshipsTable/DatabaseRelationshipTable.tsx b/console/src/features/DataRelationships/components/DatabaseRelationshipsTable/DatabaseRelationshipTable.tsx index 38f8c1d652d..44b804b73f6 100644 --- a/console/src/features/DataRelationships/components/DatabaseRelationshipsTable/DatabaseRelationshipTable.tsx +++ b/console/src/features/DataRelationships/components/DatabaseRelationshipsTable/DatabaseRelationshipTable.tsx @@ -168,7 +168,13 @@ export const DatabaseRelationshipsTable = ({ className="flex px-2 py-0.5 items-center font-semibold rounded text-secondary mr-0.5 hover:bg-indigo-50 focus:bg-indigo-100" > - Edit + {[ + 'toLocalTableFk', + 'toSameTableFk', + 'toLocalTableManual', + ].includes(relationship.type) + ? 'Rename' + : 'Edit'} diff --git a/console/src/features/DataRelationships/components/Form/EditRelationshipForm.tsx b/console/src/features/DataRelationships/components/Form/EditRelationshipForm.tsx index dd3b129cae9..adc5f92b732 100644 --- a/console/src/features/DataRelationships/components/Form/EditRelationshipForm.tsx +++ b/console/src/features/DataRelationships/components/Form/EditRelationshipForm.tsx @@ -13,6 +13,7 @@ import { } from '@/features/DataSource'; import { RemoteDBRelationshipWidget } from '../RemoteDBRelationshipWidget'; import { LocalRelationshipWidget } from '../LocalDBRelationshipWidget'; +import { RenameRelationship } from '../RenameRelationship/RenameRelationship'; type EditRelationshipFormProps = { driver: Driver; @@ -27,6 +28,7 @@ type EditRelationshipFormProps = { message?: string; type: 'success' | 'error' | 'cancel'; }) => void; + onClose?: () => void; }; export const EditRelationshipForm = ({ @@ -34,6 +36,7 @@ export const EditRelationshipForm = ({ driver, existingRelationship, onComplete, + onClose, }: EditRelationshipFormProps) => { const { data: relationship, isLoading } = useFindRelationship({ dataSourceName: existingRelationship.mapping.from.source, @@ -44,6 +47,20 @@ export const EditRelationshipForm = ({ if (!relationship) return <>Relationship Not found in metadata>; + if ( + existingRelationship.type === 'toLocalTableFk' || + existingRelationship.type === 'toLocalTableManual' || + existingRelationship.type === 'toSameTableFk' + ) { + return ( + + ); + } + if (isRemoteDBRelationship(relationship)) { return ( ); } + return ( void; + onClose?: () => void; } export const Form = ({ @@ -27,6 +28,7 @@ export const Form = ({ sourceTableInfo, onComplete, driver, + onClose, }: Props) => { if (existingRelationship) { return ( @@ -36,6 +38,7 @@ export const Form = ({ sourceTableInfo={sourceTableInfo} existingRelationship={existingRelationship} onComplete={onComplete} + onClose={onClose} /> ); diff --git a/console/src/features/DataRelationships/components/ManualLocalRelationshipWidget/ManualLocalRelationshipWidget.tsx b/console/src/features/DataRelationships/components/ManualLocalRelationshipWidget/ManualLocalRelationshipWidget.tsx index a6fbc231001..ecc2a263f9f 100644 --- a/console/src/features/DataRelationships/components/ManualLocalRelationshipWidget/ManualLocalRelationshipWidget.tsx +++ b/console/src/features/DataRelationships/components/ManualLocalRelationshipWidget/ManualLocalRelationshipWidget.tsx @@ -101,30 +101,34 @@ export const ManualLocalRelationshipWidget = ( - - - - - - - - + {mode === 'create' && ( + + + - - - - - + + + + + + + + + + + + + + + + + - - - - - + )} void; + onError?: (err: unknown) => void; +}) => { + const { renameRelationship, isLoading } = useRenameRelationship(); + + return ( + + { + renameRelationship({ + values: { + newName: data.name, + relationshipName: relationship.name, + fromTable: relationship.mapping.from.table, + fromSource: relationship.mapping.from.source, + }, + onSuccess, + onError, + }); + }} + options={{ + defaultValues: { + name: relationship.name, + }, + }} + > + {() => ( + + + + Rename + + + )} + + + ); +}; diff --git a/console/src/features/DataRelationships/components/RenameRelationship/useRenameRelationship.tsx b/console/src/features/DataRelationships/components/RenameRelationship/useRenameRelationship.tsx new file mode 100644 index 00000000000..7468e651b80 --- /dev/null +++ b/console/src/features/DataRelationships/components/RenameRelationship/useRenameRelationship.tsx @@ -0,0 +1,98 @@ +import { exportMetadata } from '@/features/DataSource'; +import { Table, useMetadataMigration } from '@/features/MetadataAPI'; +import { useHttpClient } from '@/features/Network'; +import { useFireNotification } from '@/new-components/Notifications'; +import { useCallback } from 'react'; +import { useQueryClient } from 'react-query'; + +type EditManualLocalRelationshipPayload = { + newName: string; + relationshipName: string; + fromTable: Table; + fromSource: string; +}; + +export const useRenameRelationship = () => { + const httpClient = useHttpClient(); + const { fireNotification } = useFireNotification(); + const { mutate, ...rest } = useMetadataMigration(); + const queryClient = useQueryClient(); + + const renameRelationship = useCallback( + async ({ + values, + onSuccess, + onError, + }: { + values: EditManualLocalRelationshipPayload; + onSuccess?: () => void; + onError?: (err: unknown) => void; + }) => { + try { + const { resource_version, metadata } = await exportMetadata({ + httpClient, + }); + + if (!metadata) throw Error('Unable to fetch metadata'); + + const metadataSource = metadata.sources.find( + s => s.name === values.fromSource + ); + + if (!metadataSource) throw Error('Unable to fetch metadata source'); + + const driver = metadataSource.kind; + + const type = 'rename_relationship'; + + mutate( + { + query: { + resource_version, + type: `${driver}_${type}`, + args: { + table: values.fromTable, + source: values.fromSource, + name: values.relationshipName, + new_name: values.newName, + }, + }, + }, + { + onSuccess: () => { + queryClient.refetchQueries([ + values.fromSource, + 'list_all_relationships', + ]); + + onSuccess?.(); + + fireNotification({ + type: 'success', + title: 'Success!', + message: 'A relationship renamed succesfully!', + }); + }, + onError: err => { + onError?.(err); + fireNotification({ + type: 'error', + title: 'Failed to rename relationship', + message: err?.message, + }); + }, + } + ); + } catch (err) { + fireNotification({ + title: 'Error', + type: 'error', + message: JSON.stringify(err), + }); + } + }, + [fireNotification, httpClient, mutate, queryClient] + ); + + return { renameRelationship, ...rest }; +}; diff --git a/console/src/features/ManageAgents/hooks/useListAvailableAgentsFromMetadata.ts b/console/src/features/ManageAgents/hooks/useListAvailableAgentsFromMetadata.ts index 139ac2131bb..6f9e6bceb0f 100644 --- a/console/src/features/ManageAgents/hooks/useListAvailableAgentsFromMetadata.ts +++ b/console/src/features/ManageAgents/hooks/useListAvailableAgentsFromMetadata.ts @@ -37,5 +37,6 @@ export const useListAvailableAgentsFromMetadata = () => { return result; }, + refetchOnWindowFocus: false, }); };