Skip to content

Commit

Permalink
Create a usePdf hook
Browse files Browse the repository at this point in the history
  • Loading branch information
standeren committed Sep 12, 2024
1 parent 41d510a commit 6b90fb1
Show file tree
Hide file tree
Showing 22 changed files with 187 additions and 370 deletions.
14 changes: 12 additions & 2 deletions backend/src/Designer/Controllers/AppDevelopmentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class AppDevelopmentController : Controller
private readonly IAltinnGitRepositoryFactory _altinnGitRepositoryFactory;
private readonly ApplicationInsightsSettings _applicationInsightsSettings;
private readonly IMediator _mediator;
private readonly IUserRequestsSynchronizationService _userRequestsSynchronizationService;


/// <summary>
Expand All @@ -45,14 +46,17 @@ public class AppDevelopmentController : Controller
/// <param name="sourceControl">The source control service.</param>
/// <param name="altinnGitRepositoryFactory"></param>
/// <param name="applicationInsightsSettings">An <see cref="ApplicationInsightsSettings"/></param>
public AppDevelopmentController(IAppDevelopmentService appDevelopmentService, IRepository repositoryService, ISourceControl sourceControl, IAltinnGitRepositoryFactory altinnGitRepositoryFactory, ApplicationInsightsSettings applicationInsightsSettings, IMediator mediator)
/// <param name="mediator"></param>
/// <param name="userRequestsSynchronizationService">An <see cref="IUserRequestsSynchronizationService"/> used to control parallel execution of user requests.</param>
public AppDevelopmentController(IAppDevelopmentService appDevelopmentService, IRepository repositoryService, ISourceControl sourceControl, IAltinnGitRepositoryFactory altinnGitRepositoryFactory, ApplicationInsightsSettings applicationInsightsSettings, IMediator mediator, IUserRequestsSynchronizationService userRequestsSynchronizationService)
{
_appDevelopmentService = appDevelopmentService;
_repository = repositoryService;
_sourceControl = sourceControl;
_altinnGitRepositoryFactory = altinnGitRepositoryFactory;
_applicationInsightsSettings = applicationInsightsSettings;
_mediator = mediator;
_userRequestsSynchronizationService = userRequestsSynchronizationService;
}

