Skip to content

Instantly share code, notes, and snippets.

@waldothedeveloper
Created November 8, 2022 16:47
Show Gist options
  • Select an option

  • Save waldothedeveloper/8c74e31d574cdd57ef53d28b40b22b79 to your computer and use it in GitHub Desktop.

Select an option

Save waldothedeveloper/8c74e31d574cdd57ef53d28b40b22b79 to your computer and use it in GitHub Desktop.

Revisions

  1. waldothedeveloper created this gist Nov 8, 2022.
    73 changes: 73 additions & 0 deletions useReducer.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    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 }
    }