Fireship · @fireship

Draggable Sortable Firestore

Uploaded October 8, 2018 · Length 7:57

Draggable Sortable Firestore

Summary

Build a sortable list with drag-and-drop functionality using Firestore and Angular, persisting data in a back-end database.

  • angular firestore
  • drag and drop sortable list
  • firebase database
  • angular directives
  • shopify draggable library

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    [Music]

  2. 0:06

    one of the more challenging features to

  3. 0:08

    build on a web application is a

  4. 0:10

    drag-and-drop sortable list and it's

  5. 0:12

    even more challenging if you need to

  6. 0:13

    persist the data in a back-end database

  7. 0:15

    instead of trying to reinvent the wheel

  8. 0:17

    we're going to use a library called

  9. 0:19

    draggable that's maintained by shop live

  10. 0:21

    it's relatively lightweight and we can

  11. 0:23

    wrap it in angular to make drag and drop

  12. 0:24

    features much more enjoyable to code if

  13. 0:26

    you're new the channel like and

  14. 0:28

    subscribe and you can find the source

  15. 0:29

    code on angular firebase comm what we're

  16. 0:32

    building today is a sortable list of

  17. 0:33

    emojis the user can add or remove items

  18. 0:36

    from the list and they can drag items to

  19. 0:38

    change the order and have that order be

  20. 0:40

    saved in the database this is a pretty

  21. 0:42

    complex thing to do from scratch because

  22. 0:44

    if you notice when we drag an item it

  23. 0:45

    creates a mirror of the original element

  24. 0:47

    and toggles a whole bunch of CSS classes

  25. 0:49

    on the container and the individual

  26. 0:51

    items themselves draggable will make

  27. 0:53

    this feature surprisingly easy but also

  28. 0:56

    give us enough flexibility to build

  29. 0:57

    something unique in addition firestore

  30. 0:59

    recently released a couple of new helper

  31. 1:01

    methods that make it easy to add or

  32. 1:03

    remove elements from an array so we'll

  33. 1:05

    take a look at those as well but the

  34. 1:07

    first thing you need to get started is

  35. 1:08

    an angular app with angular fire

  36. 1:10

    installed from there we'll go to the

  37. 1:12

    command line and install draggable with

  38. 1:13

    NPM there are two main elements to this

  39. 1:17

    demo first we have a smart component

  40. 1:19

    which is our emoji component and it

  41. 1:21

    handles retrieving items from the

  42. 1:22

    database and can also add or remove new

  43. 1:24

    emojis to the list then we have a

  44. 1:26

    directive called sortable which I'm

  45. 1:28

    going to call our dumb component because

  46. 1:30

    a directive is essentially just a

  47. 1:31

    component that doesn't have a custom

  48. 1:33

    HTML template it can do pretty much

  49. 1:35

    everything a component can do but

  50. 1:37

    instead it attaches itself to a host

  51. 1:39

    element and extends the functionality of

  52. 1:41

    that element when working with sort of a

  53. 1:43

    list like this you'll usually attach

  54. 1:44

    this to an unordered list and then have

  55. 1:46

    your list items be sortable within it

  56. 1:49

    we'll start by building out the

  57. 1:50

    directive and you'll notice that we're

  58. 1:52

    importing a bunch of things here from

  59. 1:53

    angular core and this is also where

  60. 1:56

    we'll import sortable from Shopify

  61. 1:58

    draggable we're going to implement the