import { RouterState , withRouter } from "found" ; import React , { FunctionComponent , useEffect , useMemo , useRef } from "react" ; import { graphql } from "react-relay" ; import { SetRedirectPathMutation } from "coral-admin/mutations" ; import { useCoralContext } from "coral-framework/lib/bootstrap/CoralContext" ; import { globalErrorReporter } from "coral-framework/lib/errors/reporter" ; import { useLocal , useMutation , withFragmentContainer , } from "coral-framework/lib/relay" ; import { AccountCompletionContainer_auth } from "coral-admin/__generated__/AccountCompletionContainer_auth.graphql" ; import { AccountCompletionContainer_viewer } from "coral-admin/__generated__/AccountCompletionContainer_viewer.graphql" ; import { AccountCompletionContainerLocal } from "coral-admin/__generated__/AccountCompletionContainerLocal.graphql" ; import CompleteAccountMutation from "./CompleteAccountMutation" ; import SetAuthViewMutation , { View } from "./SetAuthViewMutation" ; interface Props extends RouterState { auth : AccountCompletionContainer_auth ; viewer : AccountCompletionContainer_viewer | null ; } const AccountCompletionContainer = ( { auth , viewer , children , router , } ) => { const completed = useRef < boolean > ( false ) ; const completeAccount = useMutation ( CompleteAccountMutation ) ; const setAuthView = useMutation ( SetAuthViewMutation ) ; const setRedirectPath = useMutation ( SetRedirectPathMutation ) ; const { window } = useCoralContext ( ) ; const [ { authView : currentView , accessToken , authDuplicateEmail : duplicateEmail , redirectPath , } , ] = useLocal < AccountCompletionContainerLocal > ( graphql `
    fragment AccountCompletionContainerLocal on Local {
      accessToken
      authView
      authDuplicateEmail
      redirectPath
    }
  ` ) ; const localProfileEnabled = auth . integrations . local . enabled && auth . integrations . local . targetFilter . admin ; const hasUsername = ! ! viewer ?. username ; const hasEmail = ! ! viewer ?. email ; const hasLocalProfile = ! ! viewer ?. profiles . some ( ( p ) => p . __typename === "LocalProfile" ) ; const view = useMemo ( ( ) => { if ( completed . current ) { return false ; } if ( ! viewer || ! accessToken ) { return currentView as View ; } if ( ! hasEmail ) { if ( ( duplicateEmail || viewer . duplicateEmail ) && currentView !== "ADD_EMAIL_ADDRESS" ) { return "LINK_ACCOUNT" ; } return "ADD_EMAIL_ADDRESS" ; } if ( ! hasUsername ) { return "CREATE_USERNAME" ; } if ( localProfileEnabled && ! hasLocalProfile ) { return "CREATE_PASSWORD" ; } async function finish ( ) { try { await completeAccount ( { accessToken : accessToken ! } ) ; await setRedirectPath ( { path : null } ) ; if ( redirectPath && ! redirectPath . startsWith ( "/admin" ) ) { window . location . href = redirectPath ; } else { const pathname = redirectPath || "/admin" ; if ( pathname . includes ( "?" ) ) { location . href = pathname ; } else { router . replace ( { pathname } ) ; } } } catch ( err ) { globalErrorReporter . report ( err ) ; } } void finish ( ) ; completed . current = true ; return false ; } , [ accessToken , completeAccount , currentView , duplicateEmail , hasEmail , hasLocalProfile , hasUsername , localProfileEnabled , redirectPath , router , setRedirectPath , viewer , window , ] ) ; useEffect ( ( ) => { if ( ! view || currentView === view ) { return ; } setAuthView ( { view } ) ; } , [ currentView , setAuthView , view ] ) ; if ( ! view || view !== currentView ) { return null ; } return < > { children } < />; } ; const enhanced = withFragmentContainer < Props > ( { auth : graphql `
    fragment AccountCompletionContainer_auth on Auth {
      integrations {
        local {
          enabled
          targetFilter {
            admin
            stream
          }
        }
      }
    }
  ` , viewer : graphql `
    fragment AccountCompletionContainer_viewer on User {
      username
      email
      duplicateEmail
      profiles {
        __typename
      }
    }
  ` , } ) ( withRouter ( AccountCompletionContainer ) ) ; export default enhanced ;