Skip to content

Commit

Permalink
Remove create PDF button and add ConvertBackToForm button in PDF config
Browse files Browse the repository at this point in the history
  • Loading branch information
standeren committed Sep 10, 2024
1 parent 04a0e28 commit d57a769
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
padding-top: var(--fds-spacing-5);
padding-inline: var(--fds-spacing-5);
display: grid;
justify-items: flex-start;
gap: var(--fds-spacing-5);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState } from 'react';
import { StudioModal, StudioProperty } from '@studio/components';
import { StudioButton, StudioModal, StudioProperty } from '@studio/components';
import { useTranslation } from 'react-i18next';
import { FileIcon } from '@studio/icons';
import { Alert, Combobox, Heading, Switch } from '@digdir/designsystemet-react';
Expand Down Expand Up @@ -72,23 +72,30 @@ export const PdfConfig = () => {
const handleConvertPageToPdfWhenExistingPdf = (deleteCurrent?: boolean) => {
// Remove all layouts in exclude when setting a dedicated PDF or converting a current into PDF?
const pdfLayoutName = formLayoutSettings.pages.pdfLayoutName;
// Delete current pdfLayout if it exists. Should include a choice here to convert it back to regular layout or delete it
if (deleteCurrent) {
deleteLayout(pdfLayoutName);
} else {
formLayoutSettings.pages.order.push(pdfLayoutName);
}
handleConvertPageToPdf();
setShowConvertChoices(false);
};

const handleConvertPageToPdf = () => {
formLayoutSettings.pages.pdfLayoutName = selectedFormLayoutName;
formLayoutSettings.pages.order.splice(
formLayoutSettings.pages.order.indexOf(selectedFormLayoutName),
1,
);
mutateFormLayoutSettings(formLayoutSettings);
};

const convertExistingPdfToFormLayout = () => {
delete formLayoutSettings.pages.pdfLayoutName;
formLayoutSettings.pages.order.push(selectedFormLayoutName);
mutateFormLayoutSettings(formLayoutSettings);
};

const handleExcludePage = (checked: boolean) => {
const updatedFormLayoutSettings = handleExcludePageFromPdf(
formLayoutSettings,
Expand Down Expand Up @@ -146,7 +153,12 @@ export const PdfConfig = () => {
{t('ux_editor.page_config_pdf_exclude_page_from_default_pdf')}
</Switch>
{currentPageIsPdf && (
<Alert>{t('ux_editor.page_config_pdf_current_page_is_pdf_info')}</Alert>
<>
<StudioButton size='small' onClick={convertExistingPdfToFormLayout}>
{t('ux_editor.page_config_pdf_convert_existing_pdf')}
</StudioButton>
<Alert size='small'>{t('ux_editor.page_config_pdf_current_page_is_pdf_info')}</Alert>
</>
)}
{!usePdfLayout && (
<Combobox
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,21 @@ export const DesignView = (): ReactNode => {
if (layout === undefined) return null;

// Check if the layout has unique component IDs
const isValidLayout = !duplicatedIdsExistsInLayout(layout.data);
const isInvalidLayout = duplicatedIdsExistsInLayout(layout.data);

return (
<PageAccordion
key={i}
pageName={layout.page}
isOpen={layout.page === selectedFormLayoutName}
onClick={() => handleClickAccordion(layout.page)}
isValid={isValidLayout}
hasUniqueIds={!layoutsWithDuplicateComponents.duplicateLayouts.includes(layout.page)}
isInvalid={isInvalidLayout}
hasDuplicatedIds={layoutsWithDuplicateComponents.duplicateLayouts.includes(layout.page)}
>
{layout.page === selectedFormLayoutName && (
<FormLayout
layout={layout.data}
isValid={isValidLayout}
isInvalid={isInvalidLayout}
duplicateComponents={layoutsWithDuplicateComponents.duplicateComponents}
/>
)}
Expand Down Expand Up @@ -150,6 +150,9 @@ export const DesignView = (): ReactNode => {
pdfLayoutName={pdfLayoutName}
selectedFormLayoutName={selectedFormLayoutName}
onAccordionClick={() => handleClickAccordion(pdfLayoutName)}
hasDuplicatedIds={layoutsWithDuplicateComponents.duplicateLayouts.includes(
pdfLayoutName,
)}
/>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { internalLayoutWithMultiPageGroup } from '../../testing/layoutWithMultiP

const defaultProps: FormLayoutProps = {
layout: layoutMock,
isValid: true,
isInvalid: false,
};

describe('FormLayout', () => {
Expand Down Expand Up @@ -40,7 +40,7 @@ describe('FormLayout', () => {
},
};

render({ layout: layoutWithDuplicatedIds, isValid: false });
render({ layout: layoutWithDuplicatedIds, isInvalid: true });

expect(
screen.getByText(textMock('ux_editor.formLayout.warning_duplicates')),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { FormLayoutWarning } from './FormLayoutWarning';

export interface FormLayoutProps {
layout: IInternalLayout;
isValid: boolean;
isInvalid: boolean;
duplicateComponents?: string[];
}

export const FormLayout = ({ layout, isValid, duplicateComponents }: FormLayoutProps) => {
if (!isValid) {
export const FormLayout = ({ layout, isInvalid, duplicateComponents }: FormLayoutProps) => {
if (isInvalid) {
return <FormLayoutWarning layout={layout} />;
}
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export type PageAccordionProps = {
children: ReactNode;
isOpen: boolean;
onClick: () => void;
isValid?: boolean;
hasUniqueIds?: boolean;
isInvalid?: boolean;
hasDuplicatedIds?: boolean;
pageIsReceipt?: boolean;
pageIsPdf?: boolean;
showNavigationMenu?: boolean;
Expand All @@ -32,11 +32,7 @@ export type PageAccordionProps = {
* @property {ReactNode}[children] - The children of the component
* @property {boolean}[isOpen] - If the accordion is open or not
* @property {function}[onClick] - Function to execute when the accordion is clicked
<<<<<<< HEAD
=======
* @property {boolean}[pageIsReceipt] - If the page is receipt or not
* @property {boolean}[pageIsPdf] - If the page is pdf or not
>>>>>>> 5633f518a (Add PDF config for page and layoutset)
*
* @returns {ReactNode} - The rendered component
*/
Expand All @@ -45,9 +41,8 @@ export const PageAccordion = ({
children,
isOpen,
onClick,
isValid,
hasUniqueIds,
pageIsReceipt,
isInvalid,
hasDuplicatedIds,
pageIsPdf,
showNavigationMenu = true,
}: PageAccordionProps): ReactNode => {
Expand All @@ -72,11 +67,11 @@ export const PageAccordion = ({
};

return (
<Accordion.Item className={pageIsReceipt && classes.receiptItem} open={isOpen}>
<Accordion.Item open={isOpen}>
<div className={classes.accordionHeaderRow}>
<Accordion.Header
className={
isValid && hasUniqueIds ? classes.accordionHeader : classes.accordionHeaderWarning
isInvalid || hasDuplicatedIds ? classes.accordionHeaderWarning : classes.accordionHeader
}
level={3}
onHeaderClick={onClick}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ export interface PdfLayoutAccordionProps {
pdfLayoutName: string;
selectedFormLayoutName: string;
onAccordionClick: () => void;
hasDuplicatedIds: boolean;
}
export const PdfLayoutAccordion = ({
pdfLayoutName,
selectedFormLayoutName,
onAccordionClick,
hasDuplicatedIds,
}: PdfLayoutAccordionProps): React.ReactNode => {
const layouts = useFormLayouts();
const formLayoutData = mapFormLayoutsToFormLayoutPages(layouts);
Expand All @@ -25,14 +27,15 @@ export const PdfLayoutAccordion = ({
pageName={pdfLayoutData.page}
isOpen={pdfLayoutData.page === selectedFormLayoutName}
onClick={onAccordionClick}
isValid={!duplicatedIdsExistsInLayout(pdfLayoutData.data)}
isInvalid={duplicatedIdsExistsInLayout(pdfLayoutData.data)}
hasDuplicatedIds={hasDuplicatedIds}
showNavigationMenu={false}
pageIsPdf={true}
>
{pdfLayoutData.page === selectedFormLayoutName && (
<FormLayout
layout={pdfLayoutData.data}
isValid={!duplicatedIdsExistsInLayout(pdfLayoutData.data)}
isInvalid={duplicatedIdsExistsInLayout(pdfLayoutData.data)}
/>
)}
</PageAccordion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export const useAddLayoutMutation = (org: string, app: string, layoutSetName: st

return useMutation({
mutationFn: async ({ layoutName, isPdf = false }: AddLayoutMutationArgs) => {
const layoutSettings: ILayoutSettings = formLayoutSettingsQuery.data;
const layouts = formLayoutsQuery.data;

if (Object.keys(layouts).indexOf(layoutName) !== -1) throw Error('Layout already exists');
Expand Down

0 comments on commit d57a769

Please sign in to comment.