Draggable Sortable Firestore

Fireship · Intermediate ·🔍 RAG & Vector Search ·7y ago

Key Takeaways

The video demonstrates how to build a drag-and-drop sortable list using Shopify's Draggable library and Firestore, leveraging Firestore's helper methods for array operations and utilizing Angular Fire for initialization.

Full Transcript

[Music] one of the more challenging features to build on a web application is a drag-and-drop sortable list and it's even more challenging if you need to persist the data in a back-end database instead of trying to reinvent the wheel we're going to use a library called draggable that's maintained by shop live it's relatively lightweight and we can wrap it in angular to make drag and drop features much more enjoyable to code if you're new the channel like and subscribe and you can find the source code on angular firebase comm what we're building today is a sortable list of emojis the user can add or remove items from the list and they can drag items to change the order and have that order be saved in the database this is a pretty complex thing to do from scratch because if you notice when we drag an item it creates a mirror of the original element and toggles a whole bunch of CSS classes on the container and the individual items themselves draggable will make this feature surprisingly easy but also give us enough flexibility to build something unique in addition firestore recently released a couple of new helper methods that make it easy to add or remove elements from an array so we'll take a look at those as well but the first thing you need to get started is an angular app with angular fire installed from there we'll go to the command line and install draggable with NPM there are two main elements to this demo first we have a smart component which is our emoji component and it handles retrieving items from the database and can also add or remove new emojis to the list then we have a directive called sortable which I'm going to call our dumb component because a directive is essentially just a component that doesn't have a custom HTML template it can do pretty much everything a component can do but instead it attaches itself to a host element and extends the functionality of that element when working with sort of a list like this you'll usually attach this to an unordered list and then have your list items be sortable within it we'll start by building out the directive and you'll notice that we're importing a bunch of things here from angular core and this is also where we'll import sortable from Shopify draggable we're going to implement the after view and net lifecycle hook on this directive just so we know that all of the children elements are available once we start running the sortable code on it then we're going to have it emit a custom event that has the new order of the sorted item in order to be able to do that we first need to pass in the initial items which we'll do with an input property on the directive then after the items have been sorted will emit our own custom event by using the output decorator and an event emitter there's quite a few different events you can track here so I've commented those out in case you need them the only one we need is when the actual sorting has been finished then we'll inject element ref in the constructor which will give us access to the Dom element which in this case should be an unordered list so once that's done we can initialize sortable by creating a new instance of the class and then passing it the element ref native element then it takes a second argument with some options we're going to specify the draggable elements as the list items within that list the draggable library is going to emit its own custom events which we can intercept here to emit our own custom event that will have the newly sorted list again there are multiple custom events that you might need but the one that we care about is sortable stop at that point we know the user is done sorting the list so we'll go ahead and take the initial array reorder it and then you mitt that out through our custom event emitter so the parent component can listen to it and handle it accordingly the sortable stop event keeps track of all the elements in the list and it will give us the new index and the old index of the item that was moved so we'll use this method as just a context for mutating the original data so what we can do here is take the original data as our starting point then we can splice off the old index and then Reese Pleiss it back into the new index once we have the array in the format that we want we can go ahead and omit it out as the value from our custom event in the next step we'll have the parent component listen to this event and then update the firestore database with the new value now that we have our directive finished we'll go into the HTML for the component we can just insert it into an unordered list and that will give us access to both its input properties as well as its output properties the data that we read from the database will be an observable so I'm doing an NG if statement here with the document and setting that document as a template variable then we'll pass that to the data input on the directive and then lastly we will listen to our custom stop event and then we'll write an event handler that will update the database when that fire now we can loop over the array of emojis and render a list item for each one then while working in HTML I want to add a couple of extra things first I'll add another button here that the user can click to remove an item from an array then I'll move outside of our loop and add an additional method that we can use to add an additional emoji to the array and these are mostly just so I can show you the new array methods and firestore now we'll go into our component type script and finish this feature out I am importing an angular fire store and then also fires store directly from the firebase app namespace we can inject angular fire store in the constructor and then we'll set up one variable for our document reference so we can make updates on it and then another one for the actual observable data then I'll go ahead and define those during ng on an it for now I'm just going to point to a dummy document that I have saved in the emojis collection then we can define our observable of that document by calling value changes on the reference here's what that document actually looks like in the database we have an array data structure here called faves and it currently has three different emojis in it when the user sorts this list we can update the array on fire store by simply making a reference to the document and calling update with the new array we set up the event emitter in the directive to send the newly sorted array so this is all the code we need to update the document on fire store but if you want to add a single item to the array one way to do that is with the new fire store or a union method which will ensure that a unique item is added to the existing array this is really nice when you want to enforce uniqueness in the array which is exactly what we want to do here so we'll call fire store field value array Union and then pass in the element that we want to add to it if that value already exists in the array then nothing will happen but otherwise it will append that item to the end of the array you can also remove items from the array which is also very useful because a lot of times you don't already know the index of the item and you just want it gone so fire store field value array remove will find that element in the array and remove it so that takes care of all of our typescript code but in order to make drag-and-drop look good you're going to have to spend quite a bit of time in your CSS styling one of the cool things about draggable is that it will add a whole bunch of different CSS classes to your elements depending on what state they're in within the draggable list for example we might want to add a style to the container element when it's being dragged like a blue border then the source element that's being dragged we might want to make that a lighter color and scale it down a little bit then once the items placed we'll go ahead and color the background to green and draggable will remove this class after a second so we can get that green flashing effect when the item is set and another cool thing draggable does is that it creates a mirror element of the item that was selected so you can use that to build adding more complex UI and to handle things like animations when we go to our demo you can see that a lot of different classes are being swapped out on the elements as we drag things around here and as you can imagine doing this kind of stuff reliably from scratch takes a lot of time and effort so using a library like draggable is just going to make your life a lot easier I'm gonna go ahead and wrap up the video there if it helped you please like and subscribe and if you want to take your development to the next level consider becoming a pro member at angular firebase comm to get access to all kinds of advanced exclusive content designed to help you build and ship your app faster thanks for watching and I'll talk to you soon

