1
00:00:00,290 --> 00:00:00,560
All right.

2
00:00:00,560 --> 00:00:05,190
So now we're going to add the reducer function to add an item to the cart.

3
00:00:05,210 --> 00:00:09,470
So let's go down to our cart slice and this reducers object.

4
00:00:09,470 --> 00:00:15,260
And what we're going to do is add a new function called Add to Cart.

5
00:00:15,290 --> 00:00:18,290
So we'll set that to an arrow function.

6
00:00:18,650 --> 00:00:22,260
And I shut off GitHub copilot just because it was starting to get annoying.

7
00:00:22,280 --> 00:00:29,240
Now that we're writing more code, it just gets a little confusing as to what I already wrote and what

8
00:00:29,240 --> 00:00:30,520
it's recommending.

9
00:00:30,530 --> 00:00:37,010
So in add to Cart, since this is a reducer function, it's going to take in two things a state and

10
00:00:37,010 --> 00:00:37,910
an action.

11
00:00:37,910 --> 00:00:41,930
So the state is just whatever the current state is of the cart.

12
00:00:41,930 --> 00:00:48,550
And then an action will include any data inside of a payload.

13
00:00:48,560 --> 00:00:54,350
So basically in this case, we're going to be sending an item to add to the cart which we can access

14
00:00:54,350 --> 00:00:57,980
with action dot payload action dot payload.

15
00:00:57,980 --> 00:01:05,790
So let's create a variable called item and set that to action dot payload.

16
00:01:05,790 --> 00:01:09,890
So that'll be the item, including the fields or whatever.

17
00:01:09,900 --> 00:01:14,010
Now we want to check to see if the item is already in the cart.

18
00:01:14,010 --> 00:01:16,440
So we'll say let's say exist.

19
00:01:17,250 --> 00:01:22,800
Item and we'll say state, and then we can access anything that's in the state.

20
00:01:22,800 --> 00:01:23,400
Right now.

21
00:01:23,400 --> 00:01:29,370
We just have an initial state of cart items, which is an empty array, but that's what we want to check

22
00:01:29,370 --> 00:01:31,020
is cart items.

23
00:01:31,020 --> 00:01:34,410
So we'll say cart items dot find.

24
00:01:34,620 --> 00:01:40,770
So we want to find a specific item and that's the one that matches whatever the ID of this.

25
00:01:40,770 --> 00:01:50,070
So in find we'll pass in a function, we'll say we'll use X here for whatever the current item is because

26
00:01:50,070 --> 00:01:53,340
it's going to loop through, just like with any high order array method.

27
00:01:53,340 --> 00:02:04,560
And we'll say if that current item, if the dot underscore ID is equal to the item from the action from

28
00:02:04,560 --> 00:02:07,500
the payload ID, right?

29
00:02:07,500 --> 00:02:13,290
So if those match, then that's going to put that particular item into this variable.

30
00:02:14,700 --> 00:02:15,120
All right.

31
00:02:15,120 --> 00:02:18,630
And we want to update the basically we'll just update the quantity.

32
00:02:18,630 --> 00:02:20,370
So let's say if.

33
00:02:22,060 --> 00:02:24,280
So if exist item.

34
00:02:24,940 --> 00:02:30,100
Then we're going to take our state cart items.

35
00:02:30,730 --> 00:02:34,960
And we're going to set that to state dot.

36
00:02:35,780 --> 00:02:39,680
Cart items dot map because we're going to map through.

37
00:02:40,770 --> 00:02:43,290
And I'll use X here again.

38
00:02:43,980 --> 00:02:45,000
So we'll map through.

39
00:02:45,000 --> 00:02:47,300
And then what we want to do is have a condition.

40
00:02:47,310 --> 00:03:00,180
We'll say if x, if x dot underscore ID is equal to the exist item dot underscore id.

41
00:03:01,380 --> 00:03:04,330
Then we'll return that item.

42
00:03:04,350 --> 00:03:05,390
Else we'll just.

