Fireship · @fireship
Your Form was Autosaved with Vue & Firestore
Uploaded April 10, 2020 · Length 11:19
Summary
Learn how to create autosaving forms with Vue and Firestore, a real-time database that syncs with the frontend form state.
- vue autosave form
- firestore database
- real-time form sync
- vuefire plugin
- firebase sdk
- autosaving forms
- vue and firestore
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:01
Once upon a time, I was filling out a big long HTML form.
- 0:04
It didn't have an option to save my progress, and at the same time, I wasn't ready to
- 0:08
submit it.
- 0:09
So I leave the window open and expect to come back to it later, but because I have a million
- 0:12
browser tabs open, I accidentally close it at some point in the future.
- 0:15
And it's gone!
- 0:17
In today's video, you'll learn how to avoid a bad user experience like this by creating
- 0:21
autosaving forms with Vue and Firestore.
- 0:24
On the surface, this might look like a pretty advanced feature, but you might be surprised
- 0:27
at how easy it is to implement when you combine these two awesome tools.
- 0:31
Let's take a closer look at the feature we're building today.
- 0:33
When a user first lands on this component, it fetches their data from the backend Firestore
- 0:37
database and populates the form fields.
- 0:39
When the user starts typing, the form state becomes modified.
- 0:42
We debounce the event, and when the user stops typing for a few seconds, it runs an update
- 0:46
to Firestore.
- 0:47
And now our frontend form state is synced with our backend database.
- 0:51
What we have here is a real-time form that autosaves any changes similar to something
- 0:54
like Google Docs.
- 0:56
And we'll throw in a couple of other little features as well, like being able to revert
- 0:59
to the original form state, and we'll have an error state to handle any errors if the
- 1:03
form can't be synced for whatever reason.
- 1:05
Before we get started, I want to give a shoutout to Johan Walhout.
- 1:08
He's the mastermind behind the code in this video, which you can find on GitHub or in
- 1:12
his full article on Fireship.io.
- 1:14
Reach out to him if you're looking for a solid web or mobile app developer.
- 1:18
To get started with this project, we'll first need to create a Vue app.
- 1:21
We can easily take care of that with the Vue CLI.
- 1:24
Install it globally on your system with npm, and that will give you access to the view command.
- 1:28
You can run the view UI command to bring up the visual editor.
- 1:31
Go ahead and stick with the default settings, create your project, and then open it in an
- 1:35
editor like VS Code.
- 1:36
From there, we'll want to install our dependencies in this project.
- 1:39
We have Firebase, ViewFire, and then a library called debounce, which I'll explain in more
- 1:43
detail later.
- 1:44
We can install those with npm, and you'll also notice that I've removed all the boilerplate
- 1:48
code from the app.view component.
- 1:51
The next step is to initialize Firebase, and to do that we need a Firebase project.
- 1:55
If you're brand new to Firebase, you can create a Firebase account, and this project
- 1:58
can be done entirely on the free tier.