Original Description

Build a drag-n-drop list with Firestore using Shopify's draggable library. https://angularfirebase.com/lessons/sortable-drag-and-drop-lists-in-firestore/ - Draggable https://shopify.github.io/draggable/ - Firestore https://firebase.google.com/docs/firestore/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Fireship · Fireship · 0 of 60

← Previous Next →
1 Angular 4 Development and Production Environments with Firebase
Angular 4 Development and Production Environments with Firebase
Fireship
2 OAuth with Angular and Firebase Tutorial
OAuth with Angular and Firebase Tutorial
Fireship
3 Anonymous Authentication with Angular and Firebase - Lazy Registration
Anonymous Authentication with Angular and Firebase - Lazy Registration
Fireship
4 Angular Router Guards for Firebase Users
Angular Router Guards for Firebase Users
Fireship
5 Angular Firebase CRUD App with NoSQL Database Tutorial
Angular Firebase CRUD App with NoSQL Database Tutorial
Fireship
6 Upload Files from Angular to Firebase Storage
Upload Files from Angular to Firebase Storage
Fireship
7 How to Deploy an Angular App to Firebase Hosting
How to Deploy an Angular App to Firebase Hosting
Fireship
8 Sharing Data between Components in Angular
Sharing Data between Components in Angular
Fireship
9 Loading Spinners for Asynchronous Firebase Data
Loading Spinners for Asynchronous Firebase Data
Fireship
10 Angular 4 Transactional Email with Google Firebase Cloud Functions
Angular 4 Transactional Email with Google Firebase Cloud Functions
Fireship
11 Firebase Database Rules Tutorial
Firebase Database Rules Tutorial
Fireship
12 Autocomplete Search with Angular4 and Firebase
Autocomplete Search with Angular4 and Firebase
Fireship
13 Reddit Inspired Upvoting System with Angular and Firebase NoSQL
Reddit Inspired Upvoting System with Angular and Firebase NoSQL
Fireship
14 Angular Drag-and-Drop File Uploads to Firebase Storage
Angular Drag-and-Drop File Uploads to Firebase Storage
Fireship
15 Text Translation with Firebase Cloud Functions onWrite and Angular 4
Text Translation with Firebase Cloud Functions onWrite and Angular 4
Fireship
16 Custom Usernames with Firebase Authentication
Custom Usernames with Firebase Authentication
Fireship
17 Twitter-Inspired Follow Unfollow Feature with Firebase and Angular 4
Twitter-Inspired Follow Unfollow Feature with Firebase and Angular 4
Fireship
18 Simple Pagination with Firebase and Angular 4
Simple Pagination with Firebase and Angular 4
Fireship
19 How to Connect Firebase Users to their Data - 3 Methods
How to Connect Firebase Users to their Data - 3 Methods
Fireship
20 Add Toast Message Notifications to your Angular App
Add Toast Message Notifications to your Angular App
Fireship
21 Facebook-Inspired Reactions System with Angular and Firebase
Facebook-Inspired Reactions System with Angular and Firebase
Fireship
22 Learn NgModule in Angular with Examples
Learn NgModule in Angular with Examples
Fireship
23 Lazy Loading Components in Angular 4
Lazy Loading Components in Angular 4
Fireship
24 Stripe Checkout Payments with Angular and Firebase - Part 1
Stripe Checkout Payments with Angular and Firebase - Part 1
Fireship
25 Process Stripe Payments with Firebase Cloud Functions - Part 2
Process Stripe Payments with Firebase Cloud Functions - Part 2
Fireship
26 Selling Digital Content in Angular with Stripe Payments - Part 3
Selling Digital Content in Angular with Stripe Payments - Part 3
Fireship
27 Angular 4 Full Text Search with Algolia - Part 1
Angular 4 Full Text Search with Algolia - Part 1
Fireship
28 Algolia with Firebase Cloud Functions - Part 2
Algolia with Firebase Cloud Functions - Part 2
Fireship
29 Firebase Phone Authentication in Angular 4
Firebase Phone Authentication in Angular 4
Fireship
30 Top 7 RxJS Concepts for Angular Developers
Top 7 RxJS Concepts for Angular Developers
Fireship
31 Learn Angular Animations with 5 Examples
Learn Angular Animations with 5 Examples
Fireship
32 Advanced Firebase Data Filtering (Multi-Property)
Advanced Firebase Data Filtering (Multi-Property)
Fireship
33 Realtime Maps with Mapbox + Firebase + Angular
Realtime Maps with Mapbox + Firebase + Angular
Fireship
34 Angular Reactive Forms with Firebase Database Backend
Angular Reactive Forms with Firebase Database Backend
Fireship
35 Send Push Notifications in Angular with Firebase Cloud Messaging
Send Push Notifications in Angular with Firebase Cloud Messaging
Fireship
36 Top 7 Ways to Debug Angular 4 Apps
Top 7 Ways to Debug Angular 4 Apps
Fireship
37 Infinite Scroll with Angular and Firebase
Infinite Scroll with Angular and Firebase
Fireship
38 Use TypeScript with Firebase Cloud Functions
Use TypeScript with Firebase Cloud Functions
Fireship
39 Realtime Graphs and Charts with Plotly and Firebase
Realtime Graphs and Charts with Plotly and Firebase
Fireship
40 Role-Based User Permissions in Firebase
Role-Based User Permissions in Firebase
Fireship
41 User Presence System in Realtime - Online, Offline, Away
User Presence System in Realtime - Online, Offline, Away
Fireship
42 Location-based Queries with GeoFire and Angular Google Maps
Location-based Queries with GeoFire and Angular Google Maps
Fireship
43 Angular ngrx Redux Quick Start Tutorial
Angular ngrx Redux Quick Start Tutorial
Fireship
44 Angular Ngrx Effects with Firebase Database
Angular Ngrx Effects with Firebase Database
Fireship
45 Progressive Web Apps with Angular
Progressive Web Apps with Angular
Fireship
46 Angular Ngrx with Firebase Google OAuth User Authentication
Angular Ngrx with Firebase Google OAuth User Authentication
Fireship
47 RxJS Quick Start with Practical Examples
RxJS Quick Start with Practical Examples
Fireship
48 Send SMS Text Messages with Twilio and Firebase
Send SMS Text Messages with Twilio and Firebase
Fireship
49 Firebase Database Performance Profiling
Firebase Database Performance Profiling
Fireship
50 Native Desktop Apps with Angular and Electron
Native Desktop Apps with Angular and Electron
Fireship
51 Subscription Payments with Stripe, Angular, and Firebase
Subscription Payments with Stripe, Angular, and Firebase
Fireship
52 Firestore with AngularFire5 Quick Start Tutorial
Firestore with AngularFire5 Quick Start Tutorial
Fireship
53 Angular HTTP Client Quick Start Tutorial
Angular HTTP Client Quick Start Tutorial
Fireship
54 Google Sign-In with Firestore Custom User Data
Google Sign-In with Firestore Custom User Data
Fireship
55 Star Review System from Scratch with Firestore + Angular
Star Review System from Scratch with Firestore + Angular
Fireship
56 Angular Chatbot with Dialogflow (API.ai)
Angular Chatbot with Dialogflow (API.ai)
Fireship
57 Learn @ngrx/entity and Feature Modules
Learn @ngrx/entity and Feature Modules
Fireship
58 Infinite Scroll Pagination with Firestore
Infinite Scroll Pagination with Firestore
Fireship
59 Faster Firestore via Data Aggregation
Faster Firestore via Data Aggregation
Fireship
60 Contentful - CMS for Angular Progressive Web Apps
Contentful - CMS for Angular Progressive Web Apps
Fireship

