Skip to content
Snippets Groups Projects
Commit a9c95cbe authored by Bastien DUMONT's avatar Bastien DUMONT :angel:
Browse files

remove state from store

parent 9e72a692
No related branches found
No related tags found
1 merge request!1253feat(forms): re-use user values
...@@ -9,10 +9,7 @@ import { FluidType, SgeStep } from 'enums' ...@@ -9,10 +9,7 @@ import { FluidType, SgeStep } from 'enums'
import { SgeStore } from 'models' import { SgeStore } from 'models'
import React, { useCallback, useEffect, useRef, useState } from 'react' import React, { useCallback, useEffect, useRef, useState } from 'react'
import { useNavigate } from 'react-router-dom' import { useNavigate } from 'react-router-dom'
import { import { setShouldRefreshConsent } from 'store/global/global.slice'
setShouldRefreshConsent,
updateSgeStore,
} from 'store/global/global.slice'
import { useAppDispatch, useAppSelector } from 'store/hooks' import { useAppDispatch, useAppSelector } from 'store/hooks'
import '../connection.scss' import '../connection.scss'
import { useFormData, type FormData } from '../useForm' import { useFormData, type FormData } from '../useForm'
...@@ -50,13 +47,9 @@ const SgeConnectView = () => { ...@@ -50,13 +47,9 @@ const SgeConnectView = () => {
const client = useClient() const client = useClient()
const navigate = useNavigate() const navigate = useNavigate()
const dispatch = useAppDispatch() const dispatch = useAppDispatch()
// const { sgeConnect } = useAppSelector(state => state.ecolyo.global)
const { formData } = useFormData() const { formData } = useFormData()
const [isLoading, setIsLoading] = useState(false) const [isLoading, setIsLoading] = useState(false)
console.log('🚀 ~ SgeConnectView ~ formData:', formData?.lastName) const [sgeState, setSgeState] = useState<SgeStore>(createInitialState())
const [sgeState, setCurrentSgeState] = useState<SgeStore>(
createInitialState()
)
const [currentStep, setCurrentStep] = useState<SgeStep>( const [currentStep, setCurrentStep] = useState<SgeStep>(
SgeStep.IdentityAndPDL SgeStep.IdentityAndPDL
) )
...@@ -73,19 +66,22 @@ const SgeConnectView = () => { ...@@ -73,19 +66,22 @@ const SgeConnectView = () => {
sgeAuthData: sgeState, sgeAuthData: sgeState,
}) })
useEffect(() => { useEffect(
if (formData) { function applyFormData() {
setCurrentSgeState(prevState => ({ if (formData) {
...prevState, setSgeState(prevState => ({
...createInitialState(formData), ...prevState,
})) ...createInitialState(formData),
} }))
}, [formData]) }
},
[formData]
)
useEffect(() => { useEffect(() => {
async function launchConnect() { async function launchConnect() {
if (sgeState.shouldLaunchAccount) { if (sgeState.shouldLaunchAccount) {
dispatch(updateSgeStore({ ...sgeState, shouldLaunchAccount: false })) setSgeState({ ...sgeState, shouldLaunchAccount: false })
dispatch(setShouldRefreshConsent(false)) dispatch(setShouldRefreshConsent(false))
if (!account) { if (!account) {
await connect() await connect()
...@@ -134,15 +130,15 @@ const SgeConnectView = () => { ...@@ -134,15 +130,15 @@ const SgeConnectView = () => {
const handleNext = useCallback(() => { const handleNext = useCallback(() => {
if (currentStep < SgeStep.Consent) { if (currentStep < SgeStep.Consent) {
setCurrentStep(prev => prev + 1) setCurrentStep(prev => prev + 1)
dispatch(updateSgeStore(sgeState))
// handle write to form doctype
// TODO only at final step
console.log(sgeState)
client.save({ client.save({
...formData, ...formData,
firstName: sgeState.firstName, firstName: sgeState.firstName,
lastName: sgeState.lastName, lastName: sgeState.lastName,
pdl: sgeState.pdl, pdl: sgeState.pdl,
address: sgeState.address,
city: sgeState.city,
zipCode: sgeState.zipCode,
}) })
} }
if (currentStep === SgeStep.Consent && !isLoading) { if (currentStep === SgeStep.Consent && !isLoading) {
...@@ -152,19 +148,17 @@ const SgeConnectView = () => { ...@@ -152,19 +148,17 @@ const SgeConnectView = () => {
city: sgeState.city.trim(), city: sgeState.city.trim(),
shouldLaunchAccount: true, shouldLaunchAccount: true,
} }
setCurrentSgeState(updatedState) setSgeState(updatedState)
dispatch(updateSgeStore(updatedState))
} }
focusMainContent() focusMainContent()
}, [currentStep, isLoading, dispatch, sgeState, client, formData]) }, [currentStep, isLoading, sgeState, client, formData])
const handlePrev = useCallback(() => { const handlePrev = useCallback(() => {
if (currentStep !== SgeStep.IdentityAndPDL) { if (currentStep !== SgeStep.IdentityAndPDL) {
setCurrentStep(prev => prev - 1) setCurrentStep(prev => prev - 1)
} }
dispatch(updateSgeStore(sgeState))
focusMainContent() focusMainContent()
}, [sgeState, currentStep, dispatch]) }, [currentStep])
const onChange = useCallback( const onChange = useCallback(
( (
...@@ -178,7 +172,7 @@ const SgeConnectView = () => { ...@@ -178,7 +172,7 @@ const SgeConnectView = () => {
...sgeState, ...sgeState,
[key]: value, [key]: value,
} }
setCurrentSgeState(updatedState) setSgeState(updatedState)
}, },
[sgeState] [sgeState]
) )
......
...@@ -3,7 +3,6 @@ import { TermsStatus } from 'models' ...@@ -3,7 +3,6 @@ import { TermsStatus } from 'models'
import { FluidStatus } from './fluid.model' import { FluidStatus } from './fluid.model'
import { PartnersInfo } from './partnersInfo.model' import { PartnersInfo } from './partnersInfo.model'
import { ReleaseNotes } from './releaseNotes.model' import { ReleaseNotes } from './releaseNotes.model'
import { SgeStore } from './sgeStore.model'
export interface GlobalState { export interface GlobalState {
screenType: ScreenType screenType: ScreenType
...@@ -16,7 +15,6 @@ export interface GlobalState { ...@@ -16,7 +15,6 @@ export interface GlobalState {
fluidStatus: FluidStatus[] fluidStatus: FluidStatus[]
fluidTypes: FluidType[] fluidTypes: FluidType[]
shouldRefreshConsent: boolean shouldRefreshConsent: boolean
sgeConnect: SgeStore
partnersInfo: PartnersInfo partnersInfo: PartnersInfo
ecogestureFilter: Usage ecogestureFilter: Usage
lastEpglLogin: string lastEpglLogin: string
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
import { FluidSlugType, FluidState, FluidType, ScreenType, Usage } from 'enums' import { FluidSlugType, FluidState, FluidType, ScreenType, Usage } from 'enums'
import { DateTime } from 'luxon' import { DateTime } from 'luxon'
import { FluidStatus, PartnersInfo, TermsStatus } from 'models' import { FluidStatus, PartnersInfo, TermsStatus } from 'models'
import { SgeStore } from 'models/sgeStore.model'
import { accountsData } from 'tests/__mocks__/accountsData.mock' import { accountsData } from 'tests/__mocks__/accountsData.mock'
import { konnectorsData } from 'tests/__mocks__/konnectorsData.mock' import { konnectorsData } from 'tests/__mocks__/konnectorsData.mock'
import { mockGlobalState } from 'tests/__mocks__/store' import { mockGlobalState } from 'tests/__mocks__/store'
...@@ -22,7 +21,6 @@ import { ...@@ -22,7 +21,6 @@ import {
toggleChallengeExplorationNotification, toggleChallengeExplorationNotification,
updateEcogestureFilter, updateEcogestureFilter,
updateFluidConnection, updateFluidConnection,
updateSgeStore,
updateTermsStatus, updateTermsStatus,
} from './global.slice' } from './global.slice'
...@@ -232,28 +230,6 @@ describe('globalSlice', () => { ...@@ -232,28 +230,6 @@ describe('globalSlice', () => {
shouldRefreshConsent: true, shouldRefreshConsent: true,
}) })
}) })
it('should handle setSgeConnect', () => {
const expectedSgeConnect: SgeStore = {
address: 'address',
city: 'city',
currentStep: 1,
dataConsent: true,
firstName: 'firstName',
lastName: 'lastName',
pdl: 12345678901234,
pdlConfirm: true,
shouldLaunchAccount: true,
zipCode: 99999,
}
const state = globalSlice.reducer(
mockGlobalState,
updateSgeStore(expectedSgeConnect)
)
expect(state).toEqual({
...mockGlobalState,
sgeConnect: expectedSgeConnect,
})
})
it('should handle updateFluidConnection', () => { it('should handle updateFluidConnection', () => {
const state = globalSlice.reducer( const state = globalSlice.reducer(
mockGlobalState, mockGlobalState,
......
...@@ -7,7 +7,6 @@ import { ...@@ -7,7 +7,6 @@ import {
GlobalState, GlobalState,
Notes, Notes,
PartnersInfo, PartnersInfo,
SgeStore,
TermsStatus, TermsStatus,
} from 'models' } from 'models'
...@@ -107,18 +106,6 @@ const initialState: GlobalState = { ...@@ -107,18 +106,6 @@ const initialState: GlobalState = {
notification_activated: false, notification_activated: false,
}, },
shouldRefreshConsent: false, shouldRefreshConsent: false,
sgeConnect: {
currentStep: 0,
firstName: '',
lastName: '',
pdl: null,
address: '',
zipCode: null,
city: '',
dataConsent: false,
pdlConfirm: false,
shouldLaunchAccount: false,
},
ecogestureFilter: Usage.ALL, ecogestureFilter: Usage.ALL,
lastEpglLogin: '', lastEpglLogin: '',
} }
...@@ -208,9 +195,6 @@ export const globalSlice = createSlice({ ...@@ -208,9 +195,6 @@ export const globalSlice = createSlice({
setLastEpglLogin: (state, action: PayloadAction<string>) => { setLastEpglLogin: (state, action: PayloadAction<string>) => {
state.lastEpglLogin = action.payload state.lastEpglLogin = action.payload
}, },
updateSgeStore: (state, action: PayloadAction<SgeStore>) => {
state.sgeConnect = action.payload
},
updateEcogestureFilter: (state, action: PayloadAction<Usage>) => { updateEcogestureFilter: (state, action: PayloadAction<Usage>) => {
state.ecogestureFilter = action.payload state.ecogestureFilter = action.payload
}, },
...@@ -231,6 +215,5 @@ export const { ...@@ -231,6 +215,5 @@ export const {
toggleChallengeExplorationNotification, toggleChallengeExplorationNotification,
updateEcogestureFilter, updateEcogestureFilter,
updateFluidConnection, updateFluidConnection,
updateSgeStore,
updateTermsStatus, updateTermsStatus,
} = globalSlice.actions } = globalSlice.actions
...@@ -94,17 +94,5 @@ export const mockGlobalState: GlobalState = { ...@@ -94,17 +94,5 @@ export const mockGlobalState: GlobalState = {
], ],
fluidTypes: [], fluidTypes: [],
shouldRefreshConsent: false, shouldRefreshConsent: false,
sgeConnect: {
address: '',
city: '',
currentStep: 0,
dataConsent: false,
firstName: '',
lastName: '',
pdl: null,
pdlConfirm: false,
zipCode: null,
shouldLaunchAccount: false,
},
ecogestureFilter: Usage.ALL, ecogestureFilter: Usage.ALL,
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment