Transcript
Looking for something specific? Feel free to search the page for keywords to find a specific portion of the live stream that interests you most.
hey how's it going everybody my name is tom and welcome to our stream all right so today we're going to be uh
kind of adding in the ability to like and unlike comments on the new jager
site which the name will be changing here briefly as well so essentially if i dive into a lesson
here and i scroll on down to the comments uh note that i am logged in here as
myself and we take a look at the comments i've added this little heart icon here and the goal for today is to
rig this up to a like record in our database
anytime a user clicks on this and then should they click it again it would essentially do an unlike uh not a not a
dislike but an unlike so they can like the comment and then they can undo their like if they wish
and then we'll have a little count right here as well along with the comment so
uh that's what we're gonna be doing today uh so let's go ahead and just dive right in so to start with here
um oh boy i don't quite remember where i left off with this i know that if i
click on it i have that bound up to a handler that tells me what that particular comments id is
but it's not kicking anything out so let's go see whether or not i have anything in the database
for this um not quite sure
seals that just oh i did not mean to close at all i meant to okay there we go so let's
open up the local one here and let's see whether or not we have any comment votes okay i called it comment
votes so all we want to do is whenever the button's clicked for the first time for
the user let's plop a record in here with that comment id and the user's id and then should they click it again we
just delete that record out so we have this rigged up here i believe this is in
the base so you have the list of comments here
that button is let's see it would be down here along
with the actions oh yes right here so we have a heart
outline and just the heart so the heart outline is whenever it's not quite liked yet and then the heart would be the
filled in heart which would be whenever they have liked it so this is inside of a button [Music]
um and it looks like i have one
showing on hover what do we got going on here btn outline
absolutely top left okay
not quite sure what i have going on display wise with that but uh we'll take a look at here in a second main note is
we have this toggle like and that's what's kicking out to this alpine function within the comment base right
here so we need to rig this up to an api endpoint
um so let's first head into routes and then underneath the api section
let's go ahead and do kind of similar to this history
progression toggle let's go ahead and do a route post
comments um like
toggle and let's see we probably don't have a controller for this we could put it
inside of the comments controller let's see how filled up that is store update destroy
yeah we can do a a like function in there can always move it out
so that's comments controller and we'll call that like
as comments like
so we give that a save and that full url will be api comments like toggle
and let's come back into our comments controller and let's go ahead and get that set up so
public async let's see we call that like
http context contract okay so
we're going to want the response so that we can respond via
an api endpoint we're going to want off and
actually we want params as well so let's go back into that route definition
and instead of that let's do
id and uh like instead of that so do that
and then like itself will just be a toggle so
first and foremost here we will want to find the
comment i believe hang on a minute do i have that
uh with a off middleware yeah so the whole api prefix is off
middleware protected so that's good all right with that in mind now we are good to go
ahead and break this up so const comment equals i'm trying to think of
whether or not we even need the comment record or whether or not we can just plop this right on the
user comment votes user
uh well actually do i have that relationship set up i guess that would be step one wouldn't it
so let's take a look at the comment and let's see if we have anything in here
user votes okay so yes i do have it rigged up as user votes
and let's check the user because if it's in the user then we can easily just attach it to the user this way
since we already have the auth user it doesn't look like i filled this site in
got email history posts
hash no it's not in here so let's add that in here so it's going to be a minute of mini
i can probably just copy and paste it from this side and flip it so
okay let's copy you paste it there okay that's going to be
oh nope not cop yeah yeah comment
did that not import yeah it should have it's in there
um all right oh yeah i didn't change the other half of it now did i so right here
change that to comment and there we go okay so now that we have that in place let's
head back into our controller and we don't need to query the record um the only reason we might want to do
that is to verify that the comment or the uh yeah that the comment exists
um so that would be one benefit of doing it we could also just use the validator for
that um but let's go ahead and just get this
rigged up for right now so i keep wanting to do constant let's see off user
related and we should have user oh yeah no that name should change
because that's not specific to the comments so user uh to be comment votes there we go
so comment votes dot and then that should be attached
doesn't seem to want to update related comment oh i have an s on it oops
there we go votes dot attach
and then we should be able to do uh params.id right in there
and we await that and if that's good then we should be good to go ahead and return
response i'll do a status of 20204
well let's go ahead and return back whether or not it actually was liked
yeah so this is the toggle so yeah so let's go ahead and uh we let's go ahead and check for that record first
so const
let's see here
exists equals await off dot user dot
related comment votes
query dot i feel like i'm making this more difficult than it needs to be let's see
here where
yes let's inverse that i think it would be cleaner so const
exists equals await comment
dot and then we could also do the whether or not the comment itself exists this way
as well so comment dot query dot
let's see where has
user votes
now this still feels more difficult than it needs to be what am i missing
um
off user all right const a weight
user dot
related
comment votes dot
query dot where
id equals params.id yeah i think it was just the off dot
user that was throwing me off because most of the time i have my user plucked out um but yeah all right so there we go
so if exists
then we will want to remove it otherwise we'll want to create
it we could do this as a ternary
let's see so exists
await author i'm trying to do that again user dot
related comment votes dot
uh
took me way too long i'm going to blame it i'm being on it
being a friday evening um all right so otherwise we want to create it with
the [Music] see we're doing auth related so that would be the uh actual uh
not post id but
should be the comment id shouldn't
oh no not create sorry attach
params.id okay my goodness okay
so did exist or was removed or yeah was
removed so let's see what we get there
all right oh yeah now we need to bring it up on the front inside too let's go ahead in there and do that so
[Music] that was in the base
all right so const data equals
weight let's make this a sink
next year's post let's see we're going to need that to be
api comments comment id
like yeah yeah i don't know okay i don't know
why i made that a post we're not really posting anything else up um but all right
and then let's just console.log the data that we get back there for tests
all right so let's hit it
a 500 error that's what i always like to see what four
select comments comment votes user id as pivot user id
where's the end of it my id is ambiguous
all right what id would that be right here
so that would be
comments dot id right
i think let's try that again so let's clear that out click it
nope still getting the same thing or nope
this looks different does not exist relation comment votes does not ignore
column zero of relation
all right so this one's on the insert so query's good insert not good
all right let me read that one more time
is it coming up with a zero
it's got one two three it should just be two shouldn't it common id and user id
did i not make that id correctly on there should be
made this a while ago so who knows comment votes
right yeah table dot increments
okay looks right um
maybe the model no that should be right so pivot table get out there
comment votes that's mini user user here's over here
all right
oh man
oh okay all right i think i just needed that to be an array which is weird because i think
that specifies that it accepts either an array or
string number oh okay my bad i read that wrong yeah so array there
okay so that seems to be working we can go ahead into the database and verify that
cool okay user id one comment three
so that was my bad for being away from adonis for a little bit uh brain fart there so
now what we need to do is to get that to reflect on this side um and if i refresh
i don't even know if it would show so let me see it was the second comment down that i clicked on
so if we refresh here scroll all the way
down to that comment you can see it's showing up just the same as before despite it actually being liked one of
the issues is i bet i'm not querying it at all with comments so there's really nothing to even look
for right now so let's go ahead and go do that so that would i would want am i on here lessons all
right i don't know why i have these open
all right so lessons controller show
comments get for post you want to dive into there
all right
and it should just be a matter of preload
or well no that would be what so yeah no you do want to pre-load it specific to
this user but then you also want to get a count so user votes
query dot where and at this point we need the auth user
id as well so user id number
or null and
at that point we only want to preload this if we have the user so if
user id query and this will get a little messy so we'll
break this down here query dot where do we break it down at let's do it
here like so there we go where
user id or user votes
user
all right and we've already done our check up here to check and make sure that that exists so we know that that will be a number so
i'm just going to enforce that ah sorry
um getting my languages mixed up now shoot
was that not right number no
[Music] okay there we go
so i feel like this might be wrong i'm having a brain fart right now i can't quite think through what that
appropriate column selection might be it could be users.id like we had on the inverse side
over on the comments controller where we had the users related yeah i bet you it would be
so all right i had a feeling it might be so i'm gonna roll with that for right now so this is going to get the user
votes if this particular authenticated user
has voted on this particular comment otherwise this will just be null
so on the front side we want to check whether or not this exists if it does then we know the user has liked this
particular comment next what we want to do is actually get the
user votes as a whole the count of it for that particular comment so
instead of pre-loading for that we will do i believe there's a with count
yes and that should be as simple as just calling user votes
so we'll save that okay and so now we should at least have
something coming through on the front-end side to be able to select that particular comment
and we refreshed here to make sure everything's still working okay so let's jump back into our front end here and this will be
on our comment list and right here so i'm gonna make this
simple before we find out what is there let's go ahead and just
drop our count for the user likes on here so it'll be
comment dot extras dot and
user votes count i think
undefined so no that was not quite right um all right
let's go ahead and just drop it out so let's inspect comment dot extras and see exactly what
it is all right
user votes underscore count my bad okay
there we go so now we should at least get the fact that this one here has a one and whoa
okay so that was not unique to that particular comment whoops i don't want to print
all right so let's head back into here let's try to fix that so right in here
related post related comments query
why isn't that unique to that
interesting
because state id would just be applicable per
comment
hmm that is peculiar if i like a different comment will it
increment them all let's go down to this one it's like that one
refresh and see or without this changed to no it's still one so it's particular to this one still but
it's applying them to all it looks like i don't know whether or
not that's right or not um oh shoot what's that one thing that you need to call to scope things
like um like uh shoot
see what was that called
group yeah i think it was a group let's give see if there's a group version of that
no there is not okay group by but that would not be it
so um
that's i feel like that's weird
i don't know if it's me having a brain fart or if that is weird
okay tell you what let's go ahead
let's take this guy off for right now let's comment that out let's preload all user
votes all right for the particular comment and what we'll do
is see what count we're getting back for that so instead of doing this
i'm going to go ahead and comment that out and instead we'll do comment dot
uservotes.length let's see what that's coming back as i would imagine it should be zero one and that's correct
okay and then down here at the bottom it would be one again yeah so okay
this might be that bug i saw month or two ago it was i think it had
something to do with counting so it could be that in which case we'll find a work
around um
i forget can you alias these oops as
votes no okay i i wasn't quite sure
does it accept an alias no okay
so what i was looking for was to see whether or not the user vote here would be for the particular user but if it's
there we can do that on the front side so i'm just going to leave this like this for right now and
see if there's anything else that would be beneficial for that
having
yeah yeah we could just do aware exists no no no sorry
it would be like a
equimper asked do you like edge or using something like
react or do you like edge more or using something like react um
i tend to like to keep things on the servers like simplicity is nice for me um
if i don't need a whole bunch of stuff going on i like to just stick with the basics stick with edge
um if i need a bunch of you know
front end updates happening so that the user can immediately see stuff that's where i reach for react i tend not to
reach well i reach for view but i tend not to reach for a front end style package unless i absolutely need
to so if i don't i like to stick with edge it's the same focus at work i don't
get to work with adonis or anything like that um um at my day job i work with c sharp
so stick with that whenever i can if i if it you know
it doesn't quite do as much updating as i need it to do so like a simple blog like this or
something you know where most of the stuff is static you know you might update these every once in a while once
a user performs an action that's easy to do with just plain javascript that you don't really need to
reach for anything you know you attack alpine on it makes it even easier
but if i have like a ton of micro interactions within an application where
i need to pull stuff from the database uh after user interacts with a small thing update that little small thing and
i have that all over the place that's where i reach for something like viewer react so yeah
i don't like to add a whole lot of complexity to the stack if it's not needed so
um sorry that was a tangent let's see
okay so we need to find a workaround i guess
i asked you feel edges as good as blade or missing element
i saw the async and edge but not sure what this thing does
i like inertia.js lately um it's been forever since i've used blade so i really can't comment on that i do
like edge though i like i use comments or comments um components heavily
yes you can await things inside of edge that will work just fine
asynchronously loading things you can extend additional items via the database with
an edge there's a lot of availability of things that you can do within there um
inertia is what i i enjoy inertia too but again i don't reach for that unless i have a ton
of micro interactions going on within my user interface
so like i do have an application that i'm working on beside this where i am using inertia but it has
so if i touch a small button a particular row needs to update but everything else needs to stay in place
things like that it becomes a lot easier to use a
a javascript a front-end javascript library so that you can easily do that
instead of refreshing the whole page and things of that nature um so
ah all right uh equipper asked again but what async will
change in edge this will show the page and then start to fetch no so edge is
strictly back end so if i you know await something in edge it's to
load additional data for that first render so once i hit refresh here
once that page loads edge is done i can do an axios or an asynchronous
request for an additional html input from edge but
once that initial load is finished loading edge is done everything else is passed on to the
client side so if i click on reply here that's done with client side javascript not edge
click on cancel that again that so there's a separation between what edge is and what something like view is
alright so
i feel like there was a way to rename preloads but i don't quite remember it
there might might not be either relation callback
all right
so what we can do actually is take this and
i think we can drag that right onto the comment actually
well no because then we would need to add have that within here wouldn't we um
yeah because i was thinking on query you'd be able to do something like that but you
would need to know the id within the model
okay
let's see i'm gonna try to search for that
i i feel i don't know half of me says that you can and half me says you can't
um change the name of a preload here probably just recreate another preload specifically for this
because then that's specifically going to look oh wait no yeah no it's in the yeah
it's not in the name it's in the callback
um so let's do that as a user vote i guess i i had a feeling that there was a way to do it it just wasn't coming to
mind there we go so now we should have them both we can simplify this one since we
really only want the length but for some reason counts not coming back quite as i wanted to
um let's do query query dot select and let's just select the id let's simplify
that query a lot the result of that a lot easier so there we go so we have that let's go down into
the list well way too far down okay should be good to go ahead and no
won't be using that quite right now leave this as just a length check
and then take a look at the
see what i'm doing here so i it's definitely got something going on where i'm showing the whole thing is on hover
ah devtools doesn't want to work there we go
all right so we got the button div and so both
show
oh i did specific javascript or job uh css
for it so let me go find that uh because then i'll probably also want to tack on like an active
make it red something like that so let's go into the css that is called
btn filled and btn outline oops okay
so btn filled oh that's just okay
just the opacity so
on here we probably also want to btn hover phil
hover where the heck was my head at whenever i
wrote this btn hover fill oh okay
uh a month ago me what were you thinking or no that
sorry this was probably more like three months ago um let's see so all right
ptm filled
ptn over that weirdest name ever for that btm hover
filled dot active
opacity one and let's go ahead and get that make that red so color
[Music] what is that theme colors dot
no it's not coming up right i thought it was colors
maybe i don't have auto okay there they are they're coming through now see 700
and then over here make this back ticks
and let's see over here let's do
a um let's see comment
dot user like or no user vote
man my head was all over the place whenever i initially wrote this if i was doing it again now um i would
have named that table user or comment likes and this would have been user like
i don't know whatever i'm not gonna i'm not gonna
argue with uh three month ago me so let's see here comment user vote
and whether or not that exists so if that exists then we will go ahead and do active well so then we just do it
and in okay something like that
all right doesn't look like it's red and it doesn't look like it's filled in so let's see why
notice that the firefox devtools have been doing that anytime you have like an automated refresh the the element tree
just crashes
so it's coming through as undefined um
so let's see whether or not we are getting our user vote in there so let's do oh outside of the button so that it's
at least a little bit readable inspect
comment user vote see what we're getting here
guessing it would probably be undefined wouldn't it yep okay
let's verify that that's actually what i called it here it has user vote okay
so let's go ahead and just inspect the whole comment and see maybe that went into the extras or something like that
um i've never tried renaming a preload before so i'm not 100 certain
how that works so let's see all right
here's our attributes updated at original
preloads here's our user
user votes and no
no it did not go on
okay well so that's no biggie um
maybe that doesn't work i thought it would but i'm not certain
we actually don't need that query um
oh i never passed in the user id that could be why i added it but i never actually passed it in
all right i'm oi
let's see so where uh that's lessons controller
there we go all right auth user
id and
sorry um user id number or null so that's not the
undefined is what i meant to right there
there we go that should be correct now there we go so let's see whether or not
it's coming through now all right
user votes scroll up a little bit let's see whether or not it went in above here
looks like i'm on the user now yeah
okay well regardless we don't need that one um what we can do is an alternative since
we have all of the um all of the user or yeah all of the votes
for the particular comment preloaded um so if we go down to the bottom here
oh it was up there just showing oh no wait
user original user votes is
empty array okay so yeah i bet you that's messing with it so let's go ahead and just undo what we just did there
come back in here it was worth a shot i wasn't certain whether or not that was a thing
so let's get rid of this because we actually don't really need that we don't need to do an extra query for this
and so what we can do is we can just check it directly on the user votes so
if we come back down to our list here i'm gonna go ahead and refresh it again just so that i can see exactly what i need to grab
oh shoot i bet you i'm on the wrong one
yep you sure are all right well i might have been looking at the wrong one all along
all right let me try this one more time sorry
okay let's try this one more time let me make sure i'm looking at the right one this time too
all right all right so there's one
and this is the one so let's see whether or not it's in here
alright so here's our preloads there's our user user votes
and yeah so but regardless what i was saying before the reason why we don't need that is
because our user votes still despite us selecting just the id does come with those extras
and that's going to come with the pivot comment id and the pivot user id so what we need to do is really check to see
whether or not we have any user votes with the user id pivot user id present and if we
do then that means that the user liked it so
let's go ahead and get rid let's undo right no i guess redo
to get back to here come back over here and i'll undo that
and then come back into our list.edge
and we can make this a little bit cleaner for ourselves by going up to here
set a variable called user liked
oh indentation messed up between switching between um webstorm and this
i don't like that the each is on the same level all of the sets all right um so
comment dot user votes dot
find where v dot
extras dot pivot user id
equals off user id
and so now we can use our user liked
come down to i think i went past it yep right here
and instead of checking this
just checked whether or not user liked is there all right i'm seeing some red now
so we have not liked liked not liked and liked so that seems absolutely
correct um let's test our unlike so let's go ahead click again
looks like the console rigged up some errors for me got 500 so that is not quite working
delete from comments where id oops yeah no i don't want to delete the
comment i wrote that up wrong my bad
uh service no not service sorry controller
show no comments controller diesel yeah wow okay
so yeah so that would be
we can either go off of the user again or we could
so let's do user dot related comment votes
dot query where
comments dot id is params id
and delete it out give that a save
refresh or i guess i didn't need to refresh but all right did that anyway
click that again and all right now we're getting the 500 again
sorry ah
detach i don't know what the heck i'm doing all right there we go my goodness
remind me to stop streaming on friday nights and do it saturdays
all right so here we go so we got back a succ a successful response
so if i refresh that red heart should now be back to a gray with a zero count
should have picked a shorter article there we go it's back to that um
yeah so cool so now the next thing to do would be to get the front end here to immediately respond to that so if we
click it it should immediately go to red um
and that's only once we get a successful response so let's see here so being within our
i guess the best way to do that is to well no so i'm not quite looping through
them with um alpine doesn't actually know what the
comment itself is does it form list the list itself is all done in
yeah edge loops through the comments
so alpine doesn't know anything about that um so really all we need to do is just
directly reach for this particular
btn filled under the comment so on the comment itself right here oh i did give
it an id cool comment id so let's jump back into the base
and let's do a well so
what what was the variable i returned back from that api endpoint was it was deleted was removed okay
so if data dot was removed then what we want to do is
remove the active class otherwise we want to add the active
class and that's really all we need to do so let's first go ahead and grab the
comments element so we could do
document dot query selector
comment comment id dot and i cannot remember what it's called
heart filled filled btm filled my goodness okay
all right and then like icon dot
class list dot add or no sorry remove active
all right otherwise i should add it so here we go
okay so if we hit this all right it's red
uh we also need to increment the count click that bam it's gone all right
so let's do the count as well
let's jump back up to here where is my count here it is so let's put you inside of a span
just call it give it a class of bdn count and drop that back into there come back
over here
i know what did i call that you called that
you can do it btn count ah i think that's right
all right and don't forget the text l on the bottom of the back of these all right so
if we're removing then we want to do count l
um dot
text content equals
number count l dot text content
minus one uh count l dot
text content equals number count l dot
there's definitely a cleaner way to write this but we'll stick with it
for now okay and if we click not a number exactly what i wanted
perfect all right so
let me see what it's getting is text content here so
see what the count l is as well as the text content
oh it's got the new lines in it okay that's uh yeah that's the thing
all right that's easy fix
scroll down scroll down scroll down here we are not a number still
telling me zero is not a number
hmm
that's interesting
context oh my gosh
all right it's going to say 0 is definitely a number
there we go okay so now that we have one like there
and we know that this particular user can't you know like this
comment again let's make sure that the account is actually per user so let's go ahead and sign out
sign in as somebody different uh
just use the github account i guess
okay let's see it was this one scroll down
okay so no longer identifying that i liked it but that somebody has liked it being
that this is not red but it is gray and hollowed out meaning that i have not liked this with this account yet but it
does have the one signifying that somebody else has liked it and so if i click it again this should
go back for this user this should go to red and the count should go to two and there we go and if i click it again
it should go down to one and the heart should go boom cool
same thing with the other one cool and same thing with one that isn't
that one cool
hey tina thanks for stopping by uh enjoy your time off work happy friday
um so really the last thing that i would want to do is to get that
border off of there is that a border or make it a dark yeah that's some kind of a border looks
like it's going in and out like it goes away whenever however oh that's the um
btn outline
[Music] apologies about the car
uh so all right let's clean that up a little bit
and let's head in over here so outline hover i'd say don't get rid of
that because that looks a little funky and instead of doing that let's just um uh
let's just keep it there
and instead of adding the active class to this
let's add it to the parent
that way we can do i just removed it btn hover fill
active dot btn outline i think it was color
theme colors dot red 700
okay so now that border should just turn to red whenever you like it
oh right shoot you gotta switch the selector is it a base that was in base
all right so like icon l
dang man what was that called btn hover fill the uh weirdest name i think i've ever
given to a button i don't i don't really know where my head was at whenever i wrote that
but uh all right so boom back to that and there's no more
weird border action going on whenever i hover and unhover so that looks good to me so we've got
this working we have verified that it is unique per user didn't quite get it working
as i'd hoped but we got it working nonetheless so that's all that matters
i think this might be the last time i stream on a friday night [Laughter] uh it's kind of hard
your brain just wants to shut down but uh we're supposed to get some bad storms
tomorrow so i wanted to stream today instead of tomorrow because you never know
you never know um but uh yeah so we got this working i think tomorrow i'm going to actually
work on getting the site uh well it already is deployed so uh
it's already at its new domain but i think i'm going to work on actually making it the primary domain and
getting jaeger switched over to it and changing all of the names and all that fun stuff so thank you all so much for
watching i will see you all in the next one and hopefully with a different name
than jaeger and on a brand new site which we're looking at right now so long as i don't find any issues
between now and when i go to release it so alrighty have a good weekend everybody
Join The Discussion! (8 Comments)
Please sign in or sign up for free to join in on the dicussion.
farhan-reizha
other than using .preload("commentVotes") is there any other way?
Please sign in or sign up for free to reply
tomgobich
Yes, but it depends on what solution you're looking for.
There's the direct alternative to preload, called load which can be used on a model instances:
You can also reach through the comment to grab the votes directly:
If you always want the votes to be preloaded anytime you query the comments, you could use a third-party package by Melchyore call "adonis-auto-preload".
Please sign in or sign up for free to reply
farhan-reizha
i see, but if i want to grab just commentVotes count only, how is that?
Please sign in or sign up for free to reply
tomgobich
Ah okay, you can do that one of the following ways
I'm sure there are other ways as well, but this is what immediately comes to mind and should give you an idea.
Here's a link to the with count documentation
Here's a link to the count documentation
Please sign in or sign up for free to reply
farhan-reizha
means if using withCount it should use $extras?
Please sign in or sign up for free to reply
tomgobich
Yeah, if you use
withCount
the counted value will be placed on the model's$extras
property.Please sign in or sign up for free to reply
farhan-reizha
ok, thanks for answering me, because I just tried this framework just a few weeks ago.
Please sign in or sign up for free to reply
tomgobich
Anytime!! Hope you're enjoying it! 😊
Please sign in or sign up for free to reply