This video teaches you how to build a drag-and-drop sortable list using Shopify's Draggable library and Firestore, and how to leverage Firestore's helper methods for array operations. By following this lesson, you'll learn how to implement RAG search functionality and utilize Angular Fire for initialization.

Key Takeaways
  1. Install Draggable with NPM
  2. Create a smart component for emoji retrieval and addition
  3. Develop a directive for sortable list functionality
  4. Initialize Sortable with elementRef and options
  5. Intercept custom events for newly sorted list
  6. Use sortable stop event to get new and old index of item moved
  7. Splice off old index and reinsert at new index
  8. Use Firestore field value array Union to add unique item to existing array
  9. Use Firestore field value array Remove to remove item from array
  10. Style drag-and-drop look with CSS
💡 Using a library like Draggable can make development easier and more efficient, especially when implementing complex features like drag-and-drop sortable lists.

Related Reads

📰
A Production RAG Pipeline for PDFs: Relational Parsing, TOC Retrieval, Typed Answers
Learn to build a production-ready RAG pipeline for PDFs, enabling efficient document parsing, question answering, and retrieval
Towards Data Science
📰
How to Cut RAG Token Costs 90% by Caching the Prefix
Cut RAG token costs by 90% using prefix caching, reducing the financial burden of large payloads
Medium · AI
📰
How to Cut RAG Token Costs 90% by Caching the Prefix
Cut RAG token costs by 90% by caching the prefix, reducing the payload size and saving on input prices
Medium · Programming
📰
Why Your Chunking Strategy Matters More Than Your Model
Optimizing your chunking strategy can be more crucial to your app's performance than the model you choose, learn why and how to improve it
Medium · RAG
Up next
This FREE Tool Turns ANY PDF into Perfect Markdown (MinerU Live Test)
Prompt Engineer
Watch →