Skip to main content
View on YouTube to comment and like!

Syntax Video

Syntax Bytes with CJ - Code Challenges | Working on syntax.fm | CODE Q&A

Chapter 1 0:00

Welcome

hello hello welcome in to syntax bites a bi week B monthly month live stream uh we I'll just be doing a variety of things so today I've got some fun stuff planned we're going to get warmed up with some code Wars code catas and uh then I'm going to work on the syntax. FM website so you can see me go through the issues pick an issue figure out how to solve it in the code base and uh after that we'll answer some code questions I'm also open to answering questions any time in between as well so uh let's get into it welcome in everyone uh good to see you kidus and Vias and skimp and DX drony hello hello welcome in welcome in all and over on the twitch side good to see you uh Andrew and Miko and IR Roam and zero Turner and Murdoch and lazy welcome in welcome in okay so like I said we are going to start off with some code Wars code CS I got to get the the juices

flowing I got uh try to solve some codes and and kind of get warmed up um so let's do it if you haven't heard of code Wars it's a place where you can practice coding um it's a lot like leak code but they have easier problems that you can get more ramped up on it's not 0 to 60 like it is on leak code uh and the difficulty ranges from 8 Q which is the easiest all the way up to 1 Q which is the hardest and I think we'll get started with a 7 Q so let's do it I have it filtered here here by most completed and then uh cotas I haven't trained on so these are ones I haven't seen before let's do it and uh if you want to solve along with me feel free and uh I can take a look at your Solutions if you do decide to solve along with me but let's jump in so this one says given a string s your task is to return another string such that even indexed and odd index characters are

Chapter 2 1:03

What is Code Wars?

Chapter 3 1:50

7 kyu - Odd-Even String Sort

grouped and the groups are space separated even index groups come as first followed by a space and then by odd index so we have an input string we grab all of the even indices smack them together put a space all of the odd indices smack them together okay I think I can do this and I'm going to do it with JavaScript um I guess there's a couple ways to go about it I could do it like in a single pass like a single for Loop and then build up two strings but it could be fun to do with filters let's just do it with a filter because we could do something like this we could say the uh even letters are the string itself split into an array and then we map that array to give each character and we're also going to get the index and if that index is even return

true so return index mod to equals 0 so this is going to give us the even letters and then we can do the exact same thing to get the odd

letters but we say index mod 2 does not equal zero and then we just return a string that has all of the even letters join together with a space and then all of the odd letters joined together with a space this should do it now uh we do have two iterations here we're iterating over the array twice but it's not a nested iteration so it should be fine let's see did I do it I didn't even letters is not toine event letters I added an extra T okay let's go all right um oh oh because I'm doing a map I don't want a map I want a filter yeah with this map I was returning booleans I don't want booleans I want the literal letters themselves so this actually needs to be a fil I'm spelling things

wrong like crazy today this needs to be a filter all right this is the one nice all right we'll attempt it um uh Sue Austin says how would you split it if there is an emoji in the string yeah so so uh and and not just emojis we're talking about like Unicode characters here and what you would do is you would use array do from because array. from is smart enough to group together like Unicode pairings um yeah I've dealt with that before in some em emote parsing but what's up I'm here good to see you and uh poo good to see you as well it says looking fresh with a new haircut I appreciate that cool so that was not too bad uh we do have did anybody give it a try anybody want to show another solution before I I flip over to the solutions let's go ahead and submit

it cool we got it this looks like the thing that most people do two filters smack them together uh but this is what I was talking about with um doing it in a single iteration you just build up two strings with a single for Loop like technically this requires is less iteration because it's a single iteration whereas uh this is well I didn't even think about this our solution is actually Four iterations because split is one iteration to turn it into an array and then filter is another iteration and then we do it again and we do it again so yeah we could definitely speed it up or clean up um cool uh I deal with the Emoji split what do you mean so the fact that I've dealt with it before yeah let me see if I can find any articles on it because it is a thing and and and it's be it has to do with like special like Unicode

Chapter 4 6:00

Splitting Strings with Emojis

characters so so like JavaScript split uh string Emoji Unicode and array. from I know array. from uh does it yeah this this won't work because there are certain emojis that are actually a combination of two emojis um oh apparently there's an Intel segmenter I wonder if that is smart enough to handle emojis this popped up on a recent syntax episode um I think it was on the stumped episode this one

yeah describe the purpose and functionality of intel. segmenter so the way it's exp explained there is it's typically like language aware splitting and segmenting of strings um but no one here is using array Dot from here we go the proper way to split a UTF string is using array Dot from it's so far down and the answers that's that's the way to do it cool all right uh let's keep going and let's answer another one yeah so and Pa says I was just joking um that you don't have to do anything and suffer from the Emoji being sweit yeah Amir says it would be nice to mention the space and time complexity of solutions sure so that's something that you might be asked to do like in an interview um our solution spacewise is based on in so it's in and then we're creating a new array here so that's in times uh one and then again so we're

creating two arrays there like space complexity is two in and then uh time complexity is uh 4 in but they all simplified it in it's in um I guess except for the space complexity I don't know let's keep going yeah and uh vikus is asking what are we working on we're solving code Wars code cotus let's pick another one let's pick a slightly harder one we're going to move up from 7 Q to 6 Q now uh these vary in difficulty sometimes six Qs might even take me like 10 20 minutes to solve we'll see how it goes so here we go filtering by 6q let's take a look at this first one the vowel code create a function called incode to replace all the lowercase vowels in a given string with the numbers of according to the following pattern now create a function called decode to turn the numbers back into vowels that's fun I can do

Chapter 5 8:51

6 kyu - The Vowel Code

this I can do this so the first thing I'm going to do is I'm going to build up a map that says given a character this is what the number is I guess technically we could just put it into an array and then add one to the index I I like the idea of a map so here we can say uh the letter lookup is a new map and each of these needs to be a array where you have the string that we're looking up and then the the value is the number itself we got full screen too cool okay so this is the uh letter lookup and then we could create the same thing for decoding for the number lookup uh we'll just have to switch these

around like that okay so now when given a string we can split it into an array for each one we'll look in the map if it's in the map we'll replace it with the letter if it's not in the map we will return the letter itself so grab that string split it I then want to take this string of characters and map it to a new string of characters so I'm going to use the built-in map method on arrays which is different than the map object here so this is going to give us each character and then um I could do it all in one line but I I'll write it out more more readable so we can say if the letter Li up lookup has that character then we are going to return letter lookup get that character so this is going to return the number otherwise we return the character itself and then we join it all back together into a string so that should

encode it and then the decoding process is the reverse of that so if the number lookup has that character oh okay but here's an issue uh the character here is going to be a string and in our map these are numbers so I actually do want these keys to be number to strings so that way the lookup inside my map will work I mean technically I could convert the character to a to a number but then I'd have to worry about Nan so I'm just going to do this so that way we can look up the character in here and then uh grab it all right first try nice oh I forgot to link the cata okay uh that that almost seemed easier than the previous one um here we go if you would like to give it a try There's the link and thank you catic thank you for

that follow over on uh twitch um I'm going to solve a couple more and if you want to give this one to go while I'm doing the others uh I'll be happy to look at your solution if you share it in the chat what's up Alex welcome in and uh darene says how would you approach to streaming voice between two or more pair like a chat with websockets but vo is also in the room for example you would use something like uh web RTC well I mean there's a lot of ways to do it web RTC allows you to do uh peer-to-peer uh video and voice chat and um really the only server infrastructure you need is for connecting two peers together and then beyond that if any of your peers are like behind firewalls or anything like that you actually need what's called a a turn server so it actually relays the traffic so that two

