Newer
Older
import FormNavigation from 'components/CommonKit/FormNavigation/FormNavigation'
import FormProgress from 'components/CommonKit/FormProgress/FormProgress'
import Content from 'components/Content/Content'
import Header from 'components/Header/Header'
import useKonnectorAuth from 'components/Hooks/useKonnectorAuth'
import { useClient } from 'cozy-client'
import { FORM_DOCTYPE } from 'doctypes'
import { FluidType, SgeStep } from 'enums'
import { SgeStore } from 'models'
import React, { useCallback, useEffect, useRef, useState } from 'react'
import { useNavigate } from 'react-router-dom'
import { setShouldRefreshConsent } from 'store/global/global.slice'
import { useAppDispatch, useAppSelector } from 'store/hooks'
import { createInitialSgeState, useFormData } from '../useForm'
import StepAddress from './StepAddress'
import StepConsent from './StepConsent'
import StepIdentityAndPdl from './StepIdentityAndPdl'
export type SGEKeysForm =
| 'firstName'
| 'lastName'
| 'pdl'
| 'address'
| 'zipCode'
| 'city'
| 'dataConsent'
| 'pdlConfirm'
/**
* http://ecolyo.cozy.tools:8080/#/connect/electricity
*/
const [isLoading, setIsLoading] = useState(false)
const [sgeState, setSgeState] = useState<SgeStore>(createInitialSgeState())
const [currentStep, setCurrentStep] = useState<SgeStep>(
const { fluidStatus } = useAppSelector(state => state.ecolyo.global)
const currentFluidStatus = fluidStatus[FluidType.ELECTRICITY]
const account = currentFluidStatus.connection.account
const mainContentRef = useRef<HTMLDivElement>(null)
const focusMainContent = () => {
setTimeout(() => mainContentRef.current?.focus(), 0)
}
const [connect, update] = useKonnectorAuth(FluidType.ELECTRICITY, {
useEffect(
function applyFormData() {
if (formData) {
setSgeState(prevState => ({
...prevState,
...createInitialSgeState(formData),
}))
}
},
[formData]
)
useEffect(() => {
async function launchConnect() {
if (sgeState.shouldLaunchAccount) {
setSgeState({ ...sgeState, shouldLaunchAccount: false })
dispatch(setShouldRefreshConsent(false))
if (!account) {
await connect()
} else {
await update()
}
navigate('/consumption/electricity')
}
}
launchConnect()
}, [account, connect, sgeState, dispatch, navigate, update])
const isNextValid = useCallback(() => {
switch (currentStep) {
case SgeStep.IdentityAndPDL:
return (
sgeState.firstName !== '' &&
sgeState.lastName !== '' &&
sgeState.pdl !== null &&
sgeState.pdl.toString().length === 14
sgeState.address !== '' &&
sgeState.city !== '' &&
sgeState.zipCode !== null &&
sgeState.zipCode.toString().length === 5
return sgeState.dataConsent && sgeState.pdlConfirm
sgeState.address,
sgeState.city,
sgeState.dataConsent,
sgeState.firstName,
sgeState.lastName,
sgeState.pdl,
sgeState.pdlConfirm,
sgeState.zipCode,
currentStep,
])
const handleNext = useCallback(() => {
client.save({
...formData,
_type: FORM_DOCTYPE,
firstName: sgeState.firstName,
lastName: sgeState.lastName,
pdl: sgeState.pdl,
address: sgeState.address,
city: sgeState.city,
zipCode: sgeState.zipCode,
})
if (currentStep === SgeStep.Consent && !isLoading) {
setIsLoading(true)
...sgeState,
city: sgeState.city.trim(),
focusMainContent()
}, [currentStep, isLoading, client, formData, sgeState])
const handlePrev = useCallback(() => {
if (currentStep !== SgeStep.IdentityAndPDL) {
focusMainContent()
(
key: SGEKeysForm,
value: string | boolean | number,
maxLength?: number
) => {
if (maxLength && value.toString().length > maxLength) return
const updatedState = {
)
const renderStep = (step: SgeStep) => {
switch (step) {
case SgeStep.Address:
return <StepAddress sgeState={sgeState} onChange={onChange} />
return <StepConsent sgeState={sgeState} onChange={onChange} />
case SgeStep.IdentityAndPDL:
default:
return <StepIdentityAndPdl sgeState={sgeState} onChange={onChange} />
<CozyBar titleKey="common.title_sge_connect" displayBackArrow={true} />
desktopTitleKey="common.title_sge_connect"
<div ref={mainContentRef} className="connectView" tabIndex={-1}>
<FormProgress
currentStep={currentStep}
totalSteps={Object.values(SgeStep).length / 2}
/>
<FormNavigation
handlePrevious={handlePrev}
handleNext={handleNext}
isLoading={isLoading}
disablePrevButton={currentStep === SgeStep.IdentityAndPDL}
disableNextButton={!isNextValid() || isLoading}
isLastStep={currentStep === SgeStep.Consent}
/>
</div>
</Content>
</>
)
}
export default SgeConnectView