From ac1b2e0e73fbef6b6c8da5be2dfb9c3aebf73d99 Mon Sep 17 00:00:00 2001 From: Marko Haarni Date: Thu, 12 Oct 2023 11:39:43 +0300 Subject: [PATCH] HAI-1463 Change frontend to use hanke status returned from backend Status of hanke can be either DRAFT, PUBLIC or ENDED and this information is returned from backend. Changed frontend to keep track of the backend state. At this point this affects displaying draft state notification if hanke status is DRAFT as well as displaying Add application button in hanke form and hanke view if hanke status is PUBLIC. Also removed the saveType field as it's not needed anymore. --- src/domain/hanke/edit/HankeForm.tsx | 44 +++++++------------ .../hanke/edit/components/ContactsSummary.tsx | 4 +- .../HankeDraftStateNotification.tsx | 30 ++++++------- .../hanke/hankeUsers/UserRightsCheck.tsx | 2 +- src/domain/hanke/hankeView/HankeView.test.tsx | 13 ++++-- src/domain/hanke/hankeView/HankeView.tsx | 32 ++++++-------- .../hanke/portfolio/HankePortfolio.test.tsx | 10 +++++ src/domain/mocks/data/hankkeet-data.ts | 22 +++++----- src/domain/mocks/hankeDraft.ts | 2 +- src/domain/mocks/hankeList.ts | 4 +- src/domain/types/hanke.ts | 17 +++---- 11 files changed, 90 insertions(+), 90 deletions(-) diff --git a/src/domain/hanke/edit/HankeForm.tsx b/src/domain/hanke/edit/HankeForm.tsx index 0db9e4219..30a335cc3 100644 --- a/src/domain/hanke/edit/HankeForm.tsx +++ b/src/domain/hanke/edit/HankeForm.tsx @@ -14,7 +14,7 @@ import HankeFormHaitat from './HankeFormHaitat'; import HankeFormSummary from './HankeFormSummary'; import FormNotifications from './components/FormNotifications'; import './HankeForm.styles.scss'; -import { HankeData, HANKE_SAVETYPE } from '../../types/hanke'; +import { HankeData } from '../../types/hanke'; import { convertFormStateToHankeData } from './utils'; import api from '../../api/api'; import MultipageForm from '../../forms/MultipageForm'; @@ -22,21 +22,9 @@ import FormActions from '../../forms/components/FormActions'; import { useLocalizedRoutes } from '../../../common/hooks/useLocalizedRoutes'; import ApplicationAddDialog from '../../application/components/ApplicationAddDialog'; -async function saveHanke({ - data, - saveType = HANKE_SAVETYPE.DRAFT, -}: { - data: HankeDataFormState; - saveType?: HANKE_SAVETYPE; - navigateTo?: string; -}) { - if (!data.alueet?.length) { - return data; - } - +async function saveHanke({ data }: { data: HankeDataFormState; navigateTo?: string }) { const requestData = { ...convertFormStateToHankeData(data), - saveType, }; const response = data.hankeTunnus @@ -76,7 +64,7 @@ const HankeForm: React.FC> = ({ const { register, - formState: { errors, isDirty, isValid }, + formState: { errors, isDirty }, getValues, setValue, handleSubmit, @@ -85,6 +73,7 @@ const HankeForm: React.FC> = ({ const isNewHanke = !formData.hankeTunnus; const formValues = getValues(); + const isHankePublic = formValues.status === 'PUBLIC'; const formHeading = isNewHanke ? t('hankeForm:pageHeaderNew') : t('hankeForm:pageHeaderEdit', { hankeTunnus: formData.hankeTunnus }); @@ -99,9 +88,8 @@ const HankeForm: React.FC> = ({ onSuccess(data, { navigateTo }) { setValue('hankeTunnus', data.hankeTunnus); setValue('tormaystarkasteluTulos', data.tormaystarkasteluTulos); - if (data.alueet) { - setShowNotification('success'); - } + setValue('status', data.status); + setShowNotification('success'); if (navigateTo) { navigate(navigateTo); } @@ -119,13 +107,12 @@ const HankeForm: React.FC> = ({ function save() { hankeMutation.mutate({ data: getValues(), - saveType: HANKE_SAVETYPE.SUBMIT, navigateTo: HANKEPORTFOLIO.path, }); } function saveAndAddApplication() { - hankeMutation.mutate({ data: getValues(), saveType: HANKE_SAVETYPE.SUBMIT }); + hankeMutation.mutate({ data: getValues() }); setShowAddApplicationDialog(true); } @@ -210,14 +197,15 @@ const HankeForm: React.FC> = ({ )} {lastStep && ( <> - + {isHankePublic && ( + + )} - + {isHankePublic ? ( + + ) : null} @@ -251,12 +252,7 @@ const HankeView: React.FC = ({ - diff --git a/src/domain/hanke/portfolio/HankePortfolio.test.tsx b/src/domain/hanke/portfolio/HankePortfolio.test.tsx index a15336ba1..9afaab508 100644 --- a/src/domain/hanke/portfolio/HankePortfolio.test.tsx +++ b/src/domain/hanke/portfolio/HankePortfolio.test.tsx @@ -110,4 +110,14 @@ describe.only('HankePortfolio', () => { expect(screen.queryAllByTestId('hankeEditLink')).toHaveLength(1); }); }); + + test('Should show draft state notification for hankkeet that are in draft state', async () => { + render(); + + expect( + screen.getAllByText( + 'Hanke on luonnostilassa. Alueiden haittatiedot ja muut pakolliset tiedot on täytettävä hankkeen julkaisemiseksi ja lupien lisäämiseksi.', + ), + ).toHaveLength(1); + }); }); diff --git a/src/domain/mocks/data/hankkeet-data.ts b/src/domain/mocks/data/hankkeet-data.ts index b3f5738a9..5e90d217a 100644 --- a/src/domain/mocks/data/hankkeet-data.ts +++ b/src/domain/mocks/data/hankkeet-data.ts @@ -17,7 +17,7 @@ const hankkeet: HankeDataDraft[] = [ createdAt: '2020-11-27T11:33:17.0875Z', modifiedBy: null, modifiedAt: null, - saveType: 'DRAFT', + status: 'DRAFT', omistajat: [], rakennuttajat: [], toteuttajat: [], @@ -41,7 +41,7 @@ const hankkeet: HankeDataDraft[] = [ createdAt: '2022-11-27T11:43:43.481215Z', modifiedBy: null, modifiedAt: null, - saveType: 'DRAFT', + status: 'PUBLIC', liikennehaittaindeksi: { indeksi: 4.0, // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -240,7 +240,7 @@ const hankkeet: HankeDataDraft[] = [ createdAt: '2022-11-27T11:44:22.443735Z', modifiedBy: null, modifiedAt: null, - saveType: 'DRAFT', + status: 'PUBLIC', omistajat: [ { id: 1, @@ -343,7 +343,7 @@ const hankkeet: HankeDataDraft[] = [ createdAt: '2020-11-27T12:07:41.210244Z', modifiedBy: null, modifiedAt: null, - saveType: 'DRAFT', + status: 'DRAFT', omistajat: [], rakennuttajat: [], toteuttajat: [], @@ -366,7 +366,7 @@ const hankkeet: HankeDataDraft[] = [ createdAt: '2022-11-23T10:37:36.362778Z', modifiedBy: null, modifiedAt: null, - saveType: 'DRAFT', + status: 'DRAFT', omistajat: [], rakennuttajat: [], toteuttajat: [], @@ -389,7 +389,7 @@ const hankkeet: HankeDataDraft[] = [ createdAt: '2020-11-30T13:43:13.599591Z', modifiedBy: null, modifiedAt: null, - saveType: 'DRAFT', + status: 'DRAFT', omistajat: [], rakennuttajat: [], toteuttajat: [], @@ -412,7 +412,7 @@ const hankkeet: HankeDataDraft[] = [ createdAt: '2020-11-30T13:43:15.322044Z', modifiedBy: null, modifiedAt: null, - saveType: 'DRAFT', + status: 'DRAFT', omistajat: [], rakennuttajat: [], toteuttajat: [], @@ -435,8 +435,8 @@ const hankkeet: HankeDataDraft[] = [ createdAt: '2020-11-30T13:43:24.301796Z', modifiedBy: null, modifiedAt: null, - saveType: 'DRAFT', omistajat: [], + status: 'DRAFT', rakennuttajat: [], toteuttajat: [], muut: [], @@ -458,7 +458,7 @@ const hankkeet: HankeDataDraft[] = [ createdAt: '2020-11-30T13:43:27.330822Z', modifiedBy: null, modifiedAt: null, - saveType: 'DRAFT', + status: 'DRAFT', omistajat: [], rakennuttajat: [], toteuttajat: [], @@ -481,7 +481,7 @@ const hankkeet: HankeDataDraft[] = [ createdAt: '2020-12-01T14:22:11.838844Z', modifiedBy: null, modifiedAt: null, - saveType: 'DRAFT', + status: 'DRAFT', omistajat: [], rakennuttajat: [], toteuttajat: [], @@ -504,7 +504,7 @@ const hankkeet: HankeDataDraft[] = [ createdAt: '2020-12-01T14:34:24.443622Z', modifiedBy: null, modifiedAt: null, - saveType: 'DRAFT', + status: 'DRAFT', omistajat: [], rakennuttajat: [], toteuttajat: [], diff --git a/src/domain/mocks/hankeDraft.ts b/src/domain/mocks/hankeDraft.ts index 4cc9e62f8..28d1d412d 100644 --- a/src/domain/mocks/hankeDraft.ts +++ b/src/domain/mocks/hankeDraft.ts @@ -15,7 +15,7 @@ const hankeDraft: HankeDataDraft = { createdAt: '2023-11-27T11:33:17.0875Z', modifiedBy: null, modifiedAt: null, - saveType: 'DRAFT', + status: 'DRAFT', omistajat: [], rakennuttajat: [], toteuttajat: [], diff --git a/src/domain/mocks/hankeList.ts b/src/domain/mocks/hankeList.ts index 1a1de5763..5b5afb5cc 100644 --- a/src/domain/mocks/hankeList.ts +++ b/src/domain/mocks/hankeList.ts @@ -16,7 +16,7 @@ const hankeList: HankeData[] = [ createdAt: '2020-11-27T11:33:17.0875Z', modifiedBy: null, modifiedAt: null, - saveType: 'DRAFT', + status: 'PUBLIC', omistajat: [], rakennuttajat: [], toteuttajat: [], @@ -85,7 +85,7 @@ const hankeList: HankeData[] = [ createdAt: '2020-11-27T11:43:43.481215Z', modifiedBy: null, modifiedAt: null, - saveType: 'DRAFT', + status: 'DRAFT', omistajat: [], rakennuttajat: [], toteuttajat: [], diff --git a/src/domain/types/hanke.ts b/src/domain/types/hanke.ts index 6d0a4873c..a732d819e 100644 --- a/src/domain/types/hanke.ts +++ b/src/domain/types/hanke.ts @@ -90,13 +90,6 @@ export enum HANKE_TARINAHAITTA { } export type HANKE_TARINAHAITTA_KEY = keyof typeof HANKE_TARINAHAITTA; -export enum HANKE_SAVETYPE { - AUTO = 'AUTO', - DRAFT = 'DRAFT', - SUBMIT = 'SUBMIT', -} -export type HANKE_SAVETYPE_KEY = keyof typeof HANKE_SAVETYPE; - export enum HANKE_CONTACT_TYPE { OMISTAJAT = 'omistajat', RAKENNUTTAJAT = 'rakennuttajat', @@ -186,6 +179,14 @@ export enum HANKE_INDEX_STATE { export type HANKE_INDEX_STATE_KEY = keyof typeof HANKE_INDEX_STATE; +enum HANKE_STATUS { + DRAFT = 'DRAFT', + PUBLIC = 'PUBLIC', + ENDED = 'ENDED', +} + +type HANKE_STATUS_KEY = keyof typeof HANKE_STATUS; + export interface HankeData { id: number; hankeTunnus: string; @@ -205,7 +206,7 @@ export interface HankeData { toteuttajat: Array; muut: Array; tormaystarkasteluTulos: HankeIndexData | null; - saveType: HANKE_SAVETYPE_KEY; + status: HANKE_STATUS_KEY; version?: number; createdBy?: string; createdAt?: string;