Chapter 6 12:28

How to approach streaming voice / Webrtc

peers can communicate with each other um but with web RTC you can create a peer connection and then you use the uh is it the web media API basically the the API that allows you to capture a user's webcam you use this in compet in combination with web RTC yeah so this is how I would approach it uh there are a lot of uh like software as a service that will basically give you like the turn server that you would need and all the extra stuff if and also the signaling server which is the server that allows your peers to connect to each other um look up like web art TC host there's there's some stuff out there I I forget the um the specific uh services that provide it but there

are some Services where you don't have to write all of like the backend codes and everything you can just use them and they have like a monthly fee and then like a free tier as well but that's how I would approach it cool uh dogukan is asking do I have a course yeah I have a react course um it's called react Roots if you go to react roots.com um it I made it a little over a year ago it's still relevant though it's it's all about the the basics of react itself this this doesn't get into nextjs or serers side rendering or anything like that this course is all about the the basics of react but I do have that and I am working on a course for nextjs but that's going to be published for free on the the syntax channel uh all right let's do another one but uh hello hello mihi welcome in welcome in and good to see you too Stony see you on the on the twitch

Chapter 7 14:20

Do you have a react course?

Chapter 8 15:06

Deep Cloning class instances

side pal is asking do I have any solutions that deep clone JavaScript objects and classes without whipping out the Prototype structured clone destroys the Prototype of the main object and all the properties I don't have an answer for that

um yeah I mean you could P potentially use structured clone and then set the Prototype after using structured clone um and then beyond that reaching for like a third party solution like I don't know um what's built into low dash for cloning I mean I think low Dash has a deep clone so it might actually um handle these kinds of things recursively clones the value you tried with low Dash yeah I don't have a good answer for you then whatever it is it's going to be manual it's like do as much as you can with structured clone or some other library and then afterwards you might have to actually like hook it up cuz yeah I haven't done anything like that before uh what's up Paul good to see you uh we need a mustache styling course I just use a little bit of uh hair glue

and that keeps my mustache up all day you uh you tried resetting the Prototype yeah I don't have a good answer I mean if you have a a code sample maybe I could I could try to run it and play around with it uh and Mii is saying what do you think of hono trying it recently with a spot it feels so much better than those SSR Frameworks that are popular now it's cool I haven't had a chance to build a big project with it but I've done some basic stuff with it and uh I like it I want to do a more in-depth video on uh on hono basically you can think of it like an an alternative to express but it uses web standards and can be deployed a lot of different places um like Cloud flare versel netlify Azure like all you can write your code in this way and then they have adapters to be able to deploy all these other places but there's a lot of other cool stuff built

Chapter 9 16:43

What do you think of Hono?

in um like way they do routing and then they also have built-in support for jsx so this isn't like react server components this is literally just views that are immediately rendered using jsx so there's no like interactive Clan components or anything like that just using it using hono itself they do have a meta framework that uh allows for JavaScript Islands but by default this is just this is basically like using jsx as your temp SPL language for HTML um yeah and apparently they do have client comp

components fascinating yep and then uh the RPC thing is also super cool cuz it's kind of like trpc but just built into hono itself where you can specify uh an endpoint use Zod validators and everything is like fully typed here so yeah is cool I want I want to do uh like a more in-depth video on Hodo and code crafter says you are the coolest dude thank you code Crafters uh we want more CJ videos I'm working on it adir I'm I'm I definitely have more more videos coming down coming down the pipe all right uh let's do another Kata uh but Fon says I missed our Friday Friday Series yeah so that's a a series I used to do on coding Garden uh where we tried a brand new thing every Friday I want to bring it back I'm trying to find the time I'm going to be doing a lot of traveling pretty soon but eventually it'll happen it'll definitely happen all right let's do another

Kata uh I think we'll do another 6q let's see if this one seems too difficult okay assume that the hash is like a backspace in a string this means that the string a #b c # D actually is BD the task is to process a string with hash symbols I kind of like this this one seems tricky I don't think I can do this like in one go um but I like it so basically you're it's like you received some key input and you have to determine what the resulting output is if there is a backspace fascinating I'm going to do it this will be the last one cuz this seems like it's going to be somewhat involved uh but Stony says I want more coding Garden streams too I'm I'm sorry everybody there eventually eventually it's going to

Chapter 10 19:16

6 kyu - Backspaces in string

happen all right oh yeah and I'll link this one too if you want to try it um I'm thinking right now how I'm going to do it I I think I'm going to just do it with a plain old for Loop and we'll basically have the resulting string that we gradually like remove and add characters to yeah all right but uh um we're going to start this one in like one minute I will be right

back e

all right let's get into it if you're just joining us we are about to do a code Kata this one's called Backspaces in a string so you're given a string it has some hashes in it and essentially the hashes mean that that uh string had a backspace to it so um let's grab our example inputs and then I'm going to like pseudo code this first kind of like think through it so um my thought is we have like the output so uh a place to store the result and then we iterate over the input and for each element uh if element is not the hash we will appin to result and then uh else if it was a the

hash we will remove the last value from result simple as that's it and then return the result okay this isn't that hard um let's code it up so a place to store the result I'm thinking we use an array because then we can use uh pop to remove the last value in the array so let's say the result is an array and we iterate over the input let's just do a for each and then inside of there if the element is not hash so if the character does not equal hash we're just going to push it into the result so result. push that character but if it was a hash we need to remove the last character from the

result so we'll just say result. pop

and then we should be able to do result. jooin bring it back into a string that was too easy I don't know why I thought that would be hard okay uh let's see what we get okay s. 4 is yeah that makes sense so we were given a string I need to turn it into an array if I want to use a 4 each um so yeah let's split it then call for each on it

too easy all right we're gonna have to do one more that that one was way too easy for me uh and Stony is saying this is called the rubber dug duck effect I guess that's the thing it's like until you start talking about it out loud maybe you think it's harder than it's going to be uh uh poo if you want want to share a link uh twitch is probably the best way because I think YouTube Just removes all links um okay let's let's increase difficulty let's go to a 5q I could regret this because 5q is get even harder um but let's let's see let's see which one we are given first perimeter of a squares and I don't want to do that instantly I don't want to do that uh Gap in primes the prime numbers are not regularly as spaced for example 2 to three is a gap of one but 3 to five is a gap of two 7 to 11 is a gap of four between two and 50 we have the following

pairs of two Gap primes a p Prime gap of length in is a this sounds like a leak code problem I I don't want to do this um these are all like math focused complete the function that accepts two integer arrays of equal length compare the values of each member in one array to the corresponding member in the other Square the absolute value difference of both between those two values return the average of those squared absolute differences between each member pair okay I don't think it's as bad as it seems because it to me I'm thinking like we could do it with like a a reduce or something like that so um compare the value of each to the course Square the absolute value difference between those two values so 4 Min - 3 squared so 4 - 1 is 3 squar is 9 5 - 2 is 3 s is 9 6 - 3 is 3^ s is 9 so then we have an array of 9

Chapter 11 25:17

5 kyu - Mean square error

99 um and then return the average of those squared absolute values between each member pair so um the average of that is nine and then

what oh this is just a different output so we have 0 squared + 5 squared + 5^ 2 + 2 - -2 is 2 + 2 is four 42 is squared okay it's easy I can do this with a reduce let's do this with a reduce in one shot here we go and if you want to uh try along at home here it is and I'll take a look at Pablo's um cloning problem after we solve this one all

