Skip to content

breakpoint in listeners #162

@lucaleoni73

Description

@lucaleoni73

I'm using kea-forms to handle a form. In particular within this form I have a quantity picker, with buttons to increase and decrease the quantity. In the listeners section I added some instructions to perform after the actions.setFormValue is called. In particular, these are api call, so I do not want to make multiple calls that could provoke side-effects. On the documentation I read about breakpoint and the possibility to use the async breakpoint with a specific amount of ms.

I tried to do as you are suggesting, but it does not seem to work as it should.

Here a piece of my code:

listeners(({ actions, values }) => ({
        setFormValue: async (payload, breakpoint) => {
            if (
                payload.name == 'participants' &&
                payload.value.find((p: INewBookingParticipant) => p.quantity > 0)
            ) {
                await breakpoint(1000)
                // Check if the quantities are compliant
                const allocation_compliance = validateAllocations(payload.value, values.availableRules)
                if (!allocation_compliance) {
                    actions.allocationsInvalid()
                } else {
                    actions.allocationsValid()
                }
                const filter: IGetMonthlyAvailabilityParams = {
                    date_from: dayjs().format('YYYY-MM-DD'),
                    date_to: dayjs().add(1, 'month').format('YYYY-MM-DD'),
                    skip_notice_period: true,
                    allocations: values.addBookingForm.participants
                        .filter((p) => p.quantity > 0)
                        .map((p) => ({
                            type_id: p.id,
                            name: p.name!,
                            quantity: p.quantity,
                        })),
                }
                try {
                    const response: IGetMonthlyAvailabilityResponse = await api.new_booking.getMonthlyAvailability(
                        values.addBookingForm.variant,
                        filter
                    )
                    actions.setAvailableDays(response.available_days)
                } catch (e: any) {
                    debugLog('[addBookingFormLogic][listeners][setAddBookingForm][getMonthlyAvailability]Error:', e)
                }
            }
            },
    })),

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions