diff --git a/frontend/app-development/features/dataModelling/DataModelling.test.tsx b/frontend/app-development/features/dataModelling/DataModelling.test.tsx index cc836136fef..6c5f2972107 100644 --- a/frontend/app-development/features/dataModelling/DataModelling.test.tsx +++ b/frontend/app-development/features/dataModelling/DataModelling.test.tsx @@ -7,7 +7,10 @@ import { ServicesContextProvider } from 'app-shared/contexts/ServicesContext'; import { queriesMock } from 'app-shared/mocks/queriesMock'; import { createQueryClientMock } from 'app-shared/mocks/queryClientMock'; import { QueryKey } from 'app-shared/types/QueryKey'; -import { jsonMetadata1Mock } from '../../../packages/schema-editor/test/mocks/metadataMocks'; +import { + jsonMetadata1Mock, + xsdMetadata1Mock, +} from '../../../packages/schema-editor/test/mocks/metadataMocks'; import type { QueryClient } from '@tanstack/react-query'; import userEvent from '@testing-library/user-event'; import { createApiErrorMock } from 'app-shared/mocks/apiErrorMock'; @@ -174,4 +177,13 @@ describe('DataModelling', () => { expect(screen.getByTitle(textMock('data_modelling.loading'))).toBeInTheDocument(); }, ); + + it('Should call useAddXsdMutation when Xsd is loaded', async () => { + const queryClient = createQueryClientMock(); + queryClient.setQueryData([QueryKey.DataModelsXsd, org, app], [xsdMetadata1Mock]); + render({}, queryClient); + await waitForElementToBeRemoved(() => screen.queryByTitle(textMock('data_modelling.loading'))); + + expect(queriesMock.addXsdFromRepo).toHaveBeenCalledTimes(1); + }); }); diff --git a/frontend/app-development/features/dataModelling/SchemaEditorWithToolbar/SchemaEditorWithToolbar.tsx b/frontend/app-development/features/dataModelling/SchemaEditorWithToolbar/SchemaEditorWithToolbar.tsx index 82ff8bfcf5d..7c6cba15a82 100644 --- a/frontend/app-development/features/dataModelling/SchemaEditorWithToolbar/SchemaEditorWithToolbar.tsx +++ b/frontend/app-development/features/dataModelling/SchemaEditorWithToolbar/SchemaEditorWithToolbar.tsx @@ -1,11 +1,13 @@ import classes from './SchemaEditorWithToolbar.module.css'; import { TopToolbar } from './TopToolbar'; import { LandingPagePanel } from './LandingPagePanel'; -import React, { useState } from 'react'; +import React, { useEffect, useState } from 'react'; import type { MetadataOption } from '../../../types/MetadataOption'; import { SelectedSchemaEditor } from './SelectedSchemaEditor'; import type { DataModelMetadata } from 'app-shared/types/DataModelMetadata'; import { SchemaGenerationErrorsPanel } from './SchemaGenerationErrorsPanel'; +import { useAddXsdMutation } from '../../../hooks/mutations/useAddXsdMutation'; +import { isXsdFile } from 'app-shared/utils/filenameUtils'; export interface SchemaEditorWithToolbarProps { createPathOption?: boolean; @@ -19,8 +21,22 @@ export const SchemaEditorWithToolbar = ({ const [createNewOpen, setCreateNewOpen] = useState(false); const [selectedOption, setSelectedOption] = useState(undefined); const [schemaGenerationErrorMessages, setSchemaGenerationErrorMessages] = useState([]); + const { mutate: addXsdFromRepo } = useAddXsdMutation(); - const modelPath = selectedOption?.value.repositoryRelativeUrl; + const existingSelectedOption = dataModels.some( + (model) => model.fileName === selectedOption?.value.fileName, + ) + ? selectedOption + : undefined; + const modelPath = existingSelectedOption?.value?.repositoryRelativeUrl; + + useEffect(() => { + dataModels.forEach((model) => { + if (model.repositoryRelativeUrl && isXsdFile(model.repositoryRelativeUrl)) { + addXsdFromRepo(model.repositoryRelativeUrl); + } + }); + }, [dataModels, addXsdFromRepo]); return (
@@ -28,7 +44,7 @@ export const SchemaEditorWithToolbar = ({ createNewOpen={createNewOpen} createPathOption={createPathOption} dataModels={dataModels} - selectedOption={selectedOption} + selectedOption={existingSelectedOption} setCreateNewOpen={setCreateNewOpen} setSelectedOption={setSelectedOption} onSetSchemaGenerationErrorMessages={(errorMessages: string[]) => diff --git a/frontend/app-development/features/dataModelling/SchemaEditorWithToolbar/TopToolbar/TopToolbar.tsx b/frontend/app-development/features/dataModelling/SchemaEditorWithToolbar/TopToolbar/TopToolbar.tsx index e479b383f30..9139f7af038 100644 --- a/frontend/app-development/features/dataModelling/SchemaEditorWithToolbar/TopToolbar/TopToolbar.tsx +++ b/frontend/app-development/features/dataModelling/SchemaEditorWithToolbar/TopToolbar/TopToolbar.tsx @@ -33,6 +33,7 @@ export function TopToolbar({ onSetSchemaGenerationErrorMessages, }: TopToolbarProps) { const modelPath = selectedOption?.value.repositoryRelativeUrl; + const { t } = useTranslation(); const { mutate: createDataModel } = useCreateDataModelMutation(); const prevDataModels = usePrevious(dataModels); diff --git a/frontend/app-development/hooks/mutations/useAddXsdMutation.test.ts b/frontend/app-development/hooks/mutations/useAddXsdMutation.test.ts new file mode 100644 index 00000000000..ed25bc638e7 --- /dev/null +++ b/frontend/app-development/hooks/mutations/useAddXsdMutation.test.ts @@ -0,0 +1,20 @@ +import { waitFor } from '@testing-library/react'; +import { renderHookWithProviders } from '../../test/mocks'; +import { useAddXsdMutation } from './useAddXsdMutation'; +import { queriesMock } from 'app-shared/mocks/queriesMock'; +import { app, org } from '@studio/testing/testids'; + +// Test data: +const modelPath: string = 'modelPath'; + +describe('useAddXsdMutation', () => { + it('Calls useAddXsdMutation with correct arguments', async () => { + const addDataModelXsd = renderHookWithProviders()(() => useAddXsdMutation()).renderHookResult + .result; + await addDataModelXsd.current.mutateAsync(modelPath); + await waitFor(() => expect(addDataModelXsd.current.isSuccess).toBe(true)); + + expect(queriesMock.addXsdFromRepo).toHaveBeenCalledTimes(1); + expect(queriesMock.addXsdFromRepo).toHaveBeenCalledWith(org, app, modelPath); + }); +}); diff --git a/frontend/app-development/hooks/mutations/useAddXsdMutation.ts b/frontend/app-development/hooks/mutations/useAddXsdMutation.ts new file mode 100644 index 00000000000..91483daa8cd --- /dev/null +++ b/frontend/app-development/hooks/mutations/useAddXsdMutation.ts @@ -0,0 +1,21 @@ +import { useMutation, useQueryClient } from '@tanstack/react-query'; +import { QueryKey } from 'app-shared/types/QueryKey'; +import { useServicesContext } from 'app-shared/contexts/ServicesContext'; +import { StringUtils } from '@studio/pure-functions'; +import { useStudioEnvironmentParams } from 'app-shared/hooks/useStudioEnvironmentParams'; + +export const useAddXsdMutation = () => { + const { addXsdFromRepo } = useServicesContext(); + const queryClient = useQueryClient(); + const { org, app } = useStudioEnvironmentParams(); + + return useMutation({ + mutationFn: (modelPath: string) => + addXsdFromRepo(org, app, StringUtils.removeStart(modelPath, '/')), + onSuccess: () => { + queryClient.invalidateQueries({ queryKey: [QueryKey.DataModelsJson, org, app] }).then(); + queryClient.invalidateQueries({ queryKey: [QueryKey.DataModelsXsd, org, app] }).then(); + queryClient.invalidateQueries({ queryKey: [QueryKey.JsonSchema, org, app] }).then(); + }, + }); +}; diff --git a/frontend/app-development/hooks/queries/useSchemaQuery.test.ts b/frontend/app-development/hooks/queries/useSchemaQuery.test.ts index 6d5018e36d2..c921578f031 100644 --- a/frontend/app-development/hooks/queries/useSchemaQuery.test.ts +++ b/frontend/app-development/hooks/queries/useSchemaQuery.test.ts @@ -7,8 +7,6 @@ import { app, org } from '@studio/testing/testids'; // Test data: const jsonModelPathWithSlash = '/App/models/model.schema.json'; -const xsdModelPath = 'App/models/model.xsd'; -const xsdModelPathWithSlash = '/' + xsdModelPath; describe('useSchemaQuery', () => { afterEach(jest.clearAllMocks); @@ -23,19 +21,5 @@ describe('useSchemaQuery', () => { await waitFor(() => expect(result.current.isSuccess).toBe(true)); expect(queriesMock.getDataModel).toHaveBeenCalledTimes(1); expect(queriesMock.getDataModel).toHaveBeenCalledWith(org, app, jsonModelPathWithSlash); - expect(queriesMock.addXsdFromRepo).not.toHaveBeenCalled(); - }); - - it('Calls addXsdFromRepo with correct arguments when XSD', async () => { - const { - renderHookResult: { result }, - } = renderHookWithProviders( - {}, - createQueryClientMock(), - )(() => useSchemaQuery(xsdModelPathWithSlash)); - await waitFor(() => expect(result.current.isSuccess).toBe(true)); - expect(queriesMock.addXsdFromRepo).toHaveBeenCalledTimes(1); - expect(queriesMock.addXsdFromRepo).toHaveBeenCalledWith(org, app, xsdModelPath); - expect(queriesMock.getDataModel).not.toHaveBeenCalled(); }); }); diff --git a/frontend/app-development/hooks/queries/useSchemaQuery.ts b/frontend/app-development/hooks/queries/useSchemaQuery.ts index 8d2545a3eb9..c535d6e63cf 100644 --- a/frontend/app-development/hooks/queries/useSchemaQuery.ts +++ b/frontend/app-development/hooks/queries/useSchemaQuery.ts @@ -4,8 +4,6 @@ import { QueryKey } from 'app-shared/types/QueryKey'; import { useServicesContext } from 'app-shared/contexts/ServicesContext'; import type { AxiosError } from 'axios'; import type { JsonSchema } from 'app-shared/types/JsonSchema'; -import { isXsdFile } from 'app-shared/utils/filenameUtils'; -import { StringUtils } from '@studio/pure-functions'; import { useStudioEnvironmentParams } from 'app-shared/hooks/useStudioEnvironmentParams'; import type { ApiError } from 'app-shared/types/api/ApiError'; @@ -13,13 +11,10 @@ export const useSchemaQuery = ( modelPath: string, ): UseQueryResult> => { const { org, app } = useStudioEnvironmentParams(); - const { getDataModel, addXsdFromRepo } = useServicesContext(); + const { getDataModel } = useServicesContext(); return useQuery>({ queryKey: [QueryKey.JsonSchema, org, app, modelPath], - queryFn: async (): Promise => - isXsdFile(modelPath) - ? addXsdFromRepo(org, app, StringUtils.removeStart(modelPath, '/')) - : getDataModel(org, app, modelPath), + queryFn: async (): Promise => getDataModel(org, app, modelPath), structuralSharing: false, }); }; diff --git a/frontend/packages/shared/src/hooks/mutations/useResetRepositoryMutation.ts b/frontend/packages/shared/src/hooks/mutations/useResetRepositoryMutation.ts index 5e28c1e994e..d7116949661 100644 --- a/frontend/packages/shared/src/hooks/mutations/useResetRepositoryMutation.ts +++ b/frontend/packages/shared/src/hooks/mutations/useResetRepositoryMutation.ts @@ -14,7 +14,9 @@ export const useResetRepositoryMutation = (owner: string, app: string) => { q.invalidateQueries({ queryKey: [QueryKey.FormLayouts, owner, app] }), q.invalidateQueries({ queryKey: [QueryKey.FormLayoutSettings, owner, app] }), q.invalidateQueries({ queryKey: [QueryKey.TextResources, owner, app] }), - q.invalidateQueries({ queryKey: [QueryKey.JsonSchema, owner, app] }), + q.invalidateQueries({ queryKey: [QueryKey.DataModelsJson, owner, app] }), + q.invalidateQueries({ queryKey: [QueryKey.DataModelsXsd, owner, app] }), + q.removeQueries({ queryKey: [QueryKey.JsonSchema, owner, app] }), ]), }); };