right yeah I think I I can do this in a single shot with the reduce um in on the first track place your bets now okay so here well can I do it with the reduce yeah I guess I can cuz I can reduce the first array while accessing the second array with uh its index okay so we are going to return first array do reduce we are reducing this to a single value like that um and this is going to give us the a value and the index and then B is going to be the second array at that index well bracket notation so second array at that index and then we want to do um B minus a no a minus B yeah a minus

B so we say the result is a minus B and we square that whole thing whole thing so I'm going to use math.pow so take that raise it to the second power all right so that's the resulting value and um we're summing them all together so our sum starts at zero and the sum is going to be our accumulator here so we actually could just return sum plus the square and then we take that whole result and divide it by the

length yeah I think that's it all right place your bets now who thinks I'm going to do first try Stony says test fail uh doesn't matter yeah you're right it doesn't matter if it's a minus b or minus a because it's squared to begin with oh no it's absolute value square the absolute value difference okay good catch I'm glad I looked at the chat okay so maybe this wasn't going to be first try uh it needs to be math. absolute value so take the absolute value of the difference square that okay who thinks it's going to work first try I'm like 90% % sure I I might have messed up something simple oh okay it worked nice did I get the job no cheating yeah I mean I I kind of

cheated all right let's take a look at what Pao was asking about so Pao had a question specifically about uh cloning a class and maintaining the Prototype or something like that do you know of any solutions that deep clone JavaScript objects in classes without wiping out the Prototype and then we can see what they're trying to do right

Chapter 12 30:16

Exploring cloning class instances

here so we have a class and another class and we want to create a structured clone of the

instance h i mean I think I would just implement this as like an instance like or maybe like a static method on the class because ultimately

um we're just trying to create another instance right yeah and so poo is saying what if you don't have access to the class um that's the tricky part because that's what I was thinking like the whole point of classes is to be to create copies so literally create a new instance and then uh from there have like a a method to copy over all of the instance properties but how can we grab the Constructor I mean what if we did something like this what if we said um the class or I can't I mean let's call it the Creator is uh test.

Constructor and then if we did new Creator where we pass in test and um I guess you're not using test two are

you and we can pass in the instance so um if instance is defined oh I see you're you're um you're creating an instance of test two there okay so the Constructor takes in an instance and um

well no I'm telling you right this is how we get access to the class we grab the Constructor from from the instance um and so if the instance is defined then for every um

you can't modify the Constructor to take it instance in oh that's fine too well don't do that then I would just create a static clone method outside of it um so cloned and then we'll say like copy properties so from the test so copy into cloned from the test one and then uh copy properties is just a function that grabs all the iterable properties and then transfers them over um so copy properties cloned copy from and then we'll do a uh object.

Keys the object. entries of copy from and

um each one there's probably a quicker way to do this but we get the key and the value and then from there um if this is an iterable property and we set it on the Clone so cloned at that key equals value um is hone an instance property of object

yeah object. hasone copy from

Key how about now clone. method is not a function um oh I well I don't want to set the Prototype

think I did it

um yeah we did it Pao I think I mean you tell me if this is going to work but basically uh yeah you don't have access to the class but you do have access you can access Constructor on the thing and then by inv Ving uh the Constructor here now it's actually an instance and then you just pull over all of the properties from the

original I don't you tell me maybe maybe this will work um we have a suggestion object.create get prototype this could work

too uh this isn't a deep clone because I'm only copying the top level instance properties here so this um this function right here would just copy over nested and property

um I guess technically it'll copy the methods too

which ultim ultimately means we probably don't even need the Constructor we can just literally I don't know I don't know if this is the answer I I'll share this and then maybe you can play around with it but maybe that gets you a little bit closer

you might just use a different clone function like maybe here is where you use structured clone I guess structured clone is the whole thing that's actually removing the Prototype I don't know but also take a look at what Stony posted there too because this gets the Prototype of the thing you're trying to clone it then uses object. create with that prototype and then we're taking all the properties of the of this and then applying them to that created instance so this could also work cool and Charlie says nice haircut thank you Charlie um why not use run JS to do these exercises I haven't heard of rjs a JavaScript playground

oh I just like doing it in the browser that's the main reason uh what are the situations where this would be a benefit to use if we're talking about what poo was sharing it probably has to do with like Library code like he's writing some library that other people use and he needs to be able to clone things that are being passed into a function that's my guess all right I think I have time for one more more one more Kata do we do we make do we get even harder let's let's look at another 5q and maybe we're we try a 4 q I don't know um oh this is the one that we just did the mean square error convert a hex string to RGB when working with color values it can sometimes be useful to extract the individual red green and blue component so this accepts a case insensitive heximal color returns a map string to int with the

Chapter 13 39:48

5 kyu - Convert a hex string to RGB

structure RGB where RG and B are ranged from 0 to 255 your implementation does not need to support the shorthand form of hexadecimal okay how would we do this we would take in the hex split it into Pairs and then we need to convert hex decimal string into a um a bite with a value from 0 to 255

let's try it let's try it here we go if you'd like to follow along at home here it is Yo is saying uh people pass in the default value that could be complex So to avoid mutating the original you clone it but it's a class then you lose the pro the Prototype yeah hex 2 decimal is that a built-in function because that's that's basically what what we need is um I think we can use the two string method with a something like that

okay um

here we have A1 B1 oh here we we we ignore the hash we have that pair that pair and that'll be hex string so we are destructuring this hexadecimal string to grab each individual character and then the R value

is going to be um A1 and A2 concatenated together into a string and then we can say hex to decimal with that we'll have to implement hex to decimal so this gives us r g and then B so G is going to be B1 B2 or B is going to be that and then this so r g b and then we return an object with r g and B okay now how do we do Hex to decimal I might have to Google it

um uh I think if we say um I think there's a parse in that takes in a thing and then it accepts the the base of the Precision something like this so uh let's let's actually try this out if I do a parse int of F and I say this is in base 16 uhuh look at me I didn't have to look anything up so this takes this is all we need this is literally all we don't even need to write a function Beyond this we just take that in say it's base 16 and then it gives us back the the integer uh um great yeah yeah that's it that's

wow okay I um I really I need to be challenged I need to be challenged right now so against my better judgment we're going to pull up a 4 q and uh see if I can if I can solve it um it's almost like you've done this before I mean I am a programmer I have used par stamp before all right human readable duration format I've implemented this oh maybe not okay one Q or another the thing is so in the past we have tried one cues and they have taken me multiple days to solve because at that point you're implementing like language features and libraries like we basically implemented a JavaScript only version of like connects it's like it you to like query on arrays and we had to implement all the the queriable functions yeah okay write a function which formats a duration as a number of seconds in a human readable way so it takes in

Chapter 14 44:33

4 kyu - Human readable duration format

seconds so it accepts a non- negative integer uh if it's zero it just returns now otherwise the duration is expressed as a combination of years days hours minutes and seconds

I think we can do this let's let's just let's just go for it right here's the link if you want to try it after this we're going to work on the uh the syntax website so there's a couple of issues open that I'm going to give a try but right now here we

go okay so we get the seconds we need uh our hours minutes seconds days and years uh yeah it's yeah we're going to be working on a Spoke cat app and we're going to pick some some of these issues that are at the top here okay so given an amount of seconds we want the uh number of years to be those seconds divided by um well let's start small the um we'll call this total seconds and then the remaining value of the total seconds is total seconds

mod 60 so for instance if uh we pass in 61 seconds when we we mod that by 60 the seconds component is one um and if we have 120 seconds there's no remaining seconds but 121 yeah that that that seems right so uh well did I Do by six let's try that again

