1
00:00:01,310 --> 00:00:05,630
Let's take a look at the solution we're going to use to actually persist our list of cells back to our

2
00:00:05,630 --> 00:00:06,170
API.

3
00:00:06,890 --> 00:00:09,440
So we're going to create a save cells middleware.

4
00:00:09,830 --> 00:00:15,530
This nowhere's job is going to be to watch for actions that get dispatched to have a type of mousel

5
00:00:15,530 --> 00:00:17,900
update cell, insert cell after and delete cell.

6
00:00:18,290 --> 00:00:23,090
Remember, these are all the different action types that can somehow modify the list of cells that are

7
00:00:23,090 --> 00:00:24,600
stored inside of our cells reducer.

8
00:00:25,040 --> 00:00:29,360
So when we start thinking about saving our list of cells, whenever something changes, well, these

9
00:00:29,360 --> 00:00:31,470
are the only things that can actually cause a change.

10
00:00:32,210 --> 00:00:37,280
So if our middleware ever sees one of these actions, that is a sign that our list of cells is about

11
00:00:37,280 --> 00:00:37,880
to change.

12
00:00:38,480 --> 00:00:42,620
So we're going to take that action, forward it onto our store, and then immediately after, we're

13
00:00:42,620 --> 00:00:46,100
going to try to persist our list of cells using the safe cells.

14
00:00:46,100 --> 00:00:47,690
Action creator we just put together.

15
00:00:48,420 --> 00:00:51,020
This diagram is a little bit deceptively simple.

16
00:00:51,200 --> 00:00:54,800
It turns out there are going to be one or two really interesting things we need to think about as we

17
00:00:54,800 --> 00:00:55,820
put this middleware together.

18
00:00:56,160 --> 00:01:00,710
But right now, let's just start to write out the middleware and then just kind of encounter these little

19
00:01:00,710 --> 00:01:01,670
problems in turn.

20
00:01:02,360 --> 00:01:06,830
OK, so back inside my code editor, I'm going to find my state directory.

21
00:01:08,310 --> 00:01:11,220
Inside there will make a new folder called Middlebury's.

22
00:01:13,290 --> 00:01:18,330
And then inside of that, I'll make a new file and I'm going to call this persist middleware instead

23
00:01:18,330 --> 00:01:21,860
of save cells just to have a name that is different than that action creator.

24
00:01:22,230 --> 00:01:25,560
So I will call this persist middleware dot.

25
00:01:25,570 --> 00:01:29,460
It's not the best name, but good enough will deal with it.

26
00:01:31,410 --> 00:01:35,140
In general, I'm going to assume that you are familiar with Redox Middlebury's if you're not totally

27
00:01:35,160 --> 00:01:38,670
fine, just type along and you'll get pretty familiar with what's going on here pretty quickly.

28
00:01:39,400 --> 00:01:42,780
The first thing I want to remind you in general around Middlebury's is that, remember, they've got

29
00:01:42,780 --> 00:01:45,240
that really weird function signature.

30
00:01:45,630 --> 00:01:50,730
So we're going to define and export a function called persist middleware.

31
00:01:52,030 --> 00:01:57,190
And here's where things get weird, remember, a middleware is a function that returns, a function

32
00:01:57,430 --> 00:01:58,960
that returns a function.

33
00:02:00,420 --> 00:02:02,730
So this is why medal winners are just a little bit nasty.

34
00:02:03,800 --> 00:02:07,590
And then remember, each of these different functions get called with a slightly different argument.

35
00:02:08,030 --> 00:02:13,370
So the first one is going to be called with not exactly our Redux store, but it's frequently abbreviated

36
00:02:13,370 --> 00:02:14,370
and just called store.

37
00:02:14,390 --> 00:02:19,070
So this is not exactly our store, like our entire Redock store, but it is an object that is very similar.

38
00:02:20,070 --> 00:02:25,260
Then the second function is going to be called with our next function, which is how we take an action

39
00:02:25,260 --> 00:02:30,780
and for it along to the next middleware, where all of our different reducers and then finally the innermost

40
00:02:30,780 --> 00:02:32,460
function receives our action.

41
00:02:33,920 --> 00:02:38,390
So that's going to be the action that we want to take a look at and determine whether it is the Mousel

42
00:02:38,390 --> 00:02:40,700
update sell, blah, blah, blah, all those different action types.

43
00:02:41,510 --> 00:02:45,530
And right away, we do have to apply a couple of different type annotations to each of these different

44
00:02:45,530 --> 00:02:46,100
functions.

45
00:02:47,130 --> 00:02:52,530
On this store right here, the only thing that we really care about on the entire store object is dispatch.

46
00:02:52,770 --> 00:02:58,380
It's going to structure off just dispatch, and I'm going to annotate it in the same way that we've

47
00:02:58,380 --> 00:03:01,490
annotated the dispatch function inside of our different action creators.

