From d2030a63a53a963f01c810595d4941f073824a69 Mon Sep 17 00:00:00 2001
From: Jonas Braathen <jonas.braathen@usit.uio.no>
Date: Mon, 29 Nov 2021 12:11:47 +0100
Subject: [PATCH] Make consent step configurable. Workaround state changes
 being async.

---
 frontend/.env                                |  2 ++
 frontend/src/appConfig.ts                    |  4 +++
 frontend/src/routes/guest/register/index.tsx | 33 +++++++++++++-------
 3 files changed, 28 insertions(+), 11 deletions(-)

diff --git a/frontend/.env b/frontend/.env
index 70faa8f9..b519139c 100644
--- a/frontend/.env
+++ b/frontend/.env
@@ -1,6 +1,8 @@
 REACT_APP_VERSION=$npm_package_version
 REACT_APP_NAME=$npm_package_name
 
+REACT_APP_GUEST_CONSENT_STEP_ENABLED=true
+
 REACT_APP_SUPPORT_MAIL=test@example.org
 REACT_APP_INST=uio
 REACT_APP_SUPPORT_URL=https://example.org
diff --git a/frontend/src/appConfig.ts b/frontend/src/appConfig.ts
index 4af122b8..2758100a 100644
--- a/frontend/src/appConfig.ts
+++ b/frontend/src/appConfig.ts
@@ -23,6 +23,10 @@ export const appInst: string = env.REACT_APP_INST as string
 export const appStagingWarning: boolean =
   env.REACT_APP_STAGING_WARNING === 'true'
 
+/* Is there a consent step during guest registration? */
+export const guestConsentStepEnabled: boolean =
+  env.REACT_APP_GUEST_CONSENT_STEP_ENABLED === 'true'
+
 /* Footer content */
 export const appTechnicalSupportLink: string =
   env.REACT_APP_SUPPORT_URL as string
diff --git a/frontend/src/routes/guest/register/index.tsx b/frontend/src/routes/guest/register/index.tsx
index 02194ba0..c595bac7 100644
--- a/frontend/src/routes/guest/register/index.tsx
+++ b/frontend/src/routes/guest/register/index.tsx
@@ -1,9 +1,5 @@
 import React, { Suspense, useEffect, useRef, useState } from 'react'
 import { useTranslation } from 'react-i18next'
-
-import { Box, Button, CircularProgress } from '@mui/material'
-import Page from 'components/page'
-
 import { useHistory } from 'react-router-dom'
 import {
   CountryCode,
@@ -12,7 +8,11 @@ import {
 } from 'libphonenumber-js'
 import format from 'date-fns/format'
 import parse from 'date-fns/parse'
+import { Box, Button, CircularProgress } from '@mui/material'
+
 import { splitPhoneNumber, submitJsonOpts, fetchJsonOpts } from 'utils'
+import { guestConsentStepEnabled } from 'appConfig'
+import Page from 'components/page'
 import OverviewGuestButton from '../../components/overviewGuestButton'
 import { GuestRegisterCallableMethods } from './guestRegisterCallableMethods'
 import { GuestRegisterData, GuestConsentData } from './enteredGuestData'
@@ -84,6 +84,7 @@ export default function GuestRegister() {
     useState<GuestInviteInformation | null>(null)
   const [guestRegisterData, setGuestRegisterData] =
     useState<GuestRegisterData | null>(null)
+  // eslint-disable-next-line @typescript-eslint/no-unused-vars
   const [guestConsentData, setGuestConsentData] =
     useState<GuestConsentData | null>(null)
 
@@ -236,16 +237,22 @@ export default function GuestRegister() {
     return payload
   }
 
-  const submitPayload = () => {
-    if (!guestRegisterData) {
+  const submitPayload = (
+    registerData: GuestRegisterData,
+    consentData: GuestConsentData | null
+  ) => {
+    if (!registerData) {
       setActiveStep(Step.RegisterStep)
       return
     }
-    if (!guestConsentData) {
+    if (guestConsentStepEnabled && !consentData) {
       setActiveStep(Step.ConsentStep)
       return
     }
-    const payload: any = makePayload(guestRegisterData, guestConsentData)
+    const consents = guestConsentStepEnabled
+      ? consentData ?? { consents: [] }
+      : { consents: [] }
+    const payload: any = makePayload(registerData, consents)
     console.log('submitting payload', payload)
     fetch('/api/ui/v1/invited/', submitJsonOpts('POST', payload))
       .then((response) => {
@@ -266,21 +273,25 @@ export default function GuestRegister() {
   const handleForwardFromRegister = (registerData: GuestRegisterData): void => {
     console.log('handleForwardFromRegister')
     setGuestRegisterData(registerData)
+    if (!guestConsentStepEnabled) {
+      submitPayload(registerData, null)
+      return
+    }
     setActiveStep(Step.ConsentStep)
   }
 
   const handleForwardFromConsent = (consentData: GuestConsentData): void => {
-    console.log('handleForwardFromConsent')
+    console.log('handleForwardFromConsent consentData is', consentData)
     setGuestConsentData(consentData)
     if (!guestRegisterData) {
       setActiveStep(Step.RegisterStep)
       return
     }
-    if (!guestConsentData) {
+    if (!consentData) {
       setActiveStep(Step.ConsentStep)
       return
     }
-    submitPayload()
+    submitPayload(guestRegisterData, consentData)
   }
 
   const handleCancel = () => {
-- 
GitLab