diff --git a/frontend/app-development/features/appContentLibrary/AppContentLibrary.test.tsx b/frontend/app-development/features/appContentLibrary/AppContentLibrary.test.tsx index c3537873930..33f988cc2ce 100644 --- a/frontend/app-development/features/appContentLibrary/AppContentLibrary.test.tsx +++ b/frontend/app-development/features/appContentLibrary/AppContentLibrary.test.tsx @@ -11,7 +11,7 @@ import type { UserEvent } from '@testing-library/user-event'; import userEvent from '@testing-library/user-event'; import type { CodeList } from '@studio/components'; import type { ServicesContextProps } from 'app-shared/contexts/ServicesContext'; -import type { OptionsListsResponse } from 'app-shared/types/api/OptionsLists'; +import type { OptionListData } from 'app-shared/types/OptionList'; const uploadCodeListButtonTextMock = 'Upload Code List'; const updateCodeListButtonTextMock = 'Update Code List'; @@ -19,7 +19,7 @@ const updateCodeListIdButtonTextMock = 'Update Code List Id'; const codeListNameMock = 'codeListNameMock'; const newCodeListNameMock = 'newCodeListNameMock'; const codeListMock: CodeList = [{ value: '', label: '' }]; -const optionListsDataMock: OptionsListsResponse = [{ title: codeListNameMock, data: codeListMock }]; +const optionListsDataMock: OptionListData[] = [{ title: codeListNameMock, data: codeListMock }]; jest.mock( '../../../libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage', () => ({ @@ -147,7 +147,7 @@ const goToLibraryPage = async (user: UserEvent, libraryPage: string) => { type renderAppContentLibraryProps = { queries?: Partial; shouldPutDataOnCache?: boolean; - optionListsData?: OptionsListsResponse; + optionListsData?: OptionListData[]; }; const renderAppContentLibrary = ({ diff --git a/frontend/app-development/features/appContentLibrary/AppContentLibrary.tsx b/frontend/app-development/features/appContentLibrary/AppContentLibrary.tsx index e9be23e893d..c021cbc821f 100644 --- a/frontend/app-development/features/appContentLibrary/AppContentLibrary.tsx +++ b/frontend/app-development/features/appContentLibrary/AppContentLibrary.tsx @@ -3,7 +3,7 @@ import { ResourceContentLibraryImpl } from '@studio/content-library'; import React from 'react'; import { useOptionListsQuery, useOptionListsReferencesQuery } from 'app-shared/hooks/queries'; import { useStudioEnvironmentParams } from 'app-shared/hooks/useStudioEnvironmentParams'; -import { convertOptionsListsDataToCodeListsData } from './utils/convertOptionsListsDataToCodeListsData'; +import { mapToCodeListDataList } from './utils/mapToCodeListDataList'; import { StudioPageSpinner } from '@studio/components'; import { useTranslation } from 'react-i18next'; import type { ApiError } from 'app-shared/types/api/ApiError'; @@ -16,7 +16,7 @@ import { useUpdateOptionListIdMutation, useDeleteOptionListMutation, } from 'app-shared/hooks/mutations'; -import { mapToCodeListsUsage } from './utils/mapToCodeListsUsage'; +import { mapToCodeListUsages } from './utils/mapToCodeListUsages'; export function AppContentLibrary(): React.ReactElement { const { org, app } = useStudioEnvironmentParams(); @@ -31,15 +31,15 @@ export function AppContentLibrary(): React.ReactElement { }); const { mutate: updateOptionList } = useUpdateOptionListMutation(org, app); const { mutate: updateOptionListId } = useUpdateOptionListIdMutation(org, app); - const { data: optionListsUsages, isPending: optionListsUsageIsPending } = + const { data: optionListUsages, isPending: optionListsUsageIsPending } = useOptionListsReferencesQuery(org, app); if (optionListsDataPending || optionListsUsageIsPending) return ; - const codeListsData = convertOptionsListsDataToCodeListsData(optionListsData); + const codeListsData = mapToCodeListDataList(optionListsData); - const codeListsUsages: CodeListReference[] = mapToCodeListsUsage({ optionListsUsages }); + const codeListsUsages: CodeListReference[] = mapToCodeListUsages(optionListUsages); const handleUpdateCodeListId = (optionListId: string, newOptionListId: string) => { updateOptionListId({ optionListId, newOptionListId }); @@ -59,7 +59,7 @@ export function AppContentLibrary(): React.ReactElement { }; const handleUpdate = ({ title, codeList }: CodeListWithMetadata) => { - updateOptionList({ optionListId: title, optionsList: codeList }); + updateOptionList({ optionListId: title, optionList: codeList }); }; const { getContentResourceLibrary } = new ResourceContentLibraryImpl({ diff --git a/frontend/app-development/features/appContentLibrary/utils/convertOptionsListsDataToCodeListsData.ts b/frontend/app-development/features/appContentLibrary/utils/convertOptionsListsDataToCodeListsData.ts deleted file mode 100644 index e9d386db542..00000000000 --- a/frontend/app-development/features/appContentLibrary/utils/convertOptionsListsDataToCodeListsData.ts +++ /dev/null @@ -1,20 +0,0 @@ -import type { OptionsListData, OptionsListsResponse } from 'app-shared/types/api/OptionsLists'; -import type { CodeListData } from '@studio/content-library'; - -export const convertOptionsListsDataToCodeListsData = (optionListsData: OptionsListsResponse) => { - const codeListsData = []; - optionListsData.map((optionListData) => { - const codeListData = convertOptionsListDataToCodeListData(optionListData); - codeListsData.push(codeListData); - }); - return codeListsData; -}; - -const convertOptionsListDataToCodeListData = (optionListData: OptionsListData) => { - const codeListData: CodeListData = { - title: optionListData.title, - data: optionListData.data, - hasError: optionListData.hasError, - }; - return codeListData; -}; diff --git a/frontend/app-development/features/appContentLibrary/utils/convertOptionsListsDataToCodeListsData.test.ts b/frontend/app-development/features/appContentLibrary/utils/mapToCodeListDataList.test.ts similarity index 62% rename from frontend/app-development/features/appContentLibrary/utils/convertOptionsListsDataToCodeListsData.test.ts rename to frontend/app-development/features/appContentLibrary/utils/mapToCodeListDataList.test.ts index 7d67033df69..1b87582e0f5 100644 --- a/frontend/app-development/features/appContentLibrary/utils/convertOptionsListsDataToCodeListsData.test.ts +++ b/frontend/app-development/features/appContentLibrary/utils/mapToCodeListDataList.test.ts @@ -1,11 +1,11 @@ import type { CodeListData } from '@studio/content-library'; -import { convertOptionsListsDataToCodeListsData } from './convertOptionsListsDataToCodeListsData'; -import type { OptionsListsResponse } from 'app-shared/types/api/OptionsLists'; +import { mapToCodeListDataList } from './mapToCodeListDataList'; +import type { OptionListData } from 'app-shared/types/OptionList'; -describe('convertOptionsListsDataToCodeListsData', () => { +describe('mapToCodeListDataList', () => { it('converts option lists data to code lists data correctly', () => { const optionListId: string = 'optionListId'; - const optionListsData: OptionsListsResponse = [ + const optionListDataList: OptionListData[] = [ { title: optionListId, data: [ @@ -15,7 +15,7 @@ describe('convertOptionsListsDataToCodeListsData', () => { hasError: false, }, ]; - const result: CodeListData[] = convertOptionsListsDataToCodeListsData(optionListsData); + const result: CodeListData[] = mapToCodeListDataList(optionListDataList); expect(result).toEqual([ { title: optionListId, @@ -30,20 +30,20 @@ describe('convertOptionsListsDataToCodeListsData', () => { it('sets hasError to true in result when optionListsResponse returns an option list with error', () => { const optionListId: string = 'optionListId'; - const optionListsData: OptionsListsResponse = [ + const optionListDataList: OptionListData[] = [ { title: optionListId, data: null, hasError: true, }, ]; - const result: CodeListData[] = convertOptionsListsDataToCodeListsData(optionListsData); + const result: CodeListData[] = mapToCodeListDataList(optionListDataList); expect(result).toEqual([{ title: optionListId, data: null, hasError: true }]); }); it('returns a result with empty code list data array when the input option list data is empty', () => { - const optionListsData: OptionsListsResponse = []; - const result: CodeListData[] = convertOptionsListsDataToCodeListsData(optionListsData); + const optionListDataList: OptionListData[] = []; + const result: CodeListData[] = mapToCodeListDataList(optionListDataList); expect(result).toEqual([]); }); }); diff --git a/frontend/app-development/features/appContentLibrary/utils/mapToCodeListDataList.ts b/frontend/app-development/features/appContentLibrary/utils/mapToCodeListDataList.ts new file mode 100644 index 00000000000..f03615e8e3d --- /dev/null +++ b/frontend/app-development/features/appContentLibrary/utils/mapToCodeListDataList.ts @@ -0,0 +1,11 @@ +import type { OptionListData } from 'app-shared/types/OptionList'; +import type { CodeListData } from '@studio/content-library'; + +export const mapToCodeListDataList = (optionListDataList: OptionListData[]): CodeListData[] => + optionListDataList.map(convertOptionListDataToCodeListData); + +const convertOptionListDataToCodeListData = (optionListData: OptionListData): CodeListData => ({ + title: optionListData.title, + data: optionListData.data, + hasError: optionListData.hasError, +}); diff --git a/frontend/app-development/features/appContentLibrary/utils/mapToCodeListUsages.test.ts b/frontend/app-development/features/appContentLibrary/utils/mapToCodeListUsages.test.ts new file mode 100644 index 00000000000..866bdf2ae47 --- /dev/null +++ b/frontend/app-development/features/appContentLibrary/utils/mapToCodeListUsages.test.ts @@ -0,0 +1,35 @@ +import type { CodeListIdSource } from '@studio/content-library'; +import { mapToCodeListUsages } from './mapToCodeListUsages'; +import type { OptionListReferences } from 'app-shared/types/OptionListReferences'; + +const optionListId: string = 'optionListId'; +const optionListIdSources: CodeListIdSource[] = [ + { + layoutSetId: 'layoutSetId', + layoutName: 'layoutName', + componentIds: ['componentId1', 'componentId2'], + }, +]; +const optionListUsages: OptionListReferences = [ + { + optionListId, + optionListIdSources, + }, +]; + +describe('mapToCodeListUsages', () => { + it('maps optionListsUsages to codeListUsages', () => { + const codeListUsages = mapToCodeListUsages(optionListUsages); + expect(codeListUsages).toEqual([ + { + codeListId: optionListId, + codeListIdSources: optionListIdSources, + }, + ]); + }); + + it('maps undefined optionListUsages to empty array', () => { + const codeListUsages = mapToCodeListUsages(undefined); + expect(codeListUsages).toEqual([]); + }); +}); diff --git a/frontend/app-development/features/appContentLibrary/utils/mapToCodeListUsages.ts b/frontend/app-development/features/appContentLibrary/utils/mapToCodeListUsages.ts new file mode 100644 index 00000000000..389a8be27fe --- /dev/null +++ b/frontend/app-development/features/appContentLibrary/utils/mapToCodeListUsages.ts @@ -0,0 +1,12 @@ +import type { OptionListReferences } from 'app-shared/types/OptionListReferences'; +import type { CodeListReference } from '@studio/content-library'; + +export const mapToCodeListUsages = ( + optionListUsages: OptionListReferences, +): CodeListReference[] => { + if (!optionListUsages) return []; + return optionListUsages.map((optionListsUsage) => ({ + codeListId: optionListsUsage.optionListId, + codeListIdSources: optionListsUsage.optionListIdSources, + })); +}; diff --git a/frontend/app-development/features/appContentLibrary/utils/mapToCodeListsUsage.test.ts b/frontend/app-development/features/appContentLibrary/utils/mapToCodeListsUsage.test.ts deleted file mode 100644 index 1be4ee745d7..00000000000 --- a/frontend/app-development/features/appContentLibrary/utils/mapToCodeListsUsage.test.ts +++ /dev/null @@ -1,35 +0,0 @@ -import type { CodeListIdSource } from '@studio/content-library'; -import { mapToCodeListsUsage } from './mapToCodeListsUsage'; -import type { OptionListsReferences } from 'app-shared/types/api/OptionsLists'; - -const optionListId: string = 'optionListId'; -const optionListIdSources: CodeListIdSource[] = [ - { - layoutSetId: 'layoutSetId', - layoutName: 'layoutName', - componentIds: ['componentId1', 'componentId2'], - }, -]; -const optionListsUsages: OptionListsReferences = [ - { - optionListId, - optionListIdSources, - }, -]; - -describe('mapToCodeListsUsage', () => { - it('maps optionListsUsage to codeListUsage', () => { - const codeListUsage = mapToCodeListsUsage({ optionListsUsages }); - expect(codeListUsage).toEqual([ - { - codeListId: optionListId, - codeListIdSources: optionListIdSources, - }, - ]); - }); - - it('maps undefined optionListsUsage to empty array', () => { - const codeListUsage = mapToCodeListsUsage({ optionListsUsages: undefined }); - expect(codeListUsage).toEqual([]); - }); -}); diff --git a/frontend/app-development/features/appContentLibrary/utils/mapToCodeListsUsage.ts b/frontend/app-development/features/appContentLibrary/utils/mapToCodeListsUsage.ts deleted file mode 100644 index 51b13cb1e95..00000000000 --- a/frontend/app-development/features/appContentLibrary/utils/mapToCodeListsUsage.ts +++ /dev/null @@ -1,20 +0,0 @@ -import type { OptionListsReferences } from 'app-shared/types/api/OptionsLists'; -import type { CodeListReference } from '@studio/content-library'; - -type MapToCodeListsUsageProps = { - optionListsUsages: OptionListsReferences; -}; - -export const mapToCodeListsUsage = ({ - optionListsUsages, -}: MapToCodeListsUsageProps): CodeListReference[] => { - const codeListsUsages: CodeListReference[] = []; - if (!optionListsUsages) return codeListsUsages; - optionListsUsages.map((optionListsUsage) => - codeListsUsages.push({ - codeListId: optionListsUsage.optionListId, - codeListIdSources: optionListsUsage.optionListIdSources, - }), - ); - return codeListsUsages; -}; diff --git a/frontend/packages/shared/src/api/queries.ts b/frontend/packages/shared/src/api/queries.ts index afd1c83bb08..2911677abc3 100644 --- a/frontend/packages/shared/src/api/queries.ts +++ b/frontend/packages/shared/src/api/queries.ts @@ -92,7 +92,9 @@ import type { Policy } from 'app-shared/types/Policy'; import type { RepoDiffResponse } from 'app-shared/types/api/RepoDiffResponse'; import type { ExternalImageUrlValidationResponse } from 'app-shared/types/api/ExternalImageUrlValidationResponse'; import type { MaskinportenScopes } from 'app-shared/types/MaskinportenScope'; -import type { OptionListsReferences, OptionsList, OptionsListsResponse } from 'app-shared/types/api/OptionsLists'; +import type { OptionList } from 'app-shared/types/OptionList'; +import type { OptionListsResponse } from 'app-shared/types/api/OptionListsResponse'; +import type { OptionListReferences } from 'app-shared/types/OptionListReferences'; import type { LayoutSetsModel } from '../types/api/dto/LayoutSetsModel'; import type { AccessPackageResource, PolicyAccessPackageAreaGroup } from 'app-shared/types/PolicyAccessPackages'; import type { DataType } from '../types/DataType'; @@ -121,9 +123,9 @@ export const getImageFileNames = (owner: string, app: string) => get(g export const getLayoutNames = (owner: string, app: string) => get(layoutNamesPath(owner, app)); export const getLayoutSets = (owner: string, app: string) => get(layoutSetsPath(owner, app)); export const getLayoutSetsExtended = (owner: string, app: string) => get(layoutSetsPath(owner, app) + '/extended'); -export const getOptionList = (owner: string, app: string, optionsListId: string) => get(optionListPath(owner, app, optionsListId)); -export const getOptionLists = (owner: string, app: string) => get(optionListsPath(owner, app)); -export const getOptionListsReferences = (owner: string, app: string) => get(optionListReferencesPath(owner, app)); +export const getOptionList = (owner: string, app: string, optionsListId: string) => get(optionListPath(owner, app, optionsListId)); +export const getOptionLists = (owner: string, app: string) => get(optionListsPath(owner, app)); +export const getOptionListsReferences = (owner: string, app: string) => get(optionListReferencesPath(owner, app)); export const getOptionListIds = (owner: string, app: string) => get(optionListIdsPath(owner, app)); export const getOrgList = () => get(orgListUrl()); export const getOrganizations = () => get(orgsListPath()); diff --git a/frontend/packages/shared/src/hooks/mutations/useUpdateOptionListIdMutation.test.ts b/frontend/packages/shared/src/hooks/mutations/useUpdateOptionListIdMutation.test.ts index 6b2b67a29a5..ce5928c978b 100644 --- a/frontend/packages/shared/src/hooks/mutations/useUpdateOptionListIdMutation.test.ts +++ b/frontend/packages/shared/src/hooks/mutations/useUpdateOptionListIdMutation.test.ts @@ -6,7 +6,7 @@ import { useUpdateOptionListIdMutation } from './useUpdateOptionListIdMutation'; import { createQueryClientMock } from 'app-shared/mocks/queryClientMock'; import { QueryKey } from 'app-shared/types/QueryKey'; import type { Option } from 'app-shared/types/Option'; -import type { OptionsListsResponse } from 'app-shared/types/api/OptionsLists'; +import type { OptionListsResponse } from 'app-shared/types/api/OptionListsResponse'; // Test data: const optionListId: string = 'optionListId'; @@ -38,7 +38,7 @@ describe('useUpdateOptionListIdMutation', () => { const optionListC = 'optionListC'; const optionListZ = 'optionListZ'; const queryClient = createQueryClientMock(); - const oldData: OptionsListsResponse = [ + const oldData: OptionListsResponse = [ { title: optionListA, data: optionListMock }, { title: optionListB, data: optionListMock }, { title: optionListZ, data: optionListMock }, @@ -52,7 +52,7 @@ describe('useUpdateOptionListIdMutation', () => { optionListId: optionListA, newOptionListId: optionListC, }); - const cacheData: OptionsListsResponse = queryClient.getQueryData([ + const cacheData: OptionListsResponse = queryClient.getQueryData([ QueryKey.OptionLists, org, app, @@ -65,7 +65,7 @@ describe('useUpdateOptionListIdMutation', () => { test('Invalidates the optionListIds query cache', async () => { const queryClient = createQueryClientMock(); const invalidateQueriesSpy = jest.spyOn(queryClient, 'invalidateQueries'); - const oldData: OptionsListsResponse = [ + const oldData: OptionListsResponse = [ { title: 'firstOptionList', data: optionListMock }, { title: 'optionListId', data: optionListMock }, { title: 'lastOptionList', data: optionListMock }, diff --git a/frontend/packages/shared/src/hooks/mutations/useUpdateOptionListIdMutation.ts b/frontend/packages/shared/src/hooks/mutations/useUpdateOptionListIdMutation.ts index 253cc4f7233..3eac0807617 100644 --- a/frontend/packages/shared/src/hooks/mutations/useUpdateOptionListIdMutation.ts +++ b/frontend/packages/shared/src/hooks/mutations/useUpdateOptionListIdMutation.ts @@ -1,5 +1,5 @@ import { QueryKey } from 'app-shared/types/QueryKey'; -import type { OptionsListsResponse } from 'app-shared/types/api/OptionsLists'; +import type { OptionListsResponse } from 'app-shared/types/api/OptionListsResponse'; import { useQueryClient, useMutation } from '@tanstack/react-query'; import { useServicesContext } from 'app-shared/contexts/ServicesContext'; import { ArrayUtils } from '@studio/pure-functions'; @@ -21,7 +21,7 @@ export const useUpdateOptionListIdMutation = (org: string, app: string) => { })); }, onSuccess: ({ optionListId, newOptionListId }) => { - const oldData: OptionsListsResponse = queryClient.getQueryData([ + const oldData: OptionListsResponse = queryClient.getQueryData([ QueryKey.OptionLists, org, app, @@ -38,10 +38,10 @@ export const useUpdateOptionListIdMutation = (org: string, app: string) => { const changeIdAndSortCacheData = ( oldId: string, newId: string, - oldData: OptionsListsResponse, -): OptionsListsResponse => { + oldData: OptionListsResponse, +): OptionListsResponse => { const oldOptionList = oldData.find((optionList) => optionList.title === oldId); - const newOptionLists: OptionsListsResponse = ArrayUtils.replaceByPredicate( + const newOptionLists: OptionListsResponse = ArrayUtils.replaceByPredicate( oldData, (optionList) => optionList.title === oldId, { ...oldOptionList, title: newId }, diff --git a/frontend/packages/shared/src/hooks/mutations/useUpdateOptionListMutation.test.ts b/frontend/packages/shared/src/hooks/mutations/useUpdateOptionListMutation.test.ts index 7b1d40658d8..db457d48f77 100644 --- a/frontend/packages/shared/src/hooks/mutations/useUpdateOptionListMutation.test.ts +++ b/frontend/packages/shared/src/hooks/mutations/useUpdateOptionListMutation.test.ts @@ -10,11 +10,11 @@ import { createQueryClientMock } from '../../mocks/queryClientMock'; import { QueryKey } from 'app-shared/types/QueryKey'; // Test data: -const optionsListId = 'test'; +const optionListId = 'test'; const option1: Option = { value: 'test', label: 'test' }; -const optionsList: Option[] = [option1]; +const optionList: Option[] = [option1]; const updatedOptionsList: Option[] = [{ ...option1, description: 'description' }]; -const args: UpdateOptionListMutationArgs = { optionListId: optionsListId, optionsList }; +const args: UpdateOptionListMutationArgs = { optionListId, optionList }; const updateOptionList = jest.fn().mockImplementation(() => Promise.resolve(updatedOptionsList)); describe('useUpdateOptionListMutation', () => { @@ -24,14 +24,14 @@ describe('useUpdateOptionListMutation', () => { ).result; await renderUpdateOptionsListMutationResult.current.mutateAsync(args); expect(queriesMock.updateOptionList).toHaveBeenCalledTimes(1); - expect(queriesMock.updateOptionList).toHaveBeenCalledWith(org, app, optionsListId, optionsList); + expect(queriesMock.updateOptionList).toHaveBeenCalledWith(org, app, optionListId, optionList); }); - test('Sets the updated options list on the cache for all options lists when cache contains the list', async () => { + test('Sets the updated option list on the cache for all options lists when cache contains the list', async () => { const queryClient = createQueryClientMock(); queryClient.setQueryData( [QueryKey.OptionLists, org, app], - [{ title: optionsListId, data: optionsList }], + [{ title: optionListId, data: optionList }], ); const renderUpdateOptionsListMutationResult = renderHookWithProviders( () => useUpdateOptionListMutation(org, app), @@ -40,15 +40,15 @@ describe('useUpdateOptionListMutation', () => { await renderUpdateOptionsListMutationResult.current.mutateAsync(args); expect(queryClient.getQueryData([QueryKey.OptionLists, org, app])).toEqual([ { - title: optionsListId, + title: optionListId, data: updatedOptionsList, }, ]); }); - test('Adds the new options list on the cache for all options lists when cache does not contain the list', async () => { + test('Adds the new option list on the cache for all options lists when cache does not contain the list', async () => { const queryClient = createQueryClientMock(); - const existingOptionsList = { title: 'some-other-options-list-id', data: optionsList }; + const existingOptionsList = { title: 'some-other-options-list-id', data: optionList }; queryClient.setQueryData([QueryKey.OptionLists, org, app], [existingOptionsList]); const renderUpdateOptionsListMutationResult = renderHookWithProviders( () => useUpdateOptionListMutation(org, app), @@ -57,7 +57,7 @@ describe('useUpdateOptionListMutation', () => { await renderUpdateOptionsListMutationResult.current.mutateAsync(args); expect(queryClient.getQueryData([QueryKey.OptionLists, org, app])).toEqual([ { - title: optionsListId, + title: optionListId, data: updatedOptionsList, }, existingOptionsList, @@ -71,7 +71,7 @@ describe('useUpdateOptionListMutation', () => { { queries: { updateOptionList }, queryClient }, ).result; await renderUpdateOptionsListMutationResult.current.mutateAsync(args); - expect(queryClient.getQueryData([QueryKey.OptionList, org, app, optionsListId])).toEqual( + expect(queryClient.getQueryData([QueryKey.OptionList, org, app, optionListId])).toEqual( updatedOptionsList, ); }); diff --git a/frontend/packages/shared/src/hooks/mutations/useUpdateOptionListMutation.ts b/frontend/packages/shared/src/hooks/mutations/useUpdateOptionListMutation.ts index fd4e15c4e23..18d43277706 100644 --- a/frontend/packages/shared/src/hooks/mutations/useUpdateOptionListMutation.ts +++ b/frontend/packages/shared/src/hooks/mutations/useUpdateOptionListMutation.ts @@ -1,36 +1,32 @@ import type { MutationMeta } from '@tanstack/react-query'; import { QueryKey } from 'app-shared/types/QueryKey'; -import type { Option } from 'app-shared/types/Option'; -import type { - OptionsList, - OptionsListData, - OptionsListsResponse, -} from 'app-shared/types/api/OptionsLists'; +import type { OptionList, OptionListData } from 'app-shared/types/OptionList'; +import type { OptionListsResponse } from 'app-shared/types/api/OptionListsResponse'; import { useQueryClient, useMutation } from '@tanstack/react-query'; import { useServicesContext } from 'app-shared/contexts/ServicesContext'; import { ArrayUtils } from '@studio/pure-functions'; export interface UpdateOptionListMutationArgs { optionListId: string; - optionsList: Option[]; + optionList: OptionList; } export const useUpdateOptionListMutation = (org: string, app: string, meta?: MutationMeta) => { const queryClient = useQueryClient(); const { updateOptionList } = useServicesContext(); - return useMutation({ - mutationFn: ({ optionListId, optionsList }: UpdateOptionListMutationArgs) => { - return updateOptionList(org, app, optionListId, optionsList); + return useMutation({ + mutationFn: ({ optionListId, optionList }: UpdateOptionListMutationArgs) => { + return updateOptionList(org, app, optionListId, optionList); }, - onSuccess: (updatedOptionsList: Option[], { optionListId }) => { - const oldData: OptionsListsResponse = queryClient.getQueryData([ + onSuccess: (updatedOptionsList: OptionList, { optionListId }) => { + const oldData: OptionListsResponse = queryClient.getQueryData([ QueryKey.OptionLists, org, app, ]); if (isOptionsListInOptionListsCache(oldData)) { - const newData = updateListInOptionsListsData(optionListId, updatedOptionsList, oldData); + const newData = updateListInOptionListDataList(optionListId, updatedOptionsList, oldData); queryClient.setQueryData([QueryKey.OptionLists, org, app], newData); } queryClient.setQueryData([QueryKey.OptionList, org, app, optionListId], updatedOptionsList); @@ -40,50 +36,50 @@ export const useUpdateOptionListMutation = (org: string, app: string, meta?: Mut }); }; -const isOptionsListInOptionListsCache = (data: OptionsListsResponse | null): boolean => !!data; +const isOptionsListInOptionListsCache = (data: OptionListsResponse | null): boolean => !!data; -const updateListInOptionsListsData = ( - optionsListId: string, - updatedOptionsList: OptionsList, - oldData: OptionsListsResponse, -): OptionsListsResponse => { - const [oldOptionsListData, optionsListExists]: [OptionsListData | undefined, boolean] = - getOldOptionsListData(oldData, optionsListId); +const updateListInOptionListDataList = ( + optionListId: string, + updatedOptionList: OptionList, + oldData: OptionListsResponse, +): OptionListsResponse => { + const [oldOptionsListData, optionsListExists]: [OptionListData | undefined, boolean] = + getOldOptionListData(oldData, optionListId); if (optionsListExists) { - return updateExistingOptionsList(oldData, oldOptionsListData, updatedOptionsList); + return updateExistingOptionList(oldData, oldOptionsListData, updatedOptionList); } - return addNewOptionsList(oldData, optionsListId, updatedOptionsList); + return addNewOptionList(oldData, optionListId, updatedOptionList); }; -const getOldOptionsListData = ( - oldData: OptionsListsResponse, - optionsListId: string, -): [OptionsListData | undefined, boolean] => { +const getOldOptionListData = ( + oldData: OptionListsResponse, + optionListId: string, +): [OptionListData | undefined, boolean] => { const oldOptionsListData = oldData.find( - (optionsListData) => optionsListData.title === optionsListId, + (optionListData) => optionListData.title === optionListId, ); return [oldOptionsListData, !!oldOptionsListData]; }; -const updateExistingOptionsList = ( - oldData: OptionsListsResponse, - oldOptionsListData: OptionsListData, - newOptionsList: OptionsList, +const updateExistingOptionList = ( + oldData: OptionListsResponse, + oldOptionListData: OptionListData, + newOptionList: OptionList, ) => { return ArrayUtils.replaceByPredicate( oldData, - (optionsList) => optionsList.title === oldOptionsListData.title, + (optionsList) => optionsList.title === oldOptionListData.title, { - ...oldOptionsListData, - data: newOptionsList, + ...oldOptionListData, + data: newOptionList, }, ); }; -const addNewOptionsList = ( - oldData: OptionsListsResponse, - optionsListTitle: string, - newOptionsList: OptionsList, +const addNewOptionList = ( + oldData: OptionListsResponse, + optionListTitle: string, + newOptionList: OptionList, ) => { - return ArrayUtils.prepend(oldData, { title: optionsListTitle, data: newOptionsList }); + return ArrayUtils.prepend(oldData, { title: optionListTitle, data: newOptionList }); }; diff --git a/frontend/packages/shared/src/hooks/queries/useOptionListQuery.test.ts b/frontend/packages/shared/src/hooks/queries/useOptionListQuery.test.ts index 2876ebefa9f..2bb5300ec69 100644 --- a/frontend/packages/shared/src/hooks/queries/useOptionListQuery.test.ts +++ b/frontend/packages/shared/src/hooks/queries/useOptionListQuery.test.ts @@ -4,7 +4,7 @@ import { renderHookWithProviders } from 'app-shared/mocks/renderHookWithProvider import { useOptionListQuery } from 'app-shared/hooks/queries/useOptionListQuery'; import { waitFor } from '@testing-library/react'; import type { ServicesContextProps } from 'app-shared/contexts/ServicesContext'; -import type { OptionsList } from 'app-shared/types/api/OptionsLists'; +import type { OptionList } from 'app-shared/types/OptionList'; import { createQueryClientMock } from 'app-shared/mocks/queryClientMock'; const optionsListId = 'optionsListId'; @@ -16,7 +16,7 @@ describe('useOptionListQuery', () => { }); it('getOptionList returns optionList as is', async () => { - const optionsList: OptionsList = [{ value: 'value', label: 'label' }]; + const optionsList: OptionList = [{ value: 'value', label: 'label' }]; const getOptionList = jest.fn().mockImplementation(() => Promise.resolve(optionsList)); const { current: currentResult } = await render({ getOptionList }); expect(currentResult.data).toBe(optionsList); diff --git a/frontend/packages/shared/src/hooks/queries/useOptionListQuery.ts b/frontend/packages/shared/src/hooks/queries/useOptionListQuery.ts index 91ec12df941..fa0f44b5159 100644 --- a/frontend/packages/shared/src/hooks/queries/useOptionListQuery.ts +++ b/frontend/packages/shared/src/hooks/queries/useOptionListQuery.ts @@ -2,15 +2,15 @@ import { useServicesContext } from 'app-shared/contexts/ServicesContext'; import { QueryKey } from 'app-shared/types/QueryKey'; import type { UseQueryResult } from '@tanstack/react-query'; import { useQuery } from '@tanstack/react-query'; -import type { OptionsList } from 'app-shared/types/api/OptionsLists'; +import type { OptionList } from 'app-shared/types/OptionList'; export const useOptionListQuery = ( org: string, app: string, optionListId: string, -): UseQueryResult => { +): UseQueryResult => { const { getOptionList } = useServicesContext(); - return useQuery({ + return useQuery({ queryKey: [QueryKey.OptionList, org, app, optionListId], queryFn: () => getOptionList(org, app, optionListId), }); diff --git a/frontend/packages/shared/src/hooks/queries/useOptionListsQuery.ts b/frontend/packages/shared/src/hooks/queries/useOptionListsQuery.ts index 9123f432848..7ea17f6f4a7 100644 --- a/frontend/packages/shared/src/hooks/queries/useOptionListsQuery.ts +++ b/frontend/packages/shared/src/hooks/queries/useOptionListsQuery.ts @@ -2,14 +2,14 @@ import { useServicesContext } from 'app-shared/contexts/ServicesContext'; import { QueryKey } from 'app-shared/types/QueryKey'; import type { UseQueryResult } from '@tanstack/react-query'; import { useQuery } from '@tanstack/react-query'; -import type { OptionsListsResponse } from 'app-shared/types/api/OptionsLists'; +import type { OptionListsResponse } from 'app-shared/types/api/OptionListsResponse'; export const useOptionListsQuery = ( org: string, app: string, -): UseQueryResult => { +): UseQueryResult => { const { getOptionLists } = useServicesContext(); - return useQuery({ + return useQuery({ queryKey: [QueryKey.OptionLists, org, app], queryFn: () => getOptionLists(org, app), }); diff --git a/frontend/packages/shared/src/hooks/queries/useOptionListsReferencesQuery.ts b/frontend/packages/shared/src/hooks/queries/useOptionListsReferencesQuery.ts index c8c7415580f..e1449161bbe 100644 --- a/frontend/packages/shared/src/hooks/queries/useOptionListsReferencesQuery.ts +++ b/frontend/packages/shared/src/hooks/queries/useOptionListsReferencesQuery.ts @@ -2,14 +2,14 @@ import { useServicesContext } from 'app-shared/contexts/ServicesContext'; import { QueryKey } from 'app-shared/types/QueryKey'; import type { UseQueryResult } from '@tanstack/react-query'; import { useQuery } from '@tanstack/react-query'; -import type { OptionListsReferences } from 'app-shared/types/api/OptionsLists'; +import type { OptionListReferences } from 'app-shared/types/OptionListReferences'; export const useOptionListsReferencesQuery = ( org: string, app: string, -): UseQueryResult => { +): UseQueryResult => { const { getOptionListsReferences } = useServicesContext(); - return useQuery({ + return useQuery({ queryKey: [QueryKey.OptionListsUsage, org, app], queryFn: () => getOptionListsReferences(org, app), }); diff --git a/frontend/packages/shared/src/mocks/queriesMock.ts b/frontend/packages/shared/src/mocks/queriesMock.ts index c83b2427779..7ce812a6228 100644 --- a/frontend/packages/shared/src/mocks/queriesMock.ts +++ b/frontend/packages/shared/src/mocks/queriesMock.ts @@ -69,13 +69,11 @@ import type { DeploymentsResponse } from 'app-shared/types/api/DeploymentsRespon import type { RepoDiffResponse } from 'app-shared/types/api/RepoDiffResponse'; import type { ExternalImageUrlValidationResponse } from 'app-shared/types/api/ExternalImageUrlValidationResponse'; import type { MaskinportenScope } from 'app-shared/types/MaskinportenScope'; -import type { - OptionListsReferences, - OptionsList, - OptionsListsResponse, -} from 'app-shared/types/api/OptionsLists'; +import type { OptionList } from 'app-shared/types/OptionList'; +import type { OptionListReferences } from 'app-shared/types/OptionListReferences'; import type { LayoutSetsModel } from '../types/api/dto/LayoutSetsModel'; import { layoutSetsExtendedMock } from '@altinn/ux-editor/testing/layoutSetsMock'; +import type { OptionListsResponse } from 'app-shared/types/api/OptionListsResponse'; export const queriesMock: ServicesContextProps = { // Queries @@ -113,11 +111,11 @@ export const queriesMock: ServicesContextProps = { .fn() .mockImplementation(() => Promise.resolve(layoutSetsExtendedMock)), getOptionListIds: jest.fn().mockImplementation(() => Promise.resolve([])), - getOptionList: jest.fn().mockImplementation(() => Promise.resolve([])), - getOptionLists: jest.fn().mockImplementation(() => Promise.resolve([])), + getOptionList: jest.fn().mockImplementation(() => Promise.resolve([])), + getOptionLists: jest.fn().mockImplementation(() => Promise.resolve([])), getOptionListsReferences: jest .fn() - .mockImplementation(() => Promise.resolve([])), + .mockImplementation(() => Promise.resolve([])), getOrgList: jest.fn().mockImplementation(() => Promise.resolve(orgList)), getOrganizations: jest.fn().mockImplementation(() => Promise.resolve([])), getRepoMetadata: jest.fn().mockImplementation(() => Promise.resolve(repository)), diff --git a/frontend/packages/shared/src/types/OptionList.ts b/frontend/packages/shared/src/types/OptionList.ts new file mode 100644 index 00000000000..b35965201c8 --- /dev/null +++ b/frontend/packages/shared/src/types/OptionList.ts @@ -0,0 +1,9 @@ +import type { Option } from 'app-shared/types/Option'; + +export type OptionList = Option[]; + +export type OptionListData = { + title: string; + data?: OptionList; + hasError?: boolean; +}; diff --git a/frontend/packages/shared/src/types/OptionListReferences.ts b/frontend/packages/shared/src/types/OptionListReferences.ts new file mode 100644 index 00000000000..2a1bbba083d --- /dev/null +++ b/frontend/packages/shared/src/types/OptionListReferences.ts @@ -0,0 +1,8 @@ +import type { CodeListIdSource } from '@studio/content-library'; + +export type OptionListReferences = OptionListReference[]; + +export type OptionListReference = { + optionListId: string; + optionListIdSources: CodeListIdSource[]; +}; diff --git a/frontend/packages/shared/src/types/api/OptionListsResponse.ts b/frontend/packages/shared/src/types/api/OptionListsResponse.ts new file mode 100644 index 00000000000..30e0275146d --- /dev/null +++ b/frontend/packages/shared/src/types/api/OptionListsResponse.ts @@ -0,0 +1,3 @@ +import type { OptionListData } from '../OptionList'; + +export type OptionListsResponse = OptionListData[]; diff --git a/frontend/packages/shared/src/types/api/OptionsLists.ts b/frontend/packages/shared/src/types/api/OptionsLists.ts deleted file mode 100644 index 27c46294c52..00000000000 --- a/frontend/packages/shared/src/types/api/OptionsLists.ts +++ /dev/null @@ -1,19 +0,0 @@ -import type { Option } from 'app-shared/types/Option'; -import type { CodeListIdSource } from '@studio/content-library'; - -export type OptionsList = Option[]; - -export type OptionsListData = { - title: string; - data?: OptionsList; - hasError?: boolean; -}; - -export type OptionsListsResponse = OptionsListData[]; - -export type OptionListsReference = { - optionListId: string; - optionListIdSources: CodeListIdSource[]; -}; - -export type OptionListsReferences = OptionListsReference[]; diff --git a/frontend/packages/ux-editor/src/components/config/editModal/EditOptions/OptionTabs/EditTab/OptionListEditor/LibraryOptionsEditor/LibraryOptionsEditor.tsx b/frontend/packages/ux-editor/src/components/config/editModal/EditOptions/OptionTabs/EditTab/OptionListEditor/LibraryOptionsEditor/LibraryOptionsEditor.tsx index 416435c6078..975b4e8aff5 100644 --- a/frontend/packages/ux-editor/src/components/config/editModal/EditOptions/OptionTabs/EditTab/OptionListEditor/LibraryOptionsEditor/LibraryOptionsEditor.tsx +++ b/frontend/packages/ux-editor/src/components/config/editModal/EditOptions/OptionTabs/EditTab/OptionListEditor/LibraryOptionsEditor/LibraryOptionsEditor.tsx @@ -1,4 +1,3 @@ -import type { Option } from 'app-shared/types/Option'; import React, { createRef } from 'react'; import { useTranslation } from 'react-i18next'; import { StudioCodeListEditor, StudioModal, StudioAlert } from '@studio/components'; @@ -12,27 +11,28 @@ import { OptionListLabels } from '../OptionListLabels'; import { hasOptionListChanged } from '../../../utils/optionsUtils'; import { useOptionListQuery } from 'app-shared/hooks/queries'; import classes from './LibraryOptionsEditor.module.css'; +import type { OptionList } from 'app-shared/types/OptionList'; type LibraryOptionsEditorProps = { handleDelete: () => void; - optionsId: string; + optionListId: string; }; export function LibraryOptionsEditor({ handleDelete, - optionsId, + optionListId, }: LibraryOptionsEditorProps): React.ReactNode { const { t } = useTranslation(); const { org, app } = useStudioEnvironmentParams(); - const { data: optionsList } = useOptionListQuery(org, app, optionsId); + const { data: optionList } = useOptionListQuery(org, app, optionListId); const { doReloadPreview } = usePreviewContext(); const { mutate: updateOptionList } = useUpdateOptionListMutation(org, app); const editorTexts: CodeListEditorTexts = useOptionListEditorTexts(); const modalRef = createRef(); - const handleOptionsListChange = (options: Option[]) => { - if (hasOptionListChanged(optionsList, options)) { - updateOptionList({ optionListId: optionsId, optionsList: options }); + const handleOptionsListChange = (newOptionList: OptionList) => { + if (hasOptionListChanged(optionList, newOptionList)) { + updateOptionList({ optionListId, optionList: newOptionList }); doReloadPreview(); } }; @@ -43,7 +43,7 @@ export function LibraryOptionsEditor({ return ( <> - + - + Promise.resolve(apiResult)); + .mockImplementation(() => Promise.resolve(apiResult)); const componentWithOptionsId = { ...mockComponent, options: undefined, optionsId: 'some-id' }; describe('OptionListEditor', () => { @@ -123,7 +123,7 @@ describe('OptionListEditor', () => { it('should render a spinner when there is no data', () => { renderOptionListEditor({ queries: { - getOptionList: jest.fn().mockImplementation(() => Promise.resolve([])), + getOptionList: jest.fn().mockImplementation(() => Promise.resolve([])), }, props: { component: componentWithOptionsId }, }); @@ -208,14 +208,14 @@ describe('OptionListEditor', () => { }); it('should show placeholder for option label when option list label is empty', async () => { - const apiResultWithEmptyLabel: OptionsList = [ + const apiResultWithEmptyLabel: OptionList = [ { value: true, label: '', description: null, helpText: null }, ]; await renderOptionListEditorAndWaitForSpinnerToBeRemoved({ queries: { getOptionList: jest .fn() - .mockImplementation(() => Promise.resolve(apiResultWithEmptyLabel)), + .mockImplementation(() => Promise.resolve(apiResultWithEmptyLabel)), }, }); diff --git a/frontend/packages/ux-editor/src/components/config/editModal/EditOptions/OptionTabs/EditTab/OptionListEditor/OptionListEditor.tsx b/frontend/packages/ux-editor/src/components/config/editModal/EditOptions/OptionTabs/EditTab/OptionListEditor/OptionListEditor.tsx index 1ac15d0f3a3..caa7d3bd693 100644 --- a/frontend/packages/ux-editor/src/components/config/editModal/EditOptions/OptionTabs/EditTab/OptionListEditor/OptionListEditor.tsx +++ b/frontend/packages/ux-editor/src/components/config/editModal/EditOptions/OptionTabs/EditTab/OptionListEditor/OptionListEditor.tsx @@ -61,7 +61,7 @@ function OptionListResolver({ ); case 'success': { - return ; + return ; } } } diff --git a/frontend/packages/ux-editor/src/components/config/editModal/EditOptions/OptionTabs/EditTab/OptionListEditor/OptionListLabels/OptionListLabels.tsx b/frontend/packages/ux-editor/src/components/config/editModal/EditOptions/OptionTabs/EditTab/OptionListEditor/OptionListLabels/OptionListLabels.tsx index dfe940d1210..79e378a4b1d 100644 --- a/frontend/packages/ux-editor/src/components/config/editModal/EditOptions/OptionTabs/EditTab/OptionListEditor/OptionListLabels/OptionListLabels.tsx +++ b/frontend/packages/ux-editor/src/components/config/editModal/EditOptions/OptionTabs/EditTab/OptionListEditor/OptionListLabels/OptionListLabels.tsx @@ -2,22 +2,22 @@ import React from 'react'; import { useTranslation } from 'react-i18next'; import { StudioParagraph } from '@studio/components'; import { useConcatOptionsLabels } from '../hooks/useConcatOptionsLabels'; -import type { OptionsList } from 'app-shared/types/api/OptionsLists'; +import type { OptionList } from 'app-shared/types/OptionList'; import classes from './OptionListLabels.module.css'; -type OptionListLabelsProps = { optionsList: OptionsList; optionsId: string }; +type OptionListLabelsProps = { optionList: OptionList; optionListId: string }; export function OptionListLabels({ - optionsList, - optionsId, + optionList, + optionListId, }: OptionListLabelsProps): React.ReactNode { const { t } = useTranslation(); - const codeListLabels: string = useConcatOptionsLabels(optionsList); + const codeListLabels: string = useConcatOptionsLabels(optionList); return ( <> - {optionsId ?? t('ux_editor.modal_properties_code_list_custom_list')} + {optionListId ?? t('ux_editor.modal_properties_code_list_custom_list')} {codeListLabels} diff --git a/frontend/packages/ux-editor/src/components/config/editModal/EditOptions/OptionTabs/utils/optionsUtils.test.ts b/frontend/packages/ux-editor/src/components/config/editModal/EditOptions/OptionTabs/utils/optionsUtils.test.ts index f526fc0e625..e9485baccb4 100644 --- a/frontend/packages/ux-editor/src/components/config/editModal/EditOptions/OptionTabs/utils/optionsUtils.test.ts +++ b/frontend/packages/ux-editor/src/components/config/editModal/EditOptions/OptionTabs/utils/optionsUtils.test.ts @@ -1,5 +1,5 @@ import { SelectedOptionsType } from '../../../../../../components/config/editModal/EditOptions/EditOptions'; -import type { OptionsList } from 'app-shared/types/api/OptionsLists'; +import type { OptionList } from 'app-shared/types/OptionList'; import { ComponentType } from 'app-shared/types/ComponentType'; import type { FormItem } from '../../../../../../types/FormItem'; import type { SelectionComponentType } from '../../../../../../types/FormComponent'; @@ -26,7 +26,7 @@ describe('optionsUtils', () => { describe('getSelectedOptionsType', () => { it('should return SelectedOptionsType.Unknown if both options and optionsId are set', () => { const codeListId = 'codeListId'; - const options: OptionsList = [{ label: 'label1', value: 'value1' }]; + const options: OptionList = [{ label: 'label1', value: 'value1' }]; const optionListIds = ['codeListId']; const result = getSelectedOptionsType(codeListId, options, optionListIds); expect(result).toEqual(SelectedOptionsType.Unknown); @@ -67,7 +67,7 @@ describe('optionsUtils', () => { describe('getSelectedOptionsTypeWithManualSupport', () => { it('should return SelectedOptionsType.Unknown if both options and optionsId are set', () => { const codeListId = 'codeListId'; - const options: OptionsList = [{ label: 'label1', value: 'value1' }]; + const options: OptionList = [{ label: 'label1', value: 'value1' }]; const optionListIds = ['codeListId']; const result = getSelectedOptionsTypeWithManualSupport(codeListId, options, optionListIds); expect(result).toEqual(SelectedOptionsType.Unknown); @@ -135,14 +135,14 @@ describe('optionsUtils', () => { describe('hasOptionListChanged', () => { it('should return false if the optionList has not changed', () => { - const oldOptions: OptionsList = [{ label: 'label1', value: 'value1' }]; - const newOptions: OptionsList = [{ label: 'label1', value: 'value1' }]; + const oldOptions: OptionList = [{ label: 'label1', value: 'value1' }]; + const newOptions: OptionList = [{ label: 'label1', value: 'value1' }]; expect(hasOptionListChanged(oldOptions, newOptions)).toEqual(false); }); it('should return true if the optionList has changed', () => { - const oldOptions: OptionsList = [{ label: 'label1', value: 'value1' }]; - const newOptions: OptionsList = [{ label: 'new label', value: 'new value' }]; + const oldOptions: OptionList = [{ label: 'label1', value: 'value1' }]; + const newOptions: OptionList = [{ label: 'new label', value: 'new value' }]; expect(hasOptionListChanged(oldOptions, newOptions)).toEqual(true); }); }); @@ -179,7 +179,7 @@ describe('optionsUtils', () => { describe('updateComponentOptions', () => { it('should update options on the returned object', () => { - const options: OptionsList = [{ label: 'new-label', value: 'new-value' }]; + const options: OptionList = [{ label: 'new-label', value: 'new-value' }]; expect(updateComponentOptions(mockedComponent, options)).toStrictEqual({ ...mockedComponent, optionsId: undefined, @@ -212,28 +212,28 @@ describe('optionsUtils', () => { it('should return true if options ID is a string and options ID is from library', () => { const optionListIds: string[] = ['test1', 'test2']; const optionsId: string = 'test1'; - const options: OptionsList = [{ value: 'value', label: 'label' }]; + const options: OptionList = [{ value: 'value', label: 'label' }]; expect(isOptionsModifiable(optionListIds, optionsId, options)).toEqual(true); }); it('should return true if options is set on the component', () => { const optionListIds: string[] = []; const optionsId = ''; - const options: OptionsList = []; + const options: OptionList = []; expect(isOptionsModifiable(optionListIds, optionsId, options)).toEqual(true); }); it('should return false if options ID and options are undefined', () => { const optionListIds: string[] = ['test1', 'test2']; const optionsId = undefined; - const options: OptionsList = undefined; + const options: OptionList = undefined; expect(isOptionsModifiable(optionListIds, optionsId, options)).toEqual(false); }); it('should return false if options ID is not from library', () => { const optionListIds: string[] = ['test1', 'test2']; const optionsId = 'another-id'; - const options: OptionsList = undefined; + const options: OptionList = undefined; expect(isOptionsModifiable(optionListIds, optionsId, options)).toEqual(false); }); }); @@ -241,13 +241,13 @@ describe('optionsUtils', () => { describe('isInitialOptionsSet', () => { it('should return true if previousOptions is false and currentOptions is truthy', () => { const previousOptions = undefined; - const currentOptions: OptionsList = []; + const currentOptions: OptionList = []; expect(isInitialOptionsSet(previousOptions, currentOptions)).toEqual(true); }); it('should return false if previousOptions is truthy', () => { const previousOptions = []; - const currentOptions: OptionsList = [{ value: 'value', label: 'label' }]; + const currentOptions: OptionList = [{ value: 'value', label: 'label' }]; expect(isInitialOptionsSet(previousOptions, currentOptions)).toEqual(false); }); diff --git a/frontend/packages/ux-editor/src/components/config/editModal/EditOptions/OptionTabs/utils/optionsUtils.ts b/frontend/packages/ux-editor/src/components/config/editModal/EditOptions/OptionTabs/utils/optionsUtils.ts index e196aa08233..594edf59d39 100644 --- a/frontend/packages/ux-editor/src/components/config/editModal/EditOptions/OptionTabs/utils/optionsUtils.ts +++ b/frontend/packages/ux-editor/src/components/config/editModal/EditOptions/OptionTabs/utils/optionsUtils.ts @@ -1,5 +1,5 @@ import { SelectedOptionsType } from '../../../../../../components/config/editModal/EditOptions/EditOptions'; -import type { OptionsList } from 'app-shared/types/api/OptionsLists'; +import type { OptionList } from 'app-shared/types/OptionList'; import type { FormItem } from '../../../../../../types/FormItem'; import type { FormComponent, SelectionComponentType } from '../../../../../../types/FormComponent'; import type { FormContainer } from '../../../../../../types/FormContainer'; @@ -18,7 +18,7 @@ export const componentUsesDynamicCodeList = ( export function getSelectedOptionsType( codeListId: string | undefined, - options: OptionsList | undefined, + options: OptionList | undefined, optionListIds: string[] = [], ): SelectedOptionsType { /** It is not permitted for a component to have both options and optionsId set on the same component. */ @@ -34,7 +34,7 @@ export function getSelectedOptionsType( // Todo: Remove once featureFlag "optionListEditor" is removed. export function getSelectedOptionsTypeWithManualSupport( codeListId: string | undefined, - options: OptionsList | undefined, + options: OptionList | undefined, optionListIds: string[] = [], ): SelectedOptionsType { /** It is not permitted for a component to have both options and optionsId set on the same component. */ @@ -51,7 +51,7 @@ export function getSelectedOptionsTypeWithManualSupport( : SelectedOptionsType.CodeList; } -export function hasOptionListChanged(oldOptions: OptionsList, newOptions: OptionsList): boolean { +export function hasOptionListChanged(oldOptions: OptionList, newOptions: OptionList): boolean { return JSON.stringify(oldOptions) !== JSON.stringify(newOptions); } @@ -87,7 +87,7 @@ export function updateComponentOptionsId( export function updateComponentOptions( component: FormItem, - options: OptionsList, + options: OptionList, ): FormItem { let newComponent: FormItem = ObjectUtils.deepCopy(component); @@ -120,7 +120,7 @@ export function isOptionsIdReferenceId( export function isOptionsModifiable( optionListIds: string[], optionsId: undefined | string, - options: undefined | OptionsList, + options: undefined | OptionList, ): boolean { return (!!optionsId && isOptionsIdFromLibrary(optionListIds, optionsId)) || !!options; } @@ -130,8 +130,8 @@ function isOptionsIdFromLibrary(optionListIds: string[], optionsId: undefined | } export function isInitialOptionsSet( - previousOptions: OptionsList, - currentOptions: OptionsList, + previousOptions: OptionList, + currentOptions: OptionList, ): boolean { return !previousOptions && !!currentOptions; } diff --git a/frontend/packages/ux-editor/src/utils/exportUtils.test.ts b/frontend/packages/ux-editor/src/utils/exportUtils.test.ts index 521582460ad..1232e32f74b 100644 --- a/frontend/packages/ux-editor/src/utils/exportUtils.test.ts +++ b/frontend/packages/ux-editor/src/utils/exportUtils.test.ts @@ -5,7 +5,7 @@ import type { ITextResources } from 'app-shared/types/global'; import type { ExportForm } from '../types/ExportForm'; import type { FormComponent } from '../types/FormComponent'; import type { FormContainer } from '../types/FormContainer'; -import type { OptionsListsResponse } from 'app-shared/types/api/OptionsLists'; +import type { OptionListData } from 'app-shared/types/OptionList'; describe('generateExportFormFormat', () => { const settings = { @@ -102,7 +102,7 @@ describe('generateExportFormFormat', () => { }, ], }; - const optionListsData: OptionsListsResponse = [ + const optionListsData: OptionListData[] = [ { title: 'optionList1', data: [{ label: 'option1', value: 'option1' }] }, ]; diff --git a/frontend/packages/ux-editor/src/utils/exportUtils.ts b/frontend/packages/ux-editor/src/utils/exportUtils.ts index d800f4d15f5..05359e5cd1a 100644 --- a/frontend/packages/ux-editor/src/utils/exportUtils.ts +++ b/frontend/packages/ux-editor/src/utils/exportUtils.ts @@ -10,7 +10,7 @@ import type { import type { IFormLayouts, IOption, ITextResourceBindings } from '../types/global'; import { internalLayoutToExternal } from '../converters/formLayoutConverters'; import type { ExternalComponent, ExternalFormLayout } from 'app-shared/types/api'; -import type { OptionsListsResponse } from 'app-shared/types/api/OptionsLists'; +import type { OptionListData } from 'app-shared/types/OptionList'; export class ExportUtils { private readonly pageOrder: string[]; @@ -18,7 +18,7 @@ export class ExportUtils { private readonly layoutSetName: string; private readonly appId: string; private readonly textResources: ITextResources; - private readonly optionListsData: OptionsListsResponse; + private readonly optionListDataList: OptionListData[]; private readonly defaultLanguage: string; private readonly includeRestProperties: boolean; @@ -28,7 +28,7 @@ export class ExportUtils { layoutSetName: string, appId: string, textResources: ITextResources, - optionListsData: OptionsListsResponse, + optionListsData: OptionListData[], defaultLanguage: string, includeRestProperties: boolean = false, ) { @@ -37,7 +37,7 @@ export class ExportUtils { this.layoutSetName = layoutSetName; this.appId = appId; this.textResources = textResources; - this.optionListsData = optionListsData; + this.optionListDataList = optionListsData; this.defaultLanguage = defaultLanguage; this.includeRestProperties = includeRestProperties; } @@ -141,7 +141,7 @@ export class ExportUtils { return component.options; } if (component.optionsId) { - const optionListData = this.optionListsData.find( + const optionListData = this.optionListDataList.find( (optionListData) => optionListData.title === component.optionsId, ); return optionListData.data;