/// <summary>
Expand Down Expand Up @@ -213,9 +217,11 @@ public ActionResult UpdateFormLayoutName(string org, string app, [FromQuery] str
[Route("layout-settings")]
public async Task<ActionResult> SaveLayoutSettings(string org, string app, [FromQuery] string layoutSetName, [FromBody] JsonNode layoutSettings, CancellationToken cancellationToken)
{
string developer = AuthenticationHelper.GetDeveloperUserName(HttpContext);
SemaphoreSlim semaphore = _userRequestsSynchronizationService.GetRequestsSemaphore(org, app, developer);
await semaphore.WaitAsync();
try
{
string developer = AuthenticationHelper.GetDeveloperUserName(HttpContext);
var editingContext = AltinnRepoEditingContext.FromOrgRepoDeveloper(org, app, developer);
await _appDevelopmentService.SaveLayoutSettings(editingContext, layoutSettings, layoutSetName, cancellationToken);
return Ok();
Expand All @@ -224,6 +230,10 @@ public async Task<ActionResult> SaveLayoutSettings(string org, string app, [From
{
return NotFound(exception.Message);
}
finally
{
semaphore.Release();
}
}

/// <summary>
Expand Down
1 change: 0 additions & 1 deletion frontend/language/src/nb.json
Original file line number Diff line number Diff line change
Expand Up @@ -1547,7 +1547,6 @@
"ux_editor.page_config_pdf_convert_existing_pdf": "Konverter eksisterende PDF til skjemaside",
"ux_editor.page_config_pdf_convert_info_when_custom_pdf_exists": "Du har allerede en egendefinert PDF. Dersom du fortsatt ønsker å kovertere denne siden til PDF kan du velge om du vil slette den eksisterende PDFen for godt eller om du vil konvertere den tilbake til en vanlig skjemaside.",
"ux_editor.page_config_pdf_convert_page_to_pdf": "Gjør om siden til PDF",
"ux_editor.page_config_pdf_current_page_is_pdf_info": "Denne siden er satt opp til å overstyre den autogenererte PDFen. Noen PDF innstilliger er derfor ikke lenger tilgjengelige.",
"ux_editor.page_config_pdf_delete_existing_pdf": "Slett eksisterende PDF",
"ux_editor.page_config_pdf_exclude_components_from_default_pdf": "Velg hvilke komponenter fra siden som skal skjules i standard PDF",
"ux_editor.page_config_pdf_exclude_page_from_default_pdf": "Ekskluder siden fra standard PDF",
Expand Down
10 changes: 0 additions & 10 deletions frontend/packages/shared/src/hooks/usePdfLayoutName.ts

This file was deleted.

20 changes: 19 additions & 1 deletion frontend/packages/ux-editor/src/classes/FormLayoutSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export class FormLayoutSettings {
this.layoutSettings = layoutSettings;
}

public getFormLayoutSettings(): FormLayoutSettings {
return this;
}

public getLayoutSettings(): ILayoutSettings {
return this.layoutSettings;
}
Expand All @@ -16,7 +20,21 @@ export class FormLayoutSettings {
return this;
}

public getPdfLayoutName(): string {
public deletePdfLayoutName(): FormLayoutSettings {
delete this.layoutSettings.pages.pdfLayoutName;
return this;
}

public getPdfLayoutName(): string | undefined {
return this.layoutSettings.pages.pdfLayoutName;
}

public addPageToOrder(layoutName: string): void {
this.layoutSettings.pages.order.push(layoutName);
}

public deletePageFromOrder(layoutName: string): void {
const indexOfPage = this.layoutSettings.pages.order.indexOf(layoutName);
this.layoutSettings.pages.order.splice(indexOfPage, 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export const EditPageId = ({ layoutName }: EditPageIdProps) => {

const layoutOrder = formLayoutSettings?.pages?.order;

// Need to update this endpoint to handle changing name of PDF layout --> update pdfLayoutName in Settings and not order
const handleSaveNewName = (newName: string) => {
if (newName === layoutName) return;
updateLayoutName(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React from 'react';
import { StudioModal } from '@studio/components';
import { OverrideCurrentPdfByConversionChoices } from './OverrideCurrentPdfByConversionChoices';
import { Heading } from '@digdir/designsystemet-react';
import { useTranslation } from 'react-i18next';
import { usePdf } from '@altinn/ux-editor/hooks/usePdf/usePdf';
import { useStudioEnvironmentParams } from 'app-shared/hooks/useStudioEnvironmentParams';
import { useAppContext } from '@altinn/ux-editor/hooks';
import { useDeleteLayoutMutation } from '@altinn/ux-editor/hooks/mutations/useDeleteLayoutMutation';
import { useSavableFormLayoutSettings } from '@altinn/ux-editor/hooks/useSavableFormLayoutSettings';

type ConvertChoicesModalProps = {
showConvertChoices: boolean;
onClose: () => void;
};
export const ConvertChoicesModal = ({ showConvertChoices, onClose }: ConvertChoicesModalProps) => {
const { org, app } = useStudioEnvironmentParams();
const { selectedFormLayoutSetName } = useAppContext();
const { t } = useTranslation();
const { mutate: deleteLayout } = useDeleteLayoutMutation(org, app, selectedFormLayoutSetName);
const { getPdfLayoutName, convertCurrentPageToPdf, convertExistingPdfToPage } = usePdf();
const savableLayoutSettings = useSavableFormLayoutSettings();

const handleConvertPageToPdfAndConvertCurrent = () => {
convertExistingPdfToPage();
convertCurrentPageToPdf();
savableLayoutSettings.save();
onClose();
};

const handleConvertPageToPdfAndDeleteCurrent = () => {
const currentPdfLayoutName = getPdfLayoutName();
convertCurrentPageToPdf();
deleteLayout(currentPdfLayoutName);
savableLayoutSettings.save();
onClose();
};

return (
<StudioModal
isOpen={showConvertChoices}
onClose={onClose}
title={
<Heading level={1} size='small'>
{t('ux_editor.page_config_pdf_convert_page_to_pdf')}
</Heading>
}
closeButtonLabel={t('ux_editor.page_config_pdf_abort_converting_page_to_pdf')}
>
<OverrideCurrentPdfByConversionChoices
onConvertPageToPdfAndConvertCurrent={handleConvertPageToPdfAndConvertCurrent}
onConvertPageToPdfAndDeleteCurrent={handleConvertPageToPdfAndDeleteCurrent}
/>
</StudioModal>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,28 @@ import { useTranslation } from 'react-i18next';
import classes from './OverrideCurrentPdfByConversionChoices.module.css';

export interface OverrideCurrentPdfByConversionChoicesProps {
onConvertPageToPdf: (deleteCurrent: boolean) => void;
onConvertPageToPdfAndConvertCurrent: () => void;
onConvertPageToPdfAndDeleteCurrent: () => void;
}

export const OverrideCurrentPdfByConversionChoices = ({
onConvertPageToPdf,
onConvertPageToPdfAndConvertCurrent,
onConvertPageToPdfAndDeleteCurrent,
}: OverrideCurrentPdfByConversionChoicesProps) => {
const { t } = useTranslation();

return (
<div className={classes.modal}>
<Paragraph>{t('ux_editor.page_config_pdf_convert_info_when_custom_pdf_exists')}</Paragraph>
<div className={classes.buttonContainer}>
<StudioButton size='small' onClick={() => onConvertPageToPdf(false)}>
<StudioButton size='small' onClick={onConvertPageToPdfAndConvertCurrent}>
{t('ux_editor.page_config_pdf_convert_existing_pdf')}
</StudioButton>
<StudioButton
color='danger'
size='small'
variant='tertiary'
onClick={() => onConvertPageToPdf(true)}
onClick={onConvertPageToPdfAndDeleteCurrent}
>
{t('ux_editor.page_config_pdf_delete_existing_pdf')}
</StudioButton>
Expand Down

This file was deleted.

Loading

0 comments on commit 6b90fb1

Please sign in to comment.