Skip to content

Commit

Permalink
HAI-934 Make sure that user sending johtoselvitys taydennys is added …
Browse files Browse the repository at this point in the history
…as a contact person (#1006)

If user is not added as contact person, send button is disabled and a
notification is shown.
  • Loading branch information
markohaarni authored Dec 5, 2024
1 parent b538eda commit d20d36f
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/domain/application/applicationView/ApplicationView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ function ApplicationView({

const informationRequestFeatureEnabled = useIsInformationRequestFeatureEnabled(applicationType);

const { sendTaydennysButton, sendTaydennysDialog } = useSendTaydennys(application);
const { sendTaydennysButton, sendTaydennysDialog } = useSendTaydennys(application, signedInUser);

async function onSendApplication(pdr: PaperDecisionReceiver | undefined | null) {
applicationSendMutation.mutate({
Expand Down
43 changes: 32 additions & 11 deletions src/domain/application/applicationView/hooks/useSendTaydennys.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { useState } from 'react';
import { Button, IconEnvelope, IconQuestionCircle } from 'hds-react';
import { Button, IconEnvelope, IconQuestionCircle, Notification } from 'hds-react';
import { useTranslation } from 'react-i18next';
import { AlluStatus, Application } from '../../types/application';
import { validationSchema as johtoselvitysValidationSchema } from '../../../johtoselvitysTaydennys/validationSchema';
import ConfirmationDialog from '../../../../common/components/HDSConfirmationDialog/ConfirmationDialog';
import useIsInformationRequestFeatureEnabled from '../../taydennys/hooks/useIsInformationRequestFeatureEnabled';
import useSendTaydennysMutation from '../../taydennys/hooks/useSendTaydennys';
import { isContactIn } from '../../utils';
import { SignedInUser } from '../../../hanke/hankeUsers/hankeUser';

const validationSchemas = {
CABLE_REPORT: johtoselvitysValidationSchema,
EXCAVATION_NOTIFICATION: null,
};

export default function useSendTaydennys(application: Application) {
export default function useSendTaydennys(
application: Application,
signedInUser: SignedInUser | undefined,
) {
const { t } = useTranslation();
const informationRequestFeatureEnabled = useIsInformationRequestFeatureEnabled(
application.applicationType,
Expand All @@ -27,6 +32,9 @@ export default function useSendTaydennys(application: Application) {
application.taydennys.muutokset.length > 0;
const [showSendTaydennysDialog, setShowSendTaydennysDialog] = useState(false);
const sendTaydennysMutation = useSendTaydennysMutation();
const isContact =
application.taydennys && isContactIn(signedInUser, application.taydennys.applicationData);
const disableSendButton = Boolean(!isContact);

function openSendTaydennysDialog() {
setShowSendTaydennysDialog(true);
Expand All @@ -44,15 +52,28 @@ export default function useSendTaydennys(application: Application) {
}

const sendTaydennysButton = showSendTaydennysButton ? (
<Button
theme="coat"
iconLeft={<IconEnvelope />}
onClick={openSendTaydennysDialog}
loadingText={t('common:buttons:sendingText')}
isLoading={sendTaydennysMutation.isLoading}
>
{t('taydennys:buttons:sendTaydennys')}
</Button>
<>
<Button
theme="coat"
iconLeft={<IconEnvelope />}
onClick={openSendTaydennysDialog}
loadingText={t('common:buttons:sendingText')}
isLoading={sendTaydennysMutation.isLoading}
disabled={disableSendButton}
>
{t('taydennys:buttons:sendTaydennys')}
</Button>
{disableSendButton && (
<Notification
size="small"
style={{ marginTop: 'var(--spacing-xs)' }}
type="info"
label={t('hakemus:notifications:sendApplicationDisabled')}
>
{t('hakemus:notifications:sendApplicationDisabled')}
</Notification>
)}
</>
) : null;

const sendTaydennysDialog = (
Expand Down
32 changes: 32 additions & 0 deletions src/domain/johtoselvitysTaydennys/JohtoselvitysTaydennys.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import hankkeet from '../mocks/data/hankkeet-data';
import hakemukset from '../mocks/data/hakemukset-data';
import { server } from '../mocks/test-server';
import { Taydennys } from '../application/taydennys/types';
import { SignedInUser } from '../hanke/hankeUsers/hankeUser';

function setup(
options: {
Expand Down Expand Up @@ -179,4 +180,35 @@ describe('Sending taydennys', () => {

expect(screen.queryByRole('button', { name: /lähetä täydennys/i })).not.toBeInTheDocument();
});

test('Should show and disable send button and show notification when user is not a contact person', async () => {
server.use(
http.get('/api/hankkeet/:hankeTunnus/whoami', async () => {
return HttpResponse.json<SignedInUser>({
hankeKayttajaId: 'not-a-contact-person-id',
kayttooikeustaso: 'KATSELUOIKEUS',
kayttooikeudet: ['VIEW'],
});
}),
);

const { user } = setup({
taydennys: {
id: 'c0a1fe7b-326c-4b25-a7bc-d1797762c01c',
applicationData: cloneDeep(hakemukset[10] as Application<JohtoselvitysData>)
.applicationData,
muutokset: ['name'],
},
});

await user.click(screen.getByRole('button', { name: /yhteenveto/i }));

expect(screen.queryByRole('button', { name: /lähetä täydennys/i })).toBeInTheDocument();
expect(screen.queryByRole('button', { name: /lähetä täydennys/i })).toBeDisabled();
expect(
screen.queryAllByText(
'Hakemuksen voi lähettää ainoastaan hakemuksen yhteyshenkilönä oleva henkilö.',
),
).toHaveLength(2);
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { useState } from 'react';
import { FieldPath, FormProvider, useForm } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { Button, IconEnvelope, IconQuestionCircle, IconSaveDiskette, StepState } from 'hds-react';
import {
Button,
IconEnvelope,
IconQuestionCircle,
IconSaveDiskette,
Notification,
StepState,
} from 'hds-react';
import { yupResolver } from '@hookform/resolvers/yup';
import { useQueryClient } from 'react-query';
import { Taydennys } from '../application/taydennys/types';
Expand Down Expand Up @@ -34,6 +41,8 @@ import ApplicationSaveNotification from '../application/components/ApplicationSa
import { changeFormStep } from '../forms/utils';
import ConfirmationDialog from '../../common/components/HDSConfirmationDialog/ConfirmationDialog';
import useSendTaydennys from '../application/taydennys/hooks/useSendTaydennys';
import { isContactIn } from '../application/utils';
import { usePermissionsForHanke } from '../hanke/hankeUsers/hooks/useUserRightsForHanke';

type Props = {
taydennys: Taydennys<JohtoselvitysData>;
Expand All @@ -54,6 +63,7 @@ export default function JohtoselvitysTaydennysContainer({
useUpdateTaydennys<JohtoselvitysData, JohtoselvitysUpdateData>();
const sendTaydennysMutation = useSendTaydennys();
const [showSendDialog, setShowSendDialog] = useState(false);
const { data: signedInUser } = usePermissionsForHanke(hankeData.hankeTunnus);

const formContext = useForm<JohtoselvitysTaydennysFormValues>({
mode: 'onTouched',
Expand Down Expand Up @@ -240,6 +250,9 @@ export default function JohtoselvitysTaydennysContainer({

const lastStep = activeStepIndex === formSteps.length - 1;
const showSendButton = lastStep && taydennys.muutokset.length > 0 && isValid;
const isContact = isContactIn(signedInUser, getValues('applicationData'));
const disableSendButton = showSendButton && !isContact;

const saveAndQuitIsLoading = taydennysUpdateMutation.isLoading;
const saveAndQuitLoadingText = t('common:buttons:savingText');
return (
Expand All @@ -265,10 +278,21 @@ export default function JohtoselvitysTaydennysContainer({
iconLeft={<IconEnvelope aria-hidden="true" />}
loadingText={t('common:buttons:sendingText')}
isLoading={sendTaydennysMutation.isLoading}
disabled={disableSendButton}
>
{t('taydennys:buttons:sendTaydennys')}
</Button>
)}
{disableSendButton && (
<Notification
size="small"
style={{ marginTop: 'var(--spacing-xs)' }}
type="info"
label={t('hakemus:notifications:sendApplicationDisabled')}
>
{t('hakemus:notifications:sendApplicationDisabled')}
</Notification>
)}
</FormActions>
);
}}
Expand Down

0 comments on commit d20d36f

Please sign in to comment.