so yeah okay so mod 60 gives us the total number of seconds uh the the the remainder seconds the minutes is uh total seconds minus the seconds that we chopped off the end and then the remainder of uh 60 * 60 because there are 60 seconds in a minute and so this will give us the total minutes

um and then the hours is going to be total seconds minus seconds minus minutes times uh I think we need some we need some variables here so if I need something like uh second in a minute is 60

wait basically I want to hold on to this this value

here one one minute in seconds oh it's just 60 what am I doing here then because if we take the total seconds subtract the seconds we have the overall minutes and then

um modding by 3600 does that give us let's try it out so if I have 120 seconds and I'm mod that I want to convert 120 seconds to um 2

minutes it's not 60 * 60 is it it's just divided by 60 that's what it is and then drop the remainder divide it by 60 okay and then drop the

remainder okay that gives us the total number of minutes now um we take yeah then I don't need this variable clear now for the hours we take the total seconds subtract the number of seconds subtract the number of minutes times 60 cuz we're converting it back to seconds and then we divide that by 3600 yeah so

um so 120 seconds is 2 minutes uh 3600

seconds is 60 minutes

but if it's okay this is a lot harder than I thought because if it's 60 minutes then we actually only want it to be

59 H okay we're going to be here a while um

I got this thank you Stony thank you for the vote of confidence yeah I have to think about how to handle this so uh I guess do I do mod I don't think I do mod 59 maybe I do

total

seconds yeah maybe I just have to handle it

here thing is if we have 60 seconds and we that by 60 that gives us 0 seconds is that what I was doing here I just want do I just want to divide by 60 in this case I think I do I'm all my maths are wrong because I just want to divide by 60

no I want the remainder seconds that's why I'm doing mod that's why I'm doing mod okay and then we'll do something like uh if minutes is 60 then um we don't want to use it in the minutes calculation so we'll just set it to zero so that way it's used to calculate the number of

hours and then if we have uh 3600 seconds and we divide by um 60 * 60 I guess we do mod in this scenario

is 1 hour cool yeah so it gives us this gives us the total number of hours and then we'll also math. Flor this to get rid of the decimal all right let's see what we have so far um we're going to return just a combine string so we'll say seconds seconds hey what's up Chad welcome in uh minutes minutes we need to reverse this hours minutes

seconds now um just going to do some yeah this is doing hour minute seconds let's let's just see how this looks I need to do I need to add the and um here and then we'll also have some conditional logic that doesn't include it if it's zero yeah I mean I guess we'll add that now so

um we'll have our parts and this is going to be an array of

strings and then for each of those we only want to include it if uh it's greater than zero [Music] so I mean what if we did something like this create an object with ours minutes and seconds and then we want to grab each one hours minutes seconds in that specific

order and we're going to filter to only have the ones that are not zero so we get the name we want to return Parts at that name and then

so this is going to give us just hours minutes or seconds whatever is remaining and then from there we'll map it to be this it'll be

name parts at that name

okay uh for now I'm just going to join this all together with a comma but we do have to change it up so that the last value there is an and so it'll say like and seconds let's see what this gives us so far ah then we need to pluralize so um yeah there's a lot involved here so um for the name we're going to not do this in one line so that it it's a little bit easier to read and so if also going to put this in

value like this so uh if the value is uh greater than one then it'll just be the name itself otherwise it'll be name and then uh remove the

S is that how substring works and then we'll put the label there I forget how substract works or where you can use negative indices so if we say hours Sub string negative one that doesn't work what about subst what if we do zero negative one what if we do um isn't there another one there's

I'll have to look it up on mdn uh at negative one will give me the last value I want to remove the last value subst is deprecated sub is De deprecated substring is the only non-deprecated one yeah I think you just have to do uh length minus one so if I do substring from zero to the length minus one that that that removes it that's what we need oh s slice is the other way to go does slice work on

strings no that only leaves the last one but I think this one lets us do yeah that's an easier syntax than trying to substring with the length that's the way to go okay

here 1 second 2 seconds one minute two minutes so I think our I think our logic is right here all right okay good so now it's it passes this test case but then if we want to do one minute and 2 seconds uh we need to handle that so the last value uh we need to add and onto there okay let's call this the

results and uh if um results. length is if there's only one thing in there just return

it if there's any more than that we are going to grab the last one which which is just going to be results. upop and then we're going to return the remaining ones joined together on a comma and then concat and the last one like

that okay so 1 minute and two

I get the label wrong

here so 2 seconds we are

returning value space label pop Returns the thing on the end oh Pop Returns what I don't know what does pop do or how does pop work

oh it doesn't output it I I see it's it doesn't output an array it outputs the actual value itself so I literally just do this that's why I was destructuring it to grab the first value in the array okay now we have an issue um we got up to um here so 1 hour was just fine but then when we're testing this one Hour 1 minute and 2 seconds we output 61 minutes and 2 seconds so um we need to handle

that so we're doing divide by 60 maybe this isn't what we want actually we'll do this if minutes is greater than or equal to 60

um minutes

let's work with that that test value that they give us there so this needs to result in one

minute so mod 60 gives us 2 seconds which is right and so if we subtract two we are now here if we mod um we divide by 60 that gives us the total number of minutes um and then if the minutes I mean I guess if we had like a really big value divided by 60 yeah we want to mod 60 this so divide by 60 and then uh mod 60 gives us the number of remaining

minutes yeah and then math do floor this whole thing okay

div by 60 mod 60 and if it I guess I don't know if it'll ever be 60 because it'll be zero it'll zero the we don't need to handle this anymore okay where we at wow now we've handled the basic cases so like hours minutes and seconds I think they want us to handle yeah Years days as

well all

right should be easy just more maths because now

days it's going to be the running

total so total seconds minus seconds minus the total minutes minus the total hours

I I'll use 3600 I know that that that's a value

here okay so this gives us the remaining seconds I'm going to put it in a variable

then after this remaining seconds is

this and then after this the remaining seconds well let's just test it with our existing values here okay so that's still works but we're cleaning up the codes a bit um now it will be

this oh and then at this point I just need to do remaining seconds I don't have to do all that extra

maths like that

and some value let's double check this okay so now

uh we take our remaining seconds to to grab the days we're going to divide by UH 60 seconds in a minute 60 seconds and an hour 24 hours in a

day and this gives us our days component see if we get this

right I think so if we have this right where it will start to break down is if it's more more than 365 days because after 365 days we now need to uh say years but this should get us pretty far um I guess I could write my own test case I'm just going to do an attempt um un incorrect answer for seconds equals zero expected now okay so I need to handle this so uh if the total seconds uh is zero we just return now okay handle that all right

um right now we have 4,300 we have a bunch of hours we're not including days why

not okay so we don't have days at all take the remaining seconds what's up Namaste geek I see you in the twitch chat welcome in uh we'll use this as our our test case here

so our test case for this many seconds should be this

okay so I think we need to we need to handle if ours is greater than 24 than it becomes a day um so yeah we divide this whole

thing by 24 we mod it by 24 yeah okay that's what that's what we're missing we need again we missed modulus multiple times now see all right so like honestly I think we found a pattern right so that gives us uh hours and then days we would just mod by 365 to get the number of years so at this point uh remaining

seconds

um is going to be days * 24 * 3600

and then our years it's going to be remaining seconds times 365 mod 365

me add it here all right I could be wrong somewhere in there but let's give it a go now do in ruce all right let's see what we got wrong so uh 1528 days yeah we're missing the year's component again oh I thought we added mod 365 mod 365 needs to be added to days that's just

