1 00:00:00,840 --> 00:00:01,673 Instructor: In the last section, 2 00:00:01,673 --> 00:00:03,420 we wired up our Redux application 3 00:00:03,420 --> 00:00:05,430 to the react side of our app. 4 00:00:05,430 --> 00:00:07,140 So now we've got our Provider tag 5 00:00:07,140 --> 00:00:09,690 inside of our root index.js file, 6 00:00:09,690 --> 00:00:12,600 along with our instance of a Redux store. 7 00:00:12,600 --> 00:00:14,700 We'll now continue by starting to put together 8 00:00:14,700 --> 00:00:17,310 our saveComment action creator. 9 00:00:17,310 --> 00:00:20,100 This action creator is going to be very straightforward. 10 00:00:20,100 --> 00:00:23,280 It's going to take a string and dispatch an action 11 00:00:23,280 --> 00:00:26,190 of some given type that includes that string. 12 00:00:26,190 --> 00:00:28,890 So presumably, that string is gonna represent 13 00:00:28,890 --> 00:00:31,650 the comment that we're trying to add to the application. 14 00:00:31,650 --> 00:00:34,500 We'll need to also remember to flip over to our reducer 15 00:00:34,500 --> 00:00:37,290 and add a case to that thing to watch for the action 16 00:00:37,290 --> 00:00:39,510 that's created by this action creator. 17 00:00:39,510 --> 00:00:41,560 But first, let's make the action creator. 18 00:00:42,420 --> 00:00:45,480 To get started, I'll find my SRC directory, 19 00:00:45,480 --> 00:00:49,053 and inside there, I'll make a new folder called actions. 20 00:00:50,880 --> 00:00:53,730 Then inside that folder, I'm gonna make two files. 21 00:00:53,730 --> 00:00:58,080 One will be index.js, that's gonna house our action creator, 22 00:00:58,080 --> 00:01:01,470 and I'll make one more called types.js. 23 00:01:01,470 --> 00:01:03,150 that's going to house the single type 24 00:01:03,150 --> 00:01:05,519 that we're gonna have inside of our application. 25 00:01:05,519 --> 00:01:07,370 So we'll take care of the type first. 26 00:01:08,400 --> 00:01:12,270 We'll say, export const, and we'll call this action, 27 00:01:12,270 --> 00:01:14,370 how about something like, SAVE_COMMENT, 28 00:01:14,370 --> 00:01:16,260 I think that's reasonable. 29 00:01:16,260 --> 00:01:17,610 And then the actual string behind it 30 00:01:17,610 --> 00:01:20,343 will be just save_comment or whatever we want. 31 00:01:21,510 --> 00:01:24,210 So we can now import this type right here, 32 00:01:24,210 --> 00:01:27,210 into our actions index.js file. 33 00:01:27,210 --> 00:01:31,410 So over inside of actions index.js, excuse me, 34 00:01:31,410 --> 00:01:33,530 I will import SAVE_COMENT 35 00:01:35,160 --> 00:01:38,313 from actions/types. 36 00:01:39,150 --> 00:01:41,220 Don't forget the curly braces right here, 37 00:01:41,220 --> 00:01:42,930 because we might have eventually, 38 00:01:42,930 --> 00:01:44,880 multiple exports from types, 39 00:01:44,880 --> 00:01:47,340 and so we did a named export right here, 40 00:01:47,340 --> 00:01:49,650 which means we have to use those curly braces 41 00:01:49,650 --> 00:01:51,273 to get just this property. 42 00:01:53,340 --> 00:01:55,620 Then inside of our index.js file, 43 00:01:55,620 --> 00:01:57,813 we can create our actual action creator. 44 00:01:58,860 --> 00:02:01,260 So I will say, export function, 45 00:02:01,260 --> 00:02:03,813 we'll call the action creator, saveComment. 46 00:02:04,890 --> 00:02:06,930 We're going to assume that this function will be called 47 00:02:06,930 --> 00:02:09,539 with a comment that we're trying to save. 48 00:02:09,539 --> 00:02:12,753 So I will receive that as an argument called simply comment. 49 00:02:14,970 --> 00:02:18,570 Then inside the function, we can return an action 50 00:02:18,570 --> 00:02:20,403 with a type of SAVE_COMMENT, 51 00:02:21,567 --> 00:02:24,270 and a payload of the comment that was provided 52 00:02:24,270 --> 00:02:26,673 to the action creator, like so. 53 00:02:28,230 --> 00:02:29,070 All right, not that bad. 54 00:02:29,070 --> 00:02:31,420 That's pretty much it as far as our actions go. 55 00:02:32,370 --> 00:02:34,200 Now let's really quickly flip back over 56 00:02:34,200 --> 00:02:35,970 to our comments reducer, 57 00:02:35,970 --> 00:02:37,080 and we'll make sure that we watch 58 00:02:37,080 --> 00:02:39,780 for this additional type of SAVE_COMMENT. 59 00:02:39,780 --> 00:02:40,770 And anytime we see it, 60 00:02:40,770 --> 00:02:43,443 we'll add it to our active list of comments. 61 00:02:44,400 --> 00:02:47,940 So inside the reducers, comments.js file, 62 00:02:47,940 --> 00:02:50,700 I'll first add in import statement at the top of the file 63 00:02:50,700 --> 00:02:53,550 to get the type that we just imported. 64 00:02:53,550 --> 00:02:54,820 So I'll say, import 65 00:02:56,550 --> 00:02:58,170 SAVE_COMMENT, 66 00:02:58,170 --> 00:02:59,310 and again, do not not forget 67 00:02:59,310 --> 00:03:01,143 to put the curly braces in here, 68 00:03:02,010 --> 00:03:05,433 from actions/types. 69 00:03:08,130 --> 00:03:10,500 Then we can add that case or that type 70 00:03:10,500 --> 00:03:13,590 to our switch statement by adding in a new case. 71 00:03:13,590 --> 00:03:18,590 So I'll say, case SAVE_COMMENT, and anytime we see an action 72 00:03:18,630 --> 00:03:22,380 with this type right here, we'll take the actions payload 73 00:03:22,380 --> 00:03:24,840 and add it to our state array. 74 00:03:24,840 --> 00:03:29,840 So I will return a new array with dot, dot, dot state. 75 00:03:31,050 --> 00:03:32,250 So that right there is gonna take 76 00:03:32,250 --> 00:03:35,820 all of our existing comments and add it to this new array. 77 00:03:35,820 --> 00:03:37,350 And then we will add on top of that 78 00:03:37,350 --> 00:03:40,500 the new comment that just got passed into this reducer, 79 00:03:40,500 --> 00:03:44,043 which will be action.payload, like so. 80 00:03:45,840 --> 00:03:48,930 Okay, so that's pretty much it in entirety 81 00:03:48,930 --> 00:03:51,510 on the Redux side of our application. 82 00:03:51,510 --> 00:03:53,160 So now the last thing we have to do 83 00:03:53,160 --> 00:03:56,190 is go over to our comment box component 84 00:03:56,190 --> 00:03:57,960 and make sure that we wire up 85 00:03:57,960 --> 00:03:59,940 that action creator that we just added. 86 00:03:59,940 --> 00:04:02,640 So anytime someone submits a new comment 87 00:04:02,640 --> 00:04:04,710 with this handleSubmit function right here, 88 00:04:04,710 --> 00:04:07,830 we call our action creator and save the comment. 89 00:04:07,830 --> 00:04:09,300 So quick break, and we'll take care of that 90 00:04:09,300 --> 00:04:10,353 in the next section.