import React , { FunctionComponent , useCallback , useEffect , useMemo , } from "react" ; import { graphql } from "react-relay" ; import { getViewURL } from "coral-auth/helpers" ; import { SetViewMutation } from "coral-auth/mutations" ; import { redirectOAuth2 } from "coral-framework/helpers" ; import { useCoralContext } from "coral-framework/lib/bootstrap" ; import { useLocal , useMutation , withFragmentContainer , } from "coral-framework/lib/relay" ; import { SignInContainer_auth } from "coral-auth/__generated__/SignInContainer_auth.graphql" ; import { SignInContainerLocal } from "coral-auth/__generated__/SignInContainerLocal.graphql" ; import { ClearErrorMutation , withClearErrorMutation , } from "./ClearErrorMutation" ; import SignIn from "./SignIn" ; interface Props { auth : SignInContainer_auth ; clearError : ClearErrorMutation ; } function isEnabled ( integration ) { return integration . enabled && integration . targetFilter . stream ; } const SignInContainer = ( { auth , clearError } ) => { const setView = useMutation ( SetViewMutation ) ; const { window } = useCoralContext ( ) ; const [ { error } ] = useLocal < SignInContainerLocal > ( graphql `
    fragment SignInContainerLocal on Local {
      error
    }
  ` ) ; const goToSignUp = useCallback ( ( e ) => { setView ( { view : "SIGN_UP" , history : "push" } ) ; if ( e . preventDefault ) { e . preventDefault ( ) ; } } , [ setView ] ) ; const { integrations : { local , facebook , google , oidc } , } = auth ; useEffect ( ( ) => { return ( ) => { void clearError ( ) ; } ; } , [ clearError , auth ] ) ; const redirectURL = useMemo ( ( ) => { if ( error ) { return ; } if ( isEnabled ( local ) ) { return ; } const enabled = [ facebook , google , oidc ] . filter ( ( i ) => isEnabled ( i ) ) ; if ( enabled . length > 1 || enabled . length === 0 ) { return ; } const integration = enabled [ 0 ] ; if ( ! integration . redirectURL ) { return ; } return integration . redirectURL ; } , [ error , facebook , google , local , oidc ] ) ; useEffect ( ( ) => { if ( ! redirectURL ) { return ; } redirectOAuth2 ( window , redirectURL ) ; } , [ redirectURL , window ] ) ; if ( redirectURL ) { return null ; } return ( < SignIn error = { error } auth = { auth } onGotoSignUp = { goToSignUp } emailEnabled = { isEnabled ( local ) } facebookEnabled = { isEnabled ( facebook ) } googleEnabled = { isEnabled ( google ) } oidcEnabled = { isEnabled ( oidc ) } signUpHref = { getViewURL ( "SIGN_UP" , window ) } / > ) ; } ; const enhanced = withClearErrorMutation ( withFragmentContainer < Props > ( { auth : graphql `
      fragment SignInContainer_auth on Auth {
        ...SignInWithOIDCContainer_auth
        ...SignInWithGoogleContainer_auth
        ...SignInWithFacebookContainer_auth
        integrations {
          local {
            enabled
            targetFilter {
              stream
            }
          }
          facebook {
            enabled
            redirectURL
            targetFilter {
              stream
            }
          }
          google {
            enabled
            redirectURL
            targetFilter {
              stream
            }
          }
          oidc {
            enabled
            redirectURL
            targetFilter {
              stream
            }
          }
        }
      }
    ` , } ) ( SignInContainer ) ) ; export default enhanced ;