why no way I can't believe we got through that that was I mean that was that was a challenge for me that was a that was a big challenge okay um now do it with a reduce I don't know if this is a is qualified I mean I could I could convert all of this into a reduce I'm going to call it there cuz I want to work on other things but let's do our final submission hey very nice very nice yeah in their solution here they had like a lookup for like number of seconds in each of these given things fascinating all right uh I'm going to take a quick two-minute break but when I come back we're going to work on the syntax website so uh this is our site that lists all of the podcast episodes it o has our newsletter our videos and it's open source if you check it out on GitHub we have uh all the codes here it's a spel kit app and there's a couple

of issues that I think I can solve on stream um and when we come back I'll pick one actually I'll have I'll vote I'll pick a couple and then we can vote on which one specifically that I work on yeah yeah and thank you for the follows uh um heck and Namaste geek appreciate it yeah what's up complexity good to see you as well oh and you got your coding Garden shirt nice nice nice nice all right uh like I said I'll be back in two minutes to work on the syntax site see you soon see you very soon

[Music]

[Music]

he [Music] a

[Music]

[Music]

[Music]

[Music]

[Music]

Chapter 15 1:19:33

Looking through syntax.fm github issues

all right I'm back let's get into it so now I'm going to work on the syntax website so uh this site has all of our P podcast episodes videos from YouTubes our our newsletter and it's on GitHub and we have a bunch of issues here so I'm just going to go through these issues uh pick a couple that I think are interesting and then try to work on them so some of these are really old I probably I don't know if I have the the context to work on some of these um but we'll see expand Social Links so some of this uh could be easy enough so we need to include personal websites YouTube Tik Tok Instagram madon and others so right now um we have all of these links and then there is a slash Links

Page that has these two I don't really have enough context for this so I'm not going to do it because I'm not quite sure what Wes had in mind um Implement structured data for each podcast

maybe not add more meta to the show results show number show type guest Avatar if available maybe even the AI tags the show number and or date will give a bit more context which is the newest maybe not the show type yeah I mean this is something I could potentially work on it would require some review and feedback but basically on the site we have uh search built in so you can search for episodes like react and it'll pop up here so what Wes is saying is uh we should show more info I mean this is a year old at this point we are showing the show number um we don't have the

type this has kind of already been been implemented because we do have the show number in the results here okay I'm not going to worry about this one

either and thank you uh Ryan Welter thank you for that follow over on Twitch uh mosiko says what tech will be will you be using for the website it's built with spelt kit so I'll show you how once we pick an issue I'll show you how I get it up and running locally but it's a spel kit app it's got a a postgress database and uh reddis cache yeah air has a great point so when we're talking about open source uh reply in the same issue and F follow up whether it's so relevant let's do that let's do that let's just say um uh show number is now included in results spell kit mentioned yeah uh both and Scott are uh spelt ambassadors so that is a thing uh I'm going to link to the codes to where the show number is included so this will be an exercise in source code

diving because I actually have no idea where it will be but I can make some guesses um it's going to be in the source directory I'm guessing we potentially have some kind of function in lib let's see yeah so we have search inside of here and then we've got the search result

list and when it renders out we are showing the show number somewhere yeah right here so I want to link to this little bit of code to say that uh

yeah there we go so now uh they can chime in on that all

right Play episode button active State not synced with the player active State let's take a look so when I pause or pause and close the player uh or close the player the button still displaying as if the episode is being

played uh Scott says this is now partially fixed however it doesn't stop when paused just when stopped so I'll have to tweak this might not be relevant anymore because Scott kind of revamped the um the player but let's see if we can replicate the issue so let's L an episode welcome to syntax today we have we pause the pause button changes

there okay so it's that's at least fixed it seems so when we pause the state of react 2023 this is okay um cannot replicate no and the thing is Scott did a bunch of extra did a bunch of work on the player so he probably fixed all this stuff it's just welcome to syx today we have the if I close this that updates okay so closing Works um close the player or pause and close the player we we did both of those so if we're playing Welcome to syntax and close it's linked

yeah this looks to be fixed by the recent player

work that's Scott

did Mr bot

MD okay great um here's the thing we're still contributing to open source by by going through these stale issues that's work that needs to be done so even if we're not writing any code we're technically contributing right now which is a good thing um all right add episode duration to the DB this one could be fun okay uh episode duration isn't something we've saved it would be calculated at import grab the metadata at the MP3 we could also run on import or sync yeah I think so I like this one it's going to be pretty involved but I think basically what we would have to do is I would first write a script to get the duration of every existing episode and then put that into the database so basically like add a new column to the show table that has duration and calculate it locally but then we need some kind of hook so

that when a new episode is published it can run in the background grab the duration and update the dat database um we going to add this to our maybe add episode duration to DB but this could be fun because we'll actually find a library for like looking at the metadata of an MP3 file and then doing that uh with node.js in some way so that could be

fun okay this is an accessibility issue um considering the point of the episode page is to listen to the podcast owner find and or find information about the episode it would be nice to have a skip links component that has a skip to main content or skip to Media Player similar it's currently not too many tab presses to get to a play button but it doesn't allow you to then stop the audio if you get out a link to jump to the player it would mean not having to tab 100 times for screen reader users let's see how this works so if I press tab we're there there there there there there there welcome to syntax welcome to syntax welcome to syntax the state of react 2020 if I press welcome to syntax today we have the state of react 2023 this is a survey from the folks behind State of everything probably state of JS you're

you're familiar with um but they also do a whole bunch of surveys in different areas HTML graphql today we got the results of the state of react which I thought was super interesting that's a lot of tabs to get to the pause button I don't know if that's what they're asking

um yeah so it's not too many tab presses du to the play button that's what we recognize you could tab a few times hit enter the episode starts playing um but it doesn't allow you to then stop the yio yeah so right now what happens if I press this button maybe that's one of the one of the fixes we can make is if I press this button welcome to C welcome to C and then if the episode is already playing this button should actually pause I think that's probably a better at least one thing that we can do and then if you could add a link to jump to the

player yeah I I think I at a minimum we can make it so that pressing this button pauses if it's already playing let's at least add that to our to our list here uh pressing play button when already playing should pause Okay add markdown lint to the project this is already a thing

yeah Scott has been working on that a bunch so I'm not going to worry about that this one specifically add validation checks to markdown and

PRS yeah Scott's been working on this I'm not going to touch it Safari hero pattern grit is too gritty I don't want to look at that um anchors for drop downs isn't pinned I bet I wrote a better Anchor action for a side project this weekend we'll report to syntax okay that's a Scot thing add some padding in the video section so I was browsing through the videos and I noticed it could use a bit of tweak it's kind of tough to read the titles and tell the videos

apart I could agree with that um I mean we could add like an outline or something that's not too crazy

though when browsing your show notes I noticed the bullet marker is peeking to the left it's peing from behind the time stamp is this still a thing

I don't see this at

all um can we change how do I change to light mode guess we don't really have a theme toggle

weird cannot reproduce okay this is what I wanted to look at so quickly navigating to a show by number so this is currently a Thing If you go to syntax. FM and then put in a show number it goes there but if you go to showshow number it says not it just it just redirects here um I think it would be possible to update the redirect so that if it show slash just a number um redirect to the the page without the slug the shirt should index the show number and show a bang on Match when hit in putting a number um make the slug optional yeah

okay make slug optional for SL show slash number

slug accessibility