43
00:03:05,430 --> 00:03:09,810
It'll just be whatever the item is that we're looping through.

44
00:03:10,820 --> 00:03:11,330
Okay.

45
00:03:11,360 --> 00:03:12,290
Else.

46
00:03:14,550 --> 00:03:23,310
Then we just want to add the new item so we can say state dot cart items and we'll set that to an array

47
00:03:23,310 --> 00:03:25,830
and then we'll just spread across what's already there.

48
00:03:25,830 --> 00:03:32,820
So state dot cart items because remember, we're not going to use dot push, you know, state dot cart

49
00:03:32,820 --> 00:03:37,410
items dot push because state the state is immutable.

50
00:03:37,410 --> 00:03:43,740
So we're basically just making a copy of it by setting an array and putting everything in it and then

51
00:03:43,740 --> 00:03:45,180
also adding.

52
00:03:46,010 --> 00:03:47,360
The new item.

53
00:03:49,340 --> 00:03:55,850
So now what we need to do is, is do some calculations for the price we want to store the items, price,

54
00:03:55,850 --> 00:03:59,090
shipping, price, tax, price.

55
00:03:59,090 --> 00:04:02,600
And when we add those together, that will give us the total price.

56
00:04:02,600 --> 00:04:05,180
So let's go under the if statement here.

57
00:04:06,800 --> 00:04:14,030
And let's say calc calculate item's price.

58
00:04:15,320 --> 00:04:15,620
All right.

59
00:04:15,620 --> 00:04:18,019
So we want to calculate the item's price.

60
00:04:18,050 --> 00:04:22,170
We're also going to get the shipping price.

61
00:04:22,190 --> 00:04:25,430
We're going to get the tax price.

62
00:04:25,430 --> 00:04:29,450
And then all of those together will be the total price.

63
00:04:30,260 --> 00:04:30,590
All right.

64
00:04:30,590 --> 00:04:32,870
So let's start with the item's price.

65
00:04:32,870 --> 00:04:40,460
So we're just going to take our state and we're going to add on to that an item's price value.

66
00:04:40,760 --> 00:04:47,390
And we can get that by just taking all of the price values for each item in the cart and adding them

67
00:04:47,390 --> 00:04:48,110
together.

68
00:04:48,110 --> 00:04:54,080
And this is a good case for the reduce method, which is a high order array method, and we can use

69
00:04:54,080 --> 00:04:56,300
that to to add all the prices together.

70
00:04:56,300 --> 00:04:58,520
So let's take the cart items.

71
00:04:58,520 --> 00:05:04,370
So state dot cart items and let's say dot reduce.

72
00:05:04,370 --> 00:05:06,170
Let's just close that up for now.

73
00:05:06,170 --> 00:05:10,770
So we're going to reduce and that takes in a function.

74
00:05:11,570 --> 00:05:17,300
And the what we're going to pass in here is the accumulator and then the item itself.

75
00:05:17,880 --> 00:05:18,060
Okay.

76
00:05:18,140 --> 00:05:21,800
And that accumulator, we're going to set that to to zero.

77
00:05:21,800 --> 00:05:25,910
And basically it'll start from there and then we can add the prices together.

78
00:05:25,910 --> 00:05:32,810
So let's take the accumulator, add on the item dot price, and then we also need to take into account

79
00:05:32,810 --> 00:05:34,640
the quantity, right?

80
00:05:34,640 --> 00:05:39,530
Because if you have two or more of an item, then you need to add that to the price.

81
00:05:39,530 --> 00:05:44,570
So we're going to say times item dot Qty.

82
00:05:44,930 --> 00:05:45,290
Okay.

83
00:05:45,290 --> 00:05:51,290
And then the next argument we pass in is the default for the accumulator, which is going to be zero.

84
00:05:51,290 --> 00:05:53,180
So this starts at zero.

85
00:05:53,210 --> 00:05:59,930
It'll loop through, it'll add the item price multiply by the quantity and we should end up with the

