Fireship · @fireship

Your Form was Autosaved with Vue & Firestore

Uploaded April 10, 2020 · Length 11:19

Your Form was Autosaved with Vue & Firestore

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.

  1. 0:01

    Once upon a time, I was filling out a big long HTML form.

  2. 0:04

    It didn't have an option to save my progress, and at the same time, I wasn't ready to

  3. 0:08

    submit it.

  4. 0:09

    So I leave the window open and expect to come back to it later, but because I have a million

  5. 0:12

    browser tabs open, I accidentally close it at some point in the future.

  6. 0:15

    And it's gone!

  7. 0:17

    In today's video, you'll learn how to avoid a bad user experience like this by creating

  8. 0:21

    autosaving forms with Vue and Firestore.

  9. 0:24

    On the surface, this might look like a pretty advanced feature, but you might be surprised

  10. 0:27

    at how easy it is to implement when you combine these two awesome tools.

  11. 0:31

    Let's take a closer look at the feature we're building today.

  12. 0:33

    When a user first lands on this component, it fetches their data from the backend Firestore

  13. 0:37

    database and populates the form fields.

  14. 0:39

    When the user starts typing, the form state becomes modified.

  15. 0:42

    We debounce the event, and when the user stops typing for a few seconds, it runs an update

  16. 0:46

    to Firestore.

  17. 0:47

    And now our frontend form state is synced with our backend database.

  18. 0:51

    What we have here is a real-time form that autosaves any changes similar to something

  19. 0:54

    like Google Docs.

  20. 0:56

    And we'll throw in a couple of other little features as well, like being able to revert

  21. 0:59

    to the original form state, and we'll have an error state to handle any errors if the

  22. 1:03

    form can't be synced for whatever reason.

  23. 1:05

    Before we get started, I want to give a shoutout to Johan Walhout.

  24. 1:08

    He's the mastermind behind the code in this video, which you can find on GitHub or in

  25. 1:12

    his full article on Fireship.io.

  26. 1:14

    Reach out to him if you're looking for a solid web or mobile app developer.

  27. 1:18

    To get started with this project, we'll first need to create a Vue app.

  28. 1:21

    We can easily take care of that with the Vue CLI.

  29. 1:24

    Install it globally on your system with npm, and that will give you access to the view command.

  30. 1:28

    You can run the view UI command to bring up the visual editor.

  31. 1:31

    Go ahead and stick with the default settings, create your project, and then open it in an

  32. 1:35

    editor like VS Code.

  33. 1:36

    From there, we'll want to install our dependencies in this project.

  34. 1:39

    We have Firebase, ViewFire, and then a library called debounce, which I'll explain in more

  35. 1:43

    detail later.

  36. 1:44

    We can install those with npm, and you'll also notice that I've removed all the boilerplate

  37. 1:48

    code from the app.view component.

  38. 1:51

    The next step is to initialize Firebase, and to do that we need a Firebase project.

  39. 1:55

    If you're brand new to Firebase, you can create a Firebase account, and this project

  40. 1:58

    can be done entirely on the free tier.