Skip to content
Snippets Groups Projects
ConnectionOAuth.tsx 4.01 KiB
Newer Older
  • Learn to ignore specific revisions
  • import React, { useCallback, useState } from 'react'
    import { useClient } from 'cozy-client'
    import { useDispatch } from 'react-redux'
    import './connectionOAuth.scss'
    import { Konnector, Trigger, FluidStatus, FluidConnection } from 'models'
    import AccountService from 'services/account.service'
    import TriggerService from 'services/triggers.service'
    import { updatedFluidConnection } from 'store/global/global.actions'
    import { UsageEventType } from 'enum/usageEvent.enum'
    import UsageEventService from 'services/usageEvent.service'
    import PartnerConnectionStepsModal from 'components/PartnerConnectionStepsModal/PartnerConnectionStepsModal'
    import ConnectionOAuthNoPartnerAccount from 'components/Connection/ConnectionOAuthNoPartnerAccount'
    import ConnectionOAuthWithPartnerAccount from 'components/Connection/ConnectionOAuthWithPartnerAccount'
    
    interface ConnectionOAuthProps {
      fluidStatus: FluidStatus
      onSuccess: Function
    }
    
    const ConnectionOAuth: React.FC<ConnectionOAuthProps> = ({
      fluidStatus,
      onSuccess,
    }: ConnectionOAuthProps) => {
      const client = useClient()
      const dispatch = useDispatch()
    
      const [
        openPartenerConnectionModal,
        setOpenPartenerConnectionModal,
      ] = useState<boolean>(false)
      const [
        hasSeenPartnerConnectionModal,
        setHasSeenPartnerConnectionModal,
      ] = useState<boolean>(false)
    
      const konnectorSlug: string = fluidStatus.connection.konnectorConfig.slug
      const siteLink: string = fluidStatus.connection.konnectorConfig.siteLink
      const konnector: Konnector | null = fluidStatus.connection.konnector
    
      const handleSuccess = useCallback(
        async (accountId: string) => {
          if (konnector) {
            const accountService = new AccountService(client)
            const account = await accountService.getAccount(accountId)
            if (!account) {
              const updatedConnection: FluidConnection = {
                ...fluidStatus.connection,
                account: null,
                trigger: null,
              }
              dispatch(
                updatedFluidConnection(fluidStatus.fluidType, updatedConnection)
              )
              await UsageEventService.addEvent(client, {
                type: UsageEventType.KONNECTOR_CONNECT_EVENT,
                target: konnectorSlug,
                result: 'error',
              })
            } else {
              const triggersServices = new TriggerService(client)
              const trigger: Trigger = await triggersServices.createTrigger(
                account,
                konnector
              )
              const updatedConnection: FluidConnection = {
                ...fluidStatus.connection,
                account: account,
                trigger: trigger,
              }
              dispatch(
                updatedFluidConnection(fluidStatus.fluidType, updatedConnection)
              )
              onSuccess()
            }
          }
        },
        [
          client,
          konnector,
          dispatch,
          fluidStatus.fluidType,
          fluidStatus.connection,
          onSuccess,
          konnectorSlug,
        ]
      )
    
      const togglePartnerConnectionModal = useCallback(() => {
    
        setOpenPartenerConnectionModal((prev: boolean) => !prev)
    
      }, [])
    
      const handleEndSteps = useCallback(() => {
        togglePartnerConnectionModal()
        window.open(siteLink, '_blank')
        setHasSeenPartnerConnectionModal(true)
      }, [siteLink, togglePartnerConnectionModal])
    
      return (
        <>
          {hasSeenPartnerConnectionModal ? (
            <ConnectionOAuthWithPartnerAccount
              konnectorSlug={konnectorSlug}
              konnector={konnector}
              handleSuccess={handleSuccess}
              togglePartnerConnectionModal={togglePartnerConnectionModal}
            />
          ) : (
            <ConnectionOAuthNoPartnerAccount
              konnectorSlug={konnectorSlug}
              konnector={konnector}
              handleSuccess={handleSuccess}
              togglePartnerConnectionModal={togglePartnerConnectionModal}
            />
          )}
          <PartnerConnectionStepsModal
            fluidType={fluidStatus.fluidType}
            open={openPartenerConnectionModal}
            handleCloseClick={togglePartnerConnectionModal}
            handleEndSteps={handleEndSteps}
          />
        </>
      )
    }
    
    export default ConnectionOAuth