Skip to content

Commit

Permalink
Fix/ some bugs in attachmentList (#12680)
Browse files Browse the repository at this point in the history
* add dataTypeIds to customProperties

* hide PDF selector/switch when layout set is not customReceipt
  • Loading branch information
lassopicasso authored and Jondyr committed Jun 10, 2024
1 parent e1b599b commit d1a6a22
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 11 deletions.
3 changes: 2 additions & 1 deletion frontend/language/src/nb.json
Original file line number Diff line number Diff line change
Expand Up @@ -1646,7 +1646,8 @@
"ux_editor.component_title.AddressComponent": "Adresse",
"ux_editor.component_title.Alert": "Varsel",
"ux_editor.component_title.AttachmentList": "Liste over vedlegg",
"ux_editor.component_title.AttachmentList_error": "Du må velge minst ett vedlegg eller PDF",
"ux_editor.component_title.AttachmentListOrPdf_error": "Du må velge minst ett vedlegg eller PDF",
"ux_editor.component_title.AttachmentList_error": "Du må velge minst ett vedlegg",
"ux_editor.component_title.AttachmentList_legend": "Vedleggsliste",
"ux_editor.component_title.Button": "Knapp",
"ux_editor.component_title.ButtonGroup": "Knappegruppe",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ export const FormComponentConfig = ({
const { hasCustomFileEndings, validFileEndings, grid } = properties;

// Add any properties that have a custom implementation to this list so they are not duplicated in the generic view
const customProperties = ['hasCustomFileEndings', 'validFileEndings', 'grid', 'children'];
const customProperties = [
'hasCustomFileEndings',
'validFileEndings',
'grid',
'children',
'dataTypeIds',
];

const booleanPropertyKeys: string[] = getSupportedPropertyKeysForPropertyType(
schema.properties,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ const defaultLayoutSets: LayoutSets = {
dataTypes: 'layoutSetId3',
tasks: ['Task_3'],
},
{
id: 'layoutSetId4',
dataTypes: 'layoutSetId4',
tasks: ['CustomReceipt'],
},
],
};

Expand Down Expand Up @@ -117,7 +122,7 @@ describe('AttachmentListComponent', () => {
dataTypeIds: ['test3', 'test4'],
},
},
'layoutSetId3',
'layoutSetId4',
);

// Todo: Combobox onChangeValue trigger on initial render, this can be fixed when we start to use >v0.55.0 of designsystem. Replace value prop with initialValue prop in combobox
Expand Down Expand Up @@ -145,6 +150,23 @@ describe('AttachmentListComponent', () => {
expect(handleComponentChange).toHaveBeenCalledTimes(2);
});

it('should not display pdf checkbox when current task is not CustomReceipt', async () => {
await render(
{
component: {
...defaultComponent,
},
},
'layoutSetId3',
);

expect(
screen.queryByRole('checkbox', {
name: textMock('ux_editor.component_properties.select_pdf'),
}),
).not.toBeInTheDocument();
});

it('should save to backend when toggle of current task and output is valid', async () => {
await render(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export const AttachmentListComponent = ({
});
};

const isTaskCustomReceipt = layoutSets?.sets
.find((layoutSet) => layoutSet.id === selectedFormLayoutSetName)
?.tasks.includes('CustomReceipt');

const { dataTypeIds = [] } = component || {};
const internalDataFormat = convertExternalToInternalFormat(availableAttachments, dataTypeIds);

Expand All @@ -54,6 +58,7 @@ export const AttachmentListComponent = ({
onChange={handleChange}
availableAttachments={availableAttachments}
internalDataFormat={internalDataFormat}
isTaskCustomReceipt={isTaskCustomReceipt}
/>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ type AttachmentListInternalFormatProps = {
onChange: (selectedDataTypes: InternalDataTypesFormat) => void;
availableAttachments: AvailableAttachementLists;
internalDataFormat: InternalDataTypesFormat;
isTaskCustomReceipt: boolean;
};

export const AttachmentListInternalFormat = ({
onChange,
availableAttachments,
internalDataFormat,
isTaskCustomReceipt,
}: AttachmentListInternalFormatProps) => {
const [dataTypesState, setDataTypesState] = useState<InternalDataTypesFormat>(internalDataFormat);
const [isValid, setIsValid] = useState<boolean>(true);
Expand Down Expand Up @@ -69,10 +71,15 @@ export const AttachmentListInternalFormat = ({
availableAttachments,
);
const { includePdf, currentTask, selectedDataTypes } = dataTypesState;

const errorMessage = isTaskCustomReceipt
? t('ux_editor.component_title.AttachmentListOrPdf_error')
: t('ux_editor.component_title.AttachmentList_error');

return (
<Fieldset
legend={t('ux_editor.component_title.AttachmentList_legend')}
error={!isValid && t('ux_editor.component_title.AttachmentList_error')}
error={!isValid && errorMessage}
>
<Switch
onChange={(e) => handleCurrentTaskChange(e.target.checked)}
Expand All @@ -81,13 +88,15 @@ export const AttachmentListInternalFormat = ({
>
{t('ux_editor.component_properties.current_task')}
</Switch>
<Switch
onChange={(e) => handleIncludePdfChange(e.target.checked)}
size='small'
checked={includePdf}
>
{t('ux_editor.component_properties.select_pdf')}
</Switch>
{isTaskCustomReceipt && (
<Switch
onChange={(e) => handleIncludePdfChange(e.target.checked)}
size='small'
checked={includePdf}
>
{t('ux_editor.component_properties.select_pdf')}
</Switch>
)}
<AttachmentListContent
currentAvailableAttachments={currentAvailableAttachments}
selectedDataTypes={ArrayUtils.intersection(selectedDataTypes, currentAvailableAttachments)}
Expand Down

0 comments on commit d1a6a22

Please sign in to comment.