86
00:05:59,930 --> 00:06:01,480
total item price.

87
00:06:01,490 --> 00:06:05,270
Now we have some stuff to add on to that shipping and tax.

88
00:06:05,270 --> 00:06:12,270
So, but before we do that, I want to make sure that we have the right amount of decimals and it's

89
00:06:12,270 --> 00:06:13,650
formatted correctly.

90
00:06:13,650 --> 00:06:18,930
So I'm actually going to create a helper function up here called Add decimals.

91
00:06:20,100 --> 00:06:20,610
Okay.

92
00:06:20,610 --> 00:06:23,310
And then that's going to take in a number.

93
00:06:25,780 --> 00:06:26,530
And what?

94
00:06:26,530 --> 00:06:29,320
I want this to return.

95
00:06:30,050 --> 00:06:31,340
Is going to be.

96
00:06:31,610 --> 00:06:33,950
Let's use the math object here.

97
00:06:33,950 --> 00:06:35,300
We're going to round.

98
00:06:37,140 --> 00:06:38,880
Say math.round.

99
00:06:38,910 --> 00:06:40,020
The num.

100
00:06:40,840 --> 00:06:45,010
Times 100 and then just divide that by 100.

101
00:06:45,920 --> 00:06:47,090
Actually, let's put.

102
00:06:47,680 --> 00:06:51,100
This in parentheses, this whole thing.

103
00:06:52,940 --> 00:06:53,540
Like that.

104
00:06:53,540 --> 00:06:55,610
And then we want two decimal places.

105
00:06:55,610 --> 00:06:57,560
So I'm going to say two fixed.

106
00:06:58,140 --> 00:07:00,050
And pass in two.

107
00:07:00,060 --> 00:07:03,810
So I just want to use this helper function for all of our pricing.

108
00:07:03,810 --> 00:07:06,210
So let's wrap this right here.

109
00:07:07,160 --> 00:07:11,420
Where we have our let's see state item price.

110
00:07:11,720 --> 00:07:14,510
Yeah, we're going to set that to add.

111
00:07:15,570 --> 00:07:16,650
Decimals.

112
00:07:16,650 --> 00:07:18,960
And we're just going to wrap this entire thing.

113
00:07:20,100 --> 00:07:20,460
All right.

114
00:07:20,460 --> 00:07:22,440
So now for the shipping price.

115
00:07:23,920 --> 00:07:24,640
Let's do that.

116
00:07:24,640 --> 00:07:26,560
So we're going to say state dot.

117
00:07:27,770 --> 00:07:29,300
Shipping price.

118
00:07:30,080 --> 00:07:32,870
And what I'm going to do for shipping.

119
00:07:32,870 --> 00:07:34,900
I'll put it up here actually.

120
00:07:34,910 --> 00:07:39,260
So shipping will be, let's say, if.

121
00:07:40,120 --> 00:07:47,380
If order is over $100, then free.

122
00:07:47,680 --> 00:07:51,160
Else it'll be $10 shipping.

123
00:07:52,850 --> 00:07:53,420
Okay.

124
00:07:53,420 --> 00:07:55,790
And you can have any shipping policy you want.

125
00:07:55,790 --> 00:07:57,320
That's just what we're going to do.

126
00:07:57,530 --> 00:07:57,830
All right.

127
00:07:57,830 --> 00:08:00,350
So we're going to use Add decimals here as well.

128
00:08:01,160 --> 00:08:03,470
And then let's pass in here.

129
00:08:03,470 --> 00:08:11,930
We'll take our state dot items, price that we just calculated above and we'll say if that is greater

130
00:08:11,930 --> 00:08:15,140
than 100, then it'll be zero.

131
00:08:15,170 --> 00:08:16,850
Else it'll be ten.

132
00:08:16,900 --> 00:08:17,180
Okay.

133
00:08:17,180 --> 00:08:23,300
So shipping price, it's always going to be either 0 or 10 based on the amount of the order.