label okay can't edit AI generated notes this is this is another thing I wanted to to look at so this is an admin thing so behind the scenes for every episode on syntax uh we actually use AI to generate the uh summary here and so this is the summary that appears even on on this page here but that's generated by Ai and right now if AI makes a mistake the only way to update it is in the database so we could have some kind of uh backend admin page that allows you to actually inline edit the uh AI information so like all of this like this right here this needs to be editable from an admin point of

view transcript timestamp progress not updating until we pause

oh okay so uh this has to do with the transcript so if you look at the transcript um it actually updates as the episode plays TA today we have a rendering one about right now about a whole bunch of class if I pause you can see that it now the the little yellow thing updates to be at the correct section but it's basically not happening while the episode is playing that's something else we could look at uh transcript indicator not updating while playing episode okay we got a lot of issues um I'll recount what they are and then we'll just pick one and start working on it so um yeah the repo is available so if

you go to github.com syntax FM website you can take a look and what's up Nico good to see

you and Amir has a question just out of curiosity would you risk and choose Fel kit at work if you were building a mission critical software and your professional reputation was on the line yeah definitely like spel kit is production ready absolutely people are building stuff with product with spel kit and um personally I can build things faster with spell kit than I can with react or nextjs um you could call it a skill issue but spel kit typically requires less code uh things are structured in a more predictable way in my experience so yeah yeah spel kit is definitely production ready um you might run into some issues if you're trying to find certain libraries to do a specific type of thing but yeah spell kit is definitely production ready the last one seems fun to work on yeah any any thoughts in the chat or I'm just G to I might pick the oldest one

because uh that might or or I'm I might make a decision about what's the um um the one that will make the most impact um and poo says spel spell kit is the goat so the first one is uh add the episode duration to so I'm going to read these out and then let me know in the chat if you have suggestions on what I should actually work on so add episode duration to the DB this would involve using some library to look at the MP3 files to get their overall duration and then insert that into the database I would probably write one script that generates the data for all the existing episodes and then another script that gets the duration each time a new episode is published so that's what would be involved with that um this one actually I might even just do this one because it's super simple but it's basically right now when you the the play button that's on the page

if you press it while the episode is already playing it restarts the episode instead of pausing the episode so that's that one uh the next one is right now if you go to show sln number it just redirects to the list of shows but we could actually detect this and then do the same type of redirection as if when someone goes to syntax. fmn number so basically make the slug optional when visiting an episode I think we would still redirect to the slug but it would make it so that you could go to this page um allow editing of AI generated content this would involve probably some form actions where on the admin side when you're on a show edit page page you have the ability to change those generated stuffs inside the database so we'd probably like create a little edit form see the existing AI generated contents and then onsubmit update the database and then this one is when you're

on the episode page we have a transcript with a little uh yellow bar on the le- hand side that's not staying in sync with the playing episode so essentially we would probably just need to tap in with the on progress event of the audio play and when there is some kind of progress update where the um uh transcript indicator is so those are all of them I'm probably I'll probably just work on this first one because it's literally going to be if when you click the button if playing pause instead that's it um poo is saying joking aside SP is the best but spel Kate is on another level the best meta framework out there I want to use kn a little bit it seems like Nu is a pretty good Contender uh for best meta framework all right any other thoughts comments questions oh you're totally right uh over on Twitch uh you said they're using postgress the summary is my squel you're

right it's my SQL it's not it's not postgress uh but let me show you how I get things running

locally so uh in here I have a Docker container and if I do Docker compose up that will create the uh mySQL database and the reddest database and then just start it locally on my machine and then I should be able to spin up the

Chapter 16 1:40:22

Setting up syntax.fm code base

website cool cool so if I do uh pnpm Dev it makes sure the database exists and it's seated and everything else and then it starts up the app so that's a a start script that Scott wrote and then the website load so now the website is running locally on my machine um let me make sure I'm on the main branch I don't have any I do have a code change what is the code

change oh it added the package manager I don't want that okay but let's open this up with VSS

code and then we need to find the player so the first one that I'm going to work on is about the player not pausing

this one

so when we are listening to an episode I sure hope you're hungry that's an old I sure hope you're hungry when I click this button it just restarts the episode I want this button to pause the episode so let's handle that now if I had a guess it's going to be on the show page so uh in spelt kit we have route grouping so these parentheses just make it so that you can like group a bunch of rout together but they don't actually appear in the URL but it should be under site and then for an individual show and then an individual show number so this is the page that gets loaded for an individual show and I'm guessing there's going to be a play button on here somewhere

Chapter 17 1:42:33

Fixing episode play button

um so we have the show notes those get it could be a parent component it might be actually be the layout then um yeah so there's a play show button

here so if we look at the layout of the page we've got the host and the guests they're here so I'm guessing next in the in the template is going to be the actual play button so yeah when this is

um playing we call play Show are there any other references to play show they're not so what I want to do is uh if player.

status how do I determine if it is actually playing and what is player

where's player even coming

from

oh it's a store with player state

it has a status

property apparently it

doesn't I'll have to kind of dive into the code that's handling all of this

um where even was it go to

Definition h

so it's this it's this

function and then we can subscribe to the player

State on Play It's setting the status okay

then we're updating state so basically I think I need to just um expose the status or it's possible that player window status is also just available hidden active oh no that just that has to do with I think the player that's visible on the page

but where is the player State accessed

like this

I think I want to expose the state let's see if let's see what happens if I do that I'm working with spelt stores here this is kind of my first time working with them so I don't know what's going to happen but I'm going to expose the player State on the player object so then I should be able to do player do player State and then let's just see if this gives us the actual State I mean we could just do so if it works current state do status and if that is equal to uh playing then we want to player do

pause else um if the current state status is

initial start it

see if this

works I'm first try so um that gives us access to the state it's playing and then it's paused wash those hands now we're down like down here if uh that pauses it okay um I think another issue that is right now the text is saying uh playing episode it should actually say Play episode so that's not updating on the page accordingly um you know any anytime you add a dependency or up I think we should update it to say like resume episode dependency like what does it say when the the page initially loads uh there is a there is a pause function it's just I need to determine if we should start the show pause it or play it those are already

exposed but based on the status I need to yeah I need to change what function I call uh okay I want to update the text on the page because right now it says uh playing episode number oh uh what what if we go to a different episode it says Play episode

okay I think that's the other thing is uh I don't need to use get if I use dollar sign player right

maybe

not hey andas thank you thank you for the twitch follow uh that I believe is the the creator of uh coolify himself welcome in glad to see you I have dollar sign player. status let's take a look oh I do okay I want to use that because then I can use that in the um in the view as

well H good to see you hress thank you thank you for being here

okay but if this works then that means I already have access to the player status in the template which means I can change the the context contents here see your updated pendency uh you'll get feedback in the P oh that's an issue though okay so um if the show like okay so I'm on a separate page and I actually want to play episode 703 now when I C this button I need to start a different episode um so we need to keep track of

that okay

so if the current show is the actual show number here this is this is all the logic we need otherwise we literally just start it this needs to start a different one so now I'm on episode 703 this should start episode 703 yeah Monday Monday Monday I have to check it out it's it's then that pauses it and then if we go to a different episode this should start this episode Monday Monday on that so you can can focus it so like crazy to have to add all that extra1 properties to a div and not just hey that's great that's great okay uh but what I do want to do

is I want to change the text to say resume if it's already playing do it the other way around uh if it is not just do the online in return yeah no no no it's not back seeding I think you have a good idea here so um we do it like this

so if it does not equal that then we return

