Newer
Older
import GrdfConnectModal from 'components/Connection/PartnerConnectModal/GrdfConnectModal'
import { useClient } from 'cozy-client'
import { FluidType, UsageEventType } from 'enums'
import { FluidConnection, Konnector, Trigger } from 'models'
import React, { useCallback, useState } from 'react'
import AccountService from 'services/account.service'
import TriggerService from 'services/triggers.service'
import UsageEventService from 'services/usageEvent.service'
import { updateFluidConnection } from 'store/global/global.slice'
import { useAppDispatch, useAppSelector } from 'store/hooks'
import { openConnectionModal } from 'store/modal/modal.slice'
import '../connection.scss'
import GrdfBill from './GrdfBill'
import GrdfForm from './GrdfForm'
const GrdfInit = ({ onSuccess }: { onSuccess: () => Promise<void> }) => {
const {
modal: { isConnectionModalOpen },
global: { fluidStatus },
} = useAppSelector(state => state.ecolyo)
const [showForm, setShowForm] = useState(false)
const currentFluidStatus = fluidStatus[FluidType.GAS]
const konnectorSlug: string =
currentFluidStatus.connection.konnectorConfig.slug
const siteLink: string =
currentFluidStatus.connection.konnectorConfig.siteLink
const konnector: Konnector | null = currentFluidStatus.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 = {
account: null,
trigger: null,
}
dispatch(
)
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 = {
account: account,
trigger: trigger,
}
dispatch(
)
onSuccess()
}
}
},
[
client,
konnector,
dispatch,
currentFluidStatus.fluidType,
currentFluidStatus.connection,
onSuccess,
konnectorSlug,
]
)
const goToPartnerSite = useCallback(() => {
{!showForm ? <GrdfBill /> : <GrdfForm />}
open={isConnectionModalOpen}
handleCloseClick={() => dispatch(openConnectionModal(false))}
setShowForm={setShowForm}
goToPartnerSite={goToPartnerSite}
handleSuccess={handleSuccess}