134
00:08:23,830 --> 00:08:28,150
Now for the tax price, it's going to be 15%.

135
00:08:28,150 --> 00:08:32,140
So let's just say 15% tax.

136
00:08:32,740 --> 00:08:37,059
So state and then tax price.

137
00:08:38,480 --> 00:08:41,480
And we want to use add decimals here as well.

138
00:08:42,230 --> 00:08:45,440
And let's just make sure that this is a number.

139
00:08:45,440 --> 00:08:53,740
So we're going to wrap it in number and then in parentheses we'll say 0.15.

140
00:08:53,750 --> 00:08:57,320
And we want to multiply that by the item's price.

141
00:08:57,320 --> 00:09:01,490
So state dot item's price.

142
00:09:01,490 --> 00:09:03,680
So that will give us 15% of that.

143
00:09:03,680 --> 00:09:06,710
And then let's just add on to this to fixed.

144
00:09:08,480 --> 00:09:10,040
With two decibels.

145
00:09:11,490 --> 00:09:12,270
All right.

146
00:09:12,390 --> 00:09:18,540
Now for the total price, basically, we just need to to add all that stuff together.

147
00:09:18,540 --> 00:09:21,840
So let's say state dot and then total.

148
00:09:22,560 --> 00:09:25,800
Price, and we're going to set that.

149
00:09:26,680 --> 00:09:30,820
Let's wrap this in parentheses and we'll say number.

150
00:09:30,820 --> 00:09:35,350
Just make sure everything is formatted as a number so the state dot items.

151
00:09:36,200 --> 00:09:36,710
Items.

152
00:09:36,710 --> 00:09:39,920
Price And then we want to add.

153
00:09:40,990 --> 00:09:42,340
See number.

154
00:09:43,060 --> 00:09:44,620
State dot.

155
00:09:45,610 --> 00:09:47,200
Shipping price.

156
00:09:51,170 --> 00:09:53,270
State dot.

157
00:09:53,270 --> 00:09:54,290
And then let's see.

158
00:09:54,320 --> 00:09:55,610
Tax price.

159
00:09:57,430 --> 00:09:59,830
And I think that should do it.

160
00:09:59,830 --> 00:10:02,020
And then we'll just add to fixed.

161
00:10:05,290 --> 00:10:13,480
To now all of this that we have this state items price shipping, price tax, total price.

162
00:10:13,480 --> 00:10:15,820
I want this all saved in local storage.

163
00:10:15,820 --> 00:10:21,910
So we're going to go right under the line that we just finished and we're going to save to local storage.

164
00:10:21,910 --> 00:10:30,220
So let's say local storage dot set item, save it as cart and we're going to save the entire state.

165
00:10:30,220 --> 00:10:33,370
We just need to run it through Json.stringify.

166
00:10:34,090 --> 00:10:35,800
And then state.

167
00:10:37,760 --> 00:10:38,180
Okay.

168
00:10:38,180 --> 00:10:43,640
Now, in order to use this add to cart, which we're going to be using from our component, we need

169
00:10:43,640 --> 00:10:45,590
to export it as an action.

170
00:10:45,590 --> 00:10:53,870
So even though we already exported the reducer to put that in the store JS file, any function we create

171
00:10:53,870 --> 00:10:56,180
here we need to export as an action.

172
00:10:56,180 --> 00:10:57,530
So let's say.

173
00:10:58,650 --> 00:11:00,120
Export, const.

174
00:11:00,120 --> 00:11:01,200
And then.

175
00:11:02,190 --> 00:11:03,210
Add to cart.

176
00:11:03,630 --> 00:11:07,470
And that's going to be from cart slice.

177
00:11:08,160 --> 00:11:11,730
So cart slice dot actions.

178
00:11:12,660 --> 00:11:17,010
And then we should be able to bring that in and we should be able to use it, which is what we're going

179
00:11:17,010 --> 00:11:22,320
to do in the next video is hook this button up so that we can actually add these items to the cart.