this I guess I don't even yeah I don't even need to wrap it and I don't even need a return I could just do this that should handle it the same way

and then I can combine the

two sweet I should do it yeah Andre says um I'm an amateur streamer for the past week no you're a pro Andre you have a lot of people tuning in and yeah you're making it happen yeah much better we we simplified that logic for sure okay let's make sure that it all still works if I'm on a page and not I was playing episod it resumes I can pause it there go to a different episode this should start this episode what versions this should pause it think we're in great uh but the logic that I want now is

um is to change the text here so

right now it says Play episode or playing episode we could say

um if

give me some type completion um maybe it's because my I need to finish out this

paused if we're paused and this is the page that we on of the episode we want to say

resume

else

wait else I always forget this SP syntax um okay so if the player is paused and we're on the same page as the the player that's loaded as the show that's loaded into the player it should say resume otherwise um if it's not playing then it should say I don't know let's let's see what this this gives us okay so yeah this says resume episode 705 you know you were depending on resume great you know install stuff and then change Lo file and they never look at it and they're never really like it restarts now it says resume that's kind of nice beautiful yeah yeah if I work with spel kit long enough the syntax will stick but um uh I just I guess I don't work with

it often enough I've been I've been in react land so I always have to remember Ash colon slash um this is great this is great I think the the one of the main issues we started one of the main reasons we started working on this is if someone is using a screen reader and then tabs to play the episode nice they should be able to pause the episode without having to tab all the way to the player at the bottom a lot because that's that's like the first the other thing is if we refresh um when the page loads um it should say

resume the thing is right now um let's let's look at what the the status is because when the page loads basically it's loading the previous state of um like what the user was listening to

before so it goes initial the first part of accessibilities just and then I want to log this every time it changes yeah so that's what PA was saying put a put a dollar sign there and that'll log it every time it changes okay initial and then loaded use the buil-in htl okay but what if go to a different episode Mond paused loaded I think we should change the status so like if the pl if the user is resuming this episode um the current status instead of loaded should be paused I

believe so um it's fix that first of all we can get rid of um earlier we added a return here this player State we don't need this anymore

um if I go to a different page and refresh it stays initial I think so initial loaded and then an episode I haven't listened to initial and then load it does say

loaded see what load show

does check if it's saved

then resume

time if playback is coming from a time stamp use Tim stamp otherwise use last play position or zero

this is what I think we should do so if play from position um is a thing then it should start off as

paused I sure hope you were hungry

I think so now we've got episode 700 who I'm starving that um it's now paused when I refresh the page I would want this to be in a state of paused I think yeah yeah pal is mentioning check if loaded is you somewhere um

oh I think we're outside of here yeah because then it's loaded that um allows you to connect directly [Music]

so when do the rest of these values get

set if resume time is greater than zero that's probably it uh but this code isn't running so update the state for a new incoming show

um I think this is what's happening as

well so oh did that work wait what

it's setting it to oh okay I think I think that fixed it what poo mention fixed it if the resume time is greater than zero we're setting it to paus that's the way to go yeah so we can say resume episode 700 to your database so there's no middleman cool uh but if I go to an episode that isn't loaded into the player like 699 and then I refresh it should still say Play episode $699 it's not going to say resume episode 699 and then if I press this play you're listening to syntax a really I think we've done it and if I refresh it should say resume episode 699 sweet sweet uh and then we can go to different episode

um if I play this this is currently 699 is loading dat SVG you literally hope you're H uh I think we've done it yeah I think we've done

it yeah we don't need this anymore uh I'll do that yeah that's a good point pal so pal was mentioning confirm that status equals loaded isn't used anywhere cuz it be that it's expecting and waiting on status equals loaded

um don't need

that see everywhere this is used

uh can we trust that it's single quotes Okay we have the status en this is literally the only spot that it's setting

it it's right here

hey Miguel welcome in good to see you thanks for being here uh cool cool I think I think that's it um obviously uh someone will review this before it gets merged in but um I think we're good yeah I think before I do anything else I'll have to pull from

Maine cool and we changed just a couple of things so um if they're reloading the page to the same episode that has already been um that they were listening to the last time they were on the site it should start as paused and then over here um we resume the episode if the button is pressed and they been listening to it cool we're going to get rid of that

though the other thing is we update the text so um if it's paused and we're on the same page as the one loaded into the player it'll say resume otherwise it'll say play or playing

oh I think we're good let check out a new

Branch um player State updates are they State updates

sure so we'll call this a bug uh fix player button press when playing

right I'm going to open up a PR um I guess I should look at the existing PRS yeah this is all not related stuff and this is related to number 1384

thank you Pao I'm G to toggle that a couple hundred times the next few days so your PM will see that and know that the change was impactful apprciate it h what's up Migo I practice my English by listening to your videos thanks for sharing your content very quality thank you Migo yeah and that's awesome that you're practicing glad to hear it um okay this fixes the player button on the show page it will say if an episode is already playing pressing the play button will pause the episode if the episode episod is paused the button will say resume um if

the site is loaded from scratch or like is freshly loaded to a show page that the user was last listening to the button will say resume that's it right um

people pressing the button will pause the episode

people

were this is a partial fix for number 1384 I did not add any to jump to the player just improved the but button functionality

cool great uh I'll create the pool request and then add Scott as a reviewer because he he mainly worked on the the player so he'll know if I did something completely wrong all right let me pick one more issue to work on and then I'm going to get out of here so before we pick an issue I'm going to check out the main branch just so so I'm in a fresh State and let's pick another issue so this one done um episode duration would be quite involved because we'd have to like download a bunch of mp3s and then process them uh we have integration tests um but some of them are broken so I haven't really touched them um but the stack here is spelt kit with a mySQL database

and uh Prisma we talking to the database

um allowing edit of AI generated content would be really nice to have because this is something we deal with a lot let me me research this one let's actually look in the code base in the admin dashboard first of all let me see if I can log in as admin locally because I know that I've actually had some issues with that if I don't have my stuff set up right so if I go to admin now if I go to admin okay I can access the admin stuff um what we want is we if we go in here I want to be able to edit the AI show

Chapter 18 2:16:25

Adding edit form for AI show notes

notes this should be easy enough I could literally just add a form uh with the title and the description and then we updated in the DB let's do it that's what we're going to

do okay so first of all I need to find this page in the code base it's uh under admin shows and then show number so go admin shows show number and then let's take a look at the page and then we have a a data dump component so it just shows everything from the database yeah so simple I think uh all I want is to insert into the page um right here there should be a little form for editing so if we look um

AI show notes yeah so right right below the header I want a form and um let me copy a form from another place on the site um feel like we had one

I'm also going to do this so that way it um all the latest

shows I'll just add a form and see what it looks like so we have a form got a label that says uh title and then we've got an input the name of title what does that look like I mean that's not horrible that's literally all we need it's going to be totally fine so I'll do the same thing but with like a text

area uh for

description easy um let me just turn this into a flex box are we using

Tailwind uh we're not using Tailwind but we do have just Styles in this component here so I'm add a style tag we're going to call this just the form I guess I can Target it directly because these are scoped and we will say that it is a FX box Flex it via column and also add a gap of Two Rim not bad um I kind of want to put the labels on top of the inputs so let's link them up

hey that's not horrible um yeah it's not bad this is literally for admins only so it doesn't have that doesn't have to look amazing uh let's give this three

rows four rows cool and um let's increase the font size because this this looks really tiny for whatever reason it's not horrible no this is fine um I'm going to reduce the

Gap have no Gap at all have a gap of

