1 00:00:01,030 --> 00:00:02,760 There's just two more components we need to create. 2 00:00:02,770 --> 00:00:07,030 Very quickly we're gonna get through these rather fast because they're really the same as two components 3 00:00:07,030 --> 00:00:10,840 we put together before we're gonna make common create and comment list. 4 00:00:10,870 --> 00:00:11,700 Let's get to it. 5 00:00:11,710 --> 00:00:16,370 Well first start with come and create back in said my editor for The react project. 6 00:00:16,470 --> 00:00:23,850 I'm gonna make a new file inside the RC directory called comment create dot J.S. inside of here will 7 00:00:23,860 --> 00:00:32,750 import react right away and then create a new component and export it like so. 8 00:00:33,120 --> 00:00:38,520 And right now I'll just return once again an empty div. 9 00:00:38,520 --> 00:00:42,810 Now let's think about what comment create really needs to do come and create is going to represent this 10 00:00:42,810 --> 00:00:44,130 form right here. 11 00:00:44,130 --> 00:00:48,050 So user is going to type in some content for the comment and then click on submit. 12 00:00:48,090 --> 00:00:53,550 The expectation is that we should take whatever content the user enters and make a post request off 13 00:00:53,550 --> 00:00:55,190 to our comment service. 14 00:00:55,240 --> 00:01:00,090 Now one thing I want to point out in particular is that whenever we create a comment we have to know 15 00:01:00,120 --> 00:01:04,190 the I.D. of the post that we are associating that comment with. 16 00:01:04,200 --> 00:01:10,080 So inside of our comment create component we need to know the post that this comment is going to be 17 00:01:10,080 --> 00:01:11,370 associated with. 18 00:01:11,460 --> 00:01:19,570 So long story short whenever our posts list or a post inside a post list is going to create a comment 19 00:01:19,570 --> 00:01:24,530 create component we need to pass down the idea of the post that it's going to be associated with. 20 00:01:24,550 --> 00:01:26,280 That's the long and short of it. 21 00:01:26,290 --> 00:01:30,550 So inside of comment create we're going to assume that this thing is going to receive a prop so we're 22 00:01:30,550 --> 00:01:32,890 going to receive some props right here. 23 00:01:32,890 --> 00:01:36,820 That's going to contain something like post I.D. or something very similar. 24 00:01:36,880 --> 00:01:39,550 I don't really expect to receive any other props inside this thing. 25 00:01:39,670 --> 00:01:42,150 So I got a D structure out just this post. 26 00:01:42,150 --> 00:01:48,510 I do value OK so now I can make you supposed idea just yet but I just want to talk about it right away. 27 00:01:48,510 --> 00:01:49,830 Just make sure it's really clear. 28 00:01:49,860 --> 00:01:54,920 Yeah we needed to know the idea that post that we're gonna associate this comment with so we can start 29 00:01:54,920 --> 00:01:58,530 to put together the rest of this component to get started. 30 00:01:58,560 --> 00:02:01,530 Let's work on a little bit of mock markup. 31 00:02:01,740 --> 00:02:06,220 I'm going to expand this div inside there. 32 00:02:06,290 --> 00:02:10,310 We're going to want a form element because this thing is all about showing a form that the user is going 33 00:02:10,310 --> 00:02:18,460 to enter some text into inside that will place a div with a class name of form dash group inside that 34 00:02:18,460 --> 00:02:25,760 div will do an input and right above that let's do a label as well on the label. 35 00:02:25,770 --> 00:02:30,570 I'll give it some text like about new comments on the input. 36 00:02:30,570 --> 00:02:32,400 Let's add in a class name there as well. 37 00:02:32,400 --> 00:02:38,580 I'll do form dash control let in some event listeners and whatnot and just a moment first right outside 38 00:02:38,580 --> 00:02:40,350 of that closing div that we've got right there. 39 00:02:40,450 --> 00:02:48,200 I'm going to add in a button that says submit and I'll give it a class name as well. 40 00:02:48,360 --> 00:02:54,490 B10 18 dash primary gets is a good start. 41 00:02:54,490 --> 00:02:58,970 Now we need to make sure that we've got a piece of state that's gonna track whatever value a user has 42 00:02:58,970 --> 00:03:00,170 inside this input. 43 00:03:00,260 --> 00:03:04,220 We need to make sure we've got an event handler on the form itself to watch for a summit event. 44 00:03:04,220 --> 00:03:08,720 We need to make sure that we've got a function that's actually going to make a post request off to our 45 00:03:08,720 --> 00:03:11,080 comments micros service and attempt to persist. 46 00:03:11,090 --> 00:03:20,450 The given comment so let's get to it let's first add in the use state hook we're going to create a new 47 00:03:20,450 --> 00:03:23,560 piece of state that's going to track whatever value the user has inside that input. 48 00:03:24,540 --> 00:03:30,520 So we'll say how about content and set content it's going to come from your state and we'll give it 49 00:03:30,520 --> 00:03:36,220 a default value of empty string and we can set up some event handlers and value setters on the input 50 00:03:36,220 --> 00:03:37,220 itself. 51 00:03:37,330 --> 00:03:42,990 So I'll say that this thing's value is going to be equal to content and it's going to have an on change 52 00:03:43,050 --> 00:03:46,670 event listener whenever user changes this input. 53 00:03:46,680 --> 00:03:52,360 We're going to receive an event which once again will abbreviate just as E and we'll use the things 54 00:03:52,420 --> 00:03:55,910 target value property to update our content piece of state. 55 00:03:56,110 --> 00:03:59,260 We'll say that content e target value 56 00:04:03,370 --> 00:04:04,870 that looks good. 57 00:04:04,880 --> 00:04:06,980 Now let's work on the form itself. 58 00:04:06,980 --> 00:04:11,960 We're going to add in an on submit listener once again anyone this thing gets submitted we'll call a 59 00:04:11,960 --> 00:04:17,260 function of a call simply on submit let's define that function right away. 60 00:04:17,280 --> 00:04:22,040 So on submit there's gonna be an async function because we probably need to make a request inside of 61 00:04:22,040 --> 00:04:25,040 your because this is a form submission. 62 00:04:25,040 --> 00:04:32,420 Once again we're going to receive that even object and call prevent default on it. 63 00:04:32,440 --> 00:04:38,890 And so now finally right here is where we're going to want to take our content and the Post I.D. and 64 00:04:38,890 --> 00:04:41,440 use them to make a request off to our comments. 65 00:04:41,470 --> 00:04:41,950 Micro. 66 00:04:41,950 --> 00:04:43,480 Service. 67 00:04:43,480 --> 00:04:47,140 So let's make sure that we import access at the top so we can actually make that request 68 00:04:51,380 --> 00:04:57,740 and then inside of here we'll do and await axial start post it's gonna be a poster costs because we 69 00:04:57,740 --> 00:05:02,240 want to create a new comment and then remember the path that we said that we're going to use for that 70 00:05:02,240 --> 00:05:02,870 micros service. 71 00:05:02,870 --> 00:05:08,750 We said that we were going to make a post request to posts slash the idea of the post we care about 72 00:05:08,840 --> 00:05:12,270 slash comments and we're going to include a body of content. 73 00:05:12,290 --> 00:05:17,230 Now we're going to have a string so inside of here we'll use a template string. 74 00:05:17,230 --> 00:05:23,090 Notice I'm using the tactics and we'll say HDP call and slash slash local hosts. 75 00:05:23,190 --> 00:05:29,630 And then remember our comments micros service is that port four thousand one will do slash posts and 76 00:05:29,650 --> 00:05:34,000 we'll do some string templating here and put in our post I.D. which we discussed a little bit ago. 77 00:05:34,000 --> 00:05:43,150 That's coming in as a prop flash comments and we'll make sure that we provide the actual content inside 78 00:05:43,150 --> 00:05:48,130 the body the request that's inside this object right here provided as a second argument and we'll throw 79 00:05:48,130 --> 00:05:55,780 our content inside their I should create a new comment and associate it with the given post right after 80 00:05:55,780 --> 00:05:57,390 we make this request successfully. 81 00:05:57,520 --> 00:06:02,800 We're gonna once again clear out the value of that input they're going to reset the inputs value back 82 00:06:02,800 --> 00:06:08,410 to an empty string just to make sure it's clear to a user that hey your comment has been persisted right 83 00:06:08,410 --> 00:06:08,830 after that. 84 00:06:08,830 --> 00:06:14,780 I'll do a set content of empty string. 85 00:06:14,880 --> 00:06:17,240 ALEX Good Now before we test this. 86 00:06:17,240 --> 00:06:19,790 We need to make sure we actually show this component somewhere. 87 00:06:19,940 --> 00:06:28,110 Remember we want to show that component inside of our post list every single post that we display needs 88 00:06:28,110 --> 00:06:32,180 to have an instance of comment create displayed right underneath it. 89 00:06:32,250 --> 00:06:36,930 So we'll go into post lists and find where we iterate over our list opposed to build out this list right 90 00:06:36,930 --> 00:06:44,840 here so I will go back over to a post let's start J.S. I'm going to scroll up to the very top and I 91 00:06:44,840 --> 00:06:50,980 will import most or see me comment create from comment create 92 00:06:54,530 --> 00:07:01,170 will then find where we've mapped over all those posts here's the mapping statement right here that 93 00:07:01,210 --> 00:07:06,200 this is where we display the title to post and as we just saw in our mockup we want to display that 94 00:07:06,200 --> 00:07:08,120 form right underneath it. 95 00:07:08,240 --> 00:07:11,420 So right after that div or see me right after the H 3 My mistake. 96 00:07:11,420 --> 00:07:12,620 We wanted to be inside the did. 97 00:07:12,650 --> 00:07:15,830 So it's got that card body class applied to it. 98 00:07:16,060 --> 00:07:22,270 We're going to display our comment rate component and we'll make sure that we give it its required property 99 00:07:22,630 --> 00:07:31,180 which is the post I.D. We'll say post I.D. and we've got access to the post I.D. be up this host variable 100 00:07:31,270 --> 00:07:37,810 right here so pass in post dot I.D.. 101 00:07:38,020 --> 00:07:38,750 That should be it. 102 00:07:38,830 --> 00:07:45,040 Let's save this or flip back over and once again do a quick test so I can now see my common creation 103 00:07:45,040 --> 00:07:54,490 form I'll enter in a comment and then submit it's with my network request tab open I can see that that 104 00:07:54,850 --> 00:07:59,210 comment was created successfully. 105 00:07:59,270 --> 00:08:00,740 All right so looks pretty good. 106 00:08:00,740 --> 00:08:05,110 Now the last thing we have to do here is just make sure we put together the comment list components 107 00:08:06,190 --> 00:08:09,180 so quick pause work on that component finished it up in just a moment.