48
00:03:02,160 --> 00:03:06,270
So I'm going to put in this is an object with a dispatch property.

49
00:03:07,180 --> 00:03:09,040
That is of type dispatch.

50
00:03:10,130 --> 00:03:12,290
And then put in action like so.

51
00:03:13,130 --> 00:03:17,900
I'm then going to make sure that I immediately import dispatch and action from the appropriate sources

52
00:03:17,900 --> 00:03:20,960
at the top, so I will import dispatch.

53
00:03:22,350 --> 00:03:23,100
From Redux.

54
00:03:25,320 --> 00:03:28,920
And action from up on directory actions.

55
00:03:31,120 --> 00:03:36,490
Next up, our next function, this is a function that is going to be called with some action that we

56
00:03:36,490 --> 00:03:39,550
want to put it on so we can just annotate this type manually.

57
00:03:39,850 --> 00:03:41,560
We can say that this will be a function.

58
00:03:42,820 --> 00:03:47,080
They'll be called with something that will call action, and it's going to be of type action.

59
00:03:49,270 --> 00:03:53,380
And I don't expect to return anything, so I'm going to put in a return type invitation avoid.

60
00:03:54,850 --> 00:03:59,320
Then finally, our innermost function right here is going to receive some action, which once again

61
00:03:59,650 --> 00:04:01,060
is just going to be of type action.

62
00:04:03,350 --> 00:04:05,840
OK, so now we can start to put together some implementation.

63
00:04:06,650 --> 00:04:11,600
The first thing we have to do is decide whether or not that incoming action has a type of one of these

64
00:04:11,600 --> 00:04:12,020
types.

65
00:04:12,680 --> 00:04:17,240
So we better import our action type in at the top so we can actually do some comparison.

66
00:04:19,019 --> 00:04:21,390
Up here will do import action type.

67
00:04:23,500 --> 00:04:26,590
From up on directory action type's.

68
00:04:30,860 --> 00:04:35,930
Then inside of our middleware itself, we receive on those action types, we want to dispatch that action

69
00:04:35,930 --> 00:04:37,640
creator of save cells.

70
00:04:38,010 --> 00:04:40,870
So let's make sure we also import that action to right away as well.

71
00:04:41,660 --> 00:04:42,290
So I'll get.

72
00:04:43,710 --> 00:04:48,390
Save cells from up on directory action creators.

73
00:04:51,980 --> 00:04:56,600
So then inside of our innermost function right here, the first thing we want to do is take whatever

74
00:04:56,600 --> 00:05:01,400
incoming action is flowing into our middleware and no matter what, we want to pass it on to the next

75
00:05:01,400 --> 00:05:02,350
middleware on our chain.

76
00:05:02,780 --> 00:05:05,590
We're not trying to stop or modify any actions here.

77
00:05:05,810 --> 00:05:08,570
We're trying to pass along every single action that we receive.

78
00:05:08,870 --> 00:05:14,060
The only exception is that we if we receive one of these actions, we want to dispatch an additional

79
00:05:14,060 --> 00:05:14,980
action as well.

80
00:05:16,970 --> 00:05:21,380
So I'm going to put in here next with action right away, because, again, no matter what, we always

81
00:05:21,380 --> 00:05:23,150
want to it on every single action.

82
00:05:24,360 --> 00:05:29,400
Now, at this point, let's just save this file as is and wired up to our Redock store and make sure

83
00:05:29,400 --> 00:05:33,060
that everything works as expected with just this very small amount of code we have.

84
00:05:33,970 --> 00:05:36,850
So I'm going to go back over to my Staats file.

85
00:05:39,740 --> 00:05:44,750
Inside of here at the very top, I'm going to import the fiercest middleware.

86
00:05:47,000 --> 00:05:47,780
We just put together.

87
00:05:52,010 --> 00:05:54,020
Middleware is persist middleware.

88
00:05:55,840 --> 00:06:00,910
And then I'm going to wired up to our applied middleware call, we can put this new middleware before

89
00:06:00,910 --> 00:06:03,810
or after Redux Thunk it doesn't make a difference in this case.

90
00:06:04,570 --> 00:06:06,340
So either location just fine.

91
00:06:08,810 --> 00:06:09,830
Let's then save this.

92
00:06:11,190 --> 00:06:16,650
Go back over to our application, do a quick refresh, and we should still see everything working as

93
00:06:16,650 --> 00:06:17,220
expected.

94
00:06:17,460 --> 00:06:22,770
I should still be able to change the contents of a cell, add a new cell in, or do whatever else I

95
00:06:22,770 --> 00:06:23,280
want to do.

96
00:06:23,970 --> 00:06:25,170
Looks like everything is still good.

97
00:06:25,560 --> 00:06:29,300
So let's take a pause right here and then continue working on our middleware in just a moment.