0.25 cool um now uh if there are show notes I will show this form and then we're going to prepopulate the input with the

title I

don't description cool so by default it doesn't show up there but if we go uh to an episode that has AI show notes we should see a little form like this

great um let's add a

button says update let's add some margin around the form [Music] um one rim on top and bottom like that yeah so now I should be able to go in and just change some stuff and then when I click update it's going to submit a form post request will update it in in the DB so um at this point I need some actions for this specific route and I forget how all that works so let's go look up spell kit and we're going to look up form actions uh so we are going to have a p we have a page. server already it has a load function so this is what gets called when the page loads uh but we want to

export um actions and this is specifically going to get called um with the data from the form so it'll be the default action and so we can literally just set method to post on our form that will submit to that action and we should have all the data we can put it updated in the database um here I'm just going to log the event dot form data I know that we have a middleware for processing form data um we look over here

it's this what does does this add to locals

this is how we do it request. form data we can grab the request from

here okay zip now um it's actually it's going to log on the server side so when I click update it posts against itself and then on the server side there's the data now we just got to put it in the database so uh we can do

this um we grab the locals from here and then we will grab the AI show note and we will update [Music] where um show ID show number is let's grab the pams like this

I forget about yeah Prisma has a data property and we're going to say uh title is data. tile and description is data. description okay um oh it's is it just form data do I have to use get oh no that's not fun what is I thought Scott's middleware did

something yeah oh yeah and I missed your chats earlier but uh um see you later anjos just want to say hi yeah thanks for being here and thank you Wesley coder thank you for that that follow yeah this SK form data how is this supposed to

work locals. form data that's what I want

and then I want a partial of uh AI show note I don't really want to partial I can do a

pick and we want um title and description can you not can I not pick

multiple any any typescript ex experts in the chat what's the best way to do this I could have a type that's uh AI show note update and right now what I'm thinking is I do pick the title and then combine that with pick the description and then that gives me a tit that has a title and a

description so data do title and description um I specifically want the type from here I mean there they are literally just strings I mean the the way to do this actually could just be Define my own type so title is a string and description is a string but this is basically using the type from the database pick can accept a union ah good call okay let's do that thank you uh webd journey and Wesley H that's the way to go nice cool um

is this all we need I think this is literally all we need I think we probably want to validate that title and description aren't empty the thing is this is an admin endpoint so only admin you can't get even get to this endpoint unless you you're already logged in uh we might as well validate it though I do we have Zod in this code base

um let's see if we have Zod I don't think we

do yeah we don't have Zod I'm just going to do a manual validation so if there is no title um I need to to see how we can return errors from

forms you just return an object you can have a fail return

fail with whatever we want

so fail comes from spell kit and we'll just say uh message AI title cannot be

empty we'll do the same thing for description

otherwise we just

return okay here we go we've got this we come down here and we say this was updated we update it and now we come down here and it was updated so spell kit is is so easy um I mean it is literally just a form submission am I missing anything I think like we should get a a validation error we're not even we're not really even using the form info on the the front end um because we should I think we get access to data

dot do we get access to form data no how do we how do we get the form info in the page

action

data and then I guess we can show an error message at the top here so if form Dot message how do I access

it and I put it in a variable is it

running form is possibly null makes sense

okay now if I try to do it with an empty description it should say description cannot be empty um I guess one issue right now is um it needs to scroll back down to this um I don't think I'm going to get too fancy with it like toast or anything like that because like I said this is just for admins

um but I kind of want to scroll back down to this area yeah scroll to the element so I think here's what we'll do uh if the the form exists like if there was a form submission we're going to scroll to the form uh so I need to I need a ref to the form so then I can scroll it into view um how do we do element refs I could guess I think we could say something like uh find this equals form ref and then we just Define form ref and this is an HTML form element or

null and then

um let say if form we'll wait for it so if form is a thing then we're going to scroll into

view hey that

works that's too easy uh we can make it smooth though Behavior smooth

nice okay I guess the other thing is we can set the error message just give it some

style think we have some CSS variables uh import

variables we've got

red let's just try red

one and then and I will give this a class of

errors all

right do I not have access to that variable shouldn't it already be loaded on the page

um or do I actually have to import the variables maybe because it's scoped is this used anywhere else on the

site

no um I think maybe I have to import the variables

can we do a base

import all right how deep are we we go up one two three 4 five

it is applying it and it was able to look it up does it not have access to Red Hue it

does it's slightly red oh you know what you're completely right uh I guess we want like really red I this this is the one issue with uh working with CSS variables is actually don't know what V variables are available to me there might be like a I don't even know if there is a vs code plugin that can basically tell me like hey there's red 10 um ah and then it's really dark I don't know which one I want look uh I guess Scott can yell at me I'm I'm just going to use this it is it is red like that that's all I want cool um I think one thing is when you update the AI show notes we need to

clear the um clear the cash for this specific episode so that they get updated so I'm going to do that as well um cash there's some caching stuff that goes on just want to call that method

drop show cash I think is what I

want and then pass in the show number

okay uh andac thank you for that follow you'll probably say that you should have used the color mix uh that's good at least I'll get some feedback on my PR then okay so we remove from the cache the other thing is though we need to invalidate this path from versel um how do we do

that cause those load functions to rerun um because I think well like one of the things that I have noticed if I go into the database and update this directly and I remove the cache the page itself doesn't update I think it might have something to do with versel like build cache I don't know if there's a built-in way to um clear that

h i don't want load functions belonging to the currently active page I don't want that I I kind of want to deleted in the versell cache if I can maybe I won't be able to but what's up what's up P welcome in good to see you okay um I think this should do it this is probably enough uh let's make sure that that this works so for instance um if we go to episode uh 650 we can see that the AI show note is discuss discussion on why Facebook and Instagram is messy okay uh but if we go to the admin page

or episode

650 go here we can say this was updated we update it great and now if I refresh this hey it works cool that's it that's all that's all I'm going to do for this one like I said it's it's an admin page it doesn't have to be too crazy um yeah

Co so check out a branch add

everything push it up open a PR

cool this adds page to the um admin this adds a form the admin show page that allows updating of AI generated title and description

great um we'll request a review from Wes and Scott

f is that

one should Auto link to it yeah cool all right um I guess we can see did I get any responses on my pool

requests no not yet cool but that's all I'm going to work on for today um next time we can work on other things too so I'll keep this list here and uh I guess as as more issues get added to the syntax repo as well I can pick those to work on but thanks everyone for hanging out this was pretty fun we got to do some code Wars code catas we answered some questions um we got some work done on the actual syntax website which was fun and I think this was from earlier oh oh no so uh what Stony is mentioning is if you have FF probe which I think comes with FF imp Peg you can use it to grab the duration yeah I think the main issue is like if this is running as a KRON job that's a serverless function I don't know if it has access to FFM Peg or FF probe yeah and thank you webd Journey who says you're a legend I appreciate that but yeah thanks everyone for tuning

Chapter 19 2:48:31

Thanks!

in uh if you are not following over on Twitch uh be sure to because we stream in both spots and all the follows helps and if you're not following on YouTube we'd appreciate that as well so check us out over there I'm going to head out uh like I said this is a bonly thing so not next Thursday but the following Thursday there may not be a live stream because it's hack week so maybe you might not see me for another month uh but hopefully we can do another one of these soon I do appreciate everybody for hanging out it was a good time yeah see you later Stony who says catch you soon all right thanks everyone for tuning in uh yeah we'll see you next time

[Music]

hey hey

[Music] hey

[Music]

Quote it with credit and a link. Quoting Syntax