-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: fetch only single needed option list in option list editor (#…
…14266) Co-authored-by: Erling Hauan <[email protected]>
- Loading branch information
1 parent
6714508
commit a578aab
Showing
22 changed files
with
324 additions
and
246 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
frontend/packages/shared/src/hooks/queries/useOptionListQuery.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { queriesMock } from 'app-shared/mocks/queriesMock'; | ||
import { app, org } from '@studio/testing/testids'; | ||
import { renderHookWithProviders } from 'app-shared/mocks/renderHookWithProviders'; | ||
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 { createQueryClientMock } from 'app-shared/mocks/queryClientMock'; | ||
|
||
const optionsListId = 'optionsListId'; | ||
|
||
describe('useOptionListQuery', () => { | ||
it('calls getOptionList with the correct parameters', () => { | ||
render(); | ||
expect(queriesMock.getOptionList).toHaveBeenCalledWith(org, app, optionsListId); | ||
}); | ||
|
||
it('getOptionList returns optionList as is', async () => { | ||
const optionsList: OptionsList = [{ value: 'value', label: 'label' }]; | ||
const getOptionList = jest.fn().mockImplementation(() => Promise.resolve(optionsList)); | ||
const { current: currentResult } = await render({ getOptionList }); | ||
expect(currentResult.data).toBe(optionsList); | ||
}); | ||
}); | ||
|
||
const render = async (queries: Partial<ServicesContextProps> = {}) => { | ||
const queryClient = createQueryClientMock(); | ||
const { result } = renderHookWithProviders(() => useOptionListQuery(org, app, optionsListId), { | ||
queries, | ||
queryClient, | ||
}); | ||
await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
return result; | ||
}; |
17 changes: 17 additions & 0 deletions
17
frontend/packages/shared/src/hooks/queries/useOptionListQuery.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
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'; | ||
|
||
export const useOptionListQuery = ( | ||
org: string, | ||
app: string, | ||
optionListId: string, | ||
): UseQueryResult<OptionsList> => { | ||
const { getOptionList } = useServicesContext(); | ||
return useQuery<OptionsList>({ | ||
queryKey: [QueryKey.OptionList, org, app, optionListId], | ||
queryFn: () => getOptionList(org, app, optionListId), | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import type { Option } from 'app-shared/types/Option'; | ||
|
||
export type OptionsLists = Record<string, Option[]>; | ||
export type OptionsList = Option[]; | ||
|
||
export type OptionsLists = Record<string, OptionsList>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
74 changes: 74 additions & 0 deletions
74
...Options/OptionTabs/EditTab/OptionListEditor/LibraryOptionsEditor/LibraryOptionsEditor.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import React, { createRef, useState } from 'react'; | ||
import type { Option } from 'app-shared/types/Option'; | ||
import { useStudioEnvironmentParams } from 'app-shared/hooks/useStudioEnvironmentParams'; | ||
import { useUpdateOptionListMutation } from 'app-shared/hooks/mutations'; | ||
import { useOptionListEditorTexts } from '../../../../../EditOptions/OptionTabs/hooks'; | ||
import classes from './LibraryOptionsEditor.module.css'; | ||
import { useTranslation } from 'react-i18next'; | ||
import type { CodeListEditorTexts } from '@studio/components'; | ||
import { StudioAlert, StudioCodeListEditor, StudioModal, StudioProperty } from '@studio/components'; | ||
import { usePreviewContext } from 'app-development/contexts/PreviewContext'; | ||
|
||
type LibraryOptionsEditorProps = { | ||
optionsId: string; | ||
optionsList: Option[]; | ||
}; | ||
|
||
export function LibraryOptionsEditor({ | ||
optionsId, | ||
optionsList, | ||
}: LibraryOptionsEditorProps): React.ReactNode { | ||
const { t } = useTranslation(); | ||
const { org, app } = useStudioEnvironmentParams(); | ||
const { doReloadPreview } = usePreviewContext(); | ||
const { mutate: updateOptionList } = useUpdateOptionListMutation(org, app); | ||
const [localOptionList, setLocalOptionList] = useState<Option[]>(optionsList); | ||
const editorTexts: CodeListEditorTexts = useOptionListEditorTexts(); | ||
const modalRef = createRef<HTMLDialogElement>(); | ||
|
||
const optionListHasChanged = (options: Option[]): boolean => | ||
JSON.stringify(options) !== JSON.stringify(localOptionList); | ||
|
||
const handleBlurAny = (options: Option[]) => { | ||
if (optionListHasChanged(options)) { | ||
updateOptionList({ optionListId: optionsId, optionsList: options }); | ||
setLocalOptionList(options); | ||
doReloadPreview(); | ||
} | ||
}; | ||
|
||
const handleClose = () => { | ||
modalRef.current?.close(); | ||
}; | ||
|
||
return ( | ||
<> | ||
<StudioProperty.Button | ||
value={optionsId} | ||
title={t('ux_editor.options.option_edit_text')} | ||
property={t('ux_editor.modal_properties_code_list_button_title_library')} | ||
onClick={() => modalRef.current.showModal()} | ||
/> | ||
<StudioModal.Dialog | ||
ref={modalRef} | ||
className={classes.editOptionTabModal} | ||
contentClassName={classes.content} | ||
closeButtonTitle={t('general.close')} | ||
heading={t('ux_editor.modal_add_options_code_list')} | ||
onInteractOutside={handleClose} | ||
onBeforeClose={handleClose} | ||
footer={ | ||
<StudioAlert severity={'warning'} size='sm'> | ||
{t('ux_editor.modal_properties_code_list_alert_title')} | ||
</StudioAlert> | ||
} | ||
> | ||
<StudioCodeListEditor | ||
codeList={localOptionList} | ||
onBlurAny={handleBlurAny} | ||
texts={editorTexts} | ||
/> | ||
</StudioModal.Dialog> | ||
</> | ||
); | ||
} |
1 change: 1 addition & 0 deletions
1
...g/editModal/EditOptions/OptionTabs/EditTab/OptionListEditor/LibraryOptionsEditor/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { LibraryOptionsEditor } from './LibraryOptionsEditor'; |
11 changes: 11 additions & 0 deletions
11
...ns/OptionTabs/EditTab/OptionListEditor/ManualOptionsEditor/ManualOptionsEditor.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.editOptionTabModal[open] { | ||
--code-list-modal-min-width: min(80rem, 100%); | ||
--code-list-modal-height: min(45rem, 100%); | ||
|
||
min-width: var(--code-list-modal-min-width); | ||
height: var(--code-list-modal-height); | ||
} | ||
|
||
.content { | ||
height: 100%; | ||
} |
57 changes: 57 additions & 0 deletions
57
...itOptions/OptionTabs/EditTab/OptionListEditor/ManualOptionsEditor/ManualOptionsEditor.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { useOptionListEditorTexts } from '@altinn/ux-editor/components/config/editModal/EditOptions/OptionTabs/hooks'; | ||
import type { Option } from 'app-shared/types/Option'; | ||
import classes from './ManualOptionsEditor.module.css'; | ||
import type { IGenericEditComponent } from '@altinn/ux-editor/components/config/componentConfig'; | ||
import type { SelectionComponentType } from '@altinn/ux-editor/types/FormComponent'; | ||
import { useTranslation } from 'react-i18next'; | ||
import React, { useRef } from 'react'; | ||
import { StudioCodeListEditor, StudioModal, StudioProperty } from '@studio/components'; | ||
|
||
type ManualOptionsEditorProps = Pick< | ||
IGenericEditComponent<SelectionComponentType>, | ||
'component' | 'handleComponentChange' | ||
>; | ||
|
||
export function ManualOptionsEditor({ | ||
component, | ||
handleComponentChange, | ||
}: ManualOptionsEditorProps): React.ReactNode { | ||
const { t } = useTranslation(); | ||
const modalRef = useRef<HTMLDialogElement>(null); | ||
const editorTexts = useOptionListEditorTexts(); | ||
|
||
const handleBlurAny = (options: Option[]) => { | ||
if (component.optionsId) { | ||
delete component.optionsId; | ||
} | ||
|
||
handleComponentChange({ | ||
...component, | ||
options, | ||
}); | ||
}; | ||
|
||
return ( | ||
<> | ||
<StudioProperty.Button | ||
value={t('ux_editor.modal_properties_code_list_custom_list')} | ||
title={t('ux_editor.options.option_edit_text')} | ||
property={t('ux_editor.modal_properties_code_list_button_title_manual')} | ||
onClick={() => modalRef.current.showModal()} | ||
/> | ||
<StudioModal.Dialog | ||
ref={modalRef} | ||
className={classes.editOptionTabModal} | ||
contentClassName={classes.content} | ||
closeButtonTitle={t('general.close')} | ||
heading={t('ux_editor.modal_add_options_code_list')} | ||
> | ||
<StudioCodeListEditor | ||
codeList={component.options ?? []} | ||
onBlurAny={handleBlurAny} | ||
texts={editorTexts} | ||
/> | ||
</StudioModal.Dialog> | ||
</> | ||
); | ||
} |
1 change: 1 addition & 0 deletions
1
...ig/editModal/EditOptions/OptionTabs/EditTab/OptionListEditor/ManualOptionsEditor/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { ManualOptionsEditor } from './ManualOptionsEditor'; |
Oops, something went wrong.