Syntax Video
Local First with Replicache - How to Build a Full Stack App with Data Syncing Part 1
Transcript
hey what's up this is Scott from syntax and in this local first from scratch video we're going to be talking about replicas now replicas is an extremely powerful library that handles patching and sinking data and the cool thing about replicas is is you can use it just offline and local or you can use it to sync to a server somewhere else and this process is difficult but it's really great because you get full control over how this sync happens what happens on the server you know a lot of these platforms that are trying to make local first easier want to own everything they want to own your database they want to own the client side but what replic has does is it allows you to work with the client side data and then when that sync happens on the server you get to choose how the server responds to those changes meaning that you can bring this
to an already existing project now it's not that simple we'll be talking quite a bit about that as we go and in this first video we're going to be showing you how to get replic set up and working now let's take a quick look at what we're going to be building this is a basic to-do list get food nice this is a basic to-do list you can see that as we add things they get added here if I refresh the page these are all still here and if we take a look at our application Tab and down here in the replic database you know in the last video what we were doing was storing our data in a structure that was really mimicking what we would want the external database structure to be as in the data was being saved into the database in the exact structure that we were using it and then we were just reading it from the database but replicas isn't that simple in fact if we look at the data inside of here inside of our chunks it is really
not that simple in fact we open this thing up we can see what look to be just individual messages about the status of our data and this allows the application to know what's changed when it changed how it changed and this gives you the strength to be able to version things in a way where oh if uh if the server is more up to date than the client then we pick the server if the client is more up to date than the server then we push up to the client so we are getting out of the phase of just working with our raw data and getting more into augmented data that carries more information as a patch message and in fact you can even see if we open up this one there has an array inside of this array is three items you can see that the key for this is a key for this to-do the ID is the ID that was created when the to-do was created and you can see that this is our hello Todo right here now because this
is such a difficult process we're going to be taking just this first video to get replic set up I'm going to be doing it in a spelt kit application you can use any framework library of your choice this code is agnostic to the framework now when we do hit spelt specific things just like in the last video I'll show you what you have to do to make it work in other places but it's largely the same concept overall and if you're familiar with your front end framework you're going to have no problems anyways so let's get started building this to-do app with replic where we can dive into into the world of a bit more advanced local first techniques so let's get started by installing spelt kit here again you can use any framework you can even toss it into a code base you already have we can do npm create spelt and we'll just call this not local checklist because I've already used that name we'll call this checklist rep for replic Cas right okay checklist rep we're going to select a skeleton project
it's Bare Bones we're going to say yes to typescript es lint prettier and we're going to try spelt 5 for this I like it uh and everything you're going to be doing in spelt is now going to be spelt 5 so let's just use it I will talk about while we're doing it just like the last video use whatever framework you'd like and just like that it's installed instantly so we can come in here and we can say uh checklist CD to checklist rep okay I'm going to pop open my uh code so I'm going to do code. SL to open this in vs code then I'm going to do npm install npm in install okay we have our things installed we can also do npm install hyphen hyen save in repash like this r e p l i c a c h repash okay now this is a package again that allows you to do all of the syncing and we'll talk about setting this up for success for the second part where we actually bring in a full database and
sync this offline to a server that actually will live in the same application that we're on so you won't need a third party server so we're going to do npm install save replic Cas so this is great now we can do npm runev okay and we'll have our app available at local host 5173 the classic Port the classic Port here and you can see we have our welcome this F kid all right I'm going to get rid of all of this stuff and we're going to get going because I don't want to spend too much time on the stuff that does doesn't matter so we're going to do every single thing that we need to do in this application Beyond maybe CSS and in fact I'll do the CSS at the end so that way if you don't care about the CSS you don't have to watch it but the last one felt a little ugly when we finished so I want to at least give you something to make this look a little nicer so page dots felt here now I'm going to create a script with a lang of TS this is just means that we're using typescript and
the first thing I want to do is I want to create a type of to-dos because this is going to kind of inform everything that we have going forward so we're gonna have a new type name to do it's GNA have an ID it's going to have a name which is going to be a string it's going to have a deleted which is going to be an interesting thing we'll have to talk about uh not blean Boolean thank you very much um we'll also have a created at and this is going to be the type of a string believe it or not and then an updated at and this is also going to be the type of a string string now the reason being is that when we store things in a replic Cas it's going to get stored as a string so I might as well just accept that this date is going to be a string here and I'm also going to have another value which is completed which is a Boolean so what's up with deleted why do we need to delete it well in replic hash land when you're deleting things it can all get a little tough so
if you're working at it at a Basics and you're just getting used to replicas an easier strategy is actually to have a flag that is deleted that hides the record from our database queries uh rather than actually deleting this thing now you could actually delete it there's there's tons of documentation on how to do that but if I want to show you one step at a time here using a deleted flag is much more simple so that's great next thing I want to do is create my state so we're going to say let to DOS is equal to and we're going to say dollar sign State and it's going to be an empty array um sorry I need an equals there and the type of this thing is going to be to-dos as an array or Tod do I believe so again an array of to-dos next thing we'll want is our form state so we're going to say let form State and this is going to be equal to a state with a name that is empty so in
react land this is just a used State and you get the set State methods and the state itself in spel you just get dollar sign State you can mutate the state directly or bind it again react folks you know how to work with state so nothing crazy here and in fact this is the most complex the state will'll get in this our application uh essentially because our replicas is going to be doing so much for us so now that we have our basic state let's go ahead and create our HTML we're going to have a form the method for the form method is going to be equal post and we're going to have an onsubmit on here this is going to call a function onsubmit so you can have an onsubmit event again in react you know how to do your on events uh in spelt you can just do bracket onsubmit like this and then create a function named un submit like so and then now you can call
this function onsubmit now one of the things we'll want to do first off the off the bat is do an e. prevent default because we don't want this form to actually reload the page and this this will be just type event we're not going to get need to get too specific with this just yet okay cool so we have our form we have our onsubmit let's go ahead and add first thing we need a label and this is going to be four name and this will just say to do like this um it could be name it could be anything else you would like it to be but then we're going to have a type of text for an input a name of name and an ID of name and we're going to bind the value of this input to form state. name now we're doing this because in spelt you can bind values in view you can also bind value so if you're a view or spelt
user use the bind if you're a react user you'll want to run your whole unchange thing that you have to do for inputs or H use the uh form input library of your choice okay so that's it let's go ahead and actually I'm going to bump up the font size just a little bit just make it nice and obvious here so you can all see and then we're going to have a button type of submit and then it's going to have an ad I like to put a little Plus in here just for flavor okay so we have this form next thing we'll want is our unordered list again we're going to start with the HTML here that way we don't have to worry about it when things get like really real so okay we're going to do an each to-dos and you could think about it just like this is its basic structure where each todos as to-do we're going to put each one inside of a list item and the to-do name will simply be like this now I'm going to do some things to prepare us for the future I'm going to put the uh to-do name inside of a span that's going to have to uh help with CSS what we want to do and the button I'm going to have a button and
this is just going to be a button like so and eventually this is going to have an SVG in it but for now let's go ahead and I I'll save that for the end in the CSS part but for now let's just go ahead and use an emoji check mark for now okay so we don't need to do too much more other than that with our HTML just to get this going we're going to be augmenting it as we go and honestly we don't have our list items anyway so that's a good start let's actually focus on the replicas stuff now specifically again this whole project is going to take place in you know about 200 lines of HTML CSS and JavaScript in one file and it's going to give us a fully working application now again it's not going to sync offline because that's considerably more complex but we'll get to that when we get to it so the first thing I want to do is create the name of
our storage and this is going to be equivalent to the name in the database now the first thing I want to do is create the name for our storage in fact if you look inside of our indexed DB here actually this application won't have any just yet but when we get going there the name is going to be what appears inside of the index DB here so let's go ahead and create the name we're going to say let name and name is going to be equal to one of two things we're going to check to see if we are in Dev mode and if we're in Dev mode this is going to say Dev colon todos and then usually in replic land you attach the user ID to this now the user ID would have to come in from the user logging in and then syncing we'll talk about all that later for now since this data is not moving off of our computer it's fine to just have a a hardcoded string here uh for production we'll want to say something like to-dos and then user ID
once this moves into production just so it doesn't have the Dev tag in front of it now this Dev that's underlined red this can actually be imported with app. environment from spel kit so in your application whatever it may be nextjs I have no idea what your meta framework will be it's typically as a way of determining if you in the development environment that could even just be a simple access to an environment variable but for us import Dev app environment so if we're in development land uh let's go ahead and name this Dev Tod do user ID otherwise user ID cool so that's nice and easy let's go ahead and create our replic Cas now we're going to hit some bumps along the way but we'll solve them so let's go ahead and say let rep is equal to new rep lash okay and I'm going to add import from replic like this again you can import it first by hand if you'd like
the import is import replicas from replicas as a named import here so that's it obviously this is going to be angry it does not like that it has got no argument so let's fix that we're going to give it an object as an argument because it has a number of options here now the first one is going to be name which we have just given it the next one is going to be the license key I'll talk more about the license key in just a second right now it's going to be a blank string next up is going to be mutators okay and mutators is going to be the methods in which we modify our data right now I'm going to leave this completely empty because this is where some of the meat of our application will live and now that we refresh this you'll see that things are broken what's broken about this now something very fun just happened here the application crashed
out with an index DB is not defined if you want to take a second to guess why index DB is not defined it is not obvious but it's something that you might have thought about so you can take that time maybe pause it if you want to think about it you should be able to figure out the answer it's nothing specific to replicas itself but it is specific to how we're storing local data so I'm going to create a new file here to solve this and this is page.
TS now to do that in spel kit we need to do export const SSR equals false and that's from page. TS simply doing that is going to make our page only render client side in fact if we head back to our application we run it in warp it should be loading up fine and the reason this is happening is because when we're working with client side data you need this stuff to run on the client but if this template is being is attempting to load server side then it's going to crash because index DB is not available on the server side nor that even if you had index DB you would have a separate database for the server and the client and that's not what we're doing here okay so now our application is running but we are getting a curious error and this goes back to the license thing it says replic license not valid that you need a license key and the way to get a license key is to do exactly what it says here which is npx replic get
license I'm going to pop this open in my terminal I'm just going to make a new tab and I'm going to say npx replic get license and what this is going to do you give it your name uh my company project is syntax uh tutorial local first okay and the contact email is just going to be my email do you want uh is is your project commercial as defined by the terms no it's not commercial do you agree to the uh terms of service yes I do and just like that I have a license key now I'm not going to show you my license key but I'll tell you what you have to do with it and really quick before we actually dive into adding that license key I do want to just share with you that replic Cache can be a paid product now I don't know what the future of this is but for the most part you're not going to have to worry too much about this unless your applic is one bringing in money and two
bringing in a decent amount of money because the only way that repli cash charges you is if your project has greater than 200,000 annual reoccurring revenue or it has more than $500,000 worth of funding so let's say you've received venture capital for a commercial project you'll have to pay for repli Cas but for those of you who are just trying it working on side projects working on things that aren't even making very much money your application's making you 1,000 bucks a month you don't have to pay for it so that's why we have to work with the license key now I understand open- Source software people not wanting to pay for things but I think the licensing approach here is a pretty good way to support the development of this project but also it's it's pretty generous I've never paid anything for repash okay so let's go ahead and add this I'm going to go ahead and in my project create a new EnV and a EnV file is where we put our environment variables
now this is going to be different based on which framework you're using which meta framework but I'm going to say V rep license and by prefixing V in front of this it makes it so this is public now this license key yeah yeah it's one of these things that you know we working client side this thing is going to be available client side no matter what you can only hide it but it makes sense to put it in your EnV so that way the license key just isn't sitting in your git repo while uh you know people can just take it they might have to do a little bit more effort to even know your applications using this now I'm not going to show you my license key but I'll just paste it in here and you can imagine what that would look like now from here let's go ahead and utilize that license key we're going to be doing so just by moving that license key to where we had our license now there's a number of ways you can do that and any of your applications consult nextjs or whatever you're using but we can say
import. meta Dov Dot and then the one is vitore repore license uh co-pilot you're not being helpful because those are all wrong okay so with that let's actually head back to our application and see if we get rid of that error which we do you're going to see these couple of spelt errors down here in fact these are just warnings I'm going to turn them off for now it's spelled fives in beta that's currently a bug there's even a PR to fix it no worries here right okay cool so we have our application we have our replic now we actually want to start doing something with it so let's get going on these mutators now the mutators are where you mutate your data and these things are interesting because they Define what happens to the client side data so you really have to think about we're modifying the client side data and then eventually we're going to send messages that show the patch of what
changed and we'll have messages as well and we'll even have poke so that way we can poke the client side to pull again but all in due time cuz this stuff is a bit complex so we're going to say create too and this is going to be an
async function like this and we're going to first create a key for this item the key is the key value inside of our database we're going to say const key is equal to uh typically you you'd say what the the data type is so it's a to-dos forward slash and then we're going to say it's going to be the args doid okay what args you say well the args are going to be passed into here however the first parameter here is the TX frequently referred to as a TX inside of their documentation this is every time you see TX in this video it means a transaction okay and the second one is going to be args now the args believe it or not is going to be a to-do a singular to-do so like this that way we get the ID right remember we created that inside of our to-do type this allows you to make sure that when we run this function it's type
safe what about this TX you say what about that TX this TX is going to be the type of write transaction like this right transaction we can import from replic Cas right trans transaction and this comes in automatically from replic however we're using this as a type I don't know why vs code didn't do this we need to have a type right transaction okay cool we're moving so this is our create to-do we're creating a to-do with a key of uh the ARG for the ID so each one has an ID then we're going to await and then we're going to await tx. set if you try to use put here I'll say it's deprecated even though uh their docs do show put sometimes but we're going to do uh tx.
set we're going to pass it in the key and then we're going to pass it in the args which is simply R Todo so that's how we add a to-do to our cache so before we get into the toggling of a to-do let's just go ahead and get this working where we can at least add and see to-dos on the screen so you'll remember that we did in fact create our to-dos state right here which is in my which is in our application a spelt State as long as your framework of choice has State this is how you would do this now I'm going to be doing this inside of an effect and an effect in spelt is the exact same as a use effect in react you just don't have to specify the dependency array so I'm going to say effect and effect just like react takes a function okay now just like react when you return from an effect it uh you can return the the destroy from here like let's say we we build up something let's
say we create a listener um if you return the destroy when the file unmounts it will destroy that listener okay so to do that just like react we return and what we are returning is the rep. subscribe subscribe and now what's going on inside of subscribe inside of subscribe we have a async function that isn't Anonymous it does not have a name and this gives us access to a transaction again everything inside of replic cach works with transactions now inside of this transaction we need to access our to-do items so we can say const to-do items is equal to and this is going to be equal to let me even make my screen just a little bit more full size here so you can see it all this is going to be equal to await do await tx. scan okay you can see scan
gets many values from a database thank you uh overlay for hiding the code but telling me at least what it does so we're going to await TX scan and we're going to look for every single item that has a prefix of to-dos and I believe we said to-dos and not to do yes we did okay uh make sure that that is correct if you don't have the prefix the same as the key for your item here it's not going to find anything um and we're not going to stop there we added a semicolon but we're going to also do in inside of this entries and then we're going to take our entries and we're going to turn it into a two array so this is essentially getting us all of the to-dos from our database and it's going to turn them into an array like we'd be used to using so this is already quite a bit right Tod Do's items now one last thing here before we do anymore is I'm going to console log out this to-do's items and we're also going to add in a
function here here where it's just going to be items an array and we'll also console log items here okay this is just so we have an understanding of what we're doing before we actually get into doing it and I'll I'll walk through exactly what's happening here as well but before we do that let's go ahead and work on our onsubmit function that way we can actually have data to see if we're loading data correctly so you know I lied I said this is going to be a simple event this is going to be a submit event type and it's going to do prevent default from here we're going to get to use our replic we're going to say rep.
mutate and then we're going to get to use the name of our mutation now what's great about this is that repash already knows what the mutations you've created are so it has it here as a method for us that way you're not getting any spelling errors incorrectly and this is going to take essentially an object of all of the things that we said it would have including an ID now creating an ID can be tough in this scenario if you plan to sync it to a third party database so your ID creation strategy could be with a uu ID or something like that and then you could have that ID in the database if you already have items with IDs in the database then this also gets a little iffy you might even leave it out of the create process and have it only return from the server with an ID but that is part of the complexity we're dealing with for this very simple example I'm just going to say new date get time and that's going to give us enough of an ID uh next thing we're going to have is a name and this is going to be form state. name remember
that we're modifying form State and giving a name this is still angry because we haven't completed everything uh deleted is equal to false um created at is going to be equal to a new ISO string and completed is also going to be false so for once co-pilot actually got all of this stuff correctly actually believe it's a codium and not co-pilot so shout out to codium uh which is probably why it got right um big fan of codium by the way so okay created at new date ISO string updated at new date ISO string completed false we now have no more typescript errors because this is what this is expecting and here we want to clear out our form State and this going to be form state. name is equal to just an empty string just updating that value if we had a more complex form this would be different okay okay so not Lots going on here just yet we're making a to-do app right nothing too crazy but let's go ahead and add our to-dos you can see some things
are already conso logging out so we're going to say go to store and hit add you'll notice that obviously nothing has appeared here but inside of our to-dos items now is an array of two things you might expect one thing uh but it is in fact two things also uh we're getting some errors inside of here we're not going to worry about those just yet because of course we have a fix for that so to-do's items is an array and it looks like this now when we turned our to-dos items into an array and got the entries to an array you might have expected it to look like the actual data but in fact it looks like this where we have the key as one value and then the actual to-do as the second value in the array in fact if we were to add another one which is record tutorial if we scroll down here you're going to see we have two arrays that each have
two items each with the key and then the actual value so that means which is one of the reasons why we logged this out in the first place is that we need to adjust this and when we return from here we're going to return to Do's items. map and then we're going to get the individual item and it's it's going to be return in the second option in this array now you could always say item one here to get the first index out of here another thing you could do is destructure this into an array so you could say uh we're underscore is often used for things that you're not using and then colon values and then we could just simply return values this is the way that I like to do it personally you know whatever do it the way you want to do it they they do the same thing now since we're returning to do items here the value that was
previously undefined whenever we ran this is now going to be actually equal to an array of just the objects of the two things we want which means that all we have to do here in this return because what this is doing is it's grabbing the data from the transaction and sending it along and this is where we process or uh decide what we do with the data and in our case we're just going to set to-dos is equal two items like this now you'll see we get a typescript here because it's it's arguing that readon Json value is not assignable the type of to-do you could say that this is a to-do like this but then again we now have this error wordss uh not returning the right thing so of course of course of course of course eventually we have to get into casting here where you could say as too and that should make typescript happy where we now have everything as it is a to-do so with that out of the way
if we head back to our application guess what we see it's working this is fantastic right this is exactly what we wanted this to do now the next things we need to talk about are what we want to do when we are actually modifying the store because we're able to now add things to here and you'll see what was happening because we actually work with subscriptions but let's say we have a to-do which is just vacuum picked a word I know how to spell um vac great thank you uh for that and I hit add it adds it automatically not only that I refresh it's storing it to our Local Host now if we nuke the local database this data will be gone but for all intents and purposes this is saving to our database this is fantastic next thing we want to do is write our toggle mutation so that way we can actually complete a to-do so let's head back up to our mutators and we're going to say toggle too and toggle to-do is once again going to be the very similar
structure um and in fact we're taking we're kind of taking the same things we're taking the to-dos as an argument here and the right transaction as the transaction let me pull this and let me pull this open here full screen CU what we're going to do is say const key and once again that way we update the correct one we're going to make sure that the key is the exact same as what the key was before and then we're just going to once again await TX set key args now this is basically just an update function right you could think of this as hey we're updating the thing doesn't necessarily have to be name toggle to do because it's just updating it but I think this is more explicit this way uh of course in your own applications you can choose to do what you'd like let's go ahead and write our toggle function here function and this is going to be toggle toggle is going to take a to-do which is the type of to-do and this is going to
rep. mutate do toggle to-do thank you for that and we're going to spread out to to Todo and then for completed we're going to set it to the opposite of what it currently is toggle is just going to be called as an onclick event on our button so let's come in here to our button and we can say onclick equals and this is going to be a function that that toggles the specific to-do since this is happening inside of an array so that will toggle our to-dos that's really great um next thing I want to do here is add some very quick uh way so that we actually know that they're being toggled when we toggle them for instance if we go ahead onto the list item itself we could add a class of completed is equal to to Do complete this syntax in felt is simply just saying set a class of completed if
this value is true so if it's completed give it the class of completed so I I know I'm not going to write much CSS in here but let's at least do this where we'll say completed and we'll we'll cover CSS at the end of the day we'll say opacity and we'll say 0.5 so if something is completed just make it slightly transparent that way we can tell that it's uh been that way okay so let's see this in action now if we click one of these hopefully what happens is it goes transparent and you could see that it has now this all seems fairly complex and maybe overly done but when you realize what's going on behind the scenes is that it's not just saving data to an object in an index DB and it's taking in all of the information about who is doing what and how they're doing it and even just to illustrate this let me show you this I have these side by side here and this is something that comes out of the box when I click record tutorial here you can see in the other window it's immediately going and this
is because we're subscribing to that data in the local database now granted this is two tabs on the same machine in the same browser and that's why this is working obviously if you didn't in different uh browsers it might not work the same because the data isn't being stored right but that's where the whole database part of things comes in so that's really awesome and if we wanted to take this thing offline we just need to add a service worker now if you wanted to end the tutorial here and mess around with replic Cas now is a great time to do that uh the for the remainder of this tutorial we're going to be working on styling we're going to be working on just small little UI tweaks and making it a little bit more interesting so that way when we finish we get this instead of this right but again you now have enough to get replic cach basically working offline now if you're wondering why we haven't synced to a server I'm going to just show you a preview of how that might look here in fact this is an application I'm working on I'll bump up the font
size and you can see instead of just having mutators I have a pull and push URL this is where it pulls and pushes data from and it also has like an on syncing in here but even let's take a look at our mutators our mutators are way more intense um actually they're up here uh where are they at set user update yeah these are our mutators right and at the end of the day it's still just manipulating the local data but we have a lot more of them and if you feel like this isn't that complex let me just show you really quick the pushing poll so I'm going to show you really quick a push and inside of a push it's you can see like a 14-step 15-step process where what we're doing is well let's go all the way to the top this thing is intense so the first thing we're doing is checking to make sure the user is able to do what it's doing then it's getting a list of all of the transactions then it's going to be getting what's called a client group which we're not going to talk about if the client doesn't exist insert it then we're going to be then
we're going to make sure that the uh user is the the correct user to access that information then we're going to take a look at every single mutation in this message and we're going to get a client ID with the client ID from that mutation if the client doesn't exist make it again check authorization uh make sure that user has access to that client we're going to set the next version so that way it it updates a version each time you push or pull we create a server space which is like you could think of a space where the data lives on the server maybe multiple people are writing to that space this example just one person is again we're grabbing the next version we're checking to make sure the versions are correct we're saying hey if the next one is equal to the current one do this do that whatever you know we're either rolling back data we're um we're skipping the mutation and all uh check for an error otherwise if all of that is true let's go ahead and update the the
server to the next version let's update the client to the next version then let's run a emitter to say that hey uh a pull is required and then we're going to say push success equals true now you might be wondering in all of this where is even the database mutations going well there's a function I called called process mutations where we wrate a big old switch statement and this is where we actually get into our DB queries so needless to say there's a whole lot there which is why we can't do it in this video so if that's all you want to do thank you so much for watching I'll see you in the next one if those of you would like to stick around and make this thing look and feel a little bit nicer maybe learn some stuff about spel as we go stick around cuz we'll be doing that in fact I'll even show you some of my favorite resources to build this stuff uh nicely okay so uh we have all this one thing I want to do is add that neat little animation you'll remember in this version when I click something or when I let's see let's click a different one you can see it puts the uh the ones that are complete at the top and the or the ones that are complete at the bottom the ones that are yet to be complete at the
top okay and it does that by the order in which they're created so how do we Implement something like this well one thing that we can do to get the animation of them Swip swapping around is we're going to use an animate flip from spel this gives you easy flip animations animate colon flip and I'm going to set the duration of that to be 200 milliseconds cuz the actual duration that it comes with is way way way too slow okay animate flip like that duration 200 now this is getting angry because we don't have a key here if you need a key which we do you can set a key like this in react it's just key again you don't have this animate flip but I'm there's tons of Animation libraries for react and so that's a good one right here that will get us animating now what about actually sorting them correctly well each time this data comes in from our database we'll want to sort it so we can
sort it a few ways and we'll also want to potentially filter it as well so right here when we had this uh values as map as to-do we'll also want to filter to make sure that a to-do is not deleted so to-do do deleted doesn't exist so again we're not doing any deletion in this video but this hides the deletion ones if we want to delete items again we had that deleted flag next we have our to-dos equals items but before we do that let's do to-dos equal items and we're going to sort it we're going to sort it twice the first time we're going to sort it is going to be the if it's been completed or not then also created at so actually let's first do a comma B and if you don't know much about JavaScript sorting I don't love the syntax ever and so it's it's not something I'm going to spend too much time on but you can copy and paste this we're going to do a uh if created at is
greater than b created at then then we're going to want to what return one so if created out is higher than b then it goes up in the stack and then we're going to do a nested Turner because you know who doesn't love those this is like the only time I use them we're going to say oh I wrote create at not created at Oh no typescript just save me I'm going to keep it as create app at I don't like that I I like created at uh but actually yeah let's fix that because I don't like create app thank you typescript this is why we use typescript folks if you notice that because we did have updated at then you are a champion now unfortunately that means that uh some things will break here don't worry we can always create new to-dos and new items here especially when we're working in development we can say all right well if if a is not greater than uh if a is less than b then we're going to want to check
to see if B is greater than a created at if B is greater than a then move B then move a down one otherwise keep it where it's at okay so that's sort we're going to do the exact same thing now to sort whether it's been completed so we're going to say sort and we're going to say sort once again a comma B here's an array and we're going to say inside of here if a do completed is equal to B do completed then leave them where they are otherwise a do completed which is in itself a Boolean right if it's true or false if a is completed then we want a then we want it to return one okay super cool uh that should give
us sorting so now we can see that if ever we check one of these you can see nothing quite happens but let's say I say did homework okay you can see it's now added it to the list we can now check and uncheck to our hearts content and you can see that when something becomes checked it automatically goes to there in the list I actually think I did some of this logic potentially wrong here um I think I swapped these greater than and in fact I even swapped it when I was talking about remember when I said that I'm not good at sorting I'm sorry created at is less than b then return one okay so when I said a b you swap them in your head sorry about that folks uh again sort is not my favorite okay there that should do it I swapped this one and I swapped this one and that's it now again when we create a new item dance and actually it's still not working because if you can guess I I
goofed up something even here I'm still doing create at created at sorry folks let's go ahead we have everything in place now the Sorting is all correct our information is now correct this is how it works and if you ever need to Nuke out your database you come into application you find your database you click delete database you click okay you refresh and they're all gone again we're working in um what is you know an environment where you're prone to make mistakes here and I just noticed I did did homework I'm not on my language right now but now if we check any of these goes to the bottom checks them does everything correct we say um do dance now puts it at the top we check it it should be the first one that is not that is complete cool good step moving along let's go ahead and add an icon in here so instead of this Emoji check I like this checkbox I got this checkbox from an app called nucleo This is an app I really like for icons you can find it
here nucleo app.com just download it get some icons again I have an icon here and I've tweaked it just a bit this code is going to be open source so if you just want to copy directly from mine that's cool too um I'm just going to paste this SVG in line I know that uh you know that's a kind of a topic on its own I'm going to paste this SVG in line some of the most important things is that I'm using some CSS variables here that we haven't quite added yet but we will and in fact let's get to that aspect of things let's go ahead and make our CSS file or at least our root CSS file inside of our routes we can say app. CSS in this file we can import app.css let's go ahead and write that app. CSS now this CSS is going to be coming from a couple of different places the first thing I want to do is create a root in fact this is me uh writing CSS from
scratch how would I approach this well the first thing I need is a type scale and I I come into my handy dandy typ Scale app which is available to you at fluidy type. to.ski I'll have a link in the show notes and I can select the way I want this type to look I like a ma major third I'm going to add an xxs here and I'm going to even add one bigger one just for funds I'm going to go ahead and copy all of this and these are going to be my my type scale so that way I can use these variables anywhere in my application which is nice I uh I typically always get this for my fluid type this in itself is a whole interesting topic next thing I want to do is grab some variables we're going to say hyphen hyen black and black is just going to be equal to 1 1 0 1 0 just SL off black I I like hard hard on black but you know uh same thing with white e e e okay three e then what we're going to do is we're
actually going to have a BG and BG is going to use an interesting function in CSS this is going to be light hyphen dark so if we're in light mode we want to use the background as white if we're in dark mode we want to use the background the background as black okay so this is the background and likewise the foreground foreground is going to be the exact opposite we want the foreground to be black in light mode um and light in dark mode to get this to work we do need something and we can just throw it down here out of the way color hyphen scheme is going to be equal to light first then dark again this allows us to use our system colors automatically without having to do any extra work and then we have this basic functionality right here this is pretty amazing because it gets us a whole lot the next thing I want to do is add some class to the body or some styles to the body we could say font hyphen size VAR hyen
hyphen base or FS hyphen base we can have a really tiny little reset of margin zero Auto we can have a padding of 20 pixels I don't usually add a padding to the body but this example is so simple no biggy and then the background is going to be equal to VAR hyphen hyen BG and the color is going to be equal to VAR hyen hyphen FG for foreground and then font hyphen family is just going to be a system hyphen UI you could have a whole stack for this if you wanted but system UI works just fine okay okay save this bingo bango uh let's head back to our application it should be looking quite a bit more like the example in fact let me even close this console looking quite a bit more like the example here cool this is is working right it's definitely working we got the type scale we have all that stuff let's
head into our page dots felt and handle the rest of the CSS there because this is going to scope the CSS specifically to this file in fact I'm going to come all the way down here I'm going to add a label and give the a display block cuz I like my labels to be display block like
that um and I did of course the classic misspelling of label I'm going to do for each list item we're going to do a display Flex we're going to do a line item Center get things nice and centered we're going to do a gap of one R and a font size of VAR hyphen hyen FSL so we're going to get we're going to go large with this okay and actually for the UL we need the classic list style none uh not letter spacing normal um okay and then G classic margin zero padding zero okay cool cool cool actually even in here in our form we're going to give this a margin bottom one ram just to give it some space nice and easy uh but none of this Stu stuff is the fun stuff honestly it's starting to look good but it's not quite there yet right so now it's time to get to the fun stuff where we're
modifying the the way it looks when it's completed I'm just going to go all the way to the bottom here where we have this initial completed I'm actually even going to bump this down to 0.3 I like that better I'm going to give this a transition a transition of 0.2 seconds we're going to do the just classic ease I know they're better easing Curves in this and then we're going to say uh at hover we're going to give this an opacity of one no matter what I spelled transition wrong of course uh thank you for that and so now whenever these are complete we hover over them it makes it so they are non-transparent like this beauty next thing we're going to say is the span inside of here is going to have a position relative okay because we're going to want to say the after of the span we're going to want to give it some stuff it's going to have a content of empty it's going to have a position of absolute PSA it's
going to have a display Block it's going to have a height of uh 6 pixels it's going to have a BG of we'll just give it something arbitrary I don't like to do non-css variables but this is fine it's going to give a top of 50% and it's going to give a left of 0% and uh a width of 100% that should be enough we'll see in just one second if that was enough okay and what this is doing is this is creating the crossed out line so you can see with that bit of CSS with an after we now have this crossed out line that looks really nice so we're almost there we have the nice little uh check going through we have the opacity this is all zooming around here uh this is really sick I think basically the last thing we need to do here is get our button in check and the button I don't know if I gave it a class but I will need to give it a class let's grow all the way up here give it a class of uh we
call it check not a class of button no thank you and then down here check check will be getting some Styles padding zero it's going to be getting a BG of transparent not going to spell that wrong it's going to get a border of none it's going to get a display of flex and this is just to get everything vertical and you know aligning things vertically it's the joke but if you display Flex something and then align items Center it just works um we can save this and you could see just about instantly we're pretty good one more thing is that you could also you know fix this by giving the UL display a flex but you could also fix this with a neat little trick where you could say Amper sand plus Li and this is going to have a margin top of one R uh now we give them a bit more spaced out in fact we could even go go like
0.5 rims uh I use rims for spacing because it's nice and it's nice and easy to keep everything relative here cool uh actually we want to make this look a little bit nicer too and you could choose to style this stuff any way that you'd like uh but let's go ahead and do that up top inside of my form here where I had this I'm going to say both the input comma and the button are going to get some Styles um they're going to say let's say font size of VAR FSL we want to go big with these just because it's a big bold app right um BG of transparent for both of these we're going to have a border of none we're going to have uh and that's weird that it would suggest outline of none don't put outline of none on your inputs uh we need if you do keyboard access you need that outline so don't do that in a bad AI okay so we'll do trans
transition 0.2 seconds ease and this is just going to be background I almost never write the word background so forgive me for spelling that wrong and then we're going to say on Hover just give it a nice little background now let me show you another now let me show you another little CSS trick here for this background status in fact I'm going to go back into my app.css and back up here for my variables where we have um the FG or the BG now this is a neat little trick that I do where it gives you a shade or a tint depending on what exactly which color mode you're in but you don't have to even use the light dark function for this so uh let me show you exactly how I I managed that and uh for those of you who didn't go to the art school a tint makes something whiter and a shade makes something darker so if it's in dark mode we want the hover state to be darker if it's in dark mode we want the hover
state to be lighter so this is tint or shade and this is going to be automatic we're going to do a color hyphen mix which color mix is available to just about 90% of users worldwide so I feel comfortable using this function and we're going to say in okay lab comma VAR hyeny FG and we're going to mix that with transparent uh 9 5% did I write transparent correctly man my spelling has been all over the place today okay this seems like it's fine we'll see if in a second it worked so this is my tint or shade basically we're mixing the foreground which is actually toggled here and we're going to mix it with transparent 95% so no matter what we do it's going to be a little bit transparent on top of here giving us a shade or a tint whenever it's appropriate and we can say VAR
background is equal to tint or shade cool so now if we hover over the input you can see yes we do get a nice little like that again this is just going to be you know this is going to be at your own choice you can make this look at as however you'd like I'm going to say border bottom oh border bottom and it's going to be one p solid FG in fact check this out I'm going 0.5 pixels we're going to make it nice and small now we have this right here now those of you with a Keen Eye will notice that my auto complete actually goofed up I wrote font style right here so if we change this to font size now we have our nice big stuff now and for the most part this stuff will get kind of fluid now the interface doesn't necessarily hold up if you go to mobile you'll want to do some more work there but I think this is a great start to what we had and it's going to be a nice basis for adding in
the server side syncing that we had talked about all right so two more things before we're done here if you want to keep following along we're going to be adding our service worker to get this working offline and also I want to show you just a little bit really quick here about that light and dark mode thing that we did so when we went to let's do our app.css when we did our color scheme light dark and then we used uh light dark here what this allows us to do is like I said it uses the system color for light dark and if we head into our Chrome Dev tools click these three dots we go to more tools we look at rendering if we scroll all the way down here um right here pref first color scheme if we say light you can see the application actually responds it's it's pretty neat how this thing works and this actually revealed an error that I made in the CSS I nested a check inside of completed we want these properties to apply to check no matter what so it's
really good that that actually uh did that for us because it gave us a way that we could see what our application will look like it really unlocked a bug that we had so again if we change prefers color scheme light or dark this application looks great flipflops all that stuff okay lastly we're going to be using our service worker now in the last version I showed you a nice and easy service worker that you can grab directly from spel kid's own documentation uh however again any application that you're working on will most likely have its own means of getting a basic service worker up and running so don't just copy and paste this one from the spelt docs into your application if you're not using a spel kit site uh but there's even a v pwa package which I found to be a little overwhelming myself but there's tons of ways you can get a service worker for what you're working on so to utilize this service worker I'm going to head into my source I'm going to create a new
file service hin worker do we'll do JS since this is actual JavaScript and paste this in here yeah there's going to be some typescript errors I'm going to ignore those okay now we have a service worker which means that if we head back to our application uh let's go ahead and X out all of these windows upon Windows upon windows we go to the application tab which is something that you're in a whole lot in these days where we refresh here um we'll be able to see actually really quickly we'll see that a a service workers here but since I use a lot of service workers in my local stuff let's go ahead and let's go ahead and unregister this one I'm going to click unregister and now it's gone now there's a new one here but I can most likely be assured that this new one is the one I actually want in fact we can check our Network tab we can see this and you can see that it is being loaded from a service worker which means that if we do offline mode in our application and
refresh not only does the app still work but we can still use it we can still add new to to-dos uh and it's just going to be nice this thing Works no matter what so this is offline software this is local first software and this is the bones of something that could be much bigger now now obviously I know a lot of people want to get into the server syncing stuff and we got a taste of it here but in the next video what we're going to be doing is actually setting up a database we're going to be using postgress we going to set up an omm we're going to be using drizzle we're going to write our push and poll we're going to be learning all about the transactions that happen the mutations that happen on the server push and pull and then from there we'll get into more Advanced Techniques we'll even talk about the backend strategy section of the replic cas page because let me tell you there's a lot of different things you can do with replic Cas so as always this is Scott I hope you really enjoyed this one the code is going to be available to you on GitHub check it out let me know what you think if you make a
react a view or web you know just literally anything version of this let me know share it I'll give you a tweet whatever I'll check it out so thank you so much for watching and I'll see you in the next one I hope you have fun building your local first software
Quote it with credit and a link. Quoting Syntax