studio1902 / statamic-peak

Statamic Peak is an opinionated starter kit for all your Statamic sites.
GNU General Public License v3.0
588 stars 100 forks source link

[PR pre-approval] Default success hook on forms #393

Closed andjsch closed 3 months ago

andjsch commented 3 months ago

Is your feature request related to a problem? Please describe. A customer requested for forms to not reappear after a successful submission. Easy. Publish the form handler and remove the setTimeout-part. Done.

Describe the solution you'd like But what if we thought of the setTimeout() as the "default success hook"? We could put the setTimeout() into the successHook() method, which would still allow for additions, but also allow for it to be skipped by simply adding a return.

How it currently works:

submit() {
    this.submitted = true
    this.form.submit()
        .then(response => {
            this.form.reset()
            this.$refs.form.reset()
            this.success = true
            this.submitted = false
            this.successHook(response)
            setTimeout(() => {
                this.success = false
             }, 4500)
        })
        …
        })
}
successHook(response) {
    {{ success_hook }}
}

How it could work:

submit() {
    this.submitted = true
    this.form.submit()
        .then(response => {
            this.form.reset()
            this.$refs.form.reset()
            this.success = true
            this.submitted = false
            this.successHook(response)
        })
        …
        })
}
successHook(response) {
    {{ success_hook }}

    setTimeout(() => {
        this.success = false
    }, 4500)
}
{{# Call in the Peak Tools Alpine Based Precognition Form Handler. #}}
{{ partial:statamic-peak-tools::snippets/form_handler success_hook="return" }}

Describe alternatives you've considered The {{ success_hook }} could probably be placed within the then() method, but the current state is cleaner.

robdekort commented 3 months ago

So you have x-show="!success" on your form to make it disappear? From a UX and a11y POV I don't think it's a good idea to hide the form. I don't have anything right now to back this up, but it feels wrong.

andjsch commented 3 months ago

Correct. I only focus on the success message. But thanks for making me aware, I am checking this with an accessibility expert.

As mentioned, it's not a bummer to not make this change. One could always publish the form_handler. I am coming from this very specific use-case. Anyhow, but what if one wants to change the timeout to let`s say 7 seconds instead of the 4.5? You'd still need to publish the form_handler which feels overkill to me.

robdekort commented 3 months ago

Gotcha. I'm not opposed to it.