Skip to content

Instantly share code, notes, and snippets.

@waldothedeveloper
Created November 8, 2022 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waldothedeveloper/8c74e31d574cdd57ef53d28b40b22b79 to your computer and use it in GitHub Desktop.
Save waldothedeveloper/8c74e31d574cdd57ef53d28b40b22b79 to your computer and use it in GitHub Desktop.
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