Created
November 8, 2022 16:47
-
-
Save waldothedeveloper/8c74e31d574cdd57ef53d28b40b22b79 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { scheduleSteps } from '@/utils/scheduleSteps' | |
import { useReducer } from 'react' | |
const createSchedule = () => scheduleSteps | |
const reducer = (state, action) => { | |
const { type, data } = action | |
switch (type) { | |
case 1: { | |
return { | |
...state, | |
typeOfCleaning: { | |
...state.typeOfCleaning, | |
status: 'complete', | |
typesOfServices: data, | |
}, | |
address: { | |
...state.address, | |
status: 'current', | |
}, | |
} | |
} | |
case 2: | |
return { | |
...state, | |
address: { | |
...state.address, | |
status: 'complete', | |
verifiedAddress: data?.customerAddress[0]?.description, | |
propertyDetails: data?.propertyDetails, | |
}, | |
date: { ...state.date, status: 'current' }, | |
} | |
case 3: { | |
const petQuantity = data?.petQuantity.filter((pet) => pet.checked) | |
return { | |
...state, | |
date: { | |
...state.date, | |
status: 'complete', | |
serviceFrecuency: data?.selectedService, | |
verifiedDateAndTime: data?.timeAndDateOfBooking, | |
extras: data?.extrasSelected.filter((elem) => elem.checked), | |
pets: { | |
quantity: petQuantity.length > 0 ? petQuantity[0].quantity : 0, | |
price: | |
petQuantity.length > 0 | |
? petQuantity[0].quantity >= 3 | |
? 50 | |
: 25 | |
: 0, | |
}, | |
notes: data?.notes?.length > 0 ? data?.notes : ``, | |
}, | |
} | |
} | |
case 'reset': | |
return createSchedule() | |
default: | |
break | |
} | |
throw Error(`Unknown action: ${type}`) | |
} | |
export const useStepper = () => { | |
const [context, dispatch] = useReducer(reducer, null, createSchedule) | |
console.log('context: ', context) | |
return { context, dispatch } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment