All language subtitles for [English (auto-generated)] Build and Deploy a Full Stack MERN Social Media App with Auth, Pagination, Comments _ MERN Course [DownSub.com]
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,719 --> 00:00:03,439
hi there and welcome to the ultimate
2
00:00:03,439 --> 00:00:05,759
course that'll transform you into a
3
00:00:05,759 --> 00:00:08,480
master of the mern stack capable of
4
00:00:08,480 --> 00:00:11,840
building real industry projects
5
00:00:11,840 --> 00:00:15,440
in this video you'll build and deploy a
6
00:00:15,440 --> 00:00:18,320
full stack social media application with
7
00:00:18,320 --> 00:00:21,680
complete email and google authentication
8
00:00:21,680 --> 00:00:25,359
called memories with pagination search
9
00:00:25,359 --> 00:00:28,840
and filtering capabilities details pages
10
00:00:28,840 --> 00:00:32,000
authentication comments and much more
11
00:00:32,000 --> 00:00:34,480
the memories app is the best fully
12
00:00:34,480 --> 00:00:37,200
responsive mern social media application
13
00:00:37,200 --> 00:00:39,920
that you can currently find on youtube
14
00:00:39,920 --> 00:00:42,960
alongside this video i've also prepared
15
00:00:42,960 --> 00:00:46,239
a complete mirn roadmap and a cheat
16
00:00:46,239 --> 00:00:48,879
sheet that covers everything you need to
17
00:00:48,879 --> 00:00:51,920
become a fantastic man developer the
18
00:00:51,920 --> 00:00:55,280
guide includes an entire step-by-step
19
00:00:55,280 --> 00:00:58,399
mern mastery roadmap prerequisites
20
00:00:58,399 --> 00:01:01,600
helpful illustrations and even mern
21
00:01:01,600 --> 00:01:04,640
project ideas that you can build deploy
22
00:01:04,640 --> 00:01:07,920
and put on your portfolio to get a job
23
00:01:07,920 --> 00:01:10,479
you can download the ultimate guide and
24
00:01:10,479 --> 00:01:12,560
use it as a reference on your
25
00:01:12,560 --> 00:01:14,560
development journey whenever you're
26
00:01:14,560 --> 00:01:16,880
unsure what to learn next
27
00:01:16,880 --> 00:01:18,799
the link to the comprehensive guide is
28
00:01:18,799 --> 00:01:20,960
going to be in the description
29
00:01:20,960 --> 00:01:23,040
i'd strongly recommend downloading it
30
00:01:23,040 --> 00:01:25,200
now and referring to it while watching
31
00:01:25,200 --> 00:01:28,159
the video especially if you're unsure
32
00:01:28,159 --> 00:01:29,920
why are we using the particular
33
00:01:29,920 --> 00:01:31,360
technology
34
00:01:31,360 --> 00:01:33,200
this video paired with the guide
35
00:01:33,200 --> 00:01:36,079
contains absolutely everything you need
36
00:01:36,079 --> 00:01:39,439
to become a professional mirn developer
37
00:01:39,439 --> 00:01:42,079
this app is a full-fledged crud
38
00:01:42,079 --> 00:01:43,360
application
39
00:01:43,360 --> 00:01:45,200
which means that if you learn how to
40
00:01:45,200 --> 00:01:47,360
build this you'll be able to make the
41
00:01:47,360 --> 00:01:49,759
most complex and most modern
42
00:01:49,759 --> 00:01:51,520
applications out there
43
00:01:51,520 --> 00:01:54,000
if you think about it every app is a
44
00:01:54,000 --> 00:01:55,920
crud app in a way
45
00:01:55,920 --> 00:01:58,320
crud stands for create
46
00:01:58,320 --> 00:02:01,439
read update and delete
47
00:02:01,439 --> 00:02:04,000
you can find this pattern everywhere
48
00:02:04,000 --> 00:02:05,439
building a blog
49
00:02:05,439 --> 00:02:08,800
create posts read them and delete them
50
00:02:08,800 --> 00:02:10,639
building a social media network with
51
00:02:10,639 --> 00:02:11,680
users
52
00:02:11,680 --> 00:02:14,800
create users update user profiles and
53
00:02:14,800 --> 00:02:16,640
delete users
54
00:02:16,640 --> 00:02:19,200
building a movie application well add
55
00:02:19,200 --> 00:02:22,480
your favorites rate them and delete them
56
00:02:22,480 --> 00:02:23,920
you get the point
57
00:02:23,920 --> 00:02:26,560
the concept of crud is explained in more
58
00:02:26,560 --> 00:02:29,360
detail inside of the full marin guide
59
00:02:29,360 --> 00:02:31,599
that is linked below this video
60
00:02:31,599 --> 00:02:33,680
at the end of the guide you'll also find
61
00:02:33,680 --> 00:02:36,879
the best crud projects so if you haven't
62
00:02:36,879 --> 00:02:39,360
already now is the time to get your own
63
00:02:39,360 --> 00:02:40,890
guide
64
00:02:40,890 --> 00:02:47,179
[Music]
65
00:02:48,720 --> 00:02:51,840
before we start building out our project
66
00:02:51,840 --> 00:02:53,920
let's first get the hosting for the
67
00:02:53,920 --> 00:02:57,200
react side of our marin application and
68
00:02:57,200 --> 00:03:00,000
more importantly the domain name for our
69
00:03:00,000 --> 00:03:02,800
new site or any site you'll create in
70
00:03:02,800 --> 00:03:06,000
the future hostinger is my personal
71
00:03:06,000 --> 00:03:08,560
recommendation and right now they're
72
00:03:08,560 --> 00:03:11,120
offering crazy deals so i simply needed
73
00:03:11,120 --> 00:03:12,959
to show this to you
74
00:03:12,959 --> 00:03:15,599
currently they're offering a completely
75
00:03:15,599 --> 00:03:18,800
free domain name and a lifetime ssl
76
00:03:18,800 --> 00:03:22,640
certificate for 3 bucks a month
77
00:03:22,640 --> 00:03:25,280
so if we scroll down a bit you can see
78
00:03:25,280 --> 00:03:27,920
that they offer great price to quality
79
00:03:27,920 --> 00:03:31,440
ratio high speed servers free ssl
80
00:03:31,440 --> 00:03:32,480
certificate
81
00:03:32,480 --> 00:03:35,440
and 24 7 tech support
82
00:03:35,440 --> 00:03:37,760
with that said since i've partnered with
83
00:03:37,760 --> 00:03:40,000
hostinger they decided to give you an
84
00:03:40,000 --> 00:03:42,560
even bigger discount you can find the
85
00:03:42,560 --> 00:03:44,400
link and the discount code in the
86
00:03:44,400 --> 00:03:46,799
description enjoy
87
00:03:46,799 --> 00:03:48,080
once you visit the link in the
88
00:03:48,080 --> 00:03:50,239
description we can simply click get
89
00:03:50,239 --> 00:03:51,519
started
90
00:03:51,519 --> 00:03:55,519
and finally select a web hosting plan
91
00:03:55,519 --> 00:03:58,319
as you can see we have a 30 day money
92
00:03:58,319 --> 00:04:01,040
back guarantee and now we have to choose
93
00:04:01,040 --> 00:04:03,519
a period of our hosting
94
00:04:03,519 --> 00:04:06,159
in my case i'm going to choose 48 months
95
00:04:06,159 --> 00:04:07,760
as that's going to provide us with the
96
00:04:07,760 --> 00:04:09,200
cheapest price
97
00:04:09,200 --> 00:04:11,439
we can scroll down a bit to create our
98
00:04:11,439 --> 00:04:12,480
account
99
00:04:12,480 --> 00:04:14,480
and finally after you select your
100
00:04:14,480 --> 00:04:16,959
payment method you can enter a coupon
101
00:04:16,959 --> 00:04:19,600
code right here that's going to be
102
00:04:19,600 --> 00:04:23,919
javascript mastery all caps no spaces
103
00:04:23,919 --> 00:04:25,840
when you apply that you're going to get
104
00:04:25,840 --> 00:04:28,720
an even bigger discount
105
00:04:28,720 --> 00:04:30,560
with that said let's go ahead and
106
00:04:30,560 --> 00:04:32,960
purchase it and i'll see you straight
107
00:04:32,960 --> 00:04:35,840
inside of hostinger's dashboard
108
00:04:35,840 --> 00:04:38,240
as you can see i'm personally using
109
00:04:38,240 --> 00:04:41,120
hostinger for my brand for all of my
110
00:04:41,120 --> 00:04:43,520
websites
111
00:04:43,520 --> 00:04:45,759
and most importantly i'm using their
112
00:04:45,759 --> 00:04:47,840
domain names and hosting for the
113
00:04:47,840 --> 00:04:50,639
upcoming js mastery pro course
114
00:04:50,639 --> 00:04:52,400
by the way which domain name do you
115
00:04:52,400 --> 00:04:54,960
prefer jsmasterypro.com
116
00:04:54,960 --> 00:04:58,560
or simply js mastery dot pro let me know
117
00:04:58,560 --> 00:05:00,320
in the comments down below
118
00:05:00,320 --> 00:05:02,560
with that said if you purchase the
119
00:05:02,560 --> 00:05:04,800
premium shared hosting then you should
120
00:05:04,800 --> 00:05:07,199
see a claim your free domain button
121
00:05:07,199 --> 00:05:08,400
right here
122
00:05:08,400 --> 00:05:10,080
in my case i'm going to click claim
123
00:05:10,080 --> 00:05:11,199
domain
124
00:05:11,199 --> 00:05:13,280
and i'm going to do something like js
125
00:05:13,280 --> 00:05:17,199
mastery or just jsm and then dash mern
126
00:05:17,199 --> 00:05:18,880
of course you don't have to do this
127
00:05:18,880 --> 00:05:20,880
right now you can choose the domain name
128
00:05:20,880 --> 00:05:23,520
after you build your application so i'm
129
00:05:23,520 --> 00:05:26,080
just gonna check availability
130
00:05:26,080 --> 00:05:28,240
and it is completely available you can
131
00:05:28,240 --> 00:05:29,919
see we have it for free
132
00:05:29,919 --> 00:05:31,840
and you can click the claim domain
133
00:05:31,840 --> 00:05:35,039
button right here to finish your process
134
00:05:35,039 --> 00:05:37,759
and in just a few seconds our domain is
135
00:05:37,759 --> 00:05:40,160
going to be registered and then we'll be
136
00:05:40,160 --> 00:05:41,919
able to deploy our client-side
137
00:05:41,919 --> 00:05:44,160
application straight to hostinger's
138
00:05:44,160 --> 00:05:46,720
hosting and that's it in just a few
139
00:05:46,720 --> 00:05:48,800
seconds we purchased the domain name and
140
00:05:48,800 --> 00:05:50,880
set it up at the end of the video we're
141
00:05:50,880 --> 00:05:53,120
going to deploy our application to
142
00:05:53,120 --> 00:05:54,960
hostinger
143
00:05:54,960 --> 00:05:57,520
so what exactly can you do in our
144
00:05:57,520 --> 00:05:59,520
memories application
145
00:05:59,520 --> 00:06:02,240
as the name suggests it is an album of
146
00:06:02,240 --> 00:06:04,319
memories where people from all around
147
00:06:04,319 --> 00:06:06,560
the world can add the most notable
148
00:06:06,560 --> 00:06:09,199
places they visited or the wildest
149
00:06:09,199 --> 00:06:11,440
things they've done let's take a look at
150
00:06:11,440 --> 00:06:13,199
all the features of this memories
151
00:06:13,199 --> 00:06:14,720
application
152
00:06:14,720 --> 00:06:17,440
one of the main features is pagination
153
00:06:17,440 --> 00:06:19,919
which allows you to only fetch a certain
154
00:06:19,919 --> 00:06:22,080
number of memories at a time
155
00:06:22,080 --> 00:06:24,240
then we have the complete search and
156
00:06:24,240 --> 00:06:26,639
filtering functionality we can search
157
00:06:26,639 --> 00:06:29,520
for a tag like europe or search for a
158
00:06:29,520 --> 00:06:31,520
part of the title with something like
159
00:06:31,520 --> 00:06:34,960
the and get all the matching memories
160
00:06:34,960 --> 00:06:37,360
our application will also include
161
00:06:37,360 --> 00:06:40,479
client-side routing a memory details
162
00:06:40,479 --> 00:06:41,680
page
163
00:06:41,680 --> 00:06:43,840
you can click on a memory to get more
164
00:06:43,840 --> 00:06:45,759
information about it
165
00:06:45,759 --> 00:06:48,160
as you can see we'll even implement the
166
00:06:48,160 --> 00:06:50,960
recommended memory section so let's
167
00:06:50,960 --> 00:06:52,960
check out a few places
168
00:06:52,960 --> 00:06:55,680
we have the coliseum we also have the
169
00:06:55,680 --> 00:06:57,599
prague castle
170
00:06:57,599 --> 00:06:59,680
these don't necessarily have to be
171
00:06:59,680 --> 00:07:02,720
memories or places you have a fully
172
00:07:02,720 --> 00:07:05,919
functioning crud mern application and
173
00:07:05,919 --> 00:07:07,919
you can turn it into any topic you'd
174
00:07:07,919 --> 00:07:08,800
like
175
00:07:08,800 --> 00:07:11,199
make it completely your own
176
00:07:11,199 --> 00:07:14,000
auth or authentication is a complex
177
00:07:14,000 --> 00:07:16,800
process and therefore it is another
178
00:07:16,800 --> 00:07:19,440
essential part of our application
179
00:07:19,440 --> 00:07:21,919
we are going to allow our users to log
180
00:07:21,919 --> 00:07:23,919
in and register
181
00:07:23,919 --> 00:07:26,800
here a user can create a real account in
182
00:07:26,800 --> 00:07:28,000
the database
183
00:07:28,000 --> 00:07:30,639
sign in with that account or sign in
184
00:07:30,639 --> 00:07:33,199
using google automatically
185
00:07:33,199 --> 00:07:35,360
while building the authentication you'll
186
00:07:35,360 --> 00:07:38,400
thoroughly learn every step of the way
187
00:07:38,400 --> 00:07:41,840
we'll use json web tokens or jwts for
188
00:07:41,840 --> 00:07:44,960
short and google oauth finally we're
189
00:07:44,960 --> 00:07:47,199
going to add the comments and most
190
00:07:47,199 --> 00:07:48,479
importantly
191
00:07:48,479 --> 00:07:51,280
refactor the entire user interface and
192
00:07:51,280 --> 00:07:53,680
make our app completely mobile
193
00:07:53,680 --> 00:07:55,360
responsive
194
00:07:55,360 --> 00:07:57,440
no worries at all if you haven't worked
195
00:07:57,440 --> 00:07:59,840
with the marin stack before or you don't
196
00:07:59,840 --> 00:08:02,000
know what the heck mirn is
197
00:08:02,000 --> 00:08:04,879
let me give you a quick introduction
198
00:08:04,879 --> 00:08:07,759
mern stack is a javascript stack used
199
00:08:07,759 --> 00:08:11,199
for easier and faster deployment of full
200
00:08:11,199 --> 00:08:13,599
stack web applications
201
00:08:13,599 --> 00:08:17,759
the letter m in mern stands for mongodb
202
00:08:17,759 --> 00:08:21,360
a nosql database in which data is stored
203
00:08:21,360 --> 00:08:24,479
in documents that consists of key value
204
00:08:24,479 --> 00:08:28,080
pairs similar to json javascript object
205
00:08:28,080 --> 00:08:29,440
notation
206
00:08:29,440 --> 00:08:32,320
the letter e stands for express which
207
00:08:32,320 --> 00:08:34,559
helps us build the back end quickly
208
00:08:34,559 --> 00:08:36,240
while staying in the javascript
209
00:08:36,240 --> 00:08:37,599
ecosystem
210
00:08:37,599 --> 00:08:41,200
the letter r is of course for react
211
00:08:41,200 --> 00:08:43,760
almost all modern tech companies from
212
00:08:43,760 --> 00:08:46,160
early stage startups to the biggest tech
213
00:08:46,160 --> 00:08:48,640
companies like microsoft and facebook
214
00:08:48,640 --> 00:08:50,240
use react
215
00:08:50,240 --> 00:08:52,560
you learn react functional components
216
00:08:52,560 --> 00:08:54,480
and their reusability
217
00:08:54,480 --> 00:08:57,600
react file and folder structure and more
218
00:08:57,600 --> 00:09:00,480
importantly the use of redux state
219
00:09:00,480 --> 00:09:02,080
management library
220
00:09:02,080 --> 00:09:06,000
and finally the letter n is for node.js
221
00:09:06,000 --> 00:09:08,320
the runtime environment that allows you
222
00:09:08,320 --> 00:09:11,519
to write back-end code using javascript
223
00:09:11,519 --> 00:09:13,839
ultimately saving you the trouble of
224
00:09:13,839 --> 00:09:16,080
learning a new programming language
225
00:09:16,080 --> 00:09:18,720
capable of running the back end
226
00:09:18,720 --> 00:09:21,440
on top of all of that you'll also learn
227
00:09:21,440 --> 00:09:23,760
how to deploy your websites so that you
228
00:09:23,760 --> 00:09:25,600
can share them with your friends or
229
00:09:25,600 --> 00:09:27,600
potential employers put them on your
230
00:09:27,600 --> 00:09:31,040
portfolio and get a job once again
231
00:09:31,040 --> 00:09:34,000
downloading the complete mirn roadmap is
232
00:09:34,000 --> 00:09:36,959
crucial here so now is the time to go
233
00:09:36,959 --> 00:09:39,360
down in the description and get yours if
234
00:09:39,360 --> 00:09:40,959
you haven't already
235
00:09:40,959 --> 00:09:43,279
and just before we dive straight into
236
00:09:43,279 --> 00:09:45,440
the code if you'd like me to make
237
00:09:45,440 --> 00:09:48,160
another marin course leave a like
238
00:09:48,160 --> 00:09:50,880
comment and subscribe if this video
239
00:09:50,880 --> 00:09:53,839
reaches 20 000 likes i'm recording
240
00:09:53,839 --> 00:09:57,519
another complete maran application
241
00:09:57,519 --> 00:10:00,560
with that said without any further ado
242
00:10:00,560 --> 00:10:03,440
let's dive straight into the code
243
00:10:03,440 --> 00:10:05,519
first we're going to open an empty
244
00:10:05,519 --> 00:10:07,920
visual studio code window you can use
245
00:10:07,920 --> 00:10:10,000
any code editor but i prefer visual
246
00:10:10,000 --> 00:10:12,000
studio code for many benefits that it
247
00:10:12,000 --> 00:10:12,959
has
248
00:10:12,959 --> 00:10:14,640
secondly we are going to go to your
249
00:10:14,640 --> 00:10:17,360
desktop and then create a new folder you
250
00:10:17,360 --> 00:10:19,279
can name it memories
251
00:10:19,279 --> 00:10:22,079
project now you have to drag and drop it
252
00:10:22,079 --> 00:10:24,880
to your visual studio code once we're in
253
00:10:24,880 --> 00:10:27,680
you can go above and then click view and
254
00:10:27,680 --> 00:10:29,680
terminal this is going to open your
255
00:10:29,680 --> 00:10:31,839
terminal and since we'll be working
256
00:10:31,839 --> 00:10:32,880
technically with two different
257
00:10:32,880 --> 00:10:35,120
applications here we're going to split
258
00:10:35,120 --> 00:10:37,360
our terminal into two parts
259
00:10:37,360 --> 00:10:39,279
first one is going to be for our client
260
00:10:39,279 --> 00:10:41,839
side application or react side and the
261
00:10:41,839 --> 00:10:44,399
second one is going to be for node we
262
00:10:44,399 --> 00:10:46,480
are going to create two separate folders
263
00:10:46,480 --> 00:10:47,440
as well
264
00:10:47,440 --> 00:10:50,240
first one is going to be named client
265
00:10:50,240 --> 00:10:52,560
and the second one is going to be called
266
00:10:52,560 --> 00:10:53,839
server
267
00:10:53,839 --> 00:10:54,640
now
268
00:10:54,640 --> 00:10:57,200
we can cd into client with our first
269
00:10:57,200 --> 00:10:59,600
terminal on the left side and there we
270
00:10:59,600 --> 00:11:01,680
are going to initialize an empty react
271
00:11:01,680 --> 00:11:05,040
application by typing mpx create react
272
00:11:05,040 --> 00:11:06,959
app dot slash
273
00:11:06,959 --> 00:11:09,680
make sure you're in the client directory
274
00:11:09,680 --> 00:11:12,000
in terms of prerequisites the only thing
275
00:11:12,000 --> 00:11:13,680
you're going to need is going to be the
276
00:11:13,680 --> 00:11:15,600
newest version of node just click the
277
00:11:15,600 --> 00:11:17,839
newest one and download it for your own
278
00:11:17,839 --> 00:11:20,320
operating system while the client side
279
00:11:20,320 --> 00:11:22,640
is installing we can create a new file
280
00:11:22,640 --> 00:11:25,120
called index.js inside of the server
281
00:11:25,120 --> 00:11:26,880
directory this is going to be the
282
00:11:26,880 --> 00:11:29,600
starting point of our server application
283
00:11:29,600 --> 00:11:31,200
inside of the second terminal we're
284
00:11:31,200 --> 00:11:32,720
going to navigate to that server
285
00:11:32,720 --> 00:11:34,720
directory and there we're going to run
286
00:11:34,720 --> 00:11:38,399
the command mpm init minus y
287
00:11:38,399 --> 00:11:40,399
this is going to initialize an empty
288
00:11:40,399 --> 00:11:42,720
package.json and now we'll be able to
289
00:11:42,720 --> 00:11:45,200
install all the necessary dependencies
290
00:11:45,200 --> 00:11:47,040
dependencies that we need are going to
291
00:11:47,040 --> 00:11:49,920
be as follows npm install
292
00:11:49,920 --> 00:11:52,320
and we need body parser
293
00:11:52,320 --> 00:11:54,079
this one is going to enable us to send
294
00:11:54,079 --> 00:11:56,639
post requests then we need course which
295
00:11:56,639 --> 00:11:59,120
is going to enable cross origin requests
296
00:11:59,120 --> 00:12:00,959
then we need express of course we're
297
00:12:00,959 --> 00:12:02,959
going to use express as a framework for
298
00:12:02,959 --> 00:12:05,040
creating the routing of our application
299
00:12:05,040 --> 00:12:07,279
and then finally mongoose we're going to
300
00:12:07,279 --> 00:12:09,920
use mongoose to create models for our
301
00:12:09,920 --> 00:12:11,120
posts
302
00:12:11,120 --> 00:12:13,600
and then finally we need nodemon we're
303
00:12:13,600 --> 00:12:15,440
going to use nodemon so that we don't
304
00:12:15,440 --> 00:12:17,440
have to manually reset the server every
305
00:12:17,440 --> 00:12:19,920
time we make a change instead nodemon
306
00:12:19,920 --> 00:12:21,680
will be doing that for us
307
00:12:21,680 --> 00:12:23,600
you can click enter and this should
308
00:12:23,600 --> 00:12:24,399
install
309
00:12:24,399 --> 00:12:26,800
all of the necessary dependencies
310
00:12:26,800 --> 00:12:28,880
while that is installing we can go into
311
00:12:28,880 --> 00:12:32,160
index.js and import these dependencies
312
00:12:32,160 --> 00:12:34,560
first of all we have import
313
00:12:34,560 --> 00:12:35,600
express
314
00:12:35,600 --> 00:12:37,760
from express
315
00:12:37,760 --> 00:12:40,959
then we have import body parser
316
00:12:40,959 --> 00:12:44,639
from body minus parser
317
00:12:44,639 --> 00:12:48,399
import mongoose from mongoose
318
00:12:48,399 --> 00:12:51,600
and finally import course from
319
00:12:51,600 --> 00:12:53,760
course just like that
320
00:12:53,760 --> 00:12:55,760
now you might be confused if you worked
321
00:12:55,760 --> 00:12:57,440
it back in so far you know that the
322
00:12:57,440 --> 00:12:59,920
usual syntax looks like this const
323
00:12:59,920 --> 00:13:03,040
express is equal to require
324
00:13:03,040 --> 00:13:04,240
express
325
00:13:04,240 --> 00:13:06,880
thankfully in newest versions of node we
326
00:13:06,880 --> 00:13:09,200
can use the import syntax which is a lot
327
00:13:09,200 --> 00:13:11,920
more modern and a lot easier to use the
328
00:13:11,920 --> 00:13:14,240
only thing we have to do to enable it is
329
00:13:14,240 --> 00:13:16,959
that we have to go into package.json and
330
00:13:16,959 --> 00:13:19,920
there we need to add one line below the
331
00:13:19,920 --> 00:13:22,720
main index.js in here we have to say
332
00:13:22,720 --> 00:13:25,040
type make sure everything is in double
333
00:13:25,040 --> 00:13:28,000
quotes strings so type is equal to
334
00:13:28,000 --> 00:13:31,120
module and then add a comma that's it
335
00:13:31,120 --> 00:13:32,800
while we are here we are also going to
336
00:13:32,800 --> 00:13:35,600
delete this test script and add a start
337
00:13:35,600 --> 00:13:38,160
script so start is equal to
338
00:13:38,160 --> 00:13:39,399
nodemon
339
00:13:39,399 --> 00:13:41,760
index.js now you can close your
340
00:13:41,760 --> 00:13:45,440
package.json index.js and wait until our
341
00:13:45,440 --> 00:13:47,920
client side is initialized then we'll be
342
00:13:47,920 --> 00:13:49,600
able to install all the necessary
343
00:13:49,600 --> 00:13:52,320
dependencies for the client side as well
344
00:13:52,320 --> 00:13:54,800
react application is initialized let's
345
00:13:54,800 --> 00:13:56,639
take a look at what dependencies are we
346
00:13:56,639 --> 00:13:58,160
going to need
347
00:13:58,160 --> 00:14:00,720
to make sure we have the same versions
348
00:14:00,720 --> 00:14:02,399
of the dependencies installed
349
00:14:02,399 --> 00:14:05,000
i'm going to provide you with a full
350
00:14:05,000 --> 00:14:08,079
package.json file you can open up your
351
00:14:08,079 --> 00:14:10,560
package.json scroll down to the
352
00:14:10,560 --> 00:14:12,800
description find the client-side
353
00:14:12,800 --> 00:14:15,519
package.json file and then simply paste
354
00:14:15,519 --> 00:14:17,600
it over the existing one
355
00:14:17,600 --> 00:14:19,519
that way we're making sure that
356
00:14:19,519 --> 00:14:22,480
everything 100 works because we're going
357
00:14:22,480 --> 00:14:24,800
to have the same versions of specific
358
00:14:24,800 --> 00:14:26,480
dependencies
359
00:14:26,480 --> 00:14:29,279
after you're done pasting it simply type
360
00:14:29,279 --> 00:14:31,760
npm install
361
00:14:31,760 --> 00:14:33,600
dash dash legacy
362
00:14:33,600 --> 00:14:35,120
dash peer
363
00:14:35,120 --> 00:14:38,160
dash depths like this and make sure that
364
00:14:38,160 --> 00:14:40,880
you're in the client side directory
365
00:14:40,880 --> 00:14:43,600
simply press enter
366
00:14:43,600 --> 00:14:45,680
this is making sure the code you see in
367
00:14:45,680 --> 00:14:48,240
the video produces the exact same
368
00:14:48,240 --> 00:14:50,560
outcome for you even if you're watching
369
00:14:50,560 --> 00:14:53,279
the video a few years after it has been
370
00:14:53,279 --> 00:14:54,959
released
371
00:14:54,959 --> 00:14:57,360
later in the video you may see me
372
00:14:57,360 --> 00:15:00,160
install a few more dependencies once
373
00:15:00,160 --> 00:15:03,120
again things like material ui
374
00:15:03,120 --> 00:15:06,560
jwt decode and many other things
375
00:15:06,560 --> 00:15:08,560
but since now you've copied the
376
00:15:08,560 --> 00:15:10,720
package.json run the installation
377
00:15:10,720 --> 00:15:13,920
command you won't have to reinstall any
378
00:15:13,920 --> 00:15:16,480
dependencies later on in the video
379
00:15:16,480 --> 00:15:19,440
definitely not for the client side so
380
00:15:19,440 --> 00:15:22,240
just keep that in mind you already have
381
00:15:22,240 --> 00:15:24,560
all of the necessary dependencies for
382
00:15:24,560 --> 00:15:27,600
the client side development
383
00:15:27,600 --> 00:15:29,680
while that is installing we can go ahead
384
00:15:29,680 --> 00:15:31,600
and check what we have in our client
385
00:15:31,600 --> 00:15:34,079
folder inside of here we have an src
386
00:15:34,079 --> 00:15:36,240
folder with a lot of things inside of it
387
00:15:36,240 --> 00:15:37,680
we are not going to use all of these
388
00:15:37,680 --> 00:15:39,680
things so i usually like to just delete
389
00:15:39,680 --> 00:15:42,240
the whole src folder and create a new
390
00:15:42,240 --> 00:15:43,680
one
391
00:15:43,680 --> 00:15:46,079
after you do that there's only one thing
392
00:15:46,079 --> 00:15:47,920
that we absolutely must have in our
393
00:15:47,920 --> 00:15:50,320
react application and that is the
394
00:15:50,320 --> 00:15:52,639
index.js file so you can go ahead and
395
00:15:52,639 --> 00:15:55,279
create that index.js this is the file
396
00:15:55,279 --> 00:15:56,959
where we're going to connect our react
397
00:15:56,959 --> 00:16:00,079
application to index.html file
398
00:16:00,079 --> 00:16:01,360
so import
399
00:16:01,360 --> 00:16:04,079
react from react
400
00:16:04,079 --> 00:16:06,560
import react dom
401
00:16:06,560 --> 00:16:09,120
from react minus dom
402
00:16:09,120 --> 00:16:12,720
and then also import app from dot slash
403
00:16:12,720 --> 00:16:15,040
app we don't yet have this component but
404
00:16:15,040 --> 00:16:17,680
we're gonna add it soon and then we have
405
00:16:17,680 --> 00:16:21,360
react dom dot render the first thing is
406
00:16:21,360 --> 00:16:23,920
the app component and then the second
407
00:16:23,920 --> 00:16:27,120
thing is document dot get element
408
00:16:27,120 --> 00:16:28,880
by id
409
00:16:28,880 --> 00:16:30,720
and then we have to specify the string
410
00:16:30,720 --> 00:16:33,199
of root with this line we're connecting
411
00:16:33,199 --> 00:16:36,480
to the div with an id of root great now
412
00:16:36,480 --> 00:16:39,440
we have to create that app.js file
413
00:16:39,440 --> 00:16:40,959
app.js
414
00:16:40,959 --> 00:16:42,880
and there we're going to have a simple
415
00:16:42,880 --> 00:16:46,079
functional component so import react
416
00:16:46,079 --> 00:16:47,040
from
417
00:16:47,040 --> 00:16:48,639
react
418
00:16:48,639 --> 00:16:51,120
const app is equal to a functional
419
00:16:51,120 --> 00:16:53,040
component and there we're going to have
420
00:16:53,040 --> 00:16:56,160
a return for now let's just create a div
421
00:16:56,160 --> 00:16:58,000
and inside of that div we're going to
422
00:16:58,000 --> 00:16:59,759
have an h1
423
00:16:59,759 --> 00:17:02,000
which is going to say app
424
00:17:02,000 --> 00:17:04,160
and of course we have to do export
425
00:17:04,160 --> 00:17:06,160
default app
426
00:17:06,160 --> 00:17:08,799
with this we should be ready to run our
427
00:17:08,799 --> 00:17:11,599
client side let's save it and let's save
428
00:17:11,599 --> 00:17:14,079
the index as well to run the application
429
00:17:14,079 --> 00:17:16,959
we just simply have to say npm start
430
00:17:16,959 --> 00:17:20,319
that's it great we can see our app here
431
00:17:20,319 --> 00:17:22,400
and if we go back you can see compiled
432
00:17:22,400 --> 00:17:24,559
successfully for now we're going to
433
00:17:24,559 --> 00:17:27,599
close this terminal by typing control c
434
00:17:27,599 --> 00:17:29,840
we did that for a reason that first
435
00:17:29,840 --> 00:17:31,760
we're going to set up our back end we
436
00:17:31,760 --> 00:17:33,440
have more stuff that we need to set up
437
00:17:33,440 --> 00:17:35,600
there we need to connect to the database
438
00:17:35,600 --> 00:17:38,160
create a mongodb cluster set up the
439
00:17:38,160 --> 00:17:41,280
models and also create some documents so
440
00:17:41,280 --> 00:17:43,039
first of all we're going to close our
441
00:17:43,039 --> 00:17:45,919
client all together and collapse this
442
00:17:45,919 --> 00:17:47,840
and then finally just go into the
443
00:17:47,840 --> 00:17:49,919
index.js of server that's where we're
444
00:17:49,919 --> 00:17:51,280
going to spend the first part of this
445
00:17:51,280 --> 00:17:52,400
video in
446
00:17:52,400 --> 00:17:54,240
inside of here what you need to do with
447
00:17:54,240 --> 00:17:56,559
every express application is first
448
00:17:56,559 --> 00:17:58,960
initialize this app so app is going to
449
00:17:58,960 --> 00:18:01,200
be equal to express
450
00:18:01,200 --> 00:18:03,440
and then you simply run it as a function
451
00:18:03,440 --> 00:18:05,679
now we can use all different methods on
452
00:18:05,679 --> 00:18:08,480
that app instance first let's do some
453
00:18:08,480 --> 00:18:11,520
general setup we're going to say app.use
454
00:18:11,520 --> 00:18:13,280
and then inside of there we're going to
455
00:18:13,280 --> 00:18:15,440
say body parser
456
00:18:15,440 --> 00:18:16,880
dot json
457
00:18:16,880 --> 00:18:19,840
and now in here we have to do limit is
458
00:18:19,840 --> 00:18:21,760
equal to 30 megabytes
459
00:18:21,760 --> 00:18:23,200
i just did that because we're going to
460
00:18:23,200 --> 00:18:25,200
be sending some images which can be
461
00:18:25,200 --> 00:18:26,960
large in size and then we're going to
462
00:18:26,960 --> 00:18:28,640
say extended
463
00:18:28,640 --> 00:18:31,679
is equal to true just like that now you
464
00:18:31,679 --> 00:18:34,480
can copy that line paste it below and we
465
00:18:34,480 --> 00:18:37,760
are also going to do body parser dot url
466
00:18:37,760 --> 00:18:39,760
encoded and put the same thing as
467
00:18:39,760 --> 00:18:41,440
parameters we are just setting up the
468
00:18:41,440 --> 00:18:43,520
body parser so that we can properly send
469
00:18:43,520 --> 00:18:45,760
our requests now we're also going to use
470
00:18:45,760 --> 00:18:47,520
that course so we're going to say
471
00:18:47,520 --> 00:18:50,720
app.use and just put course in there and
472
00:18:50,720 --> 00:18:53,200
call it as a function that's it
473
00:18:53,200 --> 00:18:55,280
now is the time to connect our server
474
00:18:55,280 --> 00:18:58,400
application with a real database for
475
00:18:58,400 --> 00:19:00,960
that we're going to use mongodb
476
00:19:00,960 --> 00:19:04,000
more specifically their cloud atlas
477
00:19:04,000 --> 00:19:06,160
version of mongodb which means that
478
00:19:06,160 --> 00:19:08,480
they're going to host our database on
479
00:19:08,480 --> 00:19:09,760
their cloud
480
00:19:09,760 --> 00:19:11,600
we can go there the website is
481
00:19:11,600 --> 00:19:13,280
mongodb.com
482
00:19:13,280 --> 00:19:15,679
cloud atlas the link is going to be in
483
00:19:15,679 --> 00:19:18,400
the description and then in here you can
484
00:19:18,400 --> 00:19:21,120
click start free and then just create a
485
00:19:21,120 --> 00:19:22,960
free account which is going to enable
486
00:19:22,960 --> 00:19:25,200
you to create something known as a
487
00:19:25,200 --> 00:19:27,760
cluster that's going to act as our
488
00:19:27,760 --> 00:19:29,760
online cloud database
489
00:19:29,760 --> 00:19:31,760
once you do that you'll be greeted with
490
00:19:31,760 --> 00:19:34,000
a website that looks like this you can
491
00:19:34,000 --> 00:19:36,640
just go ahead and click build a cluster
492
00:19:36,640 --> 00:19:38,640
then you can choose share clusters which
493
00:19:38,640 --> 00:19:40,799
is going to be completely free and then
494
00:19:40,799 --> 00:19:42,400
you can just click create cluster at the
495
00:19:42,400 --> 00:19:43,919
bottom of the page
496
00:19:43,919 --> 00:19:45,919
that's going to start the deployment of
497
00:19:45,919 --> 00:19:47,919
our cluster it can take one to three
498
00:19:47,919 --> 00:19:50,160
minutes but while that is deploying
499
00:19:50,160 --> 00:19:52,400
let's do some setup you can go in here
500
00:19:52,400 --> 00:19:54,880
to database access and then you can
501
00:19:54,880 --> 00:19:58,000
click add a new database user in here
502
00:19:58,000 --> 00:19:59,600
you can create your own username and
503
00:19:59,600 --> 00:20:01,440
password i'm going to use javascript
504
00:20:01,440 --> 00:20:03,919
mastery and then password cannot be
505
00:20:03,919 --> 00:20:06,000
simply one two three one two three so
506
00:20:06,000 --> 00:20:07,760
i'm gonna use javascript mastery and
507
00:20:07,760 --> 00:20:10,320
then one two three just like this
508
00:20:10,320 --> 00:20:12,559
now i can click add user with this
509
00:20:12,559 --> 00:20:14,720
credentials we'll be able to read and
510
00:20:14,720 --> 00:20:17,840
write to any database great finally we
511
00:20:17,840 --> 00:20:20,000
need to go to network access and then in
512
00:20:20,000 --> 00:20:22,880
here you can click add ip address once
513
00:20:22,880 --> 00:20:25,200
you do that you can click add current ip
514
00:20:25,200 --> 00:20:27,360
address and click confirm
515
00:20:27,360 --> 00:20:28,559
that's it
516
00:20:28,559 --> 00:20:30,640
now we go back to clusters and wait
517
00:20:30,640 --> 00:20:33,360
until our cluster is deployed
518
00:20:33,360 --> 00:20:35,360
now you can click the connect button
519
00:20:35,360 --> 00:20:37,440
right here
520
00:20:37,440 --> 00:20:39,360
that's going to ask us if we want to
521
00:20:39,360 --> 00:20:41,200
connect it with a shell our own
522
00:20:41,200 --> 00:20:44,000
application or mongodb compass which is
523
00:20:44,000 --> 00:20:46,080
a graphical user interface for looking
524
00:20:46,080 --> 00:20:48,320
at our databases data we can click
525
00:20:48,320 --> 00:20:50,480
connect your application because we want
526
00:20:50,480 --> 00:20:52,720
to connect it to our server side express
527
00:20:52,720 --> 00:20:55,120
and node application you can copy this
528
00:20:55,120 --> 00:20:57,039
string and then we're going to go back
529
00:20:57,039 --> 00:20:59,440
to visual studio code
530
00:20:59,440 --> 00:21:01,440
inside of here i'm going to create a new
531
00:21:01,440 --> 00:21:03,200
variable which is going to be called
532
00:21:03,200 --> 00:21:04,559
connection
533
00:21:04,559 --> 00:21:07,600
url all uppercase and then in here you
534
00:21:07,600 --> 00:21:09,919
can create a string and simply paste the
535
00:21:09,919 --> 00:21:12,080
whole thing that we received right there
536
00:21:12,080 --> 00:21:15,039
from our mongodb atlas in here you can
537
00:21:15,039 --> 00:21:16,799
enter your username and password make
538
00:21:16,799 --> 00:21:19,600
sure to delete these arrows as well so
539
00:21:19,600 --> 00:21:22,000
mine was javascript mastery
540
00:21:22,000 --> 00:21:23,280
and then the password was
541
00:21:23,280 --> 00:21:25,360
javascriptmastery123
542
00:21:25,360 --> 00:21:27,600
you can enter your own credentials
543
00:21:27,600 --> 00:21:30,159
that's it of course in real applications
544
00:21:30,159 --> 00:21:32,400
your credentials should be secured so we
545
00:21:32,400 --> 00:21:34,799
cannot simply put them in here later on
546
00:21:34,799 --> 00:21:36,720
in our video before the deployment of
547
00:21:36,720 --> 00:21:38,640
the whole application we're going to
548
00:21:38,640 --> 00:21:41,120
create environmental variables and then
549
00:21:41,120 --> 00:21:43,360
we're going to store that connection url
550
00:21:43,360 --> 00:21:44,720
right there
551
00:21:44,720 --> 00:21:46,640
the second thing we need is going to be
552
00:21:46,640 --> 00:21:50,080
port so const port is equal to process
553
00:21:50,080 --> 00:21:52,799
dot env that port for now we're going to
554
00:21:52,799 --> 00:21:55,440
use the 5000 but later on once we push
555
00:21:55,440 --> 00:21:57,760
this to heroku heroku is automatically
556
00:21:57,760 --> 00:22:00,080
going to populate environmental variable
557
00:22:00,080 --> 00:22:03,200
called port finally we use our mongoose
558
00:22:03,200 --> 00:22:05,240
to connect to our database
559
00:22:05,240 --> 00:22:07,919
mongoose.connect it is a function that
560
00:22:07,919 --> 00:22:10,320
accepts two different parameters first
561
00:22:10,320 --> 00:22:12,880
one is going to be the connection url
562
00:22:12,880 --> 00:22:15,120
and the second one is an object with all
563
00:22:15,120 --> 00:22:16,880
the options we're going to use two
564
00:22:16,880 --> 00:22:18,559
different options first one is going to
565
00:22:18,559 --> 00:22:21,600
be use new url parser which we're going
566
00:22:21,600 --> 00:22:23,919
to set up to true and the second one is
567
00:22:23,919 --> 00:22:26,960
going to be use unified topology which
568
00:22:26,960 --> 00:22:28,960
we are also going to set up to true
569
00:22:28,960 --> 00:22:31,360
these are not required but if you don't
570
00:22:31,360 --> 00:22:32,640
set them up we're going to get some
571
00:22:32,640 --> 00:22:35,120
errors in the console or rather warnings
572
00:22:35,120 --> 00:22:36,960
so let's just have it for simplicity
573
00:22:36,960 --> 00:22:39,360
purposes now we're not going to end this
574
00:22:39,360 --> 00:22:40,880
sentence right there we're going to
575
00:22:40,880 --> 00:22:43,360
chain a dot then because this returns a
576
00:22:43,360 --> 00:22:45,919
promise so in here we're going to have a
577
00:22:45,919 --> 00:22:48,559
dot then and also we're going to have a
578
00:22:48,559 --> 00:22:50,880
dot catch just below
579
00:22:50,880 --> 00:22:53,039
now what do we want to do if our
580
00:22:53,039 --> 00:22:55,360
connection is successful well then we
581
00:22:55,360 --> 00:22:57,919
want to call our app and then on it we
582
00:22:57,919 --> 00:23:00,320
want to do app.listen
583
00:23:00,320 --> 00:23:02,480
listen accepts two parameters the first
584
00:23:02,480 --> 00:23:04,480
one is the port which we just created
585
00:23:04,480 --> 00:23:06,640
right there and the second one is a
586
00:23:06,640 --> 00:23:08,320
callback function which is going to be
587
00:23:08,320 --> 00:23:10,880
ran once our application successfully
588
00:23:10,880 --> 00:23:12,960
listens that's going to be a simple
589
00:23:12,960 --> 00:23:14,559
console log which is going to be
590
00:23:14,559 --> 00:23:16,799
template string and it's going to say
591
00:23:16,799 --> 00:23:19,919
server running on port
592
00:23:19,919 --> 00:23:21,760
and now we can use template strings to
593
00:23:21,760 --> 00:23:24,000
say port this needs to be all
594
00:23:24,000 --> 00:23:26,240
capitalized that's going to say server
595
00:23:26,240 --> 00:23:29,840
running on port 5000 great and finally
596
00:23:29,840 --> 00:23:31,760
if the connection to the database is not
597
00:23:31,760 --> 00:23:34,000
successful we're going to have an error
598
00:23:34,000 --> 00:23:36,240
there and we are simply going to kansa
599
00:23:36,240 --> 00:23:37,039
log
600
00:23:37,039 --> 00:23:39,280
error dot message
601
00:23:39,280 --> 00:23:40,400
that's it
602
00:23:40,400 --> 00:23:42,400
and the final piece of setup is going to
603
00:23:42,400 --> 00:23:44,400
be mongoose.set
604
00:23:44,400 --> 00:23:46,880
in there we're going to set use
605
00:23:46,880 --> 00:23:47,760
find
606
00:23:47,760 --> 00:23:48,840
and
607
00:23:48,840 --> 00:23:52,080
modify to false so just pass the second
608
00:23:52,080 --> 00:23:54,640
parameter false this again makes sure
609
00:23:54,640 --> 00:23:56,400
that we don't get any warnings in the
610
00:23:56,400 --> 00:23:59,279
console that's it now if we go back to
611
00:23:59,279 --> 00:24:01,440
our package.json we specify the script
612
00:24:01,440 --> 00:24:03,760
start and let's run it you can go ahead
613
00:24:03,760 --> 00:24:06,400
and type mpm start that's going to run
614
00:24:06,400 --> 00:24:09,039
at nodemon index.js and we should get a
615
00:24:09,039 --> 00:24:11,200
console.log saying server running on
616
00:24:11,200 --> 00:24:14,240
port 5000 that's it our server is
617
00:24:14,240 --> 00:24:16,720
successfully connected to the database
618
00:24:16,720 --> 00:24:18,080
and now that we are connected to the
619
00:24:18,080 --> 00:24:20,640
database we can start creating routes
620
00:24:20,640 --> 00:24:23,279
for our backend application to do that
621
00:24:23,279 --> 00:24:25,279
we are going to create a new folder
622
00:24:25,279 --> 00:24:26,880
inside of the server which is going to
623
00:24:26,880 --> 00:24:28,799
be called routes
624
00:24:28,799 --> 00:24:30,320
inside of the routes we are going to
625
00:24:30,320 --> 00:24:33,919
create a file called posts.js
626
00:24:33,919 --> 00:24:35,679
inside of there we're going to add all
627
00:24:35,679 --> 00:24:37,120
the routes that have to do something
628
00:24:37,120 --> 00:24:39,600
with posts both being that cars that you
629
00:24:39,600 --> 00:24:41,600
saw previously
630
00:24:41,600 --> 00:24:44,480
to do that we have to import express
631
00:24:44,480 --> 00:24:46,640
from express
632
00:24:46,640 --> 00:24:49,279
and we also have to set up our router
633
00:24:49,279 --> 00:24:52,159
constructor is equal to express
634
00:24:52,159 --> 00:24:53,039
dot
635
00:24:53,039 --> 00:24:55,520
router with a capital r
636
00:24:55,520 --> 00:24:58,080
now we can start adding our routes
637
00:24:58,080 --> 00:24:59,679
our first route is going to be
638
00:24:59,679 --> 00:25:01,520
router.get
639
00:25:01,520 --> 00:25:03,760
the path is just going to be slash and
640
00:25:03,760 --> 00:25:06,000
then in here we can specify a callback
641
00:25:06,000 --> 00:25:08,320
function that is going to be executed
642
00:25:08,320 --> 00:25:13,039
once someone visits localhost 5000 slash
643
00:25:13,039 --> 00:25:15,760
great in here we're always going to have
644
00:25:15,760 --> 00:25:17,760
request and response it's going to be
645
00:25:17,760 --> 00:25:20,000
the same thing for every single callback
646
00:25:20,000 --> 00:25:23,120
function inside of our router.get
647
00:25:23,120 --> 00:25:24,960
the second parameter is going to be a
648
00:25:24,960 --> 00:25:26,720
callback function that is going to be
649
00:25:26,720 --> 00:25:30,000
executed once user visits this route
650
00:25:30,000 --> 00:25:31,919
instead of every callback function we
651
00:25:31,919 --> 00:25:35,200
have request and a response for now
652
00:25:35,200 --> 00:25:37,760
let's just do response.send and then in
653
00:25:37,760 --> 00:25:40,559
there let's say this works
654
00:25:40,559 --> 00:25:43,840
that's it now we have to export the
655
00:25:43,840 --> 00:25:46,080
whole router so we're going to do export
656
00:25:46,080 --> 00:25:47,200
default
657
00:25:47,200 --> 00:25:50,000
router just like that and now we can go
658
00:25:50,000 --> 00:25:52,880
ahead in index.js and import that router
659
00:25:52,880 --> 00:25:55,120
which we just exported from there
660
00:25:55,120 --> 00:25:57,919
to do that we have to do import
661
00:25:57,919 --> 00:26:01,919
post routes from dot slash routes
662
00:26:01,919 --> 00:26:03,080
slash
663
00:26:03,080 --> 00:26:04,880
posts.js
664
00:26:04,880 --> 00:26:06,720
now we can use express middleware to
665
00:26:06,720 --> 00:26:08,480
connect this to our application to do
666
00:26:08,480 --> 00:26:11,200
that we have to type app.use and then
667
00:26:11,200 --> 00:26:13,120
for the first parameter set up the
668
00:26:13,120 --> 00:26:16,080
starting pad for all the routes inside
669
00:26:16,080 --> 00:26:18,720
of the post.js and that thing is going
670
00:26:18,720 --> 00:26:20,000
to be posts
671
00:26:20,000 --> 00:26:22,320
and in here we set the routes
672
00:26:22,320 --> 00:26:25,760
so what this did is it said that every
673
00:26:25,760 --> 00:26:28,080
route inside of the post routes is going
674
00:26:28,080 --> 00:26:31,120
to start with posts that means that this
675
00:26:31,120 --> 00:26:33,440
route inside of posts is not reached by
676
00:26:33,440 --> 00:26:35,679
going to localhost
677
00:26:35,679 --> 00:26:37,039
5000
678
00:26:37,039 --> 00:26:39,600
and then slash it's reached by going to
679
00:26:39,600 --> 00:26:42,880
localhost 5000 and then slash
680
00:26:42,880 --> 00:26:46,080
posts because we added a prefix of posts
681
00:26:46,080 --> 00:26:49,279
to all routes in here now let's see if
682
00:26:49,279 --> 00:26:51,360
we can get this response inside of the
683
00:26:51,360 --> 00:26:54,480
browser to do that we have to go to this
684
00:26:54,480 --> 00:26:58,080
url localhost 5000 slash posts i'm going
685
00:26:58,080 --> 00:27:00,400
to copy it go back and then check it in
686
00:27:00,400 --> 00:27:02,880
the browser as you can see at the top
687
00:27:02,880 --> 00:27:05,200
left corner we get this works which
688
00:27:05,200 --> 00:27:07,039
means that we successfully connected our
689
00:27:07,039 --> 00:27:09,200
express application and we are running
690
00:27:09,200 --> 00:27:12,480
it on localhost 5000 great before we
691
00:27:12,480 --> 00:27:14,480
move any further with the functionality
692
00:27:14,480 --> 00:27:16,559
we are going to set up an amazing folder
693
00:27:16,559 --> 00:27:18,159
structure for all the backend
694
00:27:18,159 --> 00:27:20,480
applications that's going to make them
695
00:27:20,480 --> 00:27:22,960
much more scalable to do that we are
696
00:27:22,960 --> 00:27:25,039
going to create a new folder called
697
00:27:25,039 --> 00:27:27,360
controllers
698
00:27:27,360 --> 00:27:29,520
inside of the controllers folder we are
699
00:27:29,520 --> 00:27:31,840
also going to create a file called
700
00:27:31,840 --> 00:27:34,000
posts.js
701
00:27:34,000 --> 00:27:36,000
inside of there we're going to create
702
00:27:36,000 --> 00:27:38,640
all the handlers for our routes what
703
00:27:38,640 --> 00:27:40,559
does that mean well let me put it side
704
00:27:40,559 --> 00:27:41,840
by side
705
00:27:41,840 --> 00:27:43,919
inside of here we don't want to have
706
00:27:43,919 --> 00:27:46,320
some logic because if we keep adding
707
00:27:46,320 --> 00:27:48,799
routes and adding more complex logic
708
00:27:48,799 --> 00:27:49,840
like this
709
00:27:49,840 --> 00:27:52,799
our file for routes is gonna get long
710
00:27:52,799 --> 00:27:54,720
and we're gonna get lost between all the
711
00:27:54,720 --> 00:27:56,960
logic and all the requests and at the
712
00:27:56,960 --> 00:27:58,720
end it's going to be hard to see what
713
00:27:58,720 --> 00:28:00,640
routes do you even have access to to
714
00:28:00,640 --> 00:28:02,799
simplify that we can do something like
715
00:28:02,799 --> 00:28:05,919
this we can extract all the functions or
716
00:28:05,919 --> 00:28:08,159
all the logic from the routes and then
717
00:28:08,159 --> 00:28:10,799
take it right here in the posts of the
718
00:28:10,799 --> 00:28:13,039
controllers inside of here we're going
719
00:28:13,039 --> 00:28:14,720
to declare that function for the
720
00:28:14,720 --> 00:28:17,919
router.get let's give it a name const
721
00:28:17,919 --> 00:28:21,279
getposts is equal to that same callback
722
00:28:21,279 --> 00:28:23,440
function which we just copied
723
00:28:23,440 --> 00:28:25,200
of course so that we can use it in
724
00:28:25,200 --> 00:28:28,080
post.js we have to first export it right
725
00:28:28,080 --> 00:28:31,919
there export const get posts great now
726
00:28:31,919 --> 00:28:34,480
we can go back into our post.js and then
727
00:28:34,480 --> 00:28:36,720
there we can import all different
728
00:28:36,720 --> 00:28:39,760
functions for now we have only one
729
00:28:39,760 --> 00:28:41,440
that's going to look like this
730
00:28:41,440 --> 00:28:44,480
import and now inside of curly braces
731
00:28:44,480 --> 00:28:46,559
considering we're doing named and not
732
00:28:46,559 --> 00:28:49,360
default export in there we can name that
733
00:28:49,360 --> 00:28:52,880
function get posts from dot dot slash
734
00:28:52,880 --> 00:28:56,640
controllers slash posts that's it
735
00:28:56,640 --> 00:28:58,559
now the only thing you have to do is
736
00:28:58,559 --> 00:29:01,200
simply say router.get and then we
737
00:29:01,200 --> 00:29:04,159
execute a function called getposts in
738
00:29:04,159 --> 00:29:07,200
here don't forget to add posts.js in
739
00:29:07,200 --> 00:29:09,120
react we don't need to do that but in
740
00:29:09,120 --> 00:29:11,919
node we absolutely have to at this point
741
00:29:11,919 --> 00:29:14,080
i don't think you can see the purpose of
742
00:29:14,080 --> 00:29:15,919
having the controllers and routes
743
00:29:15,919 --> 00:29:18,240
separate which is completely fine but as
744
00:29:18,240 --> 00:29:20,080
we keep adding more routes you're gonna
745
00:29:20,080 --> 00:29:22,159
see how clean this is gonna look like
746
00:29:22,159 --> 00:29:24,159
we'll be able to see all the routes just
747
00:29:24,159 --> 00:29:26,320
like this and then execute all the
748
00:29:26,320 --> 00:29:28,720
functions in the other file great with
749
00:29:28,720 --> 00:29:31,120
that said let's create a new model for
750
00:29:31,120 --> 00:29:32,320
our post
751
00:29:32,320 --> 00:29:34,159
we're going to create a new folder
752
00:29:34,159 --> 00:29:36,559
called models and then inside of there
753
00:29:36,559 --> 00:29:39,120
we're going to create a file called post
754
00:29:39,120 --> 00:29:42,120
message.js
755
00:29:42,159 --> 00:29:43,679
inside of there we're going to utilize
756
00:29:43,679 --> 00:29:46,399
the possibilities of mongoose to do that
757
00:29:46,399 --> 00:29:49,679
we have to say import mongoose from
758
00:29:49,679 --> 00:29:51,679
mongoose
759
00:29:51,679 --> 00:29:54,159
now first we have to create a mongoose
760
00:29:54,159 --> 00:29:55,200
schema
761
00:29:55,200 --> 00:29:59,320
const post schema is equal to
762
00:29:59,320 --> 00:30:01,039
mongoose.schema
763
00:30:01,039 --> 00:30:03,200
and that's going to be a function which
764
00:30:03,200 --> 00:30:05,520
is going to have an object immediately
765
00:30:05,520 --> 00:30:07,120
in there that's it
766
00:30:07,120 --> 00:30:10,000
well what is a schema with mongodb you
767
00:30:10,000 --> 00:30:11,440
can create documents that look
768
00:30:11,440 --> 00:30:13,279
absolutely different one can have the
769
00:30:13,279 --> 00:30:15,440
title and the message what can only have
770
00:30:15,440 --> 00:30:18,480
the message and so on mongoose allows us
771
00:30:18,480 --> 00:30:20,480
to give some sort of uniformity to our
772
00:30:20,480 --> 00:30:22,720
documents we are going to specify that
773
00:30:22,720 --> 00:30:25,120
each post is going to have to have these
774
00:30:25,120 --> 00:30:27,919
things so in here each post is going to
775
00:30:27,919 --> 00:30:30,159
have a title which is going to be of a
776
00:30:30,159 --> 00:30:31,440
type string
777
00:30:31,440 --> 00:30:32,960
each post is also going to have a
778
00:30:32,960 --> 00:30:35,440
message which is going to be a string
779
00:30:35,440 --> 00:30:37,520
it's going to have a creator which is
780
00:30:37,520 --> 00:30:39,919
going to be also of type string we're
781
00:30:39,919 --> 00:30:41,760
going to have some tags which is going
782
00:30:41,760 --> 00:30:44,399
to be an array of strings so that that's
783
00:30:44,399 --> 00:30:46,720
why we put an array sign there we're
784
00:30:46,720 --> 00:30:49,039
going to have a selected file which is
785
00:30:49,039 --> 00:30:51,039
also going to be a string we're going to
786
00:30:51,039 --> 00:30:53,520
convert an image into a string using
787
00:30:53,520 --> 00:30:55,679
that base64
788
00:30:55,679 --> 00:30:57,840
then it's also going to have a like
789
00:30:57,840 --> 00:31:00,000
count this time we're not going to
790
00:31:00,000 --> 00:31:01,840
simply say a number
791
00:31:01,840 --> 00:31:03,840
we're going to say an object and then
792
00:31:03,840 --> 00:31:06,720
inside type is equal to a number because
793
00:31:06,720 --> 00:31:08,960
we have to add additional information
794
00:31:08,960 --> 00:31:10,960
and that additional information is that
795
00:31:10,960 --> 00:31:14,080
by default we want to set it to zero
796
00:31:14,080 --> 00:31:16,000
and finally we're going to have a
797
00:31:16,000 --> 00:31:18,640
created add property which is going to
798
00:31:18,640 --> 00:31:20,640
be an object again
799
00:31:20,640 --> 00:31:23,840
of type date and also it's going to have
800
00:31:23,840 --> 00:31:26,240
a default value of new
801
00:31:26,240 --> 00:31:29,039
date just like this make sure to call it
802
00:31:29,039 --> 00:31:31,360
and write it exactly like this now that
803
00:31:31,360 --> 00:31:33,760
we have a schema we have to turn it into
804
00:31:33,760 --> 00:31:35,840
a model to do that we're going to say
805
00:31:35,840 --> 00:31:39,039
const post message is going to be equal
806
00:31:39,039 --> 00:31:41,519
to mongoose.model
807
00:31:41,519 --> 00:31:44,880
and then in there we'll do post message
808
00:31:44,880 --> 00:31:46,880
make sure to do the same thing here
809
00:31:46,880 --> 00:31:48,799
exactly like this post message or you
810
00:31:48,799 --> 00:31:50,960
can call it anything you'd like and then
811
00:31:50,960 --> 00:31:53,519
the second parameter is going to be post
812
00:31:53,519 --> 00:31:55,760
schema that's it
813
00:31:55,760 --> 00:31:59,120
now we can do export default post
814
00:31:59,120 --> 00:32:01,919
message we are exporting a mongoose
815
00:32:01,919 --> 00:32:04,480
model from the post message file and
816
00:32:04,480 --> 00:32:06,799
then on that model later on we'll be
817
00:32:06,799 --> 00:32:09,600
able to run commands such as find create
818
00:32:09,600 --> 00:32:12,640
delete and update great now that our
819
00:32:12,640 --> 00:32:15,360
model is done we can start adding more
820
00:32:15,360 --> 00:32:17,760
routes inside of here i'm going to copy
821
00:32:17,760 --> 00:32:18,799
the line
822
00:32:18,799 --> 00:32:21,000
this route is going to be
823
00:32:21,000 --> 00:32:22,559
router.post
824
00:32:22,559 --> 00:32:24,559
and once we go to that route we're going
825
00:32:24,559 --> 00:32:28,240
to run the create post function which we
826
00:32:28,240 --> 00:32:30,320
are also going to import from the
827
00:32:30,320 --> 00:32:32,240
controllers so in here
828
00:32:32,240 --> 00:32:33,279
create
829
00:32:33,279 --> 00:32:34,159
post
830
00:32:34,159 --> 00:32:36,159
now we have to save it and go to the
831
00:32:36,159 --> 00:32:37,440
controllers
832
00:32:37,440 --> 00:32:40,640
in here we're going to say export const
833
00:32:40,640 --> 00:32:43,600
create post it's also going to be an
834
00:32:43,600 --> 00:32:45,600
error function which has a request and
835
00:32:45,600 --> 00:32:48,320
response and then in there for now let's
836
00:32:48,320 --> 00:32:49,760
do res that send
837
00:32:49,760 --> 00:32:52,000
post creation
838
00:32:52,000 --> 00:32:54,000
now is the time that we have to start
839
00:32:54,000 --> 00:32:56,480
implementing real logic for getting
840
00:32:56,480 --> 00:32:59,840
posts and for creating a post to do that
841
00:32:59,840 --> 00:33:01,840
we are going to import
842
00:33:01,840 --> 00:33:04,880
post message with a capital p
843
00:33:04,880 --> 00:33:06,159
from
844
00:33:06,159 --> 00:33:08,080
dot slash
845
00:33:08,080 --> 00:33:09,200
models
846
00:33:09,200 --> 00:33:12,080
and postmessage.js
847
00:33:12,080 --> 00:33:14,880
this gives us access to a real model
848
00:33:14,880 --> 00:33:17,039
what can we do with that well first of
849
00:33:17,039 --> 00:33:19,519
all each callback function is going to
850
00:33:19,519 --> 00:33:23,120
have a try and catch block it looks like
851
00:33:23,120 --> 00:33:25,679
this try opening and closing curly brace
852
00:33:25,679 --> 00:33:28,080
and then the catch with an error inside
853
00:33:28,080 --> 00:33:30,399
the code in the catch is gonna happen if
854
00:33:30,399 --> 00:33:32,559
we get an error right there and the code
855
00:33:32,559 --> 00:33:34,480
in the try is gonna happen if everything
856
00:33:34,480 --> 00:33:37,840
is successful so in here let's try to
857
00:33:37,840 --> 00:33:39,840
retrieve all the posts that we currently
858
00:33:39,840 --> 00:33:41,360
have in the database
859
00:33:41,360 --> 00:33:44,399
const post messages
860
00:33:44,399 --> 00:33:45,760
is equal to
861
00:33:45,760 --> 00:33:47,440
post message
862
00:33:47,440 --> 00:33:48,799
dot find
863
00:33:48,799 --> 00:33:50,320
just like that
864
00:33:50,320 --> 00:33:52,399
now this wouldn't be correct because
865
00:33:52,399 --> 00:33:54,559
post message that find or finding
866
00:33:54,559 --> 00:33:57,360
something inside of a model takes time
867
00:33:57,360 --> 00:33:59,679
which means that it is an asynchronous
868
00:33:59,679 --> 00:34:02,799
action for that reason we have to add a
869
00:34:02,799 --> 00:34:05,200
weight in front of it and therefore we
870
00:34:05,200 --> 00:34:08,560
have to make this function asynchronous
871
00:34:08,560 --> 00:34:11,119
after that we can console.log our post
872
00:34:11,119 --> 00:34:13,280
messages
873
00:34:13,280 --> 00:34:15,359
and finally we need to make our function
874
00:34:15,359 --> 00:34:17,918
return something so if everything works
875
00:34:17,918 --> 00:34:20,079
then we're going to say res does status
876
00:34:20,079 --> 00:34:23,040
of 200 which means everything went okay
877
00:34:23,040 --> 00:34:25,280
and then we're going to return json
878
00:34:25,280 --> 00:34:27,599
which is going to simply be an array of
879
00:34:27,599 --> 00:34:30,480
all messages that we have we don't even
880
00:34:30,480 --> 00:34:32,560
have to cons log it we're just returning
881
00:34:32,560 --> 00:34:35,119
it that's it we also have to respond
882
00:34:35,119 --> 00:34:36,800
with something if there's an error so
883
00:34:36,800 --> 00:34:39,199
we're going to say rest that status and
884
00:34:39,199 --> 00:34:42,320
then in here let's do 404 and then dot
885
00:34:42,320 --> 00:34:43,839
json
886
00:34:43,839 --> 00:34:46,079
and we're going to return message to be
887
00:34:46,079 --> 00:34:49,199
equal to error dot message just like
888
00:34:49,199 --> 00:34:50,000
that
889
00:34:50,000 --> 00:34:52,560
now this is it for our get posts and how
890
00:34:52,560 --> 00:34:55,199
can we see if it works well our server
891
00:34:55,199 --> 00:34:57,520
is already running on localhost 5000 so
892
00:34:57,520 --> 00:34:59,839
let's go check it out and that's it you
893
00:34:59,839 --> 00:35:01,599
can see that we have an empty array this
894
00:35:01,599 --> 00:35:03,200
is not that interesting right we just
895
00:35:03,200 --> 00:35:05,200
have an empty array now we have to
896
00:35:05,200 --> 00:35:07,520
implement the logic for adding different
897
00:35:07,520 --> 00:35:09,680
posts and we're going to do that right
898
00:35:09,680 --> 00:35:12,480
here to create a post we're also going
899
00:35:12,480 --> 00:35:15,359
to do a try and catch block with post
900
00:35:15,359 --> 00:35:17,440
requests you have access to something
901
00:35:17,440 --> 00:35:19,920
known as a request that body so we're
902
00:35:19,920 --> 00:35:21,920
going to say body is equal to wreck that
903
00:35:21,920 --> 00:35:24,480
body and we won't be able to cancel like
904
00:35:24,480 --> 00:35:27,040
this right now for the reason that we
905
00:35:27,040 --> 00:35:29,119
don't have a way to send post requests
906
00:35:29,119 --> 00:35:31,520
yet that's the exact reason why
907
00:35:31,520 --> 00:35:33,680
immediately after we create this create
908
00:35:33,680 --> 00:35:35,440
post we're going to move on to the
909
00:35:35,440 --> 00:35:38,000
front-end side to create a form and
910
00:35:38,000 --> 00:35:40,640
basic layout for making different posts
911
00:35:40,640 --> 00:35:42,800
our request that body is actually going
912
00:35:42,800 --> 00:35:45,440
to be our post so we can name it like so
913
00:35:45,440 --> 00:35:47,520
and then we need to create a new post we
914
00:35:47,520 --> 00:35:50,720
do that by typing const new post is
915
00:35:50,720 --> 00:35:54,000
equal to new post message
916
00:35:54,000 --> 00:35:56,560
we call it and then in there we pass
917
00:35:56,560 --> 00:35:58,240
that values that we're receiving to the
918
00:35:58,240 --> 00:36:00,560
request that body yet we are not sending
919
00:36:00,560 --> 00:36:02,480
them but we're going to do that once we
920
00:36:02,480 --> 00:36:04,800
implement the logic on the front end
921
00:36:04,800 --> 00:36:07,200
then in the try block we're going to say
922
00:36:07,200 --> 00:36:08,720
new post
923
00:36:08,720 --> 00:36:10,079
dot save
924
00:36:10,079 --> 00:36:12,240
this is again an asynchronous action
925
00:36:12,240 --> 00:36:14,000
which means that we have to add a weight
926
00:36:14,000 --> 00:36:16,640
in front of it and also async on the
927
00:36:16,640 --> 00:36:19,119
whole function once that is saved we're
928
00:36:19,119 --> 00:36:21,839
going to respond with resda status
929
00:36:21,839 --> 00:36:24,480
201 which means successful creation and
930
00:36:24,480 --> 00:36:26,800
then that json we're just going to send
931
00:36:26,800 --> 00:36:29,440
that new post in that's it
932
00:36:29,440 --> 00:36:31,599
if it's not successful we do res dot
933
00:36:31,599 --> 00:36:32,960
status
934
00:36:32,960 --> 00:36:34,560
and then 409
935
00:36:34,560 --> 00:36:37,440
that's json and then in there we do
936
00:36:37,440 --> 00:36:41,280
message is equal to error dot message
937
00:36:41,280 --> 00:36:43,760
you can learn more about this http codes
938
00:36:43,760 --> 00:36:45,680
right there under the following link
939
00:36:45,680 --> 00:36:47,359
it's going to be in the description but
940
00:36:47,359 --> 00:36:49,359
generally it looks like this all
941
00:36:49,359 --> 00:36:51,119
requests that starts with 200 are
942
00:36:51,119 --> 00:36:55,040
successful 300s are redirections 400 are
943
00:36:55,040 --> 00:36:58,400
client errors and 500 are server errors
944
00:36:58,400 --> 00:37:00,160
you can take a look for example you can
945
00:37:00,160 --> 00:37:02,079
read only those with stars these are the
946
00:37:02,079 --> 00:37:04,320
most important ones and then see what
947
00:37:04,320 --> 00:37:06,800
each one says in here 200 is the most
948
00:37:06,800 --> 00:37:09,359
popular one the request has succeeded
949
00:37:09,359 --> 00:37:10,800
great the link is going to be in the
950
00:37:10,800 --> 00:37:12,000
description
951
00:37:12,000 --> 00:37:14,000
now we have our create post function
952
00:37:14,000 --> 00:37:16,160
completely done and now is the time that
953
00:37:16,160 --> 00:37:18,640
we focus more on client and implement
954
00:37:18,640 --> 00:37:20,560
the logic for actually sending or
955
00:37:20,560 --> 00:37:23,520
creating posts moving to the client side
956
00:37:23,520 --> 00:37:25,040
we are now going to create a whole
957
00:37:25,040 --> 00:37:27,839
skeleton for the front-end application
958
00:37:27,839 --> 00:37:29,359
there is one thing that i forgot to tell
959
00:37:29,359 --> 00:37:31,119
you to install at the beginning and that
960
00:37:31,119 --> 00:37:34,560
is npm install add material
961
00:37:34,560 --> 00:37:38,560
minus ui and then forward slash core
962
00:37:38,560 --> 00:37:40,640
this is going to be the ui kit we'll be
963
00:37:40,640 --> 00:37:43,040
using material ui is really popular when
964
00:37:43,040 --> 00:37:45,359
working with react because it allows you
965
00:37:45,359 --> 00:37:47,440
to create nice looking applications
966
00:37:47,440 --> 00:37:49,680
without a lot of styling while that is
967
00:37:49,680 --> 00:37:52,000
installing let's create our components
968
00:37:52,000 --> 00:37:53,280
folder
969
00:37:53,280 --> 00:37:55,280
inside of there we're going to have two
970
00:37:55,280 --> 00:37:58,880
folders one is going to be called posts
971
00:37:58,880 --> 00:38:01,119
and the other one is going to be called
972
00:38:01,119 --> 00:38:02,079
form
973
00:38:02,079 --> 00:38:04,160
inside of the form we're going to create
974
00:38:04,160 --> 00:38:07,119
a form.js file which is going to be our
975
00:38:07,119 --> 00:38:09,760
form component inside of the posts we're
976
00:38:09,760 --> 00:38:11,000
going to create
977
00:38:11,000 --> 00:38:13,200
posts.js file which is going to be the
978
00:38:13,200 --> 00:38:15,520
component for the posts each one of
979
00:38:15,520 --> 00:38:17,680
these is also going to have its own
980
00:38:17,680 --> 00:38:19,520
styles.js
981
00:38:19,520 --> 00:38:21,359
we'll be using materials ui way of
982
00:38:21,359 --> 00:38:24,640
styling yeah it's css and js but it's
983
00:38:24,640 --> 00:38:26,079
it's not that awful you're going to see
984
00:38:26,079 --> 00:38:28,680
it soon and then again
985
00:38:28,680 --> 00:38:31,200
styles.js in the posts that's why we
986
00:38:31,200 --> 00:38:33,280
have folders the last thing we have to
987
00:38:33,280 --> 00:38:35,040
do is create one more folder which is
988
00:38:35,040 --> 00:38:37,359
going to be inside of the posts folder
989
00:38:37,359 --> 00:38:40,160
and that name is going to be post inside
990
00:38:40,160 --> 00:38:41,839
of there we are also going to create
991
00:38:41,839 --> 00:38:43,599
just one thing which is going to be
992
00:38:43,599 --> 00:38:49,119
post.js file and also styles.js
993
00:38:49,119 --> 00:38:50,800
this is the folder structure that works
994
00:38:50,800 --> 00:38:52,800
really well in medium to large level
995
00:38:52,800 --> 00:38:55,040
applications you can see everything is
996
00:38:55,040 --> 00:38:57,119
nicely put inside of the folders we have
997
00:38:57,119 --> 00:38:59,440
the styles there and if only one
998
00:38:59,440 --> 00:39:01,839
component uses some other component like
999
00:39:01,839 --> 00:39:04,640
only posts uses post then that post
1000
00:39:04,640 --> 00:39:06,960
component is going to be inside of there
1001
00:39:06,960 --> 00:39:08,320
maybe it doesn't make a lot of sense
1002
00:39:08,320 --> 00:39:10,240
right now because we just added a lot of
1003
00:39:10,240 --> 00:39:12,160
files but not a lot of code but it's
1004
00:39:12,160 --> 00:39:13,839
going to make more sense once we
1005
00:39:13,839 --> 00:39:16,320
actually start writing code great now
1006
00:39:16,320 --> 00:39:17,440
that we have the structure of our
1007
00:39:17,440 --> 00:39:19,440
application let's actually start with
1008
00:39:19,440 --> 00:39:22,320
app.js first of all i'm going to run the
1009
00:39:22,320 --> 00:39:25,200
application again by typing npm start
1010
00:39:25,200 --> 00:39:27,119
and now we can use material ui
1011
00:39:27,119 --> 00:39:29,599
components so first of all at the top
1012
00:39:29,599 --> 00:39:31,440
i'm going to import all the components
1013
00:39:31,440 --> 00:39:33,680
we're going to use in this app.js file
1014
00:39:33,680 --> 00:39:35,200
all components start with the capital
1015
00:39:35,200 --> 00:39:37,119
letter and these are going to be
1016
00:39:37,119 --> 00:39:38,880
container
1017
00:39:38,880 --> 00:39:40,880
app bar
1018
00:39:40,880 --> 00:39:43,280
typography
1019
00:39:43,280 --> 00:39:44,560
grow
1020
00:39:44,560 --> 00:39:45,839
and grid
1021
00:39:45,839 --> 00:39:48,480
and we're going to import them from
1022
00:39:48,480 --> 00:39:51,440
add material ui and then forward slash
1023
00:39:51,440 --> 00:39:52,240
core
1024
00:39:52,240 --> 00:39:53,119
great
1025
00:39:53,119 --> 00:39:54,960
inside of here let's start with basic
1026
00:39:54,960 --> 00:39:57,119
structure i'm going to delete this div
1027
00:39:57,119 --> 00:39:59,280
and create a container that's going to
1028
00:39:59,280 --> 00:40:02,160
make sure to just center everything
1029
00:40:02,160 --> 00:40:03,680
inside of there i'm going to put the
1030
00:40:03,680 --> 00:40:06,640
property called max with equal to lg
1031
00:40:06,640 --> 00:40:08,400
which is large
1032
00:40:08,400 --> 00:40:10,000
and then inside of there first thing
1033
00:40:10,000 --> 00:40:12,720
that we have is going to be the app bar
1034
00:40:12,720 --> 00:40:15,599
app bar is going to have a position of
1035
00:40:15,599 --> 00:40:16,800
static
1036
00:40:16,800 --> 00:40:20,240
and also a color of inherit
1037
00:40:20,240 --> 00:40:22,240
inside of the app bar we are going to
1038
00:40:22,240 --> 00:40:24,720
have a typography typography in material
1039
00:40:24,720 --> 00:40:27,040
ui basically stands for any textual
1040
00:40:27,040 --> 00:40:30,000
element like h2 paragraphs or really
1041
00:40:30,000 --> 00:40:32,160
anything but it gives it a nice looking
1042
00:40:32,160 --> 00:40:34,640
font so in there we can see variant is
1043
00:40:34,640 --> 00:40:37,119
going to be equal to h2 and we are also
1044
00:40:37,119 --> 00:40:39,760
going to have a line equal to center
1045
00:40:39,760 --> 00:40:41,280
in there we can put the title of our
1046
00:40:41,280 --> 00:40:43,520
application in our case that's going to
1047
00:40:43,520 --> 00:40:46,240
be memories and this is supposed to be
1048
00:40:46,240 --> 00:40:48,640
variant just like that and then we're
1049
00:40:48,640 --> 00:40:51,440
going to have our image so image
1050
00:40:51,440 --> 00:40:53,520
in there we're going to have an src
1051
00:40:53,520 --> 00:40:56,240
which is going to say memories alt can
1052
00:40:56,240 --> 00:40:58,319
be memories
1053
00:40:58,319 --> 00:41:00,400
and then we can put the height to be
1054
00:41:00,400 --> 00:41:03,040
equal to 60. we don't need it to be more
1055
00:41:03,040 --> 00:41:05,680
than 60. now we don't have this image
1056
00:41:05,680 --> 00:41:07,680
yet so let's create it i'm going to
1057
00:41:07,680 --> 00:41:10,079
leave the link to the image down below
1058
00:41:10,079 --> 00:41:12,240
and then what we can do is you can
1059
00:41:12,240 --> 00:41:14,960
create a new folder in the src called
1060
00:41:14,960 --> 00:41:16,160
images
1061
00:41:16,160 --> 00:41:17,680
and then inside of there you can
1062
00:41:17,680 --> 00:41:20,040
download and put that image
1063
00:41:20,040 --> 00:41:22,880
memories.png looks like this just to
1064
00:41:22,880 --> 00:41:25,040
give some life to our application i
1065
00:41:25,040 --> 00:41:27,280
misspelled container there as well now
1066
00:41:27,280 --> 00:41:29,440
we can import that image that's going to
1067
00:41:29,440 --> 00:41:31,359
be import memories
1068
00:41:31,359 --> 00:41:33,359
from dot slash
1069
00:41:33,359 --> 00:41:37,280
images slash memories
1070
00:41:37,280 --> 00:41:39,200
dot png
1071
00:41:39,200 --> 00:41:41,599
great now if you save this let's see if
1072
00:41:41,599 --> 00:41:43,119
it works
1073
00:41:43,119 --> 00:41:45,440
okay and we have a huge image right
1074
00:41:45,440 --> 00:41:47,520
there once we add some styles this
1075
00:41:47,520 --> 00:41:49,520
should look much better before adding
1076
00:41:49,520 --> 00:41:50,880
the styles let's just finish with the
1077
00:41:50,880 --> 00:41:53,599
structure below this app bar we're going
1078
00:41:53,599 --> 00:41:56,240
to have a grow component grow just
1079
00:41:56,240 --> 00:41:58,960
provides some simple animation grow is
1080
00:41:58,960 --> 00:42:00,880
going to have a property of n so it's
1081
00:42:00,880 --> 00:42:02,480
going to grow in
1082
00:42:02,480 --> 00:42:04,480
and then inside of that grow we're also
1083
00:42:04,480 --> 00:42:07,440
going to have one more container inside
1084
00:42:07,440 --> 00:42:09,440
of that container we're going to have a
1085
00:42:09,440 --> 00:42:11,359
grid component
1086
00:42:11,359 --> 00:42:13,599
that grid is going to be of a type
1087
00:42:13,599 --> 00:42:14,880
container
1088
00:42:14,880 --> 00:42:17,680
it's going to say justify to space
1089
00:42:17,680 --> 00:42:19,119
between
1090
00:42:19,119 --> 00:42:22,960
and also align items to stretch
1091
00:42:22,960 --> 00:42:24,400
and we're also going to give it some
1092
00:42:24,400 --> 00:42:27,599
spacing which is going to be equal to 3
1093
00:42:27,599 --> 00:42:29,920
that's it now inside of that grid which
1094
00:42:29,920 --> 00:42:32,079
is of type container we can add two
1095
00:42:32,079 --> 00:42:34,880
different grid items let's do that
1096
00:42:34,880 --> 00:42:35,839
grid
1097
00:42:35,839 --> 00:42:37,920
and then item
1098
00:42:37,920 --> 00:42:39,920
access is going to be 12 which means
1099
00:42:39,920 --> 00:42:41,599
that it's going to take the full width
1100
00:42:41,599 --> 00:42:44,560
on extra small devices and on small and
1101
00:42:44,560 --> 00:42:46,720
medium it's going to be 7 which means
1102
00:42:46,720 --> 00:42:48,640
that it's going to take 7 out of 12
1103
00:42:48,640 --> 00:42:52,240
spaces on small or larger devices
1104
00:42:52,240 --> 00:42:54,160
great so this is our grid component and
1105
00:42:54,160 --> 00:42:56,079
then i'm going to copy it and paste it
1106
00:42:56,079 --> 00:42:58,319
below this one is also going to be extra
1107
00:42:58,319 --> 00:43:00,560
small 12 but we're going to change the
1108
00:43:00,560 --> 00:43:03,599
small to b4 inside of this first grid
1109
00:43:03,599 --> 00:43:06,240
we're going to have our posts component
1110
00:43:06,240 --> 00:43:08,160
we created the file for it but we didn't
1111
00:43:08,160 --> 00:43:10,800
yet create or import the component and
1112
00:43:10,800 --> 00:43:13,040
then just below in here we're going to
1113
00:43:13,040 --> 00:43:15,839
have our form component now we have to
1114
00:43:15,839 --> 00:43:17,920
create those components and then import
1115
00:43:17,920 --> 00:43:20,240
them right there so let's do that
1116
00:43:20,240 --> 00:43:22,319
let's go into the form and then inside
1117
00:43:22,319 --> 00:43:24,560
of here we're going to import
1118
00:43:24,560 --> 00:43:27,359
react from react
1119
00:43:27,359 --> 00:43:30,720
const form is equal to
1120
00:43:30,720 --> 00:43:32,640
a normal functional component which is
1121
00:43:32,640 --> 00:43:35,200
going to return just an h1 that says
1122
00:43:35,200 --> 00:43:36,720
form for now
1123
00:43:36,720 --> 00:43:39,440
and then we're going to export
1124
00:43:39,440 --> 00:43:40,720
default
1125
00:43:40,720 --> 00:43:41,839
form
1126
00:43:41,839 --> 00:43:44,319
that's it i'm going to copy all of this
1127
00:43:44,319 --> 00:43:47,359
now go into the posts file
1128
00:43:47,359 --> 00:43:49,520
paste it there and then just change the
1129
00:43:49,520 --> 00:43:50,400
form
1130
00:43:50,400 --> 00:43:54,079
to say posts
1131
00:43:54,079 --> 00:43:56,880
and then change this to posts
1132
00:43:56,880 --> 00:43:58,960
and finally we're going to do the same
1133
00:43:58,960 --> 00:44:01,599
thing for the singular post component in
1134
00:44:01,599 --> 00:44:04,240
here i'm going to change posts to simply
1135
00:44:04,240 --> 00:44:05,119
say
1136
00:44:05,119 --> 00:44:07,680
post that's it
1137
00:44:07,680 --> 00:44:10,400
we know our hierarchy our posts is going
1138
00:44:10,400 --> 00:44:12,560
to use the post component so what we're
1139
00:44:12,560 --> 00:44:14,400
going to do is we're going to import
1140
00:44:14,400 --> 00:44:17,520
that post into our posts so right there
1141
00:44:17,520 --> 00:44:20,400
below the title we need to add a react
1142
00:44:20,400 --> 00:44:22,400
fragment so we can add multiple things
1143
00:44:22,400 --> 00:44:23,440
in there
1144
00:44:23,440 --> 00:44:26,800
below posts we're going to import
1145
00:44:26,800 --> 00:44:31,119
the post component so import post from
1146
00:44:31,280 --> 00:44:35,200
dot slash post slash post and now we can
1147
00:44:35,200 --> 00:44:37,839
create multiple post components just
1148
00:44:37,839 --> 00:44:40,640
like so let's add two for now
1149
00:44:40,640 --> 00:44:42,880
great the last thing we have to do is
1150
00:44:42,880 --> 00:44:45,520
collapse this go into the app and then
1151
00:44:45,520 --> 00:44:47,440
finally import the components we just
1152
00:44:47,440 --> 00:44:50,480
exported that's gonna be import
1153
00:44:50,480 --> 00:44:52,400
posts from
1154
00:44:52,400 --> 00:44:55,520
dot slash components slash posts and we
1155
00:44:55,520 --> 00:44:57,680
need to go one level further to slash
1156
00:44:57,680 --> 00:45:00,800
posts and finally you can copy the line
1157
00:45:00,800 --> 00:45:03,760
and then change this to form
1158
00:45:03,760 --> 00:45:05,920
that's it now we are importing the posts
1159
00:45:05,920 --> 00:45:08,880
and the form as well in here and now we
1160
00:45:08,880 --> 00:45:10,640
shouldn't get any warnings or errors
1161
00:45:10,640 --> 00:45:12,480
that we don't have these components here
1162
00:45:12,480 --> 00:45:14,079
because they are there you can see the
1163
00:45:14,079 --> 00:45:16,480
app is compiled and running on localhost
1164
00:45:16,480 --> 00:45:19,280
3000. let's visit in the browser and see
1165
00:45:19,280 --> 00:45:21,760
what we have let's be honest this looks
1166
00:45:21,760 --> 00:45:23,680
pretty bad for now but we're going to
1167
00:45:23,680 --> 00:45:25,920
keep adding more styles and structure to
1168
00:45:25,920 --> 00:45:28,720
it firstly let's add styles to our
1169
00:45:28,720 --> 00:45:32,160
app.js here is how we can properly add
1170
00:45:32,160 --> 00:45:35,119
styles to a material ui application
1171
00:45:35,119 --> 00:45:37,760
inside of the src folder we are also
1172
00:45:37,760 --> 00:45:41,200
going to create a styles.js file in here
1173
00:45:41,200 --> 00:45:42,800
we need to import
1174
00:45:42,800 --> 00:45:45,520
make styles
1175
00:45:45,520 --> 00:45:46,560
from
1176
00:45:46,560 --> 00:45:49,760
add material ui core and then slash
1177
00:45:49,760 --> 00:45:51,359
styles
1178
00:45:51,359 --> 00:45:53,680
make sure to import it not as a default
1179
00:45:53,680 --> 00:45:55,599
import but as a named import instead of
1180
00:45:55,599 --> 00:45:59,200
curly braces and then below we do export
1181
00:45:59,200 --> 00:46:02,560
default we call that make styles
1182
00:46:02,560 --> 00:46:04,720
and now inside of there we put a
1183
00:46:04,720 --> 00:46:06,960
callback function
1184
00:46:06,960 --> 00:46:09,839
which immediately returns an object this
1185
00:46:09,839 --> 00:46:12,480
video is about creating a mern stack
1186
00:46:12,480 --> 00:46:14,000
application connecting it to the
1187
00:46:14,000 --> 00:46:15,760
database connecting the server to the
1188
00:46:15,760 --> 00:46:17,280
front end and everything else that we
1189
00:46:17,280 --> 00:46:19,760
still have to do for that reason you're
1190
00:46:19,760 --> 00:46:21,839
not going to have to type the styles i'm
1191
00:46:21,839 --> 00:46:23,680
going to leave all the files for the
1192
00:46:23,680 --> 00:46:26,000
styles down in the description and
1193
00:46:26,000 --> 00:46:28,000
you'll be able to copy them so let's
1194
00:46:28,000 --> 00:46:30,000
start with this one you're gonna have
1195
00:46:30,000 --> 00:46:32,319
these styles make sure to copy these
1196
00:46:32,319 --> 00:46:34,800
from the description save it and then go
1197
00:46:34,800 --> 00:46:37,280
back to the app.js in here we have to
1198
00:46:37,280 --> 00:46:38,800
say import
1199
00:46:38,800 --> 00:46:41,520
use styles
1200
00:46:41,520 --> 00:46:44,319
that slash styles and the last thing we
1201
00:46:44,319 --> 00:46:47,520
have to do is say const classes is equal
1202
00:46:47,520 --> 00:46:50,400
to use styles
1203
00:46:50,400 --> 00:46:53,200
and call it just like so so this is
1204
00:46:53,200 --> 00:46:54,480
going to be the process for all
1205
00:46:54,480 --> 00:46:56,079
different components again we're just
1206
00:46:56,079 --> 00:46:57,839
going to copy and paste it and you don't
1207
00:46:57,839 --> 00:46:59,920
have to worry about styles the only
1208
00:46:59,920 --> 00:47:01,680
thing you have to do is actually add
1209
00:47:01,680 --> 00:47:03,920
them in here so let's start with the app
1210
00:47:03,920 --> 00:47:07,359
bar first in here let's do class name is
1211
00:47:07,359 --> 00:47:10,720
equal to classes that app bar so the
1212
00:47:10,720 --> 00:47:13,440
same we called it right here
1213
00:47:13,440 --> 00:47:15,760
great then we have to add the classes
1214
00:47:15,760 --> 00:47:18,560
for the image so class name is equal to
1215
00:47:18,560 --> 00:47:21,920
classes that image and then finally
1216
00:47:21,920 --> 00:47:25,119
class name is equal to classes dot
1217
00:47:25,119 --> 00:47:27,760
heading right here and i just noticed
1218
00:47:27,760 --> 00:47:30,480
that i have a typo here this should be
1219
00:47:30,480 --> 00:47:33,440
height spelled like so great now if we
1220
00:47:33,440 --> 00:47:35,359
go back to our application let's see how
1221
00:47:35,359 --> 00:47:36,640
it looks like
1222
00:47:36,640 --> 00:47:39,200
as you can see this is much better now
1223
00:47:39,200 --> 00:47:41,359
let's add the styles for all the other
1224
00:47:41,359 --> 00:47:44,640
components meaning posts post and the
1225
00:47:44,640 --> 00:47:47,280
form you can do that by going into these
1226
00:47:47,280 --> 00:47:50,240
styles styles.js and then just copying
1227
00:47:50,240 --> 00:47:52,000
the link in the description you should
1228
00:47:52,000 --> 00:47:53,839
be able to find all the styles right
1229
00:47:53,839 --> 00:47:54,720
there
1230
00:47:54,720 --> 00:47:56,800
there we go i copy and pasted all the
1231
00:47:56,800 --> 00:47:58,640
styles for the form
1232
00:47:58,640 --> 00:48:01,680
and now going into the post.js i'm gonna
1233
00:48:01,680 --> 00:48:03,760
copy and paste all the styles for the
1234
00:48:03,760 --> 00:48:05,599
post.js as well
1235
00:48:05,599 --> 00:48:08,800
right here that's it and finally styles
1236
00:48:08,800 --> 00:48:11,359
for the posts plural this time we don't
1237
00:48:11,359 --> 00:48:14,160
have a lot that's it after you copy and
1238
00:48:14,160 --> 00:48:16,400
pasted all these styles the last thing
1239
00:48:16,400 --> 00:48:18,960
that you have to do is copy the same two
1240
00:48:18,960 --> 00:48:21,440
lines that's this thing here we have to
1241
00:48:21,440 --> 00:48:24,000
copy the import use styles and we also
1242
00:48:24,000 --> 00:48:26,800
have to copy cons classes is equal to
1243
00:48:26,800 --> 00:48:30,079
use styles so you can hold alt or option
1244
00:48:30,079 --> 00:48:32,559
and then copy this line and then keep
1245
00:48:32,559 --> 00:48:34,319
copying this line then you can press
1246
00:48:34,319 --> 00:48:37,280
ctrl c or command c to copy both lines
1247
00:48:37,280 --> 00:48:39,280
and let's add the same thing to all
1248
00:48:39,280 --> 00:48:41,599
these files that's going to be import
1249
00:48:41,599 --> 00:48:43,680
that and then finally drag and drop it
1250
00:48:43,680 --> 00:48:44,800
here
1251
00:48:44,800 --> 00:48:47,200
same thing goes for the post inside of
1252
00:48:47,200 --> 00:48:50,559
here you paste it import goes at the top
1253
00:48:50,559 --> 00:48:52,720
and then we have cons classes so that
1254
00:48:52,720 --> 00:48:55,040
we'll be able to use the classes and the
1255
00:48:55,040 --> 00:48:57,760
last thing is in the posts in here we
1256
00:48:57,760 --> 00:49:00,000
also have to import the used styles and
1257
00:49:00,000 --> 00:49:02,640
then finally use that classes
1258
00:49:02,640 --> 00:49:04,960
great with this all the classes are now
1259
00:49:04,960 --> 00:49:07,119
imported and then we'll keep adding the
1260
00:49:07,119 --> 00:49:09,280
jsx and the only thing you'll have to do
1261
00:49:09,280 --> 00:49:11,760
is say to something for example
1262
00:49:11,760 --> 00:49:14,359
classname is equal to
1263
00:49:14,359 --> 00:49:16,640
classes.something that's it we
1264
00:49:16,640 --> 00:49:18,480
simplified your process there because
1265
00:49:18,480 --> 00:49:20,800
this video is long enough even without
1266
00:49:20,800 --> 00:49:22,559
adding the styles we have to focus on
1267
00:49:22,559 --> 00:49:24,319
things that really matter connecting
1268
00:49:24,319 --> 00:49:26,160
this to the backend and connecting it to
1269
00:49:26,160 --> 00:49:27,280
the database
1270
00:49:27,280 --> 00:49:29,440
now let's start implementing the calls
1271
00:49:29,440 --> 00:49:32,079
to the api we're going to close this
1272
00:49:32,079 --> 00:49:33,359
collapse it
1273
00:49:33,359 --> 00:49:35,839
go to client and then in the client
1274
00:49:35,839 --> 00:49:37,760
we're going to create a new folder
1275
00:49:37,760 --> 00:49:40,079
called api
1276
00:49:40,079 --> 00:49:42,319
inside of the api we're going to have an
1277
00:49:42,319 --> 00:49:45,599
index.js file and inside of there we're
1278
00:49:45,599 --> 00:49:48,480
going to import axios from axios we're
1279
00:49:48,480 --> 00:49:50,800
going to use it to make api calls
1280
00:49:50,800 --> 00:49:53,359
in here we have to specify our url and
1281
00:49:53,359 --> 00:49:55,760
that url is going to be a string that's
1282
00:49:55,760 --> 00:49:56,839
going to say
1283
00:49:56,839 --> 00:49:59,520
http and then localhost
1284
00:49:59,520 --> 00:50:03,200
5000 slash posts as we discussed on the
1285
00:50:03,200 --> 00:50:06,160
backend side so this is the url pointing
1286
00:50:06,160 --> 00:50:09,440
to our backend route and now in here we
1287
00:50:09,440 --> 00:50:12,640
can create a function called fetch posts
1288
00:50:12,640 --> 00:50:14,240
which is going to be equal to an arrow
1289
00:50:14,240 --> 00:50:16,640
function that's simply going to make an
1290
00:50:16,640 --> 00:50:20,880
axios dot get call to our url as you
1291
00:50:20,880 --> 00:50:24,000
remember localhost 5000 posts simply
1292
00:50:24,000 --> 00:50:25,920
returns all the posts that we currently
1293
00:50:25,920 --> 00:50:27,760
have in the database
1294
00:50:27,760 --> 00:50:29,200
now we're not going to use it in this
1295
00:50:29,200 --> 00:50:31,599
file but rather we're going to export
1296
00:50:31,599 --> 00:50:34,240
that function now we have to focus on
1297
00:50:34,240 --> 00:50:37,119
adding redux capabilities because all
1298
00:50:37,119 --> 00:50:39,760
actions towards our backend are going to
1299
00:50:39,760 --> 00:50:43,200
be done using redux we need to dispatch
1300
00:50:43,200 --> 00:50:46,240
those actions to do that we have to add
1301
00:50:46,240 --> 00:50:48,720
some boilerplate code meaning we do have
1302
00:50:48,720 --> 00:50:50,960
to create a few files and folders but
1303
00:50:50,960 --> 00:50:53,440
later on on bigger applications this is
1304
00:50:53,440 --> 00:50:55,520
going to be extremely great because of
1305
00:50:55,520 --> 00:50:58,400
the scalability and i agree to implement
1306
00:50:58,400 --> 00:51:00,800
redux you have to add a lot of files and
1307
00:51:00,800 --> 00:51:02,880
folders but it's going to be great in
1308
00:51:02,880 --> 00:51:04,720
the long run because it makes our
1309
00:51:04,720 --> 00:51:07,440
application scalable that means that as
1310
00:51:07,440 --> 00:51:09,440
our application grows we'll be able to
1311
00:51:09,440 --> 00:51:12,079
use that same old consistency redux
1312
00:51:12,079 --> 00:51:15,200
offers us without any trouble great
1313
00:51:15,200 --> 00:51:17,040
so to do that we're first going to
1314
00:51:17,040 --> 00:51:19,920
create a folder called actions
1315
00:51:19,920 --> 00:51:22,640
and a folder called reducers
1316
00:51:22,640 --> 00:51:24,400
inside of actions we're going to create
1317
00:51:24,400 --> 00:51:27,280
a file called posts.js
1318
00:51:27,280 --> 00:51:29,280
and we're going to do the same thing for
1319
00:51:29,280 --> 00:51:31,920
reducers
1320
00:51:31,920 --> 00:51:34,000
inside of the reducers we are also going
1321
00:51:34,000 --> 00:51:36,480
to have an index.js file
1322
00:51:36,480 --> 00:51:38,559
and this also seems like a good starting
1323
00:51:38,559 --> 00:51:40,800
point now that that is set up we can go
1324
00:51:40,800 --> 00:51:42,880
to our index.js file and this is the
1325
00:51:42,880 --> 00:51:44,960
place where we are going to initialize
1326
00:51:44,960 --> 00:51:46,640
redux what are some of the things that
1327
00:51:46,640 --> 00:51:49,680
we need well let's start import
1328
00:51:49,680 --> 00:51:51,119
provider
1329
00:51:51,119 --> 00:51:52,000
from
1330
00:51:52,000 --> 00:51:54,880
react redux provider is going to keep
1331
00:51:54,880 --> 00:51:57,119
track of that store which is that global
1332
00:51:57,119 --> 00:52:00,079
state and that allows us to access that
1333
00:52:00,079 --> 00:52:02,960
store from anywhere inside of the app we
1334
00:52:02,960 --> 00:52:05,280
don't have to be exactly in a parent
1335
00:52:05,280 --> 00:52:07,200
component or in a child component we can
1336
00:52:07,200 --> 00:52:09,839
access that state from anywhere then
1337
00:52:09,839 --> 00:52:11,440
we're going to import
1338
00:52:11,440 --> 00:52:13,760
create store
1339
00:52:13,760 --> 00:52:17,680
apply middleware and also compose
1340
00:52:17,680 --> 00:52:18,400
from
1341
00:52:18,400 --> 00:52:20,079
redux
1342
00:52:20,079 --> 00:52:23,680
finally we need to import thunk from
1343
00:52:23,680 --> 00:52:26,400
redux minus thunk
1344
00:52:26,400 --> 00:52:27,680
that's it
1345
00:52:27,680 --> 00:52:29,520
now let's set it up
1346
00:52:29,520 --> 00:52:31,680
to set redux we first have to create
1347
00:52:31,680 --> 00:52:33,839
store so that's going to be cons store
1348
00:52:33,839 --> 00:52:36,480
is equal to create store
1349
00:52:36,480 --> 00:52:38,720
create store takes in two different
1350
00:52:38,720 --> 00:52:41,040
things first we have the reducers which
1351
00:52:41,040 --> 00:52:42,720
we didn't define yet as you can see they
1352
00:52:42,720 --> 00:52:45,119
are red we need to define the reducers
1353
00:52:45,119 --> 00:52:46,720
and the second thing is going to be
1354
00:52:46,720 --> 00:52:48,000
compose
1355
00:52:48,000 --> 00:52:50,559
which is a function and then in there we
1356
00:52:50,559 --> 00:52:53,760
put apply middleware and then in there
1357
00:52:53,760 --> 00:52:56,559
we pass thunk as you can see
1358
00:52:56,559 --> 00:52:58,480
and looks like i misspelled compose at
1359
00:52:58,480 --> 00:53:00,800
the top great so the only thing we have
1360
00:53:00,800 --> 00:53:04,079
to do right now is import reducers we
1361
00:53:04,079 --> 00:53:06,960
are going to import reducers
1362
00:53:06,960 --> 00:53:09,599
from dot slash reducers
1363
00:53:09,599 --> 00:53:11,760
that means that we also have to go ahead
1364
00:53:11,760 --> 00:53:15,520
in reducers index.js to export them
1365
00:53:15,520 --> 00:53:17,280
inside of here we're going to use
1366
00:53:17,280 --> 00:53:20,960
something known as combine reducers
1367
00:53:20,960 --> 00:53:23,280
from redux
1368
00:53:23,280 --> 00:53:27,520
and then there we can say export default
1369
00:53:27,520 --> 00:53:30,480
and call that combine reducers
1370
00:53:30,480 --> 00:53:32,960
we call it as a function and then put an
1371
00:53:32,960 --> 00:53:35,440
object inside of it now in here we can
1372
00:53:35,440 --> 00:53:37,280
use all of the individual reducers that
1373
00:53:37,280 --> 00:53:39,839
we have in our case we are only going to
1374
00:53:39,839 --> 00:53:41,520
have posts
1375
00:53:41,520 --> 00:53:44,400
so for that reason we have to import
1376
00:53:44,400 --> 00:53:47,760
posts from dot slash posts
1377
00:53:47,760 --> 00:53:50,240
so we can go to that file and now let's
1378
00:53:50,240 --> 00:53:53,680
talk about reducers what are reducers
1379
00:53:53,680 --> 00:53:56,079
well a reducer is a function so let's do
1380
00:53:56,079 --> 00:53:58,800
it like that const reducer is equal to a
1381
00:53:58,800 --> 00:53:59,839
function
1382
00:53:59,839 --> 00:54:02,000
more specifically it is equal to a
1383
00:54:02,000 --> 00:54:04,640
function that accepts the state
1384
00:54:04,640 --> 00:54:08,240
and also it accepts the action then
1385
00:54:08,240 --> 00:54:10,800
based on the action type so if
1386
00:54:10,800 --> 00:54:12,480
action.type
1387
00:54:12,480 --> 00:54:15,920
is equal to let's say create
1388
00:54:15,920 --> 00:54:19,040
then we want to do some logic here more
1389
00:54:19,040 --> 00:54:21,599
specifically we want to return either
1390
00:54:21,599 --> 00:54:24,240
action or we want to return the state
1391
00:54:24,240 --> 00:54:26,720
changed by the action usually
1392
00:54:26,720 --> 00:54:28,400
you're going to have multiple if
1393
00:54:28,400 --> 00:54:30,319
statements for a lot of different things
1394
00:54:30,319 --> 00:54:32,400
and for that reason people prefer to
1395
00:54:32,400 --> 00:54:34,400
have the switch statement and this is
1396
00:54:34,400 --> 00:54:36,480
how it works you have the switch
1397
00:54:36,480 --> 00:54:37,680
statement
1398
00:54:37,680 --> 00:54:40,079
and then right there as a key you put
1399
00:54:40,079 --> 00:54:43,440
the action that type as we discussed and
1400
00:54:43,440 --> 00:54:45,599
now in here we can specify all of our
1401
00:54:45,599 --> 00:54:47,839
types for example we know that we're
1402
00:54:47,839 --> 00:54:50,000
going to have a fetch all action for
1403
00:54:50,000 --> 00:54:51,680
fetching all the posts
1404
00:54:51,680 --> 00:54:54,079
and let's also do the one for creating
1405
00:54:54,079 --> 00:54:56,799
so that's going to be create post great
1406
00:54:56,799 --> 00:54:58,640
in here we are later going to return
1407
00:54:58,640 --> 00:55:00,480
some logic but since we don't have
1408
00:55:00,480 --> 00:55:04,240
anything yet let's just return the state
1409
00:55:04,240 --> 00:55:07,599
and in reducers the state always needs
1410
00:55:07,599 --> 00:55:09,359
to be equal to something we cannot have
1411
00:55:09,359 --> 00:55:11,599
it equal to nothing that's why we have
1412
00:55:11,599 --> 00:55:14,240
to set this initial value our posts are
1413
00:55:14,240 --> 00:55:16,480
going to be an array and that's why we
1414
00:55:16,480 --> 00:55:18,880
are specifying this empty array there
1415
00:55:18,880 --> 00:55:21,200
and also just to simplify things our
1416
00:55:21,200 --> 00:55:23,760
state is always going to be simply posts
1417
00:55:23,760 --> 00:55:25,599
because we are in a post reducer so we
1418
00:55:25,599 --> 00:55:28,400
can rename the state to posts and also
1419
00:55:28,400 --> 00:55:30,400
as a default return people usually just
1420
00:55:30,400 --> 00:55:32,559
put return
1421
00:55:32,559 --> 00:55:35,200
and then posts that's it we're gonna add
1422
00:55:35,200 --> 00:55:37,359
logic here later on but now we just need
1423
00:55:37,359 --> 00:55:39,280
to set it up so that it works together
1424
00:55:39,280 --> 00:55:41,760
once we connect the redux to the store
1425
00:55:41,760 --> 00:55:43,359
great now you can see that this reducer
1426
00:55:43,359 --> 00:55:45,280
is currently not being used so what we
1427
00:55:45,280 --> 00:55:47,040
can do is just
1428
00:55:47,040 --> 00:55:50,160
export default that function we created
1429
00:55:50,160 --> 00:55:52,640
we don't actually have to use it here we
1430
00:55:52,640 --> 00:55:55,520
are using it in here in combine reducers
1431
00:55:55,520 --> 00:55:57,680
we are importing that posts and then
1432
00:55:57,680 --> 00:56:00,319
setting posts equal to be posts
1433
00:56:00,319 --> 00:56:02,000
considering both the key and the value
1434
00:56:02,000 --> 00:56:04,559
are the same we can only keep the first
1435
00:56:04,559 --> 00:56:06,960
one that's it we can put this in one
1436
00:56:06,960 --> 00:56:08,000
line
1437
00:56:08,000 --> 00:56:10,480
and as you can see our index.js off
1438
00:56:10,480 --> 00:56:12,720
reducer is done now we are successfully
1439
00:56:12,720 --> 00:56:14,640
exporting the thing that we previously
1440
00:56:14,640 --> 00:56:17,280
imported in here in the index.js of the
1441
00:56:17,280 --> 00:56:18,559
src
1442
00:56:18,559 --> 00:56:20,559
and now we have this variable in the
1443
00:56:20,559 --> 00:56:22,559
store now that the store variable is
1444
00:56:22,559 --> 00:56:24,640
successfully created we can wrap our
1445
00:56:24,640 --> 00:56:26,640
application with a provider component
1446
00:56:26,640 --> 00:56:29,440
we're going to do it right here
1447
00:56:29,440 --> 00:56:30,960
provider
1448
00:56:30,960 --> 00:56:33,599
and then we wrap our application with it
1449
00:56:33,599 --> 00:56:35,359
we can do it like so we're going to put
1450
00:56:35,359 --> 00:56:37,040
the app here
1451
00:56:37,040 --> 00:56:39,599
and then just move the comma right there
1452
00:56:39,599 --> 00:56:41,520
people usually like to span it across
1453
00:56:41,520 --> 00:56:43,440
multiple lines just so it looks simpler
1454
00:56:43,440 --> 00:56:44,640
to understand
1455
00:56:44,640 --> 00:56:46,799
and this is it the last thing we have to
1456
00:56:46,799 --> 00:56:49,920
do is specify the store to be equal to
1457
00:56:49,920 --> 00:56:52,720
the store we just created right there
1458
00:56:52,720 --> 00:56:54,799
that's it our application is now
1459
00:56:54,799 --> 00:56:57,359
successfully using redux and we can
1460
00:56:57,359 --> 00:56:59,599
start using all of its capabilities
1461
00:56:59,599 --> 00:57:01,200
looks like we forgot to install one
1462
00:57:01,200 --> 00:57:03,599
thing which is react redux so just copy
1463
00:57:03,599 --> 00:57:06,559
that close the terminal by control c and
1464
00:57:06,559 --> 00:57:09,040
then in there you can type npm install
1465
00:57:09,040 --> 00:57:11,200
and then react redux
1466
00:57:11,200 --> 00:57:12,880
once that is done it means that our
1467
00:57:12,880 --> 00:57:14,960
application is successfully connected to
1468
00:57:14,960 --> 00:57:18,160
redux now we can start making use of it
1469
00:57:18,160 --> 00:57:20,880
first we have to dispatch our get posts
1470
00:57:20,880 --> 00:57:23,359
action and we are going to do that into
1471
00:57:23,359 --> 00:57:26,640
our app.js so let's go in there in
1472
00:57:26,640 --> 00:57:29,520
pre-hooks era redux was kind of hard you
1473
00:57:29,520 --> 00:57:31,680
had to go down here and then do some
1474
00:57:31,680 --> 00:57:34,160
crazy syntax like this and then map
1475
00:57:34,160 --> 00:57:37,520
state to props it was crazy now with use
1476
00:57:37,520 --> 00:57:39,920
of hooks we can use redux much more
1477
00:57:39,920 --> 00:57:43,200
easily what we can do is import a hook
1478
00:57:43,200 --> 00:57:46,079
from react redux so import
1479
00:57:46,079 --> 00:57:48,319
use dispatch
1480
00:57:48,319 --> 00:57:51,440
from react minus redux
1481
00:57:51,440 --> 00:57:54,079
what this allows us to do is to dispatch
1482
00:57:54,079 --> 00:57:57,119
an action okay so how does it work first
1483
00:57:57,119 --> 00:57:59,520
of all we have to define that dispatch
1484
00:57:59,520 --> 00:58:02,400
so we need to say const this patch is
1485
00:58:02,400 --> 00:58:06,240
equal to use dispatch that is a hook
1486
00:58:06,240 --> 00:58:08,480
now that we have access to this dispatch
1487
00:58:08,480 --> 00:58:10,559
we need to find a way where we are
1488
00:58:10,559 --> 00:58:12,720
actually going to dispatch the action
1489
00:58:12,720 --> 00:58:15,599
and the best way to do that is inside of
1490
00:58:15,599 --> 00:58:18,160
the use effect use effect is initially
1491
00:58:18,160 --> 00:58:19,680
going to be just the component that
1492
00:58:19,680 --> 00:58:21,760
mount but later on it's going to become
1493
00:58:21,760 --> 00:58:24,240
the component will update
1494
00:58:24,240 --> 00:58:26,559
so to do that we have to specify the use
1495
00:58:26,559 --> 00:58:29,440
effect it looks like this we call it and
1496
00:58:29,440 --> 00:58:31,280
then inside of there we put a callback
1497
00:58:31,280 --> 00:58:33,920
function and as the second parameter we
1498
00:58:33,920 --> 00:58:37,040
put an empty array there and finally we
1499
00:58:37,040 --> 00:58:39,280
can use this dispatch to dispatch an
1500
00:58:39,280 --> 00:58:41,359
action right now we don't have any
1501
00:58:41,359 --> 00:58:44,079
actions in here so our next goal is to
1502
00:58:44,079 --> 00:58:46,160
create an action we can do that right
1503
00:58:46,160 --> 00:58:49,119
here by importing it import
1504
00:58:49,119 --> 00:58:50,720
get posts
1505
00:58:50,720 --> 00:58:54,480
from dot slash actions slash posts you
1506
00:58:54,480 --> 00:58:56,400
can see it's not currently being used so
1507
00:58:56,400 --> 00:58:58,559
how do we use it well you just specify
1508
00:58:58,559 --> 00:59:01,200
the function get posts and then you call
1509
00:59:01,200 --> 00:59:03,680
it this is our successful dispatch
1510
00:59:03,680 --> 00:59:06,319
that's it and react is telling us please
1511
00:59:06,319 --> 00:59:08,079
include the dispatch in here in the
1512
00:59:08,079 --> 00:59:10,799
dependency array so let's do that
1513
00:59:10,799 --> 00:59:13,440
great now it's time for us to actually
1514
00:59:13,440 --> 00:59:16,000
export get posts from the actions let's
1515
00:59:16,000 --> 00:59:17,200
go there
1516
00:59:17,200 --> 00:59:18,480
inside of here
1517
00:59:18,480 --> 00:59:20,640
inside of the post we just have a
1518
00:59:20,640 --> 00:59:23,119
completely empty file now we have to
1519
00:59:23,119 --> 00:59:25,119
make use of this index.js file in the
1520
00:59:25,119 --> 00:59:28,000
api we created not that long ago to do
1521
00:59:28,000 --> 00:59:31,040
that we're going to do import star
1522
00:59:31,040 --> 00:59:32,480
as api
1523
00:59:32,480 --> 00:59:33,280
from
1524
00:59:33,280 --> 00:59:35,359
dot slash api
1525
00:59:35,359 --> 00:59:38,319
this import star as means that we import
1526
00:59:38,319 --> 00:59:41,920
everything from the actions as api that
1527
00:59:41,920 --> 00:59:44,400
means that we'll be able to use this
1528
00:59:44,400 --> 00:59:46,400
fetch posts like this
1529
00:59:46,400 --> 00:59:49,599
api dot fetch posts and we do it this
1530
00:59:49,599 --> 00:59:51,200
way because we're going to have a lot of
1531
00:59:51,200 --> 00:59:53,520
calls exported from the api
1532
00:59:53,520 --> 00:59:55,680
now we have to create actions more
1533
00:59:55,680 --> 00:59:58,799
specifically action creators
1534
00:59:58,799 --> 01:00:01,599
action creators are functions that
1535
01:00:01,599 --> 01:00:05,280
return actions how do they look const
1536
01:00:05,280 --> 01:00:06,640
get posts
1537
01:00:06,640 --> 01:00:09,119
is equal to an arrow function and there
1538
01:00:09,119 --> 01:00:11,440
we can declare an action const action is
1539
01:00:11,440 --> 01:00:14,160
an object actions must have the type
1540
01:00:14,160 --> 01:00:16,000
property in this case let's say that
1541
01:00:16,000 --> 01:00:18,160
we're going to name it fetch all and
1542
01:00:18,160 --> 01:00:20,240
also they can have some payload and
1543
01:00:20,240 --> 01:00:22,319
payload is usually the data where we
1544
01:00:22,319 --> 01:00:25,359
store all of our posts and finally we
1545
01:00:25,359 --> 01:00:27,760
have to return it return
1546
01:00:27,760 --> 01:00:30,079
action this is it we successfully
1547
01:00:30,079 --> 01:00:32,000
created this action creator the problem
1548
01:00:32,000 --> 01:00:34,400
is we are working with asynchronous data
1549
01:00:34,400 --> 01:00:36,559
to actually fetch all the posts some
1550
01:00:36,559 --> 01:00:39,280
time is gonna have to pass and for that
1551
01:00:39,280 --> 01:00:42,640
we have to use redux tank redux tank
1552
01:00:42,640 --> 01:00:45,760
allows us to in here specify an
1553
01:00:45,760 --> 01:00:48,240
additional arrow function bear with me
1554
01:00:48,240 --> 01:00:50,160
the syntax is crazy but you just have to
1555
01:00:50,160 --> 01:00:51,280
do this
1556
01:00:51,280 --> 01:00:53,520
async and then one more set of
1557
01:00:53,520 --> 01:00:55,520
parentheses and then in there as a
1558
01:00:55,520 --> 01:00:58,079
property we get access to this patch
1559
01:00:58,079 --> 01:01:01,200
this is it now we created a function
1560
01:01:01,200 --> 01:01:03,359
that returns another function and now we
1561
01:01:03,359 --> 01:01:06,400
can use the async await capabilities of
1562
01:01:06,400 --> 01:01:08,079
course we have to have another arrow
1563
01:01:08,079 --> 01:01:09,280
right there
1564
01:01:09,280 --> 01:01:11,599
great and there is one more difference
1565
01:01:11,599 --> 01:01:13,680
instead of actually returning the action
1566
01:01:13,680 --> 01:01:16,000
with redux tank what you have to do is
1567
01:01:16,000 --> 01:01:19,200
you have to dispatch the action that's
1568
01:01:19,200 --> 01:01:22,000
it so just to recap let's go back
1569
01:01:22,000 --> 01:01:24,319
action creators are functions that
1570
01:01:24,319 --> 01:01:26,880
return an action and an action is just
1571
01:01:26,880 --> 01:01:28,799
an object that has the type and a
1572
01:01:28,799 --> 01:01:31,359
payload with redux thunk since we'll be
1573
01:01:31,359 --> 01:01:33,359
dealing with asynchronous logic we have
1574
01:01:33,359 --> 01:01:35,680
to add this async dispatch function in
1575
01:01:35,680 --> 01:01:37,280
front of it and then instead of
1576
01:01:37,280 --> 01:01:39,359
returning the action we have to dispatch
1577
01:01:39,359 --> 01:01:42,079
it to actually make this work let's add
1578
01:01:42,079 --> 01:01:44,720
a few lines we're going to add that try
1579
01:01:44,720 --> 01:01:46,160
and catch block
1580
01:01:46,160 --> 01:01:48,640
and then in the try i'm going to try to
1581
01:01:48,640 --> 01:01:50,880
fetch all the data from the api we can
1582
01:01:50,880 --> 01:01:53,599
do it like this const we're immediately
1583
01:01:53,599 --> 01:01:56,000
going to destructure the data
1584
01:01:56,000 --> 01:01:59,599
and then is equal to await api that
1585
01:01:59,599 --> 01:02:01,440
fetch posts
1586
01:02:01,440 --> 01:02:03,520
so what we're doing in here is we are
1587
01:02:03,520 --> 01:02:06,079
first getting the response from the api
1588
01:02:06,079 --> 01:02:07,839
and then in the response we always have
1589
01:02:07,839 --> 01:02:09,760
the data object which we're returning
1590
01:02:09,760 --> 01:02:11,680
from the back end and then we get the
1591
01:02:11,680 --> 01:02:14,720
data and basically data represents the
1592
01:02:14,720 --> 01:02:17,599
posts so what we can do is in here we
1593
01:02:17,599 --> 01:02:19,920
can remove this action and we can
1594
01:02:19,920 --> 01:02:21,119
immediately
1595
01:02:21,119 --> 01:02:22,240
dispatch
1596
01:02:22,240 --> 01:02:23,359
an action
1597
01:02:23,359 --> 01:02:25,760
an action is as we said an object which
1598
01:02:25,760 --> 01:02:27,119
includes the type
1599
01:02:27,119 --> 01:02:29,440
the type is going to be fetch
1600
01:02:29,440 --> 01:02:30,240
all
1601
01:02:30,240 --> 01:02:32,079
and then the payload
1602
01:02:32,079 --> 01:02:33,599
is going to be
1603
01:02:33,599 --> 01:02:34,480
data
1604
01:02:34,480 --> 01:02:36,799
now we are successfully using redux to
1605
01:02:36,799 --> 01:02:39,920
actually pass or dispatch an action from
1606
01:02:39,920 --> 01:02:42,880
the data from our back end in here we
1607
01:02:42,880 --> 01:02:44,720
can simply call the log
1608
01:02:44,720 --> 01:02:47,440
the error dot message and if you
1609
01:02:47,440 --> 01:02:49,520
remember we actually imported this
1610
01:02:49,520 --> 01:02:51,760
action which means that we have to
1611
01:02:51,760 --> 01:02:55,599
export it so export const get posts
1612
01:02:55,599 --> 01:02:57,839
that's it as soon as this action gets
1613
01:02:57,839 --> 01:02:59,839
dispatched and we're doing that from
1614
01:02:59,839 --> 01:03:02,880
here we immediately go to the post
1615
01:03:02,880 --> 01:03:05,280
reducer and there we have to handle the
1616
01:03:05,280 --> 01:03:08,319
logic of fetching all posts
1617
01:03:08,319 --> 01:03:11,440
considering we are immediately fetching
1618
01:03:11,440 --> 01:03:14,000
all of our posts in here we are fetching
1619
01:03:14,000 --> 01:03:16,559
the data from the api and then sending
1620
01:03:16,559 --> 01:03:19,760
that data through the action.payload
1621
01:03:19,760 --> 01:03:22,319
we can immediately in the reducer just
1622
01:03:22,319 --> 01:03:24,240
do this return
1623
01:03:24,240 --> 01:03:27,039
action dot payload and that action that
1624
01:03:27,039 --> 01:03:30,559
payload are our actual posts now we've
1625
01:03:30,559 --> 01:03:32,319
been working a lot with all of this
1626
01:03:32,319 --> 01:03:34,559
redux data passing but how do we
1627
01:03:34,559 --> 01:03:36,880
actually retrieve the data from within
1628
01:03:36,880 --> 01:03:38,240
our components
1629
01:03:38,240 --> 01:03:40,480
well to do that we are now going to in
1630
01:03:40,480 --> 01:03:42,160
the component that actually needs the
1631
01:03:42,160 --> 01:03:44,400
posts and that's going to be
1632
01:03:44,400 --> 01:03:47,280
post component inside of there we have
1633
01:03:47,280 --> 01:03:49,599
to somehow fetch the data from that
1634
01:03:49,599 --> 01:03:52,559
global redux store we can do that with
1635
01:03:52,559 --> 01:03:55,520
the help of something known as selectors
1636
01:03:55,520 --> 01:03:58,640
so in there we're going to import use
1637
01:03:58,640 --> 01:04:00,079
selector
1638
01:04:00,079 --> 01:04:00,960
from
1639
01:04:00,960 --> 01:04:03,440
react redux
1640
01:04:03,440 --> 01:04:06,000
great now that we have that we again
1641
01:04:06,000 --> 01:04:08,079
initialize it as a hook we're going to
1642
01:04:08,079 --> 01:04:09,599
say const
1643
01:04:09,599 --> 01:04:13,839
posts is equal to use selector
1644
01:04:13,839 --> 01:04:15,760
inside of the use selector we're going
1645
01:04:15,760 --> 01:04:17,920
to have a callback function as a
1646
01:04:17,920 --> 01:04:20,000
parameter in that callback function we
1647
01:04:20,000 --> 01:04:22,480
get access to that whole global redux
1648
01:04:22,480 --> 01:04:24,720
store or state and then we can
1649
01:04:24,720 --> 01:04:26,400
immediately return
1650
01:04:26,400 --> 01:04:28,400
state that posts
1651
01:04:28,400 --> 01:04:30,400
how do we know that it is called posts
1652
01:04:30,400 --> 01:04:33,599
well if you go to the reducers index.js
1653
01:04:33,599 --> 01:04:34,799
you can see
1654
01:04:34,799 --> 01:04:38,079
in here we have posts that's it now that
1655
01:04:38,079 --> 01:04:39,680
we're getting these posts what do you
1656
01:04:39,680 --> 01:04:42,000
say that we just do a simple console log
1657
01:04:42,000 --> 01:04:43,920
and put them there
1658
01:04:43,920 --> 01:04:46,400
great now we save this and let's see if
1659
01:04:46,400 --> 01:04:48,319
any data is showing up
1660
01:04:48,319 --> 01:04:51,039
open the console refresh and you should
1661
01:04:51,039 --> 01:04:52,480
be able to see
1662
01:04:52,480 --> 01:04:55,520
we do get an empty array at first in
1663
01:04:55,520 --> 01:04:58,720
post 11 but then we get a network error
1664
01:04:58,720 --> 01:05:01,440
saying access from origin localhost 3000
1665
01:05:01,440 --> 01:05:04,079
has been blocked by course policy there
1666
01:05:04,079 --> 01:05:05,839
is one thing that we need to do in our
1667
01:05:05,839 --> 01:05:08,480
react applications to make requests to
1668
01:05:08,480 --> 01:05:11,119
our server work to do that we need to go
1669
01:05:11,119 --> 01:05:13,680
back into our package.json of the client
1670
01:05:13,680 --> 01:05:16,640
side and just below this private to true
1671
01:05:16,640 --> 01:05:18,960
we're going to set a proxy
1672
01:05:18,960 --> 01:05:21,359
proxy is going to be equal to and now we
1673
01:05:21,359 --> 01:05:24,559
need to specify our port http
1674
01:05:24,559 --> 01:05:26,880
localhost
1675
01:05:26,880 --> 01:05:29,359
and that's going to be 5000.
1676
01:05:29,359 --> 01:05:31,680
if we now save this whenever you change
1677
01:05:31,680 --> 01:05:33,920
a package.json file you need to rerun
1678
01:05:33,920 --> 01:05:36,400
your terminal so i'm gonna close it and
1679
01:05:36,400 --> 01:05:39,200
then run npm start one more time and
1680
01:05:39,200 --> 01:05:41,520
let's see if that's gonna fix our issue
1681
01:05:41,520 --> 01:05:43,680
after i spent some time debugging i
1682
01:05:43,680 --> 01:05:44,960
found that
1683
01:05:44,960 --> 01:05:46,880
the thing that we added the proxy that
1684
01:05:46,880 --> 01:05:49,119
was absolutely necessary but that wasn't
1685
01:05:49,119 --> 01:05:50,480
the fix to our issue
1686
01:05:50,480 --> 01:05:52,960
the fix was this little bug right there
1687
01:05:52,960 --> 01:05:55,440
we specified the routes before we
1688
01:05:55,440 --> 01:05:58,720
specified app.use course so we need to
1689
01:05:58,720 --> 01:06:01,920
have the course above the actual app.use
1690
01:06:01,920 --> 01:06:04,480
routes right there if you now save this
1691
01:06:04,480 --> 01:06:06,799
and then go back you should see if we
1692
01:06:06,799 --> 01:06:07,839
refresh
1693
01:06:07,839 --> 01:06:10,079
that we don't get an error anymore and
1694
01:06:10,079 --> 01:06:12,079
we just have an empty array
1695
01:06:12,079 --> 01:06:13,920
at first we have this empty array
1696
01:06:13,920 --> 01:06:15,599
because that's what we set up right
1697
01:06:15,599 --> 01:06:18,799
there in the reducer but later on once
1698
01:06:18,799 --> 01:06:20,400
we make a request
1699
01:06:20,400 --> 01:06:22,400
then we get the new empty array and
1700
01:06:22,400 --> 01:06:24,640
that's after the data has been fetched
1701
01:06:24,640 --> 01:06:26,640
the data has been successfully fetched
1702
01:06:26,640 --> 01:06:29,200
but still it's an empty array that means
1703
01:06:29,200 --> 01:06:31,280
that the get request works and now is
1704
01:06:31,280 --> 01:06:33,520
the time to implement the form so that
1705
01:06:33,520 --> 01:06:35,520
we can make a post request to our
1706
01:06:35,520 --> 01:06:38,640
database and actually add new posts
1707
01:06:38,640 --> 01:06:40,799
let's start by adding the jsx to our
1708
01:06:40,799 --> 01:06:41,760
form
1709
01:06:41,760 --> 01:06:43,920
first of all we have to import all the
1710
01:06:43,920 --> 01:06:45,839
components we're going to use that's
1711
01:06:45,839 --> 01:06:48,480
going to be text field
1712
01:06:48,480 --> 01:06:50,079
button
1713
01:06:50,079 --> 01:06:51,839
typography
1714
01:06:51,839 --> 01:06:53,280
and paper
1715
01:06:53,280 --> 01:06:55,760
and all of that is coming from
1716
01:06:55,760 --> 01:06:57,839
add material ui
1717
01:06:57,839 --> 01:06:59,599
forward slash core
1718
01:06:59,599 --> 01:07:01,520
now let's create our form
1719
01:07:01,520 --> 01:07:04,000
inside of here we are going to create a
1720
01:07:04,000 --> 01:07:06,559
paper paper is like a div that has
1721
01:07:06,559 --> 01:07:08,319
whitish background
1722
01:07:08,319 --> 01:07:11,039
in there we're going to add a class name
1723
01:07:11,039 --> 01:07:12,319
which is going to be equal to
1724
01:07:12,319 --> 01:07:13,839
classes.paper
1725
01:07:13,839 --> 01:07:15,839
remember we imported all the classes so
1726
01:07:15,839 --> 01:07:17,760
that we don't have to create them now
1727
01:07:17,760 --> 01:07:19,599
inside of there we're going to have a
1728
01:07:19,599 --> 01:07:20,720
form
1729
01:07:20,720 --> 01:07:23,520
form is going to have autocomplete set
1730
01:07:23,520 --> 01:07:24,880
to off
1731
01:07:24,880 --> 01:07:27,680
then we can add no validate
1732
01:07:27,680 --> 01:07:29,280
classname
1733
01:07:29,280 --> 01:07:32,559
is equal to classes.form
1734
01:07:32,559 --> 01:07:35,280
and then finally we have the on submit
1735
01:07:35,280 --> 01:07:38,400
which is going to say handle submit just
1736
01:07:38,400 --> 01:07:40,720
like that a long line but we have
1737
01:07:40,720 --> 01:07:42,799
everything that we need and that handle
1738
01:07:42,799 --> 01:07:44,640
submit is going to be the handler
1739
01:07:44,640 --> 01:07:46,799
function so we can create it right now
1740
01:07:46,799 --> 01:07:49,599
handle submit is going to be equal to an
1741
01:07:49,599 --> 01:07:51,920
error function and then later on we're
1742
01:07:51,920 --> 01:07:54,000
going to add what we need inside of
1743
01:07:54,000 --> 01:07:56,880
there great moving on to what we have
1744
01:07:56,880 --> 01:07:59,440
inside of the form inside of the form
1745
01:07:59,440 --> 01:08:00,799
first thing that we're going to have is
1746
01:08:00,799 --> 01:08:02,799
going to be a typography
1747
01:08:02,799 --> 01:08:04,640
more specifically we're going to use a
1748
01:08:04,640 --> 01:08:05,839
variant
1749
01:08:05,839 --> 01:08:07,920
of h6
1750
01:08:07,920 --> 01:08:09,599
and then inside of there we can say
1751
01:08:09,599 --> 01:08:10,799
creating
1752
01:08:10,799 --> 01:08:13,200
a memory because all our posts are
1753
01:08:13,200 --> 01:08:14,960
indeed memories
1754
01:08:14,960 --> 01:08:17,359
below that we are going to have a text
1755
01:08:17,359 --> 01:08:20,799
field text field is a self-closing tag
1756
01:08:20,799 --> 01:08:23,120
and then first text field is going to be
1757
01:08:23,120 --> 01:08:25,600
about who is creating a post so we're
1758
01:08:25,600 --> 01:08:29,359
going to say name is equal to creator
1759
01:08:29,359 --> 01:08:34,080
then variant is going to be outlined
1760
01:08:34,158 --> 01:08:36,000
label is going to be creator with a
1761
01:08:36,000 --> 01:08:38,719
capital c
1762
01:08:38,719 --> 01:08:40,560
then what else do we have we're gonna
1763
01:08:40,560 --> 01:08:43,920
make it a full width so full width just
1764
01:08:43,920 --> 01:08:46,479
like so and then finally we can add a
1765
01:08:46,479 --> 01:08:49,120
few more properties that's gonna be
1766
01:08:49,120 --> 01:08:51,600
value and the on change two of the most
1767
01:08:51,600 --> 01:08:53,679
important things and this is already
1768
01:08:53,679 --> 01:08:55,920
getting long so let's space this out in
1769
01:08:55,920 --> 01:08:57,439
multiple lines
1770
01:08:57,439 --> 01:09:00,319
variant label full width and then
1771
01:09:00,319 --> 01:09:02,080
finally the last two are going to be the
1772
01:09:02,080 --> 01:09:04,319
most important ones and that's going to
1773
01:09:04,319 --> 01:09:08,640
be value and finally the on change okay
1774
01:09:08,640 --> 01:09:09,520
great
1775
01:09:09,520 --> 01:09:11,279
so where are we going to store that
1776
01:09:11,279 --> 01:09:13,279
value and what is the on change going to
1777
01:09:13,279 --> 01:09:14,000
do
1778
01:09:14,000 --> 01:09:15,679
well the value is of course going to be
1779
01:09:15,679 --> 01:09:18,479
stored in the state more specifically in
1780
01:09:18,479 --> 01:09:21,600
the state called post data and then dot
1781
01:09:21,600 --> 01:09:23,679
creator that means that the whole data
1782
01:09:23,679 --> 01:09:25,839
from our post is going to be stored in
1783
01:09:25,839 --> 01:09:28,158
the pose data object in the state and
1784
01:09:28,158 --> 01:09:30,238
then each object key is going to be a
1785
01:09:30,238 --> 01:09:32,479
specific text field so let's create that
1786
01:09:32,479 --> 01:09:35,198
state using the use state property in
1787
01:09:35,198 --> 01:09:38,080
here we're gonna import the use state
1788
01:09:38,080 --> 01:09:38,880
hook
1789
01:09:38,880 --> 01:09:40,479
and then right there we're gonna say
1790
01:09:40,479 --> 01:09:41,520
const
1791
01:09:41,520 --> 01:09:45,439
post data and then set post data
1792
01:09:45,439 --> 01:09:48,479
is equal to use state and that's going
1793
01:09:48,479 --> 01:09:50,960
to be an empty object inside of that
1794
01:09:50,960 --> 01:09:52,960
empty object we need to specify the
1795
01:09:52,960 --> 01:09:54,800
properties that the object is going to
1796
01:09:54,800 --> 01:09:57,280
start with so in here we need to mention
1797
01:09:57,280 --> 01:09:59,199
everything that it's going to have first
1798
01:09:59,199 --> 01:10:01,199
of all the creator property which we
1799
01:10:01,199 --> 01:10:03,040
just specified is at the beginning going
1800
01:10:03,040 --> 01:10:05,679
to be an empty string title also an
1801
01:10:05,679 --> 01:10:07,280
empty string at the start
1802
01:10:07,280 --> 01:10:08,560
message
1803
01:10:08,560 --> 01:10:10,640
also going to be an empty string
1804
01:10:10,640 --> 01:10:13,120
tags is going to be also an empty string
1805
01:10:13,120 --> 01:10:15,760
and finally selected file
1806
01:10:15,760 --> 01:10:18,159
is also going to be an empty string
1807
01:10:18,159 --> 01:10:19,840
we're going to convert an image to a
1808
01:10:19,840 --> 01:10:22,960
base64 string just in a second okay we
1809
01:10:22,960 --> 01:10:24,719
can put this in one line so it's easier
1810
01:10:24,719 --> 01:10:25,840
to read
1811
01:10:25,840 --> 01:10:28,000
and that's it we can remove the last
1812
01:10:28,000 --> 01:10:28,960
comma
1813
01:10:28,960 --> 01:10:31,520
great now we have that post data and we
1814
01:10:31,520 --> 01:10:34,400
are using that right here in the value
1815
01:10:34,400 --> 01:10:36,800
so how are we going to change the value
1816
01:10:36,800 --> 01:10:39,600
of that state field using this on change
1817
01:10:39,600 --> 01:10:41,920
well that's a bit harder considering in
1818
01:10:41,920 --> 01:10:44,320
here we have an object that's in the
1819
01:10:44,320 --> 01:10:46,560
state and we want to update just one of
1820
01:10:46,560 --> 01:10:48,400
the object's properties
1821
01:10:48,400 --> 01:10:50,960
to do that we can use this syntax
1822
01:10:50,960 --> 01:10:53,199
in here as always we have a callback
1823
01:10:53,199 --> 01:10:55,360
function that has that event as a
1824
01:10:55,360 --> 01:10:56,800
parameter
1825
01:10:56,800 --> 01:11:00,159
then we have the set post data which is
1826
01:11:00,159 --> 01:11:02,400
a setter method for that state and then
1827
01:11:02,400 --> 01:11:04,239
in here it would be easy if we could
1828
01:11:04,239 --> 01:11:06,640
just specify an object and then in this
1829
01:11:06,640 --> 01:11:09,679
case say creator and then creator is
1830
01:11:09,679 --> 01:11:13,840
equal to e dot target that value right
1831
01:11:13,840 --> 01:11:15,520
that would be okay that won't be all
1832
01:11:15,520 --> 01:11:18,239
that complicated but it's not that easy
1833
01:11:18,239 --> 01:11:20,960
because later on if we add a second text
1834
01:11:20,960 --> 01:11:23,040
field that means that
1835
01:11:23,040 --> 01:11:25,840
we would always override everything and
1836
01:11:25,840 --> 01:11:27,840
simply have the creator we wouldn't have
1837
01:11:27,840 --> 01:11:30,000
any other fields because you can see we
1838
01:11:30,000 --> 01:11:32,560
are not specifying them in this object
1839
01:11:32,560 --> 01:11:33,920
right here
1840
01:11:33,920 --> 01:11:36,560
to fix that what we actually have to do
1841
01:11:36,560 --> 01:11:39,520
is first spread the pose data so that's
1842
01:11:39,520 --> 01:11:42,880
dot dot post theta
1843
01:11:42,880 --> 01:11:45,600
and then we have the creator right there
1844
01:11:45,600 --> 01:11:47,920
that means that in every text field if
1845
01:11:47,920 --> 01:11:50,080
we do the same thing but only change the
1846
01:11:50,080 --> 01:11:52,560
last property that means that all the
1847
01:11:52,560 --> 01:11:54,960
data is going to persist while changing
1848
01:11:54,960 --> 01:11:57,120
only the specific property of that
1849
01:11:57,120 --> 01:11:59,760
specific text field that's it a bit
1850
01:11:59,760 --> 01:12:02,320
harder but a great lesson to learn
1851
01:12:02,320 --> 01:12:04,560
whenever i come across a piece of syntax
1852
01:12:04,560 --> 01:12:06,800
that is a bit lengthier such as this one
1853
01:12:06,800 --> 01:12:09,280
i copy it and then go to the website
1854
01:12:09,280 --> 01:12:11,760
called mem.dev
1855
01:12:11,760 --> 01:12:13,840
in here i can log in and it's going to
1856
01:12:13,840 --> 01:12:17,360
allow me to train on that same syntax
1857
01:12:17,360 --> 01:12:19,679
in here i can just click create a card
1858
01:12:19,679 --> 01:12:22,000
and paste that snippet right there in
1859
01:12:22,000 --> 01:12:23,760
this case i just want to learn this
1860
01:12:23,760 --> 01:12:25,360
thing that's the hardest thing that's
1861
01:12:25,360 --> 01:12:26,880
the thing that we have to spread and
1862
01:12:26,880 --> 01:12:28,960
then use the creator we can set the text
1863
01:12:28,960 --> 01:12:31,520
highlighting to javascript and i can say
1864
01:12:31,520 --> 01:12:35,199
i just learned how to set the state
1865
01:12:35,199 --> 01:12:37,280
using an object
1866
01:12:37,280 --> 01:12:39,199
now it allows me to train it's going to
1867
01:12:39,199 --> 01:12:41,440
keep asking me to do the same thing
1868
01:12:41,440 --> 01:12:44,000
until i'm 100 sure that i can replicate
1869
01:12:44,000 --> 01:12:45,920
this always and it's completely
1870
01:12:45,920 --> 01:12:47,840
engrained in my memory i know that i
1871
01:12:47,840 --> 01:12:50,480
need to spread it post data and then
1872
01:12:50,480 --> 01:12:53,760
finally i have to do creator is equal to
1873
01:12:53,760 --> 01:12:56,400
e dot target data value
1874
01:12:56,400 --> 01:12:58,880
and that's it i click submit you can see
1875
01:12:58,880 --> 01:13:01,120
it's correct and now since i know it i
1876
01:13:01,120 --> 01:13:03,120
can click easy but if you don't know it
1877
01:13:03,120 --> 01:13:04,800
you can practice on this and it's going
1878
01:13:04,800 --> 01:13:06,719
to keep repeating so you can practice it
1879
01:13:06,719 --> 01:13:08,880
every few days until you actually
1880
01:13:08,880 --> 01:13:11,120
remember it great that's it just wanted
1881
01:13:11,120 --> 01:13:12,640
to let you know that
1882
01:13:12,640 --> 01:13:14,480
now we can go ahead and add multiple
1883
01:13:14,480 --> 01:13:16,480
text fields this one is only for the
1884
01:13:16,480 --> 01:13:19,120
creator i'm gonna put all of this in one
1885
01:13:19,120 --> 01:13:20,880
line since we already know what is in
1886
01:13:20,880 --> 01:13:23,040
there and now i'm gonna just copy and
1887
01:13:23,040 --> 01:13:25,040
paste it a few more times
1888
01:13:25,040 --> 01:13:27,199
we're gonna need it once for the creator
1889
01:13:27,199 --> 01:13:29,120
once for the title
1890
01:13:29,120 --> 01:13:30,400
then the message
1891
01:13:30,400 --> 01:13:33,600
and then finally the tags
1892
01:13:33,600 --> 01:13:35,280
great of course we have to change the
1893
01:13:35,280 --> 01:13:37,520
labels as well so this is creator this
1894
01:13:37,520 --> 01:13:39,199
is going to be the title
1895
01:13:39,199 --> 01:13:41,280
in here we have the message
1896
01:13:41,280 --> 01:13:43,280
and finally the tags
1897
01:13:43,280 --> 01:13:45,360
and now we have to change the values as
1898
01:13:45,360 --> 01:13:47,280
well this thing here is going to be a
1899
01:13:47,280 --> 01:13:49,360
title therefore title is also going to
1900
01:13:49,360 --> 01:13:52,080
be here in here and in here we have the
1901
01:13:52,080 --> 01:13:53,199
message
1902
01:13:53,199 --> 01:13:55,760
and finally in here and in here we have
1903
01:13:55,760 --> 01:13:58,800
the tags that's it we now have four text
1904
01:13:58,800 --> 01:14:01,120
fields now let's do the most important
1905
01:14:01,120 --> 01:14:03,280
thing we're going to create a div
1906
01:14:03,280 --> 01:14:05,679
that div is going to have a class name
1907
01:14:05,679 --> 01:14:09,040
of classes that file input
1908
01:14:09,040 --> 01:14:10,560
and then inside of there we have to use
1909
01:14:10,560 --> 01:14:12,320
that package we install at the start
1910
01:14:12,320 --> 01:14:14,800
it's called file base so at the top of
1911
01:14:14,800 --> 01:14:17,360
our file we're gonna import
1912
01:14:17,360 --> 01:14:19,280
file base
1913
01:14:19,280 --> 01:14:23,600
from react minus file minus base 64. and
1914
01:14:23,600 --> 01:14:25,840
now we can use it as a component
1915
01:14:25,840 --> 01:14:28,480
file base and we have to specify a lot
1916
01:14:28,480 --> 01:14:30,800
of properties in there so first of all
1917
01:14:30,800 --> 01:14:32,560
the type is going to be equal to the
1918
01:14:32,560 --> 01:14:33,600
file
1919
01:14:33,600 --> 01:14:35,760
multiple is going to be set to false we
1920
01:14:35,760 --> 01:14:39,040
only need one and then on done function
1921
01:14:39,040 --> 01:14:41,280
is going to be set to
1922
01:14:41,280 --> 01:14:42,880
a callback function
1923
01:14:42,880 --> 01:14:46,080
where in here we do one destructuring we
1924
01:14:46,080 --> 01:14:48,400
get that base64 thing
1925
01:14:48,400 --> 01:14:50,560
and then in here we need to return or
1926
01:14:50,560 --> 01:14:51,640
rather
1927
01:14:51,640 --> 01:14:55,280
setpostdata and then we have an object
1928
01:14:55,280 --> 01:14:57,920
in there we spread the post data and
1929
01:14:57,920 --> 01:15:01,280
finally set the selected file
1930
01:15:01,280 --> 01:15:04,000
to be equal to this base 64 we are
1931
01:15:04,000 --> 01:15:06,560
receiving from here that's it we can
1932
01:15:06,560 --> 01:15:10,640
also put this in one line right now
1933
01:15:10,640 --> 01:15:12,960
that's it we now have all the inputs we
1934
01:15:12,960 --> 01:15:15,360
are missing only one thing and these are
1935
01:15:15,360 --> 01:15:18,400
going to be the buttons so first button
1936
01:15:18,400 --> 01:15:19,440
right there
1937
01:15:19,440 --> 01:15:23,520
the button is going to have a class name
1938
01:15:23,520 --> 01:15:27,679
of classes dot button submit
1939
01:15:27,679 --> 01:15:30,400
it's also going to have a variant of
1940
01:15:30,400 --> 01:15:32,640
contained
1941
01:15:32,640 --> 01:15:36,000
color of primary
1942
01:15:36,000 --> 01:15:37,440
size of
1943
01:15:37,440 --> 01:15:39,760
large
1944
01:15:39,760 --> 01:15:42,719
type of submit and then finally it's
1945
01:15:42,719 --> 01:15:45,199
going to have a full width property and
1946
01:15:45,199 --> 01:15:47,760
the button itself is going to say submit
1947
01:15:47,760 --> 01:15:50,480
you can see we have so many props in
1948
01:15:50,480 --> 01:15:53,040
here so many but usually if you don't
1949
01:15:53,040 --> 01:15:54,320
have this you would have to have a
1950
01:15:54,320 --> 01:15:56,080
hundred lines of css for that nice
1951
01:15:56,080 --> 01:15:58,560
looking button with material ui we just
1952
01:15:58,560 --> 01:16:01,199
passed props and that's it we need one
1953
01:16:01,199 --> 01:16:02,960
more button to clear the form for that
1954
01:16:02,960 --> 01:16:04,800
we're gonna copy this one
1955
01:16:04,800 --> 01:16:07,040
simply remove the class name we don't
1956
01:16:07,040 --> 01:16:08,239
need it here
1957
01:16:08,239 --> 01:16:10,400
the variant should be contained in both
1958
01:16:10,400 --> 01:16:13,360
cases not container contained color is
1959
01:16:13,360 --> 01:16:15,280
going to be secondary here
1960
01:16:15,280 --> 01:16:17,360
size is going to be small
1961
01:16:17,360 --> 01:16:19,120
and then in here we're going to have an
1962
01:16:19,120 --> 01:16:21,280
on click function rather than type
1963
01:16:21,280 --> 01:16:23,199
submit so on click
1964
01:16:23,199 --> 01:16:25,679
is going to be equal to clear that's
1965
01:16:25,679 --> 01:16:27,760
going to be a callback function to clear
1966
01:16:27,760 --> 01:16:30,719
it and in here we can also say clear
1967
01:16:30,719 --> 01:16:32,800
great now as we did with the handle
1968
01:16:32,800 --> 01:16:34,640
submit let's create that clear so that
1969
01:16:34,640 --> 01:16:37,760
we don't have any errors
1970
01:16:39,280 --> 01:16:42,159
there we go and save it now if we go
1971
01:16:42,159 --> 01:16:45,120
back to the browser we should see a nice
1972
01:16:45,120 --> 01:16:46,640
looking form
1973
01:16:46,640 --> 01:16:48,880
and we have a nice looking form but it's
1974
01:16:48,880 --> 01:16:50,640
not perfect these inputs should be
1975
01:16:50,640 --> 01:16:52,239
stretching and have some margins so
1976
01:16:52,239 --> 01:16:54,159
let's see what went wrong there in here
1977
01:16:54,159 --> 01:16:56,400
what we needed to do is have multiple
1978
01:16:56,400 --> 01:16:58,719
classes here so we need to make it a
1979
01:16:58,719 --> 01:17:01,040
template string and the first class is
1980
01:17:01,040 --> 01:17:03,280
going to be classes that root to
1981
01:17:03,280 --> 01:17:05,520
actually provide that padding and margin
1982
01:17:05,520 --> 01:17:06,960
and the second one is going to be
1983
01:17:06,960 --> 01:17:08,880
classes.form
1984
01:17:08,880 --> 01:17:10,960
that should fix the issue let's see if
1985
01:17:10,960 --> 01:17:11,920
it works
1986
01:17:11,920 --> 01:17:14,480
save it and then go back as you can see
1987
01:17:14,480 --> 01:17:16,719
now we have those margins and the last
1988
01:17:16,719 --> 01:17:19,520
thing would be yeah in here we have full
1989
01:17:19,520 --> 01:17:20,400
width
1990
01:17:20,400 --> 01:17:22,159
value but we need to separate these
1991
01:17:22,159 --> 01:17:24,080
words of course so if you do this like
1992
01:17:24,080 --> 01:17:25,840
this and go back
1993
01:17:25,840 --> 01:17:28,400
that's it we finally have a nice looking
1994
01:17:28,400 --> 01:17:31,199
form and now that we make a post request
1995
01:17:31,199 --> 01:17:33,679
we are going to be able to add a new
1996
01:17:33,679 --> 01:17:36,320
post into our database connected to our
1997
01:17:36,320 --> 01:17:40,000
server now let's add the api request and
1998
01:17:40,000 --> 01:17:42,719
the action for the post request
1999
01:17:42,719 --> 01:17:45,199
first let's go to the api and then in
2000
01:17:45,199 --> 01:17:48,800
here we're gonna do export const create
2001
01:17:48,800 --> 01:17:50,000
post
2002
01:17:50,000 --> 01:17:51,840
in here we're gonna have a callback
2003
01:17:51,840 --> 01:17:52,960
function
2004
01:17:52,960 --> 01:17:55,199
this one is going to take in one
2005
01:17:55,199 --> 01:17:57,440
parameter and that parameter is going to
2006
01:17:57,440 --> 01:17:59,520
be the entire post
2007
01:17:59,520 --> 01:18:01,440
after we get the post we can say
2008
01:18:01,440 --> 01:18:03,920
axios.post
2009
01:18:03,920 --> 01:18:05,840
first we have to specify the url and
2010
01:18:05,840 --> 01:18:07,600
then we have to specify the data we are
2011
01:18:07,600 --> 01:18:10,000
sending of course in this case the data
2012
01:18:10,000 --> 01:18:11,679
we are sending is going to be the whole
2013
01:18:11,679 --> 01:18:13,280
new post
2014
01:18:13,280 --> 01:18:15,280
now that we are exporting this we can go
2015
01:18:15,280 --> 01:18:16,960
back to actions
2016
01:18:16,960 --> 01:18:20,159
inside of the actions let's create it
2017
01:18:20,159 --> 01:18:22,400
export const
2018
01:18:22,400 --> 01:18:23,520
create
2019
01:18:23,520 --> 01:18:25,760
post is going to be equal to the same
2020
01:18:25,760 --> 01:18:26,400
thing
2021
01:18:26,400 --> 01:18:28,719
an error function which then returns
2022
01:18:28,719 --> 01:18:31,120
another error function with a dispatch
2023
01:18:31,120 --> 01:18:33,920
right there that comes from redux tank
2024
01:18:33,920 --> 01:18:36,000
and finally we're going to do a try
2025
01:18:36,000 --> 01:18:38,080
catch block inside of there
2026
01:18:38,080 --> 01:18:39,440
try catch
2027
01:18:39,440 --> 01:18:41,600
inside of the try we need to get that
2028
01:18:41,600 --> 01:18:44,159
data so that's going to be const
2029
01:18:44,159 --> 01:18:45,520
we destruction the data from the
2030
01:18:45,520 --> 01:18:48,960
response and that is equal to await
2031
01:18:48,960 --> 01:18:52,719
api dot create post this is basically
2032
01:18:52,719 --> 01:18:55,920
making an api request a post api request
2033
01:18:55,920 --> 01:18:58,640
to our backend server and we are sending
2034
01:18:58,640 --> 01:19:00,480
a post right there
2035
01:19:00,480 --> 01:19:03,280
finally we're going to dispatch
2036
01:19:03,280 --> 01:19:04,480
an action
2037
01:19:04,480 --> 01:19:06,400
the type of the action is going to be
2038
01:19:06,400 --> 01:19:07,600
create
2039
01:19:07,600 --> 01:19:09,920
and then the payload is going to again
2040
01:19:09,920 --> 01:19:12,560
be the data just like so
2041
01:19:12,560 --> 01:19:15,280
great in here we have the post and the
2042
01:19:15,280 --> 01:19:17,360
post we're receiving it if you remember
2043
01:19:17,360 --> 01:19:19,360
through here so in here we're going to
2044
01:19:19,360 --> 01:19:21,520
say post great
2045
01:19:21,520 --> 01:19:24,560
and finally if this is not successful we
2046
01:19:24,560 --> 01:19:27,199
are simply gonna console log the error
2047
01:19:27,199 --> 01:19:28,640
just like so
2048
01:19:28,640 --> 01:19:30,640
now if you remember correctly we have to
2049
01:19:30,640 --> 01:19:33,520
dispatch that action to do that we can
2050
01:19:33,520 --> 01:19:36,320
go into the form and then in here we can
2051
01:19:36,320 --> 01:19:39,440
import that use dispatch from react
2052
01:19:39,440 --> 01:19:40,480
redux
2053
01:19:40,480 --> 01:19:41,679
import
2054
01:19:41,679 --> 01:19:45,120
use dispatch
2055
01:19:45,280 --> 01:19:46,159
from
2056
01:19:46,159 --> 01:19:48,960
react minus redux
2057
01:19:48,960 --> 01:19:52,080
just like this great we have to use it
2058
01:19:52,080 --> 01:19:55,120
right there cons dispatch is equal to
2059
01:19:55,120 --> 01:19:57,440
use dispatch
2060
01:19:57,440 --> 01:19:59,840
and now this allows us to actually
2061
01:19:59,840 --> 01:20:03,120
dispatch actions the question is where
2062
01:20:03,120 --> 01:20:06,000
do we want to dispatch it well of course
2063
01:20:06,000 --> 01:20:08,320
on the handle submit once the user
2064
01:20:08,320 --> 01:20:10,880
submits we want to send over a post
2065
01:20:10,880 --> 01:20:13,040
request with all the data that user
2066
01:20:13,040 --> 01:20:14,159
typed in
2067
01:20:14,159 --> 01:20:16,159
first of all we always have to say
2068
01:20:16,159 --> 01:20:18,159
event.preventdefault
2069
01:20:18,159 --> 01:20:20,480
not to get the refresh in the browser
2070
01:20:20,480 --> 01:20:22,080
and then in there we're going to
2071
01:20:22,080 --> 01:20:25,440
dispatch an action so dispatch
2072
01:20:25,440 --> 01:20:28,320
this time it's going to be create post
2073
01:20:28,320 --> 01:20:30,400
and now inside of there we're going to
2074
01:20:30,400 --> 01:20:33,280
pass all the data from our state
2075
01:20:33,280 --> 01:20:34,719
post data
2076
01:20:34,719 --> 01:20:37,040
you can see create post is still red
2077
01:20:37,040 --> 01:20:38,960
which means we have to import it from
2078
01:20:38,960 --> 01:20:42,239
actions so in here we can say import
2079
01:20:42,239 --> 01:20:45,040
create post
2080
01:20:45,040 --> 01:20:48,320
from dot dot slash dot slash
2081
01:20:48,320 --> 01:20:50,960
actions slash posts
2082
01:20:50,960 --> 01:20:52,719
just like that
2083
01:20:52,719 --> 01:20:54,880
i can see event is not there so we have
2084
01:20:54,880 --> 01:20:57,440
to specify it here and now we are making
2085
01:20:57,440 --> 01:20:59,520
that request once we click the submit
2086
01:20:59,520 --> 01:21:02,080
button great once the action is
2087
01:21:02,080 --> 01:21:05,520
dispatched then we go to reducers right
2088
01:21:05,520 --> 01:21:07,840
there in the post reducer
2089
01:21:07,840 --> 01:21:10,000
and then what do we want to do once we
2090
01:21:10,000 --> 01:21:13,120
go to the create well we have to send
2091
01:21:13,120 --> 01:21:15,520
over an array of course we have an array
2092
01:21:15,520 --> 01:21:18,320
of posts first of all we have to spread
2093
01:21:18,320 --> 01:21:21,040
all the posts and then we have to add a
2094
01:21:21,040 --> 01:21:24,320
new post and that new post is stored in
2095
01:21:24,320 --> 01:21:26,480
the action.payload
2096
01:21:26,480 --> 01:21:27,679
that's it
2097
01:21:27,679 --> 01:21:29,440
now if we save that
2098
01:21:29,440 --> 01:21:31,120
if we are lucky enough if we don't have
2099
01:21:31,120 --> 01:21:33,920
any bugs this should actually work let's
2100
01:21:33,920 --> 01:21:36,320
go back to our form let's try creating
2101
01:21:36,320 --> 01:21:37,840
our first memory
2102
01:21:37,840 --> 01:21:40,320
inside of here i'm gonna put creator as
2103
01:21:40,320 --> 01:21:44,000
js mastery the title is going to be new
2104
01:21:44,000 --> 01:21:45,760
year's
2105
01:21:45,760 --> 01:21:47,400
eve
2106
01:21:47,400 --> 01:21:50,639
2019 the message is gonna be
2107
01:21:50,639 --> 01:21:53,360
had a lot of fun i guess
2108
01:21:53,360 --> 01:21:55,440
and then let's add some tags let's say
2109
01:21:55,440 --> 01:21:58,239
new year and also
2110
01:21:58,239 --> 01:22:00,000
happy new year people do that with
2111
01:22:00,000 --> 01:22:01,280
hashtags right
2112
01:22:01,280 --> 01:22:04,000
great now we have to choose a file okay
2113
01:22:04,000 --> 01:22:06,560
i prepared a nice looking fireworks
2114
01:22:06,560 --> 01:22:08,560
there let's save it
2115
01:22:08,560 --> 01:22:11,199
and now let's click submit and hope for
2116
01:22:11,199 --> 01:22:13,679
the best as you can see something
2117
01:22:13,679 --> 01:22:14,719
happened
2118
01:22:14,719 --> 01:22:17,840
and we got an array back array that has
2119
01:22:17,840 --> 01:22:20,960
one file in it that seems great of
2120
01:22:20,960 --> 01:22:22,560
course we're not seeing anything yet
2121
01:22:22,560 --> 01:22:24,719
because our posts and post is not
2122
01:22:24,719 --> 01:22:27,199
displaying anything but if we go back to
2123
01:22:27,199 --> 01:22:29,520
our cluster and then in here you can go
2124
01:22:29,520 --> 01:22:32,800
back to atlas and then click collections
2125
01:22:32,800 --> 01:22:34,480
if you go into collections you can see
2126
01:22:34,480 --> 01:22:36,159
the data that is actually in the
2127
01:22:36,159 --> 01:22:39,360
database in here we have post messages
2128
01:22:39,360 --> 01:22:41,120
and would you look at that
2129
01:22:41,120 --> 01:22:44,000
that definitely looks like the object or
2130
01:22:44,000 --> 01:22:46,800
the memory we just created which means
2131
01:22:46,800 --> 01:22:49,280
we right now at this point successfully
2132
01:22:49,280 --> 01:22:52,159
created a full stack marin application
2133
01:22:52,159 --> 01:22:54,320
first our react application more
2134
01:22:54,320 --> 01:22:56,960
specifically the form right there sent a
2135
01:22:56,960 --> 01:23:00,080
post request to our backend application
2136
01:23:00,080 --> 01:23:01,600
then the backend application
2137
01:23:01,600 --> 01:23:04,000
communicated that to our database and
2138
01:23:04,000 --> 01:23:06,480
that's it we have this file saved that
2139
01:23:06,480 --> 01:23:08,639
means that now we can set up the posts
2140
01:23:08,639 --> 01:23:11,360
and the post create some jsx there and
2141
01:23:11,360 --> 01:23:13,199
that post is going to be there every
2142
01:23:13,199 --> 01:23:15,280
time we refresh the page and we can keep
2143
01:23:15,280 --> 01:23:17,920
adding new ones we are now inside of the
2144
01:23:17,920 --> 01:23:20,320
posts component we are now successfully
2145
01:23:20,320 --> 01:23:22,159
fetching the posts and now we have to
2146
01:23:22,159 --> 01:23:24,639
actually do something with them so let's
2147
01:23:24,639 --> 01:23:27,440
first form a nice looking grid to do
2148
01:23:27,440 --> 01:23:29,600
that we are going to import
2149
01:23:29,600 --> 01:23:33,840
grid and also circular progress
2150
01:23:33,840 --> 01:23:34,880
from
2151
01:23:34,880 --> 01:23:38,159
add material ui core in here we're going
2152
01:23:38,159 --> 01:23:41,920
to say if not posts that length
2153
01:23:41,920 --> 01:23:42,719
then
2154
01:23:42,719 --> 01:23:43,600
show
2155
01:23:43,600 --> 01:23:45,440
circular progress
2156
01:23:45,440 --> 01:23:47,199
and that circular progress is just a
2157
01:23:47,199 --> 01:23:48,560
loading spinner
2158
01:23:48,560 --> 01:23:50,560
and then we're going to put else and
2159
01:23:50,560 --> 01:23:52,000
then inside of here we're going to
2160
01:23:52,000 --> 01:23:54,480
create a grid of our posts so that's
2161
01:23:54,480 --> 01:23:55,760
going to be grid
2162
01:23:55,760 --> 01:23:57,679
and let's just do it like this for now
2163
01:23:57,679 --> 01:23:59,920
let me explain this thing so if there is
2164
01:23:59,920 --> 01:24:02,000
no post that length then return this
2165
01:24:02,000 --> 01:24:03,920
otherwise return this
2166
01:24:03,920 --> 01:24:05,920
this means that if post stat length is
2167
01:24:05,920 --> 01:24:07,120
zero
2168
01:24:07,120 --> 01:24:08,719
then zero is full c this is going to
2169
01:24:08,719 --> 01:24:10,560
make it to trudy which means that we're
2170
01:24:10,560 --> 01:24:12,960
going to show a circular progress if
2171
01:24:12,960 --> 01:24:14,800
both that length is something other than
2172
01:24:14,800 --> 01:24:18,159
zero for example 5 10 20 then this whole
2173
01:24:18,159 --> 01:24:20,400
thing is going to be equal to false then
2174
01:24:20,400 --> 01:24:22,560
we're going to show the posts themselves
2175
01:24:22,560 --> 01:24:24,800
great first of all our grid is going to
2176
01:24:24,800 --> 01:24:28,080
have a class name of classes dot
2177
01:24:28,080 --> 01:24:29,360
container
2178
01:24:29,360 --> 01:24:30,880
it's also going to be of a type
2179
01:24:30,880 --> 01:24:32,239
container so we're going to just put
2180
01:24:32,239 --> 01:24:33,280
that there
2181
01:24:33,280 --> 01:24:36,000
we're going to say align items equal to
2182
01:24:36,000 --> 01:24:37,760
stretch
2183
01:24:37,760 --> 01:24:40,560
and also set the spacing to be equal to
2184
01:24:40,560 --> 01:24:42,320
3.
2185
01:24:42,320 --> 01:24:43,199
great
2186
01:24:43,199 --> 01:24:45,040
inside of there we're going to use curly
2187
01:24:45,040 --> 01:24:46,639
braces to indicate we'll be using
2188
01:24:46,639 --> 01:24:48,960
javascript logic and then we're going to
2189
01:24:48,960 --> 01:24:50,800
loop over the posts
2190
01:24:50,800 --> 01:24:52,800
posts that map
2191
01:24:52,800 --> 01:24:55,679
in a map we get a post and then for each
2192
01:24:55,679 --> 01:24:57,360
post we're going to immediately return
2193
01:24:57,360 --> 01:24:59,360
something notice how it didn't put curly
2194
01:24:59,360 --> 01:25:01,760
braces there but rather just normal
2195
01:25:01,760 --> 01:25:03,120
parentheses
2196
01:25:03,120 --> 01:25:05,360
inside of here we're going to return yet
2197
01:25:05,360 --> 01:25:10,320
again a grid but this time off type item
2198
01:25:10,320 --> 01:25:12,560
we need to close it
2199
01:25:12,560 --> 01:25:13,840
like so
2200
01:25:13,840 --> 01:25:16,239
and now that grid is also going to have
2201
01:25:16,239 --> 01:25:19,520
a few more things first of all a key key
2202
01:25:19,520 --> 01:25:21,920
is going to be equal to post dot
2203
01:25:21,920 --> 01:25:23,920
underscore id
2204
01:25:23,920 --> 01:25:26,400
it's going to stay as item and then we
2205
01:25:26,400 --> 01:25:30,000
can say xs which means how large is it
2206
01:25:30,000 --> 01:25:31,920
going to be on mobile devices on extra
2207
01:25:31,920 --> 01:25:33,760
small devices it's going to take the
2208
01:25:33,760 --> 01:25:37,199
full width 12 spaces on small and larger
2209
01:25:37,199 --> 01:25:40,159
it's also going to take six spaces great
2210
01:25:40,159 --> 01:25:42,880
and finally inside of there we have our
2211
01:25:42,880 --> 01:25:44,000
post
2212
01:25:44,000 --> 01:25:46,800
now as props in the post we'll be
2213
01:25:46,800 --> 01:25:49,679
sending this post that's it as we are
2214
01:25:49,679 --> 01:25:51,679
mapping with our real posts now not
2215
01:25:51,679 --> 01:25:54,400
generic ones we can send that individual
2216
01:25:54,400 --> 01:25:57,760
value of a post to each post component
2217
01:25:57,760 --> 01:26:00,000
great now that that is done we can go to
2218
01:26:00,000 --> 01:26:02,719
a singular post component and implement
2219
01:26:02,719 --> 01:26:04,239
the logic there
2220
01:26:04,239 --> 01:26:06,400
inside of here we are going to import a
2221
01:26:06,400 --> 01:26:08,639
lot of things from material ui that's
2222
01:26:08,639 --> 01:26:10,239
going to be card
2223
01:26:10,239 --> 01:26:11,360
card
2224
01:26:11,360 --> 01:26:13,360
actions
2225
01:26:13,360 --> 01:26:15,760
card content
2226
01:26:15,760 --> 01:26:17,760
card media
2227
01:26:17,760 --> 01:26:18,800
button
2228
01:26:18,800 --> 01:26:21,199
and a typography
2229
01:26:21,199 --> 01:26:22,560
again
2230
01:26:22,560 --> 01:26:23,360
from
2231
01:26:23,360 --> 01:26:27,199
add material ui core great then we're
2232
01:26:27,199 --> 01:26:28,960
also going to import some icons from
2233
01:26:28,960 --> 01:26:30,960
material ui to do that we're going to
2234
01:26:30,960 --> 01:26:32,560
say import
2235
01:26:32,560 --> 01:26:33,840
that's going to be
2236
01:26:33,840 --> 01:26:34,639
thumb
2237
01:26:34,639 --> 01:26:35,360
up
2238
01:26:35,360 --> 01:26:36,639
alternative
2239
01:26:36,639 --> 01:26:38,000
so just alt
2240
01:26:38,000 --> 01:26:39,199
icon
2241
01:26:39,199 --> 01:26:40,239
from
2242
01:26:40,239 --> 01:26:42,159
add material
2243
01:26:42,159 --> 01:26:44,400
ui and then it's not going to be core
2244
01:26:44,400 --> 01:26:47,840
it's going to be icons and then thumb
2245
01:26:47,840 --> 01:26:48,560
up
2246
01:26:48,560 --> 01:26:50,800
alt so make sure to copy this in the
2247
01:26:50,800 --> 01:26:52,719
description you'll have access to the
2248
01:26:52,719 --> 01:26:55,360
full code of this project so if you just
2249
01:26:55,360 --> 01:26:57,280
mistyped something or something doesn't
2250
01:26:57,280 --> 01:27:00,159
work just refer down the description and
2251
01:27:00,159 --> 01:27:01,920
copy and paste it
2252
01:27:01,920 --> 01:27:04,960
then we have import delete
2253
01:27:04,960 --> 01:27:05,920
icon
2254
01:27:05,920 --> 01:27:06,880
from
2255
01:27:06,880 --> 01:27:09,520
add material ui it's again not going to
2256
01:27:09,520 --> 01:27:11,760
be core it's going to be icons and then
2257
01:27:11,760 --> 01:27:14,320
forward slash delete make sure it is
2258
01:27:14,320 --> 01:27:16,080
uppercase d
2259
01:27:16,080 --> 01:27:18,639
and the last one is going to be import
2260
01:27:18,639 --> 01:27:19,679
more
2261
01:27:19,679 --> 01:27:20,800
horus
2262
01:27:20,800 --> 01:27:23,440
icon there's an i there
2263
01:27:23,440 --> 01:27:25,760
and that's going to be imported from
2264
01:27:25,760 --> 01:27:28,080
add material ui
2265
01:27:28,080 --> 01:27:31,840
icons and then more horiz just like that
2266
01:27:31,840 --> 01:27:33,440
with that out of the way we can now
2267
01:27:33,440 --> 01:27:36,080
start generating our jsx to do that
2268
01:27:36,080 --> 01:27:39,520
we're going to have a card in here
2269
01:27:39,520 --> 01:27:42,239
that card is going to have a class name
2270
01:27:42,239 --> 01:27:45,040
of classes dot card
2271
01:27:45,040 --> 01:27:47,360
inside of there we're going to have card
2272
01:27:47,360 --> 01:27:48,960
media
2273
01:27:48,960 --> 01:27:51,520
which is going to be just an image
2274
01:27:51,520 --> 01:27:54,000
it's going to be a self-closing tag and
2275
01:27:54,000 --> 01:27:56,480
it's going to have a class name
2276
01:27:56,480 --> 01:27:57,800
of
2277
01:27:57,800 --> 01:27:59,600
classes.media
2278
01:27:59,600 --> 01:28:02,719
image is going to be equal to post that
2279
01:28:02,719 --> 01:28:04,800
selected file
2280
01:28:04,800 --> 01:28:06,000
that's it
2281
01:28:06,000 --> 01:28:08,239
and finally title
2282
01:28:08,239 --> 01:28:12,159
is going to be equal to post dot title
2283
01:28:12,159 --> 01:28:14,320
okay so we don't yet have the post but
2284
01:28:14,320 --> 01:28:16,480
we can immediately get it from props
2285
01:28:16,480 --> 01:28:18,159
considering we are pasting it right
2286
01:28:18,159 --> 01:28:18,960
there
2287
01:28:18,960 --> 01:28:21,360
what we did here is we destructured the
2288
01:28:21,360 --> 01:28:23,360
props like so
2289
01:28:23,360 --> 01:28:24,480
great
2290
01:28:24,480 --> 01:28:28,880
just down below we have a div
2291
01:28:28,880 --> 01:28:31,360
that div is going to have a class name
2292
01:28:31,360 --> 01:28:34,239
of classes.overlay
2293
01:28:34,239 --> 01:28:35,920
and inside of there we're going to have
2294
01:28:35,920 --> 01:28:37,520
two typographies
2295
01:28:37,520 --> 01:28:39,679
so typography1
2296
01:28:39,679 --> 01:28:41,440
and then this one is going to have a
2297
01:28:41,440 --> 01:28:42,480
variant
2298
01:28:42,480 --> 01:28:45,040
of h6
2299
01:28:45,040 --> 01:28:47,520
and also it's going to say pose that
2300
01:28:47,520 --> 01:28:49,600
creator so we're getting all data from
2301
01:28:49,600 --> 01:28:50,639
the card
2302
01:28:50,639 --> 01:28:52,320
the second one i'm going to copy this
2303
01:28:52,320 --> 01:28:55,120
one is going to be of body 2 and this
2304
01:28:55,120 --> 01:28:57,040
one is going to be about when the post
2305
01:28:57,040 --> 01:28:59,280
was created we can use that moment
2306
01:28:59,280 --> 01:29:01,040
library installed at the beginning
2307
01:29:01,040 --> 01:29:03,920
that's going to be import moment from
2308
01:29:03,920 --> 01:29:05,600
moment
2309
01:29:05,600 --> 01:29:08,719
and now we can do it like this moment
2310
01:29:08,719 --> 01:29:11,600
we pass in that post that created
2311
01:29:11,600 --> 01:29:14,000
add and then moment has something known
2312
01:29:14,000 --> 01:29:17,040
as dot from now and you call it as a
2313
01:29:17,040 --> 01:29:19,360
function this basically says five
2314
01:29:19,360 --> 01:29:22,400
minutes ago five seconds ago and so on
2315
01:29:22,400 --> 01:29:24,719
that's it below that we're going to have
2316
01:29:24,719 --> 01:29:25,840
a div
2317
01:29:25,840 --> 01:29:27,679
and this div is going to have a class
2318
01:29:27,679 --> 01:29:29,120
name of
2319
01:29:29,120 --> 01:29:32,800
classes dot overlay 2.
2320
01:29:32,800 --> 01:29:35,040
below that we're going to have a button
2321
01:29:35,040 --> 01:29:36,960
or rather inside of that
2322
01:29:36,960 --> 01:29:40,320
that button is going to have a style of
2323
01:29:40,320 --> 01:29:41,440
color
2324
01:29:41,440 --> 01:29:43,840
white like this and then we're going to
2325
01:29:43,840 --> 01:29:46,400
give it a size equal to small
2326
01:29:46,400 --> 01:29:48,639
and we are also going to add the on
2327
01:29:48,639 --> 01:29:51,280
click listener on it with nothing in it
2328
01:29:51,280 --> 01:29:54,159
yet just an empty function
2329
01:29:54,159 --> 01:29:56,639
that button is going to include the icon
2330
01:29:56,639 --> 01:29:59,600
so in there i'm going to say more horus
2331
01:29:59,600 --> 01:30:00,960
icon
2332
01:30:00,960 --> 01:30:03,280
and we're going to give it a font size
2333
01:30:03,280 --> 01:30:06,239
of default
2334
01:30:06,239 --> 01:30:09,040
just like that close it and that's our
2335
01:30:09,040 --> 01:30:10,000
button
2336
01:30:10,000 --> 01:30:12,000
now below that div we're going to have
2337
01:30:12,000 --> 01:30:15,920
another div with a class name of details
2338
01:30:15,920 --> 01:30:20,080
class name is equal to classes.details
2339
01:30:20,080 --> 01:30:21,679
div is also going to contain a
2340
01:30:21,679 --> 01:30:24,320
typography so i'm going to copy this one
2341
01:30:24,320 --> 01:30:25,760
first
2342
01:30:25,760 --> 01:30:28,560
the variant of this one is going to be
2343
01:30:28,560 --> 01:30:30,639
body2
2344
01:30:30,639 --> 01:30:35,040
the color is going to be text secondary
2345
01:30:35,040 --> 01:30:36,800
and then in there we're going to loop
2346
01:30:36,800 --> 01:30:39,840
over our tags and add the hashtag before
2347
01:30:39,840 --> 01:30:43,600
them we do it this way post that tags
2348
01:30:43,600 --> 01:30:46,800
that map and then for each tag we want
2349
01:30:46,800 --> 01:30:49,120
to do something and that something is
2350
01:30:49,120 --> 01:30:51,920
return a string where each string is
2351
01:30:51,920 --> 01:30:54,639
going to start with a hashtag sign and
2352
01:30:54,639 --> 01:30:57,679
then immediately afterwards tag make
2353
01:30:57,679 --> 01:31:00,000
sure to leave one space afterwards
2354
01:31:00,000 --> 01:31:01,199
that's it
2355
01:31:01,199 --> 01:31:02,880
below that we're going to have yet
2356
01:31:02,880 --> 01:31:04,719
another typography
2357
01:31:04,719 --> 01:31:08,719
this one is going to have a class name
2358
01:31:08,719 --> 01:31:09,880
of
2359
01:31:09,880 --> 01:31:11,520
classes.title
2360
01:31:11,520 --> 01:31:13,760
it's not going the variant is going to
2361
01:31:13,760 --> 01:31:17,040
be of h5 it's not going to have text
2362
01:31:17,040 --> 01:31:18,960
secondary rather it's going to have
2363
01:31:18,960 --> 01:31:20,560
gutter
2364
01:31:20,560 --> 01:31:21,920
bottom
2365
01:31:21,920 --> 01:31:23,840
great and inside of there we're going to
2366
01:31:23,840 --> 01:31:26,239
remove this and say
2367
01:31:26,239 --> 01:31:28,560
post dot message
2368
01:31:28,560 --> 01:31:30,159
that should have been in the card
2369
01:31:30,159 --> 01:31:32,480
content so i'm gonna add card content
2370
01:31:32,480 --> 01:31:34,159
right there
2371
01:31:34,159 --> 01:31:37,120
and put that typography inside of there
2372
01:31:37,120 --> 01:31:38,480
great
2373
01:31:38,480 --> 01:31:40,960
finally below the card content we have
2374
01:31:40,960 --> 01:31:41,920
card
2375
01:31:41,920 --> 01:31:46,080
actions with a class name of classes dot
2376
01:31:46,080 --> 01:31:48,800
card actions inside of there we're gonna
2377
01:31:48,800 --> 01:31:50,560
have two buttons they're gonna be really
2378
01:31:50,560 --> 01:31:53,120
similar first one is going to be of size
2379
01:31:53,120 --> 01:31:55,120
equal to small
2380
01:31:55,120 --> 01:31:58,560
also of color equal to primary
2381
01:31:58,560 --> 01:32:00,480
and it's going to have an on click
2382
01:32:00,480 --> 01:32:02,719
function just like this we're going to
2383
01:32:02,719 --> 01:32:04,800
leave it blank for now
2384
01:32:04,800 --> 01:32:07,360
this one is going to have a thumb
2385
01:32:07,360 --> 01:32:10,960
up icon so thumb up icon
2386
01:32:10,960 --> 01:32:13,120
and the font size of that icon is going
2387
01:32:13,120 --> 01:32:15,520
to be small
2388
01:32:15,520 --> 01:32:17,120
just like so
2389
01:32:17,120 --> 01:32:19,600
next to it we're going to say like and
2390
01:32:19,600 --> 01:32:22,400
next to it we're going to add post dot
2391
01:32:22,400 --> 01:32:24,960
like count in here later on we're going
2392
01:32:24,960 --> 01:32:26,719
to have the like count
2393
01:32:26,719 --> 01:32:29,360
now i'm gonna copy that whole button and
2394
01:32:29,360 --> 01:32:30,880
in this one everything is going to be
2395
01:32:30,880 --> 01:32:32,400
the same but this one is going to
2396
01:32:32,400 --> 01:32:35,199
contain the delete icon and also it's
2397
01:32:35,199 --> 01:32:38,080
going to say delete and we don't need
2398
01:32:38,080 --> 01:32:40,560
the like account there okay looks like
2399
01:32:40,560 --> 01:32:42,639
we have one error and that is that we
2400
01:32:42,639 --> 01:32:45,199
are missing opening and closing curly
2401
01:32:45,199 --> 01:32:48,320
brace there let's save it and if we did
2402
01:32:48,320 --> 01:32:50,560
everything correctly our card should
2403
01:32:50,560 --> 01:32:52,800
look pretty good right now and this
2404
01:32:52,800 --> 01:32:54,800
error is saying that we don't have
2405
01:32:54,800 --> 01:32:57,360
access to material ui icons that means
2406
01:32:57,360 --> 01:32:59,679
that material ui icons is not part of
2407
01:32:59,679 --> 01:33:01,280
the material ui core
2408
01:33:01,280 --> 01:33:03,040
then to get access to it we're going to
2409
01:33:03,040 --> 01:33:06,000
copy this part and simply install it so
2410
01:33:06,000 --> 01:33:08,560
close the terminal run npm install and
2411
01:33:08,560 --> 01:33:11,600
then paste add material ui forward slash
2412
01:33:11,600 --> 01:33:12,719
icons
2413
01:33:12,719 --> 01:33:14,639
once that installs we'll be able to run
2414
01:33:14,639 --> 01:33:17,040
npm start and see how everything looks
2415
01:33:17,040 --> 01:33:18,080
like
2416
01:33:18,080 --> 01:33:19,920
let's run it
2417
01:33:19,920 --> 01:33:22,000
that's it you can see we have a nice
2418
01:33:22,000 --> 01:33:24,000
looking card with a lot of styles
2419
01:33:24,000 --> 01:33:26,800
overlay we have also this created add
2420
01:33:26,800 --> 01:33:29,280
function we can click this button which
2421
01:33:29,280 --> 01:33:31,040
later that button is going to fill all
2422
01:33:31,040 --> 01:33:32,800
of this input so we'll be able to change
2423
01:33:32,800 --> 01:33:35,600
it and yeah everything looks good right
2424
01:33:35,600 --> 01:33:37,120
now we don't have the functionality we
2425
01:33:37,120 --> 01:33:40,000
cannot edit like or delete but this is
2426
01:33:40,000 --> 01:33:42,560
the card this is the css now we can keep
2427
01:33:42,560 --> 01:33:44,320
on adding new cards and they're gonna
2428
01:33:44,320 --> 01:33:46,960
appear right there now is the time to
2429
01:33:46,960 --> 01:33:49,360
implement all the other functionalities
2430
01:33:49,360 --> 01:33:51,120
there's one thing we can do before
2431
01:33:51,120 --> 01:33:52,719
adding all the other functionality and
2432
01:33:52,719 --> 01:33:54,159
it's immediately going to make our
2433
01:33:54,159 --> 01:33:56,480
application look so much better let's go
2434
01:33:56,480 --> 01:33:59,760
back to code inside of here in the src
2435
01:33:59,760 --> 01:34:02,400
folder create a new file called
2436
01:34:02,400 --> 01:34:05,400
index.css
2437
01:34:07,679 --> 01:34:10,080
we have to connect that index.css to our
2438
01:34:10,080 --> 01:34:12,800
index.js file so let's do that here
2439
01:34:12,800 --> 01:34:17,280
just say import dot slash index.css
2440
01:34:17,280 --> 01:34:19,360
inside of that index.css we're going to
2441
01:34:19,360 --> 01:34:22,239
add our svg background so let's go to
2442
01:34:22,239 --> 01:34:24,480
chrome and you can visit this site
2443
01:34:24,480 --> 01:34:26,639
svgbackgrounds.com
2444
01:34:26,639 --> 01:34:28,480
in here we have a lot of backgrounds you
2445
01:34:28,480 --> 01:34:30,560
can choose the one you like for me
2446
01:34:30,560 --> 01:34:32,880
personally i chose this one so let's
2447
01:34:32,880 --> 01:34:35,520
click that and then you can copy all of
2448
01:34:35,520 --> 01:34:38,480
this css output let's take that and
2449
01:34:38,480 --> 01:34:41,040
let's go back inside of the css file
2450
01:34:41,040 --> 01:34:43,360
we're going to target our body and then
2451
01:34:43,360 --> 01:34:45,679
simply paste everything we copied that
2452
01:34:45,679 --> 01:34:48,800
should work let's save it and go back as
2453
01:34:48,800 --> 01:34:51,199
you can see that's it already looks so
2454
01:34:51,199 --> 01:34:54,320
much better now on to the functionality
2455
01:34:54,320 --> 01:34:56,400
next logical thing to do would be to
2456
01:34:56,400 --> 01:34:58,719
implement the update route so let's do
2457
01:34:58,719 --> 01:35:00,639
just that i'm going to collapse our
2458
01:35:00,639 --> 01:35:03,920
terminals right here and then go to
2459
01:35:03,920 --> 01:35:05,280
server
2460
01:35:05,280 --> 01:35:08,320
routes and then finally to posts we can
2461
01:35:08,320 --> 01:35:10,960
add a new route just below and that's
2462
01:35:10,960 --> 01:35:13,360
going to be router.patch
2463
01:35:13,360 --> 01:35:15,840
patch is used for updating existing
2464
01:35:15,840 --> 01:35:18,480
documents in here it's not only going to
2465
01:35:18,480 --> 01:35:20,320
be forward slash it's going to be
2466
01:35:20,320 --> 01:35:22,239
forward slash id
2467
01:35:22,239 --> 01:35:24,320
and that's going to be dynamic for the
2468
01:35:24,320 --> 01:35:26,400
reason that we need to know the id
2469
01:35:26,400 --> 01:35:28,400
before we want to edit something so for
2470
01:35:28,400 --> 01:35:30,159
the editing we need the id for the
2471
01:35:30,159 --> 01:35:31,920
creation of course we don't because
2472
01:35:31,920 --> 01:35:34,400
we're gonna just add a new id but for
2473
01:35:34,400 --> 01:35:36,639
updating we need to know the id of the
2474
01:35:36,639 --> 01:35:38,239
existing post
2475
01:35:38,239 --> 01:35:40,880
and now let's call our new function
2476
01:35:40,880 --> 01:35:42,400
update post
2477
01:35:42,400 --> 01:35:44,239
of course we'll be importing that from
2478
01:35:44,239 --> 01:35:46,400
the controllers and it would make sense
2479
01:35:46,400 --> 01:35:48,480
to go ahead and create it so now we can
2480
01:35:48,480 --> 01:35:51,040
go over to controllers and then in here
2481
01:35:51,040 --> 01:35:52,800
let's start with the creation of that
2482
01:35:52,800 --> 01:35:54,320
new function
2483
01:35:54,320 --> 01:35:57,440
export const update post
2484
01:35:57,440 --> 01:35:59,440
it's also going to be an asynchronous
2485
01:35:59,440 --> 01:36:01,840
function and as parameters is going to
2486
01:36:01,840 --> 01:36:04,159
have request and response first thing
2487
01:36:04,159 --> 01:36:06,159
that we want to do is we want to extract
2488
01:36:06,159 --> 01:36:07,840
that id
2489
01:36:07,840 --> 01:36:09,119
from
2490
01:36:09,119 --> 01:36:10,719
that params
2491
01:36:10,719 --> 01:36:12,159
so how do we know we are going to
2492
01:36:12,159 --> 01:36:14,880
receive this well if you remember we set
2493
01:36:14,880 --> 01:36:16,639
our route to be this
2494
01:36:16,639 --> 01:36:19,280
so that means that finally once we make
2495
01:36:19,280 --> 01:36:21,199
a request the request is going to be
2496
01:36:21,199 --> 01:36:23,920
made to posts and then one two three
2497
01:36:23,920 --> 01:36:25,600
that one two three is immediately going
2498
01:36:25,600 --> 01:36:28,480
to fill the value of id
2499
01:36:28,480 --> 01:36:30,159
while using object destructuring we can
2500
01:36:30,159 --> 01:36:32,320
also rename our properties and in this
2501
01:36:32,320 --> 01:36:34,159
case we're going to have to rename our
2502
01:36:34,159 --> 01:36:37,520
id to underscore id just like that
2503
01:36:37,520 --> 01:36:39,199
moving further we're going to do one
2504
01:36:39,199 --> 01:36:41,360
simple check to see if this underscore
2505
01:36:41,360 --> 01:36:44,880
id is really a mongoose object id to do
2506
01:36:44,880 --> 01:36:46,800
that we're going to add a simple if
2507
01:36:46,800 --> 01:36:48,960
check and there we're going to check if
2508
01:36:48,960 --> 01:36:51,520
mongoose.types
2509
01:36:51,520 --> 01:36:55,920
that object id that is valid in there we
2510
01:36:55,920 --> 01:36:59,119
pass in that underscore id so if it is
2511
01:36:59,119 --> 01:37:02,000
not valid we can add that not at the
2512
01:37:02,000 --> 01:37:05,199
start then we simply want to return rest
2513
01:37:05,199 --> 01:37:08,320
that status of 404 and then we can say
2514
01:37:08,320 --> 01:37:10,400
dot send
2515
01:37:10,400 --> 01:37:15,199
no post with that id just like that
2516
01:37:15,199 --> 01:37:17,840
on the other hand if the id is valid
2517
01:37:17,840 --> 01:37:20,560
then we can update our post to do that
2518
01:37:20,560 --> 01:37:22,320
we're going to call our model which is
2519
01:37:22,320 --> 01:37:24,880
the post message and we're going to call
2520
01:37:24,880 --> 01:37:28,800
a method called find by id and update on
2521
01:37:28,800 --> 01:37:29,600
it
2522
01:37:29,600 --> 01:37:32,080
there we need to pass the underscore id
2523
01:37:32,080 --> 01:37:34,320
as the first parameter as the second
2524
01:37:34,320 --> 01:37:36,480
parameter or the argument we have to
2525
01:37:36,480 --> 01:37:39,440
pass the whole updated post but where
2526
01:37:39,440 --> 01:37:41,199
are we receiving the data for the
2527
01:37:41,199 --> 01:37:42,239
updates
2528
01:37:42,239 --> 01:37:43,760
well we are receiving it from the
2529
01:37:43,760 --> 01:37:47,040
request.body so in here we can say const
2530
01:37:47,040 --> 01:37:50,080
post is equal to request that body that
2531
01:37:50,080 --> 01:37:52,080
is going to be sent from the front end
2532
01:37:52,080 --> 01:37:54,400
now in here we can pass that post
2533
01:37:54,400 --> 01:37:57,040
and finally we have to specify new to be
2534
01:37:57,040 --> 01:37:59,440
equal to true so that we can actually
2535
01:37:59,440 --> 01:38:02,159
receive the updated version of that post
2536
01:38:02,159 --> 01:38:04,400
we can get that through here
2537
01:38:04,400 --> 01:38:08,239
const updated post is equal to and then
2538
01:38:08,239 --> 01:38:10,480
this is an asynchronous action so what
2539
01:38:10,480 --> 01:38:13,119
we have to do is add a weight in front
2540
01:38:13,119 --> 01:38:15,520
of it now we have access to the updated
2541
01:38:15,520 --> 01:38:18,239
post and also it is going to be updated
2542
01:38:18,239 --> 01:38:19,679
in the database
2543
01:38:19,679 --> 01:38:22,719
finally we can do rest.json
2544
01:38:22,719 --> 01:38:26,159
and we can send over that updated post
2545
01:38:26,159 --> 01:38:28,080
that's going to be it for the update
2546
01:38:28,080 --> 01:38:30,239
route and the controller now let's try
2547
01:38:30,239 --> 01:38:32,320
adding the client side for the same
2548
01:38:32,320 --> 01:38:35,119
logic first let's go into the components
2549
01:38:35,119 --> 01:38:37,280
in the form because that's where we will
2550
01:38:37,280 --> 01:38:39,600
be dealing with the update logic
2551
01:38:39,600 --> 01:38:41,920
in here we have to somehow
2552
01:38:41,920 --> 01:38:45,679
get the current id of the post we're on
2553
01:38:45,679 --> 01:38:47,679
what does that mean well if we go back
2554
01:38:47,679 --> 01:38:50,159
to the page in here you can see these
2555
01:38:50,159 --> 01:38:52,320
three dots at the top right corner
2556
01:38:52,320 --> 01:38:54,400
that's going to be the edit button once
2557
01:38:54,400 --> 01:38:56,800
we click that we need to pass over the
2558
01:38:56,800 --> 01:38:59,920
id of this specific post to our form
2559
01:38:59,920 --> 01:39:02,000
component that way we can change this
2560
01:39:02,000 --> 01:39:04,560
from creating a memory to editing a
2561
01:39:04,560 --> 01:39:08,000
memory under this specific id so let's
2562
01:39:08,000 --> 01:39:10,480
do that this button is definitely under
2563
01:39:10,480 --> 01:39:13,600
the post component moving on to posts
2564
01:39:13,600 --> 01:39:16,960
and posts in here let's find that edit
2565
01:39:16,960 --> 01:39:19,600
button and it looks like it's here we
2566
01:39:19,600 --> 01:39:21,920
have the styles for it we have the size
2567
01:39:21,920 --> 01:39:24,400
and there is the on click property but
2568
01:39:24,400 --> 01:39:26,960
you can see we left it empty so in here
2569
01:39:26,960 --> 01:39:29,119
now we have to actually do something we
2570
01:39:29,119 --> 01:39:31,760
have to keep track of the current id to
2571
01:39:31,760 --> 01:39:34,159
do that we are going to go back not only
2572
01:39:34,159 --> 01:39:38,159
to posts but also to our app.js in here
2573
01:39:38,159 --> 01:39:39,600
we're going to keep track of that
2574
01:39:39,600 --> 01:39:40,880
current id
2575
01:39:40,880 --> 01:39:42,960
why in the app because we have to share
2576
01:39:42,960 --> 01:39:45,199
that state of current id between the
2577
01:39:45,199 --> 01:39:48,320
posts and the form and our app is the
2578
01:39:48,320 --> 01:39:50,639
only parent component that is parent to
2579
01:39:50,639 --> 01:39:53,040
both posts and the form
2580
01:39:53,040 --> 01:39:54,719
now later on we're going to refactor
2581
01:39:54,719 --> 01:39:56,960
this to use redux but for now let me
2582
01:39:56,960 --> 01:39:58,560
show you how to do it with just plain
2583
01:39:58,560 --> 01:40:00,880
react in the app we're going to import
2584
01:40:00,880 --> 01:40:03,760
the use state hook and then at the top
2585
01:40:03,760 --> 01:40:05,679
we can say const
2586
01:40:05,679 --> 01:40:10,320
current id and set current id
2587
01:40:10,320 --> 01:40:13,360
then we can use the use state and set
2588
01:40:13,360 --> 01:40:15,040
the id to be
2589
01:40:15,040 --> 01:40:17,040
null at the start if you don't have the
2590
01:40:17,040 --> 01:40:19,520
id selected now what we're going to do
2591
01:40:19,520 --> 01:40:22,880
is we're going to pass that current id
2592
01:40:22,880 --> 01:40:24,560
over to the form
2593
01:40:24,560 --> 01:40:27,920
so we say current id is equal to current
2594
01:40:27,920 --> 01:40:30,639
id just like that and we are also going
2595
01:40:30,639 --> 01:40:33,280
to pass the setter methods for that id
2596
01:40:33,280 --> 01:40:36,960
both in here to posts and also in here
2597
01:40:36,960 --> 01:40:39,679
to form so set current
2598
01:40:39,679 --> 01:40:42,639
id is going to be equal to set current
2599
01:40:42,639 --> 01:40:43,760
id
2600
01:40:43,760 --> 01:40:46,320
and again why are we doing this well i
2601
01:40:46,320 --> 01:40:48,960
want to show you how data management
2602
01:40:48,960 --> 01:40:51,199
would look like without redux you can
2603
01:40:51,199 --> 01:40:53,040
see you have to bring something all the
2604
01:40:53,040 --> 01:40:55,920
way up to the app.js and then send it
2605
01:40:55,920 --> 01:40:58,320
over to all the components of course
2606
01:40:58,320 --> 01:41:00,080
since we're sending it that implies that
2607
01:41:00,080 --> 01:41:02,239
we have to go over to the form and now
2608
01:41:02,239 --> 01:41:05,280
accept these as props so in here we can
2609
01:41:05,280 --> 01:41:08,480
destructure and then use that current id
2610
01:41:08,480 --> 01:41:11,280
as well as the set current id
2611
01:41:11,280 --> 01:41:13,360
and just to say consistent we are also
2612
01:41:13,360 --> 01:41:15,600
going to receive the set current id
2613
01:41:15,600 --> 01:41:18,880
inside of posts as props
2614
01:41:18,880 --> 01:41:21,119
now inside of posts we're going to pass
2615
01:41:21,119 --> 01:41:23,280
that set current id prop
2616
01:41:23,280 --> 01:41:25,679
one more time one level deeper to the
2617
01:41:25,679 --> 01:41:26,639
post
2618
01:41:26,639 --> 01:41:28,880
and this is called props drilling we are
2619
01:41:28,880 --> 01:41:31,280
basically continuing sending the same
2620
01:41:31,280 --> 01:41:34,080
prop over and over again to the most
2621
01:41:34,080 --> 01:41:36,480
child component and that's the exact
2622
01:41:36,480 --> 01:41:39,440
problem that redux solves great now that
2623
01:41:39,440 --> 01:41:41,440
we have this we can know that also in
2624
01:41:41,440 --> 01:41:43,600
the post we are going to receive that
2625
01:41:43,600 --> 01:41:46,400
set current id right here
2626
01:41:46,400 --> 01:41:48,880
along the post finally we are back where
2627
01:41:48,880 --> 01:41:51,760
we were that now on the on click we can
2628
01:41:51,760 --> 01:41:54,480
call that set current id
2629
01:41:54,480 --> 01:41:57,760
and we can set it to be equal to post
2630
01:41:57,760 --> 01:42:01,360
dot underscore id great now that is
2631
01:42:01,360 --> 01:42:03,280
changing and it's not only going to be
2632
01:42:03,280 --> 01:42:05,280
changed in here it's also going to be
2633
01:42:05,280 --> 01:42:07,520
changed in the form and also it's going
2634
01:42:07,520 --> 01:42:10,239
to be changed in the app so we wanted to
2635
01:42:10,239 --> 01:42:12,880
bring that information over to the form
2636
01:42:12,880 --> 01:42:15,199
so if we go there you can see now we are
2637
01:42:15,199 --> 01:42:18,000
accepting that current id inside of the
2638
01:42:18,000 --> 01:42:18,880
form
2639
01:42:18,880 --> 01:42:21,360
what we're going to do now is in here
2640
01:42:21,360 --> 01:42:24,239
we're going to add one if statement
2641
01:42:24,239 --> 01:42:24,960
if
2642
01:42:24,960 --> 01:42:27,840
we have a current id which means that if
2643
01:42:27,840 --> 01:42:30,480
the current id is not null
2644
01:42:30,480 --> 01:42:33,199
then we're not going to dispatch a
2645
01:42:33,199 --> 01:42:35,679
create post we're going to dispatch
2646
01:42:35,679 --> 01:42:38,159
something different
2647
01:42:38,159 --> 01:42:39,760
and that something is going to be
2648
01:42:39,760 --> 01:42:41,199
dispatch
2649
01:42:41,199 --> 01:42:42,400
update
2650
01:42:42,400 --> 01:42:43,760
post
2651
01:42:43,760 --> 01:42:46,880
just like that but keep in mind with
2652
01:42:46,880 --> 01:42:49,679
update post we need to know also the id
2653
01:42:49,679 --> 01:42:51,679
so the post data is going to be the
2654
01:42:51,679 --> 01:42:54,159
second parameter the first parameter is
2655
01:42:54,159 --> 01:42:56,880
going to be the current id
2656
01:42:56,880 --> 01:42:59,840
and else we are going to dispatch a
2657
01:42:59,840 --> 01:43:01,199
create post
2658
01:43:01,199 --> 01:43:03,280
so if we don't have a currently selected
2659
01:43:03,280 --> 01:43:06,320
id that must mean that we are creating a
2660
01:43:06,320 --> 01:43:09,440
post and not updating it that's great
2661
01:43:09,440 --> 01:43:11,920
but you know that currently this update
2662
01:43:11,920 --> 01:43:14,000
post is not defined so we have to go
2663
01:43:14,000 --> 01:43:16,800
ahead and import it from here update
2664
01:43:16,800 --> 01:43:19,920
post not only that we now have to go to
2665
01:43:19,920 --> 01:43:23,119
slash actions slash post to implement it
2666
01:43:23,119 --> 01:43:25,920
but actions are using the api so let's
2667
01:43:25,920 --> 01:43:28,639
first go to the api inside of here we're
2668
01:43:28,639 --> 01:43:31,360
going to implement the api call for our
2669
01:43:31,360 --> 01:43:33,520
update post route that's going to look
2670
01:43:33,520 --> 01:43:34,639
like this
2671
01:43:34,639 --> 01:43:36,400
export const
2672
01:43:36,400 --> 01:43:39,840
update post and in this case you know
2673
01:43:39,840 --> 01:43:41,440
that we're receiving two parameters
2674
01:43:41,440 --> 01:43:44,159
we're receiving in the id and also we
2675
01:43:44,159 --> 01:43:46,560
are saving in the updated post or you
2676
01:43:46,560 --> 01:43:48,800
can call it post data that's going to be
2677
01:43:48,800 --> 01:43:50,480
equal to an arrow function with an
2678
01:43:50,480 --> 01:43:52,800
instantaneous return and in there we're
2679
01:43:52,800 --> 01:43:55,840
going to have axios.patch so to which
2680
01:43:55,840 --> 01:43:59,040
url will be making that request well
2681
01:43:59,040 --> 01:44:00,719
it's going to be
2682
01:44:00,719 --> 01:44:01,920
url
2683
01:44:01,920 --> 01:44:04,880
forward slash and then id so for the
2684
01:44:04,880 --> 01:44:07,600
first time ever we not only have the url
2685
01:44:07,600 --> 01:44:10,159
we also have the id specified because we
2686
01:44:10,159 --> 01:44:12,400
need to know what post do we want to
2687
01:44:12,400 --> 01:44:14,960
update and then as the second parameter
2688
01:44:14,960 --> 01:44:18,320
we are sending over the updated post
2689
01:44:18,320 --> 01:44:20,960
great now we can go to actions
2690
01:44:20,960 --> 01:44:22,560
inside of the actions of course we're
2691
01:44:22,560 --> 01:44:24,320
going to create another function which
2692
01:44:24,320 --> 01:44:27,440
is going to be called update post
2693
01:44:27,440 --> 01:44:29,600
in there we are receiving now two things
2694
01:44:29,600 --> 01:44:32,000
not just one which is going to be the id
2695
01:44:32,000 --> 01:44:33,679
and the post
2696
01:44:33,679 --> 01:44:35,520
and then of course we have async
2697
01:44:35,520 --> 01:44:37,520
dispatch
2698
01:44:37,520 --> 01:44:39,600
and then in there we're going to have a
2699
01:44:39,600 --> 01:44:43,040
try and catch block
2700
01:44:43,040 --> 01:44:44,800
inside of the try we're going to be
2701
01:44:44,800 --> 01:44:47,280
making that api request to update the
2702
01:44:47,280 --> 01:44:49,520
post so that's going to be like this
2703
01:44:49,520 --> 01:44:50,560
await
2704
01:44:50,560 --> 01:44:54,239
api dot update post and we have to pass
2705
01:44:54,239 --> 01:44:55,600
over the id
2706
01:44:55,600 --> 01:44:57,040
and the post
2707
01:44:57,040 --> 01:44:59,600
what is this api request returning well
2708
01:44:59,600 --> 01:45:02,320
it's returning the updated memory or the
2709
01:45:02,320 --> 01:45:05,600
post so we can get the response here but
2710
01:45:05,600 --> 01:45:07,520
not only the response we can destructure
2711
01:45:07,520 --> 01:45:09,840
the response and immediately get the
2712
01:45:09,840 --> 01:45:11,199
data
2713
01:45:11,199 --> 01:45:12,960
once we have the data for the updated
2714
01:45:12,960 --> 01:45:15,360
post we can call this patch
2715
01:45:15,360 --> 01:45:18,080
in there pass in an action of type
2716
01:45:18,080 --> 01:45:19,679
update
2717
01:45:19,679 --> 01:45:21,760
and also we're going to be sending over
2718
01:45:21,760 --> 01:45:23,920
the payload which is simply going to be
2719
01:45:23,920 --> 01:45:25,679
data if you've been working with redux
2720
01:45:25,679 --> 01:45:27,360
for some time then you know that it's
2721
01:45:27,360 --> 01:45:29,679
usually a good idea to have action types
2722
01:45:29,679 --> 01:45:32,080
set as constants and then to import them
2723
01:45:32,080 --> 01:45:33,520
in these files
2724
01:45:33,520 --> 01:45:34,880
we're going to do that later in the
2725
01:45:34,880 --> 01:45:36,960
video i just wanted to lay that out for
2726
01:45:36,960 --> 01:45:39,600
you who were expecting that great now
2727
01:45:39,600 --> 01:45:41,920
after we add the console log here so
2728
01:45:41,920 --> 01:45:44,800
console.log error.message you know what
2729
01:45:44,800 --> 01:45:46,080
is the next step
2730
01:45:46,080 --> 01:45:48,400
after we do the action we have to go to
2731
01:45:48,400 --> 01:45:51,040
the reducers so save this file and let's
2732
01:45:51,040 --> 01:45:53,080
move over to the reducers
2733
01:45:53,080 --> 01:45:54,880
posts.js
2734
01:45:54,880 --> 01:45:57,520
in here we have to implement the case
2735
01:45:57,520 --> 01:46:00,239
for the update post or let's see how did
2736
01:46:00,239 --> 01:46:02,639
we call it we called it just update
2737
01:46:02,639 --> 01:46:06,000
so in here let's add a new case that
2738
01:46:06,000 --> 01:46:09,280
case is going to say update
2739
01:46:09,280 --> 01:46:12,560
so what do we want to do on the update
2740
01:46:12,560 --> 01:46:14,480
well in this case we want to return
2741
01:46:14,480 --> 01:46:16,719
something of course and that something
2742
01:46:16,719 --> 01:46:19,679
is going to be posts dot map a quick
2743
01:46:19,679 --> 01:46:22,080
question do you know what is the output
2744
01:46:22,080 --> 01:46:24,719
of any map for that matter so when we
2745
01:46:24,719 --> 01:46:27,440
have array dot map what is the output of
2746
01:46:27,440 --> 01:46:29,119
a map method
2747
01:46:29,119 --> 01:46:31,600
it is an array so we'll be mapping over
2748
01:46:31,600 --> 01:46:33,280
the posts array we'll be changing
2749
01:46:33,280 --> 01:46:34,960
something in there and then we'll be
2750
01:46:34,960 --> 01:46:37,119
returning the changed array
2751
01:46:37,119 --> 01:46:40,000
inside of the map we have a single post
2752
01:46:40,000 --> 01:46:41,760
and then what we'll do is we're going to
2753
01:46:41,760 --> 01:46:44,480
have a ternary expression in there
2754
01:46:44,480 --> 01:46:46,719
we're going to say if post that
2755
01:46:46,719 --> 01:46:48,239
underscore id
2756
01:46:48,239 --> 01:46:51,920
is equal to the action dot payload keep
2757
01:46:51,920 --> 01:46:54,080
in mind that action.payload is the
2758
01:46:54,080 --> 01:46:55,679
updated post
2759
01:46:55,679 --> 01:46:56,480
so
2760
01:46:56,480 --> 01:46:58,960
if the post id is equal to the action
2761
01:46:58,960 --> 01:47:01,440
that payload that underscore id
2762
01:47:01,440 --> 01:47:03,199
then we want to return the
2763
01:47:03,199 --> 01:47:05,040
action.payload
2764
01:47:05,040 --> 01:47:07,360
why because that action payload is the
2765
01:47:07,360 --> 01:47:10,960
newly updated post or memory otherwise
2766
01:47:10,960 --> 01:47:13,040
we want to return just the post as it
2767
01:47:13,040 --> 01:47:15,920
was without any updates that's it
2768
01:47:15,920 --> 01:47:18,000
great so what are the next steps the
2769
01:47:18,000 --> 01:47:20,320
next step would be to update the fields
2770
01:47:20,320 --> 01:47:22,960
of the form with the values of the post
2771
01:47:22,960 --> 01:47:24,800
we want to update of course when you
2772
01:47:24,800 --> 01:47:26,320
click this we don't want to start
2773
01:47:26,320 --> 01:47:28,400
everything from scratch we want to have
2774
01:47:28,400 --> 01:47:30,560
these values already in there and then
2775
01:47:30,560 --> 01:47:32,800
we simply want to update them so that's
2776
01:47:32,800 --> 01:47:34,719
our next step to do that we of course
2777
01:47:34,719 --> 01:47:37,280
have to go to the form and then in here
2778
01:47:37,280 --> 01:47:40,000
we have to fetch a new post you already
2779
01:47:40,000 --> 01:47:41,840
know how to fetch some data from redux
2780
01:47:41,840 --> 01:47:44,320
right we've been doing it so far inside
2781
01:47:44,320 --> 01:47:46,719
of posts if i'm not mistaken
2782
01:47:46,719 --> 01:47:48,639
inside of here we are fetching in the
2783
01:47:48,639 --> 01:47:52,480
post so let's copy that use selector and
2784
01:47:52,480 --> 01:47:54,400
let's also copy this line
2785
01:47:54,400 --> 01:47:58,560
we can move that over to the form
2786
01:47:58,560 --> 01:48:01,199
and inside of here let's first import
2787
01:48:01,199 --> 01:48:03,840
that use selector from react redux i'm
2788
01:48:03,840 --> 01:48:06,400
just going to place it inside of here
2789
01:48:06,400 --> 01:48:07,679
and also
2790
01:48:07,679 --> 01:48:10,239
now we're fetching all the posts but in
2791
01:48:10,239 --> 01:48:13,520
this case we don't want all the posts we
2792
01:48:13,520 --> 01:48:16,239
only want the data for the updated post
2793
01:48:16,239 --> 01:48:18,000
to do that in here we're not going to
2794
01:48:18,000 --> 01:48:20,159
return all the posts what we're going to
2795
01:48:20,159 --> 01:48:21,760
do is we're going to have another
2796
01:48:21,760 --> 01:48:24,159
ternary and that turner is going to say
2797
01:48:24,159 --> 01:48:26,800
if we have a current id so if it's not
2798
01:48:26,800 --> 01:48:27,600
null
2799
01:48:27,600 --> 01:48:30,560
then we want to loop over state that
2800
01:48:30,560 --> 01:48:33,119
posts and we want to call a find method
2801
01:48:33,119 --> 01:48:34,320
on them
2802
01:48:34,320 --> 01:48:36,960
more specifically we want to find a post
2803
01:48:36,960 --> 01:48:39,440
let's call it p in this case that has
2804
01:48:39,440 --> 01:48:43,360
the same id so p dot underscore id
2805
01:48:43,360 --> 01:48:47,280
as our current id just like that so this
2806
01:48:47,280 --> 01:48:49,280
is going to make sure that we find only
2807
01:48:49,280 --> 01:48:51,679
that specific post if we don't have the
2808
01:48:51,679 --> 01:48:54,159
current id though in that case we just
2809
01:48:54,159 --> 01:48:56,320
want to have null and now we're not
2810
01:48:56,320 --> 01:48:58,080
returning posts here it's simply going
2811
01:48:58,080 --> 01:48:59,760
to be post
2812
01:48:59,760 --> 01:49:02,000
because the find method is returning one
2813
01:49:02,000 --> 01:49:04,320
singular thing great
2814
01:49:04,320 --> 01:49:05,920
so now that we have the data for the
2815
01:49:05,920 --> 01:49:08,080
post we'll just have to do one single
2816
01:49:08,080 --> 01:49:10,560
thing which is going to be first import
2817
01:49:10,560 --> 01:49:11,760
use effect
2818
01:49:11,760 --> 01:49:14,000
and now use that use effect to populate
2819
01:49:14,000 --> 01:49:15,760
the values of the form
2820
01:49:15,760 --> 01:49:18,000
we can use the use effect like so we
2821
01:49:18,000 --> 01:49:20,639
just call it it accepts two parameters
2822
01:49:20,639 --> 01:49:23,119
first one is a callback function
2823
01:49:23,119 --> 01:49:24,800
and the second one is called a
2824
01:49:24,800 --> 01:49:28,080
dependency array in here we say when
2825
01:49:28,080 --> 01:49:29,679
this callback function should be ran
2826
01:49:29,679 --> 01:49:32,239
when what changes well when the post
2827
01:49:32,239 --> 01:49:34,800
value changes from nothing to to the
2828
01:49:34,800 --> 01:49:37,040
actual post then we want to run this
2829
01:49:37,040 --> 01:49:39,440
function and then in here we're going to
2830
01:49:39,440 --> 01:49:41,760
say if post exists
2831
01:49:41,760 --> 01:49:44,480
then we're going to set post data
2832
01:49:44,480 --> 01:49:46,400
and remember that is our state field
2833
01:49:46,400 --> 01:49:49,199
here set post data and we are simply
2834
01:49:49,199 --> 01:49:51,199
going to populate it with the data of
2835
01:49:51,199 --> 01:49:53,280
the post that's it
2836
01:49:53,280 --> 01:49:55,840
let's save it and see if it works okay
2837
01:49:55,840 --> 01:49:58,400
we are back in here let me zoom this in
2838
01:49:58,400 --> 01:50:00,719
and i'm gonna click this button
2839
01:50:00,719 --> 01:50:03,199
there we go the values are populated and
2840
01:50:03,199 --> 01:50:04,880
most of the work is done
2841
01:50:04,880 --> 01:50:07,119
and let's change some things so first of
2842
01:50:07,119 --> 01:50:09,280
all i'm gonna type javascript mastery
2843
01:50:09,280 --> 01:50:11,119
instead of js mastery
2844
01:50:11,119 --> 01:50:13,199
let's change this to new year's eve
2845
01:50:13,199 --> 01:50:15,599
2020. let's hope it's going to be good
2846
01:50:15,599 --> 01:50:18,159
and now i'm going to add a new tag here
2847
01:50:18,159 --> 01:50:20,560
which is going to be 2020. now i'm going
2848
01:50:20,560 --> 01:50:22,080
to click submit
2849
01:50:22,080 --> 01:50:24,239
looks like nothing happened right but
2850
01:50:24,239 --> 01:50:26,239
let me refresh the page
2851
01:50:26,239 --> 01:50:28,719
as soon as i refresh the changes are
2852
01:50:28,719 --> 01:50:31,360
here that's good so the only thing we
2853
01:50:31,360 --> 01:50:33,119
have to do is we have to initiate
2854
01:50:33,119 --> 01:50:36,159
another get posts request after we
2855
01:50:36,159 --> 01:50:38,960
update the post well let's first
2856
01:50:38,960 --> 01:50:40,800
implement a few different things and one
2857
01:50:40,800 --> 01:50:42,719
of these things is gonna fix that get
2858
01:50:42,719 --> 01:50:44,320
posts immediately
2859
01:50:44,320 --> 01:50:46,719
first of all once we're updating let's
2860
01:50:46,719 --> 01:50:48,960
change this to editing a memory instead
2861
01:50:48,960 --> 01:50:50,159
of creating
2862
01:50:50,159 --> 01:50:52,239
that's gonna be as simple as going in
2863
01:50:52,239 --> 01:50:55,119
here and then having the code in here
2864
01:50:55,119 --> 01:50:57,520
and the only thing we'll be doing is
2865
01:50:57,520 --> 01:51:00,639
using a ternary so if the current id
2866
01:51:00,639 --> 01:51:03,199
exists then we want to return a string
2867
01:51:03,199 --> 01:51:05,199
of editing so in here we're going to
2868
01:51:05,199 --> 01:51:06,880
have editing
2869
01:51:06,880 --> 01:51:09,199
and if current id doesn't exist then
2870
01:51:09,199 --> 01:51:11,760
we're going to have a string of creating
2871
01:51:11,760 --> 01:51:14,080
that's it as simple as that and we don't
2872
01:51:14,080 --> 01:51:16,880
need this dollar sign great let's save
2873
01:51:16,880 --> 01:51:18,960
it and see if that works
2874
01:51:18,960 --> 01:51:21,199
and now we have creating a memory if i
2875
01:51:21,199 --> 01:51:23,119
click this button we have editing a
2876
01:51:23,119 --> 01:51:25,520
memory great the thing will the thing
2877
01:51:25,520 --> 01:51:27,840
that we want to do next is once we click
2878
01:51:27,840 --> 01:51:30,159
submit we want to clear all the inputs
2879
01:51:30,159 --> 01:51:32,000
because that memory is edited and we
2880
01:51:32,000 --> 01:51:34,000
want to keep moving forwards and we want
2881
01:51:34,000 --> 01:51:36,800
to have this back to creating so to do
2882
01:51:36,800 --> 01:51:38,960
that we have to implement this clear
2883
01:51:38,960 --> 01:51:40,560
function we talked about
2884
01:51:40,560 --> 01:51:42,320
inside of the clear first of all we're
2885
01:51:42,320 --> 01:51:44,719
going to set the current id
2886
01:51:44,719 --> 01:51:46,639
to be equal to null so we're going to
2887
01:51:46,639 --> 01:51:48,960
reset it back to null and then also what
2888
01:51:48,960 --> 01:51:50,880
we have to do is take all the default
2889
01:51:50,880 --> 01:51:52,560
values right here we have them in the
2890
01:51:52,560 --> 01:51:53,440
state
2891
01:51:53,440 --> 01:51:56,000
and simply update the state again that's
2892
01:51:56,000 --> 01:51:58,800
going to be set post data and we're
2893
01:51:58,800 --> 01:52:00,719
going to set it to everything at an
2894
01:52:00,719 --> 01:52:02,480
empty string great
2895
01:52:02,480 --> 01:52:05,440
if i now save this that should work
2896
01:52:05,440 --> 01:52:07,840
going back refreshing and now let's try
2897
01:52:07,840 --> 01:52:10,480
to edit and let's just try sending
2898
01:52:10,480 --> 01:52:12,400
javascript mastery one
2899
01:52:12,400 --> 01:52:13,520
submit
2900
01:52:13,520 --> 01:52:16,800
and that didn't do it clear function is
2901
01:52:16,800 --> 01:52:19,840
implemented and if we start editing and
2902
01:52:19,840 --> 01:52:22,400
i click clear that works
2903
01:52:22,400 --> 01:52:25,040
but we also want to call clear function
2904
01:52:25,040 --> 01:52:27,119
once we edit or once we create a new
2905
01:52:27,119 --> 01:52:29,040
form so that's going to be clear
2906
01:52:29,040 --> 01:52:31,760
right there or even better since we'll
2907
01:52:31,760 --> 01:52:34,000
be using clear at both times no matter
2908
01:52:34,000 --> 01:52:36,000
the if or the else we're gonna call it
2909
01:52:36,000 --> 01:52:38,560
at the end right here i'll save it and
2910
01:52:38,560 --> 01:52:41,040
go back and see if it works okay i'm
2911
01:52:41,040 --> 01:52:42,880
gonna change this to javascript mastery
2912
01:52:42,880 --> 01:52:46,000
2 this time and let's click submit as
2913
01:52:46,000 --> 01:52:48,320
you can see this returned to normal and
2914
01:52:48,320 --> 01:52:50,400
in here if i refresh the post is going
2915
01:52:50,400 --> 01:52:52,639
to be changed well remember how i told
2916
01:52:52,639 --> 01:52:54,800
you that emptying these values are going
2917
01:52:54,800 --> 01:52:57,920
to actually immediately reset our posts
2918
01:52:57,920 --> 01:52:59,360
so that we don't have to refresh the
2919
01:52:59,360 --> 01:53:00,320
page
2920
01:53:00,320 --> 01:53:02,080
well for that we just have to do one
2921
01:53:02,080 --> 01:53:03,440
more step
2922
01:53:03,440 --> 01:53:06,400
and that step is going into the app.js
2923
01:53:06,400 --> 01:53:09,040
and then changing this use effect from
2924
01:53:09,040 --> 01:53:11,360
only having dispatch in here to having
2925
01:53:11,360 --> 01:53:13,119
the current id
2926
01:53:13,119 --> 01:53:15,280
why is that going to work well
2927
01:53:15,280 --> 01:53:17,760
considering that we are now clearing the
2928
01:53:17,760 --> 01:53:20,000
input and cleaning the input means
2929
01:53:20,000 --> 01:53:22,000
changing the current id
2930
01:53:22,000 --> 01:53:24,000
as soon as we change the current id in
2931
01:53:24,000 --> 01:53:26,159
the app the app is going to actually
2932
01:53:26,159 --> 01:53:28,880
dispatch to get post action and that is
2933
01:53:28,880 --> 01:53:31,040
going to make sure that every change we
2934
01:53:31,040 --> 01:53:33,679
are going to get new posts let's save it
2935
01:53:33,679 --> 01:53:35,360
and see if it works
2936
01:53:35,360 --> 01:53:37,360
i'm going to click the edit button and
2937
01:53:37,360 --> 01:53:39,280
change this back to javascript mastery
2938
01:53:39,280 --> 01:53:40,880
without any number
2939
01:53:40,880 --> 01:53:42,239
click submit
2940
01:53:42,239 --> 01:53:44,719
and it looks like it doesn't work well
2941
01:53:44,719 --> 01:53:46,639
there could be a few reasons i just
2942
01:53:46,639 --> 01:53:48,880
spent some time debugging this and i
2943
01:53:48,880 --> 01:53:52,239
found out that inside of our controllers
2944
01:53:52,239 --> 01:53:54,719
we are using mongoose here but we never
2945
01:53:54,719 --> 01:53:57,760
actually imported it so go ahead and go
2946
01:53:57,760 --> 01:53:59,840
inside of the controllers inside of the
2947
01:53:59,840 --> 01:54:02,080
post and make sure to have this line
2948
01:54:02,080 --> 01:54:05,280
that's gonna be import mongoose from
2949
01:54:05,280 --> 01:54:07,119
mongoose
2950
01:54:07,119 --> 01:54:10,080
great but not only that we had one small
2951
01:54:10,080 --> 01:54:12,000
mistake in this file as well
2952
01:54:12,000 --> 01:54:14,080
if you go right there you can see that
2953
01:54:14,080 --> 01:54:16,560
we are updating the post where we are
2954
01:54:16,560 --> 01:54:18,639
passing all the data we are receiving
2955
01:54:18,639 --> 01:54:20,560
from the front end right there as the
2956
01:54:20,560 --> 01:54:23,599
post well what is a post a post is an
2957
01:54:23,599 --> 01:54:25,920
object that includes a title maybe a
2958
01:54:25,920 --> 01:54:26,800
name
2959
01:54:26,800 --> 01:54:28,639
a message
2960
01:54:28,639 --> 01:54:30,880
tags and everything else represented but
2961
01:54:30,880 --> 01:54:33,679
we forgot to send over the id as well
2962
01:54:33,679 --> 01:54:35,920
because in the database each post has to
2963
01:54:35,920 --> 01:54:37,440
have its own id
2964
01:54:37,440 --> 01:54:39,520
so what we have to do is we have to
2965
01:54:39,520 --> 01:54:42,320
create a new object just like so
2966
01:54:42,320 --> 01:54:44,480
spread all of these properties from an
2967
01:54:44,480 --> 01:54:46,080
object that we are receiving from the
2968
01:54:46,080 --> 01:54:48,800
front end and then also pass in that
2969
01:54:48,800 --> 01:54:51,119
underscore id now this is going to be
2970
01:54:51,119 --> 01:54:53,920
our newly updated post and in here
2971
01:54:53,920 --> 01:54:56,000
instead of the post we can have that
2972
01:54:56,000 --> 01:54:58,239
post with all the things but only now we
2973
01:54:58,239 --> 01:55:01,040
have the underscore id as well now let's
2974
01:55:01,040 --> 01:55:04,159
try to save it and see if that works one
2975
01:55:04,159 --> 01:55:06,560
more tip is that you don't have to
2976
01:55:06,560 --> 01:55:08,400
refresh the browser once you make
2977
01:55:08,400 --> 01:55:10,800
changes on the back end if only changes
2978
01:55:10,800 --> 01:55:12,560
you made were on the backend you can
2979
01:55:12,560 --> 01:55:14,480
just initiate a new request and don't
2980
01:55:14,480 --> 01:55:17,040
have to reverse the front end so in here
2981
01:55:17,040 --> 01:55:18,719
let's say we go a bit into the future
2982
01:55:18,719 --> 01:55:21,920
and let's say new year's eve 2025. i'm
2983
01:55:21,920 --> 01:55:24,400
gonna click submit and let's see this
2984
01:55:24,400 --> 01:55:26,239
cleared out but i don't see that the
2985
01:55:26,239 --> 01:55:28,639
year changed here i just noticed we're
2986
01:55:28,639 --> 01:55:30,880
missing the title altogether here so
2987
01:55:30,880 --> 01:55:33,360
let's fix that we can go to the post and
2988
01:55:33,360 --> 01:55:34,960
then see where the title should have
2989
01:55:34,960 --> 01:55:35,840
been
2990
01:55:35,840 --> 01:55:37,840
and we have the creator here we have the
2991
01:55:37,840 --> 01:55:40,159
time and then the title should have been
2992
01:55:40,159 --> 01:55:42,239
somewhere around here
2993
01:55:42,239 --> 01:55:44,800
there we go class is that title and then
2994
01:55:44,800 --> 01:55:46,960
that's post that message but we never
2995
01:55:46,960 --> 01:55:49,679
have pause that title right so to fix
2996
01:55:49,679 --> 01:55:51,119
that we're going to add another
2997
01:55:51,119 --> 01:55:54,000
typography just below the tags we can
2998
01:55:54,000 --> 01:55:55,840
even copy this one and then bring it
2999
01:55:55,840 --> 01:55:57,840
above the cart content
3000
01:55:57,840 --> 01:55:59,520
inside of here this one is going to have
3001
01:55:59,520 --> 01:56:02,400
a class name of classes.title and we can
3002
01:56:02,400 --> 01:56:04,480
leave everything as it is but this time
3003
01:56:04,480 --> 01:56:07,119
in here is going to be post.title
3004
01:56:07,119 --> 01:56:09,440
and this one since it is a message it's
3005
01:56:09,440 --> 01:56:13,119
not going to have a classes.title class
3006
01:56:13,119 --> 01:56:14,800
great let's save this and see if it
3007
01:56:14,800 --> 01:56:16,000
works
3008
01:56:16,000 --> 01:56:18,480
as you can see we have the title now the
3009
01:56:18,480 --> 01:56:20,239
last thing is to check if our update
3010
01:56:20,239 --> 01:56:22,880
works let's change absolutely everything
3011
01:56:22,880 --> 01:56:25,360
so it's going to be js mastery now let's
3012
01:56:25,360 --> 01:56:27,520
go to year 2030
3013
01:56:27,520 --> 01:56:29,599
let's add a second exclamation mark and
3014
01:56:29,599 --> 01:56:32,080
in here i'm gonna change the tag to 2030
3015
01:56:32,080 --> 01:56:35,119
again click submit
3016
01:56:35,119 --> 01:56:37,520
and i spent about 20 minutes debugging
3017
01:56:37,520 --> 01:56:39,760
it and turns out it was just a small
3018
01:56:39,760 --> 01:56:42,480
typo let's go step by step
3019
01:56:42,480 --> 01:56:45,440
first of all i noticed that we are using
3020
01:56:45,440 --> 01:56:48,080
mongoose here but we forgot to import it
3021
01:56:48,080 --> 01:56:50,320
at the top so let's do that
3022
01:56:50,320 --> 01:56:52,800
import
3023
01:56:53,119 --> 01:56:56,719
mongoose from mongoose and i want to
3024
01:56:56,719 --> 01:56:58,800
guide you to the process of how i was
3025
01:56:58,800 --> 01:57:00,480
trying to discover the error i just want
3026
01:57:00,480 --> 01:57:02,320
to skip through the video or cut it out
3027
01:57:02,320 --> 01:57:04,719
i want to show you that errors are
3028
01:57:04,719 --> 01:57:06,719
normal and that we need to be able to
3029
01:57:06,719 --> 01:57:09,119
fix them and let me show you that last
3030
01:57:09,119 --> 01:57:11,199
error that we had or at least i had but
3031
01:57:11,199 --> 01:57:13,920
it happens so if you do this and then if
3032
01:57:13,920 --> 01:57:16,320
you change the title or the creator you
3033
01:57:16,320 --> 01:57:19,840
can click submit and open the console
3034
01:57:19,840 --> 01:57:21,760
after you open the console you get a
3035
01:57:21,760 --> 01:57:25,119
message from js line 30 saying cannot
3036
01:57:25,119 --> 01:57:28,080
read property underscore id of undefined
3037
01:57:28,080 --> 01:57:30,000
if you click that you're going to notice
3038
01:57:30,000 --> 01:57:32,719
that that's coming from actions so now
3039
01:57:32,719 --> 01:57:35,040
if you go to actions you can see that
3040
01:57:35,040 --> 01:57:38,320
line 30 is right here air that message
3041
01:57:38,320 --> 01:57:40,800
and that's coming after we make this api
3042
01:57:40,800 --> 01:57:43,199
request which could make you assume that
3043
01:57:43,199 --> 01:57:44,800
the error is actually coming from the
3044
01:57:44,800 --> 01:57:47,360
back end but it is not and here's a
3045
01:57:47,360 --> 01:57:49,920
great tip for you never console log
3046
01:57:49,920 --> 01:57:52,080
air.message because then you're losing
3047
01:57:52,080 --> 01:57:53,440
the information
3048
01:57:53,440 --> 01:57:55,360
just console log the error and that's
3049
01:57:55,360 --> 01:57:57,760
going to give you more info so now let's
3050
01:57:57,760 --> 01:57:59,440
do one more request to see what the
3051
01:57:59,440 --> 01:58:01,440
error is going to give us
3052
01:58:01,440 --> 01:58:04,480
if we go here try to edit the post and
3053
01:58:04,480 --> 01:58:06,239
then finally change this to javascript
3054
01:58:06,239 --> 01:58:09,440
mastery 2 click submit and you should be
3055
01:58:09,440 --> 01:58:11,920
able to see cannot read property id of
3056
01:58:11,920 --> 01:58:15,840
undefined but now we get where add posts
3057
01:58:15,840 --> 01:58:19,440
js4 if you click that it actually turns
3058
01:58:19,440 --> 01:58:21,840
out to be the reducer so let's go to the
3059
01:58:21,840 --> 01:58:23,520
reducers
3060
01:58:23,520 --> 01:58:25,440
inside of the reducers that's going to
3061
01:58:25,440 --> 01:58:27,679
be line four
3062
01:58:27,679 --> 01:58:30,639
and take a look cannot read property id
3063
01:58:30,639 --> 01:58:33,040
or underscore id from
3064
01:58:33,040 --> 01:58:35,280
action.paylad
3065
01:58:35,280 --> 01:58:36,320
what
3066
01:58:36,320 --> 01:58:38,560
whole time the mistake was that there is
3067
01:58:38,560 --> 01:58:41,119
a typo so now i'm going to change this
3068
01:58:41,119 --> 01:58:44,239
to payload just as it's supposed to be
3069
01:58:44,239 --> 01:58:46,400
and i want to tell you that this happens
3070
01:58:46,400 --> 01:58:48,159
it's completely normal so once you're
3071
01:58:48,159 --> 01:58:49,760
going through the process
3072
01:58:49,760 --> 01:58:51,360
typos are going to happen errors are
3073
01:58:51,360 --> 01:58:53,040
going to happen but you need to be able
3074
01:58:53,040 --> 01:58:55,599
to solve them step by step by using cons
3075
01:58:55,599 --> 01:58:57,840
of logs or any other method as a matter
3076
01:58:57,840 --> 01:58:59,760
of fact now let's hope that one more
3077
01:58:59,760 --> 01:59:01,679
request for our update is going to be
3078
01:59:01,679 --> 01:59:03,199
our final test
3079
01:59:03,199 --> 01:59:05,199
if we now go ahead here let's try to
3080
01:59:05,199 --> 01:59:08,639
change everything js mastery let's do
3081
01:59:08,639 --> 01:59:11,679
new year's eve 2030
3082
01:59:11,679 --> 01:59:13,920
two exclamation marks and let's change
3083
01:59:13,920 --> 01:59:17,360
the tag to 2030. i'm gonna click submit
3084
01:59:17,360 --> 01:59:18,719
and let's see
3085
01:59:18,719 --> 01:59:20,960
there we go all of the data was changed
3086
01:59:20,960 --> 01:59:22,639
and that's it
3087
01:59:22,639 --> 01:59:24,880
the message here is we need to be able
3088
01:59:24,880 --> 01:59:27,840
to solve the errors and especially typos
3089
01:59:27,840 --> 01:59:30,480
because they do happen next thing that
3090
01:59:30,480 --> 01:59:32,400
we're going to implement is going to be
3091
01:59:32,400 --> 01:59:33,679
the delete
3092
01:59:33,679 --> 01:59:36,000
so right now if you click delete nothing
3093
01:59:36,000 --> 01:59:38,400
happens let's see why is that
3094
01:59:38,400 --> 01:59:40,800
well if you go into a post right there
3095
01:59:40,800 --> 01:59:42,480
and if you collapse this terminal so we
3096
01:59:42,480 --> 01:59:44,800
can see better you can see delete
3097
01:59:44,800 --> 01:59:47,760
currently has no on click nothing is
3098
01:59:47,760 --> 01:59:50,000
happening that means that we first have
3099
01:59:50,000 --> 01:59:52,000
to implement the backend logic and then
3100
01:59:52,000 --> 01:59:53,599
we're going to connect that to the front
3101
01:59:53,599 --> 01:59:56,400
end so if we collapse all of this
3102
01:59:56,400 --> 01:59:58,880
in the server first of all as always
3103
01:59:58,880 --> 02:00:01,040
we're going to create a new route
3104
02:00:01,040 --> 02:00:03,040
the route is going to look like this
3105
02:00:03,040 --> 02:00:05,840
router dot delete
3106
02:00:05,840 --> 02:00:08,719
great and now we also need to have
3107
02:00:08,719 --> 02:00:11,760
access to id same as with the update
3108
02:00:11,760 --> 02:00:13,760
because we need to know which post are
3109
02:00:13,760 --> 02:00:16,080
we deleting and then we're going to call
3110
02:00:16,080 --> 02:00:17,440
a function
3111
02:00:17,440 --> 02:00:19,360
called delete
3112
02:00:19,360 --> 02:00:21,280
post just like that
3113
02:00:21,280 --> 02:00:22,800
now we're going to import it from the
3114
02:00:22,800 --> 02:00:25,679
controllers delete post and then we can
3115
02:00:25,679 --> 02:00:27,520
go to controllers to actually create
3116
02:00:27,520 --> 02:00:29,119
that function
3117
02:00:29,119 --> 02:00:30,560
inside of here
3118
02:00:30,560 --> 02:00:33,840
export const delete post
3119
02:00:33,840 --> 02:00:35,599
it's going to be equal to an async
3120
02:00:35,599 --> 02:00:38,239
function that has request and response
3121
02:00:38,239 --> 02:00:40,000
as parameters
3122
02:00:40,000 --> 02:00:41,920
and then in there we also need to get
3123
02:00:41,920 --> 02:00:44,159
this id so let's do that
3124
02:00:44,159 --> 02:00:47,520
const we get the id from request that
3125
02:00:47,520 --> 02:00:49,920
params that's good
3126
02:00:49,920 --> 02:00:51,760
we're going to copy this line because we
3127
02:00:51,760 --> 02:00:54,000
need to make sure that the id is valid
3128
02:00:54,000 --> 02:00:56,000
so i'm going to paste that there and
3129
02:00:56,000 --> 02:00:57,920
finally we have to implement the logic
3130
02:00:57,920 --> 02:01:00,320
to delete a to do and that's going to
3131
02:01:00,320 --> 02:01:03,840
look like this await post message
3132
02:01:03,840 --> 02:01:06,560
dot find by id
3133
02:01:06,560 --> 02:01:09,760
and remove and then in there we can pass
3134
02:01:09,760 --> 02:01:12,639
in that id we are receiving from the
3135
02:01:12,639 --> 02:01:13,760
params
3136
02:01:13,760 --> 02:01:17,040
finally we can return rest.json
3137
02:01:17,040 --> 02:01:19,360
where the message is going to be equal
3138
02:01:19,360 --> 02:01:20,080
to
3139
02:01:20,080 --> 02:01:24,400
post deleted successfully
3140
02:01:24,719 --> 02:01:27,119
now that we have that route we can go to
3141
02:01:27,119 --> 02:01:29,599
the front end and actually initiate it
3142
02:01:29,599 --> 02:01:31,360
first of all we're going to go to the
3143
02:01:31,360 --> 02:01:33,840
api and then in there we're going to
3144
02:01:33,840 --> 02:01:36,880
implement that api call that's going to
3145
02:01:36,880 --> 02:01:40,880
be export const delete post
3146
02:01:40,880 --> 02:01:43,760
in there we receive only id this time we
3147
02:01:43,760 --> 02:01:45,520
don't have the updated post because we
3148
02:01:45,520 --> 02:01:47,360
just delete it and then we're going to
3149
02:01:47,360 --> 02:01:50,000
call axios.delete
3150
02:01:50,000 --> 02:01:54,080
we pass in the url and forward slash
3151
02:01:54,080 --> 02:01:56,400
we also pass in the id
3152
02:01:56,400 --> 02:01:58,960
same thing we did in here with the axios
3153
02:01:58,960 --> 02:02:00,239
patch
3154
02:02:00,239 --> 02:02:02,080
great now we know that we can go into
3155
02:02:02,080 --> 02:02:04,159
actions and there we can create our
3156
02:02:04,159 --> 02:02:05,840
action creator
3157
02:02:05,840 --> 02:02:08,159
inside of here that's going to be export
3158
02:02:08,159 --> 02:02:11,040
const delete post
3159
02:02:11,040 --> 02:02:12,880
we take in the id
3160
02:02:12,880 --> 02:02:17,199
and then we have the async dispatch
3161
02:02:17,199 --> 02:02:20,159
and inside of there we have a try and
3162
02:02:20,159 --> 02:02:22,560
catch block
3163
02:02:22,560 --> 02:02:25,599
since we don't need to pass in any data
3164
02:02:25,599 --> 02:02:27,599
inside of here we're gonna await that
3165
02:02:27,599 --> 02:02:31,040
api call that's gonna be api dot delete
3166
02:02:31,040 --> 02:02:34,320
post and we pass in the id
3167
02:02:34,320 --> 02:02:36,480
since we don't need the response of this
3168
02:02:36,480 --> 02:02:39,440
we can simply say this patch
3169
02:02:39,440 --> 02:02:41,360
and then pass in the type
3170
02:02:41,360 --> 02:02:45,480
that's going to say delete
3171
02:02:46,080 --> 02:02:49,119
the payload can be the id we want to
3172
02:02:49,119 --> 02:02:51,040
delete we don't need to do something
3173
02:02:51,040 --> 02:02:53,280
like this const response is equal to
3174
02:02:53,280 --> 02:02:55,280
this because we are not interested in
3175
02:02:55,280 --> 02:02:57,599
the return data we only want to delete
3176
02:02:57,599 --> 02:02:59,199
it and that's it
3177
02:02:59,199 --> 02:03:00,719
and inside of here we're going to
3178
02:03:00,719 --> 02:03:03,840
console log the error
3179
02:03:03,840 --> 02:03:05,920
now one more time where do we go after
3180
02:03:05,920 --> 02:03:08,000
we create an action well we go to
3181
02:03:08,000 --> 02:03:10,800
reducers so in here we're passing over
3182
02:03:10,800 --> 02:03:12,800
the id and that's going to be under the
3183
02:03:12,800 --> 02:03:15,199
type of delete
3184
02:03:15,199 --> 02:03:17,440
so inside of our posts we have to
3185
02:03:17,440 --> 02:03:20,239
implement that delete right here
3186
02:03:20,239 --> 02:03:22,080
and now what are we going to do when we
3187
02:03:22,080 --> 02:03:23,440
delete something
3188
02:03:23,440 --> 02:03:25,360
well it's going to be really similar to
3189
02:03:25,360 --> 02:03:28,480
the update but just a bit simpler we're
3190
02:03:28,480 --> 02:03:30,080
going to return
3191
02:03:30,080 --> 02:03:32,560
posts dot filter
3192
02:03:32,560 --> 02:03:34,960
and in here we have a post and we're
3193
02:03:34,960 --> 02:03:37,040
going to basically return all the posts
3194
02:03:37,040 --> 02:03:38,960
but we're going to filter out the one
3195
02:03:38,960 --> 02:03:40,800
that we deleted and that's going to look
3196
02:03:40,800 --> 02:03:44,800
like this if the post that underscore id
3197
02:03:44,800 --> 02:03:47,840
is not equal to the action.payload
3198
02:03:47,840 --> 02:03:50,639
in that case we're going to remove it so
3199
02:03:50,639 --> 02:03:53,040
we're going to keep all the posts except
3200
02:03:53,040 --> 02:03:56,119
the one where the id is equal to the
3201
02:03:56,119 --> 02:03:57,679
action.payload
3202
02:03:57,679 --> 02:03:59,920
now that our reducer is done we can go
3203
02:03:59,920 --> 02:04:01,679
inside of the post
3204
02:04:01,679 --> 02:04:04,480
and finally dispatch that action where
3205
02:04:04,480 --> 02:04:07,119
do we do that and how do we do that well
3206
02:04:07,119 --> 02:04:09,199
first of all we have to import that
3207
02:04:09,199 --> 02:04:11,199
action and since we don't have the
3208
02:04:11,199 --> 02:04:13,599
reducer setup in here yet we have to do
3209
02:04:13,599 --> 02:04:17,599
that as well we can do that by importing
3210
02:04:17,599 --> 02:04:19,679
use dispatch
3211
02:04:19,679 --> 02:04:23,840
as we always do from react redux
3212
02:04:23,840 --> 02:04:25,520
then in here we're going to initialize
3213
02:04:25,520 --> 02:04:28,239
the dispatch to be equal to that hook
3214
02:04:28,239 --> 02:04:30,159
use dispatch
3215
02:04:30,159 --> 02:04:33,119
and we also have to import actions so
3216
02:04:33,119 --> 02:04:36,560
inside of here we're going to import
3217
02:04:36,560 --> 02:04:39,679
delete post which we just created from
3218
02:04:39,679 --> 02:04:42,239
that dot slash dot dot slash
3219
02:04:42,239 --> 02:04:46,480
dot slash actions slash posts
3220
02:04:46,480 --> 02:04:50,400
and finally moving down we can dispatch
3221
02:04:50,400 --> 02:04:52,159
delete
3222
02:04:52,159 --> 02:04:53,360
post
3223
02:04:53,360 --> 02:04:56,079
in here we need to pass over the post
3224
02:04:56,079 --> 02:04:58,719
dot underscore id
3225
02:04:58,719 --> 02:05:01,199
that's it now we did the whole circle of
3226
02:05:01,199 --> 02:05:03,199
first doing the back end connecting it
3227
02:05:03,199 --> 02:05:06,159
to component creating actions api calls
3228
02:05:06,159 --> 02:05:08,800
and reducers for the delete request
3229
02:05:08,800 --> 02:05:11,119
great let's see if it works
3230
02:05:11,119 --> 02:05:12,880
before we actually delete something
3231
02:05:12,880 --> 02:05:16,239
let's add a new card let's say that john
3232
02:05:16,239 --> 02:05:18,400
went skydiving so we're going to put
3233
02:05:18,400 --> 02:05:21,360
skydiving there and he's gonna say it
3234
02:05:21,360 --> 02:05:24,639
was awesome great and in here we can do
3235
02:05:24,639 --> 02:05:27,920
some tags so let's do skydiving
3236
02:05:27,920 --> 02:05:30,480
i downloaded the image of man skydiving
3237
02:05:30,480 --> 02:05:32,560
and we're gonna submit it and let's do
3238
02:05:32,560 --> 02:05:34,960
that great as you can see john is
3239
02:05:34,960 --> 02:05:37,599
skydiving and it is awesome now let's
3240
02:05:37,599 --> 02:05:40,960
try to delete the initial post
3241
02:05:40,960 --> 02:05:43,360
as you can see nothing happened so let's
3242
02:05:43,360 --> 02:05:45,599
start debugging it first of all we're
3243
02:05:45,599 --> 02:05:47,840
going to go into the controllers part of
3244
02:05:47,840 --> 02:05:50,159
our server and then in there i want to
3245
02:05:50,159 --> 02:05:52,239
see if the delete request was indeed
3246
02:05:52,239 --> 02:05:54,639
made so inside of here i'm going to
3247
02:05:54,639 --> 02:05:57,280
console.log
3248
02:05:57,360 --> 02:05:59,360
delete just so we know that we reach
3249
02:05:59,360 --> 02:06:02,400
this point in our code great so if i now
3250
02:06:02,400 --> 02:06:04,960
do that and then try sending another
3251
02:06:04,960 --> 02:06:06,960
request from the front end
3252
02:06:06,960 --> 02:06:08,880
let's see what happens
3253
02:06:08,880 --> 02:06:11,440
we do get an error here and the error
3254
02:06:11,440 --> 02:06:13,440
says
3255
02:06:13,440 --> 02:06:15,360
unhandled promise rejection warning
3256
02:06:15,360 --> 02:06:18,079
reference error underscore id is not
3257
02:06:18,079 --> 02:06:22,480
defined at delete post line 41 that
3258
02:06:22,480 --> 02:06:24,480
means that in here we don't have the
3259
02:06:24,480 --> 02:06:26,239
underscore id
3260
02:06:26,239 --> 02:06:28,480
and yeah that was just a small typo in
3261
02:06:28,480 --> 02:06:30,639
here i copy this line but it should have
3262
02:06:30,639 --> 02:06:32,719
been just the id so we're going to leave
3263
02:06:32,719 --> 02:06:34,079
the id there
3264
02:06:34,079 --> 02:06:36,000
save the page and then go back to the
3265
02:06:36,000 --> 02:06:37,599
browser
3266
02:06:37,599 --> 02:06:40,239
if we go here and now press delete let's
3267
02:06:40,239 --> 02:06:41,840
see what happens
3268
02:06:41,840 --> 02:06:44,880
the post was successfully deleted that's
3269
02:06:44,880 --> 02:06:47,920
it amazing now let's add the ability to
3270
02:06:47,920 --> 02:06:49,840
like the post
3271
02:06:49,840 --> 02:06:52,239
to implement the liking functionality we
3272
02:06:52,239 --> 02:06:54,480
have to do one full circle and that's
3273
02:06:54,480 --> 02:06:56,239
something i want to point out
3274
02:06:56,239 --> 02:06:58,000
is that once you have the proper
3275
02:06:58,000 --> 02:07:00,560
structure set up using redux controllers
3276
02:07:00,560 --> 02:07:03,440
route that's extremely scalable it's
3277
02:07:03,440 --> 02:07:05,760
easy to keep adding new things onto it
3278
02:07:05,760 --> 02:07:07,760
just by following structure
3279
02:07:07,760 --> 02:07:08,560
so
3280
02:07:08,560 --> 02:07:11,040
now what we're going to do is as we
3281
02:07:11,040 --> 02:07:13,920
always do follow the structure first we
3282
02:07:13,920 --> 02:07:16,560
are going to add a router that patch
3283
02:07:16,560 --> 02:07:19,119
request and it's going to be to forward
3284
02:07:19,119 --> 02:07:22,560
slash id and forward slash like post
3285
02:07:22,560 --> 02:07:24,159
just so we know that this is the route
3286
02:07:24,159 --> 02:07:26,719
for liking why is it a patch request if
3287
02:07:26,719 --> 02:07:28,480
we said that patch request is used for
3288
02:07:28,480 --> 02:07:30,639
updating well if you think about it
3289
02:07:30,639 --> 02:07:33,040
liking something actually is updating it
3290
02:07:33,040 --> 02:07:34,719
it's updating the number of likes that
3291
02:07:34,719 --> 02:07:36,320
the post has
3292
02:07:36,320 --> 02:07:38,800
so moving forwards we add the function
3293
02:07:38,800 --> 02:07:41,520
that's going to be like post and we have
3294
02:07:41,520 --> 02:07:44,639
to import it from controllers like post
3295
02:07:44,639 --> 02:07:45,840
here
3296
02:07:45,840 --> 02:07:48,239
then we move over to controllers and
3297
02:07:48,239 --> 02:07:50,960
then in here we can create that function
3298
02:07:50,960 --> 02:07:54,560
export const like post is equal to an
3299
02:07:54,560 --> 02:07:56,400
async function where we have the request
3300
02:07:56,400 --> 02:07:57,760
and the response
3301
02:07:57,760 --> 02:08:00,000
and in here we also have to get the id
3302
02:08:00,000 --> 02:08:01,599
so we can do the same thing we're
3303
02:08:01,599 --> 02:08:03,599
already used to
3304
02:08:03,599 --> 02:08:06,159
great once we get that we also have to
3305
02:08:06,159 --> 02:08:07,679
check whether this is valid so we're
3306
02:08:07,679 --> 02:08:09,199
going to use this
3307
02:08:09,199 --> 02:08:11,520
now in this case we first have to find
3308
02:08:11,520 --> 02:08:13,440
the post we're looking for and that's
3309
02:08:13,440 --> 02:08:15,920
going to look like this const post is
3310
02:08:15,920 --> 02:08:18,239
equal to a weight
3311
02:08:18,239 --> 02:08:20,000
post message
3312
02:08:20,000 --> 02:08:23,599
dot find by id and then in there we
3313
02:08:23,599 --> 02:08:25,920
specify the id this is going to return
3314
02:08:25,920 --> 02:08:27,360
us a post
3315
02:08:27,360 --> 02:08:30,400
and now we have the updated post whereas
3316
02:08:30,400 --> 02:08:33,920
const updated post is going to be equal
3317
02:08:33,920 --> 02:08:35,679
to a weight
3318
02:08:35,679 --> 02:08:37,599
post message
3319
02:08:37,599 --> 02:08:41,920
dot find by id and update where we pass
3320
02:08:41,920 --> 02:08:43,760
in the id
3321
02:08:43,760 --> 02:08:46,000
and now this is the crucial part as the
3322
02:08:46,000 --> 02:08:48,560
second parameter we want to pass in our
3323
02:08:48,560 --> 02:08:51,440
updates so that's going to be an object
3324
02:08:51,440 --> 02:08:53,840
in there we want to increment the like
3325
02:08:53,840 --> 02:08:56,239
count so we're going to set like count
3326
02:08:56,239 --> 02:09:00,159
to be equal to post dot like count this
3327
02:09:00,159 --> 02:09:02,000
post is the post we're fetching right
3328
02:09:02,000 --> 02:09:04,639
here and then what we simply do is
3329
02:09:04,639 --> 02:09:07,040
increment by one
3330
02:09:07,040 --> 02:09:09,040
and then with update requests we have to
3331
02:09:09,040 --> 02:09:11,199
specify the third parameter where we say
3332
02:09:11,199 --> 02:09:14,239
that new is equal to true
3333
02:09:14,239 --> 02:09:16,480
this is inside of an object where new
3334
02:09:16,480 --> 02:09:19,599
colon is true great that's it and now we
3335
02:09:19,599 --> 02:09:22,239
have access to the updated post below
3336
02:09:22,239 --> 02:09:25,199
that allows us to do rest.json and then
3337
02:09:25,199 --> 02:09:26,239
in there
3338
02:09:26,239 --> 02:09:29,360
updated post you can notice that this
3339
02:09:29,360 --> 02:09:32,159
thing is really really similar to the
3340
02:09:32,159 --> 02:09:34,320
update post great
3341
02:09:34,320 --> 02:09:36,400
now we're gonna save that and let's move
3342
02:09:36,400 --> 02:09:38,239
on to the frontend part
3343
02:09:38,239 --> 02:09:40,880
as always we first have to add the api
3344
02:09:40,880 --> 02:09:42,159
call
3345
02:09:42,159 --> 02:09:45,679
and that's going to be export const like
3346
02:09:45,679 --> 02:09:48,079
post we need the id
3347
02:09:48,079 --> 02:09:49,760
and then we have an error function where
3348
02:09:49,760 --> 02:09:50,599
we say
3349
02:09:50,599 --> 02:09:52,239
axios.patch
3350
02:09:52,239 --> 02:09:56,000
this time the url is going to be url
3351
02:09:56,000 --> 02:09:58,320
forward slash id
3352
02:09:58,320 --> 02:10:00,320
which needs to be a dynamic value
3353
02:10:00,320 --> 02:10:04,560
forward slash like post just like that
3354
02:10:04,560 --> 02:10:06,719
as always once we have the api request
3355
02:10:06,719 --> 02:10:09,360
we can move over to actions and in here
3356
02:10:09,360 --> 02:10:11,840
let's create the like action
3357
02:10:11,840 --> 02:10:13,840
export const
3358
02:10:13,840 --> 02:10:14,639
like
3359
02:10:14,639 --> 02:10:15,840
post
3360
02:10:15,840 --> 02:10:18,800
we have the id in there and then finally
3361
02:10:18,800 --> 02:10:20,400
async
3362
02:10:20,400 --> 02:10:22,000
dispatch
3363
02:10:22,000 --> 02:10:26,159
where we have a try and catch block
3364
02:10:26,159 --> 02:10:28,320
inside of here we want to get the data
3365
02:10:28,320 --> 02:10:30,480
of the newly updated post and that's
3366
02:10:30,480 --> 02:10:32,560
basically going to be the same as the
3367
02:10:32,560 --> 02:10:34,159
update so i'm going to take this whole
3368
02:10:34,159 --> 02:10:35,360
part
3369
02:10:35,360 --> 02:10:37,440
paste it in here and the only thing we
3370
02:10:37,440 --> 02:10:38,800
want to change
3371
02:10:38,800 --> 02:10:42,320
is from update post to like post that's
3372
02:10:42,320 --> 02:10:44,639
it in here of course we're gonna console
3373
02:10:44,639 --> 02:10:45,920
log
3374
02:10:45,920 --> 02:10:47,520
the error
3375
02:10:47,520 --> 02:10:49,280
oh and as well you can see we have the
3376
02:10:49,280 --> 02:10:50,480
error here
3377
02:10:50,480 --> 02:10:52,159
since we're just liking we don't have to
3378
02:10:52,159 --> 02:10:54,400
have the post here we just like it and
3379
02:10:54,400 --> 02:10:56,320
that's it we don't have to provide what
3380
02:10:56,320 --> 02:10:58,800
we want to do with it that's it
3381
02:10:58,800 --> 02:11:01,280
finally we go to reducers
3382
02:11:01,280 --> 02:11:03,679
posts and in here we have to implement
3383
02:11:03,679 --> 02:11:06,159
the logic for liking posts
3384
02:11:06,159 --> 02:11:08,400
and the logic is actually the same as
3385
02:11:08,400 --> 02:11:10,560
for the updates that's why i said that
3386
02:11:10,560 --> 02:11:12,800
these are really similar so first we map
3387
02:11:12,800 --> 02:11:15,119
over the posts we check what is the one
3388
02:11:15,119 --> 02:11:16,960
pose that changed or what is the one
3389
02:11:16,960 --> 02:11:18,639
pose that was liked
3390
02:11:18,639 --> 02:11:21,760
return the post with a change or if the
3391
02:11:21,760 --> 02:11:23,840
post is not liked then just return the
3392
02:11:23,840 --> 02:11:25,440
post as it was
3393
02:11:25,440 --> 02:11:27,040
since they're completely the same we
3394
02:11:27,040 --> 02:11:28,639
could do something like this where we
3395
02:11:28,639 --> 02:11:31,280
copy the same thing and then we say
3396
02:11:31,280 --> 02:11:32,159
like
3397
02:11:32,159 --> 02:11:35,280
but what we can also do is just put two
3398
02:11:35,280 --> 02:11:38,719
cases one below each other and then they
3399
02:11:38,719 --> 02:11:40,800
both are going to do the same thing
3400
02:11:40,800 --> 02:11:43,199
finally we can go to posts and then the
3401
02:11:43,199 --> 02:11:45,360
post component and in here with the
3402
02:11:45,360 --> 02:11:48,480
delete post we also want to import like
3403
02:11:48,480 --> 02:11:49,440
post
3404
02:11:49,440 --> 02:11:51,520
moving below to our button we just need
3405
02:11:51,520 --> 02:11:53,920
to add an on click which is going to say
3406
02:11:53,920 --> 02:11:55,840
dispatch
3407
02:11:55,840 --> 02:11:56,719
like
3408
02:11:56,719 --> 02:11:59,440
post and then in there we send over the
3409
02:11:59,440 --> 02:12:03,040
post dot underscore id that's it
3410
02:12:03,040 --> 02:12:04,800
let's see if it works
3411
02:12:04,800 --> 02:12:07,199
i'll try to like this post
3412
02:12:07,199 --> 02:12:09,840
as you can see we have one like
3413
02:12:09,840 --> 02:12:12,480
with our current implementation one user
3414
02:12:12,480 --> 02:12:15,119
can like a post as many times as he
3415
02:12:15,119 --> 02:12:16,320
wants
3416
02:12:16,320 --> 02:12:18,480
if we want to give users the ability to
3417
02:12:18,480 --> 02:12:21,679
like the post only once per account we
3418
02:12:21,679 --> 02:12:24,159
have to implement accounts what does
3419
02:12:24,159 --> 02:12:25,520
that mean
3420
02:12:25,520 --> 02:12:27,239
that means that we need the full
3421
02:12:27,239 --> 02:12:29,440
authentication system we need
3422
02:12:29,440 --> 02:12:32,000
registration we need login we need to
3423
02:12:32,000 --> 02:12:34,239
allow users to reset their passwords
3424
02:12:34,239 --> 02:12:36,800
create accounts and do everything else
3425
02:12:36,800 --> 02:12:39,520
that's usually called authentication you
3426
02:12:39,520 --> 02:12:41,840
guys can try to implement that by
3427
02:12:41,840 --> 02:12:43,760
yourselves but if you feel like you
3428
02:12:43,760 --> 02:12:45,920
cannot just let me know feel free to
3429
02:12:45,920 --> 02:12:48,079
like this video leave a lot of comments
3430
02:12:48,079 --> 02:12:51,040
and if this video goes well i might even
3431
02:12:51,040 --> 02:12:53,440
do the third part where we're going to
3432
02:12:53,440 --> 02:12:56,400
add authentication to this mern project
3433
02:12:56,400 --> 02:12:59,199
to make it even more complete
3434
02:12:59,199 --> 02:13:01,280
currently we're fine with the ability
3435
02:13:01,280 --> 02:13:03,679
for users to like the post as many times
3436
02:13:03,679 --> 02:13:06,960
as they want so that is completely fine
3437
02:13:06,960 --> 02:13:08,719
with that said let's move forward with
3438
02:13:08,719 --> 02:13:10,719
our app what else do we need to do to
3439
02:13:10,719 --> 02:13:13,119
make our app better and our code more
3440
02:13:13,119 --> 02:13:15,440
professional and presentable
3441
02:13:15,440 --> 02:13:17,599
well first of all i noticed that we have
3442
02:13:17,599 --> 02:13:19,440
a slight mistake here
3443
02:13:19,440 --> 02:13:21,760
the title and the description have the
3444
02:13:21,760 --> 02:13:24,560
same font or the same styles so let's go
3445
02:13:24,560 --> 02:13:27,159
ahead and change that you can go to the
3446
02:13:27,159 --> 02:13:31,280
post.js and then inside of the post.js
3447
02:13:31,280 --> 02:13:34,400
that's going to be somewhere around here
3448
02:13:34,400 --> 02:13:37,280
typography this is the body2 that's not
3449
02:13:37,280 --> 02:13:38,880
what we're looking for
3450
02:13:38,880 --> 02:13:41,119
there we go pose that created that we
3451
02:13:41,119 --> 02:13:43,040
are looking for the title there we go
3452
02:13:43,040 --> 02:13:46,079
post.title that is it and then in here
3453
02:13:46,079 --> 02:13:48,880
we have post dot message
3454
02:13:48,880 --> 02:13:51,119
what we want to do for the message is we
3455
02:13:51,119 --> 02:13:54,880
want to make it a variant of body 2 and
3456
02:13:54,880 --> 02:13:56,880
we want to give it a color
3457
02:13:56,880 --> 02:13:58,480
of text
3458
02:13:58,480 --> 02:14:00,239
secondary
3459
02:14:00,239 --> 02:14:01,760
just like this
3460
02:14:01,760 --> 02:14:04,000
and we can also say component is going
3461
02:14:04,000 --> 02:14:05,440
to be equal to
3462
02:14:05,440 --> 02:14:08,239
p it's a p tag and we don't need this
3463
02:14:08,239 --> 02:14:10,719
gutter bottom if we go back and refresh
3464
02:14:10,719 --> 02:14:14,320
the page we get it was awesome let's add
3465
02:14:14,320 --> 02:14:16,560
some more tags and some more words to
3466
02:14:16,560 --> 02:14:18,639
the description so we can see it better
3467
02:14:18,639 --> 02:14:20,800
in here i'm going to initiate our edit
3468
02:14:20,800 --> 02:14:22,400
which now fully works i'm going to
3469
02:14:22,400 --> 02:14:24,639
remove this exclamation mark and i'm
3470
02:14:24,639 --> 02:14:27,840
going to say it was awesome this was the
3471
02:14:27,840 --> 02:14:29,520
first time
3472
02:14:29,520 --> 02:14:32,960
i sky dived ever
3473
02:14:32,960 --> 02:14:34,480
great
3474
02:14:34,480 --> 02:14:35,920
experience
3475
02:14:35,920 --> 02:14:39,440
just like that and we can say skydiving
3476
02:14:39,440 --> 02:14:43,520
2020 uh great time just like that and
3477
02:14:43,520 --> 02:14:44,960
click submit
3478
02:14:44,960 --> 02:14:47,360
as you can see now this looks so much
3479
02:14:47,360 --> 02:14:49,920
better our card now has the title
3480
02:14:49,920 --> 02:14:52,239
description is properly styled and we
3481
02:14:52,239 --> 02:14:54,719
have some hashtags right there
3482
02:14:54,719 --> 02:14:56,880
and i noticed one more thing to make our
3483
02:14:56,880 --> 02:14:59,520
app just a bit better currently our tags
3484
02:14:59,520 --> 02:15:01,840
are not working as they're supposed to
3485
02:15:01,840 --> 02:15:04,320
as you can see they're all kinda the
3486
02:15:04,320 --> 02:15:06,719
same hashtag right even if you try to
3487
02:15:06,719 --> 02:15:09,599
change them and then add spaces it's
3488
02:15:09,599 --> 02:15:11,040
still not going to be many different
3489
02:15:11,040 --> 02:15:13,599
hashtags so later on if we want to
3490
02:15:13,599 --> 02:15:16,400
implement like search posts by hashtags
3491
02:15:16,400 --> 02:15:18,800
this is not gonna work what we need to
3492
02:15:18,800 --> 02:15:21,119
do to make it work is go back to our
3493
02:15:21,119 --> 02:15:24,719
form and change the way we save our tags
3494
02:15:24,719 --> 02:15:27,199
that's going to be in the form folder
3495
02:15:27,199 --> 02:15:29,599
and then in the form component
3496
02:15:29,599 --> 02:15:32,000
and that's going to be around here as
3497
02:15:32,000 --> 02:15:34,639
you can see this text field is used to
3498
02:15:34,639 --> 02:15:37,119
save tags currently we're just saving
3499
02:15:37,119 --> 02:15:39,599
them as a string what we want to do
3500
02:15:39,599 --> 02:15:42,800
though is do e target.value this is a
3501
02:15:42,800 --> 02:15:45,520
string of tags we want to call the dot
3502
02:15:45,520 --> 02:15:48,480
split method on it and we want to split
3503
02:15:48,480 --> 02:15:50,560
it into an array
3504
02:15:50,560 --> 02:15:52,880
by a comma so in here we are saying
3505
02:15:52,880 --> 02:15:54,560
split this string
3506
02:15:54,560 --> 02:15:57,599
by commas no space afterward and then
3507
02:15:57,599 --> 02:16:00,159
give me an array of all the tags if we
3508
02:16:00,159 --> 02:16:03,040
now save this and go back we should be
3509
02:16:03,040 --> 02:16:05,199
able to edit this into a fully
3510
02:16:05,199 --> 02:16:07,840
functional post with hashtags
3511
02:16:07,840 --> 02:16:10,079
let's take a look we are back in here
3512
02:16:10,079 --> 02:16:12,159
i'm going to click edit and now i'm
3513
02:16:12,159 --> 02:16:14,400
going to try putting all of these
3514
02:16:14,400 --> 02:16:15,599
just as
3515
02:16:15,599 --> 02:16:18,800
tags with comma separated values and
3516
02:16:18,800 --> 02:16:20,079
click submit
3517
02:16:20,079 --> 02:16:22,239
there we go this looks so much better
3518
02:16:22,239 --> 02:16:24,320
now we have three different hashtags
3519
02:16:24,320 --> 02:16:28,000
skydiving 2020 and great time it's great
3520
02:16:28,000 --> 02:16:30,400
in regards to the cards i think our work
3521
02:16:30,400 --> 02:16:32,879
is done we can add a few more cards just
3522
02:16:32,879 --> 02:16:35,280
to get a feel of how this looks like
3523
02:16:35,280 --> 02:16:37,840
as you can see i added one new card it
3524
02:16:37,840 --> 02:16:41,599
is new year's eve 2019 james said that
3525
02:16:41,599 --> 02:16:44,000
it was the best new year's eve ever and
3526
02:16:44,000 --> 02:16:46,718
that he was in times square amazing so
3527
02:16:46,718 --> 02:16:48,558
our cards look pretty good we can like
3528
02:16:48,558 --> 02:16:50,718
them we can delete them i just noticed
3529
02:16:50,718 --> 02:16:52,638
like we need to add a space in here
3530
02:16:52,638 --> 02:16:55,040
between the like the icon and the number
3531
02:16:55,040 --> 02:16:57,359
so let's do that right now
3532
02:16:57,359 --> 02:16:59,679
for that we're going to go into post
3533
02:16:59,679 --> 02:17:02,398
right here and then find where is our
3534
02:17:02,398 --> 02:17:05,439
like button you see we could add a space
3535
02:17:05,439 --> 02:17:07,760
here and here but that's not going to do
3536
02:17:07,760 --> 02:17:09,120
anything that's not going to be
3537
02:17:09,120 --> 02:17:12,240
interpreted by our jsx what we need to
3538
02:17:12,240 --> 02:17:16,000
do is add a code for the space character
3539
02:17:16,000 --> 02:17:18,318
it is and and b
3540
02:17:18,318 --> 02:17:20,398
sp semicolon
3541
02:17:20,398 --> 02:17:22,478
this is the same as you wrote a space
3542
02:17:22,478 --> 02:17:24,398
character but we need to use that in
3543
02:17:24,398 --> 02:17:27,599
here so let's add that just before the
3544
02:17:27,599 --> 02:17:30,160
like and also after the like i think
3545
02:17:30,160 --> 02:17:32,558
this is gonna be good let's test it out
3546
02:17:32,558 --> 02:17:35,439
save it and go back to the browser
3547
02:17:35,439 --> 02:17:37,840
as you can see this is much better we
3548
02:17:37,840 --> 02:17:40,160
have some more padding in here and we
3549
02:17:40,160 --> 02:17:43,200
can like the post we're moving on to an
3550
02:17:43,200 --> 02:17:46,160
incredibly important thing and that is
3551
02:17:46,160 --> 02:17:49,439
is our website or the application mobile
3552
02:17:49,439 --> 02:17:52,638
friendly is it responsive for that you
3553
02:17:52,638 --> 02:17:56,558
can right click and click inspect
3554
02:17:56,558 --> 02:17:58,398
this is going to open the responsive
3555
02:17:58,398 --> 02:18:00,959
view to view it in a mobile device click
3556
02:18:00,959 --> 02:18:03,599
here into responsive and choose a phone
3557
02:18:03,599 --> 02:18:06,160
let's choose iphone x
3558
02:18:06,160 --> 02:18:08,160
and would you look at that
3559
02:18:08,160 --> 02:18:11,120
this application is responsive out of
3560
02:18:11,120 --> 02:18:13,280
the box because we were using material
3561
02:18:13,280 --> 02:18:15,439
ui did you think we're going to have to
3562
02:18:15,439 --> 02:18:17,679
do a lot of work to actually make this
3563
02:18:17,679 --> 02:18:19,840
accessible for mobile devices
3564
02:18:19,840 --> 02:18:22,879
well that is not the case as you can see
3565
02:18:22,879 --> 02:18:25,679
the application is fully responsive both
3566
02:18:25,679 --> 02:18:28,718
the form and the cards
3567
02:18:28,718 --> 02:18:30,799
the only thing we could do is put the
3568
02:18:30,799 --> 02:18:33,280
form on the top of the screen for mobile
3569
02:18:33,280 --> 02:18:35,679
devices because as you can see currently
3570
02:18:35,679 --> 02:18:38,558
it's kind of buried down there how would
3571
02:18:38,558 --> 02:18:40,879
we do that well let's take a look the
3572
02:18:40,879 --> 02:18:43,280
form is currently in here and that is
3573
02:18:43,280 --> 02:18:45,200
inside of this
3574
02:18:45,200 --> 02:18:48,240
container material ui container so what
3575
02:18:48,240 --> 02:18:50,080
we would need to do is change this
3576
02:18:50,080 --> 02:18:52,638
container into display
3577
02:18:52,638 --> 02:18:53,840
flex
3578
02:18:53,840 --> 02:18:55,040
like this
3579
02:18:55,040 --> 02:18:58,080
give it a flex direction of column
3580
02:18:58,080 --> 02:19:00,638
but i think it already has that and then
3581
02:19:00,638 --> 02:19:03,519
i think we can do flex direction yeah
3582
02:19:03,519 --> 02:19:06,398
flex direction column reverse that's
3583
02:19:06,398 --> 02:19:08,558
what we want to go for
3584
02:19:08,558 --> 02:19:10,718
as you can see that didn't do it i'm
3585
02:19:10,718 --> 02:19:12,000
guessing because there is another
3586
02:19:12,000 --> 02:19:14,000
container inside of there so maybe we
3587
02:19:14,000 --> 02:19:16,398
need to be changing this one this one is
3588
02:19:16,398 --> 02:19:18,318
for the cards as you can see but this is
3589
02:19:18,318 --> 02:19:20,240
the one we want to be targeting not the
3590
02:19:20,240 --> 02:19:23,120
one above rather just this one so in
3591
02:19:23,120 --> 02:19:26,558
here i'm gonna do flex direction and
3592
02:19:26,558 --> 02:19:29,760
that's going to be column reverse
3593
02:19:29,760 --> 02:19:33,120
and would you look at that our form just
3594
02:19:33,120 --> 02:19:36,318
to the top so now if we have 10 20 or 30
3595
02:19:36,318 --> 02:19:38,478
cards you wouldn't have to scroll you
3596
02:19:38,478 --> 02:19:40,718
just have to put the form at the top
3597
02:19:40,718 --> 02:19:44,959
so this was the material ui spacing 3
3598
02:19:44,959 --> 02:19:47,679
grid so if we go back to the code i'm
3599
02:19:47,679 --> 02:19:49,920
just gonna search for spacing is equal
3600
02:19:49,920 --> 02:19:51,840
to three because that that's what our
3601
02:19:51,840 --> 02:19:53,280
container has
3602
02:19:53,280 --> 02:19:56,240
and that is currently in here in the app
3603
02:19:56,240 --> 02:19:58,800
our app is wrapping our posts and the
3604
02:19:58,800 --> 02:19:59,680
form
3605
02:19:59,680 --> 02:20:02,000
so the only thing we have to do in here
3606
02:20:02,000 --> 02:20:05,120
is something like flex direction
3607
02:20:05,120 --> 02:20:07,439
and then let's try to do
3608
02:20:07,439 --> 02:20:09,120
column
3609
02:20:09,120 --> 02:20:10,960
reverse we'll see if that works i'm not
3610
02:20:10,960 --> 02:20:12,960
sure if they have built in property for
3611
02:20:12,960 --> 02:20:14,640
flex direction
3612
02:20:14,640 --> 02:20:17,840
if we save it and go back let's see if
3613
02:20:17,840 --> 02:20:20,479
that worked unfortunately it didn't at
3614
02:20:20,479 --> 02:20:22,080
this point i would just go into the
3615
02:20:22,080 --> 02:20:24,080
material ui documentation and take a
3616
02:20:24,080 --> 02:20:25,920
look but let's give it just one more
3617
02:20:25,920 --> 02:20:28,319
shot let's try with just direction
3618
02:20:28,319 --> 02:20:30,319
instead of flex direction because in
3619
02:20:30,319 --> 02:20:32,240
here you can see it's just justify just
3620
02:20:32,240 --> 02:20:35,040
the line items so maybe who knows that
3621
02:20:35,040 --> 02:20:37,760
works and looks like it did would you
3622
02:20:37,760 --> 02:20:39,840
look at that material ui is really
3623
02:20:39,840 --> 02:20:40,960
intuitive
3624
02:20:40,960 --> 02:20:42,960
but we don't want this to happen for
3625
02:20:42,960 --> 02:20:45,920
desktop devices so we only want to apply
3626
02:20:45,920 --> 02:20:49,200
this if we are on mobile because of that
3627
02:20:49,200 --> 02:20:51,280
i'm going to copy this and simply give
3628
02:20:51,280 --> 02:20:53,200
it a class name so we're going to say
3629
02:20:53,200 --> 02:20:55,280
class name and this is going to be
3630
02:20:55,280 --> 02:20:57,040
something like classes
3631
02:20:57,040 --> 02:21:01,040
that a main container just like that now
3632
02:21:01,040 --> 02:21:04,720
we can go into our styles of the app.js
3633
02:21:04,720 --> 02:21:07,479
let's take a look that is right here in
3634
02:21:07,479 --> 02:21:10,240
styles.js and then in here we can say
3635
02:21:10,240 --> 02:21:12,479
main container
3636
02:21:12,479 --> 02:21:14,720
do it like this as an object and then in
3637
02:21:14,720 --> 02:21:17,520
there we want to do flex
3638
02:21:17,520 --> 02:21:20,160
direction and that is going to be equal
3639
02:21:20,160 --> 02:21:20,960
to
3640
02:21:20,960 --> 02:21:24,160
column reverse but how do we do that
3641
02:21:24,160 --> 02:21:27,840
only on mobile material ui offers
3642
02:21:27,840 --> 02:21:30,319
something known as breakpoints they are
3643
02:21:30,319 --> 02:21:32,240
simply media queries
3644
02:21:32,240 --> 02:21:34,560
so the only thing we have to do to make
3645
02:21:34,560 --> 02:21:38,080
this work is put the array sign and then
3646
02:21:38,080 --> 02:21:40,560
in there we're going gonna say theme
3647
02:21:40,560 --> 02:21:43,280
dot breakpoints
3648
02:21:43,280 --> 02:21:45,040
dot down
3649
02:21:45,040 --> 02:21:46,800
and then you call it as a method and
3650
02:21:46,800 --> 02:21:49,280
then in there you say sm
3651
02:21:49,280 --> 02:21:52,240
as you can see we have sm excess medium
3652
02:21:52,240 --> 02:21:55,040
large and extra large so in here we're
3653
02:21:55,040 --> 02:21:58,640
basically saying run the css after this
3654
02:21:58,640 --> 02:22:02,000
only for the devices that are small or
3655
02:22:02,000 --> 02:22:04,560
even smaller than small like extra small
3656
02:22:04,560 --> 02:22:06,720
and then in here we can move all of our
3657
02:22:06,720 --> 02:22:08,560
styles that we have here
3658
02:22:08,560 --> 02:22:11,280
and that is it as you can see our team
3659
02:22:11,280 --> 02:22:13,200
is not being declared so we have to
3660
02:22:13,200 --> 02:22:16,399
import our team right here make styles
3661
02:22:16,399 --> 02:22:17,760
theme
3662
02:22:17,760 --> 02:22:19,680
again if you're not familiar with what
3663
02:22:19,680 --> 02:22:21,840
is happening right now it is just from
3664
02:22:21,840 --> 02:22:24,080
the docs you can go ahead and research
3665
02:22:24,080 --> 02:22:27,280
material ui into a bit more details or i
3666
02:22:27,280 --> 02:22:29,280
can create a fully separate video on
3667
02:22:29,280 --> 02:22:31,840
material ui if you'd like me to do that
3668
02:22:31,840 --> 02:22:35,359
feel free to leave a comment down below
3669
02:22:35,359 --> 02:22:37,760
so in regards to the mobile friendliness
3670
02:22:37,760 --> 02:22:41,200
we are fully done works on mobile and
3671
02:22:41,200 --> 02:22:43,680
works on desktop that is great if you
3672
02:22:43,680 --> 02:22:46,000
want to you can make it work on tablets
3673
02:22:46,000 --> 02:22:48,000
as well there shouldn't be a lot of
3674
02:22:48,000 --> 02:22:50,960
problems but this is good for now our
3675
02:22:50,960 --> 02:22:53,200
next step is not going to add new
3676
02:22:53,200 --> 02:22:55,120
functionalities to the app or make it
3677
02:22:55,120 --> 02:22:58,160
look better it's going to make our code
3678
02:22:58,160 --> 02:23:01,120
look better and be more professional
3679
02:23:01,120 --> 02:23:02,720
if you're employing for jobs and if you
3680
02:23:02,720 --> 02:23:04,800
put the code on github you want to make
3681
02:23:04,800 --> 02:23:07,040
it as good as it can be
3682
02:23:07,040 --> 02:23:09,840
no errors no silly indentations all
3683
02:23:09,840 --> 02:23:12,319
around the place like this or spaces
3684
02:23:12,319 --> 02:23:14,800
running around you want your code to be
3685
02:23:14,800 --> 02:23:17,439
as professional as it can be because
3686
02:23:17,439 --> 02:23:19,760
later on you're gonna work in themes
3687
02:23:19,760 --> 02:23:22,560
so what people often do in redux is they
3688
02:23:22,560 --> 02:23:25,520
add constants for their action types
3689
02:23:25,520 --> 02:23:27,920
currently if we close all of this so
3690
02:23:27,920 --> 02:23:30,160
close all the files and collapse the
3691
02:23:30,160 --> 02:23:32,160
file tree by clicking this icon right
3692
02:23:32,160 --> 02:23:35,280
there we want to go to client and then
3693
02:23:35,280 --> 02:23:37,439
actions right here
3694
02:23:37,439 --> 02:23:40,240
take a look we have all of these strings
3695
02:23:40,240 --> 02:23:42,720
right here this is a string this is a
3696
02:23:42,720 --> 02:23:45,680
string all of our action types are
3697
02:23:45,680 --> 02:23:47,600
indeed strings right
3698
02:23:47,600 --> 02:23:49,840
so what do you think would happen if for
3699
02:23:49,840 --> 02:23:53,280
example i misspelled this from fetch all
3700
02:23:53,280 --> 02:23:56,160
to fetch all like this there is a typo
3701
02:23:56,160 --> 02:23:58,160
but maybe while you're coding you don't
3702
02:23:58,160 --> 02:23:59,200
see it
3703
02:23:59,200 --> 02:24:00,560
of course this would break our
3704
02:24:00,560 --> 02:24:03,520
application but here's the catch
3705
02:24:03,520 --> 02:24:04,640
take a look
3706
02:24:04,640 --> 02:24:07,359
there is no error and even if you go to
3707
02:24:07,359 --> 02:24:10,560
the browser you would see okay it's not
3708
02:24:10,560 --> 02:24:12,720
loading something is not working and
3709
02:24:12,720 --> 02:24:15,040
even if you went to the inspect element
3710
02:24:15,040 --> 02:24:17,359
you wouldn't be able to find any errors
3711
02:24:17,359 --> 02:24:19,200
because there are none you simply
3712
02:24:19,200 --> 02:24:21,200
misspelled a variable
3713
02:24:21,200 --> 02:24:23,359
but it's not like this it's not like
3714
02:24:23,359 --> 02:24:25,520
when you have a variable cons test is
3715
02:24:25,520 --> 02:24:27,600
equal to test like this
3716
02:24:27,600 --> 02:24:30,240
and then you want to do a console log
3717
02:24:30,240 --> 02:24:32,399
test and then you mispell it here like
3718
02:24:32,399 --> 02:24:35,120
this it's not the same situation because
3719
02:24:35,120 --> 02:24:37,680
in here it's immediately going to say
3720
02:24:37,680 --> 02:24:40,960
this variable doesn't exist there is an
3721
02:24:40,960 --> 02:24:43,040
error but it's tricky working with
3722
02:24:43,040 --> 02:24:45,439
strings right so this is why people have
3723
02:24:45,439 --> 02:24:47,120
figured they're going to create
3724
02:24:47,120 --> 02:24:50,720
constants for these action types so how
3725
02:24:50,720 --> 02:24:52,080
can we do that
3726
02:24:52,080 --> 02:24:54,399
well we're going to create a new folder
3727
02:24:54,399 --> 02:24:59,120
in our src folder called constants
3728
02:24:59,520 --> 02:25:01,280
inside of our constants we're going to
3729
02:25:01,280 --> 02:25:05,680
create a new file called action types
3730
02:25:05,680 --> 02:25:07,280
dot js
3731
02:25:07,280 --> 02:25:09,040
and then inside of there we're going to
3732
02:25:09,040 --> 02:25:11,200
create a lot of separate variables for
3733
02:25:11,200 --> 02:25:14,720
these strings fetch all create we need
3734
02:25:14,720 --> 02:25:17,359
more for update delete and everything
3735
02:25:17,359 --> 02:25:19,840
else that we have so if we go to action
3736
02:25:19,840 --> 02:25:20,800
types
3737
02:25:20,800 --> 02:25:22,800
so in here we're going to start creating
3738
02:25:22,800 --> 02:25:24,560
all these variables
3739
02:25:24,560 --> 02:25:27,359
const create is going to be equal to
3740
02:25:27,359 --> 02:25:28,560
create
3741
02:25:28,560 --> 02:25:29,760
const
3742
02:25:29,760 --> 02:25:30,960
update
3743
02:25:30,960 --> 02:25:33,760
is going to be equal to a string of
3744
02:25:33,760 --> 02:25:35,920
update or uppercase
3745
02:25:35,920 --> 02:25:37,760
then we have
3746
02:25:37,760 --> 02:25:40,640
cons delete is equal to delete
3747
02:25:40,640 --> 02:25:42,160
like this
3748
02:25:42,160 --> 02:25:43,359
we have
3749
02:25:43,359 --> 02:25:47,600
const fetch all is equal to
3750
02:25:47,600 --> 02:25:49,200
fetch
3751
02:25:49,200 --> 02:25:50,560
all
3752
02:25:50,560 --> 02:25:53,760
and then we finally have const like is
3753
02:25:53,760 --> 02:25:55,359
equal to
3754
02:25:55,359 --> 02:25:57,520
of course you guessed it an uppercase
3755
02:25:57,520 --> 02:25:59,280
string of like
3756
02:25:59,280 --> 02:26:00,880
of course we don't want to have this
3757
02:26:00,880 --> 02:26:02,640
here because as you can see we are never
3758
02:26:02,640 --> 02:26:05,120
using it so what we want to do is we
3759
02:26:05,120 --> 02:26:08,240
want to export all of these so we can do
3760
02:26:08,240 --> 02:26:11,920
it like this export const create update
3761
02:26:11,920 --> 02:26:14,720
delete fetch all and now we will be able
3762
02:26:14,720 --> 02:26:17,520
to go to posts and then import all of
3763
02:26:17,520 --> 02:26:21,920
these types or action types right there
3764
02:26:21,920 --> 02:26:24,080
right before the first line i'm going to
3765
02:26:24,080 --> 02:26:24,960
say
3766
02:26:24,960 --> 02:26:26,080
import
3767
02:26:26,080 --> 02:26:28,000
open curly braces
3768
02:26:28,000 --> 02:26:30,240
fetch
3769
02:26:30,240 --> 02:26:31,920
all
3770
02:26:31,920 --> 02:26:33,120
create
3771
02:26:33,120 --> 02:26:34,479
update
3772
02:26:34,479 --> 02:26:37,920
delete and like we're importing all of
3773
02:26:37,920 --> 02:26:39,600
these from
3774
02:26:39,600 --> 02:26:42,560
dot slash constants slash
3775
02:26:42,560 --> 02:26:46,080
action types just like that
3776
02:26:46,080 --> 02:26:48,319
and now what we're going to do is in
3777
02:26:48,319 --> 02:26:50,560
here instead of this string of fetch all
3778
02:26:50,560 --> 02:26:53,040
we're simply going to use this variable
3779
02:26:53,040 --> 02:26:55,760
the same things go for create
3780
02:26:55,760 --> 02:26:57,120
update
3781
02:26:57,120 --> 02:26:58,000
like
3782
02:26:58,000 --> 02:27:00,720
and delete
3783
02:27:00,720 --> 02:27:03,680
make sure not to misspell something
3784
02:27:03,680 --> 02:27:05,760
but let's add all of the variable names
3785
02:27:05,760 --> 02:27:08,319
right there
3786
02:27:12,080 --> 02:27:13,920
oh looks like we used the update for
3787
02:27:13,920 --> 02:27:16,080
like as well so looks like we don't need
3788
02:27:16,080 --> 02:27:18,560
the like after all i'm going to get rid
3789
02:27:18,560 --> 02:27:21,280
of that go to action types and remove
3790
02:27:21,280 --> 02:27:22,720
the like
3791
02:27:22,720 --> 02:27:25,760
so now here's the thing if you misspell
3792
02:27:25,760 --> 02:27:27,280
something like this
3793
02:27:27,280 --> 02:27:29,120
it's immediately going to give you an
3794
02:27:29,120 --> 02:27:32,160
error this might seem like a lot of work
3795
02:27:32,160 --> 02:27:34,399
to actually implement this import it and
3796
02:27:34,399 --> 02:27:37,200
then use it here but really trust me if
3797
02:27:37,200 --> 02:27:39,520
you misspell a string here you can spend
3798
02:27:39,520 --> 02:27:41,920
hours and hours looking for an error and
3799
02:27:41,920 --> 02:27:44,000
you won't be able to find it easily but
3800
02:27:44,000 --> 02:27:46,160
with this you're just making your code
3801
02:27:46,160 --> 02:27:49,120
less error prone and more scalable as
3802
02:27:49,120 --> 02:27:51,520
you keep adding more controllers more
3803
02:27:51,520 --> 02:27:53,680
actions more reducers you'll just keep
3804
02:27:53,680 --> 02:27:56,080
adding different variable names
3805
02:27:56,080 --> 02:27:59,120
and not only that we can use these same
3806
02:27:59,120 --> 02:28:01,680
action types we used here in our
3807
02:28:01,680 --> 02:28:04,960
reducers because they are the same so in
3808
02:28:04,960 --> 02:28:07,200
here i'm also going to import the same
3809
02:28:07,200 --> 02:28:09,680
thing and then we can change all of
3810
02:28:09,680 --> 02:28:10,560
these
3811
02:28:10,560 --> 02:28:14,240
to our normal variables i'm just going
3812
02:28:14,240 --> 02:28:15,840
to remove this
3813
02:28:15,840 --> 02:28:20,160
and let's do it like this fetch all
3814
02:28:20,160 --> 02:28:22,240
looks like in here we do have the like
3815
02:28:22,240 --> 02:28:24,960
case but we are using only the update so
3816
02:28:24,960 --> 02:28:27,120
we don't really care about that
3817
02:28:27,120 --> 02:28:30,399
great now we implemented constant action
3818
02:28:30,399 --> 02:28:32,560
types and we just made our code much
3819
02:28:32,560 --> 02:28:35,760
more scalable and much less error prone
3820
02:28:35,760 --> 02:28:37,840
if we go back to our application it
3821
02:28:37,840 --> 02:28:40,560
should still work just as fine
3822
02:28:40,560 --> 02:28:43,120
great that is done let's move back to
3823
02:28:43,120 --> 02:28:44,960
our code and let me show you one of the
3824
02:28:44,960 --> 02:28:46,720
final things i want to show you on this
3825
02:28:46,720 --> 02:28:48,800
project again we're going to close
3826
02:28:48,800 --> 02:28:51,200
everything collapse it and this time
3827
02:28:51,200 --> 02:28:53,760
we're going to go into our server more
3828
02:28:53,760 --> 02:28:56,640
specifically the index.js file of the
3829
02:28:56,640 --> 02:28:58,240
server
3830
02:28:58,240 --> 02:29:00,319
we have this connection string right
3831
02:29:00,319 --> 02:29:03,040
here we also have the port right here
3832
02:29:03,040 --> 02:29:05,120
and if you think about it in this string
3833
02:29:05,120 --> 02:29:07,680
you can see our username you can see the
3834
02:29:07,680 --> 02:29:10,319
password of our user and you can also
3835
02:29:10,319 --> 02:29:12,240
see the name of the database
3836
02:29:12,240 --> 02:29:14,319
that info should not be publicly
3837
02:29:14,319 --> 02:29:16,560
available you shouldn't be posting your
3838
02:29:16,560 --> 02:29:18,880
projects on github like this
3839
02:29:18,880 --> 02:29:21,040
this needs to be stored somewhere where
3840
02:29:21,040 --> 02:29:22,960
other people cannot have access to your
3841
02:29:22,960 --> 02:29:26,000
passwords or secret api keys
3842
02:29:26,000 --> 02:29:27,920
so how can we fix that
3843
02:29:27,920 --> 02:29:30,479
the only thing we have to do is create
3844
02:29:30,479 --> 02:29:33,520
one new file in our server.js and that
3845
02:29:33,520 --> 02:29:36,399
is going to be called dot env
3846
02:29:36,399 --> 02:29:39,359
env stands for environmental variables
3847
02:29:39,359 --> 02:29:41,439
and these are files that are only going
3848
02:29:41,439 --> 02:29:44,479
to be accessible in here on your pc
3849
02:29:44,479 --> 02:29:46,479
these are not going to be posted on
3850
02:29:46,479 --> 02:29:47,520
github
3851
02:29:47,520 --> 02:29:50,160
so now in this file you can add all of
3852
02:29:50,160 --> 02:29:52,399
your variables for example let's start
3853
02:29:52,399 --> 02:29:54,319
with the port first the only thing you
3854
02:29:54,319 --> 02:29:57,120
have to do is take this port and that is
3855
02:29:57,120 --> 02:29:59,439
going to be the name of your variable
3856
02:29:59,439 --> 02:30:01,520
then followed by an equal sign you can
3857
02:30:01,520 --> 02:30:04,880
specify the value for the port now this
3858
02:30:04,880 --> 02:30:07,680
value should be 5000 but let's change
3859
02:30:07,680 --> 02:30:09,760
all the other values we're also going to
3860
02:30:09,760 --> 02:30:12,479
take our connection url so connection
3861
02:30:12,479 --> 02:30:15,600
url is going to be equal to this string
3862
02:30:15,600 --> 02:30:17,760
i'm going to copy the string and paste
3863
02:30:17,760 --> 02:30:19,760
it right here and that is going to be
3864
02:30:19,760 --> 02:30:21,520
equal to connection
3865
02:30:21,520 --> 02:30:22,560
url
3866
02:30:22,560 --> 02:30:25,040
of course with an equal sign and without
3867
02:30:25,040 --> 02:30:28,319
the string signs just like this
3868
02:30:28,319 --> 02:30:31,040
for these variables to be visible by our
3869
02:30:31,040 --> 02:30:33,520
application we need to install something
3870
02:30:33,520 --> 02:30:38,080
called dot env so you can press mpm i
3871
02:30:38,080 --> 02:30:40,800
dot env like this
3872
02:30:40,800 --> 02:30:43,280
once we save that our application will
3873
02:30:43,280 --> 02:30:45,680
be able to read the process env
3874
02:30:45,680 --> 02:30:48,319
variables
3875
02:30:50,319 --> 02:30:52,479
that is installed and let's run our
3876
02:30:52,479 --> 02:30:54,560
application even if you didn't install
3877
02:30:54,560 --> 02:30:56,240
it or if you had it before you still
3878
02:30:56,240 --> 02:30:58,479
need to press ctrl c to close the server
3879
02:30:58,479 --> 02:31:00,399
and then start it again for the
3880
02:31:00,399 --> 02:31:03,600
variables to actually take place finally
3881
02:31:03,600 --> 02:31:05,280
we have to import
3882
02:31:05,280 --> 02:31:07,439
dot env from
3883
02:31:07,439 --> 02:31:09,280
dot env
3884
02:31:09,280 --> 02:31:12,800
and then we have to call just at the top
3885
02:31:12,800 --> 02:31:15,600
dot env dot config
3886
02:31:15,600 --> 02:31:18,240
just like that let's save it and see how
3887
02:31:18,240 --> 02:31:19,920
it goes
3888
02:31:19,920 --> 02:31:22,080
this is saved our server is running on
3889
02:31:22,080 --> 02:31:24,880
port 5000 and if we go back to our
3890
02:31:24,880 --> 02:31:28,000
application you can see everything works
3891
02:31:28,000 --> 02:31:30,000
perfectly how can we know we are
3892
02:31:30,000 --> 02:31:32,240
changing something well let's try to
3893
02:31:32,240 --> 02:31:34,880
console log this port or even we can
3894
02:31:34,880 --> 02:31:36,319
just change it and then you'll be able
3895
02:31:36,319 --> 02:31:38,880
to see it in the console right away i'm
3896
02:31:38,880 --> 02:31:41,520
going to say 10 000.
3897
02:31:41,520 --> 02:31:43,920
if we do that save it
3898
02:31:43,920 --> 02:31:46,240
and rerun the file
3899
02:31:46,240 --> 02:31:48,640
it's still saying server running let's
3900
02:31:48,640 --> 02:31:49,520
see
3901
02:31:49,520 --> 02:31:51,359
yeah it immediately changed so we're
3902
02:31:51,359 --> 02:31:53,520
running on port ten thousand we don't
3903
02:31:53,520 --> 02:31:55,280
wanna do that because that would break
3904
02:31:55,280 --> 02:31:57,120
our application our front-end is
3905
02:31:57,120 --> 02:32:00,240
expecting localhost 5000 so let's go
3906
02:32:00,240 --> 02:32:03,280
back and change it to 5000. now we are
3907
02:32:03,280 --> 02:32:05,920
securely storing our connection url
3908
02:32:05,920 --> 02:32:09,040
inside of our.env
3909
02:32:09,040 --> 02:32:11,680
and since r.e and v is not going to be
3910
02:32:11,680 --> 02:32:14,479
saved on our github what i like to do is
3911
02:32:14,479 --> 02:32:16,359
i like to do dot
3912
02:32:16,359 --> 02:32:17,920
env.example
3913
02:32:17,920 --> 02:32:19,840
this file is going to be published to
3914
02:32:19,840 --> 02:32:23,200
github so in here we can give an example
3915
02:32:23,200 --> 02:32:25,760
of what all people need to have in their
3916
02:32:25,760 --> 02:32:28,640
environments for this to work so in here
3917
02:32:28,640 --> 02:32:30,800
we're going to say
3918
02:32:30,800 --> 02:32:33,920
url string and then in here
3919
02:32:33,920 --> 02:32:36,800
at the top we can just say
3920
02:32:36,800 --> 02:32:39,840
port that way they know that they have
3921
02:32:39,840 --> 02:32:42,080
to add their own thing in here for this
3922
02:32:42,080 --> 02:32:43,120
to work
3923
02:32:43,120 --> 02:32:46,319
great now our application works and it
3924
02:32:46,319 --> 02:32:48,640
works really really well people can
3925
02:32:48,640 --> 02:32:51,359
create memories edit memories like them
3926
02:32:51,359 --> 02:32:52,479
delete them
3927
02:32:52,479 --> 02:32:54,399
it is a fully functional crowd
3928
02:32:54,399 --> 02:32:57,040
application
3929
02:32:57,120 --> 02:32:59,120
we can start on the front-end side we
3930
02:32:59,120 --> 02:33:01,680
can first start adding on the navigation
3931
02:33:01,680 --> 02:33:04,319
bar the items for the login and
3932
02:33:04,319 --> 02:33:06,640
registration so once we click that we'll
3933
02:33:06,640 --> 02:33:09,200
be redirected to a different form the
3934
02:33:09,200 --> 02:33:11,840
form for login or register so let's do
3935
02:33:11,840 --> 02:33:12,960
that first
3936
02:33:12,960 --> 02:33:14,960
if we go back to our code you can head
3937
02:33:14,960 --> 02:33:18,479
to client we can go to src components
3938
02:33:18,479 --> 02:33:20,479
and then right inside of here we can
3939
02:33:20,479 --> 02:33:22,240
create that nav bar
3940
02:33:22,240 --> 02:33:24,560
if i remember correctly we also had a
3941
02:33:24,560 --> 02:33:26,640
bit of a navbar before but we didn't
3942
02:33:26,640 --> 02:33:29,040
have it as a separate component so if we
3943
02:33:29,040 --> 02:33:31,359
go to app.js you can see that our nav
3944
02:33:31,359 --> 02:33:33,840
bar is here so we're going to divide
3945
02:33:33,840 --> 02:33:36,319
that into a new component
3946
02:33:36,319 --> 02:33:38,880
so inside of components we can create a
3947
02:33:38,880 --> 02:33:41,280
new component it's going to be called
3948
02:33:41,280 --> 02:33:42,560
navbar
3949
02:33:42,560 --> 02:33:44,960
and now we can create a file called
3950
02:33:44,960 --> 02:33:48,399
navbar dot js and also
3951
02:33:48,399 --> 02:33:49,840
styles
3952
02:33:49,840 --> 02:33:51,439
dot js
3953
02:33:51,439 --> 02:33:53,760
we're going to copy what we have so far
3954
02:33:53,760 --> 02:33:54,960
in our app
3955
02:33:54,960 --> 02:33:56,479
so right here
3956
02:33:56,479 --> 02:33:59,359
and i'm going to create a rafc a react
3957
02:33:59,359 --> 02:34:01,439
function component with exports and
3958
02:34:01,439 --> 02:34:03,680
we're simply going to paste what we have
3959
02:34:03,680 --> 02:34:05,200
so far
3960
02:34:05,200 --> 02:34:07,200
okay that's great as you can see i'm
3961
02:34:07,200 --> 02:34:09,040
using eslint so some of the things are
3962
02:34:09,040 --> 02:34:10,720
going to get changed immediately but
3963
02:34:10,720 --> 02:34:12,800
don't worry about that and finally we
3964
02:34:12,800 --> 02:34:16,560
have to import some classes so in here
3965
02:34:16,560 --> 02:34:19,359
we also need our app bar and typography
3966
02:34:19,359 --> 02:34:23,359
so i'm going to go here and say import
3967
02:34:23,359 --> 02:34:25,359
what do we need the app bar and
3968
02:34:25,359 --> 02:34:29,120
typography from add material ui forward
3969
02:34:29,120 --> 02:34:30,560
slash core
3970
02:34:30,560 --> 02:34:32,800
and then finally we also need our
3971
02:34:32,800 --> 02:34:35,359
classes so to do that i'm going to
3972
02:34:35,359 --> 02:34:38,720
switch this back to the normal return
3973
02:34:38,720 --> 02:34:39,760
function
3974
02:34:39,760 --> 02:34:41,200
right there
3975
02:34:41,200 --> 02:34:46,000
and what we need to say is const classes
3976
02:34:46,000 --> 02:34:48,960
is equal to use styles
3977
02:34:48,960 --> 02:34:50,800
of course right now we don't have the
3978
02:34:50,800 --> 02:34:53,439
styles but we have to import them so
3979
02:34:53,439 --> 02:34:55,840
again as we always do i'm going to give
3980
02:34:55,840 --> 02:34:58,240
you all the styles right away right here
3981
02:34:58,240 --> 02:34:59,840
you can simply copy and paste them
3982
02:34:59,840 --> 02:35:01,200
they're going to be down in the
3983
02:35:01,200 --> 02:35:03,680
description for absolutely all of the
3984
02:35:03,680 --> 02:35:06,160
components now at the top of the file we
3985
02:35:06,160 --> 02:35:10,399
can say import use styles
3986
02:35:10,399 --> 02:35:12,399
from and that's going to be just dot
3987
02:35:12,399 --> 02:35:14,000
slash styles
3988
02:35:14,000 --> 02:35:15,520
a lot of you guys mentioned that you
3989
02:35:15,520 --> 02:35:17,040
like these applications and that you
3990
02:35:17,040 --> 02:35:19,280
like building everything out and even
3991
02:35:19,280 --> 02:35:21,120
though you prefer that i simply copy and
3992
02:35:21,120 --> 02:35:23,520
paste the styles for simplicity and time
3993
02:35:23,520 --> 02:35:25,520
saving purposes you'd want to learn a
3994
02:35:25,520 --> 02:35:27,600
bit more about material ui and how to
3995
02:35:27,600 --> 02:35:30,399
styles things with it so what do you say
3996
02:35:30,399 --> 02:35:32,399
do you guys want me to make a separate
3997
02:35:32,399 --> 02:35:34,160
material ui video where i'm going to
3998
02:35:34,160 --> 02:35:36,319
teach you how to do everything material
3999
02:35:36,319 --> 02:35:38,800
ui and also how to do the styles let me
4000
02:35:38,800 --> 02:35:40,640
know in the comments down below
4001
02:35:40,640 --> 02:35:42,240
okay with that said looks like we are
4002
02:35:42,240 --> 02:35:44,080
missing one thing from our navbar and
4003
02:35:44,080 --> 02:35:46,160
that is the link to the memories image
4004
02:35:46,160 --> 02:35:48,240
so if you go back to the app you can see
4005
02:35:48,240 --> 02:35:50,240
that we simply need to import it i'm
4006
02:35:50,240 --> 02:35:52,319
going to copy it from here
4007
02:35:52,319 --> 02:35:54,160
also i'm going to remove these things
4008
02:35:54,160 --> 02:35:55,680
right there because we don't need them
4009
02:35:55,680 --> 02:35:56,720
anymore
4010
02:35:56,720 --> 02:35:58,960
and now in here we have to import that
4011
02:35:58,960 --> 02:35:59,760
path
4012
02:35:59,760 --> 02:36:01,439
now the pad is going to be just a bit
4013
02:36:01,439 --> 02:36:04,000
different it is going to be dot slash
4014
02:36:04,000 --> 02:36:06,240
and then dot slash
4015
02:36:06,240 --> 02:36:08,399
images memories png
4016
02:36:08,399 --> 02:36:10,399
okay with this we haven't done anything
4017
02:36:10,399 --> 02:36:14,240
special uh we just removed our navbar so
4018
02:36:14,240 --> 02:36:16,000
to bring it back we have to go to the
4019
02:36:16,000 --> 02:36:18,800
app.js and then in here we need to
4020
02:36:18,800 --> 02:36:20,880
import that nav bar
4021
02:36:20,880 --> 02:36:22,720
if you simply start typing in visual
4022
02:36:22,720 --> 02:36:24,960
studio code it's going to immediately
4023
02:36:24,960 --> 02:36:26,880
import that for you so that's really
4024
02:36:26,880 --> 02:36:27,920
handy
4025
02:36:27,920 --> 02:36:30,240
i'm going to put it right there and that
4026
02:36:30,240 --> 02:36:33,359
is going to be a self-closing component
4027
02:36:33,359 --> 02:36:35,120
if it didn't import it for you just make
4028
02:36:35,120 --> 02:36:37,439
sure to say import navbar dot slash
4029
02:36:37,439 --> 02:36:40,560
components and then nav bar nav bar
4030
02:36:40,560 --> 02:36:42,720
okay with that said if we go back you
4031
02:36:42,720 --> 02:36:44,560
can see that we have everything we had
4032
02:36:44,560 --> 02:36:46,080
so far
4033
02:36:46,080 --> 02:36:47,680
so why did we even put it in a new
4034
02:36:47,680 --> 02:36:49,439
component in the first place
4035
02:36:49,439 --> 02:36:50,800
well now we're going to make it more
4036
02:36:50,800 --> 02:36:52,640
complicated so we didn't want to clutter
4037
02:36:52,640 --> 02:36:54,560
the app we want to have everything in
4038
02:36:54,560 --> 02:36:57,280
its special components okay so what else
4039
02:36:57,280 --> 02:36:59,359
do we need in this navbar well we're
4040
02:36:59,359 --> 02:37:01,359
going to change it a bit so inside of
4041
02:37:01,359 --> 02:37:04,640
here i'm going to create a new div
4042
02:37:04,640 --> 02:37:07,280
that div is going to have a class name
4043
02:37:07,280 --> 02:37:11,200
of classes dot brand container and we're
4044
02:37:11,200 --> 02:37:13,120
going to put all of these things inside
4045
02:37:13,120 --> 02:37:15,439
of it so now we have a div and we have a
4046
02:37:15,439 --> 02:37:18,160
typography and image right there this
4047
02:37:18,160 --> 02:37:20,640
typography is going to be of a component
4048
02:37:20,640 --> 02:37:22,960
which is equal to link because it's
4049
02:37:22,960 --> 02:37:25,280
going to be pointing to our home so we
4050
02:37:25,280 --> 02:37:28,880
can say to and we can just say
4051
02:37:28,880 --> 02:37:31,040
slash because it's going to be our main
4052
02:37:31,040 --> 02:37:32,479
route
4053
02:37:32,479 --> 02:37:35,359
and of course we have to import
4054
02:37:35,359 --> 02:37:36,479
link
4055
02:37:36,479 --> 02:37:37,439
from
4056
02:37:37,439 --> 02:37:38,479
react
4057
02:37:38,479 --> 02:37:39,680
router
4058
02:37:39,680 --> 02:37:41,280
dom
4059
02:37:41,280 --> 02:37:43,439
also i'm not sure if installed react
4060
02:37:43,439 --> 02:37:46,000
router dom looks like i missed that so
4061
02:37:46,000 --> 02:37:48,080
what we have to do is open our terminal
4062
02:37:48,080 --> 02:37:50,399
one more time we have to close this from
4063
02:37:50,399 --> 02:37:53,280
running by pressing ctrl c and then y
4064
02:37:53,280 --> 02:37:55,920
and you can simply say npm install
4065
02:37:55,920 --> 02:37:57,120
react
4066
02:37:57,120 --> 02:38:00,399
router dom yep we also need that because
4067
02:38:00,399 --> 02:38:02,479
now we're gonna have a complete app
4068
02:38:02,479 --> 02:38:04,800
where we're gonna route things
4069
02:38:04,800 --> 02:38:06,800
so you can wait until that installs and
4070
02:38:06,800 --> 02:38:08,800
we will simply restart the application
4071
02:38:08,800 --> 02:38:11,439
by running npm start great now we have
4072
02:38:11,439 --> 02:38:13,680
the react router installed as well and
4073
02:38:13,680 --> 02:38:16,080
that's not going to be a problem okay
4074
02:38:16,080 --> 02:38:18,160
that's going to be it for our typography
4075
02:38:18,160 --> 02:38:19,840
and that's also going to be it for our
4076
02:38:19,840 --> 02:38:23,280
image so moving below just below our div
4077
02:38:23,280 --> 02:38:25,200
we can create a new component which is
4078
02:38:25,200 --> 02:38:27,600
going to be called toolbar
4079
02:38:27,600 --> 02:38:30,000
that toolbar is going to have a class
4080
02:38:30,000 --> 02:38:30,960
name
4081
02:38:30,960 --> 02:38:32,560
of classes
4082
02:38:32,560 --> 02:38:34,399
dot toolbar
4083
02:38:34,399 --> 02:38:36,160
and down below we're going to show some
4084
02:38:36,160 --> 02:38:38,399
logic depending on if we have the user
4085
02:38:38,399 --> 02:38:41,200
logged in or if you don't so right now
4086
02:38:41,200 --> 02:38:43,600
i'm going to create just a mock user so
4087
02:38:43,600 --> 02:38:46,560
we can say const user is equal to null
4088
02:38:46,560 --> 02:38:48,399
later this can be filled with an object
4089
02:38:48,399 --> 02:38:50,560
with a real user or maybe the user is
4090
02:38:50,560 --> 02:38:51,920
not logged in
4091
02:38:51,920 --> 02:38:54,640
so what we can do is we can do something
4092
02:38:54,640 --> 02:38:58,640
like this if user exists right if user
4093
02:38:58,640 --> 02:38:59,600
exists
4094
02:38:59,600 --> 02:39:02,080
then we're going to show something right
4095
02:39:02,080 --> 02:39:04,399
here and then we can use another ternary
4096
02:39:04,399 --> 02:39:07,359
to say else show something else right
4097
02:39:07,359 --> 02:39:09,439
here so we'll be showing something if a
4098
02:39:09,439 --> 02:39:11,200
user is logged in and something
4099
02:39:11,200 --> 02:39:13,840
different if a user is not logged in
4100
02:39:13,840 --> 02:39:15,760
if the user is logged in we want to show
4101
02:39:15,760 --> 02:39:17,760
his information so in here i'm going to
4102
02:39:17,760 --> 02:39:21,040
create a div with a class name
4103
02:39:21,040 --> 02:39:24,080
of classes.profile
4104
02:39:24,080 --> 02:39:26,319
in here we're gonna have an avatar
4105
02:39:26,319 --> 02:39:28,080
author is simply going to be a small
4106
02:39:28,080 --> 02:39:30,240
circular image displaying how a user
4107
02:39:30,240 --> 02:39:32,080
looks like or it's simply going to show
4108
02:39:32,080 --> 02:39:34,319
a letter of their first name
4109
02:39:34,319 --> 02:39:37,439
so we can say avatar class name is equal
4110
02:39:37,439 --> 02:39:40,080
to classes.avatar
4111
02:39:40,080 --> 02:39:43,200
or it's going to be classes.purple yep
4112
02:39:43,200 --> 02:39:45,840
and then we have an alt tag alt is going
4113
02:39:45,840 --> 02:39:50,280
to be equal to user.result.name
4114
02:39:50,960 --> 02:39:53,520
and then it's also going to have an src
4115
02:39:53,520 --> 02:39:57,200
so src can be equal to user dot result
4116
02:39:57,200 --> 02:40:00,319
dot image url so later on when we
4117
02:40:00,319 --> 02:40:01,920
actually get this user we want to
4118
02:40:01,920 --> 02:40:04,160
display all of these informations
4119
02:40:04,160 --> 02:40:05,920
finally if it doesn't have an image we
4120
02:40:05,920 --> 02:40:07,600
want to show the first letter of his
4121
02:40:07,600 --> 02:40:10,160
name and that is going to be user
4122
02:40:10,160 --> 02:40:12,640
but we need to open a block of logic
4123
02:40:12,640 --> 02:40:13,359
here
4124
02:40:13,359 --> 02:40:19,120
user dot result dot name dot car add and
4125
02:40:19,120 --> 02:40:21,279
simply call it and say zero so we want
4126
02:40:21,279 --> 02:40:23,279
to get the first character
4127
02:40:23,279 --> 02:40:26,319
that is it for the avatar now below we
4128
02:40:26,319 --> 02:40:28,080
want to have a typography so we're going
4129
02:40:28,080 --> 02:40:29,920
to say typography
4130
02:40:29,920 --> 02:40:33,359
the class name is going to be equal to
4131
02:40:33,359 --> 02:40:36,399
classes dot user name
4132
02:40:36,399 --> 02:40:40,720
and variant is going to be equal to h6
4133
02:40:40,720 --> 02:40:42,800
inside of there we can simply open a new
4134
02:40:42,800 --> 02:40:46,240
block of code and say user
4135
02:40:46,240 --> 02:40:48,880
dot result dot name
4136
02:40:48,880 --> 02:40:50,479
the last thing that we need to have is
4137
02:40:50,479 --> 02:40:52,399
the button to log out so i'm going to
4138
02:40:52,399 --> 02:40:54,800
create a new button
4139
02:40:54,800 --> 02:40:57,600
the variant is going to be contained
4140
02:40:57,600 --> 02:40:59,200
like this
4141
02:40:59,200 --> 02:41:02,160
and a class name is going to be equal to
4142
02:41:02,160 --> 02:41:05,680
classes dot log out
4143
02:41:05,680 --> 02:41:07,520
we are also going to have a color and
4144
02:41:07,520 --> 02:41:10,720
that color is going to be secondary
4145
02:41:10,720 --> 02:41:12,960
we need to have an on click property but
4146
02:41:12,960 --> 02:41:15,200
we're going to leave that empty for now
4147
02:41:15,200 --> 02:41:16,800
and finally we have to write what the
4148
02:41:16,800 --> 02:41:18,560
button is going to say and that is going
4149
02:41:18,560 --> 02:41:20,640
to be simply logout
4150
02:41:20,640 --> 02:41:22,399
great so this is the block of code we
4151
02:41:22,399 --> 02:41:24,080
see if we are logged in
4152
02:41:24,080 --> 02:41:26,240
if we are not we simply want to show the
4153
02:41:26,240 --> 02:41:27,760
button to log in
4154
02:41:27,760 --> 02:41:30,479
so let's create that button that is
4155
02:41:30,479 --> 02:41:32,800
going to be button it's going to be a
4156
02:41:32,800 --> 02:41:35,840
component which is equal to link
4157
02:41:35,840 --> 02:41:38,240
and it's going to have a to property
4158
02:41:38,240 --> 02:41:40,640
to forward slash auth
4159
02:41:40,640 --> 02:41:42,080
so this one is actually going to
4160
02:41:42,080 --> 02:41:44,080
redirect us to a different page where
4161
02:41:44,080 --> 02:41:46,319
we're gonna show the authentication it
4162
02:41:46,319 --> 02:41:49,279
is going to have a variant of contained
4163
02:41:49,279 --> 02:41:52,319
and a caller of primary
4164
02:41:52,319 --> 02:41:54,160
what the button is going to say is
4165
02:41:54,160 --> 02:41:57,120
simply going to be sign in so in here we
4166
02:41:57,120 --> 02:41:59,359
can say sign in
4167
02:41:59,359 --> 02:42:01,439
okay and now you can see we don't have a
4168
02:42:01,439 --> 02:42:03,200
few components imported so that's going
4169
02:42:03,200 --> 02:42:06,080
to be a toolbar and a button so right
4170
02:42:06,080 --> 02:42:09,279
here we can import the toolbar
4171
02:42:09,279 --> 02:42:11,760
and the button great so now if we save
4172
02:42:11,760 --> 02:42:14,720
that let's see how does that look like
4173
02:42:14,720 --> 02:42:16,800
okay we first got an error because we
4174
02:42:16,800 --> 02:42:19,920
are using a link outside a router
4175
02:42:19,920 --> 02:42:22,240
that means that we have to actually use
4176
02:42:22,240 --> 02:42:25,040
our react router dom to make our app
4177
02:42:25,040 --> 02:42:27,520
multi-page we make our react apps
4178
02:42:27,520 --> 02:42:30,960
multi-page using react router dom so we
4179
02:42:30,960 --> 02:42:32,800
can do that if you go back to the
4180
02:42:32,800 --> 02:42:35,359
application and now inside of here we
4181
02:42:35,359 --> 02:42:37,520
can import some of the things from react
4182
02:42:37,520 --> 02:42:39,120
router dom
4183
02:42:39,120 --> 02:42:41,520
these things are going to be browser
4184
02:42:41,520 --> 02:42:43,200
router
4185
02:42:43,200 --> 02:42:44,960
switch
4186
02:42:44,960 --> 02:42:47,520
and also route
4187
02:42:47,520 --> 02:42:48,880
from
4188
02:42:48,880 --> 02:42:51,920
react router dom
4189
02:42:51,920 --> 02:42:53,520
great
4190
02:42:53,520 --> 02:42:56,080
now inside of here i would first like to
4191
02:42:56,080 --> 02:42:58,479
move this thing into a new component
4192
02:42:58,479 --> 02:43:00,080
because we'll be adding a lot of things
4193
02:43:00,080 --> 02:43:00,960
to it
4194
02:43:00,960 --> 02:43:04,000
we want to keep our app clean
4195
02:43:04,000 --> 02:43:06,560
so i'm going to copy all of this with
4196
02:43:06,560 --> 02:43:08,880
the grow and we want to create a new
4197
02:43:08,880 --> 02:43:10,800
component so i'm going to create a new
4198
02:43:10,800 --> 02:43:13,120
folder called home
4199
02:43:13,120 --> 02:43:16,640
create a new file called home.js now
4200
02:43:16,640 --> 02:43:18,960
that we have the home js component in
4201
02:43:18,960 --> 02:43:21,600
there we can also create an rafc and
4202
02:43:21,600 --> 02:43:23,600
simply paste everything that includes
4203
02:43:23,600 --> 02:43:25,920
this grow right here
4204
02:43:25,920 --> 02:43:27,359
of course we'll have to import some
4205
02:43:27,359 --> 02:43:29,359
components so i'm going to copy the
4206
02:43:29,359 --> 02:43:31,359
imports from the app.js
4207
02:43:31,359 --> 02:43:34,560
and paste it right here container grow
4208
02:43:34,560 --> 02:43:37,120
and then grid you can also see that we
4209
02:43:37,120 --> 02:43:39,279
are missing the posts and the form so
4210
02:43:39,279 --> 02:43:41,439
let's import that
4211
02:43:41,439 --> 02:43:42,479
import
4212
02:43:42,479 --> 02:43:45,439
posts from dot slash
4213
02:43:45,439 --> 02:43:47,600
it's going to be dot slash
4214
02:43:47,600 --> 02:43:50,479
posts forward slash posts
4215
02:43:50,479 --> 02:43:51,920
like this
4216
02:43:51,920 --> 02:43:53,680
and then we also need a form so that's
4217
02:43:53,680 --> 02:43:56,720
going to be import form from
4218
02:43:56,720 --> 02:44:00,160
dot dot slash form forward slash form
4219
02:44:00,160 --> 02:44:02,560
okay now we have the form as well and
4220
02:44:02,560 --> 02:44:03,600
you can see that we're missing the
4221
02:44:03,600 --> 02:44:06,479
current id and the set current id so how
4222
02:44:06,479 --> 02:44:08,640
do we get that well if you think about
4223
02:44:08,640 --> 02:44:10,640
it we can declare it right here and not
4224
02:44:10,640 --> 02:44:12,319
have it in the app because because the
4225
02:44:12,319 --> 02:44:14,560
app shouldn't be doing any major logic
4226
02:44:14,560 --> 02:44:17,120
especially not with the posts ports are
4227
02:44:17,120 --> 02:44:19,520
only contained in the home so we can
4228
02:44:19,520 --> 02:44:22,560
actually use this dire logic and move it
4229
02:44:22,560 --> 02:44:24,399
all the way to our home
4230
02:44:24,399 --> 02:44:26,160
first let's delete this thing that we
4231
02:44:26,160 --> 02:44:28,880
copied so inside of here we no longer
4232
02:44:28,880 --> 02:44:31,279
have this what we have now is going to
4233
02:44:31,279 --> 02:44:33,200
be self-closing component which is
4234
02:44:33,200 --> 02:44:35,439
simply going to say home okay with that
4235
02:44:35,439 --> 02:44:37,840
said we can copy this entire part and we
4236
02:44:37,840 --> 02:44:41,200
can paste it in the home component
4237
02:44:41,200 --> 02:44:43,359
my eslint again turned this into an
4238
02:44:43,359 --> 02:44:45,520
instant return but i'm going to bring it
4239
02:44:45,520 --> 02:44:46,560
back
4240
02:44:46,560 --> 02:44:48,640
and create a normal return just so i can
4241
02:44:48,640 --> 02:44:50,399
put these things here
4242
02:44:50,399 --> 02:44:53,279
so let me just space that out
4243
02:44:53,279 --> 02:44:55,040
and as you can see now we have the
4244
02:44:55,040 --> 02:44:57,680
current id and the set current id right
4245
02:44:57,680 --> 02:45:00,080
there we have to import the use state
4246
02:45:00,080 --> 02:45:02,080
use effect get posts and the use
4247
02:45:02,080 --> 02:45:03,920
dispatch though so we can head to the
4248
02:45:03,920 --> 02:45:05,840
app and we can mostly get all of the
4249
02:45:05,840 --> 02:45:07,600
imports from here
4250
02:45:07,600 --> 02:45:09,760
you can see import used this patch from
4251
02:45:09,760 --> 02:45:12,640
react redux so let's copy that
4252
02:45:12,640 --> 02:45:15,279
then we need get posts we also need to
4253
02:45:15,279 --> 02:45:17,680
import that so that's going to be around
4254
02:45:17,680 --> 02:45:19,760
here
4255
02:45:19,760 --> 02:45:21,439
but the pad is going to be different
4256
02:45:21,439 --> 02:45:23,520
it's going to be dot slash
4257
02:45:23,520 --> 02:45:26,560
and then one more time dot slash
4258
02:45:26,560 --> 02:45:29,040
okay and the final thing is to import
4259
02:45:29,040 --> 02:45:30,640
use state
4260
02:45:30,640 --> 02:45:33,040
and use effect
4261
02:45:33,040 --> 02:45:35,040
from react
4262
02:45:35,040 --> 02:45:37,680
with that our component is filled and
4263
02:45:37,680 --> 02:45:39,840
now in the app we can get rid of this
4264
02:45:39,840 --> 02:45:41,439
extra logic
4265
02:45:41,439 --> 02:45:42,960
there we go take a look at how many
4266
02:45:42,960 --> 02:45:44,560
things we don't need right now to
4267
02:45:44,560 --> 02:45:46,800
clutter our app we have everything in
4268
02:45:46,800 --> 02:45:48,399
the home
4269
02:45:48,399 --> 02:45:51,680
so let me let me remove these things
4270
02:45:51,680 --> 02:45:54,319
we don't need that
4271
02:45:54,319 --> 02:45:57,680
no need for any components right now
4272
02:45:57,680 --> 02:45:59,439
and the only thing that we have is the
4273
02:45:59,439 --> 02:46:01,760
nav bar and the home and as you can see
4274
02:46:01,760 --> 02:46:04,160
we have that and i think i removed the
4275
02:46:04,160 --> 02:46:07,120
import from react router dom which we
4276
02:46:07,120 --> 02:46:09,920
are going to need yep that is right here
4277
02:46:09,920 --> 02:46:12,000
so i'm gonna bring this back
4278
02:46:12,000 --> 02:46:14,240
and now we can implement our react
4279
02:46:14,240 --> 02:46:15,600
router
4280
02:46:15,600 --> 02:46:17,760
so how can you do that
4281
02:46:17,760 --> 02:46:20,160
well you have to wrap everything in the
4282
02:46:20,160 --> 02:46:22,720
browser router so i'm gonna do that
4283
02:46:22,720 --> 02:46:25,600
right now browser router and then we
4284
02:46:25,600 --> 02:46:28,000
have our container right there then
4285
02:46:28,000 --> 02:46:29,920
we're going to have a switch statement
4286
02:46:29,920 --> 02:46:31,279
right here
4287
02:46:31,279 --> 02:46:33,920
so you can say switch and i see that i
4288
02:46:33,920 --> 02:46:35,439
didn't name it properly so i'm going to
4289
02:46:35,439 --> 02:46:37,279
rename it the switch right there and
4290
02:46:37,279 --> 02:46:39,040
then inside of the switch you need to
4291
02:46:39,040 --> 02:46:41,279
have a few routes so i'm going to create
4292
02:46:41,279 --> 02:46:42,880
our first route
4293
02:46:42,880 --> 02:46:44,880
that route is going to be self-closing
4294
02:46:44,880 --> 02:46:47,680
tag and we're going to simply say path
4295
02:46:47,680 --> 02:46:49,680
is equal to slash so this is our home
4296
02:46:49,680 --> 02:46:50,800
route
4297
02:46:50,800 --> 02:46:52,399
it needs to be exact
4298
02:46:52,399 --> 02:46:54,080
and the component
4299
02:46:54,080 --> 02:46:57,120
is going to be equal to home component
4300
02:46:57,120 --> 02:46:59,359
we have to import it right there
4301
02:46:59,359 --> 02:47:01,439
then below that we're going to add one
4302
02:47:01,439 --> 02:47:03,760
new route this one is going to have a
4303
02:47:03,760 --> 02:47:06,640
path of auth and the component is going
4304
02:47:06,640 --> 02:47:08,880
to be called auth
4305
02:47:08,880 --> 02:47:10,880
this component is not defined yet so we
4306
02:47:10,880 --> 02:47:12,240
have to create it
4307
02:47:12,240 --> 02:47:14,479
let's do that right now
4308
02:47:14,479 --> 02:47:16,240
inside of our components i'm going to
4309
02:47:16,240 --> 02:47:18,640
create a new component or rather a new
4310
02:47:18,640 --> 02:47:21,359
folder called auth
4311
02:47:21,359 --> 02:47:23,439
inside of that odd i'm going to create a
4312
02:47:23,439 --> 02:47:27,040
new file called auth.js and also we're
4313
02:47:27,040 --> 02:47:28,520
going to create
4314
02:47:28,520 --> 02:47:30,080
styles.js
4315
02:47:30,080 --> 02:47:32,640
for the odd file again i'm simply going
4316
02:47:32,640 --> 02:47:35,120
to say rafc and by the way if you don't
4317
02:47:35,120 --> 02:47:38,080
have that that is going to be a package
4318
02:47:38,080 --> 02:47:40,319
uh let me take a look it's going to be
4319
02:47:40,319 --> 02:47:43,439
es7 react redux draftql snippets so you
4320
02:47:43,439 --> 02:47:44,720
can download that that's going to
4321
02:47:44,720 --> 02:47:47,760
autofill the rafc for you
4322
02:47:47,760 --> 02:47:50,160
okay and then in here for now i'm simply
4323
02:47:50,160 --> 02:47:53,120
going to say auth that is going to be it
4324
02:47:53,120 --> 02:47:56,080
okay and i'm using eslint and i don't
4325
02:47:56,080 --> 02:47:57,600
want this to be changed on instant
4326
02:47:57,600 --> 02:47:59,439
return because later on we will need
4327
02:47:59,439 --> 02:48:01,279
something i'm just going to say const
4328
02:48:01,279 --> 02:48:03,760
state is equal to null here
4329
02:48:03,760 --> 02:48:05,840
okay that way everything is going to be
4330
02:48:05,840 --> 02:48:08,080
centered like this
4331
02:48:08,080 --> 02:48:10,160
okay with that we now have to import
4332
02:48:10,160 --> 02:48:12,080
this component and again if you simply
4333
02:48:12,080 --> 02:48:14,240
type it and click right there it's going
4334
02:48:14,240 --> 02:48:16,479
to be imported right for you
4335
02:48:16,479 --> 02:48:18,880
okay so with that our routing should be
4336
02:48:18,880 --> 02:48:21,120
done as you can see we have our nav bar
4337
02:48:21,120 --> 02:48:22,960
it's not inside of the switch so that
4338
02:48:22,960 --> 02:48:25,279
means it's going to be showing up always
4339
02:48:25,279 --> 02:48:27,520
and then we have our switch which means
4340
02:48:27,520 --> 02:48:29,359
switch between these two components
4341
02:48:29,359 --> 02:48:32,160
either show the home or show the auth
4342
02:48:32,160 --> 02:48:34,000
and of course we have to remove this
4343
02:48:34,000 --> 02:48:35,760
home from the bottom because we are
4344
02:48:35,760 --> 02:48:37,920
showing it right here
4345
02:48:37,920 --> 02:48:40,319
okay now can we actually see this in the
4346
02:48:40,319 --> 02:48:42,640
browser let's test it out and there we
4347
02:48:42,640 --> 02:48:45,040
go everything is as it used to be but we
4348
02:48:45,040 --> 02:48:46,560
made a few different components we
4349
02:48:46,560 --> 02:48:48,720
created the one for the navbar and we
4350
02:48:48,720 --> 02:48:50,640
created a home component with the form
4351
02:48:50,640 --> 02:48:52,080
and all the posts
4352
02:48:52,080 --> 02:48:53,920
now let's try clicking the sign in and
4353
02:48:53,920 --> 02:48:55,760
see what happens
4354
02:48:55,760 --> 02:48:57,600
and there we go we are routed to a
4355
02:48:57,600 --> 02:48:59,520
completely different component that now
4356
02:48:59,520 --> 02:49:01,920
says auth this is the place where we're
4357
02:49:01,920 --> 02:49:03,840
going to implement the login and
4358
02:49:03,840 --> 02:49:05,520
registration form
4359
02:49:05,520 --> 02:49:07,840
that seems like a valid next step so
4360
02:49:07,840 --> 02:49:10,479
let's do that next okay let's start
4361
02:49:10,479 --> 02:49:13,439
creating the layout for our auth form
4362
02:49:13,439 --> 02:49:15,600
inside of the auth.js we're going to
4363
02:49:15,600 --> 02:49:18,319
first import a few things and these are
4364
02:49:18,319 --> 02:49:21,200
going to be again avatar
4365
02:49:21,200 --> 02:49:22,319
button
4366
02:49:22,319 --> 02:49:23,520
paper
4367
02:49:23,520 --> 02:49:24,720
grid
4368
02:49:24,720 --> 02:49:26,720
then we're going to need a typography
4369
02:49:26,720 --> 02:49:29,439
and finally a container
4370
02:49:29,439 --> 02:49:30,800
all of these are going to be imported
4371
02:49:30,800 --> 02:49:34,560
from add material ui forward slash core
4372
02:49:34,560 --> 02:49:36,560
while we're here we can also import the
4373
02:49:36,560 --> 02:49:40,880
styles meaning use styles from dot slash
4374
02:49:40,880 --> 02:49:42,080
styles
4375
02:49:42,080 --> 02:49:44,319
and of course we have to create those so
4376
02:49:44,319 --> 02:49:46,479
again linked in the description are
4377
02:49:46,479 --> 02:49:48,720
going to be the styles for this specific
4378
02:49:48,720 --> 02:49:49,760
file
4379
02:49:49,760 --> 02:49:51,680
i don't want you to spend time on
4380
02:49:51,680 --> 02:49:54,240
writing css i want you to focus on the
4381
02:49:54,240 --> 02:49:56,399
important things which is in this case
4382
02:49:56,399 --> 02:49:57,920
authentication
4383
02:49:57,920 --> 02:50:00,319
okay with that said we can say const
4384
02:50:00,319 --> 02:50:04,080
classes is equal to use styles
4385
02:50:04,080 --> 02:50:06,080
and call it as a function
4386
02:50:06,080 --> 02:50:08,399
now we can start creating the layout for
4387
02:50:08,399 --> 02:50:10,960
our odd form and it's going to look like
4388
02:50:10,960 --> 02:50:13,120
this first of all we're going to wrap
4389
02:50:13,120 --> 02:50:14,160
everything
4390
02:50:14,160 --> 02:50:16,080
inside of a container
4391
02:50:16,080 --> 02:50:18,080
that container is going to have a
4392
02:50:18,080 --> 02:50:19,920
component property which is going to be
4393
02:50:19,920 --> 02:50:22,240
equal to a string of main it's also
4394
02:50:22,240 --> 02:50:24,399
going to have a max width property which
4395
02:50:24,399 --> 02:50:27,520
is going to be equal to xs
4396
02:50:27,520 --> 02:50:29,200
down below we're going to wrap
4397
02:50:29,200 --> 02:50:31,680
everything with a paper so we can say
4398
02:50:31,680 --> 02:50:34,240
paper and that paper is going to have a
4399
02:50:34,240 --> 02:50:37,760
class name of classes
4400
02:50:37,760 --> 02:50:41,760
dot paper and also an elevation of three
4401
02:50:41,760 --> 02:50:43,840
this is simply going to make a white
4402
02:50:43,840 --> 02:50:46,080
style div that looks like a paper and
4403
02:50:46,080 --> 02:50:48,080
there we can have all the inputs inside
4404
02:50:48,080 --> 02:50:48,840
of
4405
02:50:48,840 --> 02:50:51,760
that now first thing inside of that is
4406
02:50:51,760 --> 02:50:53,920
we're gonna have an avatar and that
4407
02:50:53,920 --> 02:50:58,479
avatar is going to have a class name
4408
02:50:58,880 --> 02:51:02,160
of classes.avatar
4409
02:51:02,160 --> 02:51:03,840
finally inside of there we're going to
4410
02:51:03,840 --> 02:51:07,600
have a lock icon so right inside of here
4411
02:51:07,600 --> 02:51:09,760
you can create something like this
4412
02:51:09,760 --> 02:51:13,040
lock outlined icon and that is going to
4413
02:51:13,040 --> 02:51:14,960
be a self-closing tag that we have to
4414
02:51:14,960 --> 02:51:17,760
import so at the top let's import that
4415
02:51:17,760 --> 02:51:18,960
icon
4416
02:51:18,960 --> 02:51:21,439
it's simply going to say import
4417
02:51:21,439 --> 02:51:23,760
lock outlined icon
4418
02:51:23,760 --> 02:51:25,120
from
4419
02:51:25,120 --> 02:51:27,120
and in this case it's not going to be
4420
02:51:27,120 --> 02:51:29,439
add material ui core it's going to be
4421
02:51:29,439 --> 02:51:32,319
add material ui forward slash icons and
4422
02:51:32,319 --> 02:51:33,840
then forward slash
4423
02:51:33,840 --> 02:51:35,040
lock
4424
02:51:35,040 --> 02:51:37,040
outlined
4425
02:51:37,040 --> 02:51:38,479
there we go
4426
02:51:38,479 --> 02:51:40,080
now that we have that we can continue
4427
02:51:40,080 --> 02:51:41,920
with our form
4428
02:51:41,920 --> 02:51:44,160
below the avatar we're going to have one
4429
02:51:44,160 --> 02:51:46,640
typography this typography is going to
4430
02:51:46,640 --> 02:51:50,399
be of a variant which is going to be h5
4431
02:51:50,399 --> 02:51:53,120
and it simply needs to say either sign
4432
02:51:53,120 --> 02:51:56,880
up or sign in depending on what we want
4433
02:51:56,880 --> 02:51:59,840
to do so right now i'm going to create
4434
02:51:59,840 --> 02:52:02,319
once again a mock variable for now which
4435
02:52:02,319 --> 02:52:03,840
is going to be called
4436
02:52:03,840 --> 02:52:04,640
is
4437
02:52:04,640 --> 02:52:05,840
sign up
4438
02:52:05,840 --> 02:52:07,359
and we're going to leave that to false
4439
02:52:07,359 --> 02:52:08,479
right now
4440
02:52:08,479 --> 02:52:10,160
based on this variable we're going to
4441
02:52:10,160 --> 02:52:12,319
change a lot of different things in the
4442
02:52:12,319 --> 02:52:14,960
form so right now we can open a new
4443
02:52:14,960 --> 02:52:18,960
logic block and we can say if is sign up
4444
02:52:18,960 --> 02:52:20,960
then we want to show something like a
4445
02:52:20,960 --> 02:52:23,680
string of sign up
4446
02:52:23,680 --> 02:52:26,399
otherwise we want to show sign
4447
02:52:26,399 --> 02:52:27,359
in
4448
02:52:27,359 --> 02:52:29,359
now below that we're finally starting
4449
02:52:29,359 --> 02:52:30,720
with our form
4450
02:52:30,720 --> 02:52:33,279
our form is going to have a class name
4451
02:52:33,279 --> 02:52:36,240
of classes dot form
4452
02:52:36,240 --> 02:52:39,359
and it's also going to have an on submit
4453
02:52:39,359 --> 02:52:41,279
right now we don't have announce a bit
4454
02:52:41,279 --> 02:52:43,359
but we could as well create it so i'm
4455
02:52:43,359 --> 02:52:45,359
going to say on submit the function
4456
02:52:45,359 --> 02:52:46,800
we're going to call is going to be
4457
02:52:46,800 --> 02:52:49,680
called handle submit so just right here
4458
02:52:49,680 --> 02:52:52,640
you can create a const handle submit
4459
02:52:52,640 --> 02:52:53,760
function
4460
02:52:53,760 --> 02:52:55,680
there we go for now we're going to leave
4461
02:52:55,680 --> 02:52:56,800
it blank
4462
02:52:56,800 --> 02:52:58,960
now inside of our form we're going to
4463
02:52:58,960 --> 02:53:01,040
have a grid
4464
02:53:01,040 --> 02:53:02,800
that grid is going to be of a type
4465
02:53:02,800 --> 02:53:04,640
container and it's going to have a
4466
02:53:04,640 --> 02:53:07,200
spacing equal to 2.
4467
02:53:07,200 --> 02:53:09,359
inside of here we can show all of our
4468
02:53:09,359 --> 02:53:11,200
inputs so let's first start with the
4469
02:53:11,200 --> 02:53:14,240
first name and the last name remember we
4470
02:53:14,240 --> 02:53:15,920
only have the first name and the last
4471
02:53:15,920 --> 02:53:18,000
name if we are signing up if we're
4472
02:53:18,000 --> 02:53:20,880
already signed in we don't need that so
4473
02:53:20,880 --> 02:53:23,680
we can open a new blog and we can say if
4474
02:53:23,680 --> 02:53:25,359
is sign up
4475
02:53:25,359 --> 02:53:27,520
then we could use the ternary but we can
4476
02:53:27,520 --> 02:53:29,600
also use the end and operator so we can
4477
02:53:29,600 --> 02:53:32,800
say is sign up only if it is sign up
4478
02:53:32,800 --> 02:53:35,279
then show something and in this case
4479
02:53:35,279 --> 02:53:36,800
we're going to show an empty react
4480
02:53:36,800 --> 02:53:39,200
fragment and then inside of there we're
4481
02:53:39,200 --> 02:53:41,359
going to show two different inputs so
4482
02:53:41,359 --> 02:53:42,800
these are going to be text field
4483
02:53:42,800 --> 02:53:44,160
elements
4484
02:53:44,160 --> 02:53:46,800
we can say text field
4485
02:53:46,800 --> 02:53:48,240
like that
4486
02:53:48,240 --> 02:53:50,000
and we're going to have a few different
4487
02:53:50,000 --> 02:53:52,080
things there we need to have a name so
4488
02:53:52,080 --> 02:53:54,560
name is going to be equal to
4489
02:53:54,560 --> 02:53:57,120
first name
4490
02:53:57,120 --> 02:53:59,680
then we also need a label which is going
4491
02:53:59,680 --> 02:54:02,640
to be equal to first name
4492
02:54:02,640 --> 02:54:04,960
we need a handle change method so i'm
4493
02:54:04,960 --> 02:54:07,600
going to say handle change here
4494
02:54:07,600 --> 02:54:09,279
we can go ahead and create that handle
4495
02:54:09,279 --> 02:54:11,359
change right now handle change is going
4496
02:54:11,359 --> 02:54:13,840
to simply be equal to handle change so
4497
02:54:13,840 --> 02:54:16,160
let's create an arrow function here
4498
02:54:16,160 --> 02:54:18,880
const handle change
4499
02:54:18,880 --> 02:54:20,319
is going to be equal to an arrow
4500
02:54:20,319 --> 02:54:22,000
function
4501
02:54:22,000 --> 02:54:25,040
okay what else do we need on our input
4502
02:54:25,040 --> 02:54:28,000
we need also the auto focus because this
4503
02:54:28,000 --> 02:54:29,600
is going to be the first element we want
4504
02:54:29,600 --> 02:54:32,479
to focus on and then we can say xs is
4505
02:54:32,479 --> 02:54:33,600
equal to
4506
02:54:33,600 --> 02:54:34,560
6
4507
02:54:34,560 --> 02:54:36,319
and we'll need a lot more things i'm
4508
02:54:36,319 --> 02:54:38,240
going to explain why this is going to be
4509
02:54:38,240 --> 02:54:40,399
different in just a second so this is
4510
02:54:40,399 --> 02:54:42,880
going to be a self closing tag there we
4511
02:54:42,880 --> 02:54:44,160
go
4512
02:54:44,160 --> 02:54:45,760
now of course we're going to have one
4513
02:54:45,760 --> 02:54:47,920
more for the last name we have to change
4514
02:54:47,920 --> 02:54:49,760
all of these things
4515
02:54:49,760 --> 02:54:51,439
then we're going to have multiple more
4516
02:54:51,439 --> 02:54:53,760
text fields for passwords for confirming
4517
02:54:53,760 --> 02:54:56,160
passwords for email and all of these are
4518
02:54:56,160 --> 02:54:58,240
going to be kind of different but in
4519
02:54:58,240 --> 02:55:00,560
most cases they are going to stay the
4520
02:55:00,560 --> 02:55:03,040
same so we don't want to have the inputs
4521
02:55:03,040 --> 02:55:05,680
that have like 10 different props
4522
02:55:05,680 --> 02:55:07,840
what you want to do when you have most
4523
02:55:07,840 --> 02:55:09,600
of the same components
4524
02:55:09,600 --> 02:55:11,120
most of the components that look and
4525
02:55:11,120 --> 02:55:13,439
behave the same is you want to create a
4526
02:55:13,439 --> 02:55:15,760
new custom component which is going to
4527
02:55:15,760 --> 02:55:18,399
generalize the logic that way you don't
4528
02:55:18,399 --> 02:55:20,399
have to write everything for each one of
4529
02:55:20,399 --> 02:55:22,319
them you can simply create a one that
4530
02:55:22,319 --> 02:55:24,720
has all the things and then only specify
4531
02:55:24,720 --> 02:55:26,479
the things that change
4532
02:55:26,479 --> 02:55:28,160
it might be complicated but let me show
4533
02:55:28,160 --> 02:55:30,160
you what i mean this is a really really
4534
02:55:30,160 --> 02:55:32,000
good practice in react
4535
02:55:32,000 --> 02:55:34,160
so inside of here instead of our auth
4536
02:55:34,160 --> 02:55:36,479
i'm going to create a new file called
4537
02:55:36,479 --> 02:55:39,600
input dot js
4538
02:55:39,600 --> 02:55:42,800
that input is going to be an rafce a
4539
02:55:42,800 --> 02:55:44,720
simple component and we're going to
4540
02:55:44,720 --> 02:55:47,600
import a few things from material ui
4541
02:55:47,600 --> 02:55:50,479
these things are going to be text field
4542
02:55:50,479 --> 02:55:52,560
we're going to import a grid
4543
02:55:52,560 --> 02:55:56,560
we're going to import an input adornment
4544
02:55:56,560 --> 02:55:59,680
and we're also going to import the icon
4545
02:55:59,680 --> 02:56:00,720
button
4546
02:56:00,720 --> 02:56:03,840
from react material ui and that's going
4547
02:56:03,840 --> 02:56:05,359
to be core
4548
02:56:05,359 --> 02:56:08,080
okay great and i also forgot like in
4549
02:56:08,080 --> 02:56:10,479
here we don't only need a text field we
4550
02:56:10,479 --> 02:56:13,680
need to wrap each text field with a grid
4551
02:56:13,680 --> 02:56:16,000
so it would look something like this
4552
02:56:16,000 --> 02:56:18,160
and then each grid would have a property
4553
02:56:18,160 --> 02:56:22,479
of xs is equal to 6 and then md is equal
4554
02:56:22,479 --> 02:56:24,800
to 12 or something like that so you can
4555
02:56:24,800 --> 02:56:27,359
see here we have this grid once but then
4556
02:56:27,359 --> 02:56:30,319
again you need to have it for each new
4557
02:56:30,319 --> 02:56:32,640
text field and you can see it it kind of
4558
02:56:32,640 --> 02:56:35,279
adds a lot of code it makes it unclean a
4559
02:56:35,279 --> 02:56:37,200
lot of repetitive code at least you can
4560
02:56:37,200 --> 02:56:38,080
see
4561
02:56:38,080 --> 02:56:40,000
so now we want to change that we want to
4562
02:56:40,000 --> 02:56:42,720
make a generalized input field
4563
02:56:42,720 --> 02:56:45,040
so to do that we can create one grid
4564
02:56:45,040 --> 02:56:47,279
right there and all of our inputs are
4565
02:56:47,279 --> 02:56:49,120
going to have that grid that's going to
4566
02:56:49,120 --> 02:56:50,800
save us the trouble of writing this
4567
02:56:50,800 --> 02:56:52,800
multiple times
4568
02:56:52,800 --> 02:56:55,520
this grid is going to be of type item it
4569
02:56:55,520 --> 02:56:57,840
is going to have the property of xs
4570
02:56:57,840 --> 02:57:00,240
equal to 12 but what if we want some
4571
02:57:00,240 --> 02:57:02,640
inputs to take only half the width
4572
02:57:02,640 --> 02:57:04,640
then we can do something like sm is
4573
02:57:04,640 --> 02:57:07,040
equal to 6 but now all of them are going
4574
02:57:07,040 --> 02:57:09,439
to take half the width so we have to
4575
02:57:09,439 --> 02:57:11,680
have a special prop we can name it
4576
02:57:11,680 --> 02:57:13,840
something like half
4577
02:57:13,840 --> 02:57:16,319
and then if half is true we want this to
4578
02:57:16,319 --> 02:57:19,600
be 6 otherwise we want this to be 12. so
4579
02:57:19,600 --> 02:57:22,080
see how we can make it dynamic
4580
02:57:22,080 --> 02:57:24,800
okay now inside of there we want to have
4581
02:57:24,800 --> 02:57:27,520
a text field text field belongs to
4582
02:57:27,520 --> 02:57:29,760
material ui and it is going to be a
4583
02:57:29,760 --> 02:57:31,600
self-closing tag
4584
02:57:31,600 --> 02:57:33,680
i'm going to expand it on multiple rows
4585
02:57:33,680 --> 02:57:36,319
so we can add our props inside of there
4586
02:57:36,319 --> 02:57:38,479
each text field needs to have a name so
4587
02:57:38,479 --> 02:57:40,479
i'm going to say name is equal to name
4588
02:57:40,479 --> 02:57:42,160
right there because name needs to be
4589
02:57:42,160 --> 02:57:43,359
dynamic
4590
02:57:43,359 --> 02:57:45,120
as you can see right now we don't have a
4591
02:57:45,120 --> 02:57:46,960
name so all of these things we have to
4592
02:57:46,960 --> 02:57:49,040
import as props
4593
02:57:49,040 --> 02:57:50,720
so inside of here i'm going to add a
4594
02:57:50,720 --> 02:57:52,240
name
4595
02:57:52,240 --> 02:57:54,399
then each text field needs to have an
4596
02:57:54,399 --> 02:57:56,319
unchanged property so i'm going to say
4597
02:57:56,319 --> 02:57:59,439
on change is equal to handle
4598
02:57:59,439 --> 02:58:00,960
change
4599
02:58:00,960 --> 02:58:03,600
then we have a variant that is equal to
4600
02:58:03,600 --> 02:58:06,240
outlined this is the same for absolutely
4601
02:58:06,240 --> 02:58:08,319
all text fields so we don't have to
4602
02:58:08,319 --> 02:58:10,240
change that another thing that is the
4603
02:58:10,240 --> 02:58:12,160
same for all text fields is going to be
4604
02:58:12,160 --> 02:58:14,640
required we don't have to send that as a
4605
02:58:14,640 --> 02:58:16,720
prop that is always going to say the
4606
02:58:16,720 --> 02:58:19,520
same all of them are going to have the
4607
02:58:19,520 --> 02:58:21,840
property of full width
4608
02:58:21,840 --> 02:58:23,840
then for the label we have to change
4609
02:58:23,840 --> 02:58:26,160
that so i'm going to say label is equal
4610
02:58:26,160 --> 02:58:28,399
to label and we're going to pass that as
4611
02:58:28,399 --> 02:58:30,000
a prop
4612
02:58:30,000 --> 02:58:33,040
finally auto focus is going to be equal
4613
02:58:33,040 --> 02:58:34,880
to autofocus
4614
02:58:34,880 --> 02:58:38,160
and type is going to be equal to type
4615
02:58:38,160 --> 02:58:39,920
and one final thing that's going to be
4616
02:58:39,920 --> 02:58:41,760
happening only on one input is going to
4617
02:58:41,760 --> 02:58:44,080
be called input props like this with a
4618
02:58:44,080 --> 02:58:47,200
capital i capital props that is simply
4619
02:58:47,200 --> 02:58:49,120
an icon that we're showing on the right
4620
02:58:49,120 --> 02:58:51,840
side of the input if we want to click it
4621
02:58:51,840 --> 02:58:54,240
and that's going to be for show password
4622
02:58:54,240 --> 02:58:55,439
you know that field when you have a
4623
02:58:55,439 --> 02:58:57,040
password then you you're not sure what
4624
02:58:57,040 --> 02:58:58,960
you typed in you want to click that and
4625
02:58:58,960 --> 02:59:00,960
then it's going to show it to you
4626
02:59:00,960 --> 02:59:03,600
so we can open a new block and then we
4627
02:59:03,600 --> 02:59:06,479
can say if name is equal to password
4628
02:59:06,479 --> 02:59:07,760
like this
4629
02:59:07,760 --> 02:59:09,920
then we want to show a block of code so
4630
02:59:09,920 --> 02:59:12,840
you can open this and say end
4631
02:59:12,840 --> 02:59:15,439
adornment is equal to
4632
02:59:15,439 --> 02:59:17,600
and in here you can use the input
4633
02:59:17,600 --> 02:59:20,080
adornment that's going to be
4634
02:59:20,080 --> 02:59:21,920
a component
4635
02:59:21,920 --> 02:59:24,720
the position is going to be set to end
4636
02:59:24,720 --> 02:59:27,200
so position set to end
4637
02:59:27,200 --> 02:59:29,200
and all that we need is going to be an
4638
02:59:29,200 --> 02:59:30,240
icon
4639
02:59:30,240 --> 02:59:32,000
button
4640
02:59:32,000 --> 02:59:34,080
that icon button is going to have an on
4641
02:59:34,080 --> 02:59:35,760
click property which is going to be
4642
02:59:35,760 --> 02:59:37,520
equal to handle
4643
02:59:37,520 --> 02:59:38,319
show
4644
02:59:38,319 --> 02:59:40,240
password
4645
02:59:40,240 --> 02:59:44,399
and in there we're going to show if type
4646
02:59:44,399 --> 02:59:46,000
is equal to
4647
02:59:46,000 --> 02:59:48,080
password so if type triple equal to
4648
02:59:48,080 --> 02:59:50,640
password
4649
02:59:50,640 --> 02:59:52,640
then we're going to show an icon that
4650
02:59:52,640 --> 02:59:56,040
says visibility
4651
02:59:56,399 --> 02:59:58,240
a self-closing tag
4652
02:59:58,240 --> 03:00:00,160
otherwise we're gonna have an icon that
4653
03:00:00,160 --> 03:00:01,920
says visibility
4654
03:00:01,920 --> 03:00:02,800
off
4655
03:00:02,800 --> 03:00:04,720
and it's also going to be a self-closing
4656
03:00:04,720 --> 03:00:06,000
tag
4657
03:00:06,000 --> 03:00:08,160
we can now this is going to be the end
4658
03:00:08,160 --> 03:00:10,640
adornment but if we don't have that we
4659
03:00:10,640 --> 03:00:14,160
can simply say null don't show anything
4660
03:00:14,160 --> 03:00:16,160
and if you know react or javascript you
4661
03:00:16,160 --> 03:00:17,600
know that when we have a ternary
4662
03:00:17,600 --> 03:00:19,439
operator with the second value being
4663
03:00:19,439 --> 03:00:21,680
null what you can do is simply change
4664
03:00:21,680 --> 03:00:24,000
this to end end and then you don't have
4665
03:00:24,000 --> 03:00:26,720
to provide that null right there
4666
03:00:26,720 --> 03:00:28,399
great so if you save that you'll see
4667
03:00:28,399 --> 03:00:30,000
that we're missing quite a few things
4668
03:00:30,000 --> 03:00:31,680
that we need to import here
4669
03:00:31,680 --> 03:00:33,439
and this is actually good because this
4670
03:00:33,439 --> 03:00:35,120
means that we are taking all of the
4671
03:00:35,120 --> 03:00:36,960
things that we need in here for this
4672
03:00:36,960 --> 03:00:38,960
special input and we are importing them
4673
03:00:38,960 --> 03:00:42,479
right here and not in our auth because
4674
03:00:42,479 --> 03:00:44,399
in that way we're cluttering our odd
4675
03:00:44,399 --> 03:00:46,640
component which by itself is already be
4676
03:00:46,640 --> 03:00:48,000
complicated
4677
03:00:48,000 --> 03:00:50,720
so let's import those icons right there
4678
03:00:50,720 --> 03:00:52,560
import visibility
4679
03:00:52,560 --> 03:00:55,920
from add material ui icons forward slash
4680
03:00:55,920 --> 03:00:57,279
visibility
4681
03:00:57,279 --> 03:00:59,040
i'm going to copy that line and the
4682
03:00:59,040 --> 03:01:00,720
second line is simply going to say
4683
03:01:00,720 --> 03:01:03,120
visibility
4684
03:01:03,120 --> 03:01:05,520
off great now all of the other things
4685
03:01:05,520 --> 03:01:07,600
that we need like handle change label
4686
03:01:07,600 --> 03:01:10,399
autofocus we're importing from here so
4687
03:01:10,399 --> 03:01:11,680
let's do that
4688
03:01:11,680 --> 03:01:12,640
name
4689
03:01:12,640 --> 03:01:15,760
handle change we need a label we need a
4690
03:01:15,760 --> 03:01:16,960
half
4691
03:01:16,960 --> 03:01:21,120
then we need the auto focus the type and
4692
03:01:21,120 --> 03:01:24,160
finally the handle show password
4693
03:01:24,160 --> 03:01:28,000
with that our custom input is done
4694
03:01:28,000 --> 03:01:30,160
you can see we are exporting it as the
4695
03:01:30,160 --> 03:01:32,640
input finally we can import it right
4696
03:01:32,640 --> 03:01:34,720
here import
4697
03:01:34,720 --> 03:01:35,840
input
4698
03:01:35,840 --> 03:01:36,720
from
4699
03:01:36,720 --> 03:01:38,000
dot slash
4700
03:01:38,000 --> 03:01:38,960
input
4701
03:01:38,960 --> 03:01:40,399
great
4702
03:01:40,399 --> 03:01:42,800
now instead of typing all of this we can
4703
03:01:42,800 --> 03:01:45,279
remove the grids completely we can
4704
03:01:45,279 --> 03:01:46,720
remove that
4705
03:01:46,720 --> 03:01:49,120
this as well and we're going to rename
4706
03:01:49,120 --> 03:01:52,560
this to input instead of a text field
4707
03:01:52,560 --> 03:01:54,720
and now as you can see we are passing
4708
03:01:54,720 --> 03:01:56,560
only the props that we need
4709
03:01:56,560 --> 03:01:58,720
name is equal to first name label is
4710
03:01:58,720 --> 03:02:00,800
equal to first name we're passing the
4711
03:02:00,800 --> 03:02:03,359
handle change method and everything else
4712
03:02:03,359 --> 03:02:04,640
that we need
4713
03:02:04,640 --> 03:02:07,040
in this case i think we simply specify
4714
03:02:07,040 --> 03:02:09,439
the property half so we don't need to
4715
03:02:09,439 --> 03:02:12,880
say the xs is equal to six we can simply
4716
03:02:12,880 --> 03:02:13,680
say
4717
03:02:13,680 --> 03:02:16,160
half and that is going to make it six
4718
03:02:16,160 --> 03:02:18,800
and for the second one we don't need the
4719
03:02:18,800 --> 03:02:21,520
auto focus as well because we have that
4720
03:02:21,520 --> 03:02:23,680
and we can say half as you can see this
4721
03:02:23,680 --> 03:02:26,160
makes our inputs a lot slimmer we don't
4722
03:02:26,160 --> 03:02:28,319
need to have something like let me take
4723
03:02:28,319 --> 03:02:31,040
a look something like variant outline
4724
03:02:31,040 --> 03:02:33,279
for each one required for each one full
4725
03:02:33,279 --> 03:02:35,439
width for each one and all of these
4726
03:02:35,439 --> 03:02:38,479
things we simply specify what we need
4727
03:02:38,479 --> 03:02:40,319
and now we can continue with all the
4728
03:02:40,319 --> 03:02:42,800
inputs that we have both in the login
4729
03:02:42,800 --> 03:02:45,439
and the register form so let's create
4730
03:02:45,439 --> 03:02:46,160
them
4731
03:02:46,160 --> 03:02:48,399
we have a new input
4732
03:02:48,399 --> 03:02:50,319
this one is going to have the name of
4733
03:02:50,319 --> 03:02:51,920
email
4734
03:02:51,920 --> 03:02:54,080
a label of
4735
03:02:54,080 --> 03:02:55,279
email
4736
03:02:55,279 --> 03:02:56,880
address
4737
03:02:56,880 --> 03:02:59,279
and a handle change which is going to be
4738
03:02:59,279 --> 03:03:01,920
equal to handle change
4739
03:03:01,920 --> 03:03:04,479
finally in this case we need to have a
4740
03:03:04,479 --> 03:03:06,880
type and the type is going to be equal
4741
03:03:06,880 --> 03:03:08,720
to email
4742
03:03:08,720 --> 03:03:10,960
below that one we're going to create one
4743
03:03:10,960 --> 03:03:14,479
new input also a self-closing tag
4744
03:03:14,479 --> 03:03:16,720
this one is going to have a name is
4745
03:03:16,720 --> 03:03:18,880
equal to password
4746
03:03:18,880 --> 03:03:21,279
it's also going to have a label which is
4747
03:03:21,279 --> 03:03:24,160
going to be equal to password
4748
03:03:24,160 --> 03:03:26,240
and it's going to have you guessed it a
4749
03:03:26,240 --> 03:03:29,920
handle change equal to a handle change
4750
03:03:29,920 --> 03:03:31,840
the type is going to be of course
4751
03:03:31,840 --> 03:03:34,240
password but what if you want to click
4752
03:03:34,240 --> 03:03:36,720
that button which means show password in
4753
03:03:36,720 --> 03:03:38,319
that case we have to change this from
4754
03:03:38,319 --> 03:03:40,399
password to simply text
4755
03:03:40,399 --> 03:03:41,840
that means that we need to have some
4756
03:03:41,840 --> 03:03:43,920
kind of variable that's going to manage
4757
03:03:43,920 --> 03:03:46,560
our show password and that is going to
4758
03:03:46,560 --> 03:03:49,120
be a state so we can do that right now
4759
03:03:49,120 --> 03:03:52,160
we can import react as well as the use
4760
03:03:52,160 --> 03:03:54,960
state hook at the top and now we can
4761
03:03:54,960 --> 03:03:58,240
create that show password field so right
4762
03:03:58,240 --> 03:04:00,880
here i'm going to say const make a new
4763
03:04:00,880 --> 03:04:01,760
state
4764
03:04:01,760 --> 03:04:04,000
and we're going to say show password
4765
03:04:04,000 --> 03:04:05,120
and
4766
03:04:05,120 --> 03:04:06,399
set
4767
03:04:06,399 --> 03:04:07,200
show
4768
03:04:07,200 --> 03:04:09,120
password so this is going to be our
4769
03:04:09,120 --> 03:04:10,960
state
4770
03:04:10,960 --> 03:04:13,200
and that's going to be equal to use
4771
03:04:13,200 --> 03:04:14,479
state
4772
03:04:14,479 --> 03:04:16,479
and at the start it is going to be set
4773
03:04:16,479 --> 03:04:17,920
to false
4774
03:04:17,920 --> 03:04:20,880
okay so now based on this state we can
4775
03:04:20,880 --> 03:04:23,439
say if right here i'm going to delete
4776
03:04:23,439 --> 03:04:24,960
this
4777
03:04:24,960 --> 03:04:27,600
if show password is true
4778
03:04:27,600 --> 03:04:29,439
then show
4779
03:04:29,439 --> 03:04:30,560
text
4780
03:04:30,560 --> 03:04:34,560
otherwise show password okay and finally
4781
03:04:34,560 --> 03:04:36,640
the final prop that we have to pass is
4782
03:04:36,640 --> 03:04:38,960
handle show password is going to be
4783
03:04:38,960 --> 03:04:42,399
equal to handle show password
4784
03:04:42,399 --> 03:04:44,560
and what is that well that is going to
4785
03:04:44,560 --> 03:04:46,800
be one function that is going to handle
4786
03:04:46,800 --> 03:04:49,040
the show password obviously and the
4787
03:04:49,040 --> 03:04:50,800
handle show password is going to be an
4788
03:04:50,800 --> 03:04:53,840
arrow function handle show password
4789
03:04:53,840 --> 03:04:56,160
that is simply going to toggle the state
4790
03:04:56,160 --> 03:04:59,359
of the password so we can say like this
4791
03:04:59,359 --> 03:05:02,479
set show password
4792
03:05:02,479 --> 03:05:05,840
and then in here we need a prep state so
4793
03:05:05,840 --> 03:05:07,520
previous state whenever you're changing
4794
03:05:07,520 --> 03:05:09,359
the state using the old state you have
4795
03:05:09,359 --> 03:05:11,680
to create a callback function and then
4796
03:05:11,680 --> 03:05:15,040
you have to get the prev show password
4797
03:05:15,040 --> 03:05:16,880
and then what we want to do in here is
4798
03:05:16,880 --> 03:05:19,120
simply say if the previous show password
4799
03:05:19,120 --> 03:05:22,000
is something we want to say false and
4800
03:05:22,000 --> 03:05:24,000
then previous show password
4801
03:05:24,000 --> 03:05:25,920
that means that we're toggling it so if
4802
03:05:25,920 --> 03:05:28,240
it's on turn it off if it's off turn it
4803
03:05:28,240 --> 03:05:29,200
on
4804
03:05:29,200 --> 03:05:31,760
and we're passing that over as a prop
4805
03:05:31,760 --> 03:05:34,960
handle show password
4806
03:05:34,960 --> 03:05:35,840
okay
4807
03:05:35,840 --> 03:05:37,279
there we go
4808
03:05:37,279 --> 03:05:41,120
now our input for the password is done
4809
03:05:41,120 --> 03:05:42,960
below that we want to have one for the
4810
03:05:42,960 --> 03:05:45,520
confirmation and again the password
4811
03:05:45,520 --> 03:05:47,920
confirm field is going to show only if
4812
03:05:47,920 --> 03:05:49,920
you're on the signup form not on the
4813
03:05:49,920 --> 03:05:52,800
sign in so i'm going to say if is sign
4814
03:05:52,800 --> 03:05:53,520
up
4815
03:05:53,520 --> 03:05:54,560
then
4816
03:05:54,560 --> 03:05:56,000
end end
4817
03:05:56,000 --> 03:05:58,960
then we show this input field this input
4818
03:05:58,960 --> 03:06:01,279
is going to have a name of confirm
4819
03:06:01,279 --> 03:06:02,560
password
4820
03:06:02,560 --> 03:06:04,640
it's going to have a label of something
4821
03:06:04,640 --> 03:06:06,720
like repeat
4822
03:06:06,720 --> 03:06:09,279
password
4823
03:06:10,399 --> 03:06:13,920
and it's going to have a handle change
4824
03:06:13,920 --> 03:06:17,680
again be equal to handle change
4825
03:06:17,680 --> 03:06:20,080
finally the type of this one is going to
4826
03:06:20,080 --> 03:06:22,640
be password
4827
03:06:22,640 --> 03:06:24,720
okay we have all of the input fields
4828
03:06:24,720 --> 03:06:27,200
that we need right now finally we need a
4829
03:06:27,200 --> 03:06:28,800
submit button
4830
03:06:28,800 --> 03:06:30,800
so right below the grid i'm going to
4831
03:06:30,800 --> 03:06:33,359
create a button
4832
03:06:33,359 --> 03:06:36,319
and that button is going to be of a type
4833
03:06:36,319 --> 03:06:38,640
is equal to submit so we're going to
4834
03:06:38,640 --> 03:06:40,640
submit the form on this button
4835
03:06:40,640 --> 03:06:44,080
it's going to have a full width property
4836
03:06:44,080 --> 03:06:46,560
also a variant
4837
03:06:46,560 --> 03:06:48,800
of contained
4838
03:06:48,800 --> 03:06:52,640
and a color of primary as well as the
4839
03:06:52,640 --> 03:06:53,920
class name
4840
03:06:53,920 --> 03:06:54,720
of
4841
03:06:54,720 --> 03:06:56,399
classes
4842
03:06:56,399 --> 03:06:58,240
dot submit
4843
03:06:58,240 --> 03:07:00,319
and now inside of that button we want to
4844
03:07:00,319 --> 03:07:03,040
say either sign in or sign up so we can
4845
03:07:03,040 --> 03:07:06,240
again use that property is sign up
4846
03:07:06,240 --> 03:07:09,520
if it is sign up we're gonna show sign
4847
03:07:09,520 --> 03:07:10,560
up
4848
03:07:10,560 --> 03:07:13,680
otherwise we're gonna show sign
4849
03:07:13,680 --> 03:07:14,800
in
4850
03:07:14,800 --> 03:07:17,359
great so now it would make sense to take
4851
03:07:17,359 --> 03:07:19,200
a look what do we have
4852
03:07:19,200 --> 03:07:21,520
let's go back to the browser and see if
4853
03:07:21,520 --> 03:07:24,479
we have the odd right here
4854
03:07:24,479 --> 03:07:26,720
and there we go take a look at that we
4855
03:07:26,720 --> 03:07:29,680
have our sign in form but right now
4856
03:07:29,680 --> 03:07:31,439
there is no way for us to switch between
4857
03:07:31,439 --> 03:07:34,000
the sign in and the sign up so how can
4858
03:07:34,000 --> 03:07:35,120
we do that
4859
03:07:35,120 --> 03:07:37,200
well right now we can just change this
4860
03:07:37,200 --> 03:07:39,279
dummy variable to true
4861
03:07:39,279 --> 03:07:41,600
and that should show us our sign up
4862
03:07:41,600 --> 03:07:43,439
now we have the sign up with the first
4863
03:07:43,439 --> 03:07:46,080
name last name email and password repeat
4864
03:07:46,080 --> 03:07:47,920
passwords as well and the button says
4865
03:07:47,920 --> 03:07:49,120
sign up
4866
03:07:49,120 --> 03:07:50,960
this is extremely good because what
4867
03:07:50,960 --> 03:07:52,960
beginners in react would do is they
4868
03:07:52,960 --> 03:07:54,960
would create two different components
4869
03:07:54,960 --> 03:07:57,279
for the sign up and the sign in
4870
03:07:57,279 --> 03:07:59,600
they would copy all of this code right
4871
03:07:59,600 --> 03:08:00,560
here
4872
03:08:00,560 --> 03:08:02,960
and create two different components what
4873
03:08:02,960 --> 03:08:05,600
we did is we just created one variable
4874
03:08:05,600 --> 03:08:07,920
called is sign up and then based on the
4875
03:08:07,920 --> 03:08:10,080
change of that variable we changed it
4876
03:08:10,080 --> 03:08:12,399
from sign up the title we added or
4877
03:08:12,399 --> 03:08:14,800
deleted some fields and we also changed
4878
03:08:14,800 --> 03:08:17,520
the title of the button you can see in
4879
03:08:17,520 --> 03:08:20,560
react you have to think reactively one
4880
03:08:20,560 --> 03:08:22,160
last thing that we could do is we could
4881
03:08:22,160 --> 03:08:23,600
make a button that's going to allow us
4882
03:08:23,600 --> 03:08:25,680
to switch from the sign up and the sign
4883
03:08:25,680 --> 03:08:28,240
in so let's do that right now
4884
03:08:28,240 --> 03:08:30,640
below our button still inside of the
4885
03:08:30,640 --> 03:08:33,600
form we're going to have another grid
4886
03:08:33,600 --> 03:08:35,600
this grid is going to be of a type
4887
03:08:35,600 --> 03:08:37,600
container and it's going to have a
4888
03:08:37,600 --> 03:08:39,600
justify property
4889
03:08:39,600 --> 03:08:42,240
which is going to say a string of flex
4890
03:08:42,240 --> 03:08:43,680
end
4891
03:08:43,680 --> 03:08:45,760
inside of there i'm going to create a
4892
03:08:45,760 --> 03:08:46,960
grid
4893
03:08:46,960 --> 03:08:50,479
this grid is going to be of a type item
4894
03:08:50,479 --> 03:08:52,560
and then inside of that grid we're going
4895
03:08:52,560 --> 03:08:54,399
to have a button
4896
03:08:54,399 --> 03:08:56,160
and that button is going to have an on
4897
03:08:56,160 --> 03:08:58,399
click property and on click we're going
4898
03:08:58,399 --> 03:09:01,760
to call the function called switch mode
4899
03:09:01,760 --> 03:09:03,760
so that switch mode again is just a
4900
03:09:03,760 --> 03:09:05,120
function which we're going to declare
4901
03:09:05,120 --> 03:09:08,319
right there const switch mode it's going
4902
03:09:08,319 --> 03:09:10,399
to be an arrow function and then in here
4903
03:09:10,399 --> 03:09:11,840
we're going to switch the mode from the
4904
03:09:11,840 --> 03:09:14,080
sign up to sign in
4905
03:09:14,080 --> 03:09:16,479
now what is our button going to say
4906
03:09:16,479 --> 03:09:18,160
again it's going to be different if
4907
03:09:18,160 --> 03:09:20,080
we're logged in or not so i'm going to
4908
03:09:20,080 --> 03:09:23,520
again open this say is sign up
4909
03:09:23,520 --> 03:09:25,200
if we are on the sign up we're going to
4910
03:09:25,200 --> 03:09:26,800
say already
4911
03:09:26,800 --> 03:09:28,319
have
4912
03:09:28,319 --> 03:09:30,640
an account question mark
4913
03:09:30,640 --> 03:09:32,399
sign in
4914
03:09:32,399 --> 03:09:34,399
otherwise we're gonna say
4915
03:09:34,399 --> 03:09:35,680
something like
4916
03:09:35,680 --> 03:09:38,399
don't have an account
4917
03:09:38,399 --> 03:09:41,279
question mark sign up
4918
03:09:41,279 --> 03:09:43,600
okay let's save that and see how does it
4919
03:09:43,600 --> 03:09:45,040
look like
4920
03:09:45,040 --> 03:09:46,479
there we go you can see that we have
4921
03:09:46,479 --> 03:09:48,479
this button right there but right now
4922
03:09:48,479 --> 03:09:50,560
it's not doing anything so what do we
4923
03:09:50,560 --> 03:09:53,359
have to do to actually make this is sign
4924
03:09:53,359 --> 03:09:55,439
up switchable
4925
03:09:55,439 --> 03:09:57,359
think about this for a second try
4926
03:09:57,359 --> 03:09:59,920
implementing this yourself and then once
4927
03:09:59,920 --> 03:10:01,840
you've figured it out or once you're
4928
03:10:01,840 --> 03:10:03,920
stuck come back to this video and i'm
4929
03:10:03,920 --> 03:10:07,120
going to show you how to do it
4930
03:10:07,200 --> 03:10:09,279
what we have to do to make this work in
4931
03:10:09,279 --> 03:10:13,120
react fashion is make a new state field
4932
03:10:13,120 --> 03:10:15,040
i'm going to take this is sign up
4933
03:10:15,040 --> 03:10:17,920
variable and i'm going to turn it into a
4934
03:10:17,920 --> 03:10:20,640
state field so i'm going to say const
4935
03:10:20,640 --> 03:10:24,800
is sign up and set is sign up
4936
03:10:24,800 --> 03:10:28,000
that is going to be equal to use state
4937
03:10:28,000 --> 03:10:32,319
and at the start that can be false
4938
03:10:32,319 --> 03:10:34,399
now we can remove this everything is
4939
03:10:34,399 --> 03:10:36,239
still got a function but now we have
4940
03:10:36,239 --> 03:10:39,040
access to this set is sign up
4941
03:10:39,040 --> 03:10:41,200
and now we can use that inside of our
4942
03:10:41,200 --> 03:10:43,520
switch mode function so right inside of
4943
03:10:43,520 --> 03:10:47,040
here we can say set is sign up and what
4944
03:10:47,040 --> 03:10:48,880
we simply need to do is do the same
4945
03:10:48,880 --> 03:10:50,319
thing that we did right here for the
4946
03:10:50,319 --> 03:10:51,840
password
4947
03:10:51,840 --> 03:10:53,600
since we are using the previous state to
4948
03:10:53,600 --> 03:10:55,840
manage a new one we have to get a
4949
03:10:55,840 --> 03:10:58,880
callback function and say prev is sign
4950
03:10:58,880 --> 03:10:59,680
up
4951
03:10:59,680 --> 03:11:03,200
and then in here simply say not prev is
4952
03:11:03,200 --> 03:11:05,200
sign up that's going to switch it on and
4953
03:11:05,200 --> 03:11:06,720
off like a switch
4954
03:11:06,720 --> 03:11:09,200
also i want to reset the show password
4955
03:11:09,200 --> 03:11:11,359
if i switch the mode so i'm going to say
4956
03:11:11,359 --> 03:11:12,560
right here
4957
03:11:12,560 --> 03:11:14,239
handle show password and i'm going to
4958
03:11:14,239 --> 03:11:16,479
pass the value of false
4959
03:11:16,479 --> 03:11:18,960
let's save it and see if it works we are
4960
03:11:18,960 --> 03:11:21,920
on the sign in i click it we are on the
4961
03:11:21,920 --> 03:11:23,040
sign up
4962
03:11:23,040 --> 03:11:26,319
there we go this works as expected now
4963
03:11:26,319 --> 03:11:28,399
we have a great looking form as you can
4964
03:11:28,399 --> 03:11:30,720
see we can go back in here we have all
4965
03:11:30,720 --> 03:11:33,279
of the posts we can head to the sign in
4966
03:11:33,279 --> 03:11:35,520
and there we go in here we have the sign
4967
03:11:35,520 --> 03:11:38,160
in and the sign up form which both look
4968
03:11:38,160 --> 03:11:39,279
amazing
4969
03:11:39,279 --> 03:11:41,840
now the login and register using these
4970
03:11:41,840 --> 03:11:44,399
fields the first name last name email
4971
03:11:44,399 --> 03:11:46,080
and all of that is going to take a bit
4972
03:11:46,080 --> 03:11:48,479
longer so what i would want to do first
4973
03:11:48,479 --> 03:11:51,120
is implement the google login button
4974
03:11:51,120 --> 03:11:52,880
that way we can first handle the
4975
03:11:52,880 --> 03:11:54,960
authentication with google and then
4976
03:11:54,960 --> 03:11:57,120
we'll be able to do everything else
4977
03:11:57,120 --> 03:11:58,800
that's better because if we first
4978
03:11:58,800 --> 03:12:01,040
implement google that way we'll still
4979
03:12:01,040 --> 03:12:03,200
have everything functioning as normal
4980
03:12:03,200 --> 03:12:05,200
and then later on we'll be able to add
4981
03:12:05,200 --> 03:12:08,560
everything else including these fields
4982
03:12:08,560 --> 03:12:11,200
let's add the google sign-in button
4983
03:12:11,200 --> 03:12:14,160
right here above our current one
4984
03:12:14,160 --> 03:12:16,080
to do that we'll have to import one
4985
03:12:16,080 --> 03:12:18,640
thing and that thing is called google
4986
03:12:18,640 --> 03:12:21,439
login so right below our material ui
4987
03:12:21,439 --> 03:12:24,560
imports in here we can say import
4988
03:12:24,560 --> 03:12:25,439
google
4989
03:12:25,439 --> 03:12:26,880
login
4990
03:12:26,880 --> 03:12:30,080
from and that is going to be react
4991
03:12:30,080 --> 03:12:32,880
google login we installed that package
4992
03:12:32,880 --> 03:12:35,120
at the start of this video
4993
03:12:35,120 --> 03:12:37,200
okay and now we have to use that google
4994
03:12:37,200 --> 03:12:39,520
login how do we use it well it's going
4995
03:12:39,520 --> 03:12:42,479
to be a simple button so right here
4996
03:12:42,479 --> 03:12:44,800
above our current one i'm going to call
4997
03:12:44,800 --> 03:12:48,080
that google login as a component
4998
03:12:48,080 --> 03:12:49,840
and it is going to be a self-closing
4999
03:12:49,840 --> 03:12:52,080
component and here's a hint for you
5000
03:12:52,080 --> 03:12:53,680
whenever you're importing something and
5001
03:12:53,680 --> 03:12:55,200
that something starts with a capital
5002
03:12:55,200 --> 03:12:56,080
letter
5003
03:12:56,080 --> 03:12:59,439
most likely it's going to be a component
5004
03:12:59,439 --> 03:13:01,920
inside of here you have to have your own
5005
03:13:01,920 --> 03:13:04,239
client google id so this is something
5006
03:13:04,239 --> 03:13:06,000
that we're going to add as soon as we're
5007
03:13:06,000 --> 03:13:07,680
done with creating the layout for this
5008
03:13:07,680 --> 03:13:09,120
so i'm going to say google
5009
03:13:09,120 --> 03:13:12,000
id we have to put that right there
5010
03:13:12,000 --> 03:13:14,560
then below we have render
5011
03:13:14,560 --> 03:13:16,640
render means how are we going to show
5012
03:13:16,640 --> 03:13:18,560
our button what is it going to look like
5013
03:13:18,560 --> 03:13:21,120
so in here you get something known as
5014
03:13:21,120 --> 03:13:24,080
render props and this is a callback
5015
03:13:24,080 --> 03:13:26,000
function so inside of here you can
5016
03:13:26,000 --> 03:13:28,160
create a callback function it's not
5017
03:13:28,160 --> 03:13:29,840
going to have curly braces it's going to
5018
03:13:29,840 --> 03:13:32,160
be just an instant return and then
5019
03:13:32,160 --> 03:13:33,840
inside of there we're going to put a
5020
03:13:33,840 --> 03:13:36,239
normal material ui button
5021
03:13:36,239 --> 03:13:38,560
that button is going to have a class
5022
03:13:38,560 --> 03:13:40,880
name of classes
5023
03:13:40,880 --> 03:13:42,560
dot google
5024
03:13:42,560 --> 03:13:43,600
button
5025
03:13:43,600 --> 03:13:47,520
it's going to have a color of primary
5026
03:13:47,520 --> 03:13:50,000
also it's going to be a full width
5027
03:13:50,000 --> 03:13:51,200
button
5028
03:13:51,200 --> 03:13:53,600
and it's going to have an on click
5029
03:13:53,600 --> 03:13:54,960
listener
5030
03:13:54,960 --> 03:13:56,960
on click we're going to call the render
5031
03:13:56,960 --> 03:13:58,080
props
5032
03:13:58,080 --> 03:14:00,560
dot on click
5033
03:14:00,560 --> 03:14:02,239
it's also going to have a disabled
5034
03:14:02,239 --> 03:14:03,359
property
5035
03:14:03,359 --> 03:14:06,000
and there we're going to put the render
5036
03:14:06,000 --> 03:14:08,960
props dot disabled
5037
03:14:08,960 --> 03:14:12,399
and it's also going to have a start icon
5038
03:14:12,399 --> 03:14:14,640
that start icon we're going to import
5039
03:14:14,640 --> 03:14:16,960
but it's going to be simply
5040
03:14:16,960 --> 03:14:19,439
icon a self closing component that's
5041
03:14:19,439 --> 03:14:21,760
simply going to say icon
5042
03:14:21,760 --> 03:14:23,840
we're going to have one more thing and
5043
03:14:23,840 --> 03:14:26,880
that one thing is going to be a variant
5044
03:14:26,880 --> 03:14:28,479
of contained
5045
03:14:28,479 --> 03:14:31,200
variant is equal to contain i see that
5046
03:14:31,200 --> 03:14:33,040
we have quite a lot of things some
5047
03:14:33,040 --> 03:14:35,680
people prefer to have them like this
5048
03:14:35,680 --> 03:14:37,760
this way it's more easily noticeable
5049
03:14:37,760 --> 03:14:40,080
what we have and i agree we can leave it
5050
03:14:40,080 --> 03:14:41,600
like that for now
5051
03:14:41,600 --> 03:14:43,840
until we actually finish the button so
5052
03:14:43,840 --> 03:14:46,000
we have the button and finally what can
5053
03:14:46,000 --> 03:14:48,560
we say in there we can say google
5054
03:14:48,560 --> 03:14:49,439
sign
5055
03:14:49,439 --> 03:14:50,560
in
5056
03:14:50,560 --> 03:14:52,080
now you can see that we're missing the
5057
03:14:52,080 --> 03:14:55,359
icon property so we have to import that
5058
03:14:55,359 --> 03:14:59,359
the icon is going to be a simple svg so
5059
03:14:59,359 --> 03:15:01,200
we're going to create a new file right
5060
03:15:01,200 --> 03:15:04,319
there called icon.js
5061
03:15:04,319 --> 03:15:06,880
and now in here we can paste that icon
5062
03:15:06,880 --> 03:15:09,439
so this is going to be an svg react
5063
03:15:09,439 --> 03:15:10,560
component
5064
03:15:10,560 --> 03:15:13,120
and we can simply import it inside of
5065
03:15:13,120 --> 03:15:14,239
our auth
5066
03:15:14,239 --> 03:15:16,479
you can again copy and paste this icon
5067
03:15:16,479 --> 03:15:17,600
it's going to be linked in the
5068
03:15:17,600 --> 03:15:19,279
description
5069
03:15:19,279 --> 03:15:21,520
now inside of our auth we simply have to
5070
03:15:21,520 --> 03:15:24,399
import that icon we can do that easily
5071
03:15:24,399 --> 03:15:27,680
by saying import
5072
03:15:27,680 --> 03:15:29,120
icon
5073
03:15:29,120 --> 03:15:30,960
from dot slash
5074
03:15:30,960 --> 03:15:31,920
icon
5075
03:15:31,920 --> 03:15:33,760
as you can see now we have everything
5076
03:15:33,760 --> 03:15:35,760
that we need in our button so what i
5077
03:15:35,760 --> 03:15:38,640
prefer to do is to have it in one row
5078
03:15:38,640 --> 03:15:40,479
because this way it doesn't clutter our
5079
03:15:40,479 --> 03:15:42,720
view we know that it's a button it has a
5080
03:15:42,720 --> 03:15:45,600
bunch of props and it says google sign
5081
03:15:45,600 --> 03:15:48,319
in okay great now we can continue with
5082
03:15:48,319 --> 03:15:50,000
adding different props to our google
5083
03:15:50,000 --> 03:15:53,520
login and these props are going to be on
5084
03:15:53,520 --> 03:15:54,800
success
5085
03:15:54,800 --> 03:15:58,319
and also on failure so we have two
5086
03:15:58,319 --> 03:16:00,479
handlers here the handler for success
5087
03:16:00,479 --> 03:16:03,040
action and for the failure action
5088
03:16:03,040 --> 03:16:05,600
on success we're going to call google
5089
03:16:05,600 --> 03:16:06,960
success
5090
03:16:06,960 --> 03:16:08,000
function
5091
03:16:08,000 --> 03:16:10,080
and on failure we're going to call
5092
03:16:10,080 --> 03:16:12,640
google failure function we're going to
5093
03:16:12,640 --> 03:16:14,880
create both of these functions finally
5094
03:16:14,880 --> 03:16:16,319
the last thing that we need is going to
5095
03:16:16,319 --> 03:16:17,520
be cookie
5096
03:16:17,520 --> 03:16:20,080
policy and that is going to be equal to
5097
03:16:20,080 --> 03:16:21,120
single
5098
03:16:21,120 --> 03:16:24,800
underscore host underscore origin
5099
03:16:24,800 --> 03:16:25,760
okay
5100
03:16:25,760 --> 03:16:27,680
now we can create those two functions
5101
03:16:27,680 --> 03:16:29,120
just above
5102
03:16:29,120 --> 03:16:30,960
inside of here i'm going to create the
5103
03:16:30,960 --> 03:16:33,040
arrow function for both of these so i'm
5104
03:16:33,040 --> 03:16:34,880
going to select them both
5105
03:16:34,880 --> 03:16:38,160
and say const google success and google
5106
03:16:38,160 --> 03:16:41,120
failure is equal to an error function
5107
03:16:41,120 --> 03:16:44,239
and it's going to be a block of code so
5108
03:16:44,239 --> 03:16:47,120
what do we do on google success and on
5109
03:16:47,120 --> 03:16:48,880
google failure
5110
03:16:48,880 --> 03:16:50,880
well on google failure i simply want to
5111
03:16:50,880 --> 03:16:52,720
console log an error so in here i'm
5112
03:16:52,720 --> 03:16:54,960
going to say console log
5113
03:16:54,960 --> 03:16:57,600
google and that's going to be a string
5114
03:16:57,600 --> 03:16:59,359
google sign
5115
03:16:59,359 --> 03:17:01,040
in
5116
03:17:01,040 --> 03:17:04,560
was unsuccessful
5117
03:17:04,640 --> 03:17:06,960
okay and we can say something like try
5118
03:17:06,960 --> 03:17:08,319
again
5119
03:17:08,319 --> 03:17:09,920
later
5120
03:17:09,920 --> 03:17:11,840
okay so this is going to be our google
5121
03:17:11,840 --> 03:17:14,000
failure and then what do we want to do
5122
03:17:14,000 --> 03:17:17,279
on success well in here we get access to
5123
03:17:17,279 --> 03:17:19,680
a full response so i'm going to say rest
5124
03:17:19,680 --> 03:17:22,080
here and for now i simply want to
5125
03:17:22,080 --> 03:17:24,960
console.log that res
5126
03:17:24,960 --> 03:17:26,960
of course this is not going to work out
5127
03:17:26,960 --> 03:17:28,880
of the box because we still haven't
5128
03:17:28,880 --> 03:17:31,600
supplied the google login with our
5129
03:17:31,600 --> 03:17:34,960
client id so we have to find that
5130
03:17:34,960 --> 03:17:36,880
to get your own client id you have to
5131
03:17:36,880 --> 03:17:38,840
head to
5132
03:17:38,840 --> 03:17:40,720
console.developers.google.com i'm going
5133
03:17:40,720 --> 03:17:43,040
to put this link down in the description
5134
03:17:43,040 --> 03:17:44,800
instead of here on the left side you'll
5135
03:17:44,800 --> 03:17:47,279
have a sidebar that says credentials and
5136
03:17:47,279 --> 03:17:50,000
then oauth consent screen first we're
5137
03:17:50,000 --> 03:17:52,880
going to go to oauth consent screen
5138
03:17:52,880 --> 03:17:54,800
i already have mine created you'll have
5139
03:17:54,800 --> 03:17:57,520
to create a new all odd consent screen
5140
03:17:57,520 --> 03:17:59,200
you can name it something like memories
5141
03:17:59,200 --> 03:18:01,359
app or something similar and then in
5142
03:18:01,359 --> 03:18:03,920
here we'll have to do a bit of a setup
5143
03:18:03,920 --> 03:18:06,319
you can create our app name you can put
5144
03:18:06,319 --> 03:18:08,479
your user support email
5145
03:18:08,479 --> 03:18:10,160
and then finally you can add your
5146
03:18:10,160 --> 03:18:12,960
developer contact information and click
5147
03:18:12,960 --> 03:18:15,840
save and continue with that you created
5148
03:18:15,840 --> 03:18:18,479
your odd consent screen
5149
03:18:18,479 --> 03:18:20,800
in this case under scopes i currently
5150
03:18:20,800 --> 03:18:22,479
have nothing
5151
03:18:22,479 --> 03:18:24,960
nothing under test users and this is the
5152
03:18:24,960 --> 03:18:26,479
summary
5153
03:18:26,479 --> 03:18:27,600
that's it
5154
03:18:27,600 --> 03:18:29,200
and finally we can go back to the
5155
03:18:29,200 --> 03:18:30,319
dashboard
5156
03:18:30,319 --> 03:18:32,160
now that you have the oauth consent
5157
03:18:32,160 --> 03:18:34,800
screen you can head under credentials
5158
03:18:34,800 --> 03:18:36,560
inside of the credentials you can click
5159
03:18:36,560 --> 03:18:38,640
create new credentials and then you can
5160
03:18:38,640 --> 03:18:41,279
choose odd client id
5161
03:18:41,279 --> 03:18:42,720
inside of here you can choose the
5162
03:18:42,720 --> 03:18:44,800
application type and in our case that's
5163
03:18:44,800 --> 03:18:46,960
going to be a web application
5164
03:18:46,960 --> 03:18:49,680
i'm going to call it memories app
5165
03:18:49,680 --> 03:18:51,600
inside of here you can put the localhost
5166
03:18:51,600 --> 03:18:54,080
3000 as the uri
5167
03:18:54,080 --> 03:18:55,920
and we can also do that for the
5168
03:18:55,920 --> 03:18:58,880
authorized redirect uris so inside of
5169
03:18:58,880 --> 03:19:01,680
here we can do something like http
5170
03:19:01,680 --> 03:19:05,200
localhost and let's do three thousand
5171
03:19:05,200 --> 03:19:07,040
i think that we can also add the forward
5172
03:19:07,040 --> 03:19:09,680
slash off this one
5173
03:19:09,680 --> 03:19:11,600
with that said we can create it and i
5174
03:19:11,600 --> 03:19:13,279
think that should be enough localhost
5175
03:19:13,279 --> 03:19:15,120
three thousand and localhost three
5176
03:19:15,120 --> 03:19:18,239
thousand four slash auth for that and
5177
03:19:18,239 --> 03:19:21,359
there you go you get your oauth client
5178
03:19:21,359 --> 03:19:23,920
id and the client secret don't forget
5179
03:19:23,920 --> 03:19:25,840
this site yet we'll have to come back to
5180
03:19:25,840 --> 03:19:27,920
here once we actually deploy our
5181
03:19:27,920 --> 03:19:29,920
application because in here you'll have
5182
03:19:29,920 --> 03:19:32,479
to add a different url because now it
5183
03:19:32,479 --> 03:19:35,200
works only on our local host okay great
5184
03:19:35,200 --> 03:19:37,520
so we can copy our client id we can come
5185
03:19:37,520 --> 03:19:39,120
back to our application
5186
03:19:39,120 --> 03:19:41,120
and now inside of here we can simply
5187
03:19:41,120 --> 03:19:42,720
paste that id
5188
03:19:42,720 --> 03:19:45,120
right in here with that said i'm
5189
03:19:45,120 --> 03:19:47,359
interested to see what this response is
5190
03:19:47,359 --> 03:19:49,200
going to be and how does her button look
5191
03:19:49,200 --> 03:19:51,600
like with so i can close this tab for
5192
03:19:51,600 --> 03:19:54,800
now and go back to memories project
5193
03:19:54,800 --> 03:19:56,880
as you can see we have our google sign
5194
03:19:56,880 --> 03:19:58,560
in button right here
5195
03:19:58,560 --> 03:20:01,359
looks like it's connected all the way to
5196
03:20:01,359 --> 03:20:03,760
our front button so what i'm going to do
5197
03:20:03,760 --> 03:20:05,680
right now is just switch them so i'm
5198
03:20:05,680 --> 03:20:08,319
going to switch our google login and our
5199
03:20:08,319 --> 03:20:11,200
normal login button right there so now
5200
03:20:11,200 --> 03:20:13,439
we have our manual login button on top
5201
03:20:13,439 --> 03:20:15,439
of our google login
5202
03:20:15,439 --> 03:20:17,600
and there we go that looks so much
5203
03:20:17,600 --> 03:20:20,000
better so now the question is if i open
5204
03:20:20,000 --> 03:20:21,359
the console
5205
03:20:21,359 --> 03:20:24,160
what is our response going to look like
5206
03:20:24,160 --> 03:20:27,359
once i click google sign in
5207
03:20:27,359 --> 03:20:30,239
okay so right now we get redirect uri
5208
03:20:30,239 --> 03:20:32,560
mismatch that means that we have to go
5209
03:20:32,560 --> 03:20:36,120
back to our apis and services meaning
5210
03:20:36,120 --> 03:20:37,920
console.developers.google.com and we
5211
03:20:37,920 --> 03:20:39,680
have to see if we have set up everything
5212
03:20:39,680 --> 03:20:40,960
correctly
5213
03:20:40,960 --> 03:20:43,040
okay inside of here we do see that we
5214
03:20:43,040 --> 03:20:46,319
have our memories app all odd client so
5215
03:20:46,319 --> 03:20:48,720
maybe we have to add the localhost 3000
5216
03:20:48,720 --> 03:20:51,760
forward slash auth to here as well but
5217
03:20:51,760 --> 03:20:53,840
you can see that it cannot contain a
5218
03:20:53,840 --> 03:20:55,840
path or end with that so that's not
5219
03:20:55,840 --> 03:20:57,760
going to be the solution what about if
5220
03:20:57,760 --> 03:21:00,479
we add the http right there and not
5221
03:21:00,479 --> 03:21:03,359
https let's try with that
5222
03:21:03,359 --> 03:21:05,600
that works so maybe definitely that's
5223
03:21:05,600 --> 03:21:06,800
the solution
5224
03:21:06,800 --> 03:21:08,560
let's save it
5225
03:21:08,560 --> 03:21:10,560
and let's try one more time in our
5226
03:21:10,560 --> 03:21:12,239
memories app
5227
03:21:12,239 --> 03:21:14,960
okay if we click the google sign in now
5228
03:21:14,960 --> 03:21:17,040
as you can see we get this nice window
5229
03:21:17,040 --> 03:21:18,160
from google
5230
03:21:18,160 --> 03:21:19,920
but before we go ahead and click
5231
03:21:19,920 --> 03:21:21,680
anywhere else i just noticed that we
5232
03:21:21,680 --> 03:21:24,080
have one more thing that we have to do
5233
03:21:24,080 --> 03:21:26,080
our google success function is going to
5234
03:21:26,080 --> 03:21:28,319
be asynchronous that means that we have
5235
03:21:28,319 --> 03:21:30,720
to add an async keyword in front of this
5236
03:21:30,720 --> 03:21:31,920
function
5237
03:21:31,920 --> 03:21:34,319
now let's save it go back
5238
03:21:34,319 --> 03:21:36,479
and now finally let's click that google
5239
03:21:36,479 --> 03:21:39,279
sign in this is crazy because we didn't
5240
03:21:39,279 --> 03:21:41,439
code any of this google automatically
5241
03:21:41,439 --> 03:21:43,840
gave us this window so now i'll try to
5242
03:21:43,840 --> 03:21:46,479
log in with javascript mastery and take
5243
03:21:46,479 --> 03:21:48,560
a look at that we got google sign-in was
5244
03:21:48,560 --> 03:21:51,600
unsuccessful not as i supposed to be but
5245
03:21:51,600 --> 03:21:54,160
that's normal errors happen so along
5246
03:21:54,160 --> 03:21:56,160
with this console log google sign-in was
5247
03:21:56,160 --> 03:21:58,319
unsuccessful i'll also try to console
5248
03:21:58,319 --> 03:22:00,640
log the error so inside of here we can
5249
03:22:00,640 --> 03:22:03,120
console.log error
5250
03:22:03,120 --> 03:22:05,359
okay we got an error right there and
5251
03:22:05,359 --> 03:22:07,840
i'll try to copy the details and paste
5252
03:22:07,840 --> 03:22:09,840
them right in here so that we can both
5253
03:22:09,840 --> 03:22:12,560
see what our google says i'm going to
5254
03:22:12,560 --> 03:22:14,720
extend this into multiple rows
5255
03:22:14,720 --> 03:22:16,000
usually you should not share your
5256
03:22:16,000 --> 03:22:18,080
client's id with everyone but i'm going
5257
03:22:18,080 --> 03:22:20,160
to delete this after recording the video
5258
03:22:20,160 --> 03:22:21,760
so it's okay
5259
03:22:21,760 --> 03:22:24,000
okay so the error says not a valid
5260
03:22:24,000 --> 03:22:26,160
origin for the client http localhost
5261
03:22:26,160 --> 03:22:29,040
3000 has not been whitelisted for client
5262
03:22:29,040 --> 03:22:30,239
id
5263
03:22:30,239 --> 03:22:32,720
please go to they give us the url and
5264
03:22:32,720 --> 03:22:35,279
whitelist origin for your project client
5265
03:22:35,279 --> 03:22:36,239
id
5266
03:22:36,239 --> 03:22:38,479
after a bit of googling i found out that
5267
03:22:38,479 --> 03:22:40,399
the issue was not that we had to add
5268
03:22:40,399 --> 03:22:41,920
something to our google developers
5269
03:22:41,920 --> 03:22:44,479
console or something to our code we
5270
03:22:44,479 --> 03:22:46,399
simply had to clean the cookies so let
5271
03:22:46,399 --> 03:22:48,399
me show you the solution that they found
5272
03:22:48,399 --> 03:22:50,399
the solution was in stack overflow and
5273
03:22:50,399 --> 03:22:52,720
what you simply had to do is clear the
5274
03:22:52,720 --> 03:22:55,680
browser's cache so in chrome settings
5275
03:22:55,680 --> 03:22:58,319
advanced clear browsing data cached
5276
03:22:58,319 --> 03:23:00,880
images and files and that's it that's
5277
03:23:00,880 --> 03:23:02,479
gonna do the trick
5278
03:23:02,479 --> 03:23:04,319
so after you do that and come back to
5279
03:23:04,319 --> 03:23:07,600
the site if you click google sign in
5280
03:23:07,600 --> 03:23:10,239
take a look right now you can see all
5281
03:23:10,239 --> 03:23:12,560
the details right here which means our
5282
03:23:12,560 --> 03:23:14,720
login was successful
5283
03:23:14,720 --> 03:23:16,560
also while we're here let's get rid of
5284
03:23:16,560 --> 03:23:19,200
this boring warning it simply says
5285
03:23:19,200 --> 03:23:21,680
invalid prop input props of type boolean
5286
03:23:21,680 --> 03:23:24,319
supply to forward draft text field
5287
03:23:24,319 --> 03:23:28,000
that means that if we go back into our
5288
03:23:28,000 --> 03:23:30,640
input component right here this is
5289
03:23:30,640 --> 03:23:33,120
getting a boolean and that means that in
5290
03:23:33,120 --> 03:23:35,680
this case we cannot use the end end we
5291
03:23:35,680 --> 03:23:38,319
rather have to use the null because this
5292
03:23:38,319 --> 03:23:40,399
cannot be a boolean it has to be a null
5293
03:23:40,399 --> 03:23:43,120
or a specific component so if we save
5294
03:23:43,120 --> 03:23:46,000
that the error is gone and as we
5295
03:23:46,000 --> 03:23:49,439
discussed our sign in works beautifully
5296
03:23:49,439 --> 03:23:51,680
now we have to use some of these details
5297
03:23:51,680 --> 03:23:54,800
we got right in our response
5298
03:23:54,800 --> 03:23:57,520
and we can use them right inside of here
5299
03:23:57,520 --> 03:24:00,399
odd google success now we have access to
5300
03:24:00,399 --> 03:24:02,080
this entire object
5301
03:24:02,080 --> 03:24:03,600
so what do we need
5302
03:24:03,600 --> 03:24:06,720
well we can say const result is equal to
5303
03:24:06,720 --> 03:24:09,359
res and now we can use the optional
5304
03:24:09,359 --> 03:24:12,479
chaining operator the question mark dot
5305
03:24:12,479 --> 03:24:14,319
the question mark dot is a special
5306
03:24:14,319 --> 03:24:16,239
operator that's not going to throw an
5307
03:24:16,239 --> 03:24:18,399
error if we don't have access to the
5308
03:24:18,399 --> 03:24:20,720
rest object because maybe sometimes
5309
03:24:20,720 --> 03:24:22,800
we're not going to have this rest object
5310
03:24:22,800 --> 03:24:24,080
so we want to make sure that we don't
5311
03:24:24,080 --> 03:24:26,080
get an error if we don't have it that
5312
03:24:26,080 --> 03:24:29,760
way you can say profile i'll be j so now
5313
03:24:29,760 --> 03:24:31,760
we are not gonna get an error if this
5314
03:24:31,760 --> 03:24:33,040
doesn't exist
5315
03:24:33,040 --> 03:24:35,359
before if you use just the dot
5316
03:24:35,359 --> 03:24:37,840
if the rest object didn't exist then you
5317
03:24:37,840 --> 03:24:39,600
would get an error saying cannot get
5318
03:24:39,600 --> 03:24:41,279
property
5319
03:24:41,279 --> 03:24:45,279
profile obj off undefined because res
5320
03:24:45,279 --> 03:24:48,000
profile obj would be undefined if you do
5321
03:24:48,000 --> 03:24:49,920
this it's not going to throw you an
5322
03:24:49,920 --> 03:24:52,479
error it's simply going to say
5323
03:24:52,479 --> 03:24:54,319
undefined that's going to be the value
5324
03:24:54,319 --> 03:24:57,040
of the result great so now we know how
5325
03:24:57,040 --> 03:24:59,439
the optional chaining operator works and
5326
03:24:59,439 --> 03:25:01,040
now that i think about it it's kind of
5327
03:25:01,040 --> 03:25:02,800
crazy that i knew the exact error
5328
03:25:02,800 --> 03:25:04,560
message that we would get if we didn't
5329
03:25:04,560 --> 03:25:05,840
use this
5330
03:25:05,840 --> 03:25:08,080
yeah so with that said let's continue we
5331
03:25:08,080 --> 03:25:09,840
also want to get the token and we can
5332
03:25:09,840 --> 03:25:13,359
get the token from res question mark dot
5333
03:25:13,359 --> 03:25:15,279
token
5334
03:25:15,279 --> 03:25:16,239
id
5335
03:25:16,239 --> 03:25:17,920
so now we have the token and we have the
5336
03:25:17,920 --> 03:25:20,000
result that's basically everything that
5337
03:25:20,000 --> 03:25:21,200
we need
5338
03:25:21,200 --> 03:25:22,720
since we are working with an async
5339
03:25:22,720 --> 03:25:24,800
function we can use the try and catch
5340
03:25:24,800 --> 03:25:25,840
block
5341
03:25:25,840 --> 03:25:28,399
so now if our login is successful we
5342
03:25:28,399 --> 03:25:30,560
want to dispatch something otherwise
5343
03:25:30,560 --> 03:25:32,319
we're simply going to console.log the
5344
03:25:32,319 --> 03:25:33,760
error
5345
03:25:33,760 --> 03:25:36,000
so how do we dispatch something well of
5346
03:25:36,000 --> 03:25:38,640
course we have to import use dispatch
5347
03:25:38,640 --> 03:25:40,800
from redux so inside of here i'm going
5348
03:25:40,800 --> 03:25:42,479
to say import
5349
03:25:42,479 --> 03:25:43,600
use
5350
03:25:43,600 --> 03:25:45,680
dispatch and that's going to be coming
5351
03:25:45,680 --> 03:25:48,560
from react dash redux
5352
03:25:48,560 --> 03:25:50,560
of course since it's a hook you need to
5353
03:25:50,560 --> 03:25:53,680
say cons dispatch is equal to use
5354
03:25:53,680 --> 03:25:54,880
dispatch
5355
03:25:54,880 --> 03:25:57,120
and finally we can use that dispatch to
5356
03:25:57,120 --> 03:25:59,040
dispatch an action so we can say this
5357
03:25:59,040 --> 03:26:01,359
patch call it as a function and now
5358
03:26:01,359 --> 03:26:04,560
inside of there we can send some data in
5359
03:26:04,560 --> 03:26:06,160
the type of our action is going to be
5360
03:26:06,160 --> 03:26:07,600
simply auth
5361
03:26:07,600 --> 03:26:09,840
and then the data we'll be sending in is
5362
03:26:09,840 --> 03:26:12,000
going to be the payload and all that we
5363
03:26:12,000 --> 03:26:13,680
want to send is a new object that
5364
03:26:13,680 --> 03:26:15,279
contains the result
5365
03:26:15,279 --> 03:26:19,200
and the token that is going to be it
5366
03:26:19,200 --> 03:26:21,200
now that we are dispatching this action
5367
03:26:21,200 --> 03:26:23,200
and we're sending the payload in the
5368
03:26:23,200 --> 03:26:25,600
only thing we have to do is add reducer
5369
03:26:25,600 --> 03:26:28,160
that's going to handle it properly so
5370
03:26:28,160 --> 03:26:30,960
let's go to our reducers folder and then
5371
03:26:30,960 --> 03:26:32,880
in there we need to create the reducers
5372
03:26:32,880 --> 03:26:35,279
for the auth so inside of here i'm going
5373
03:26:35,279 --> 03:26:39,439
to say new file off dot js
5374
03:26:39,439 --> 03:26:41,200
inside of here as always we need to
5375
03:26:41,200 --> 03:26:43,680
create a basic reducer so we're going to
5376
03:26:43,680 --> 03:26:44,960
do the same thing that we did in the
5377
03:26:44,960 --> 03:26:47,520
post first we need to import some of the
5378
03:26:47,520 --> 03:26:48,640
constants
5379
03:26:48,640 --> 03:26:50,319
in this case we're not going to have
5380
03:26:50,319 --> 03:26:52,720
these what we want to have is going to
5381
03:26:52,720 --> 03:26:54,239
be the auth
5382
03:26:54,239 --> 03:26:56,319
and the logout
5383
03:26:56,319 --> 03:26:58,000
of course these are constants in the
5384
03:26:58,000 --> 03:27:00,239
action types constants so we can go to
5385
03:27:00,239 --> 03:27:03,120
this folder action types and now inside
5386
03:27:03,120 --> 03:27:05,359
of here let's create those
5387
03:27:05,359 --> 03:27:07,760
export const and that is going to be
5388
03:27:07,760 --> 03:27:10,720
auth is equal to a string of auth
5389
03:27:10,720 --> 03:27:13,680
and this one is also going to be logout
5390
03:27:13,680 --> 03:27:15,680
just to reiterate we are doing this to
5391
03:27:15,680 --> 03:27:17,439
save ourselves the trouble of
5392
03:27:17,439 --> 03:27:19,680
accidentally misspelling a variable
5393
03:27:19,680 --> 03:27:21,520
because that way we wouldn't have the
5394
03:27:21,520 --> 03:27:23,520
error this way since everything is a
5395
03:27:23,520 --> 03:27:26,160
variable if we misspell something we are
5396
03:27:26,160 --> 03:27:29,279
going to get an error okay let's create
5397
03:27:29,279 --> 03:27:32,720
our reducer const odd reducer
5398
03:27:32,720 --> 03:27:35,680
reducers are functions more specifically
5399
03:27:35,680 --> 03:27:38,239
functions that are accepting the state
5400
03:27:38,239 --> 03:27:40,399
and an action
5401
03:27:40,399 --> 03:27:43,520
based on that they have a switch case
5402
03:27:43,520 --> 03:27:45,279
in the switch case we're looking for the
5403
03:27:45,279 --> 03:27:47,760
action.type in this case it can be
5404
03:27:47,760 --> 03:27:49,439
something like odd
5405
03:27:49,439 --> 03:27:51,200
and it can also be logout which we're
5406
03:27:51,200 --> 03:27:53,359
going to implement later on
5407
03:27:53,359 --> 03:27:56,720
so if our action type is equal to odd
5408
03:27:56,720 --> 03:27:58,319
what do we want to do
5409
03:27:58,319 --> 03:28:00,239
well remember we're getting all the
5410
03:28:00,239 --> 03:28:03,520
details inside of the action dot payload
5411
03:28:03,520 --> 03:28:05,680
or more specifically we call the data so
5412
03:28:05,680 --> 03:28:07,520
let's first try to console.log that to
5413
03:28:07,520 --> 03:28:09,680
see what are we getting
5414
03:28:09,680 --> 03:28:13,120
so inside of here i'll try to get action
5415
03:28:13,120 --> 03:28:15,279
dot data i'm also going to use the
5416
03:28:15,279 --> 03:28:17,120
question mark because not always is that
5417
03:28:17,120 --> 03:28:18,960
data gonna be there
5418
03:28:18,960 --> 03:28:21,040
and that's it let's try to console.log
5419
03:28:21,040 --> 03:28:23,279
it and see if we're getting it
5420
03:28:23,279 --> 03:28:26,800
of course we have to export default auth
5421
03:28:26,800 --> 03:28:29,439
reducer
5422
03:28:29,520 --> 03:28:31,920
and we also have to use it in the
5423
03:28:31,920 --> 03:28:34,640
index.js of our reducers so in our
5424
03:28:34,640 --> 03:28:37,200
index.js we have to import not only the
5425
03:28:37,200 --> 03:28:38,399
post reducer
5426
03:28:38,399 --> 03:28:41,200
but also the auth reducer and then we
5427
03:28:41,200 --> 03:28:43,680
have to use it here in combined reducers
5428
03:28:43,680 --> 03:28:47,040
and combine the odd as well
5429
03:28:47,040 --> 03:28:49,279
not to forget we always have to return
5430
03:28:49,279 --> 03:28:51,520
something so if we don't have any case
5431
03:28:51,520 --> 03:28:54,000
in here i'm going to return just the old
5432
03:28:54,000 --> 03:28:56,319
state but in this case we also need to
5433
03:28:56,319 --> 03:28:58,479
return something so for now until we're
5434
03:28:58,479 --> 03:29:00,239
just cons logging things i'm going to
5435
03:29:00,239 --> 03:29:02,080
just return the state
5436
03:29:02,080 --> 03:29:04,160
and right now our state is undefined so
5437
03:29:04,160 --> 03:29:06,080
we have to give it a default value so
5438
03:29:06,080 --> 03:29:08,479
i'm going to say equal to and now i'm
5439
03:29:08,479 --> 03:29:11,920
going to say odd data is equal to null
5440
03:29:11,920 --> 03:29:14,319
so if we don't have anything our odd
5441
03:29:14,319 --> 03:29:16,640
theta of the state is simply going to be
5442
03:29:16,640 --> 03:29:18,239
set to nothing
5443
03:29:18,239 --> 03:29:20,160
if we save that let's take a look what
5444
03:29:20,160 --> 03:29:22,640
do we get
5445
03:29:23,279 --> 03:29:25,760
okay one more time through the process
5446
03:29:25,760 --> 03:29:28,479
click google sign in
5447
03:29:28,479 --> 03:29:30,560
okay and take a look this is our whole
5448
03:29:30,560 --> 03:29:33,040
thing we have a result in here we have
5449
03:29:33,040 --> 03:29:36,160
javascript mastery we have our google id
5450
03:29:36,160 --> 03:29:38,880
and we have our token
5451
03:29:38,880 --> 03:29:40,720
that's all that we need
5452
03:29:40,720 --> 03:29:42,720
so what do we do with that well we want
5453
03:29:42,720 --> 03:29:45,200
to save it in the local storage because
5454
03:29:45,200 --> 03:29:47,520
that way once we refresh the page the
5455
03:29:47,520 --> 03:29:49,200
browser is still going to know that we
5456
03:29:49,200 --> 03:29:50,640
are logged in
5457
03:29:50,640 --> 03:29:54,560
so to do that we can say local storage
5458
03:29:54,560 --> 03:29:58,640
dot set item is equal to profile
5459
03:29:58,640 --> 03:30:02,000
then we want to say json.stringify
5460
03:30:02,000 --> 03:30:05,840
and then there we want to say dot action
5461
03:30:05,840 --> 03:30:07,120
question mark
5462
03:30:07,120 --> 03:30:08,239
data
5463
03:30:08,239 --> 03:30:10,640
this is it so we are setting all of this
5464
03:30:10,640 --> 03:30:12,319
that we have all of the data for the
5465
03:30:12,319 --> 03:30:14,840
login we're setting that to the local
5466
03:30:14,840 --> 03:30:17,600
storage finally at the end we want to
5467
03:30:17,600 --> 03:30:20,239
return something we're going to return
5468
03:30:20,239 --> 03:30:21,760
dot state
5469
03:30:21,760 --> 03:30:24,399
and then we want to specify the odd data
5470
03:30:24,399 --> 03:30:28,160
to be equal to action dot data
5471
03:30:28,160 --> 03:30:30,160
and this action data is also going to
5472
03:30:30,160 --> 03:30:32,000
use optional chaining
5473
03:30:32,000 --> 03:30:34,000
so this is it for the auth
5474
03:30:34,000 --> 03:30:35,920
now that we have this return right there
5475
03:30:35,920 --> 03:30:38,000
and we are setting our profile with the
5476
03:30:38,000 --> 03:30:40,080
data field for the user we can try to
5477
03:30:40,080 --> 03:30:41,439
use it somewhere
5478
03:30:41,439 --> 03:30:43,040
the best place to use it would be a
5479
03:30:43,040 --> 03:30:45,520
navbar because in here we already have
5480
03:30:45,520 --> 03:30:47,840
our user which is currently set to null
5481
03:30:47,840 --> 03:30:50,479
so now let's try to add a real user in
5482
03:30:50,479 --> 03:30:51,600
here
5483
03:30:51,600 --> 03:30:53,840
to do that we can create a new state
5484
03:30:53,840 --> 03:30:55,120
field
5485
03:30:55,120 --> 03:30:58,000
and we can do something like user const
5486
03:30:58,000 --> 03:30:58,960
user
5487
03:30:58,960 --> 03:31:00,800
and set user
5488
03:31:00,800 --> 03:31:03,840
and finally in there we can use the use
5489
03:31:03,840 --> 03:31:05,920
state
5490
03:31:05,920 --> 03:31:08,080
use state of course has to be imported
5491
03:31:08,080 --> 03:31:09,760
so inside of here i'm going to import
5492
03:31:09,760 --> 03:31:12,560
the use state as well as the use effect
5493
03:31:12,560 --> 03:31:15,200
because we'll need to use that later on
5494
03:31:15,200 --> 03:31:16,880
now this use effect is going to be a bit
5495
03:31:16,880 --> 03:31:19,200
different we're immediately going to try
5496
03:31:19,200 --> 03:31:21,520
to fetch something and that something is
5497
03:31:21,520 --> 03:31:24,080
going to be the actual user so how do we
5498
03:31:24,080 --> 03:31:25,520
retrieve something from the local
5499
03:31:25,520 --> 03:31:26,640
storage
5500
03:31:26,640 --> 03:31:29,439
we say json.parse
5501
03:31:29,439 --> 03:31:32,439
localstorage.getitem
5502
03:31:32,479 --> 03:31:34,239
and then you specify the name of that
5503
03:31:34,239 --> 03:31:36,720
item in this case it is going to be the
5504
03:31:36,720 --> 03:31:38,960
profile so this is how we can get the
5505
03:31:38,960 --> 03:31:41,359
user right here
5506
03:31:41,359 --> 03:31:44,080
now that we have the user let's try to
5507
03:31:44,080 --> 03:31:46,160
simply cons a login
5508
03:31:46,160 --> 03:31:48,800
console.log user i'm going to go back to
5509
03:31:48,800 --> 03:31:51,520
the browser and refresh the page
5510
03:31:51,520 --> 03:31:53,520
so currently the user is null because
5511
03:31:53,520 --> 03:31:55,760
remember last time we didn't save it in
5512
03:31:55,760 --> 03:31:57,200
the local storage
5513
03:31:57,200 --> 03:31:59,600
so if i open the application tab and
5514
03:31:59,600 --> 03:32:02,239
right here refresh you can see we don't
5515
03:32:02,239 --> 03:32:04,640
have anything in the local storage but
5516
03:32:04,640 --> 03:32:06,880
now let me try to log in one more time
5517
03:32:06,880 --> 03:32:09,040
with my google account
5518
03:32:09,040 --> 03:32:11,439
and there we go we have our complete
5519
03:32:11,439 --> 03:32:12,720
information
5520
03:32:12,720 --> 03:32:15,760
now if i go to memories
5521
03:32:15,760 --> 03:32:18,080
it are we going to get the console log
5522
03:32:18,080 --> 03:32:19,520
with that profile
5523
03:32:19,520 --> 03:32:22,560
it is still null if we refresh though it
5524
03:32:22,560 --> 03:32:25,040
is there and as you can see
5525
03:32:25,040 --> 03:32:26,560
this automatically filled with
5526
03:32:26,560 --> 03:32:30,080
javascript mastery and my icon so that
5527
03:32:30,080 --> 03:32:31,600
is amazing
5528
03:32:31,600 --> 03:32:33,760
one bad thing is that we had to refresh
5529
03:32:33,760 --> 03:32:35,680
and manually head back to the localhost
5530
03:32:35,680 --> 03:32:38,560
3000 and not the login form so let's
5531
03:32:38,560 --> 03:32:40,720
re-navigate automatically so we don't
5532
03:32:40,720 --> 03:32:42,960
have to do it manually we can do that
5533
03:32:42,960 --> 03:32:45,520
using a use effect inside of here i'm
5534
03:32:45,520 --> 03:32:48,000
going to use a use effect
5535
03:32:48,000 --> 03:32:50,239
it's going to accept a callback function
5536
03:32:50,239 --> 03:32:51,520
and of course it needs to have a
5537
03:32:51,520 --> 03:32:53,840
dependency array in this use effect
5538
03:32:53,840 --> 03:32:55,279
we're going to check for the token so
5539
03:32:55,279 --> 03:32:57,840
we're going to say if token exists
5540
03:32:57,840 --> 03:32:58,800
user
5541
03:32:58,800 --> 03:33:00,800
and then question mark dot the optional
5542
03:33:00,800 --> 03:33:02,880
chaining token so we're checking if the
5543
03:33:02,880 --> 03:33:04,640
token exists and if it does we're
5544
03:33:04,640 --> 03:33:06,960
sending it to the token variable
5545
03:33:06,960 --> 03:33:08,720
then finally later on we're going to
5546
03:33:08,720 --> 03:33:10,960
check for the json web token here if we
5547
03:33:10,960 --> 03:33:12,960
do the manual sign up but now we're
5548
03:33:12,960 --> 03:33:14,960
using google so we don't have to do that
5549
03:33:14,960 --> 03:33:17,200
but what we can do is we can say set
5550
03:33:17,200 --> 03:33:20,960
user and set user is equal to again the
5551
03:33:20,960 --> 03:33:23,279
same thing that thing is going to be our
5552
03:33:23,279 --> 03:33:28,040
user json.parse localstorage.getitem
5553
03:33:28,319 --> 03:33:30,479
and finally we have to do one more
5554
03:33:30,479 --> 03:33:33,279
change in the auth more specifically
5555
03:33:33,279 --> 03:33:35,840
once we dispatch this we actually have
5556
03:33:35,840 --> 03:33:38,720
to redirect back to the home so for that
5557
03:33:38,720 --> 03:33:42,080
we can use history dot push
5558
03:33:42,080 --> 03:33:44,640
like this and where do we push to we
5559
03:33:44,640 --> 03:33:46,800
push to forward slash
5560
03:33:46,800 --> 03:33:48,960
history belongs to react router dom so
5561
03:33:48,960 --> 03:33:51,439
we have to import that
5562
03:33:51,439 --> 03:33:54,399
import use history
5563
03:33:54,399 --> 03:33:58,000
from react router dom
5564
03:33:58,000 --> 03:34:00,160
and then right in here we need to say
5565
03:34:00,160 --> 03:34:02,640
const history is equal to
5566
03:34:02,640 --> 03:34:04,560
use history
5567
03:34:04,560 --> 03:34:07,760
and call it of course
5568
03:34:07,760 --> 03:34:10,560
okay so now this should redirect us
5569
03:34:10,560 --> 03:34:14,000
immediately to the home page once we log
5570
03:34:14,000 --> 03:34:14,720
in
5571
03:34:14,720 --> 03:34:16,319
let's try to log out
5572
03:34:16,319 --> 03:34:18,479
oh our logout doesn't work right now but
5573
03:34:18,479 --> 03:34:20,560
if we head manually to forward slash
5574
03:34:20,560 --> 03:34:22,080
auth
5575
03:34:22,080 --> 03:34:24,479
there we go and we can click google sign
5576
03:34:24,479 --> 03:34:25,920
in
5577
03:34:25,920 --> 03:34:28,239
once that is done as you saw we were
5578
03:34:28,239 --> 03:34:30,720
automatically redirected
5579
03:34:30,720 --> 03:34:32,160
the thing that would make sense to do
5580
03:34:32,160 --> 03:34:34,640
right now is to simply do the logout so
5581
03:34:34,640 --> 03:34:36,800
that's going to be our next step
5582
03:34:36,800 --> 03:34:38,479
to implement the logout we of course
5583
03:34:38,479 --> 03:34:41,279
have to find where this button is and as
5584
03:34:41,279 --> 03:34:43,359
you can see it is in the nav bar so if
5585
03:34:43,359 --> 03:34:46,479
we go right here let's find that button
5586
03:34:46,479 --> 03:34:49,040
logout and we're going to add an on
5587
03:34:49,040 --> 03:34:50,800
click listener
5588
03:34:50,800 --> 03:34:52,560
what are we going to do on the on click
5589
03:34:52,560 --> 03:34:54,720
we're simply going to log out
5590
03:34:54,720 --> 03:34:57,680
that logout is going to be one function
5591
03:34:57,680 --> 03:35:00,720
inside of this nav bar more specifically
5592
03:35:00,720 --> 03:35:02,239
const logout
5593
03:35:02,239 --> 03:35:04,239
is equal to an arrow function
5594
03:35:04,239 --> 03:35:06,239
and what are we going to do we're going
5595
03:35:06,239 --> 03:35:08,720
to dispatch an action so we have to
5596
03:35:08,720 --> 03:35:10,720
first import
5597
03:35:10,720 --> 03:35:11,760
use
5598
03:35:11,760 --> 03:35:13,279
dispatch
5599
03:35:13,279 --> 03:35:14,239
from
5600
03:35:14,239 --> 03:35:16,239
react redux
5601
03:35:16,239 --> 03:35:18,640
and then in here at the top we need to
5602
03:35:18,640 --> 03:35:23,120
say const dispatch is equal to use
5603
03:35:23,120 --> 03:35:24,800
dispatch
5604
03:35:24,800 --> 03:35:26,560
there we go
5605
03:35:26,560 --> 03:35:28,560
now in the log out what we can simply
5606
03:35:28,560 --> 03:35:31,760
say is dispatch and we're dispatching an
5607
03:35:31,760 --> 03:35:34,800
action that has a type of
5608
03:35:34,800 --> 03:35:38,399
we can do just a string of log out and
5609
03:35:38,399 --> 03:35:40,960
that's going to be it once we log out we
5610
03:35:40,960 --> 03:35:42,960
also want to redirect to the main route
5611
03:35:42,960 --> 03:35:44,720
so i'm going to say history
5612
03:35:44,720 --> 03:35:46,560
dot push
5613
03:35:46,560 --> 03:35:47,439
and
5614
03:35:47,439 --> 03:35:49,920
also we have to import that history so
5615
03:35:49,920 --> 03:35:52,800
at the top we're going to say import
5616
03:35:52,800 --> 03:35:54,720
use history
5617
03:35:54,720 --> 03:35:56,080
from
5618
03:35:56,080 --> 03:35:58,560
that's going to be react router dom i
5619
03:35:58,560 --> 03:36:00,080
see that we already have it so i'm going
5620
03:36:00,080 --> 03:36:02,560
to move the import right here
5621
03:36:02,560 --> 03:36:05,760
and how do we get it well cons history
5622
03:36:05,760 --> 03:36:09,520
is equal to use history
5623
03:36:09,520 --> 03:36:11,760
so once somebody logs out we're going to
5624
03:36:11,760 --> 03:36:15,120
push them to the root route to the home
5625
03:36:15,120 --> 03:36:17,120
there we go
5626
03:36:17,120 --> 03:36:18,960
what do we also have to do is we have to
5627
03:36:18,960 --> 03:36:21,760
set the user equal to null so if
5628
03:36:21,760 --> 03:36:24,000
somebody is logged out the user has to
5629
03:36:24,000 --> 03:36:25,359
be null
5630
03:36:25,359 --> 03:36:28,239
okay and i think with that right now we
5631
03:36:28,239 --> 03:36:30,239
don't even need this use effect we're
5632
03:36:30,239 --> 03:36:31,600
going to need that for the json web
5633
03:36:31,600 --> 03:36:33,760
token so i'm going to comment that out
5634
03:36:33,760 --> 03:36:36,800
and let's see if our flow now completely
5635
03:36:36,800 --> 03:36:37,760
works
5636
03:36:37,760 --> 03:36:39,120
so we're right there i'm going to
5637
03:36:39,120 --> 03:36:40,720
refresh the page
5638
03:36:40,720 --> 03:36:42,960
i logged out there we go we are logged
5639
03:36:42,960 --> 03:36:43,680
out
5640
03:36:43,680 --> 03:36:45,920
i'll check the application tab
5641
03:36:45,920 --> 03:36:48,560
looks like we still have our profile
5642
03:36:48,560 --> 03:36:50,399
there and as you can see if we refresh
5643
03:36:50,399 --> 03:36:51,840
it comes back
5644
03:36:51,840 --> 03:36:53,279
that means that we haven't fully
5645
03:36:53,279 --> 03:36:55,920
implemented the logout dispatch
5646
03:36:55,920 --> 03:36:58,160
so we need to go to reducers
5647
03:36:58,160 --> 03:37:00,560
off and then in here we have to do
5648
03:37:00,560 --> 03:37:02,560
something if we log out more
5649
03:37:02,560 --> 03:37:04,560
specifically it is going to be really
5650
03:37:04,560 --> 03:37:05,600
simple
5651
03:37:05,600 --> 03:37:09,120
if we have the logout case like this
5652
03:37:09,120 --> 03:37:10,880
then what we have to do is say
5653
03:37:10,880 --> 03:37:13,600
localstorage.clear
5654
03:37:13,600 --> 03:37:15,279
this is going to completely clear the
5655
03:37:15,279 --> 03:37:17,040
entire local storage
5656
03:37:17,040 --> 03:37:19,600
and what we want to return well the same
5657
03:37:19,600 --> 03:37:22,080
thing that we're turning right here
5658
03:37:22,080 --> 03:37:24,960
except the odd data is going to stay
5659
03:37:24,960 --> 03:37:26,160
null
5660
03:37:26,160 --> 03:37:28,880
okay great so now if we go back
5661
03:37:28,880 --> 03:37:31,520
refresh the page we have our javascript
5662
03:37:31,520 --> 03:37:34,399
mastery there i'm going to click log out
5663
03:37:34,399 --> 03:37:36,560
as you can see the console was cleared
5664
03:37:36,560 --> 03:37:39,359
and if i refresh there we go our user is
5665
03:37:39,359 --> 03:37:40,880
still logged out
5666
03:37:40,880 --> 03:37:42,880
now let's try to go to the whole process
5667
03:37:42,880 --> 03:37:44,800
of signing in
5668
03:37:44,800 --> 03:37:46,160
go here
5669
03:37:46,160 --> 03:37:48,479
sign in using google
5670
03:37:48,479 --> 03:37:50,880
there we go we are logged in
5671
03:37:50,880 --> 03:37:52,720
and as you can see our profile is
5672
03:37:52,720 --> 03:37:55,359
populated and when we refresh we get
5673
03:37:55,359 --> 03:37:57,200
this javascript mastery
5674
03:37:57,200 --> 03:37:59,279
i don't like that we had to refresh to
5675
03:37:59,279 --> 03:38:02,319
get the user who's logged in name so we
5676
03:38:02,319 --> 03:38:04,800
have to go back and looks like that in
5677
03:38:04,800 --> 03:38:06,399
the nav bar
5678
03:38:06,399 --> 03:38:08,960
which is right here we still need that
5679
03:38:08,960 --> 03:38:10,800
use effect so i'm going to add that use
5680
03:38:10,800 --> 03:38:11,680
effect
5681
03:38:11,680 --> 03:38:13,279
so when do we want to call this use
5682
03:38:13,279 --> 03:38:15,520
effect as soon as the url changes from
5683
03:38:15,520 --> 03:38:17,520
the forward slash auth to the simply
5684
03:38:17,520 --> 03:38:19,040
forward slash
5685
03:38:19,040 --> 03:38:22,239
and we can use the property use location
5686
03:38:22,239 --> 03:38:24,720
from react router dom we need to do the
5687
03:38:24,720 --> 03:38:28,720
same thing const location is equal to
5688
03:38:28,720 --> 03:38:31,520
use location
5689
03:38:31,520 --> 03:38:34,080
and based on this we'll have access to
5690
03:38:34,080 --> 03:38:36,560
that change when this changes we want to
5691
03:38:36,560 --> 03:38:38,960
simply call the user when location
5692
03:38:38,960 --> 03:38:41,120
changes i can put that right here
5693
03:38:41,120 --> 03:38:43,359
when location changes simply set the
5694
03:38:43,359 --> 03:38:44,399
user
5695
03:38:44,399 --> 03:38:46,319
now let's take a look if that's going to
5696
03:38:46,319 --> 03:38:48,239
work
5697
03:38:48,239 --> 03:38:49,840
if i go back
5698
03:38:49,840 --> 03:38:51,760
log out one more time
5699
03:38:51,760 --> 03:38:52,840
log
5700
03:38:52,840 --> 03:38:56,640
in and there we go now we immediately
5701
03:38:56,640 --> 03:38:58,640
got this user right there because we
5702
03:38:58,640 --> 03:39:01,040
automatically set the user profile once
5703
03:39:01,040 --> 03:39:03,439
we came back from the forward slash auth
5704
03:39:03,439 --> 03:39:06,720
and it was populated
5705
03:39:06,720 --> 03:39:08,960
amazing and now if you refresh the page
5706
03:39:08,960 --> 03:39:11,040
go back go somewhere else you're still
5707
03:39:11,040 --> 03:39:12,960
going to be logged in for i think about
5708
03:39:12,960 --> 03:39:15,439
an hour because our token lasts for
5709
03:39:15,439 --> 03:39:16,880
about an hour
5710
03:39:16,880 --> 03:39:19,040
or if you want to manually log out you
5711
03:39:19,040 --> 03:39:21,359
can simply do that right away
5712
03:39:21,359 --> 03:39:23,600
crazy how simple the google all aud
5713
03:39:23,600 --> 03:39:26,080
system is right we implemented this in a
5714
03:39:26,080 --> 03:39:28,160
really short amount of time
5715
03:39:28,160 --> 03:39:30,319
next step is to implement the manual
5716
03:39:30,319 --> 03:39:33,040
login and registration system using the
5717
03:39:33,040 --> 03:39:35,279
json web token for that we're going to
5718
03:39:35,279 --> 03:39:37,520
use this form that we created let's
5719
03:39:37,520 --> 03:39:39,120
start working on that
5720
03:39:39,120 --> 03:39:41,279
i'm going to close all of these files
5721
03:39:41,279 --> 03:39:42,880
just so we have a clean working
5722
03:39:42,880 --> 03:39:44,880
environment i'm also going to collapse
5723
03:39:44,880 --> 03:39:46,800
all the files and folders
5724
03:39:46,800 --> 03:39:48,800
so this is now going to be a first time
5725
03:39:48,800 --> 03:39:50,800
in this video that we are going to focus
5726
03:39:50,800 --> 03:39:52,880
on the back end as well because we have
5727
03:39:52,880 --> 03:39:54,640
to create the models for the user as
5728
03:39:54,640 --> 03:39:56,319
well as the controllers and the routes
5729
03:39:56,319 --> 03:39:59,040
for the login and registration for now
5730
03:39:59,040 --> 03:40:01,040
let's handle the client side of the
5731
03:40:01,040 --> 03:40:03,920
authentication system to do that we have
5732
03:40:03,920 --> 03:40:06,000
to go to our odd form
5733
03:40:06,000 --> 03:40:07,680
right inside of here
5734
03:40:07,680 --> 03:40:09,279
and now remember
5735
03:40:09,279 --> 03:40:10,960
whenever you're working on something
5736
03:40:10,960 --> 03:40:13,279
take a look at the actual jsx and then
5737
03:40:13,279 --> 03:40:16,239
ask yourself when is this happening and
5738
03:40:16,239 --> 03:40:18,960
the answer is our login and registration
5739
03:40:18,960 --> 03:40:20,960
is happening after the user clicks this
5740
03:40:20,960 --> 03:40:23,520
button that means that we have to focus
5741
03:40:23,520 --> 03:40:26,080
to implement actions once this button is
5742
03:40:26,080 --> 03:40:27,520
clicked
5743
03:40:27,520 --> 03:40:29,920
going back to here we can see that our
5744
03:40:29,920 --> 03:40:32,239
button is right here if you take a
5745
03:40:32,239 --> 03:40:34,080
closer look at this button you can see
5746
03:40:34,080 --> 03:40:35,920
that once you click it it is of type
5747
03:40:35,920 --> 03:40:37,920
submit that means that the only thing it
5748
03:40:37,920 --> 03:40:41,120
does is it calls this on submit function
5749
03:40:41,120 --> 03:40:43,120
in this case the on submit function is
5750
03:40:43,120 --> 03:40:45,600
the handle submit so let's see where is
5751
03:40:45,600 --> 03:40:46,640
that
5752
03:40:46,640 --> 03:40:49,279
and there we go it's right in here more
5753
03:40:49,279 --> 03:40:51,359
specifically it is empty so we're not
5754
03:40:51,359 --> 03:40:53,680
doing anything with it right now now
5755
03:40:53,680 --> 03:40:55,520
what do we have to do with it to make
5756
03:40:55,520 --> 03:40:57,359
our login work
5757
03:40:57,359 --> 03:40:59,359
first thing to do would be to check if
5758
03:40:59,359 --> 03:41:01,840
we have access to all of the state
5759
03:41:01,840 --> 03:41:04,560
fields more specifically are the values
5760
03:41:04,560 --> 03:41:07,040
of these inputs in the state and right
5761
03:41:07,040 --> 03:41:09,359
now we cannot see that we are mentioning
5762
03:41:09,359 --> 03:41:11,120
state nowhere right
5763
03:41:11,120 --> 03:41:12,960
and while we're here i also noticed that
5764
03:41:12,960 --> 03:41:15,760
this should be last name and also in
5765
03:41:15,760 --> 03:41:18,080
here a last name
5766
03:41:18,080 --> 03:41:19,760
yeah so we are never ever mentioning
5767
03:41:19,760 --> 03:41:22,319
state so how can we do that well let's
5768
03:41:22,319 --> 03:41:25,439
create a new state field right inside of
5769
03:41:25,439 --> 03:41:27,359
here const and we can call it something
5770
03:41:27,359 --> 03:41:32,800
like form data form data set form data
5771
03:41:32,800 --> 03:41:35,199
and that is going to be equal to use
5772
03:41:35,199 --> 03:41:36,399
state
5773
03:41:36,399 --> 03:41:38,239
of course we have to make this use state
5774
03:41:38,239 --> 03:41:40,239
at the start be equal to something so
5775
03:41:40,239 --> 03:41:42,000
just at the top of our component let's
5776
03:41:42,000 --> 03:41:44,080
create a new field
5777
03:41:44,080 --> 03:41:47,120
const initial state is going to be equal
5778
03:41:47,120 --> 03:41:49,439
to and now in here we have to put the
5779
03:41:49,439 --> 03:41:51,199
initial values for all of the state
5780
03:41:51,199 --> 03:41:52,239
fields
5781
03:41:52,239 --> 03:41:54,080
that is going to be for example the
5782
03:41:54,080 --> 03:41:56,399
first name which is going to be equal to
5783
03:41:56,399 --> 03:41:59,199
an empty string then we have the last
5784
03:41:59,199 --> 03:42:02,080
name also equal to an empty string
5785
03:42:02,080 --> 03:42:03,920
we have the email
5786
03:42:03,920 --> 03:42:05,920
again everything is going to be an empty
5787
03:42:05,920 --> 03:42:06,800
string
5788
03:42:06,800 --> 03:42:08,399
we have the password
5789
03:42:08,399 --> 03:42:10,880
and we have the confirm
5790
03:42:10,880 --> 03:42:12,399
password
5791
03:42:12,399 --> 03:42:15,040
also equal to an empty string
5792
03:42:15,040 --> 03:42:16,560
and that is going to be it for our
5793
03:42:16,560 --> 03:42:19,040
initial state now how do we use it well
5794
03:42:19,040 --> 03:42:21,120
you simply put it in here that is going
5795
03:42:21,120 --> 03:42:24,080
to be the initial state of the form data
5796
03:42:24,080 --> 03:42:26,880
okay now let's try to console.log the
5797
03:42:26,880 --> 03:42:28,239
form data
5798
03:42:28,239 --> 03:42:29,920
on submit
5799
03:42:29,920 --> 03:42:32,080
and obviously immediately considering
5800
03:42:32,080 --> 03:42:34,080
that we are never setting form data we
5801
03:42:34,080 --> 03:42:36,160
expect that to be empty so let's take a
5802
03:42:36,160 --> 03:42:37,359
look
5803
03:42:37,359 --> 03:42:39,359
if we go back in here if i type
5804
03:42:39,359 --> 03:42:42,160
something and if i type a password i'm
5805
03:42:42,160 --> 03:42:44,960
going to open up the console right here
5806
03:42:44,960 --> 03:42:47,359
and click sign in
5807
03:42:47,359 --> 03:42:49,040
and first of all it immediately
5808
03:42:49,040 --> 03:42:50,800
refreshed that's because it is the
5809
03:42:50,800 --> 03:42:52,560
default behavior of the browser to
5810
03:42:52,560 --> 03:42:55,040
refresh on form submit so we have to add
5811
03:42:55,040 --> 03:42:58,000
the event in here we get the event
5812
03:42:58,000 --> 03:42:59,680
e dot prevent
5813
03:42:59,680 --> 03:43:01,920
default
5814
03:43:01,920 --> 03:43:03,760
that is something that you always add on
5815
03:43:03,760 --> 03:43:05,920
the form submit in react because there
5816
03:43:05,920 --> 03:43:08,319
we don't prefer reloads we always want
5817
03:43:08,319 --> 03:43:10,479
to have something on the screen
5818
03:43:10,479 --> 03:43:12,160
so with that said let's see if we get
5819
03:43:12,160 --> 03:43:13,920
something there i'm going to type the
5820
03:43:13,920 --> 03:43:15,920
same thing one more time
5821
03:43:15,920 --> 03:43:17,840
and click sign in
5822
03:43:17,840 --> 03:43:20,319
and as we expected we got all of the
5823
03:43:20,319 --> 03:43:22,080
fields there because that is our initial
5824
03:43:22,080 --> 03:43:24,880
state but they are all an empty string
5825
03:43:24,880 --> 03:43:27,040
now the question is how can we populate
5826
03:43:27,040 --> 03:43:28,560
these fields
5827
03:43:28,560 --> 03:43:30,640
and the easy answer is to look just a
5828
03:43:30,640 --> 03:43:33,279
bit below our handle submit we have to
5829
03:43:33,279 --> 03:43:34,479
handle change
5830
03:43:34,479 --> 03:43:36,640
and each one of our inputs is using that
5831
03:43:36,640 --> 03:43:40,560
handle change but right now it is empty
5832
03:43:40,560 --> 03:43:42,960
so what do we have to do to fill it well
5833
03:43:42,960 --> 03:43:45,760
in here we get the actual event and then
5834
03:43:45,760 --> 03:43:49,199
what we can do is say set form data we
5835
03:43:49,199 --> 03:43:50,800
get an object right
5836
03:43:50,800 --> 03:43:52,720
we spread the form
5837
03:43:52,720 --> 03:43:54,880
dot form data
5838
03:43:54,880 --> 03:43:57,920
and then we update only a specific input
5839
03:43:57,920 --> 03:44:00,000
that we're currently managing
5840
03:44:00,000 --> 03:44:01,840
and the question is how can we know on
5841
03:44:01,840 --> 03:44:04,560
which input are we currently well each
5842
03:44:04,560 --> 03:44:07,680
one of the inputs has its own name
5843
03:44:07,680 --> 03:44:09,359
first of all you have to make sure that
5844
03:44:09,359 --> 03:44:11,680
your names are exactly the same as the
5845
03:44:11,680 --> 03:44:14,000
names in the initial state right here
5846
03:44:14,000 --> 03:44:16,479
first name last name email password
5847
03:44:16,479 --> 03:44:18,880
confirm password and so on
5848
03:44:18,880 --> 03:44:21,040
if that's the case you can use these
5849
03:44:21,040 --> 03:44:23,600
square brackets right there and say e
5850
03:44:23,600 --> 03:44:26,720
dot target dot name
5851
03:44:26,720 --> 03:44:29,920
and that's going to be equal to e dot
5852
03:44:29,920 --> 03:44:32,160
target dot value
5853
03:44:32,160 --> 03:44:34,080
this is going to make sure to spread all
5854
03:44:34,080 --> 03:44:36,960
of the other properties but only change
5855
03:44:36,960 --> 03:44:39,120
the one specific one you're currently on
5856
03:44:39,120 --> 03:44:41,040
with the target value that means the
5857
03:44:41,040 --> 03:44:44,000
current input that we have in there with
5858
03:44:44,000 --> 03:44:46,160
that said if we save that let's try one
5859
03:44:46,160 --> 03:44:48,399
more time and see what do we have in
5860
03:44:48,399 --> 03:44:49,920
this form
5861
03:44:49,920 --> 03:44:52,720
so i'll first try to enter something
5862
03:44:52,720 --> 03:44:55,199
and there we go we have our email and we
5863
03:44:55,199 --> 03:44:58,160
have our password if i switch to here we
5864
03:44:58,160 --> 03:44:59,840
have our first name
5865
03:44:59,840 --> 03:45:03,040
and if i click sign up there we go we
5866
03:45:03,040 --> 03:45:05,920
have all of our values listed right in
5867
03:45:05,920 --> 03:45:08,640
here so that's why we had to use the
5868
03:45:08,640 --> 03:45:10,960
same handle change function for each one
5869
03:45:10,960 --> 03:45:13,120
because otherwise because otherwise you
5870
03:45:13,120 --> 03:45:14,800
would have to change each state field
5871
03:45:14,800 --> 03:45:17,199
separately which is quite a lot of code
5872
03:45:17,199 --> 03:45:19,359
this way it is a lot easier this is
5873
03:45:19,359 --> 03:45:22,000
dynamic and works for infinite number of
5874
03:45:22,000 --> 03:45:24,319
fields with that said that's great now
5875
03:45:24,319 --> 03:45:25,840
we have our handle submit but the
5876
03:45:25,840 --> 03:45:28,080
question is what do we want to do on
5877
03:45:28,080 --> 03:45:29,520
that submit
5878
03:45:29,520 --> 03:45:30,720
well they're going to be two different
5879
03:45:30,720 --> 03:45:32,160
types of submits
5880
03:45:32,160 --> 03:45:34,880
one is going to be our sign up and the
5881
03:45:34,880 --> 03:45:37,520
other one is going to be our sign in
5882
03:45:37,520 --> 03:45:39,520
so we already have a variable to check
5883
03:45:39,520 --> 03:45:42,560
that so we can say if is sign up in that
5884
03:45:42,560 --> 03:45:44,319
case we want to do the logic to sign up
5885
03:45:44,319 --> 03:45:45,600
the user
5886
03:45:45,600 --> 03:45:47,760
else if we're not on the sign up we want
5887
03:45:47,760 --> 03:45:50,399
to do the logic to sign in the user so
5888
03:45:50,399 --> 03:45:52,399
in this case what we want to do is we
5889
03:45:52,399 --> 03:45:54,960
want to dispatch an action
5890
03:45:54,960 --> 03:45:56,560
and more specifically that is going to
5891
03:45:56,560 --> 03:45:59,040
be a sign up action
5892
03:45:59,040 --> 03:46:01,520
and to that action we want to pass in
5893
03:46:01,520 --> 03:46:04,399
the entire form data and we also want to
5894
03:46:04,399 --> 03:46:07,040
pass the history we pass the form data
5895
03:46:07,040 --> 03:46:09,120
so well we can have it in our database
5896
03:46:09,120 --> 03:46:11,359
of course but we pass the history object
5897
03:46:11,359 --> 03:46:13,359
so that we can navigate once something
5898
03:46:13,359 --> 03:46:14,319
happens
5899
03:46:14,319 --> 03:46:17,040
okay great and the other one we can copy
5900
03:46:17,040 --> 03:46:19,279
this and simply put it right there this
5901
03:46:19,279 --> 03:46:22,880
one is going to be sign in all of these
5902
03:46:22,880 --> 03:46:25,040
are actions that we have to import first
5903
03:46:25,040 --> 03:46:27,680
so just above i'm going to import these
5904
03:46:27,680 --> 03:46:29,439
two actions
5905
03:46:29,439 --> 03:46:32,160
inside of here i'm going to say import
5906
03:46:32,160 --> 03:46:35,600
sign in and sign up and that's going to
5907
03:46:35,600 --> 03:46:36,880
be from
5908
03:46:36,880 --> 03:46:39,439
dot dot slash dot slash
5909
03:46:39,439 --> 03:46:41,680
actions and then auth
5910
03:46:41,680 --> 03:46:43,760
this file doesn't exist yet but we're
5911
03:46:43,760 --> 03:46:46,239
going to create it right now
5912
03:46:46,239 --> 03:46:48,319
okay so if we save that we're ready to
5913
03:46:48,319 --> 03:46:50,560
start creating the actions file
5914
03:46:50,560 --> 03:46:52,880
that's going to be inside of the actions
5915
03:46:52,880 --> 03:46:56,960
and we simply need to do auth.js
5916
03:46:56,960 --> 03:46:59,199
inside of here we can start creating our
5917
03:46:59,199 --> 03:47:00,880
odd actions
5918
03:47:00,880 --> 03:47:02,239
first of all we're going to need some
5919
03:47:02,239 --> 03:47:04,720
things from here like constants action
5920
03:47:04,720 --> 03:47:07,760
types and all the things from the api
5921
03:47:07,760 --> 03:47:09,840
so let's copy these and simply paste
5922
03:47:09,840 --> 03:47:11,520
them right here
5923
03:47:11,520 --> 03:47:13,760
in this case we won't need those the
5924
03:47:13,760 --> 03:47:16,080
ones that we need is simply going to be
5925
03:47:16,080 --> 03:47:17,279
auth
5926
03:47:17,279 --> 03:47:19,760
and from the api later on we're gonna
5927
03:47:19,760 --> 03:47:22,080
use some of the calls but right now we
5928
03:47:22,080 --> 03:47:24,479
don't need anything so let's start with
5929
03:47:24,479 --> 03:47:26,160
creating the actions while we're
5930
03:47:26,160 --> 03:47:28,160
creating them we can also export them
5931
03:47:28,160 --> 03:47:31,279
that's gonna mean export const and we
5932
03:47:31,279 --> 03:47:34,000
named it sign in
5933
03:47:34,000 --> 03:47:36,160
that is going to be an action creator
5934
03:47:36,160 --> 03:47:38,160
which is a function that simply returns
5935
03:47:38,160 --> 03:47:39,199
an action
5936
03:47:39,199 --> 03:47:41,439
and as we discussed in the last video if
5937
03:47:41,439 --> 03:47:43,279
actions or more specifically action
5938
03:47:43,279 --> 03:47:45,840
creators are asynchronous then we have
5939
03:47:45,840 --> 03:47:48,239
to use redux tank meaning we have a
5940
03:47:48,239 --> 03:47:51,199
function that returns an async function
5941
03:47:51,199 --> 03:47:53,680
with a dispatch i know it's a crazy
5942
03:47:53,680 --> 03:47:55,680
syntax but we just have to follow the
5943
03:47:55,680 --> 03:47:57,279
rules in this case
5944
03:47:57,279 --> 03:48:00,239
so we have this and then we have async
5945
03:48:00,239 --> 03:48:02,720
dispatch function which then has a
5946
03:48:02,720 --> 03:48:04,000
function block
5947
03:48:04,000 --> 03:48:06,800
in here we get what we passed into our
5948
03:48:06,800 --> 03:48:09,199
function so we passed the form data and
5949
03:48:09,199 --> 03:48:11,279
we passed in the history so we can
5950
03:48:11,279 --> 03:48:15,920
repeat that here form data and history
5951
03:48:15,920 --> 03:48:17,920
if we're in an async block of code we
5952
03:48:17,920 --> 03:48:20,319
can use the try and catch and now in
5953
03:48:20,319 --> 03:48:22,319
here we can do the logic
5954
03:48:22,319 --> 03:48:23,680
if you remember what we were doing in
5955
03:48:23,680 --> 03:48:24,960
the posts
5956
03:48:24,960 --> 03:48:27,120
actions you can see that in here we were
5957
03:48:27,120 --> 03:48:28,640
trying to fetch some of the things from
5958
03:48:28,640 --> 03:48:30,479
the database and that's going to be the
5959
03:48:30,479 --> 03:48:32,399
same thing that we're going to do right
5960
03:48:32,399 --> 03:48:33,520
in here
5961
03:48:33,520 --> 03:48:35,040
in here we're going to try to get the
5962
03:48:35,040 --> 03:48:37,040
data for the sign in or rather we're
5963
03:48:37,040 --> 03:48:38,880
going to send the data to the database
5964
03:48:38,880 --> 03:48:40,960
or to the backend so that it knows to
5965
03:48:40,960 --> 03:48:42,560
sign in the user
5966
03:48:42,560 --> 03:48:44,800
right now we don't have the backend
5967
03:48:44,800 --> 03:48:47,359
endpoints for this to actually work so
5968
03:48:47,359 --> 03:48:49,040
this is going to be a good time to
5969
03:48:49,040 --> 03:48:51,359
transfer back to the backend before we
5970
03:48:51,359 --> 03:48:53,120
do that though let's just create a
5971
03:48:53,120 --> 03:48:55,199
complete mock of the sign in and the
5972
03:48:55,199 --> 03:48:57,120
sign up and then we'll move to the back
5973
03:48:57,120 --> 03:48:57,920
end
5974
03:48:57,920 --> 03:49:01,279
so for now in here i'm just gonna say
5975
03:49:01,279 --> 03:49:03,359
log in the user
5976
03:49:03,359 --> 03:49:05,439
and then finally why do we have that
5977
03:49:05,439 --> 03:49:07,359
history because we want to navigate to
5978
03:49:07,359 --> 03:49:09,520
the home page so that means that we can
5979
03:49:09,520 --> 03:49:12,319
do history dot push and then simply push
5980
03:49:12,319 --> 03:49:13,600
the slash
5981
03:49:13,600 --> 03:49:15,840
after we log in the user we push to the
5982
03:49:15,840 --> 03:49:18,560
home page and then in the catch for now
5983
03:49:18,560 --> 03:49:21,600
we can simply console log the error
5984
03:49:21,600 --> 03:49:24,000
okay and now i'm going to copy this and
5985
03:49:24,000 --> 03:49:27,199
do the same thing for the sign up i'm
5986
03:49:27,199 --> 03:49:29,520
going to just rename this to sign up
5987
03:49:29,520 --> 03:49:31,359
we also get the form data and the
5988
03:49:31,359 --> 03:49:33,840
history we get the dispatch and in this
5989
03:49:33,840 --> 03:49:35,439
case we need to
5990
03:49:35,439 --> 03:49:39,600
sign up the user and then finally we
5991
03:49:39,600 --> 03:49:41,439
navigate to the home page and console
5992
03:49:41,439 --> 03:49:42,720
log the error
5993
03:49:42,720 --> 03:49:44,399
and now we're pretty much done with the
5994
03:49:44,399 --> 03:49:46,640
front-end side for now of course later
5995
03:49:46,640 --> 03:49:48,399
on on the back end while we create those
5996
03:49:48,399 --> 03:49:50,880
end points then in here we'll be able to
5997
03:49:50,880 --> 03:49:53,760
call them but for now there's not really
5998
03:49:53,760 --> 03:49:55,520
a lot we can do on the front-end side
5999
03:49:55,520 --> 03:49:57,199
now we need to move to the back end
6000
03:49:57,199 --> 03:49:59,199
create the user model create the
6001
03:49:59,199 --> 03:50:00,960
controllers and the routes and then
6002
03:50:00,960 --> 03:50:03,279
we'll be able to continue so let's do
6003
03:50:03,279 --> 03:50:04,560
that
6004
03:50:04,560 --> 03:50:06,560
as before i closed all the windows
6005
03:50:06,560 --> 03:50:08,720
collapsed the folders and let's start
6006
03:50:08,720 --> 03:50:11,040
with the back end we can focus on the
6007
03:50:11,040 --> 03:50:13,359
routes first more specifically in the
6008
03:50:13,359 --> 03:50:16,960
index.js we need to add the routes for
6009
03:50:16,960 --> 03:50:19,359
the users so instead of here i'm gonna
6010
03:50:19,359 --> 03:50:21,439
say app.use
6011
03:50:21,439 --> 03:50:24,399
we're gonna use the user endpoint and
6012
03:50:24,399 --> 03:50:25,920
then in that case we're gonna have the
6013
03:50:25,920 --> 03:50:29,120
routes called user routes
6014
03:50:29,120 --> 03:50:30,960
those user routes as you can see they're
6015
03:50:30,960 --> 03:50:33,920
undefined yet but we have to import them
6016
03:50:33,920 --> 03:50:36,479
as we imported the post routes
6017
03:50:36,479 --> 03:50:38,960
so i'm going to copy this line and just
6018
03:50:38,960 --> 03:50:40,640
change this to user
6019
03:50:40,640 --> 03:50:42,239
routes and that's going to be coming
6020
03:50:42,239 --> 03:50:45,239
from that slash routes and
6021
03:50:45,239 --> 03:50:47,120
users.js
6022
03:50:47,120 --> 03:50:48,800
now we know that we have to create the
6023
03:50:48,800 --> 03:50:51,040
routes right inside of here
6024
03:50:51,040 --> 03:50:53,920
users.js
6025
03:50:53,920 --> 03:50:55,920
so how are these routes going to look
6026
03:50:55,920 --> 03:50:57,920
like well first we have to have the
6027
03:50:57,920 --> 03:51:00,160
basic structure as we already have with
6028
03:51:00,160 --> 03:51:02,720
this thing we have to import express
6029
03:51:02,720 --> 03:51:04,160
from express
6030
03:51:04,160 --> 03:51:05,279
then we have to get some of the
6031
03:51:05,279 --> 03:51:07,359
controllers from our controllers right
6032
03:51:07,359 --> 03:51:09,120
now we don't have any
6033
03:51:09,120 --> 03:51:11,120
then we have to create the instance of a
6034
03:51:11,120 --> 03:51:14,239
router and finally we have to export the
6035
03:51:14,239 --> 03:51:16,479
folder router before we export the
6036
03:51:16,479 --> 03:51:19,120
router we of course have to add those
6037
03:51:19,120 --> 03:51:21,199
routes and you add them in the following
6038
03:51:21,199 --> 03:51:22,160
manner
6039
03:51:22,160 --> 03:51:24,319
router that and this is going to be a
6040
03:51:24,319 --> 03:51:28,880
post route to forward slash sign in
6041
03:51:28,880 --> 03:51:31,040
why is this a post route because you
6042
03:51:31,040 --> 03:51:34,080
have to send some data to the back end
6043
03:51:34,080 --> 03:51:36,160
more specifically you have to send all
6044
03:51:36,160 --> 03:51:38,080
of the details from the form to the
6045
03:51:38,080 --> 03:51:39,199
backend
6046
03:51:39,199 --> 03:51:42,479
the form sign in and sign up is the best
6047
03:51:42,479 --> 03:51:45,199
way to explain how does the post request
6048
03:51:45,199 --> 03:51:46,160
work
6049
03:51:46,160 --> 03:51:48,080
you need it to be a post request because
6050
03:51:48,080 --> 03:51:49,920
you have to send all of the information
6051
03:51:49,920 --> 03:51:51,840
from the login form to the backend and
6052
03:51:51,840 --> 03:51:53,520
then and then back and does something
6053
03:51:53,520 --> 03:51:55,680
based on that information in this case
6054
03:51:55,680 --> 03:51:57,760
it's going to sign in the user
6055
03:51:57,760 --> 03:51:59,600
so what are we going to do when we go to
6056
03:51:59,600 --> 03:52:02,080
the sign in well we're going to call our
6057
03:52:02,080 --> 03:52:05,040
sign in controller and i'm going to copy
6058
03:52:05,040 --> 03:52:08,000
this one more time we have the sign
6059
03:52:08,000 --> 03:52:10,720
up as well and this is going to be sign
6060
03:52:10,720 --> 03:52:11,840
up
6061
03:52:11,840 --> 03:52:13,840
obviously these are undefined right now
6062
03:52:13,840 --> 03:52:16,720
so let's import them from here sign in
6063
03:52:16,720 --> 03:52:19,279
and also sign up
6064
03:52:19,279 --> 03:52:21,040
of course these are not coming from the
6065
03:52:21,040 --> 03:52:24,319
posts they will be coming from user.js
6066
03:52:24,319 --> 03:52:27,000
of controllers so in here we can create
6067
03:52:27,000 --> 03:52:29,680
user.js of controllers and this is going
6068
03:52:29,680 --> 03:52:31,920
to be the place where we're going to put
6069
03:52:31,920 --> 03:52:33,840
all the complex logic of actually
6070
03:52:33,840 --> 03:52:36,800
signing in and signing up the user
6071
03:52:36,800 --> 03:52:38,479
in this file we're going to need a few
6072
03:52:38,479 --> 03:52:40,880
things first we're gonna need a package
6073
03:52:40,880 --> 03:52:43,040
called bcrypt
6074
03:52:43,040 --> 03:52:45,279
bcrypt is used to hash the password of
6075
03:52:45,279 --> 03:52:47,120
course if you're creating your users
6076
03:52:47,120 --> 03:52:49,439
users want to have some security if your
6077
03:52:49,439 --> 03:52:51,120
database is hacked and if you're storing
6078
03:52:51,120 --> 03:52:53,600
your passwords in a plain text everybody
6079
03:52:53,600 --> 03:52:55,760
will be able to see it if you hash the
6080
03:52:55,760 --> 03:52:58,080
passwords that won't be the case that's
6081
03:52:58,080 --> 03:53:00,560
why we use bcrypt we can say import
6082
03:53:00,560 --> 03:53:03,199
bcrypt from bcrypt
6083
03:53:03,199 --> 03:53:05,120
js like this
6084
03:53:05,120 --> 03:53:06,840
and we also need
6085
03:53:06,840 --> 03:53:12,080
jwt jwt stands for json web token and
6086
03:53:12,080 --> 03:53:14,160
that is a safe way for us to store the
6087
03:53:14,160 --> 03:53:16,800
users or more specifically store the
6088
03:53:16,800 --> 03:53:18,960
user in a browser for some period of
6089
03:53:18,960 --> 03:53:21,520
time for example for an hour 2 hour or
6090
03:53:21,520 --> 03:53:24,160
even a week that way if user leaves the
6091
03:53:24,160 --> 03:53:26,239
site he will still be able to stay
6092
03:53:26,239 --> 03:53:28,960
logged in no matter what in this file we
6093
03:53:28,960 --> 03:53:31,199
have to use the user model which we
6094
03:53:31,199 --> 03:53:33,120
haven't created yet so now would be a
6095
03:53:33,120 --> 03:53:35,520
good time to create the user model
6096
03:53:35,520 --> 03:53:37,960
inside of here i'm going to create a
6097
03:53:37,960 --> 03:53:41,359
user.js file that is inside of the
6098
03:53:41,359 --> 03:53:42,720
models
6099
03:53:42,720 --> 03:53:44,479
now the situation is going to be similar
6100
03:53:44,479 --> 03:53:47,279
to what we have with the post message we
6101
03:53:47,279 --> 03:53:49,199
have to import mongoose we have to
6102
03:53:49,199 --> 03:53:51,680
create a schema and finally we have to
6103
03:53:51,680 --> 03:53:54,160
create a model which we then export so
6104
03:53:54,160 --> 03:53:56,239
let's repeat the process
6105
03:53:56,239 --> 03:53:57,199
import
6106
03:53:57,199 --> 03:54:01,040
mongoose from mongoose
6107
03:54:01,040 --> 03:54:05,880
then const user schema is equal to
6108
03:54:05,880 --> 03:54:07,840
mongoose.schema make sure to put the
6109
03:54:07,840 --> 03:54:10,399
capital s right here and there we have
6110
03:54:10,399 --> 03:54:12,479
an object now inside of that object you
6111
03:54:12,479 --> 03:54:14,880
put all of the necessary details we have
6112
03:54:14,880 --> 03:54:16,080
a name
6113
03:54:16,080 --> 03:54:19,279
and name is going to be required so
6114
03:54:19,279 --> 03:54:21,680
inside of here we can say name
6115
03:54:21,680 --> 03:54:24,239
type is going to be of a string and
6116
03:54:24,239 --> 03:54:27,439
required is going to be set to true
6117
03:54:27,439 --> 03:54:29,279
then we have an email
6118
03:54:29,279 --> 03:54:31,840
email is also going to be of a type
6119
03:54:31,840 --> 03:54:32,880
string
6120
03:54:32,880 --> 03:54:36,960
and required is going to be set to true
6121
03:54:36,960 --> 03:54:39,199
we have a password
6122
03:54:39,199 --> 03:54:41,520
password is also going to be of a type
6123
03:54:41,520 --> 03:54:43,920
string and the required is going to be
6124
03:54:43,920 --> 03:54:45,680
set to true
6125
03:54:45,680 --> 03:54:48,160
and then we have the id
6126
03:54:48,160 --> 03:54:52,080
id is going to have a type of string
6127
03:54:52,080 --> 03:54:54,479
and that is going to be it for this user
6128
03:54:54,479 --> 03:54:55,439
schema
6129
03:54:55,439 --> 03:54:58,720
finally we can say export default
6130
03:54:58,720 --> 03:55:01,439
mongoose dot model
6131
03:55:01,439 --> 03:55:03,199
you need to specify the user model with
6132
03:55:03,199 --> 03:55:04,640
a capital u
6133
03:55:04,640 --> 03:55:06,880
and you need to specify a schema on
6134
03:55:06,880 --> 03:55:08,880
which that model is built upon and in
6135
03:55:08,880 --> 03:55:10,800
this case that is going to be the user
6136
03:55:10,800 --> 03:55:11,840
schema
6137
03:55:11,840 --> 03:55:14,160
now in the controllers we can import
6138
03:55:14,160 --> 03:55:16,479
that model the user model and then based
6139
03:55:16,479 --> 03:55:18,399
on that we can create a lot of instances
6140
03:55:18,399 --> 03:55:21,199
of users our memories learn project
6141
03:55:21,199 --> 03:55:22,239
users
6142
03:55:22,239 --> 03:55:24,479
so for that we can say import
6143
03:55:24,479 --> 03:55:26,239
user from
6144
03:55:26,239 --> 03:55:30,880
dot slash models slash user dot js
6145
03:55:30,880 --> 03:55:33,760
and now we can create our controllers we
6146
03:55:33,760 --> 03:55:35,520
know that we need to have two one is
6147
03:55:35,520 --> 03:55:39,040
going to be export const sign in and
6148
03:55:39,040 --> 03:55:42,160
that is going to be an async function on
6149
03:55:42,160 --> 03:55:44,080
all of the controllers we have the rec
6150
03:55:44,080 --> 03:55:45,760
and the res which is the request and the
6151
03:55:45,760 --> 03:55:46,880
response
6152
03:55:46,880 --> 03:55:49,439
and we have a function block
6153
03:55:49,439 --> 03:55:51,760
this is going to be the function for the
6154
03:55:51,760 --> 03:55:54,319
sign in and i'm going to copy this and
6155
03:55:54,319 --> 03:55:57,920
simply replicate it for the sign up
6156
03:55:57,920 --> 03:55:59,680
so now the question is how does the
6157
03:55:59,680 --> 03:56:01,680
logic for the sign in and the signup
6158
03:56:01,680 --> 03:56:04,080
look like that could be a complex part
6159
03:56:04,080 --> 03:56:04,880
right
6160
03:56:04,880 --> 03:56:08,080
well both yes and no it can be daunting
6161
03:56:08,080 --> 03:56:09,680
if you're just starting with it or if
6162
03:56:09,680 --> 03:56:11,279
you're trying to build this yourself for
6163
03:56:11,279 --> 03:56:13,120
the first time but in this case you're
6164
03:56:13,120 --> 03:56:15,120
watching this tutorial and i'll try to
6165
03:56:15,120 --> 03:56:17,760
be as thorough as possible and guide you
6166
03:56:17,760 --> 03:56:20,399
step by step through how to do this
6167
03:56:20,399 --> 03:56:23,040
usually all sign in sign up projects are
6168
03:56:23,040 --> 03:56:25,120
similar in any application you need to
6169
03:56:25,120 --> 03:56:27,920
have a sign in system so once you know
6170
03:56:27,920 --> 03:56:29,840
how to do this once you'll be able to
6171
03:56:29,840 --> 03:56:32,000
mostly duplicate the code and achieve
6172
03:56:32,000 --> 03:56:34,160
the same purpose in any app that you
6173
03:56:34,160 --> 03:56:35,120
build
6174
03:56:35,120 --> 03:56:37,040
first of all for the sign in we're going
6175
03:56:37,040 --> 03:56:39,600
to get two things from the front end
6176
03:56:39,600 --> 03:56:40,960
and those things are going to be the
6177
03:56:40,960 --> 03:56:43,520
email and the password so the question
6178
03:56:43,520 --> 03:56:44,960
is how are we getting it from the front
6179
03:56:44,960 --> 03:56:47,520
end whenever you have a post request you
6180
03:56:47,520 --> 03:56:48,760
get all the data through the
6181
03:56:48,760 --> 03:56:52,000
request.body we can say const and then
6182
03:56:52,000 --> 03:56:55,680
the structure the email and the password
6183
03:56:55,680 --> 03:56:56,560
from
6184
03:56:56,560 --> 03:56:59,439
rec.body so we're destructing that all
6185
03:56:59,439 --> 03:57:01,279
the post data that you send is going to
6186
03:57:01,279 --> 03:57:04,000
be available for you in direct.body
6187
03:57:04,000 --> 03:57:06,319
okay now that we have these values what
6188
03:57:06,319 --> 03:57:07,439
do we do
6189
03:57:07,439 --> 03:57:09,359
well we are in an async block so i'm
6190
03:57:09,359 --> 03:57:12,479
going to open a try and catch
6191
03:57:12,479 --> 03:57:15,199
if we are signing in we first have to
6192
03:57:15,199 --> 03:57:17,600
find the old user right so i can say
6193
03:57:17,600 --> 03:57:21,199
const old or we can better say existing
6194
03:57:21,199 --> 03:57:23,600
user existing user
6195
03:57:23,600 --> 03:57:25,359
and that is going to be equal to a
6196
03:57:25,359 --> 03:57:26,319
weight
6197
03:57:26,319 --> 03:57:29,199
user dot find one
6198
03:57:29,199 --> 03:57:32,160
and we find the user by email
6199
03:57:32,160 --> 03:57:33,760
that means that we are searching for the
6200
03:57:33,760 --> 03:57:35,600
existing user in the database because if
6201
03:57:35,600 --> 03:57:37,520
you're signing in we cannot simply
6202
03:57:37,520 --> 03:57:39,520
create a new one we need to find the old
6203
03:57:39,520 --> 03:57:42,080
one already existing in the database
6204
03:57:42,080 --> 03:57:44,479
in this case we're gonna say if
6205
03:57:44,479 --> 03:57:46,560
no existing user
6206
03:57:46,560 --> 03:57:49,199
then we're going to simply return
6207
03:57:49,199 --> 03:57:51,279
rest.status
6208
03:57:51,279 --> 03:57:53,840
404 because it means we cannot find it
6209
03:57:53,840 --> 03:57:56,160
and then dot json
6210
03:57:56,160 --> 03:57:59,680
the message is going to be equal to
6211
03:57:59,680 --> 03:58:00,800
user
6212
03:58:00,800 --> 03:58:01,840
doesn't
6213
03:58:01,840 --> 03:58:02,880
exist
6214
03:58:02,880 --> 03:58:05,439
that is it that's if we don't have the
6215
03:58:05,439 --> 03:58:07,840
existing user in the database if that is
6216
03:58:07,840 --> 03:58:10,160
not the case we have to check if the
6217
03:58:10,160 --> 03:58:12,560
password is correct so if the new
6218
03:58:12,560 --> 03:58:14,399
password that user typed in is the same
6219
03:58:14,399 --> 03:58:17,359
one to the password used when he or she
6220
03:58:17,359 --> 03:58:19,760
initially created the account
6221
03:58:19,760 --> 03:58:23,279
we can say cons is password
6222
03:58:23,279 --> 03:58:24,800
correct
6223
03:58:24,800 --> 03:58:27,439
and that is going to be equal to await
6224
03:58:27,439 --> 03:58:28,720
decrypt
6225
03:58:28,720 --> 03:58:32,239
dot compare and what we want to compare
6226
03:58:32,239 --> 03:58:33,680
is the password
6227
03:58:33,680 --> 03:58:37,439
to existing user dot password we cannot
6228
03:58:37,439 --> 03:58:40,080
simply do a normal string check because
6229
03:58:40,080 --> 03:58:42,720
before or rather later when we implement
6230
03:58:42,720 --> 03:58:44,720
the b bcrypt we're going to hash the
6231
03:58:44,720 --> 03:58:46,720
password so we still have to use bcrypt
6232
03:58:46,720 --> 03:58:49,040
to compare the hashed passwords and see
6233
03:58:49,040 --> 03:58:50,640
if they're the same
6234
03:58:50,640 --> 03:58:53,520
then we can do one more check and say if
6235
03:58:53,520 --> 03:58:55,680
no is password correct so if the
6236
03:58:55,680 --> 03:58:57,439
password is not correct
6237
03:58:57,439 --> 03:58:59,199
return
6238
03:58:59,199 --> 03:59:00,880
res dot status
6239
03:59:00,880 --> 03:59:02,880
that's going to be 400
6240
03:59:02,880 --> 03:59:05,359
dot json and in there we're going to
6241
03:59:05,359 --> 03:59:06,880
have a message
6242
03:59:06,880 --> 03:59:10,319
and what we can say is simply invalid
6243
03:59:10,319 --> 03:59:12,239
credentials
6244
03:59:12,239 --> 03:59:13,040
okay
6245
03:59:13,040 --> 03:59:15,040
that's that's good
6246
03:59:15,040 --> 03:59:17,439
and moving on if the user already exists
6247
03:59:17,439 --> 03:59:19,520
in the database and if the password is
6248
03:59:19,520 --> 03:59:22,880
correct then we can finally get his json
6249
03:59:22,880 --> 03:59:24,880
web token that we need to send to the
6250
03:59:24,880 --> 03:59:27,840
front end so to do that i'm going to say
6251
03:59:27,840 --> 03:59:33,439
cons token is equal to jsonwebtoken.sign
6252
03:59:33,439 --> 03:59:35,199
and in there we have to provide all the
6253
03:59:35,199 --> 03:59:36,960
information that we want to store in the
6254
03:59:36,960 --> 03:59:39,120
token in this case we could store the
6255
03:59:39,120 --> 03:59:41,600
email email is going to be equal to
6256
03:59:41,600 --> 03:59:44,399
existing user dot email
6257
03:59:44,399 --> 03:59:46,880
and we also want to keep the id which is
6258
03:59:46,880 --> 03:59:48,399
going to be equal to
6259
03:59:48,399 --> 03:59:52,080
existing user dot id or it's going to be
6260
03:59:52,080 --> 03:59:54,319
dot underscore id don't forget that
6261
03:59:54,319 --> 03:59:56,720
underscore with the id
6262
03:59:56,720 --> 03:59:58,399
the second argument to the json web
6263
03:59:58,399 --> 04:00:00,960
token sign is the secret instead of here
6264
04:00:00,960 --> 04:00:02,880
you have to have the secret string that
6265
04:00:02,880 --> 04:00:05,199
only you know usually you would put it
6266
04:00:05,199 --> 04:00:07,600
in a separate env file where you store
6267
04:00:07,600 --> 04:00:09,439
all of your variables where nobody else
6268
04:00:09,439 --> 04:00:11,520
can see in this case we can simply say
6269
04:00:11,520 --> 04:00:13,279
test right there and that is going to be
6270
04:00:13,279 --> 04:00:14,160
enough
6271
04:00:14,160 --> 04:00:15,920
as this is a youtube video i'm trying to
6272
04:00:15,920 --> 04:00:18,479
keep it as concise as possible
6273
04:00:18,479 --> 04:00:19,920
if you'd like me to make a separate
6274
04:00:19,920 --> 04:00:21,680
video of this series where i go through
6275
04:00:21,680 --> 04:00:24,160
the entire project and just apply all of
6276
04:00:24,160 --> 04:00:26,080
the best possible programming habits to
6277
04:00:26,080 --> 04:00:28,479
it like move all of the things that
6278
04:00:28,479 --> 04:00:30,479
should be saved to somewhere else and
6279
04:00:30,479 --> 04:00:32,800
refactor the code and so on feel free to
6280
04:00:32,800 --> 04:00:34,399
let me know in the comments for now
6281
04:00:34,399 --> 04:00:36,800
we're gonna simply leave this as test
6282
04:00:36,800 --> 04:00:38,800
finally the last thing is the options
6283
04:00:38,800 --> 04:00:40,720
object so inside of here one of the
6284
04:00:40,720 --> 04:00:43,840
options we'll use is expires in and
6285
04:00:43,840 --> 04:00:46,479
we're going to say expires in one hour
6286
04:00:46,479 --> 04:00:49,040
just one h and that is going to be it
6287
04:00:49,040 --> 04:00:50,640
for our token
6288
04:00:50,640 --> 04:00:52,560
finally now that we have the token the
6289
04:00:52,560 --> 04:00:54,319
last thing that we have to do is simply
6290
04:00:54,319 --> 04:00:57,840
return it so we can say rest.status
6291
04:00:57,840 --> 04:01:00,800
status is going to be equal to 200 and
6292
04:01:00,800 --> 04:01:03,040
dot json what are we sending we're
6293
04:01:03,040 --> 04:01:04,800
sending the result which is going to be
6294
04:01:04,800 --> 04:01:06,880
equal to the existing user or rather the
6295
04:01:06,880 --> 04:01:09,680
user that's trying to log in and we send
6296
04:01:09,680 --> 04:01:12,399
over the token that we just created here
6297
04:01:12,399 --> 04:01:14,640
on the backend if the token creation
6298
04:01:14,640 --> 04:01:16,399
didn't win successfully we can say
6299
04:01:16,399 --> 04:01:19,520
rest.status 500 that means an undefined
6300
04:01:19,520 --> 04:01:22,479
server error and then we can say json
6301
04:01:22,479 --> 04:01:24,640
and what are we returning well just a
6302
04:01:24,640 --> 04:01:26,160
message that says
6303
04:01:26,160 --> 04:01:28,800
something went wrong we don't really
6304
04:01:28,800 --> 04:01:31,359
have the info on what went wrong
6305
04:01:31,359 --> 04:01:34,239
and this is it this is our sign in
6306
04:01:34,239 --> 04:01:36,880
controller of course more important part
6307
04:01:36,880 --> 04:01:38,960
at least right now is to create a sign
6308
04:01:38,960 --> 04:01:41,040
up that way we'll be able to add the
6309
04:01:41,040 --> 04:01:43,120
user to the database so let's add the
6310
04:01:43,120 --> 04:01:45,680
sign up right now it's going to be quite
6311
04:01:45,680 --> 04:01:46,399
similar
6312
04:01:46,399 --> 04:01:48,720
but it's going to have a few distinctive
6313
04:01:48,720 --> 04:01:49,840
differences
6314
04:01:49,840 --> 04:01:51,600
again we want to get something from the
6315
04:01:51,600 --> 04:01:53,199
reg.body
6316
04:01:53,199 --> 04:01:55,359
rec.body we're destructuring the things
6317
04:01:55,359 --> 04:01:57,680
that we send over and remember what do
6318
04:01:57,680 --> 04:01:59,439
we have on the front and side when we
6319
04:01:59,439 --> 04:02:00,880
are signing up
6320
04:02:00,880 --> 04:02:03,600
we have first of all the email
6321
04:02:03,600 --> 04:02:06,560
password and then we have the first name
6322
04:02:06,560 --> 04:02:09,920
and the last name as well
6323
04:02:09,920 --> 04:02:12,560
we also have the confirm password field
6324
04:02:12,560 --> 04:02:15,040
don't forget about that okay so now that
6325
04:02:15,040 --> 04:02:16,720
we're getting all the data let's start
6326
04:02:16,720 --> 04:02:19,359
with the logic we again have the try and
6327
04:02:19,359 --> 04:02:20,560
catch block
6328
04:02:20,560 --> 04:02:22,479
and in this case we are also going to
6329
04:02:22,479 --> 04:02:25,040
try to find the existing user so we can
6330
04:02:25,040 --> 04:02:28,239
say const existing user and this is
6331
04:02:28,239 --> 04:02:30,319
going to be the same as we had right
6332
04:02:30,319 --> 04:02:32,880
here so we try to find a user with that
6333
04:02:32,880 --> 04:02:34,000
email
6334
04:02:34,000 --> 04:02:36,399
why do we do that if you're signing up
6335
04:02:36,399 --> 04:02:38,000
think about it we cannot create an
6336
04:02:38,000 --> 04:02:40,399
account if there already is an existing
6337
04:02:40,399 --> 04:02:42,800
user so in this case i'm going to copy
6338
04:02:42,800 --> 04:02:45,359
the same line that we have right there
6339
04:02:45,359 --> 04:02:47,760
but in this case i'm not going to say if
6340
04:02:47,760 --> 04:02:50,000
not existing user i'm going to say if
6341
04:02:50,000 --> 04:02:51,760
existing user that's the problem right
6342
04:02:51,760 --> 04:02:52,560
there
6343
04:02:52,560 --> 04:02:54,640
and then what do we want to return res
6344
04:02:54,640 --> 04:02:57,279
the status that's going to be 400 and
6345
04:02:57,279 --> 04:02:59,520
we're gonna say user
6346
04:02:59,520 --> 04:03:02,080
already exists
6347
04:03:02,080 --> 04:03:04,160
moving forward if we don't have the
6348
04:03:04,160 --> 04:03:05,920
existing user that means that we're good
6349
04:03:05,920 --> 04:03:08,319
to go to create an account but we first
6350
04:03:08,319 --> 04:03:10,160
have to check if the password is the
6351
04:03:10,160 --> 04:03:12,800
same to the confirmed password so we can
6352
04:03:12,800 --> 04:03:14,479
say password
6353
04:03:14,479 --> 04:03:17,840
is equal to confirm password
6354
04:03:17,840 --> 04:03:20,160
or rather we can say if the password is
6355
04:03:20,160 --> 04:03:22,880
not equal to to the confirmed password
6356
04:03:22,880 --> 04:03:24,640
in that case we're going to return the
6357
04:03:24,640 --> 04:03:26,319
same error right here so i'm going to
6358
04:03:26,319 --> 04:03:27,520
paste that
6359
04:03:27,520 --> 04:03:28,560
return
6360
04:03:28,560 --> 04:03:30,720
and in this case we're going to say
6361
04:03:30,720 --> 04:03:32,640
passwords
6362
04:03:32,640 --> 04:03:34,720
don't
6363
04:03:34,720 --> 04:03:36,880
match that's that means that the user
6364
04:03:36,880 --> 04:03:39,680
has to re-enter them one more time
6365
04:03:39,680 --> 04:03:40,560
okay
6366
04:03:40,560 --> 04:03:42,960
great moving forward
6367
04:03:42,960 --> 04:03:45,359
if we don't have the existing user and
6368
04:03:45,359 --> 04:03:47,359
if the passwords match that means that
6369
04:03:47,359 --> 04:03:49,680
we're good to go to create the user
6370
04:03:49,680 --> 04:03:51,760
before we create him though we have to
6371
04:03:51,760 --> 04:03:54,080
hash the password because we don't want
6372
04:03:54,080 --> 04:03:56,560
to store it in a plain text to do that
6373
04:03:56,560 --> 04:04:00,239
we can say const hashed password
6374
04:04:00,239 --> 04:04:02,720
and that's going to be equal to a weight
6375
04:04:02,720 --> 04:04:06,319
bcrypt dot hash and then in here we
6376
04:04:06,319 --> 04:04:10,160
simply pass in the password
6377
04:04:10,160 --> 04:04:12,160
the second thing that you pass is called
6378
04:04:12,160 --> 04:04:14,080
salt and that is the level of difficulty
6379
04:04:14,080 --> 04:04:15,520
that you want to use to hash your
6380
04:04:15,520 --> 04:04:18,479
password people usually use 12.
6381
04:04:18,479 --> 04:04:20,000
okay so now that we have the hash
6382
04:04:20,000 --> 04:04:22,640
password we have to create our user i'm
6383
04:04:22,640 --> 04:04:25,199
gonna say cons result is going to be
6384
04:04:25,199 --> 04:04:27,199
equal to await
6385
04:04:27,199 --> 04:04:30,160
user dot create and how do we create a
6386
04:04:30,160 --> 04:04:32,560
user we have to pass in all the data we
6387
04:04:32,560 --> 04:04:34,640
pass in the email we pass in the
6388
04:04:34,640 --> 04:04:37,680
password but take care it's going to be
6389
04:04:37,680 --> 04:04:40,080
hashed password in this case
6390
04:04:40,080 --> 04:04:42,479
then we pass the name the name in this
6391
04:04:42,479 --> 04:04:44,800
case is going to be a template string
6392
04:04:44,800 --> 04:04:47,279
where we first take the first name
6393
04:04:47,279 --> 04:04:49,199
and then add a comma
6394
04:04:49,199 --> 04:04:52,000
and then add a space and then enter the
6395
04:04:52,000 --> 04:04:54,000
last name so we're gonna combine the
6396
04:04:54,000 --> 04:04:57,359
first name and the last name together
6397
04:04:57,359 --> 04:04:59,040
just like this
6398
04:04:59,040 --> 04:05:01,199
okay now that we created the user we
6399
04:05:01,199 --> 04:05:03,279
also have to create the token so i'm
6400
04:05:03,279 --> 04:05:05,439
going to copy this line from above
6401
04:05:05,439 --> 04:05:09,880
cons token is equal to json wt
6402
04:05:09,880 --> 04:05:12,479
jsonwebtoken.sign email is going to be
6403
04:05:12,479 --> 04:05:13,399
equal to
6404
04:05:13,399 --> 04:05:16,080
result.email and id is going to be equal
6405
04:05:16,080 --> 04:05:19,359
to result dot underscore id
6406
04:05:19,359 --> 04:05:21,520
the secret is going to stay the same and
6407
04:05:21,520 --> 04:05:23,520
expires in is also going to stay the
6408
04:05:23,520 --> 04:05:24,640
same
6409
04:05:24,640 --> 04:05:26,880
finally after this is all successful we
6410
04:05:26,880 --> 04:05:28,960
have to return our user so i'm going to
6411
04:05:28,960 --> 04:05:31,600
copy the return from the line above and
6412
04:05:31,600 --> 04:05:35,199
what we can say is rest.status 200 means
6413
04:05:35,199 --> 04:05:38,080
everything went good and we send the
6414
04:05:38,080 --> 04:05:40,800
result in this case the user itself is
6415
04:05:40,800 --> 04:05:44,399
the result and we also send in the token
6416
04:05:44,399 --> 04:05:46,560
if something went wrong of course in
6417
04:05:46,560 --> 04:05:49,680
here we can simply send res that status
6418
04:05:49,680 --> 04:05:52,640
500 something went wrong
6419
04:05:52,640 --> 04:05:54,239
and this is it now we have the
6420
04:05:54,239 --> 04:05:56,960
controllers for the sign in and the sign
6421
04:05:56,960 --> 04:05:59,760
up this alone should be able to allow us
6422
04:05:59,760 --> 04:06:02,399
to log in and register
6423
04:06:02,399 --> 04:06:04,319
i'm really trying to be turo explaining
6424
04:06:04,319 --> 04:06:06,640
this because i know it's a hard concept
6425
04:06:06,640 --> 04:06:07,920
i'm really glad we went through this
6426
04:06:07,920 --> 04:06:10,000
hard part it's definitely not easy so
6427
04:06:10,000 --> 04:06:11,520
make sure to re-watch it if you don't
6428
04:06:11,520 --> 04:06:13,680
understand some of the parts if you like
6429
04:06:13,680 --> 04:06:15,279
my current way of teaching definitely
6430
04:06:15,279 --> 04:06:17,199
make sure to leave a like and comment it
6431
04:06:17,199 --> 04:06:19,600
really helps with the youtube algorithm
6432
04:06:19,600 --> 04:06:21,359
with that said let's move to the second
6433
04:06:21,359 --> 04:06:24,080
step and that is once the user is
6434
04:06:24,080 --> 04:06:27,199
actually logged in he can do all kinds
6435
04:06:27,199 --> 04:06:29,680
of actions he can delete a post that he
6436
04:06:29,680 --> 04:06:31,840
created he can like it he can like all
6437
04:06:31,840 --> 04:06:33,840
the other posts and their back end needs
6438
04:06:33,840 --> 04:06:37,840
to be able to basically say okay you are
6439
04:06:37,840 --> 04:06:39,760
allowed to do that you a real user
6440
04:06:39,760 --> 04:06:41,680
you're currently logged in and you can
6441
04:06:41,680 --> 04:06:44,800
do that for that we use something known
6442
04:06:44,800 --> 04:06:46,239
as middleware
6443
04:06:46,239 --> 04:06:48,479
so our next step is going to be creating
6444
04:06:48,479 --> 04:06:51,359
authentication middleware to create any
6445
04:06:51,359 --> 04:06:53,199
kind of middleware we can create a
6446
04:06:53,199 --> 04:06:56,080
middleware folder so right inside of the
6447
04:06:56,080 --> 04:06:58,880
server we can create a middle
6448
04:06:58,880 --> 04:07:00,640
where folder
6449
04:07:00,640 --> 04:07:02,960
you can see if you use some kind of icon
6450
04:07:02,960 --> 04:07:04,880
theme like material ui icons it's
6451
04:07:04,880 --> 04:07:06,960
automatically going to get a different
6452
04:07:06,960 --> 04:07:09,680
look inside of there i'm simply going to
6453
04:07:09,680 --> 04:07:10,520
say
6454
04:07:10,520 --> 04:07:13,040
auth.js because the middleware inside of
6455
04:07:13,040 --> 04:07:15,920
here is going to be for auth right now
6456
04:07:15,920 --> 04:07:17,840
you might not understand what a
6457
04:07:17,840 --> 04:07:20,000
middleware means it's a specific name
6458
04:07:20,000 --> 04:07:21,920
but as soon as we create it i'm going to
6459
04:07:21,920 --> 04:07:24,239
show you where that piece of the puzzle
6460
04:07:24,239 --> 04:07:26,640
is gonna fit in as i show you that it's
6461
04:07:26,640 --> 04:07:28,800
going to make complete sense
6462
04:07:28,800 --> 04:07:30,239
so for now let's start with the
6463
04:07:30,239 --> 04:07:33,920
middleware we're gonna import jwt from
6464
04:07:33,920 --> 04:07:36,560
json web token
6465
04:07:36,560 --> 04:07:38,479
then we're going to create our odd
6466
04:07:38,479 --> 04:07:41,279
function so we can say const auth and
6467
04:07:41,279 --> 04:07:42,800
it's going to be equal to an async
6468
04:07:42,800 --> 04:07:46,239
function where we have a rec res and the
6469
04:07:46,239 --> 04:07:47,680
next
6470
04:07:47,680 --> 04:07:49,199
even by looking at this you can
6471
04:07:49,199 --> 04:07:51,920
immediately see that our middleware is
6472
04:07:51,920 --> 04:07:54,239
really similar to what we have on our
6473
04:07:54,239 --> 04:07:57,120
controllers they all have the wreck and
6474
04:07:57,120 --> 04:07:58,399
the res
6475
04:07:58,399 --> 04:08:00,960
but our odd middleware has something
6476
04:08:00,960 --> 04:08:04,080
known as next that means do something
6477
04:08:04,080 --> 04:08:06,560
and then move to the next thing again
6478
04:08:06,560 --> 04:08:08,080
you're going to see it in just a few
6479
04:08:08,080 --> 04:08:09,840
moments in action
6480
04:08:09,840 --> 04:08:11,439
but now inside of there we're going to
6481
04:08:11,439 --> 04:08:14,239
add a try and catch block
6482
04:08:14,239 --> 04:08:15,760
for the catch we're going to simply
6483
04:08:15,760 --> 04:08:18,640
cancel log the error for now and now in
6484
04:08:18,640 --> 04:08:21,920
the try block we have to see if the user
6485
04:08:21,920 --> 04:08:24,560
is really who he is claiming to be and
6486
04:08:24,560 --> 04:08:28,239
we can do that using json web token
6487
04:08:28,239 --> 04:08:31,120
so after the user is signed up or signed
6488
04:08:31,120 --> 04:08:34,239
in he gets this specific token
6489
04:08:34,239 --> 04:08:36,080
now when he wants to do something like
6490
04:08:36,080 --> 04:08:38,479
like a post or delete a post we have to
6491
04:08:38,479 --> 04:08:41,040
check if his token is valid and that's
6492
04:08:41,040 --> 04:08:42,880
what we're doing right here
6493
04:08:42,880 --> 04:08:44,800
so to get the token from the front end
6494
04:08:44,800 --> 04:08:46,800
we can say cons token
6495
04:08:46,800 --> 04:08:50,239
is equal to rec dot headers dot
6496
04:08:50,239 --> 04:08:52,560
authorization
6497
04:08:52,560 --> 04:08:54,960
dot split so we split it
6498
04:08:54,960 --> 04:08:57,279
and we only want the token itself and
6499
04:08:57,279 --> 04:08:59,520
the token is on the first position in
6500
04:08:59,520 --> 04:09:02,159
the array after we split it
6501
04:09:02,159 --> 04:09:03,920
then we're gonna have two kinds of
6502
04:09:03,920 --> 04:09:05,520
tokens we're gonna have the one from the
6503
04:09:05,520 --> 04:09:08,159
google auth and we're gonna have our own
6504
04:09:08,159 --> 04:09:10,479
so inside of here we can decide if it is
6505
04:09:10,479 --> 04:09:12,720
our own or the google auth
6506
04:09:12,720 --> 04:09:14,479
that's going to be a variable where
6507
04:09:14,479 --> 04:09:18,080
we're going to say is custom off
6508
04:09:18,080 --> 04:09:20,000
and if the token
6509
04:09:20,000 --> 04:09:24,080
dot length is lower than 500 then it
6510
04:09:24,080 --> 04:09:26,640
means that it is our own if token length
6511
04:09:26,640 --> 04:09:28,880
is greater than 500 that is going to be
6512
04:09:28,880 --> 04:09:30,399
google auth
6513
04:09:30,399 --> 04:09:32,640
then we can create a variable called
6514
04:09:32,640 --> 04:09:35,199
decoded data so that is the data that we
6515
04:09:35,199 --> 04:09:37,840
want to get from the token itself
6516
04:09:37,840 --> 04:09:41,279
and now finally if we have the token
6517
04:09:41,279 --> 04:09:44,640
and if the token is our own so if it is
6518
04:09:44,640 --> 04:09:46,319
a custom auth
6519
04:09:46,319 --> 04:09:48,800
in that case we want to set the decoded
6520
04:09:48,800 --> 04:09:50,800
data to be equal to
6521
04:09:50,800 --> 04:09:53,800
jsonwebtoken.verify
6522
04:09:54,159 --> 04:09:56,000
this is going to give us the data from
6523
04:09:56,000 --> 04:09:58,159
each specific token it's going to give
6524
04:09:58,159 --> 04:10:01,840
us the username of the person and its id
6525
04:10:01,840 --> 04:10:03,920
inside of here we have to pass the token
6526
04:10:03,920 --> 04:10:05,760
and we need to have that secret we were
6527
04:10:05,760 --> 04:10:07,840
talking about in this case we know that
6528
04:10:07,840 --> 04:10:11,040
the secret is test it has to be the same
6529
04:10:11,040 --> 04:10:13,680
secret you used when you were creating
6530
04:10:13,680 --> 04:10:16,000
that specific token you can see right
6531
04:10:16,000 --> 04:10:18,960
here we use test and in here we use test
6532
04:10:18,960 --> 04:10:21,279
and now that we have the decoded data we
6533
04:10:21,279 --> 04:10:23,680
know which user is logged in and which
6534
04:10:23,680 --> 04:10:25,920
user is for example liking the post or
6535
04:10:25,920 --> 04:10:27,760
deleting the post so we're going to
6536
04:10:27,760 --> 04:10:30,800
store his id in direct.user
6537
04:10:30,800 --> 04:10:31,840
id
6538
04:10:31,840 --> 04:10:34,560
so reg.user id is going to be equal to
6539
04:10:34,560 --> 04:10:37,120
decoded data question mark dot for the
6540
04:10:37,120 --> 04:10:40,239
optional chaining id okay this is how we
6541
04:10:40,239 --> 04:10:43,359
get the user's id if we are working with
6542
04:10:43,359 --> 04:10:45,600
our own token and if you're working with
6543
04:10:45,600 --> 04:10:47,840
the google's or token we're gonna do the
6544
04:10:47,840 --> 04:10:48,960
same thing
6545
04:10:48,960 --> 04:10:51,600
decoded data is going to be equal to
6546
04:10:51,600 --> 04:10:52,560
json
6547
04:10:52,560 --> 04:10:55,439
web token dot decode in this case it's
6548
04:10:55,439 --> 04:10:58,239
going to be the code and simply pass in
6549
04:10:58,239 --> 04:11:01,199
the token in this case we don't need the
6550
04:11:01,199 --> 04:11:02,640
secret
6551
04:11:02,640 --> 04:11:04,479
once we have that the situation is going
6552
04:11:04,479 --> 04:11:05,680
to be similar
6553
04:11:05,680 --> 04:11:08,560
rec dot user id is equal to decoded data
6554
04:11:08,560 --> 04:11:10,800
question mark dot and in this case we're
6555
04:11:10,800 --> 04:11:13,279
going to say something known as sub
6556
04:11:13,279 --> 04:11:15,520
sub is simply google's name for a
6557
04:11:15,520 --> 04:11:19,199
specific id that differentiates every
6558
04:11:19,199 --> 04:11:21,279
single google user
6559
04:11:21,279 --> 04:11:22,880
basically it's an id that we can
6560
04:11:22,880 --> 04:11:25,600
differentiate the users with finally we
6561
04:11:25,600 --> 04:11:28,479
can call the next so that we can pass
6562
04:11:28,479 --> 04:11:30,479
the action onto the second thing for
6563
04:11:30,479 --> 04:11:33,359
example if the user wanted to like a
6564
04:11:33,359 --> 04:11:35,600
post so let's say a user went and he
6565
04:11:35,600 --> 04:11:38,880
wants to like a post wants to like a
6566
04:11:38,880 --> 04:11:41,199
post he has to do this
6567
04:11:41,199 --> 04:11:43,760
click the like button
6568
04:11:43,760 --> 04:11:45,760
and then this is what happens
6569
04:11:45,760 --> 04:11:48,000
first we don't immediately like it
6570
04:11:48,000 --> 04:11:49,600
because we are not sure if he has
6571
04:11:49,600 --> 04:11:52,080
permissions to like it first we go
6572
04:11:52,080 --> 04:11:54,880
through the odd middleware okay
6573
04:11:54,880 --> 04:11:57,920
auth middleware confirms or denies that
6574
04:11:57,920 --> 04:12:00,560
request so if all this is correct we're
6575
04:12:00,560 --> 04:12:01,840
going to call the next and we're going
6576
04:12:01,840 --> 04:12:03,920
to say okay you're okay to like that
6577
04:12:03,920 --> 04:12:07,439
post so if odd middleware says okay
6578
04:12:07,439 --> 04:12:09,760
you're good to go only then are we going
6579
04:12:09,760 --> 04:12:12,319
to call the like controller
6580
04:12:12,319 --> 04:12:15,040
and this is what middleware is for for
6581
04:12:15,040 --> 04:12:17,840
any kind of action that happens before
6582
04:12:17,840 --> 04:12:21,120
something this next thing is crucial we
6583
04:12:21,120 --> 04:12:23,680
are saying do this and then do something
6584
04:12:23,680 --> 04:12:26,080
next do something after this is done and
6585
04:12:26,080 --> 04:12:28,000
that is a middleware
6586
04:12:28,000 --> 04:12:30,960
okay with that said we can finally
6587
04:12:30,960 --> 04:12:33,120
export default
6588
04:12:33,120 --> 04:12:35,199
auth
6589
04:12:35,199 --> 04:12:37,920
now the question is where do we use that
6590
04:12:37,920 --> 04:12:39,439
auth middleware
6591
04:12:39,439 --> 04:12:41,760
and the answer is in the routes for
6592
04:12:41,760 --> 04:12:44,399
example we said when somebody likes
6593
04:12:44,399 --> 04:12:46,640
something in that case we want to use
6594
04:12:46,640 --> 04:12:48,800
that middleware so first of all let's
6595
04:12:48,800 --> 04:12:50,319
import it
6596
04:12:50,319 --> 04:12:52,800
import auth from
6597
04:12:52,800 --> 04:12:56,080
dot slash middleware
6598
04:12:56,080 --> 04:12:59,199
slash auth and we are on the back end so
6599
04:12:59,199 --> 04:13:01,359
we have to do the odd.js
6600
04:13:01,359 --> 04:13:05,040
okay this is going to be our middleware
6601
04:13:05,040 --> 04:13:07,760
now we want to add it before specific
6602
04:13:07,760 --> 04:13:10,800
actions for example all of the users no
6603
04:13:10,800 --> 04:13:12,720
matter if they're logged in or not they
6604
04:13:12,720 --> 04:13:14,720
can see all the posts
6605
04:13:14,720 --> 04:13:17,680
but to create a post you need to have
6606
04:13:17,680 --> 04:13:20,319
your own id you need to be logged in
6607
04:13:20,319 --> 04:13:21,920
then in this case i noticed that this is
6608
04:13:21,920 --> 04:13:23,840
an extra action that we don't need so we
6609
04:13:23,840 --> 04:13:25,600
can delete this one because we don't
6610
04:13:25,600 --> 04:13:27,760
have a specific memory page we can
6611
04:13:27,760 --> 04:13:29,199
implement that in one of the future
6612
04:13:29,199 --> 04:13:30,239
videos
6613
04:13:30,239 --> 04:13:32,239
then we have the edit post or update
6614
04:13:32,239 --> 04:13:34,399
post in there we definitely need to have
6615
04:13:34,399 --> 04:13:36,239
the auth happening before because we
6616
04:13:36,239 --> 04:13:37,520
need to see if the user has the
6617
04:13:37,520 --> 04:13:40,319
permission to update a post same thing
6618
04:13:40,319 --> 04:13:42,399
happens for deletion do we have the
6619
04:13:42,399 --> 04:13:44,479
right to delete that post
6620
04:13:44,479 --> 04:13:46,239
and finally the same thing happens for
6621
04:13:46,239 --> 04:13:49,040
liking why although everybody has the
6622
04:13:49,040 --> 04:13:51,040
permission to like something he cannot
6623
04:13:51,040 --> 04:13:53,680
like twice or three times that's why we
6624
04:13:53,680 --> 04:13:56,239
need to call the odd before to have the
6625
04:13:56,239 --> 04:13:58,800
specific user's id and he can only like
6626
04:13:58,800 --> 04:14:01,199
once for that specific id
6627
04:14:01,199 --> 04:14:02,080
great
6628
04:14:02,080 --> 04:14:04,399
now you know how to create auth and also
6629
04:14:04,399 --> 04:14:06,000
where to add it
6630
04:14:06,000 --> 04:14:07,760
now the majority of our back-end work is
6631
04:14:07,760 --> 04:14:09,760
done but we still have to implement the
6632
04:14:09,760 --> 04:14:12,560
logic for for example liking only one
6633
04:14:12,560 --> 04:14:14,479
specific post right now you can like as
6634
04:14:14,479 --> 04:14:16,080
many times as you want
6635
04:14:16,080 --> 04:14:17,920
we also need to implement the logic for
6636
04:14:17,920 --> 04:14:20,239
deleting only the post that you created
6637
04:14:20,239 --> 04:14:22,399
or same for updating the posts only you
6638
04:14:22,399 --> 04:14:24,560
created you cannot simply
6639
04:14:24,560 --> 04:14:27,760
update or delete any post that's there
6640
04:14:27,760 --> 04:14:30,000
and that's going to be it this one the
6641
04:14:30,000 --> 04:14:31,920
like post is going to be managed on the
6642
04:14:31,920 --> 04:14:34,000
backend and if i'm not mistaken the
6643
04:14:34,000 --> 04:14:36,239
delete post and update post are going to
6644
04:14:36,239 --> 04:14:38,720
be managed on the front end because what
6645
04:14:38,720 --> 04:14:40,239
we have to do to disable user from
6646
04:14:40,239 --> 04:14:41,600
deleting something
6647
04:14:41,600 --> 04:14:42,399
well
6648
04:14:42,399 --> 04:14:44,399
if he's not the creator of the post
6649
04:14:44,399 --> 04:14:47,040
simply remove the delete button it's not
6650
04:14:47,040 --> 04:14:49,199
going to even be there he won't be able
6651
04:14:49,199 --> 04:14:51,439
to click it there we go
6652
04:14:51,439 --> 04:14:53,279
so while we're in the back end we're
6653
04:14:53,279 --> 04:14:55,279
going to focus on completing it so now
6654
04:14:55,279 --> 04:14:57,120
our next step is to implement the like
6655
04:14:57,120 --> 04:14:59,439
post only a single one and then we're
6656
04:14:59,439 --> 04:15:01,439
going to move to the front end to
6657
04:15:01,439 --> 04:15:04,000
finalize the things with the auth and
6658
04:15:04,000 --> 04:15:06,319
with the update and delete post
6659
04:15:06,319 --> 04:15:08,000
starting with the light controller
6660
04:15:08,000 --> 04:15:09,520
that's the one that we created in the
6661
04:15:09,520 --> 04:15:12,239
last video inside of the post js of
6662
04:15:12,239 --> 04:15:15,600
controllers if you scroll a bit down you
6663
04:15:15,600 --> 04:15:18,640
should see a like post controller now we
6664
04:15:18,640 --> 04:15:20,960
have to modify it so that the users can
6665
04:15:20,960 --> 04:15:24,479
only like the post once so let's do that
6666
04:15:24,479 --> 04:15:26,960
first of all we have to see if a user is
6667
04:15:26,960 --> 04:15:29,439
even authenticated we can do that
6668
04:15:29,439 --> 04:15:32,239
because now we have that special value
6669
04:15:32,239 --> 04:15:35,920
and that is rec dot user id
6670
04:15:35,920 --> 04:15:37,840
this is really really important to
6671
04:15:37,840 --> 04:15:38,960
mention
6672
04:15:38,960 --> 04:15:42,000
if you call a middleware like this
6673
04:15:42,000 --> 04:15:44,640
before a specific action
6674
04:15:44,640 --> 04:15:47,120
like in this case
6675
04:15:47,120 --> 04:15:49,840
then you can populate the request and
6676
04:15:49,840 --> 04:15:52,000
then you'll have access to that request
6677
04:15:52,000 --> 04:15:54,399
right into the next action that you have
6678
04:15:54,399 --> 04:15:55,359
so
6679
04:15:55,359 --> 04:15:58,159
if we go into our auth and then in here
6680
04:15:58,159 --> 04:16:01,359
if we populate the request.userid
6681
04:16:01,359 --> 04:16:03,199
then we go to the next controller in the
6682
04:16:03,199 --> 04:16:05,439
row which is going to be the like post
6683
04:16:05,439 --> 04:16:07,920
and now the request is going to have
6684
04:16:07,920 --> 04:16:11,040
that user id property with this we can
6685
04:16:11,040 --> 04:16:14,479
check something we can say if no
6686
04:16:14,479 --> 04:16:16,399
rec.user id
6687
04:16:16,399 --> 04:16:18,000
that means that the user is not
6688
04:16:18,000 --> 04:16:19,920
authenticated and we can simply say
6689
04:16:19,920 --> 04:16:21,080
return
6690
04:16:21,080 --> 04:16:24,399
res.json and then message is going to be
6691
04:16:24,399 --> 04:16:27,680
equal to on authenticated in here we
6692
04:16:27,680 --> 04:16:30,000
have the same old check to see if we do
6693
04:16:30,000 --> 04:16:31,600
have the post that the user wants to
6694
04:16:31,600 --> 04:16:32,560
like
6695
04:16:32,560 --> 04:16:35,040
then we get the actual post and now we
6696
04:16:35,040 --> 04:16:38,239
have to see if the user's id is already
6697
04:16:38,239 --> 04:16:41,359
in the like section or is it not so we
6698
04:16:41,359 --> 04:16:43,439
can say const index
6699
04:16:43,439 --> 04:16:44,800
is equal to
6700
04:16:44,800 --> 04:16:49,840
post dot likes dot find index
6701
04:16:49,840 --> 04:16:51,680
inside of here we have a callback
6702
04:16:51,680 --> 04:16:54,080
function where we loop through all the
6703
04:16:54,080 --> 04:16:57,040
ids so each like is going to be the id
6704
04:16:57,040 --> 04:16:58,800
from a specific person
6705
04:16:58,800 --> 04:17:00,880
that's how we're going to know who liked
6706
04:17:00,880 --> 04:17:02,479
the specific post
6707
04:17:02,479 --> 04:17:04,720
and what we have to do is we have to say
6708
04:17:04,720 --> 04:17:07,600
if id is equal to
6709
04:17:07,600 --> 04:17:10,000
we have to convert his user id to a
6710
04:17:10,000 --> 04:17:12,640
string and then we can say if the id is
6711
04:17:12,640 --> 04:17:16,880
equal to string of rec.user id
6712
04:17:16,880 --> 04:17:19,520
if that is the case that means that his
6713
04:17:19,520 --> 04:17:22,080
id is already in there that means that
6714
04:17:22,080 --> 04:17:24,159
that person already liked the post and
6715
04:17:24,159 --> 04:17:26,319
this is going to be a dislike and not
6716
04:17:26,319 --> 04:17:27,840
the like
6717
04:17:27,840 --> 04:17:31,359
so how do we do that we can say if index
6718
04:17:31,359 --> 04:17:34,960
is triple equal to -1
6719
04:17:34,960 --> 04:17:38,800
only if his id is not in here only then
6720
04:17:38,800 --> 04:17:41,279
is this going to be equal to -1 so this
6721
04:17:41,279 --> 04:17:44,560
is if he wants to like the post
6722
04:17:44,560 --> 04:17:46,880
else we're gonna get the index of his
6723
04:17:46,880 --> 04:17:49,279
specific like in that case we want to
6724
04:17:49,279 --> 04:17:53,120
delete his like or rather dislike a post
6725
04:17:53,120 --> 04:17:54,000
great
6726
04:17:54,000 --> 04:17:55,600
so what is going to be the logic for
6727
04:17:55,600 --> 04:17:57,359
adding the like that is going to be
6728
04:17:57,359 --> 04:17:58,960
really simple
6729
04:17:58,960 --> 04:18:02,399
post dot likes dot push
6730
04:18:02,399 --> 04:18:05,199
rec dot user id we're basically just
6731
04:18:05,199 --> 04:18:07,120
pushing his id
6732
04:18:07,120 --> 04:18:08,880
and if we want to remove his like we
6733
04:18:08,880 --> 04:18:10,720
need to remove his id from the likes
6734
04:18:10,720 --> 04:18:13,000
array that's going to be like this
6735
04:18:13,000 --> 04:18:17,040
pose.likes is equal to post dot likes
6736
04:18:17,040 --> 04:18:18,560
dot filter
6737
04:18:18,560 --> 04:18:21,439
our filter accepts a callback function
6738
04:18:21,439 --> 04:18:23,040
and it's going to loop through all the
6739
04:18:23,040 --> 04:18:25,040
ids you can see likes are going to be
6740
04:18:25,040 --> 04:18:27,600
ids and then in here we have to filter
6741
04:18:27,600 --> 04:18:29,680
out the like from the current person we
6742
04:18:29,680 --> 04:18:33,279
can do that by saying id is not equal to
6743
04:18:33,279 --> 04:18:36,319
string of rec.user
6744
04:18:36,319 --> 04:18:38,720
id and this is simply going to return us
6745
04:18:38,720 --> 04:18:40,800
the array of all the likes besides the
6746
04:18:40,800 --> 04:18:42,800
current person's like
6747
04:18:42,800 --> 04:18:44,399
now that we have the current number of
6748
04:18:44,399 --> 04:18:46,720
likes we are going to create the updated
6749
04:18:46,720 --> 04:18:48,640
post and that is going to be the same as
6750
04:18:48,640 --> 04:18:50,000
we have it here
6751
04:18:50,000 --> 04:18:53,120
post message that find by id and update
6752
04:18:53,120 --> 04:18:54,960
but in this case we're not going to
6753
04:18:54,960 --> 04:18:57,520
update only the likes or the like count
6754
04:18:57,520 --> 04:18:59,520
we don't have that anymore we're simply
6755
04:18:59,520 --> 04:19:02,159
going to create a new post so the post
6756
04:19:02,159 --> 04:19:04,159
that we had we're going to update it
6757
04:19:04,159 --> 04:19:06,880
because now we have the same old post
6758
04:19:06,880 --> 04:19:10,479
that now includes the like itself
6759
04:19:10,479 --> 04:19:12,560
one more thing that we have to do is add
6760
04:19:12,560 --> 04:19:15,359
the actual likes to each post that is
6761
04:19:15,359 --> 04:19:18,159
going to be in the post message model
6762
04:19:18,159 --> 04:19:19,359
right here
6763
04:19:19,359 --> 04:19:23,439
so far what we had was the like count
6764
04:19:23,439 --> 04:19:25,600
we no longer have the like count we're
6765
04:19:25,600 --> 04:19:28,960
going to have likes the array of likes
6766
04:19:28,960 --> 04:19:31,120
inside of there the type is actually
6767
04:19:31,120 --> 04:19:33,120
going to be an array
6768
04:19:33,120 --> 04:19:35,920
of strings this is how we write that so
6769
04:19:35,920 --> 04:19:38,239
the array of ids
6770
04:19:38,239 --> 04:19:40,000
and then the default value is going to
6771
04:19:40,000 --> 04:19:42,239
be an empty array
6772
04:19:42,239 --> 04:19:44,479
great now this is done make sure to save
6773
04:19:44,479 --> 04:19:46,640
all the files and now we will be able to
6774
04:19:46,640 --> 04:19:49,199
go to the front-end part and see if we
6775
04:19:49,199 --> 04:19:51,680
can make the calls to the sign in and
6776
04:19:51,680 --> 04:19:55,199
the sign up routes and controllers
6777
04:19:55,199 --> 04:19:56,640
this is the thing with back-end if you
6778
04:19:56,640 --> 04:19:58,319
work with the back end most likely you
6779
04:19:58,319 --> 04:20:00,000
have to write a lot of things and not
6780
04:20:00,000 --> 04:20:02,159
visually see something that's just how
6781
04:20:02,159 --> 04:20:03,760
it is with the back end but now we're
6782
04:20:03,760 --> 04:20:05,199
going to move back to the front end
6783
04:20:05,199 --> 04:20:07,199
since we're building a full stack app
6784
04:20:07,199 --> 04:20:09,279
and we will be able to see the logic
6785
04:20:09,279 --> 04:20:12,399
that we implemented on the back end i
6786
04:20:12,399 --> 04:20:13,920
closed all the windows just so it's
6787
04:20:13,920 --> 04:20:15,600
easier for us to go back to the front
6788
04:20:15,600 --> 04:20:18,319
end and let's focus on important things
6789
04:20:18,319 --> 04:20:20,640
inside of our actions more specifically
6790
04:20:20,640 --> 04:20:23,040
aud actions we said that we need to log
6791
04:20:23,040 --> 04:20:25,439
in the user but at the time we didn't
6792
04:20:25,439 --> 04:20:27,520
have the actual endpoints on the back
6793
04:20:27,520 --> 04:20:30,080
end that we needed to contact now we do
6794
04:20:30,080 --> 04:20:32,640
have them so that is our next step to
6795
04:20:32,640 --> 04:20:35,279
log in or to sign up the user
6796
04:20:35,279 --> 04:20:37,840
for that we're first going to create api
6797
04:20:37,840 --> 04:20:39,760
endpoints like we did for all of our
6798
04:20:39,760 --> 04:20:41,600
actions with the posts now we need to
6799
04:20:41,600 --> 04:20:44,560
create these only for the authentication
6800
04:20:44,560 --> 04:20:46,239
before we start with that we're first
6801
04:20:46,239 --> 04:20:47,920
going to do some updates to our current
6802
04:20:47,920 --> 04:20:50,560
setup i'm going to create an axios
6803
04:20:50,560 --> 04:20:53,600
instance const api and that is going to
6804
04:20:53,600 --> 04:20:57,040
be equal to axios dot create
6805
04:20:57,040 --> 04:21:00,479
inside of here we can create a base url
6806
04:21:00,479 --> 04:21:02,239
so you can put options like this an
6807
04:21:02,239 --> 04:21:04,159
empty object and then inside of there
6808
04:21:04,159 --> 04:21:06,800
you can specify the base url
6809
04:21:06,800 --> 04:21:09,040
now we're going to set the base url to
6810
04:21:09,040 --> 04:21:10,000
be
6811
04:21:10,000 --> 04:21:13,439
a string of http forward slash localhost
6812
04:21:13,439 --> 04:21:16,800
and then 5000 just like so we don't want
6813
04:21:16,800 --> 04:21:19,520
to have that for slash posts because now
6814
04:21:19,520 --> 04:21:22,319
we're going to have the api also to make
6815
04:21:22,319 --> 04:21:23,920
some different calls for example to the
6816
04:21:23,920 --> 04:21:26,560
users and not only to the posts now we
6817
04:21:26,560 --> 04:21:28,880
can delete this and now instead of two
6818
04:21:28,880 --> 04:21:30,880
axios we'll be making all of our
6819
04:21:30,880 --> 04:21:33,920
requests to the uppercase api this is
6820
04:21:33,920 --> 04:21:35,760
going to do the same thing absolutely
6821
04:21:35,760 --> 04:21:37,760
the same thing but now you don't have to
6822
04:21:37,760 --> 04:21:40,880
specify the starting part of this url
6823
04:21:40,880 --> 04:21:43,439
so how do you change that well now it's
6824
04:21:43,439 --> 04:21:46,000
not going to be api.get forward slash
6825
04:21:46,000 --> 04:21:48,159
url it is simply going to be forward
6826
04:21:48,159 --> 04:21:51,359
slash posts we always have to add that
6827
04:21:51,359 --> 04:21:53,840
in this case we also want to reach out
6828
04:21:53,840 --> 04:21:56,560
to something like forward slash posts
6829
04:21:56,560 --> 04:21:57,840
that's the same
6830
04:21:57,840 --> 04:22:00,080
in this case it is going to be forward
6831
04:22:00,080 --> 04:22:03,600
slash post and then forward slash
6832
04:22:03,600 --> 04:22:06,399
id and then forward slash like post
6833
04:22:06,399 --> 04:22:07,680
in this case
6834
04:22:07,680 --> 04:22:10,640
posts only forward slash post and in
6835
04:22:10,640 --> 04:22:14,080
this case also posts we already have the
6836
04:22:14,080 --> 04:22:15,279
id
6837
04:22:15,279 --> 04:22:17,600
we did that so that later on we can use
6838
04:22:17,600 --> 04:22:19,760
some more advanced features of axios
6839
04:22:19,760 --> 04:22:21,359
some of the features of the not often
6840
04:22:21,359 --> 04:22:23,680
mentioned in easy tutorials and are
6841
04:22:23,680 --> 04:22:26,560
definitely a good thing to know
6842
04:22:26,560 --> 04:22:28,560
before that though let's simply create
6843
04:22:28,560 --> 04:22:30,880
the routes for the sign in and the sign
6844
04:22:30,880 --> 04:22:31,680
up
6845
04:22:31,680 --> 04:22:34,800
export const sign in that is going to be
6846
04:22:34,800 --> 04:22:36,720
an error function and that arrow
6847
04:22:36,720 --> 04:22:38,640
function is going to accept the form
6848
04:22:38,640 --> 04:22:39,760
data
6849
04:22:39,760 --> 04:22:41,760
so what do we call once we make that
6850
04:22:41,760 --> 04:22:42,840
request
6851
04:22:42,840 --> 04:22:45,439
api.post and that's going to be a post
6852
04:22:45,439 --> 04:22:47,600
request to forward slash users forward
6853
04:22:47,600 --> 04:22:49,520
slash sign in
6854
04:22:49,520 --> 04:22:51,920
finally as the post payload we want to
6855
04:22:51,920 --> 04:22:55,120
send the form data
6856
04:22:55,120 --> 04:22:57,279
i'm going to replicate this request and
6857
04:22:57,279 --> 04:23:00,239
we're just going to rename it to sign up
6858
04:23:00,239 --> 04:23:02,560
right here and we also have to rename it
6859
04:23:02,560 --> 04:23:05,840
right here sign up everything else
6860
04:23:05,840 --> 04:23:08,080
should be the same
6861
04:23:08,080 --> 04:23:10,720
with that said we can go back to our
6862
04:23:10,720 --> 04:23:13,439
actions right here and now we have those
6863
04:23:13,439 --> 04:23:17,359
api calls the sign in and the sign up
6864
04:23:17,359 --> 04:23:19,920
so what we can do is we can say const
6865
04:23:19,920 --> 04:23:22,319
destructure the data from our request
6866
04:23:22,319 --> 04:23:26,399
and make that equal to a weight api dot
6867
04:23:26,399 --> 04:23:29,840
sign in of course we have to pass the
6868
04:23:29,840 --> 04:23:31,359
form data
6869
04:23:31,359 --> 04:23:33,199
and i know this redux flow can be
6870
04:23:33,199 --> 04:23:35,520
confusing like we have this form data
6871
04:23:35,520 --> 04:23:37,439
here we have it in the api then we're
6872
04:23:37,439 --> 04:23:39,439
passing it from here where is that
6873
04:23:39,439 --> 04:23:41,279
coming from where is that going to how
6874
04:23:41,279 --> 04:23:43,520
do we get the reducers i know that can
6875
04:23:43,520 --> 04:23:45,040
be confusing
6876
04:23:45,040 --> 04:23:46,560
so one more time let me give you the
6877
04:23:46,560 --> 04:23:49,600
whole explanation of the redux workflow
6878
04:23:49,600 --> 04:23:52,479
first we get to the actual form the form
6879
04:23:52,479 --> 04:23:55,520
is a component right here in the auth
6880
04:23:55,520 --> 04:23:57,359
once we fill in all the inputs we want
6881
04:23:57,359 --> 04:23:59,600
to dispatch something this patch
6882
04:23:59,600 --> 04:24:00,960
immediately has something to do with
6883
04:24:00,960 --> 04:24:03,920
redux we are dispatching an action
6884
04:24:03,920 --> 04:24:05,920
this action is called sign up for
6885
04:24:05,920 --> 04:24:08,960
example let's do sign in in this case
6886
04:24:08,960 --> 04:24:12,000
so we're dispatching a sign in action
6887
04:24:12,000 --> 04:24:13,520
and we're giving it two different things
6888
04:24:13,520 --> 04:24:15,840
the form data and the history
6889
04:24:15,840 --> 04:24:18,640
once we dispatch that we come to our
6890
04:24:18,640 --> 04:24:19,760
actions
6891
04:24:19,760 --> 04:24:21,760
and that is this thing we're dispatching
6892
04:24:21,760 --> 04:24:23,279
this whole action
6893
04:24:23,279 --> 04:24:25,920
now this action makes another call to
6894
04:24:25,920 --> 04:24:27,760
our api just so we know where we're
6895
04:24:27,760 --> 04:24:30,319
going and it's basically this it makes a
6896
04:24:30,319 --> 04:24:32,960
post request says hey database get me
6897
04:24:32,960 --> 04:24:35,840
some data and return it to me and we
6898
04:24:35,840 --> 04:24:38,159
write in our action that's what we do we
6899
04:24:38,159 --> 04:24:41,040
get some data and we get it right there
6900
04:24:41,040 --> 04:24:43,040
finally from our action creator we can
6901
04:24:43,040 --> 04:24:45,359
finally dispatch some things and then
6902
04:24:45,359 --> 04:24:48,399
we're coming into our reducers so in
6903
04:24:48,399 --> 04:24:50,560
this case once we have the data we want
6904
04:24:50,560 --> 04:24:54,399
to dispatch an action with a type of
6905
04:24:54,399 --> 04:24:55,439
auth
6906
04:24:55,439 --> 04:24:58,000
and we want to pass over the data to our
6907
04:24:58,000 --> 04:24:59,199
reducer
6908
04:24:59,199 --> 04:25:00,479
now we're going to repeat the same
6909
04:25:00,479 --> 04:25:03,840
process for the sign up it is identical
6910
04:25:03,840 --> 04:25:06,560
so in here we get the same thing but we
6911
04:25:06,560 --> 04:25:10,159
change this from sign in to sign up and
6912
04:25:10,159 --> 04:25:13,520
with that our odd actions are done now
6913
04:25:13,520 --> 04:25:16,239
we have to see if our odd reducers are
6914
04:25:16,239 --> 04:25:17,680
also done
6915
04:25:17,680 --> 04:25:20,000
inside of here we're getting the profile
6916
04:25:20,000 --> 04:25:21,600
and then we're setting it to the local
6917
04:25:21,600 --> 04:25:24,880
storage so this part also seems to be
6918
04:25:24,880 --> 04:25:27,520
done for now to check if it works i'm
6919
04:25:27,520 --> 04:25:29,600
gonna save all the files and go back to
6920
04:25:29,600 --> 04:25:30,960
the browser
6921
04:25:30,960 --> 04:25:33,840
okay once we are here for now we cannot
6922
04:25:33,840 --> 04:25:36,479
see the posts but that's okay let's try
6923
04:25:36,479 --> 04:25:38,080
to sign up
6924
04:25:38,080 --> 04:25:40,080
inside of here we have to sign in but
6925
04:25:40,080 --> 04:25:42,159
let's first create an account
6926
04:25:42,159 --> 04:25:44,159
let's do something like john wick and
6927
04:25:44,159 --> 04:25:45,760
that is going to be john
6928
04:25:45,760 --> 04:25:47,199
wick
6929
04:25:47,199 --> 04:25:48,800
gmail.com
6930
04:25:48,800 --> 04:25:50,239
the password is going to be one two
6931
04:25:50,239 --> 04:25:51,840
three one two three
6932
04:25:51,840 --> 04:25:53,840
first of all obviously something is
6933
04:25:53,840 --> 04:25:56,239
wrong right there because our input is
6934
04:25:56,239 --> 04:25:59,199
not hidden it's not a password and then
6935
04:25:59,199 --> 04:26:01,520
our repeat password looks okay so let's
6936
04:26:01,520 --> 04:26:03,120
go ahead and check what's wrong with
6937
04:26:03,120 --> 04:26:05,760
that why don't we have our password
6938
04:26:05,760 --> 04:26:07,680
that is going to be in the auth more
6939
04:26:07,680 --> 04:26:09,920
specifically in the password field so
6940
04:26:09,920 --> 04:26:12,000
that should be right here
6941
04:26:12,000 --> 04:26:14,239
so if the show password is true then set
6942
04:26:14,239 --> 04:26:16,000
that to text otherwise show that to
6943
04:26:16,000 --> 04:26:18,560
password okay that seems to be great but
6944
04:26:18,560 --> 04:26:19,600
maybe
6945
04:26:19,600 --> 04:26:21,439
our show password is wrong so let's
6946
04:26:21,439 --> 04:26:23,680
check the show password
6947
04:26:23,680 --> 04:26:25,840
show password by default should be set
6948
04:26:25,840 --> 04:26:28,399
to false and it is
6949
04:26:28,399 --> 04:26:31,359
so where does the error seem to be let's
6950
04:26:31,359 --> 04:26:32,800
check that out
6951
04:26:32,800 --> 04:26:35,040
well one thing that i can notice is that
6952
04:26:35,040 --> 04:26:37,279
in here in the switch mode we're calling
6953
04:26:37,279 --> 04:26:39,439
handle show password and setting that to
6954
04:26:39,439 --> 04:26:40,399
false
6955
04:26:40,399 --> 04:26:42,159
we simply should have
6956
04:26:42,159 --> 04:26:43,279
called the
6957
04:26:43,279 --> 04:26:45,840
set show password and set that to false
6958
04:26:45,840 --> 04:26:47,359
so inside of here i'm going to change
6959
04:26:47,359 --> 04:26:49,760
this to set show password
6960
04:26:49,760 --> 04:26:51,279
let's save that and see if it makes a
6961
04:26:51,279 --> 04:26:53,359
difference
6962
04:26:53,359 --> 04:26:55,760
okay now it's good but i still don't see
6963
04:26:55,760 --> 04:26:58,159
that icon on the right side that lets me
6964
04:26:58,159 --> 04:27:00,000
toggle this thing so let's see what is
6965
04:27:00,000 --> 04:27:01,680
wrong with that
6966
04:27:01,680 --> 04:27:04,080
so if i go back right there
6967
04:27:04,080 --> 04:27:05,520
and that's going to be inside of our
6968
04:27:05,520 --> 04:27:07,040
input component so i'm going to head
6969
04:27:07,040 --> 04:27:08,000
there
6970
04:27:08,000 --> 04:27:08,960
let's see
6971
04:27:08,960 --> 04:27:12,239
so if name is equal to password oh there
6972
04:27:12,239 --> 04:27:14,960
we go password what is a password
6973
04:27:14,960 --> 04:27:16,720
javascript doesn't know that so
6974
04:27:16,720 --> 04:27:19,840
obviously we have to make this password
6975
04:27:19,840 --> 04:27:21,840
now if we save that let's see if that is
6976
04:27:21,840 --> 04:27:23,600
going to make a difference
6977
04:27:23,600 --> 04:27:25,680
there we go now we have this nice icon
6978
04:27:25,680 --> 04:27:28,560
we can we can again go as john wick
6979
04:27:28,560 --> 04:27:30,159
john wick
6980
04:27:30,159 --> 04:27:32,159
one two three one two three
6981
04:27:32,159 --> 04:27:33,840
let's see if we made a mistake we
6982
04:27:33,840 --> 04:27:36,640
haven't that's really good and finally
6983
04:27:36,640 --> 04:27:38,880
repeat password one two three one two
6984
04:27:38,880 --> 04:27:42,000
three now i'm going to collapse this i'm
6985
04:27:42,000 --> 04:27:44,239
going to clear the console and go to the
6986
04:27:44,239 --> 04:27:47,439
application tab in here let's see if our
6987
04:27:47,439 --> 04:27:49,840
local storage is going to be populated
6988
04:27:49,840 --> 04:27:52,880
with our newly created profile
6989
04:27:52,880 --> 04:27:55,359
i'm going to click sign up right now
6990
04:27:55,359 --> 04:27:57,920
and looks like something did not work
6991
04:27:57,920 --> 04:27:59,760
and don't worry i just wanted to show
6992
04:27:59,760 --> 04:28:01,120
you that this is the thing that usually
6993
04:28:01,120 --> 04:28:03,439
happens remember for the past half an
6994
04:28:03,439 --> 04:28:05,600
hour or so we worked on the back end
6995
04:28:05,600 --> 04:28:07,680
solely we didn't check for mistakes we
6996
04:28:07,680 --> 04:28:09,279
didn't see if anything goes wrong we
6997
04:28:09,279 --> 04:28:11,359
just went ahead and tested it usually
6998
04:28:11,359 --> 04:28:12,640
when you change something for a bigger
6999
04:28:12,640 --> 04:28:14,800
amount of time it's not going to work
7000
04:28:14,800 --> 04:28:16,399
the first time you test it
7001
04:28:16,399 --> 04:28:18,239
that's just what i wanted to show you so
7002
04:28:18,239 --> 04:28:20,880
right now we have 404 and looks like
7003
04:28:20,880 --> 04:28:23,760
it's just psy up so it's just one more
7004
04:28:23,760 --> 04:28:25,920
typo and this is going to be happening
7005
04:28:25,920 --> 04:28:28,080
on the front-end side i think so if i go
7006
04:28:28,080 --> 04:28:29,600
back to the code
7007
04:28:29,600 --> 04:28:33,520
to our api there we go sign up so we're
7008
04:28:33,520 --> 04:28:36,399
just missing that n right here
7009
04:28:36,399 --> 04:28:38,159
with that changed let's try to make
7010
04:28:38,159 --> 04:28:39,520
another request
7011
04:28:39,520 --> 04:28:41,120
john wick
7012
04:28:41,120 --> 04:28:43,680
again john wick one two three one two
7013
04:28:43,680 --> 04:28:46,560
three and let's test it out again i'm
7014
04:28:46,560 --> 04:28:48,720
going to open the application tab to see
7015
04:28:48,720 --> 04:28:50,319
if our local storage is going to get
7016
04:28:50,319 --> 04:28:52,000
populated
7017
04:28:52,000 --> 04:28:53,120
sign up
7018
04:28:53,120 --> 04:28:55,279
and again we have an error
7019
04:28:55,279 --> 04:28:58,399
sign up not found localhost 5000 forward
7020
04:28:58,399 --> 04:29:02,159
slash users forward slash sign up
7021
04:29:02,159 --> 04:29:03,920
okay so now i see that we are making a
7022
04:29:03,920 --> 04:29:05,920
request to the correct route localhost
7023
04:29:05,920 --> 04:29:08,399
5000 forward slash users forward slash
7024
04:29:08,399 --> 04:29:09,439
sign up
7025
04:29:09,439 --> 04:29:12,800
let's see what seems to be the problem
7026
04:29:12,800 --> 04:29:14,800
i think that on the back end in our
7027
04:29:14,800 --> 04:29:17,199
routes more specifically in the index.js
7028
04:29:17,199 --> 04:29:20,800
of the server we said user not users so
7029
04:29:20,800 --> 04:29:23,040
again it's just a small typo where you
7030
04:29:23,040 --> 04:29:24,479
have to be really concise with the
7031
04:29:24,479 --> 04:29:26,159
routes on the back end and the routes on
7032
04:29:26,159 --> 04:29:28,239
the front end so now that i change this
7033
04:29:28,239 --> 04:29:31,600
to user it should work right now
7034
04:29:31,600 --> 04:29:32,399
again
7035
04:29:32,399 --> 04:29:35,040
john wick one two three one two three
7036
04:29:35,040 --> 04:29:38,319
and let's test it out
7037
04:29:38,319 --> 04:29:40,800
okay there we go looks like we are
7038
04:29:40,800 --> 04:29:43,359
logged in we have our j right there john
7039
04:29:43,359 --> 04:29:46,159
wick and let's open the application tab
7040
04:29:46,159 --> 04:29:48,560
and would you look at that we have our
7041
04:29:48,560 --> 04:29:51,040
email we have our name our password
7042
04:29:51,040 --> 04:29:53,120
which is now hashed take a look at this
7043
04:29:53,120 --> 04:29:55,040
this is not one two three one three this
7044
04:29:55,040 --> 04:29:57,199
is a complete hash of the password and
7045
04:29:57,199 --> 04:29:59,600
finally we have the backend generated
7046
04:29:59,600 --> 04:30:01,840
token which stores all the information
7047
04:30:01,840 --> 04:30:04,239
about the user so now if i refresh the
7048
04:30:04,239 --> 04:30:07,199
page the user shouldn't be logged out
7049
04:30:07,199 --> 04:30:10,319
and there we go he is not he is still
7050
04:30:10,319 --> 04:30:11,520
here
7051
04:30:11,520 --> 04:30:14,720
finally can we log out the answer is yes
7052
04:30:14,720 --> 04:30:17,359
we can and to test our sign in let's go
7053
04:30:17,359 --> 04:30:19,520
with john wick one more time one two
7054
04:30:19,520 --> 04:30:22,960
three one two three and test it out okay
7055
04:30:22,960 --> 04:30:24,960
with the sign in we again got a warning
7056
04:30:24,960 --> 04:30:27,920
or an error saying user okay it looks
7057
04:30:27,920 --> 04:30:29,680
like i'm missing the end right there you
7058
04:30:29,680 --> 04:30:31,760
guys are sure seeing this and are like
7059
04:30:31,760 --> 04:30:34,239
oh you missed the end there so okay now
7060
04:30:34,239 --> 04:30:36,319
we have it now we should be able to sign
7061
04:30:36,319 --> 04:30:37,199
in
7062
04:30:37,199 --> 04:30:39,040
one more time
7063
04:30:39,040 --> 04:30:41,279
and let's do it
7064
04:30:41,279 --> 04:30:43,680
there we have it we successfully created
7065
04:30:43,680 --> 04:30:46,080
the account stored it in the database
7066
04:30:46,080 --> 04:30:49,199
registered and logged in and the login
7067
04:30:49,199 --> 04:30:51,359
is also persistent you can refresh all
7068
04:30:51,359 --> 04:30:53,279
the way you want the user is still going
7069
04:30:53,279 --> 04:30:56,239
to be signed in for an hour or until he
7070
04:30:56,239 --> 04:30:58,800
presses log out now our next step is to
7071
04:30:58,800 --> 04:31:01,120
check if the user can do all of the user
7072
04:31:01,120 --> 04:31:03,520
actions like creating a post or deleting
7073
04:31:03,520 --> 04:31:07,040
a post or maybe liking some posts so
7074
04:31:07,040 --> 04:31:09,600
that's going to be our next step
7075
04:31:09,600 --> 04:31:11,600
before we check for all of these actions
7076
04:31:11,600 --> 04:31:13,439
though there's one more thing that we
7077
04:31:13,439 --> 04:31:15,600
have to do and that is going to be in
7078
04:31:15,600 --> 04:31:18,159
the api remember how i told you that we
7079
04:31:18,159 --> 04:31:20,080
created this axios instance so that we
7080
04:31:20,080 --> 04:31:22,560
can add one more thing to it
7081
04:31:22,560 --> 04:31:24,640
and that one thing is going to help our
7082
04:31:24,640 --> 04:31:27,199
odd middleware our middleware cannot
7083
04:31:27,199 --> 04:31:29,680
work without that one thing
7084
04:31:29,680 --> 04:31:32,080
and that thing is adding something
7085
04:31:32,080 --> 04:31:35,520
specific to each one of our requests how
7086
04:31:35,520 --> 04:31:36,800
do we do that
7087
04:31:36,800 --> 04:31:40,880
we can say api dot interceptors and dot
7088
04:31:40,880 --> 04:31:42,000
request
7089
04:31:42,000 --> 04:31:44,000
this is going to be a function that's
7090
04:31:44,000 --> 04:31:45,439
going to happen
7091
04:31:45,439 --> 04:31:48,159
on each one of our requests so in here
7092
04:31:48,159 --> 04:31:51,279
we can say dot use and then you provide
7093
04:31:51,279 --> 04:31:53,439
a callback function
7094
04:31:53,439 --> 04:31:56,000
that callback function gets a request as
7095
04:31:56,000 --> 04:31:57,840
the first parameter
7096
04:31:57,840 --> 04:32:01,040
so again this is going to happen before
7097
04:32:01,040 --> 04:32:03,359
all of these requests
7098
04:32:03,359 --> 04:32:05,199
so why do we need this because we have
7099
04:32:05,199 --> 04:32:08,239
to send our token back to our backend so
7100
04:32:08,239 --> 04:32:10,319
that the backend middleware can verify
7101
04:32:10,319 --> 04:32:12,800
that we are actually logged in for that
7102
04:32:12,800 --> 04:32:14,239
we can say if
7103
04:32:14,239 --> 04:32:17,239
localstorage.getitem
7104
04:32:17,520 --> 04:32:19,439
and want to get the item of profile
7105
04:32:19,439 --> 04:32:21,600
that's where we store the token so if
7106
04:32:21,600 --> 04:32:23,120
that exists
7107
04:32:23,120 --> 04:32:25,040
then we want to add something to our
7108
04:32:25,040 --> 04:32:27,040
request and that something is going to
7109
04:32:27,040 --> 04:32:31,680
be rec dot headers dot authorization
7110
04:32:31,680 --> 04:32:34,000
just like this
7111
04:32:34,000 --> 04:32:36,159
remember if we go to the back end and
7112
04:32:36,159 --> 04:32:39,279
take a look at our middleware right here
7113
04:32:39,279 --> 04:32:41,279
you can see that we're taking something
7114
04:32:41,279 --> 04:32:44,600
from rec.headers.authorization
7115
04:32:45,040 --> 04:32:47,439
and now they look at it this must be
7116
04:32:47,439 --> 04:32:49,120
uppercased
7117
04:32:49,120 --> 04:32:51,600
great so now let's take a look
7118
04:32:51,600 --> 04:32:53,600
wreck.headers.authorization
7119
04:32:53,600 --> 04:32:56,479
in here we need to put our token and the
7120
04:32:56,479 --> 04:32:58,399
token is going to be a string
7121
04:32:58,399 --> 04:33:00,960
and it needs to start with the word
7122
04:33:00,960 --> 04:33:03,520
bearer so this is going to be a bearer
7123
04:33:03,520 --> 04:33:06,320
token the first thing is the bearer just
7124
04:33:06,320 --> 04:33:08,080
a string and then the second thing
7125
04:33:08,080 --> 04:33:11,520
divided by a space is the actual token
7126
04:33:11,520 --> 04:33:14,879
we can get that by saying json.parse
7127
04:33:14,879 --> 04:33:18,320
inside of there we get the local storage
7128
04:33:18,320 --> 04:33:22,400
dot get item we get the profile
7129
04:33:22,400 --> 04:33:24,320
and then finally if i can close this
7130
04:33:24,320 --> 04:33:25,840
correctly
7131
04:33:25,840 --> 04:33:29,680
on that profile we need to say dot token
7132
04:33:29,680 --> 04:33:31,680
like this so we want to get the token
7133
04:33:31,680 --> 04:33:34,561
from that specific profile it looks like
7134
04:33:34,561 --> 04:33:36,080
i'm missing something
7135
04:33:36,080 --> 04:33:39,199
no that seems to be it so we're adding
7136
04:33:39,199 --> 04:33:41,919
the bearer prepending that string to our
7137
04:33:41,919 --> 04:33:44,320
token and now we're adding that to each
7138
04:33:44,320 --> 04:33:45,919
and every request
7139
04:33:45,919 --> 04:33:48,080
finally with interceptors we have to
7140
04:33:48,080 --> 04:33:50,799
return the actual request so that we can
7141
04:33:50,799 --> 04:33:54,719
make all these future requests
7142
04:33:54,719 --> 04:33:57,039
with that our backend will be able to
7143
04:33:57,039 --> 04:33:59,840
get a specific header and then based on
7144
04:33:59,840 --> 04:34:01,759
that header we can do what we've done
7145
04:34:01,759 --> 04:34:04,561
here decode the data and based on that
7146
04:34:04,561 --> 04:34:06,400
our backend is going to know that our
7147
04:34:06,400 --> 04:34:09,278
user is indeed logged in
7148
04:34:09,278 --> 04:34:11,599
so for the user creation now that we
7149
04:34:11,599 --> 04:34:13,680
have this rack user id on the back end
7150
04:34:13,680 --> 04:34:16,639
we can head to controllers and posts
7151
04:34:16,639 --> 04:34:18,561
we've dealt with the like post but we
7152
04:34:18,561 --> 04:34:21,039
also have to make a change to our create
7153
04:34:21,039 --> 04:34:22,000
post
7154
04:34:22,000 --> 04:34:23,680
right here
7155
04:34:23,680 --> 04:34:25,278
so you can find that create post
7156
04:34:25,278 --> 04:34:27,680
controller in the post.js controllers
7157
04:34:27,680 --> 04:34:29,278
and that in here we can make some
7158
04:34:29,278 --> 04:34:30,719
changes
7159
04:34:30,719 --> 04:34:32,561
inside of here we don't have to take all
7160
04:34:32,561 --> 04:34:34,480
of these values because they are just
7161
04:34:34,480 --> 04:34:36,879
one thing they are the entire post so we
7162
04:34:36,879 --> 04:34:39,359
want to have the entire post and then we
7163
04:34:39,359 --> 04:34:41,359
set the new post message to be equal to
7164
04:34:41,359 --> 04:34:43,039
new post message
7165
04:34:43,039 --> 04:34:45,039
instead of here we're going to have the
7166
04:34:45,039 --> 04:34:47,840
entire post so what we can do is first
7167
04:34:47,840 --> 04:34:50,799
of all spread the values of a specific
7168
04:34:50,799 --> 04:34:53,599
post and then this is the key part we
7169
04:34:53,599 --> 04:34:56,320
want to set the creator of the post so
7170
04:34:56,320 --> 04:35:01,438
creator to be equal to rec dot user id
7171
04:35:01,438 --> 04:35:03,438
so now our backend automatically
7172
04:35:03,438 --> 04:35:07,359
specifies the creator of a specific post
7173
04:35:07,359 --> 04:35:10,240
and then we also want to add a created
7174
04:35:10,240 --> 04:35:13,118
add property so created
7175
04:35:13,118 --> 04:35:15,840
add and created that we can set that to
7176
04:35:15,840 --> 04:35:18,320
be equal to new date call that as a
7177
04:35:18,320 --> 04:35:22,400
function and then put dot to iso string
7178
04:35:22,400 --> 04:35:25,039
this is going to make sure that our date
7179
04:35:25,039 --> 04:35:27,759
definitely shows the value when it was
7180
04:35:27,759 --> 04:35:28,799
created
7181
04:35:28,799 --> 04:35:30,160
so these were some of the changes that
7182
04:35:30,160 --> 04:35:32,480
we had to make to this create post but
7183
04:35:32,480 --> 04:35:34,799
now that we change this now our creator
7184
04:35:34,799 --> 04:35:36,799
is no longer going to be the name that
7185
04:35:36,799 --> 04:35:39,680
we ourself specify it is going to be an
7186
04:35:39,680 --> 04:35:41,039
id
7187
04:35:41,039 --> 04:35:42,958
that means that we have to go to our
7188
04:35:42,958 --> 04:35:46,000
post message model right there and now
7189
04:35:46,000 --> 04:35:48,240
along with the creator we need to add
7190
04:35:48,240 --> 04:35:50,719
one more thing and that is going to be a
7191
04:35:50,719 --> 04:35:53,118
name so we still want to have the name
7192
04:35:53,118 --> 04:35:55,438
but it is going to be the name of the
7193
04:35:55,438 --> 04:35:57,680
person who is logged in not something
7194
04:35:57,680 --> 04:36:00,480
that we can type ourselves before we had
7195
04:36:00,480 --> 04:36:02,000
to type in our name because we didn't
7196
04:36:02,000 --> 04:36:05,199
have the login but now we do and that is
7197
04:36:05,199 --> 04:36:06,719
a great thing
7198
04:36:06,719 --> 04:36:08,958
i understand that some of these changes
7199
04:36:08,958 --> 04:36:11,599
are really hard to get a good grasp on
7200
04:36:11,599 --> 04:36:13,599
if your first time building this thing
7201
04:36:13,599 --> 04:36:16,320
like what is even json web token how
7202
04:36:16,320 --> 04:36:18,320
does that relate to middleware and why
7203
04:36:18,320 --> 04:36:20,561
are we adding headers and a token each
7204
04:36:20,561 --> 04:36:21,919
time that we send the request from the
7205
04:36:21,919 --> 04:36:24,320
front end these things can definitely be
7206
04:36:24,320 --> 04:36:26,320
dawn thing so if you're having some
7207
04:36:26,320 --> 04:36:28,240
troubles understanding definitely make
7208
04:36:28,240 --> 04:36:30,000
sure to rewind some of the parts of the
7209
04:36:30,000 --> 04:36:33,278
video and re-watch them but still one
7210
04:36:33,278 --> 04:36:35,359
more time i'm going to make sure to go
7211
04:36:35,359 --> 04:36:37,199
through the whole workflow to explain
7212
04:36:37,199 --> 04:36:38,639
what's happening
7213
04:36:38,639 --> 04:36:40,958
this time i'll show all of that in the
7214
04:36:40,958 --> 04:36:43,278
browser's console so one more time i'll
7215
04:36:43,278 --> 04:36:45,680
go back right here and we're going to
7216
04:36:45,680 --> 04:36:47,840
open our network tab i'm going to be
7217
04:36:47,840 --> 04:36:49,840
looking at the requests that we've made
7218
04:36:49,840 --> 04:36:52,400
from our front end to the back end
7219
04:36:52,400 --> 04:36:54,240
before we do that i forgot to remove
7220
04:36:54,240 --> 04:36:56,320
this creator right here we don't need
7221
04:36:56,320 --> 04:36:58,879
that anymore so let's change that in our
7222
04:36:58,879 --> 04:37:03,278
code that is going to be in the form
7223
04:37:03,278 --> 04:37:06,000
right here so inside of the form we have
7224
04:37:06,000 --> 04:37:09,520
to remove that creator variable
7225
04:37:09,520 --> 04:37:11,359
we can completely remove it since we
7226
04:37:11,359 --> 04:37:13,199
don't need that anymore
7227
04:37:13,199 --> 04:37:15,438
and are there some other changes yeah we
7228
04:37:15,438 --> 04:37:17,359
have to remove it from here set both
7229
04:37:17,359 --> 04:37:18,561
data
7230
04:37:18,561 --> 04:37:20,400
and is there something else that we want
7231
04:37:20,400 --> 04:37:22,000
to change
7232
04:37:22,000 --> 04:37:24,320
yep we have the creator right here as
7233
04:37:24,320 --> 04:37:26,840
well so we want to delete
7234
04:37:26,840 --> 04:37:30,400
that okay now we remove that name but
7235
04:37:30,400 --> 04:37:32,080
how is our backend going to know what
7236
04:37:32,080 --> 04:37:35,118
name do we use well what you can do is
7237
04:37:35,118 --> 04:37:37,039
right in here when you're sending that
7238
04:37:37,039 --> 04:37:39,759
specific user you can add it so if you
7239
04:37:39,759 --> 04:37:42,879
dispatch a creation right here we can
7240
04:37:42,879 --> 04:37:45,199
specify the pose data so we can create a
7241
04:37:45,199 --> 04:37:46,639
new object
7242
04:37:46,639 --> 04:37:48,639
we can spread the post data so this is
7243
04:37:48,639 --> 04:37:50,480
not going to change anything that we had
7244
04:37:50,480 --> 04:37:53,359
so far but now we can add a name
7245
04:37:53,359 --> 04:37:56,160
property and name is going to be equal
7246
04:37:56,160 --> 04:37:58,561
to the users that's currently logged in
7247
04:37:58,561 --> 04:38:00,639
and that user is going to be stored in
7248
04:38:00,639 --> 04:38:02,639
the local storage so how do we get the
7249
04:38:02,639 --> 04:38:06,719
user you can say const user is equal to
7250
04:38:06,719 --> 04:38:08,400
json.parse
7251
04:38:08,400 --> 04:38:10,561
and then local storage
7252
04:38:10,561 --> 04:38:13,840
dot get item and then in there you put a
7253
04:38:13,840 --> 04:38:16,400
string of profile this is going to make
7254
04:38:16,400 --> 04:38:18,639
sure to grab the user for you
7255
04:38:18,639 --> 04:38:21,118
now in here we can say user question
7256
04:38:21,118 --> 04:38:22,958
mark dot that's checking if we currently
7257
04:38:22,958 --> 04:38:25,840
have the user and then result question
7258
04:38:25,840 --> 04:38:28,879
mark dot and finally in here is the name
7259
04:38:28,879 --> 04:38:30,080
of the user
7260
04:38:30,080 --> 04:38:32,080
all of that is going to be equal to a
7261
04:38:32,080 --> 04:38:33,118
name
7262
04:38:33,118 --> 04:38:35,118
if somebody is changing this we also
7263
04:38:35,118 --> 04:38:37,039
want to keep his username so in this
7264
04:38:37,039 --> 04:38:38,719
case we're also going to do the same
7265
04:38:38,719 --> 04:38:42,400
thing create an object spread the post
7266
04:38:42,400 --> 04:38:44,879
data and finally set the name to be
7267
04:38:44,879 --> 04:38:48,600
equal to user.result.name
7268
04:38:48,639 --> 04:38:50,561
one more thing that we can do in here is
7269
04:38:50,561 --> 04:38:52,320
check if there is no current to logged
7270
04:38:52,320 --> 04:38:54,561
in users in that case we want to show a
7271
04:38:54,561 --> 04:38:56,958
card that says no you cannot create a
7272
04:38:56,958 --> 04:38:59,438
post right now so let's do that
7273
04:38:59,438 --> 04:39:00,240
if
7274
04:39:00,240 --> 04:39:03,438
no user question mark dot result
7275
04:39:03,438 --> 04:39:05,520
question mark dot name
7276
04:39:05,520 --> 04:39:08,400
in that case we want to return something
7277
04:39:08,400 --> 04:39:10,719
and that something is going to be a
7278
04:39:10,719 --> 04:39:12,639
paper like we're wrapping something in
7279
04:39:12,639 --> 04:39:15,278
paper like our form is wrapped
7280
04:39:15,278 --> 04:39:18,160
that paper is going to have a class name
7281
04:39:18,160 --> 04:39:21,199
of classes dot paper
7282
04:39:21,199 --> 04:39:22,879
and then inside of here we're gonna
7283
04:39:22,879 --> 04:39:25,840
simply add one typography
7284
04:39:25,840 --> 04:39:27,759
that typography is going to be of a
7285
04:39:27,759 --> 04:39:30,719
variant h6 and it is going to have a
7286
04:39:30,719 --> 04:39:34,160
line equal to center
7287
04:39:34,160 --> 04:39:37,280
in there i'm gonna type please sign in
7288
04:39:37,280 --> 04:39:41,520
to create your own memories and like
7289
04:39:41,520 --> 04:39:43,760
others memories
7290
04:39:43,760 --> 04:39:45,840
now we can save that and i think that's
7291
04:39:45,840 --> 04:39:48,718
going to be it with our form let's go
7292
04:39:48,718 --> 04:39:51,120
back and test it out now we have the
7293
04:39:51,120 --> 04:39:53,440
title message everything else is the
7294
04:39:53,440 --> 04:39:54,400
same
7295
04:39:54,400 --> 04:39:55,200
okay
7296
04:39:55,200 --> 04:39:57,120
you can head to the network tab and you
7297
04:39:57,120 --> 04:39:58,718
can clear it now we're going to be
7298
04:39:58,718 --> 04:40:00,160
looking at the requests that we are
7299
04:40:00,160 --> 04:40:02,160
making from the front end to the back
7300
04:40:02,160 --> 04:40:03,120
end
7301
04:40:03,120 --> 04:40:04,160
again
7302
04:40:04,160 --> 04:40:06,718
title is going to be something like new
7303
04:40:06,718 --> 04:40:09,600
year message is going to be happy new
7304
04:40:09,600 --> 04:40:12,160
year and by the way guys i really wish
7305
04:40:12,160 --> 04:40:14,638
you a happy new year a wonderful one a
7306
04:40:14,638 --> 04:40:17,760
productive one have fun enjoy it and i
7307
04:40:17,760 --> 04:40:19,600
wish it's definitely going to be better
7308
04:40:19,600 --> 04:40:21,520
than the previous one
7309
04:40:21,520 --> 04:40:23,840
okay now let's add the tags you know how
7310
04:40:23,840 --> 04:40:25,920
people do the tax new
7311
04:40:25,920 --> 04:40:26,958
year
7312
04:40:26,958 --> 04:40:30,480
happy new year and also let's do 2021
7313
04:40:30,480 --> 04:40:32,160
finally i'm going to select an image
7314
04:40:32,160 --> 04:40:36,480
let's do fireworks and let's submit it
7315
04:40:36,480 --> 04:40:39,200
there we go you can see that we have 204
7316
04:40:39,200 --> 04:40:41,680
requests right here 204 is not good you
7317
04:40:41,680 --> 04:40:44,160
can see that our post wasn't created but
7318
04:40:44,160 --> 04:40:46,878
let's open it and see what's in there
7319
04:40:46,878 --> 04:40:49,600
so 204 means that it didn't went right
7320
04:40:49,600 --> 04:40:52,240
but let's see what happened in here we
7321
04:40:52,240 --> 04:40:54,080
can see that
7322
04:40:54,080 --> 04:40:56,560
we don't have headers at least i don't
7323
04:40:56,560 --> 04:40:58,560
see them request headers
7324
04:40:58,560 --> 04:40:59,920
we don't have the headers for the
7325
04:40:59,920 --> 04:41:02,718
specific token yep they're definitely
7326
04:41:02,718 --> 04:41:03,760
missing
7327
04:41:03,760 --> 04:41:06,480
so let me open the application tab
7328
04:41:06,480 --> 04:41:08,560
and let's see here in the result do we
7329
04:41:08,560 --> 04:41:12,080
have our token we do it is right in here
7330
04:41:12,080 --> 04:41:15,120
but for some reason this token wasn't
7331
04:41:15,120 --> 04:41:17,520
added to our request
7332
04:41:17,520 --> 04:41:19,920
okay so in this first one we do have all
7333
04:41:19,920 --> 04:41:21,840
the information from the request payload
7334
04:41:21,840 --> 04:41:24,560
we have the tags title selected file and
7335
04:41:24,560 --> 04:41:27,280
the message but in the second request
7336
04:41:27,280 --> 04:41:30,080
we don't have anything we have to figure
7337
04:41:30,080 --> 04:41:32,560
out why the post wasn't made on the back
7338
04:41:32,560 --> 04:41:34,878
end and if we open our code more
7339
04:41:34,878 --> 04:41:37,200
specifically our terminal we should get
7340
04:41:37,200 --> 04:41:39,520
some kind of an error from the back end
7341
04:41:39,520 --> 04:41:42,240
and we do it is right in here
7342
04:41:42,240 --> 04:41:44,080
cannot read properties split of
7343
04:41:44,080 --> 04:41:46,080
undefined add auth
7344
04:41:46,080 --> 04:41:47,920
that is happening on the back end more
7345
04:41:47,920 --> 04:41:50,000
specifically in the middleware right
7346
04:41:50,000 --> 04:41:51,920
here it's saying that it cannot read
7347
04:41:51,920 --> 04:41:54,400
properties split off undefined
7348
04:41:54,400 --> 04:41:57,120
that means that direct.headers wasn't
7349
04:41:57,120 --> 04:41:58,480
sent
7350
04:41:58,480 --> 04:42:00,718
so one more time in here i'm going to
7351
04:42:00,718 --> 04:42:03,000
console.log this time just the
7352
04:42:03,000 --> 04:42:05,280
rec.headers to see if you're getting
7353
04:42:05,280 --> 04:42:07,360
absolutely any headers from the front
7354
04:42:07,360 --> 04:42:10,080
end i'm going to go back and just type
7355
04:42:10,080 --> 04:42:11,840
some gibberish right here
7356
04:42:11,840 --> 04:42:14,480
test this test and click submit let's
7357
04:42:14,480 --> 04:42:16,240
see if this times the headers are being
7358
04:42:16,240 --> 04:42:17,360
sent
7359
04:42:17,360 --> 04:42:19,760
looks like they're not if i go back we
7360
04:42:19,760 --> 04:42:22,718
get the same error again but we do see
7361
04:42:22,718 --> 04:42:24,798
some headers right there the question is
7362
04:42:24,798 --> 04:42:27,200
is do we have the authorization and
7363
04:42:27,200 --> 04:42:30,000
looks like there it is authorization
7364
04:42:30,000 --> 04:42:33,840
bearer token is right in here so that is
7365
04:42:33,840 --> 04:42:36,000
good we have them oh but here's the
7366
04:42:36,000 --> 04:42:36,718
thing
7367
04:42:36,718 --> 04:42:38,798
first time that i wrote authorization it
7368
04:42:38,798 --> 04:42:41,280
was correct with lowercase letter no
7369
04:42:41,280 --> 04:42:43,040
matter how you write it at the front end
7370
04:42:43,040 --> 04:42:44,560
at the back end it is always going to
7371
04:42:44,560 --> 04:42:47,520
receive as the lowercase key letter
7372
04:42:47,520 --> 04:42:49,360
okay so now that this is done there is
7373
04:42:49,360 --> 04:42:51,040
one more thing that we have to do and
7374
04:42:51,040 --> 04:42:54,798
that is exchange the creator for name in
7375
04:42:54,798 --> 04:42:57,120
our post that is going to be right here
7376
04:42:57,120 --> 04:42:59,760
inside of posts and then post
7377
04:42:59,760 --> 04:43:01,680
before creator was the name of our
7378
04:43:01,680 --> 04:43:03,680
person but now it's not going to be the
7379
04:43:03,680 --> 04:43:07,200
creator it is going to be simply name we
7380
04:43:07,200 --> 04:43:10,000
change that the creator is now the id of
7381
04:43:10,000 --> 04:43:11,840
the person who is creating it
7382
04:43:11,840 --> 04:43:13,440
with that said we can collapse our
7383
04:43:13,440 --> 04:43:16,560
terminal and potentially go back and try
7384
04:43:16,560 --> 04:43:19,200
one more time let's go with new year one
7385
04:43:19,200 --> 04:43:20,320
more time
7386
04:43:20,320 --> 04:43:22,400
happy new year
7387
04:43:22,400 --> 04:43:24,638
and in here i'm just gonna do holidays
7388
04:43:24,638 --> 04:43:25,440
now
7389
04:43:25,440 --> 04:43:27,680
we're gonna add the fireworks image and
7390
04:43:27,680 --> 04:43:30,240
let's see our request
7391
04:43:30,240 --> 04:43:33,040
there we go john wick's name was
7392
04:43:33,040 --> 04:43:35,600
immediately populated the timestamp is
7393
04:43:35,600 --> 04:43:38,400
correct a few seconds ago hashtags are
7394
04:43:38,400 --> 04:43:41,280
here title is here and everything is
7395
04:43:41,280 --> 04:43:43,680
here if we take a look in here we get
7396
04:43:43,680 --> 04:43:46,480
201 created and we get all of the
7397
04:43:46,480 --> 04:43:49,920
information for our specific post this
7398
04:43:49,920 --> 04:43:52,320
is great that means the user can
7399
04:43:52,320 --> 04:43:55,520
successfully create posts right now
7400
04:43:55,520 --> 04:43:57,440
you can see that our user also has the
7401
04:43:57,440 --> 04:43:59,440
edit button and the delete button we
7402
04:43:59,440 --> 04:44:00,798
still haven't changed those for
7403
04:44:00,798 --> 04:44:02,400
non-locked end user that's going to be
7404
04:44:02,400 --> 04:44:04,718
the next step and and we also have the
7405
04:44:04,718 --> 04:44:07,440
like button and now if we log out take a
7406
04:44:07,440 --> 04:44:08,240
look
7407
04:44:08,240 --> 04:44:09,760
please sign in to create your own
7408
04:44:09,760 --> 04:44:11,520
memories and like other people's
7409
04:44:11,520 --> 04:44:13,680
memories so right now we cannot create
7410
04:44:13,680 --> 04:44:16,000
new ones if we're not signed in
7411
04:44:16,000 --> 04:44:18,638
of course the edit delete and like
7412
04:44:18,638 --> 04:44:20,160
shouldn't be here as well because if
7413
04:44:20,160 --> 04:44:22,000
you're not logged in you shouldn't be
7414
04:44:22,000 --> 04:44:24,400
able to do those actions
7415
04:44:24,400 --> 04:44:27,440
okay so now let's log back in as john
7416
04:44:27,440 --> 04:44:30,080
wick and let's see if they can like
7417
04:44:30,080 --> 04:44:32,560
delete and edit these posts
7418
04:44:32,560 --> 04:44:34,878
we can start with like
7419
04:44:34,878 --> 04:44:37,600
and looks like our like action is
7420
04:44:37,600 --> 04:44:39,520
dispatched as you can see that right
7421
04:44:39,520 --> 04:44:42,560
here on the network tab we have 200
7422
04:44:42,560 --> 04:44:44,958
and the like was indeed added under
7423
04:44:44,958 --> 04:44:47,120
likes and if we remove it
7424
04:44:47,120 --> 04:44:48,878
let's take a look
7425
04:44:48,878 --> 04:44:51,440
yeah it was removed that means that the
7426
04:44:51,440 --> 04:44:54,160
backend for likes works completely but
7427
04:44:54,160 --> 04:44:56,000
on the front end we are not seeing the
7428
04:44:56,000 --> 04:44:58,080
changed as you can see we only have the
7429
04:44:58,080 --> 04:44:59,840
like we don't see the number of likes
7430
04:44:59,840 --> 04:45:02,000
that we have so that is going to be our
7431
04:45:02,000 --> 04:45:04,400
next step of course we are seeing likes
7432
04:45:04,400 --> 04:45:06,718
under the post and post is this
7433
04:45:06,718 --> 04:45:09,920
component post.js inside of here
7434
04:45:09,920 --> 04:45:12,080
somewhere we need to have the button to
7435
04:45:12,080 --> 04:45:15,360
like something and it is indeed here
7436
04:45:15,360 --> 04:45:16,958
just like and then we have the like
7437
04:45:16,958 --> 04:45:17,920
count
7438
04:45:17,920 --> 04:45:19,600
but considering the like count doesn't
7439
04:45:19,600 --> 04:45:22,560
exist anymore it's not showing it
7440
04:45:22,560 --> 04:45:24,958
so how do we fix that
7441
04:45:24,958 --> 04:45:26,958
well this is going to be a bit tougher
7442
04:45:26,958 --> 04:45:28,878
because you know how it goes with likes
7443
04:45:28,878 --> 04:45:30,958
like if you have one like and how it
7444
04:45:30,958 --> 04:45:33,200
goes with grammar at all you have one
7445
04:45:33,200 --> 04:45:35,120
like but then there are two
7446
04:45:35,120 --> 04:45:37,840
likes and then three likes and then if
7447
04:45:37,840 --> 04:45:39,280
you like something but other people
7448
04:45:39,280 --> 04:45:40,638
don't like something you have to have a
7449
04:45:40,638 --> 04:45:43,200
different message so to show all of
7450
04:45:43,200 --> 04:45:44,080
these
7451
04:45:44,080 --> 04:45:46,638
likes and like and different buttons
7452
04:45:46,638 --> 04:45:48,320
depending on if you liked something or
7453
04:45:48,320 --> 04:45:50,000
didn't like something i'm going to
7454
04:45:50,000 --> 04:45:52,000
create a new let's call it a sub
7455
04:45:52,000 --> 04:45:54,160
component of the post component it is
7456
04:45:54,160 --> 04:45:56,160
going to be called likes
7457
04:45:56,160 --> 04:45:57,840
the logic for creating the likes sub
7458
04:45:57,840 --> 04:45:59,600
component is going to be incredibly
7459
04:45:59,600 --> 04:46:01,440
simple but you have to write a lot of
7460
04:46:01,440 --> 04:46:03,120
code to get it right because you have to
7461
04:46:03,120 --> 04:46:04,878
check for a lot of things
7462
04:46:04,878 --> 04:46:06,878
so what i'm going to do is i'm simply
7463
04:46:06,878 --> 04:46:08,718
going to give you the logic for that
7464
04:46:08,718 --> 04:46:09,840
right here
7465
04:46:09,840 --> 04:46:11,680
i'm going to paste it because it would
7466
04:46:11,680 --> 04:46:13,440
take us quite some time to actually go
7467
04:46:13,440 --> 04:46:15,520
through everything and see what do we
7468
04:46:15,520 --> 04:46:18,240
have so just to go over it we are first
7469
04:46:18,240 --> 04:46:20,080
checking if a current person likes
7470
04:46:20,080 --> 04:46:22,320
something or if it didn't like something
7471
04:46:22,320 --> 04:46:24,080
we can check that in two ways we're
7472
04:46:24,080 --> 04:46:26,958
checking if the likes array contains the
7473
04:46:26,958 --> 04:46:29,440
id of the current person and that can be
7474
04:46:29,440 --> 04:46:31,760
either the google id if the person did
7475
04:46:31,760 --> 04:46:34,080
the all auth or it can be a custom id
7476
04:46:34,080 --> 04:46:35,440
from the database
7477
04:46:35,440 --> 04:46:37,840
if that is the case we want to say you
7478
04:46:37,840 --> 04:46:38,638
and
7479
04:46:38,638 --> 04:46:40,000
a certain number of people like
7480
04:46:40,000 --> 04:46:42,798
something or the post has one like or
7481
04:46:42,798 --> 04:46:45,120
multiple likes you can see the s there
7482
04:46:45,120 --> 04:46:46,878
and then if the person didn't like it we
7483
04:46:46,878 --> 04:46:49,600
can just say the number of like or likes
7484
04:46:49,600 --> 04:46:51,680
and so on and then if nothing happened
7485
04:46:51,680 --> 04:46:53,600
if you're the first to like it it's just
7486
04:46:53,600 --> 04:46:55,760
like so a lot of things happening here
7487
04:46:55,760 --> 04:46:57,120
but again just wanted to give you a bit
7488
04:46:57,120 --> 04:46:58,560
of code so you don't have to write this
7489
04:46:58,560 --> 04:47:00,160
by hand because really it's not
7490
04:47:00,160 --> 04:47:02,320
complicated logic it is really simple
7491
04:47:02,320 --> 04:47:03,680
but it takes some time to write
7492
04:47:03,680 --> 04:47:04,878
correctly
7493
04:47:04,878 --> 04:47:06,320
okay with that said you can see that
7494
04:47:06,320 --> 04:47:08,080
we're missing the user right there and
7495
04:47:08,080 --> 04:47:10,240
the user is just coming from the local
7496
04:47:10,240 --> 04:47:11,920
storage for example we're getting the
7497
04:47:11,920 --> 04:47:14,400
user from the form so you can copy that
7498
04:47:14,400 --> 04:47:17,440
line it is just going to be const user
7499
04:47:17,440 --> 04:47:20,480
is equal to json.parts local storage get
7500
04:47:20,480 --> 04:47:22,560
item and then profile
7501
04:47:22,560 --> 04:47:24,718
finally now that we have that we can put
7502
04:47:24,718 --> 04:47:28,000
those likes right inside of our like
7503
04:47:28,000 --> 04:47:30,080
button i'll try to expand it into
7504
04:47:30,080 --> 04:47:32,320
multiple lines like this we currently
7505
04:47:32,320 --> 04:47:35,360
have this but now we have the button i'm
7506
04:47:35,360 --> 04:47:38,080
also going to add a disabled property so
7507
04:47:38,080 --> 04:47:40,000
if we don't have the currently logged in
7508
04:47:40,000 --> 04:47:42,798
user i'm going to say disabled and it's
7509
04:47:42,798 --> 04:47:44,798
going to be disabled
7510
04:47:44,798 --> 04:47:48,160
if there is no user question mark dot
7511
04:47:48,160 --> 04:47:50,160
result so if there is no user we cannot
7512
04:47:50,160 --> 04:47:51,520
press the button
7513
04:47:51,520 --> 04:47:53,760
okay and now inside of that button we're
7514
04:47:53,760 --> 04:47:55,760
simply going to have our new component
7515
04:47:55,760 --> 04:47:58,638
which is called likes this should solve
7516
04:47:58,638 --> 04:48:00,718
all of our troubles with viewing the
7517
04:48:00,718 --> 04:48:03,440
likes great let's check it out
7518
04:48:03,440 --> 04:48:05,680
if i go back you can see we have one
7519
04:48:05,680 --> 04:48:07,920
like and it seems liked if i click it
7520
04:48:07,920 --> 04:48:10,560
again it says nothing it just says like
7521
04:48:10,560 --> 04:48:12,638
it doesn't say zero likes it's not blue
7522
04:48:12,638 --> 04:48:14,400
you can see it's just outlined and it
7523
04:48:14,400 --> 04:48:15,840
just says like
7524
04:48:15,840 --> 04:48:18,878
if i like it and if i log out now and
7525
04:48:18,878 --> 04:48:21,120
try to sign in as a different person for
7526
04:48:21,120 --> 04:48:22,878
example i'm gonna go here
7527
04:48:22,878 --> 04:48:25,040
and go with javascript mastery this time
7528
04:48:25,040 --> 04:48:26,878
you can see it's one like it's only
7529
04:48:26,878 --> 04:48:28,000
outlined
7530
04:48:28,000 --> 04:48:31,120
if i click it it's now two likes so that
7531
04:48:31,120 --> 04:48:33,280
looks really good it is like facebook
7532
04:48:33,280 --> 04:48:34,400
has it
7533
04:48:34,400 --> 04:48:36,080
if more than two people like this i
7534
04:48:36,080 --> 04:48:38,400
think it's gonna say you and three
7535
04:48:38,400 --> 04:48:40,878
others or five others or 100 others so
7536
04:48:40,878 --> 04:48:42,560
this is really good
7537
04:48:42,560 --> 04:48:44,160
with that said we can move on to the
7538
04:48:44,160 --> 04:48:45,760
next thing and that is happening right
7539
04:48:45,760 --> 04:48:47,440
now as we logged in with javascript
7540
04:48:47,440 --> 04:48:49,600
mastery as you can see
7541
04:48:49,600 --> 04:48:51,680
we didn't create this post of course we
7542
04:48:51,680 --> 04:48:53,600
can like it sure but we shouldn't be
7543
04:48:53,600 --> 04:48:56,160
able to delete it or edit it and now the
7544
04:48:56,160 --> 04:48:59,040
delete and edit buttons are right here
7545
04:48:59,040 --> 04:49:00,718
so we have to disable that from
7546
04:49:00,718 --> 04:49:03,280
happening let's go back to our post and
7547
04:49:03,280 --> 04:49:05,440
move those buttons away
7548
04:49:05,440 --> 04:49:07,600
inside of here what we have to do is we
7549
04:49:07,600 --> 04:49:10,000
have to check if the current post was
7550
04:49:10,000 --> 04:49:12,958
created from the current user only in
7551
04:49:12,958 --> 04:49:15,760
that case do we want to show the current
7552
04:49:15,760 --> 04:49:17,600
delete button
7553
04:49:17,600 --> 04:49:19,360
so to do that i'm going to open the
7554
04:49:19,360 --> 04:49:21,600
logic block and then inside of there i'm
7555
04:49:21,600 --> 04:49:23,520
going to write some parentheses i'm
7556
04:49:23,520 --> 04:49:26,000
going to check if user question mark dot
7557
04:49:26,000 --> 04:49:28,878
result question mark dot google id we
7558
04:49:28,878 --> 04:49:30,480
already have that so we're checking if
7559
04:49:30,480 --> 04:49:31,600
the id
7560
04:49:31,600 --> 04:49:34,638
is equal to post question mark dot
7561
04:49:34,638 --> 04:49:36,480
creator so we're checking if the
7562
04:49:36,480 --> 04:49:39,440
currently logged in user via google has
7563
04:49:39,440 --> 04:49:42,718
the same id as the creator of the post
7564
04:49:42,718 --> 04:49:45,440
or there is a second possibility if the
7565
04:49:45,440 --> 04:49:48,638
user question mark dot result question
7566
04:49:48,638 --> 04:49:52,480
mark dot underscore ib
7567
04:49:52,480 --> 04:49:54,958
if that is equal to post question mark
7568
04:49:54,958 --> 04:49:57,840
dot creator so now we're checking if the
7569
04:49:57,840 --> 04:50:00,160
google logged in user or if our manually
7570
04:50:00,160 --> 04:50:01,520
logged in user
7571
04:50:01,520 --> 04:50:04,718
is the creator of this post only if that
7572
04:50:04,718 --> 04:50:06,400
is the case then we want to show the
7573
04:50:06,400 --> 04:50:08,718
delete button so to do that we can do
7574
04:50:08,718 --> 04:50:12,160
what we learned and end and then in here
7575
04:50:12,160 --> 04:50:14,638
we put the button this is going to now
7576
04:50:14,638 --> 04:50:18,080
appear only if the logged in person is
7577
04:50:18,080 --> 04:50:20,638
the post creator
7578
04:50:20,638 --> 04:50:23,120
if we go back let's see if that works
7579
04:50:23,120 --> 04:50:26,480
and there we go the delete button is now
7580
04:50:26,480 --> 04:50:27,920
well deleted
7581
04:50:27,920 --> 04:50:29,520
let's try to do the same thing for the
7582
04:50:29,520 --> 04:50:31,360
edit remember the edit is hidden right
7583
04:50:31,360 --> 04:50:33,760
here in the top right
7584
04:50:33,760 --> 04:50:35,920
if we go back you can essentially just
7585
04:50:35,920 --> 04:50:39,200
copy this line the line 52 for me now
7586
04:50:39,200 --> 04:50:41,120
and we're going to put it above our
7587
04:50:41,120 --> 04:50:43,360
overlay so right here
7588
04:50:43,360 --> 04:50:46,160
what you have to do is simply close it
7589
04:50:46,160 --> 04:50:49,440
down here this is also going to make the
7590
04:50:49,440 --> 04:50:52,240
button for the edit show up only if the
7591
04:50:52,240 --> 04:50:55,600
creator is the currently logged in user
7592
04:50:55,600 --> 04:50:58,798
go back and edit button is gone
7593
04:50:58,798 --> 04:51:00,638
now i'll log out
7594
04:51:00,638 --> 04:51:02,080
go back in
7595
04:51:02,080 --> 04:51:04,480
sign in as john wick
7596
04:51:04,480 --> 04:51:06,400
and let's test it out
7597
04:51:06,400 --> 04:51:08,638
there we go our delete button is here
7598
04:51:08,638 --> 04:51:11,600
and our edit button is here that works
7599
04:51:11,600 --> 04:51:13,120
perfectly
7600
04:51:13,120 --> 04:51:15,360
if i'm not mistaken to make this app
7601
04:51:15,360 --> 04:51:17,760
complete there's only one more step that
7602
04:51:17,760 --> 04:51:18,958
we have to do
7603
04:51:18,958 --> 04:51:21,360
and that is check if the token has
7604
04:51:21,360 --> 04:51:24,000
expired right now we are never checking
7605
04:51:24,000 --> 04:51:26,560
if the token has expired and remember it
7606
04:51:26,560 --> 04:51:28,560
only lasts for an hour
7607
04:51:28,560 --> 04:51:30,400
that means that if the user is logged in
7608
04:51:30,400 --> 04:51:32,240
for more than an hour and he tries to
7609
04:51:32,240 --> 04:51:34,400
make a certain change like create a post
7610
04:51:34,400 --> 04:51:37,280
or delete a post or edit it he won't be
7611
04:51:37,280 --> 04:51:39,760
able to do so in that case we have to
7612
04:51:39,760 --> 04:51:43,040
check it and we have to log him out
7613
04:51:43,040 --> 04:51:44,798
so right now i'm going to open the
7614
04:51:44,798 --> 04:51:47,280
console and the application tab we can
7615
04:51:47,280 --> 04:51:48,958
clearly see that our user is logged in
7616
04:51:48,958 --> 04:51:51,120
right now and if we refresh he's still
7617
04:51:51,120 --> 04:51:52,638
going to be logged in
7618
04:51:52,638 --> 04:51:54,718
i'm not sure if an hour had passed but
7619
04:51:54,718 --> 04:51:56,718
let me try to delete this
7620
04:51:56,718 --> 04:51:59,200
as you saw i just tried to delete a post
7621
04:51:59,200 --> 04:52:01,600
but nothing happened and if we go to the
7622
04:52:01,600 --> 04:52:03,360
console well we don't get a lot of
7623
04:52:03,360 --> 04:52:06,160
information but if we go to our network
7624
04:52:06,160 --> 04:52:08,798
tab and take a look at a request you can
7625
04:52:08,798 --> 04:52:11,120
see it is still pending and we don't
7626
04:52:11,120 --> 04:52:12,480
really have a lot of information on
7627
04:52:12,480 --> 04:52:15,040
what's going on but if we go to the back
7628
04:52:15,040 --> 04:52:17,440
end that could give us more info
7629
04:52:17,440 --> 04:52:19,280
so right now i'm going to open the
7630
04:52:19,280 --> 04:52:22,718
server and take a look
7631
04:52:23,840 --> 04:52:25,280
expired
7632
04:52:25,280 --> 04:52:27,040
that means that we are not allowed to
7633
04:52:27,040 --> 04:52:29,760
like or delete a post there is going to
7634
04:52:29,760 --> 04:52:32,000
be a simple way to solve this and that
7635
04:52:32,000 --> 04:52:34,560
is if token is expired we simply need to
7636
04:52:34,560 --> 04:52:36,080
log the user out
7637
04:52:36,080 --> 04:52:37,760
for that we're going to head to our nav
7638
04:52:37,760 --> 04:52:39,040
bar
7639
04:52:39,040 --> 04:52:41,200
what we have to do is we have to check
7640
04:52:41,200 --> 04:52:43,440
if the token is expired how can we do
7641
04:52:43,440 --> 04:52:45,760
that well we first have to check even if
7642
04:52:45,760 --> 04:52:47,920
the token exists and then if that's the
7643
04:52:47,920 --> 04:52:50,240
case we want to decode it we can decode
7644
04:52:50,240 --> 04:52:53,920
a token like this kant's decoded token
7645
04:52:53,920 --> 04:52:56,718
is equal to the code and then we pass in
7646
04:52:56,718 --> 04:52:57,920
the token
7647
04:52:57,920 --> 04:52:59,600
that the code is the function that we
7648
04:52:59,600 --> 04:53:02,000
need to import we need to import it from
7649
04:53:02,000 --> 04:53:08,080
jwt the code so import the code from jwt
7650
04:53:08,080 --> 04:53:10,400
the code that way it's going to give us
7651
04:53:10,400 --> 04:53:12,240
information on when this token is
7652
04:53:12,240 --> 04:53:15,440
expiring finally we need a simple if
7653
04:53:15,440 --> 04:53:18,400
check that is going to be if decoded
7654
04:53:18,400 --> 04:53:19,680
token
7655
04:53:19,680 --> 04:53:22,638
exp that's the expiry multiplied by a
7656
04:53:22,638 --> 04:53:27,040
thousand is lower than new date and then
7657
04:53:27,040 --> 04:53:30,000
finally dot get time so we want to get
7658
04:53:30,000 --> 04:53:31,280
the current time
7659
04:53:31,280 --> 04:53:33,360
if that is the case we want to log out
7660
04:53:33,360 --> 04:53:35,840
so if the decoded token that expiry
7661
04:53:35,840 --> 04:53:37,920
multiplied by thousand this is going to
7662
04:53:37,920 --> 04:53:40,400
be a certain value in milliseconds if
7663
04:53:40,400 --> 04:53:41,958
that is lower than
7664
04:53:41,958 --> 04:53:43,840
newdate.gettime so this is the current
7665
04:53:43,840 --> 04:53:46,320
time in milliseconds in that case we
7666
04:53:46,320 --> 04:53:49,440
simply want to call the logout action
7667
04:53:49,440 --> 04:53:51,600
and that is in this case this function
7668
04:53:51,600 --> 04:53:53,120
here which is going to dispatch the
7669
04:53:53,120 --> 04:53:56,400
logout and then push us back
7670
04:53:56,400 --> 04:53:58,718
if we save that that should be it
7671
04:53:58,718 --> 04:54:01,200
now considering our john wick was
7672
04:54:01,200 --> 04:54:03,840
expired or his token was as soon as we
7673
04:54:03,840 --> 04:54:07,600
came back to the page we were signed out
7674
04:54:07,600 --> 04:54:09,440
on the signed out users as you can see
7675
04:54:09,440 --> 04:54:11,840
we have nothing we cannot even like them
7676
04:54:11,840 --> 04:54:13,840
and we also don't have a delete or edit
7677
04:54:13,840 --> 04:54:16,320
button now if we go ahead and try to
7678
04:54:16,320 --> 04:54:18,480
sign in as a different user for example
7679
04:54:18,480 --> 04:54:20,240
javascript mastery
7680
04:54:20,240 --> 04:54:22,240
there we go we can like it but we cannot
7681
04:54:22,240 --> 04:54:24,400
delete or edit and let me try to create
7682
04:54:24,400 --> 04:54:26,080
one new post
7683
04:54:26,080 --> 04:54:28,638
let's do something like mern 3 is now
7684
04:54:28,638 --> 04:54:30,320
out you guys have been requesting this
7685
04:54:30,320 --> 04:54:32,798
video for so long and now it's out i'm
7686
04:54:32,798 --> 04:54:33,920
gonna say
7687
04:54:33,920 --> 04:54:37,440
awesome tutorial and tags is gonna be
7688
04:54:37,440 --> 04:54:40,400
auth and all auth something like that
7689
04:54:40,400 --> 04:54:42,480
there we go so let's choose an image we
7690
04:54:42,480 --> 04:54:45,520
can also choose fireworks and submit it
7691
04:54:45,520 --> 04:54:47,440
there we go as you can see we can edit
7692
04:54:47,440 --> 04:54:49,920
it we can delete it we can like it yeah
7693
04:54:49,920 --> 04:54:51,840
you can like your own post nobody's
7694
04:54:51,840 --> 04:54:54,160
saying you should do that but you can
7695
04:54:54,160 --> 04:54:56,878
and finally if we log out now and sign
7696
04:54:56,878 --> 04:54:58,638
in with a different person like john
7697
04:54:58,638 --> 04:55:00,718
wick
7698
04:55:00,718 --> 04:55:03,360
there we go we have this different pose
7699
04:55:03,360 --> 04:55:05,280
that can be only liked but cannot be
7700
04:55:05,280 --> 04:55:08,560
deleted or edited
7701
04:55:09,680 --> 04:55:12,000
a smart place to start would be to
7702
04:55:12,000 --> 04:55:14,718
create the pagination component so we
7703
04:55:14,718 --> 04:55:17,440
can go to client we can go to the source
7704
04:55:17,440 --> 04:55:19,840
and components and then right inside of
7705
04:55:19,840 --> 04:55:22,240
here we're going to create a new file
7706
04:55:22,240 --> 04:55:25,638
called pagination.jsx
7707
04:55:26,240 --> 04:55:28,320
inside of here we're going to import
7708
04:55:28,320 --> 04:55:31,600
react from react as we always do
7709
04:55:31,600 --> 04:55:33,840
we are also going to import some of the
7710
04:55:33,840 --> 04:55:37,040
components from material ui lab
7711
04:55:37,040 --> 04:55:39,600
we can say import and that's going to be
7712
04:55:39,600 --> 04:55:42,878
pagination as well as the pagination
7713
04:55:42,878 --> 04:55:44,000
item
7714
04:55:44,000 --> 04:55:46,240
and this is not being imported from
7715
04:55:46,240 --> 04:55:48,798
material ui core because these are still
7716
04:55:48,798 --> 04:55:51,200
experimental features so we're importing
7717
04:55:51,200 --> 04:55:54,480
them from the material ui lab library of
7718
04:55:54,480 --> 04:55:56,160
course we're going to have some styles
7719
04:55:56,160 --> 04:55:59,280
so you can say import styles from dot
7720
04:55:59,280 --> 04:56:01,280
slash styles
7721
04:56:01,280 --> 04:56:02,878
and of course you could have done this
7722
04:56:02,878 --> 04:56:04,480
in a folder but in this case i'm just
7723
04:56:04,480 --> 04:56:07,200
gonna do them right in here and that's
7724
04:56:07,200 --> 04:56:09,840
going to be styles.js
7725
04:56:09,840 --> 04:56:11,840
the styles for the pagination component
7726
04:56:11,840 --> 04:56:14,080
are going to be incredibly simple we can
7727
04:56:14,080 --> 04:56:15,680
say import
7728
04:56:15,680 --> 04:56:17,520
make styles
7729
04:56:17,520 --> 04:56:20,878
from add material ui forward slash core
7730
04:56:20,878 --> 04:56:22,480
forward slash styles
7731
04:56:22,480 --> 04:56:24,480
this is how we usually do styles in
7732
04:56:24,480 --> 04:56:26,080
material ui
7733
04:56:26,080 --> 04:56:29,040
finally you can say export default
7734
04:56:29,040 --> 04:56:30,560
make styles
7735
04:56:30,560 --> 04:56:32,240
and then in there we call that as a
7736
04:56:32,240 --> 04:56:34,638
function and provided a callback
7737
04:56:34,638 --> 04:56:36,638
function that instantly returns an
7738
04:56:36,638 --> 04:56:39,280
object like this it is a bit of a weird
7739
04:56:39,280 --> 04:56:41,680
syntax but we have to do what we have to
7740
04:56:41,680 --> 04:56:42,878
do
7741
04:56:42,878 --> 04:56:44,798
inside of there we'll have only one
7742
04:56:44,798 --> 04:56:46,480
class called ul
7743
04:56:46,480 --> 04:56:48,320
and then it's going to have the property
7744
04:56:48,320 --> 04:56:51,680
of justify content is equal to space
7745
04:56:51,680 --> 04:56:52,878
round
7746
04:56:52,878 --> 04:56:54,638
great that's going to be it for the
7747
04:56:54,638 --> 04:56:56,400
styles now we can go back to the
7748
04:56:56,400 --> 04:56:58,400
pagination component and continue
7749
04:56:58,400 --> 04:57:01,120
working on the jsx our pagination is
7750
04:57:01,120 --> 04:57:03,600
going to be a react component called
7751
04:57:03,600 --> 04:57:05,040
paginate
7752
04:57:05,040 --> 04:57:06,638
and it's going to be of course a
7753
04:57:06,638 --> 04:57:08,560
functional component
7754
04:57:08,560 --> 04:57:10,160
inside of there we're going to use our
7755
04:57:10,160 --> 04:57:12,878
classes by saying cons classes is equal
7756
04:57:12,878 --> 04:57:16,000
to use styles and then we call that as a
7757
04:57:16,000 --> 04:57:17,040
function
7758
04:57:17,040 --> 04:57:18,798
and at the top this should have been
7759
04:57:18,798 --> 04:57:21,440
used styles and not styles
7760
04:57:21,440 --> 04:57:24,320
great so now we have the classes now
7761
04:57:24,320 --> 04:57:26,240
what are we going to return well the
7762
04:57:26,240 --> 04:57:28,638
return is going to be the pagination
7763
04:57:28,638 --> 04:57:30,878
component that we are importing right at
7764
04:57:30,878 --> 04:57:34,000
the top that pagination is going to be a
7765
04:57:34,000 --> 04:57:36,480
self-closing tag but it's going to have
7766
04:57:36,480 --> 04:57:39,040
a few props the first prop is going to
7767
04:57:39,040 --> 04:57:40,638
be classes
7768
04:57:40,638 --> 04:57:43,600
and classes is equal to object and then
7769
04:57:43,600 --> 04:57:47,280
inside of there ul is equal to classes
7770
04:57:47,280 --> 04:57:49,120
dot ul
7771
04:57:49,120 --> 04:57:51,520
moving further we're gonna have a count
7772
04:57:51,520 --> 04:57:53,760
this is the number of pages in this case
7773
04:57:53,760 --> 04:57:56,400
we can make count equal to five this is
7774
04:57:56,400 --> 04:57:58,400
just a static amount but we'll have to
7775
04:57:58,400 --> 04:58:00,638
dynamically fetch the number of pages
7776
04:58:00,638 --> 04:58:02,480
depending on the number of posts we
7777
04:58:02,480 --> 04:58:04,638
currently have
7778
04:58:04,638 --> 04:58:07,040
then we have to have the current page in
7779
04:58:07,040 --> 04:58:09,520
this case let's only say something like
7780
04:58:09,520 --> 04:58:11,760
one of course this is later on going to
7781
04:58:11,760 --> 04:58:14,958
be dynamic as well then we're going to
7782
04:58:14,958 --> 04:58:16,718
have a variant which is going to be
7783
04:58:16,718 --> 04:58:19,680
simply a string of outlined we're also
7784
04:58:19,680 --> 04:58:21,360
going to have a color which is going to
7785
04:58:21,360 --> 04:58:23,600
be equal to primary and we're going to
7786
04:58:23,600 --> 04:58:26,560
have a render item property which is
7787
04:58:26,560 --> 04:58:28,878
going to be a dynamic block of code with
7788
04:58:28,878 --> 04:58:31,920
a callback function inside of there
7789
04:58:31,920 --> 04:58:33,760
our callback function is going to take
7790
04:58:33,760 --> 04:58:36,480
an item as a prop and we're going to
7791
04:58:36,480 --> 04:58:38,240
instantly return something so we're not
7792
04:58:38,240 --> 04:58:39,920
going to use curly braces we're going to
7793
04:58:39,920 --> 04:58:43,360
use immediately simple parentheses
7794
04:58:43,360 --> 04:58:45,040
the thing we're going to return is going
7795
04:58:45,040 --> 04:58:47,760
to be a pagination item which is yet
7796
04:58:47,760 --> 04:58:50,400
again a self-closing tag
7797
04:58:50,400 --> 04:58:52,240
inside of there we're first going to
7798
04:58:52,240 --> 04:58:53,200
spread
7799
04:58:53,200 --> 04:58:55,440
the item so we want to pass all of the
7800
04:58:55,440 --> 04:58:57,680
data from the item right inside of the
7801
04:58:57,680 --> 04:58:59,440
pagination item
7802
04:58:59,440 --> 04:59:01,040
then we're going to say that this
7803
04:59:01,040 --> 04:59:03,920
pagination item is going to be a special
7804
04:59:03,920 --> 04:59:06,560
link component that we're importing from
7805
04:59:06,560 --> 04:59:09,520
react router dom so we can say import
7806
04:59:09,520 --> 04:59:10,400
link
7807
04:59:10,400 --> 04:59:12,240
from react
7808
04:59:12,240 --> 04:59:13,440
router
7809
04:59:13,440 --> 04:59:14,240
dom
7810
04:59:14,240 --> 04:59:15,600
great we're going to use this for
7811
04:59:15,600 --> 04:59:16,718
routing
7812
04:59:16,718 --> 04:59:18,638
and then finally we have to see where is
7813
04:59:18,638 --> 04:59:21,360
this pointing to so we can say 2 and
7814
04:59:21,360 --> 04:59:24,798
that 2 is going to be equal to a dynamic
7815
04:59:24,798 --> 04:59:26,958
string of course and then inside of
7816
04:59:26,958 --> 04:59:29,040
there we can say something like forward
7817
04:59:29,040 --> 04:59:32,480
slash posts and then question mark page
7818
04:59:32,480 --> 04:59:35,520
is equal to and then a dynamic page
7819
04:59:35,520 --> 04:59:37,760
which is in this case going to be 1 but
7820
04:59:37,760 --> 04:59:39,760
later on we're going to have a real
7821
04:59:39,760 --> 04:59:41,760
dynamic link
7822
04:59:41,760 --> 04:59:43,680
now if we save that of course we have to
7823
04:59:43,680 --> 04:59:45,520
do export default
7824
04:59:45,520 --> 04:59:46,878
paginate
7825
04:59:46,878 --> 04:59:48,878
and with that said our currently static
7826
04:59:48,878 --> 04:59:51,520
pagination component is now done so we
7827
04:59:51,520 --> 04:59:53,680
can import it somewhere and start using
7828
04:59:53,680 --> 04:59:54,480
it
7829
04:59:54,480 --> 04:59:56,638
we are going to import it inside of our
7830
04:59:56,638 --> 04:59:58,080
home component
7831
04:59:58,080 --> 05:00:00,080
inside of there we are going to import
7832
05:00:00,080 --> 05:00:02,240
pagination from
7833
05:00:02,240 --> 05:00:05,360
dot slash vagination in this case we
7834
05:00:05,360 --> 05:00:06,718
don't have to do pagination forward
7835
05:00:06,718 --> 05:00:08,798
slash pagination because it's not in a
7836
05:00:08,798 --> 05:00:10,560
special folder
7837
05:00:10,560 --> 05:00:12,560
great now that we have it let's see
7838
05:00:12,560 --> 05:00:14,638
where we can actually call it i'm going
7839
05:00:14,638 --> 05:00:17,520
to scroll a bit down and then in here
7840
05:00:17,520 --> 05:00:19,760
just below our form we want to call our
7841
05:00:19,760 --> 05:00:22,160
pagination but we're going to call it
7842
05:00:22,160 --> 05:00:24,718
inside of a paper component
7843
05:00:24,718 --> 05:00:26,240
that paper is going to be coming from
7844
05:00:26,240 --> 05:00:28,878
material ui so just at the top we can
7845
05:00:28,878 --> 05:00:31,920
import that paper paper is just a white
7846
05:00:31,920 --> 05:00:35,120
div that paper is going to have a class
7847
05:00:35,120 --> 05:00:36,000
name
7848
05:00:36,000 --> 05:00:39,040
that is going to be equal to classes dot
7849
05:00:39,040 --> 05:00:40,638
imagination
7850
05:00:40,638 --> 05:00:42,958
it's also going to have the elevation
7851
05:00:42,958 --> 05:00:45,360
equal to 6 which is going to give it a
7852
05:00:45,360 --> 05:00:46,958
nice shadow
7853
05:00:46,958 --> 05:00:49,440
finally inside of there we can call our
7854
05:00:49,440 --> 05:00:52,160
pagination component
7855
05:00:52,160 --> 05:00:55,200
of course it is a self-closing tag
7856
05:00:55,200 --> 05:00:56,958
as you can see currently we don't have
7857
05:00:56,958 --> 05:00:58,638
classes in there so i'm going to delete
7858
05:00:58,638 --> 05:01:00,400
that and we're going to do that later
7859
05:01:00,400 --> 05:01:02,560
because we have to do a refactor of the
7860
05:01:02,560 --> 05:01:04,400
entire home component
7861
05:01:04,400 --> 05:01:06,638
now if i save this and go back
7862
05:01:06,638 --> 05:01:09,120
as you can see just below this form
7863
05:01:09,120 --> 05:01:11,760
doesn't look that pretty but we have our
7864
05:01:11,760 --> 05:01:14,638
pagination component with five pages as
7865
05:01:14,638 --> 05:01:17,600
we specified currently if we click that
7866
05:01:17,600 --> 05:01:19,200
nothing is going to happen because we
7867
05:01:19,200 --> 05:01:21,360
are navigating to forward slash posts
7868
05:01:21,360 --> 05:01:23,840
question mark page is equal to one
7869
05:01:23,840 --> 05:01:26,320
but our current client side navigation
7870
05:01:26,320 --> 05:01:28,400
doesn't know what that is because all
7871
05:01:28,400 --> 05:01:31,760
the posts are simply on localhost 3000
7872
05:01:31,760 --> 05:01:34,320
so we have to make our client side
7873
05:01:34,320 --> 05:01:37,360
navigation and react router work with
7874
05:01:37,360 --> 05:01:40,320
the current new pagination system
7875
05:01:40,320 --> 05:01:42,160
first of all we're going to import one
7876
05:01:42,160 --> 05:01:44,320
more thing here from react router dom
7877
05:01:44,320 --> 05:01:46,400
and that is going to be a redirect
7878
05:01:46,400 --> 05:01:47,920
component
7879
05:01:47,920 --> 05:01:50,480
then we can start working on the jsx
7880
05:01:50,480 --> 05:01:53,200
i'm going to change this from lg large
7881
05:01:53,200 --> 05:01:55,440
to xl which is extra large because we
7882
05:01:55,440 --> 05:01:58,080
want to fit more posts on the screen
7883
05:01:58,080 --> 05:02:00,560
then we have our navbar and then we have
7884
05:02:00,560 --> 05:02:01,840
our switch
7885
05:02:01,840 --> 05:02:04,000
so first of all we're going to have a
7886
05:02:04,000 --> 05:02:06,638
path just slash which usually renders
7887
05:02:06,638 --> 05:02:08,718
first on the page we don't want to
7888
05:02:08,718 --> 05:02:11,120
render a home component there what we
7889
05:02:11,120 --> 05:02:14,000
want to do is call a callback function
7890
05:02:14,000 --> 05:02:16,000
which is simply going to call the
7891
05:02:16,000 --> 05:02:19,120
redirect component and that redirect is
7892
05:02:19,120 --> 05:02:21,120
going to redirect us to
7893
05:02:21,120 --> 05:02:23,200
and that's going to be forward slash
7894
05:02:23,200 --> 05:02:26,480
posts so we only want to see posts if we
7895
05:02:26,480 --> 05:02:28,958
are on this specific path and we're
7896
05:02:28,958 --> 05:02:31,200
going to be immediately redirected if we
7897
05:02:31,200 --> 05:02:32,560
do this
7898
05:02:32,560 --> 05:02:34,320
of course we have to keep in mind that
7899
05:02:34,320 --> 05:02:35,840
if we want to show something on this
7900
05:02:35,840 --> 05:02:38,798
specific route we also need to create it
7901
05:02:38,798 --> 05:02:40,958
so just below the current route i'm
7902
05:02:40,958 --> 05:02:43,040
going to create a one more self-closing
7903
05:02:43,040 --> 05:02:43,920
tag
7904
05:02:43,920 --> 05:02:46,160
this one is going to have the path of
7905
05:02:46,160 --> 05:02:48,320
forward slash posts
7906
05:02:48,320 --> 05:02:49,920
it's also going to have the exact
7907
05:02:49,920 --> 05:02:51,200
parameter
7908
05:02:51,200 --> 05:02:53,520
and it's going to render the component
7909
05:02:53,520 --> 05:02:54,718
called whole
7910
05:02:54,718 --> 05:02:56,080
before we were rendering the home
7911
05:02:56,080 --> 05:02:58,958
component on the just slash path we can
7912
05:02:58,958 --> 05:03:01,520
now copy and paste this home route just
7913
05:03:01,520 --> 05:03:03,920
below because we also want to render the
7914
05:03:03,920 --> 05:03:06,400
home component if we are on the forward
7915
05:03:06,400 --> 05:03:09,680
slash posts forward slash search
7916
05:03:09,680 --> 05:03:11,280
this is going to be rendered when we are
7917
05:03:11,280 --> 05:03:13,440
searching for something and then in here
7918
05:03:13,440 --> 05:03:15,040
we'll be able to provide some more
7919
05:03:15,040 --> 05:03:17,920
parameters like search query and so on
7920
05:03:17,920 --> 05:03:19,520
but for now it's important for us to
7921
05:03:19,520 --> 05:03:21,760
have this right here
7922
05:03:21,760 --> 05:03:23,680
then below that we're going to have one
7923
05:03:23,680 --> 05:03:25,360
more route
7924
05:03:25,360 --> 05:03:27,120
that route is also a self-closing
7925
05:03:27,120 --> 05:03:29,680
component that has the path which is
7926
05:03:29,680 --> 05:03:32,560
equal to forward slash posts and then
7927
05:03:32,560 --> 05:03:36,718
finally forward slash column id
7928
05:03:36,718 --> 05:03:39,120
this is going to be the post details
7929
05:03:39,120 --> 05:03:42,240
path and this column id means that this
7930
05:03:42,240 --> 05:03:45,200
id is going to be dynamic finally we're
7931
05:03:45,200 --> 05:03:48,958
going to render a component called
7932
05:03:48,958 --> 05:03:51,200
post details
7933
05:03:51,200 --> 05:03:53,200
of course we haven't yet created or
7934
05:03:53,200 --> 05:03:55,520
imported postdetails component so let's
7935
05:03:55,520 --> 05:03:57,440
do that right now
7936
05:03:57,440 --> 05:03:59,520
inside of our components i'll create a
7937
05:03:59,520 --> 05:04:02,080
new folder called post
7938
05:04:02,080 --> 05:04:03,440
details
7939
05:04:03,440 --> 05:04:05,840
inside of there we can create a new file
7940
05:04:05,840 --> 05:04:10,240
called post details dot jsx and inside
7941
05:04:10,240 --> 05:04:12,320
of there we can create a new react
7942
05:04:12,320 --> 05:04:15,280
component we can use the raf ce this is
7943
05:04:15,280 --> 05:04:18,080
the extension for es7 react redux
7944
05:04:18,080 --> 05:04:20,320
snippets so i'm going to use that which
7945
05:04:20,320 --> 05:04:22,240
is simply going to generate a react
7946
05:04:22,240 --> 05:04:24,240
component for us but of course you can
7947
05:04:24,240 --> 05:04:26,878
write this by hand if you want to
7948
05:04:26,878 --> 05:04:28,958
for now in there we can simply put a
7949
05:04:28,958 --> 05:04:31,040
konza log that's going to say something
7950
05:04:31,040 --> 05:04:32,400
like post
7951
05:04:32,400 --> 05:04:33,760
details
7952
05:04:33,760 --> 05:04:36,240
and we can also just render a string of
7953
05:04:36,240 --> 05:04:38,958
post details in the jsx just so we can
7954
05:04:38,958 --> 05:04:41,520
see if our component is showing up
7955
05:04:41,520 --> 05:04:43,760
now let's go back to the app and then in
7956
05:04:43,760 --> 05:04:45,840
here let's import that pose details
7957
05:04:45,840 --> 05:04:48,718
component by saying import post details
7958
05:04:48,718 --> 05:04:50,798
from post details
7959
05:04:50,798 --> 05:04:52,240
i just want to point your attention to
7960
05:04:52,240 --> 05:04:54,000
this nice tool i've recently started
7961
05:04:54,000 --> 05:04:56,560
using which is called tab 9. you can see
7962
05:04:56,560 --> 05:04:59,520
it's using ai to autofill code and it
7963
05:04:59,520 --> 05:05:01,040
immediately knew that i wanted to
7964
05:05:01,040 --> 05:05:03,360
autofill this exact
7965
05:05:03,360 --> 05:05:05,040
thing which is kinda crazy if you think
7966
05:05:05,040 --> 05:05:06,160
about it
7967
05:05:06,160 --> 05:05:08,320
but with that said we are now creating
7968
05:05:08,320 --> 05:05:11,040
and importing post details and we are
7969
05:05:11,040 --> 05:05:13,280
pointing to it when somebody goes to
7970
05:05:13,280 --> 05:05:16,718
post and then a specific id
7971
05:05:16,718 --> 05:05:19,360
let's see how that looks in action if we
7972
05:05:19,360 --> 05:05:21,360
try to remove the forward slash posts
7973
05:05:21,360 --> 05:05:23,360
and just check the localhost 3000 and
7974
05:05:23,360 --> 05:05:25,520
click enter that's going to point us
7975
05:05:25,520 --> 05:05:27,520
straight back to forward slash posts
7976
05:05:27,520 --> 05:05:29,680
exactly how we want it
7977
05:05:29,680 --> 05:05:31,840
then we also added the forward slash
7978
05:05:31,840 --> 05:05:33,520
search
7979
05:05:33,520 --> 05:05:35,920
that works we are right there and then
7980
05:05:35,920 --> 05:05:39,040
we also added a specific post page post
7981
05:05:39,040 --> 05:05:41,280
details and in there you have to enter
7982
05:05:41,280 --> 05:05:43,360
some id right now it doesn't matter
7983
05:05:43,360 --> 05:05:45,440
which id we enter so let's try with one
7984
05:05:45,440 --> 05:05:47,920
two three and as you can see if i zoom
7985
05:05:47,920 --> 05:05:50,958
it in we have our post details right in
7986
05:05:50,958 --> 05:05:52,000
here
7987
05:05:52,000 --> 05:05:54,480
finally we have our forward slash odd
7988
05:05:54,480 --> 05:05:56,240
page which is going to be our sign in
7989
05:05:56,240 --> 05:05:57,360
form
7990
05:05:57,360 --> 05:05:59,280
if you open that up an alert is going to
7991
05:05:59,280 --> 05:06:01,600
pop up and the reason why is because we
7992
05:06:01,600 --> 05:06:04,878
haven't connected a real google sign in
7993
05:06:04,878 --> 05:06:06,718
to see how you can set up full google
7994
05:06:06,718 --> 05:06:08,878
authentication make sure to watch the
7995
05:06:08,878 --> 05:06:11,040
third part of this series because there
7996
05:06:11,040 --> 05:06:13,680
i teach you how to do exactly that
7997
05:06:13,680 --> 05:06:15,440
for now to get rid of that alert we're
7998
05:06:15,440 --> 05:06:18,160
going to go back to our auth
7999
05:06:18,160 --> 05:06:20,160
which is in components and then auth
8000
05:06:20,160 --> 05:06:22,400
right there we're going to scroll down a
8001
05:06:22,400 --> 05:06:24,878
bit and we can see this alert we're
8002
05:06:24,878 --> 05:06:27,080
simply going to swap that into a
8003
05:06:27,080 --> 05:06:30,000
console.log for now that way the alert
8004
05:06:30,000 --> 05:06:31,920
is not going to pop up if you don't have
8005
05:06:31,920 --> 05:06:35,120
the valid google console connected
8006
05:06:35,120 --> 05:06:36,958
but then again if you want to connect it
8007
05:06:36,958 --> 05:06:38,560
you just have to watch the third part of
8008
05:06:38,560 --> 05:06:40,878
the video and you're gonna have a valid
8009
05:06:40,878 --> 05:06:43,520
id to connect it with
8010
05:06:43,520 --> 05:06:46,080
with that said this is our odd route if
8011
05:06:46,080 --> 05:06:48,560
i enter my email and sign in you can see
8012
05:06:48,560 --> 05:06:51,040
that everything works right but somebody
8013
05:06:51,040 --> 05:06:53,520
can still go to forward slash auth and
8014
05:06:53,520 --> 05:06:55,040
see this page
8015
05:06:55,040 --> 05:06:56,958
that shouldn't be the case if we're
8016
05:06:56,958 --> 05:06:58,878
currently logged in we should only be
8017
05:06:58,878 --> 05:07:01,200
able to see this if we're logged out so
8018
05:07:01,200 --> 05:07:02,560
let's fix that
8019
05:07:02,560 --> 05:07:04,480
we can easily fix that by going to
8020
05:07:04,480 --> 05:07:06,958
app.js and then inside of here instead
8021
05:07:06,958 --> 05:07:09,360
of rendering the auth we first want to
8022
05:07:09,360 --> 05:07:11,520
check if the user is logged in and we
8023
05:07:11,520 --> 05:07:13,200
can do that because we are storing our
8024
05:07:13,200 --> 05:07:15,920
users information in local storage
8025
05:07:15,920 --> 05:07:17,600
so i'm going to switch this from a
8026
05:07:17,600 --> 05:07:20,798
parenthesis to a code block and i'm
8027
05:07:20,798 --> 05:07:23,120
going to put all of this part inside of
8028
05:07:23,120 --> 05:07:25,360
the return of this component
8029
05:07:25,360 --> 05:07:27,040
so just here we're going to have a
8030
05:07:27,040 --> 05:07:28,160
return
8031
05:07:28,160 --> 05:07:29,680
and we're going to put all of the code
8032
05:07:29,680 --> 05:07:30,798
here
8033
05:07:30,798 --> 05:07:32,320
but before we do that we're going to
8034
05:07:32,320 --> 05:07:35,718
have a const user which is equal to
8035
05:07:35,718 --> 05:07:37,280
json.parse
8036
05:07:37,280 --> 05:07:39,920
and we want to parse the local storage
8037
05:07:39,920 --> 05:07:42,638
dot get item and we want to get the
8038
05:07:42,638 --> 05:07:43,760
profile
8039
05:07:43,760 --> 05:07:45,600
that way we'll have the access to the
8040
05:07:45,600 --> 05:07:47,280
user variable
8041
05:07:47,280 --> 05:07:49,200
now depending of the state of the user
8042
05:07:49,200 --> 05:07:50,958
we can render something different here
8043
05:07:50,958 --> 05:07:52,480
instead of the odd
8044
05:07:52,480 --> 05:07:55,360
so we can have a callback function
8045
05:07:55,360 --> 05:07:57,680
and then there we can say if
8046
05:07:57,680 --> 05:07:59,760
no user
8047
05:07:59,760 --> 05:08:02,000
then we want to render the auth
8048
05:08:02,000 --> 05:08:03,920
component that makes sense right we want
8049
05:08:03,920 --> 05:08:05,840
to render the odd if the user isn't
8050
05:08:05,840 --> 05:08:06,878
logged in
8051
05:08:06,878 --> 05:08:08,878
but if they are we simply want to
8052
05:08:08,878 --> 05:08:12,798
redirect the user so redirect that is a
8053
05:08:12,798 --> 05:08:14,878
self-closing component that's going to
8054
05:08:14,878 --> 05:08:18,878
point to forward slash posts
8055
05:08:18,878 --> 05:08:21,440
just like this let's see if i have that
8056
05:08:21,440 --> 05:08:22,560
correctly
8057
05:08:22,560 --> 05:08:25,120
that looks fine to me
8058
05:08:25,120 --> 05:08:27,040
so now if you go back
8059
05:08:27,040 --> 05:08:30,160
and if i try going to forward slash auth
8060
05:08:30,160 --> 05:08:32,160
it's simply going to redirect me back to
8061
05:08:32,160 --> 05:08:33,120
posts
8062
05:08:33,120 --> 05:08:34,160
great
8063
05:08:34,160 --> 05:08:36,638
that was our refactor of the app.js
8064
05:08:36,638 --> 05:08:38,798
component
8065
05:08:38,798 --> 05:08:40,798
now we can start the refactor of the
8066
05:08:40,798 --> 05:08:42,400
home component so that we can
8067
05:08:42,400 --> 05:08:45,200
accommodate it to use the pagination
8068
05:08:45,200 --> 05:08:47,360
let's navigate to the home component and
8069
05:08:47,360 --> 05:08:49,200
let's see what changes do we have to
8070
05:08:49,200 --> 05:08:51,200
make there are gonna be quite a few
8071
05:08:51,200 --> 05:08:53,120
because a home component is going to be
8072
05:08:53,120 --> 05:08:54,958
a really important component that's
8073
05:08:54,958 --> 05:08:56,400
going to handle the logic for the
8074
05:08:56,400 --> 05:08:59,600
pagination and some other stuff
8075
05:08:59,600 --> 05:09:02,160
so let's make our life easier by first
8076
05:09:02,160 --> 05:09:03,840
importing all the things we're going to
8077
05:09:03,840 --> 05:09:04,878
use
8078
05:09:04,878 --> 05:09:06,718
we're going to import a few things from
8079
05:09:06,718 --> 05:09:08,878
material ui and those are going to be
8080
05:09:08,878 --> 05:09:10,718
the app bar
8081
05:09:10,718 --> 05:09:13,600
the text field we're gonna also use a
8082
05:09:13,600 --> 05:09:16,160
button and finally a paper which we
8083
05:09:16,160 --> 05:09:18,160
already have right there
8084
05:09:18,160 --> 05:09:19,760
then we're going to import some things
8085
05:09:19,760 --> 05:09:21,680
from react router dom
8086
05:09:21,680 --> 05:09:24,240
these things are going to be called use
8087
05:09:24,240 --> 05:09:28,320
history and also use location
8088
05:09:28,320 --> 05:09:30,000
we're going to use that so that we know
8089
05:09:30,000 --> 05:09:32,718
on which page are we currently and we're
8090
05:09:32,718 --> 05:09:34,160
going to use the history so that we can
8091
05:09:34,160 --> 05:09:36,958
re-navigate to certain pages and search
8092
05:09:36,958 --> 05:09:37,920
terms
8093
05:09:37,920 --> 05:09:40,400
that's going to be imported from and
8094
05:09:40,400 --> 05:09:44,560
that is going to be react router dom
8095
05:09:44,560 --> 05:09:46,798
inside of here we're going to also use
8096
05:09:46,798 --> 05:09:48,638
something known as a chip
8097
05:09:48,638 --> 05:09:50,798
input and we're going to import the chip
8098
05:09:50,798 --> 05:09:53,040
input from and that is going to be
8099
05:09:53,040 --> 05:09:57,200
material ui chip input chip input is a
8100
05:09:57,200 --> 05:09:59,680
normal input but it works great for tags
8101
05:09:59,680 --> 05:10:01,600
let me show you what i mean
8102
05:10:01,600 --> 05:10:03,680
right here on the top right i can search
8103
05:10:03,680 --> 05:10:06,560
for tags let's do europe and you can see
8104
05:10:06,560 --> 05:10:08,400
this is not a general input this is a
8105
05:10:08,400 --> 05:10:10,638
chip input so it turns it into this
8106
05:10:10,638 --> 05:10:12,718
spill-like structure we can also search
8107
05:10:12,718 --> 05:10:16,400
for asia we can search for usa and all
8108
05:10:16,400 --> 05:10:18,718
of these are going to be in so called
8109
05:10:18,718 --> 05:10:20,878
chips so that is what we'll be using for
8110
05:10:20,878 --> 05:10:24,400
our tags we also have to set up our url
8111
05:10:24,400 --> 05:10:26,320
search params we're going to use that to
8112
05:10:26,320 --> 05:10:28,560
know on which page are we currently on
8113
05:10:28,560 --> 05:10:30,798
and what search term are we looking for
8114
05:10:30,798 --> 05:10:33,600
to do that react router dom says to use
8115
05:10:33,600 --> 05:10:37,280
a function and call it use query
8116
05:10:37,280 --> 05:10:39,920
that use query is just a normal function
8117
05:10:39,920 --> 05:10:43,600
and in there we are returning new url
8118
05:10:43,600 --> 05:10:45,600
search params
8119
05:10:45,600 --> 05:10:49,040
we call it and pass use location which
8120
05:10:49,040 --> 05:10:52,000
we call again and call the dot search
8121
05:10:52,000 --> 05:10:53,280
after that
8122
05:10:53,280 --> 05:10:55,360
that simply allows us to use it as a
8123
05:10:55,360 --> 05:10:58,240
hook so we can simply say cons query is
8124
05:10:58,240 --> 05:11:01,680
equal to use query as you're used to
8125
05:11:01,680 --> 05:11:03,680
we also have the use history so we can
8126
05:11:03,680 --> 05:11:05,440
say const
8127
05:11:05,440 --> 05:11:09,760
history is equal to use history query is
8128
05:11:09,760 --> 05:11:12,000
where we'll be getting our page info
8129
05:11:12,000 --> 05:11:14,240
from so you can say something like const
8130
05:11:14,240 --> 05:11:18,560
page is equal to query dot get and then
8131
05:11:18,560 --> 05:11:21,200
in here we can say page so this is going
8132
05:11:21,200 --> 05:11:24,000
to read our url and see if we have a
8133
05:11:24,000 --> 05:11:26,480
page parameter in there if so it's going
8134
05:11:26,480 --> 05:11:28,798
to populate this variable so we can say
8135
05:11:28,798 --> 05:11:31,200
that or one so if you don't have the
8136
05:11:31,200 --> 05:11:33,600
page we must be on the first one
8137
05:11:33,600 --> 05:11:35,600
finally we're also going to have a
8138
05:11:35,600 --> 05:11:38,000
search query so we can say cons search
8139
05:11:38,000 --> 05:11:39,120
query
8140
05:11:39,120 --> 05:11:41,840
is going to be equal to query.get
8141
05:11:41,840 --> 05:11:45,280
and that's going to be search query
8142
05:11:45,280 --> 05:11:46,958
great now we have some of the things
8143
05:11:46,958 --> 05:11:49,280
that are necessary and now let's modify
8144
05:11:49,280 --> 05:11:51,120
our jsx
8145
05:11:51,120 --> 05:11:52,480
the first thing that we're going to
8146
05:11:52,480 --> 05:11:54,798
modify is that we're going to have our
8147
05:11:54,798 --> 05:11:57,200
container and that container is going to
8148
05:11:57,200 --> 05:11:59,360
have a max width
8149
05:11:59,360 --> 05:12:01,520
equal to xl
8150
05:12:01,520 --> 05:12:04,000
then we have this grid container and the
8151
05:12:04,000 --> 05:12:05,360
only thing we're going to do with it is
8152
05:12:05,360 --> 05:12:07,520
we're going to give it a class name the
8153
05:12:07,520 --> 05:12:09,280
class name is going to be equal to
8154
05:12:09,280 --> 05:12:10,400
classes
8155
05:12:10,400 --> 05:12:12,080
dot grid
8156
05:12:12,080 --> 05:12:13,360
container
8157
05:12:13,360 --> 05:12:15,360
for now we don't have classes but don't
8158
05:12:15,360 --> 05:12:16,958
worry we're going to import them really
8159
05:12:16,958 --> 05:12:18,160
soon
8160
05:12:18,160 --> 05:12:20,160
then we're going to have our grid item
8161
05:12:20,160 --> 05:12:22,080
and in this case we're going to make it
8162
05:12:22,080 --> 05:12:25,040
a bit more mobile responsive by saying
8163
05:12:25,040 --> 05:12:28,400
take 12 spaces on mobile then take half
8164
05:12:28,400 --> 05:12:31,680
the screen on a bit larger devices and
8165
05:12:31,680 --> 05:12:34,560
finally on medium devices so take three
8166
05:12:34,560 --> 05:12:37,120
quarters of the screen on bigger screens
8167
05:12:37,120 --> 05:12:39,360
we want our posts to take most of the
8168
05:12:39,360 --> 05:12:40,560
screen
8169
05:12:40,560 --> 05:12:42,958
finally on this screen where we have our
8170
05:12:42,958 --> 05:12:45,200
options like the form and the pagination
8171
05:12:45,200 --> 05:12:47,920
we can leave this as 12 then on small
8172
05:12:47,920 --> 05:12:49,600
devices that's gonna take the half the
8173
05:12:49,600 --> 05:12:52,160
screen and on medium devices it's the
8174
05:12:52,160 --> 05:12:54,560
screen is big enough so three spaces are
8175
05:12:54,560 --> 05:12:56,798
going to be just enough
8176
05:12:56,798 --> 05:12:58,480
finally we're going to make this look a
8177
05:12:58,480 --> 05:13:00,480
bit better because now we have the form
8178
05:13:00,480 --> 05:13:02,638
we have the paper pagination and soon
8179
05:13:02,638 --> 05:13:03,920
enough we're going to have the search as
8180
05:13:03,920 --> 05:13:06,560
well so let's add something known as the
8181
05:13:06,560 --> 05:13:08,000
app bar
8182
05:13:08,000 --> 05:13:10,560
app bar is going to have a class name
8183
05:13:10,560 --> 05:13:11,638
equal to
8184
05:13:11,638 --> 05:13:15,520
classes.app bar search it's also going
8185
05:13:15,520 --> 05:13:17,440
to have the position which is going to
8186
05:13:17,440 --> 05:13:19,200
be equal to static
8187
05:13:19,200 --> 05:13:21,120
and it's going to have the caller which
8188
05:13:21,120 --> 05:13:22,840
is going to be equal to
8189
05:13:22,840 --> 05:13:25,920
inherit inside of that app bar we're
8190
05:13:25,920 --> 05:13:28,240
gonna have our search so inside of there
8191
05:13:28,240 --> 05:13:30,320
we can create a text field that's going
8192
05:13:30,320 --> 05:13:32,878
to serve as our search
8193
05:13:32,878 --> 05:13:34,638
the text field is going to be a
8194
05:13:34,638 --> 05:13:37,040
self-closing component and it's going to
8195
05:13:37,040 --> 05:13:39,440
have quite a few properties the first
8196
05:13:39,440 --> 05:13:41,200
one is going to be the name which is
8197
05:13:41,200 --> 05:13:43,280
going to be equal to search
8198
05:13:43,280 --> 05:13:45,280
then it's going to have a variant which
8199
05:13:45,280 --> 05:13:47,840
is going to be equal to outlined
8200
05:13:47,840 --> 05:13:50,480
then it's also going to have a label
8201
05:13:50,480 --> 05:13:52,160
our label is simply going to be
8202
05:13:52,160 --> 05:13:53,760
something like search
8203
05:13:53,760 --> 05:13:55,040
memories
8204
05:13:55,040 --> 05:13:56,878
we have many props for text fields so
8205
05:13:56,878 --> 05:13:58,718
i'm going to put them all in separate
8206
05:13:58,718 --> 05:13:59,920
line
8207
05:13:59,920 --> 05:14:01,760
then we're also going to have a full
8208
05:14:01,760 --> 05:14:02,958
width prop
8209
05:14:02,958 --> 05:14:05,360
we're also going to have the value which
8210
05:14:05,360 --> 05:14:07,520
is going to be equal to a search for now
8211
05:14:07,520 --> 05:14:09,920
we can say something like test
8212
05:14:09,920 --> 05:14:11,440
then we're also going to have the
8213
05:14:11,440 --> 05:14:13,440
onchange property which is going to
8214
05:14:13,440 --> 05:14:15,600
change our value so for now i'm going to
8215
05:14:15,600 --> 05:14:18,638
leave it as an empty function like this
8216
05:14:18,638 --> 05:14:21,440
and that's going to be it so now let's
8217
05:14:21,440 --> 05:14:23,840
add our classes and we'll be able to see
8218
05:14:23,840 --> 05:14:26,480
how this looks like in action
8219
05:14:26,480 --> 05:14:28,160
as you know the procedure for adding
8220
05:14:28,160 --> 05:14:30,878
classes is similar so we can simply say
8221
05:14:30,878 --> 05:14:33,280
import use styles
8222
05:14:33,280 --> 05:14:36,160
from dot slash styles
8223
05:14:36,160 --> 05:14:38,638
that's it and finally we need to create
8224
05:14:38,638 --> 05:14:39,480
the
8225
05:14:39,480 --> 05:14:43,280
styles.js file inside of the home folder
8226
05:14:43,280 --> 05:14:45,680
as always to save your time i'm going to
8227
05:14:45,680 --> 05:14:47,440
simply give you the code give it the
8228
05:14:47,440 --> 05:14:49,600
styles to simply copy and paste them
8229
05:14:49,600 --> 05:14:51,680
it's nothing special adding some border
8230
05:14:51,680 --> 05:14:54,400
radiuses some display flexes padding
8231
05:14:54,400 --> 05:14:56,718
margin and so on nothing scary you
8232
05:14:56,718 --> 05:14:59,040
should worry about but just to save some
8233
05:14:59,040 --> 05:15:01,120
times you'll be able to find all the
8234
05:15:01,120 --> 05:15:03,120
styles i mentioned in this video down in
8235
05:15:03,120 --> 05:15:05,200
the description so just go ahead find
8236
05:15:05,200 --> 05:15:08,798
the link and paste them right in here
8237
05:15:08,798 --> 05:15:10,400
once you're done with that we need to
8238
05:15:10,400 --> 05:15:12,718
call our use styles as a hook so we can
8239
05:15:12,718 --> 05:15:14,480
say cons classes
8240
05:15:14,480 --> 05:15:17,360
is equal to use styles and we call it
8241
05:15:17,360 --> 05:15:18,958
like a function
8242
05:15:18,958 --> 05:15:20,798
now if we save this we shouldn't have
8243
05:15:20,798 --> 05:15:22,638
any errors and we can check it out in
8244
05:15:22,638 --> 05:15:23,840
the browser
8245
05:15:23,840 --> 05:15:26,080
and that already looks so much better
8246
05:15:26,080 --> 05:15:29,120
take a look now on big screens our posts
8247
05:15:29,120 --> 05:15:31,840
are taking more of the width and we have
8248
05:15:31,840 --> 05:15:33,680
our search on the right side which
8249
05:15:33,680 --> 05:15:36,160
already looks good of course we cannot
8250
05:15:36,160 --> 05:15:37,840
currently type in it because we have a
8251
05:15:37,840 --> 05:15:40,320
constant value in react we have to
8252
05:15:40,320 --> 05:15:43,360
manage the fields using state so let's
8253
05:15:43,360 --> 05:15:45,840
create a state field for our search
8254
05:15:45,840 --> 05:15:47,520
memories field
8255
05:15:47,520 --> 05:15:50,080
we can scroll just a bit up and then in
8256
05:15:50,080 --> 05:15:52,240
here we can say const
8257
05:15:52,240 --> 05:15:56,080
search and set search
8258
05:15:56,080 --> 05:15:59,680
is going to be equal to use state and
8259
05:15:59,680 --> 05:16:01,840
then inside of that state the default
8260
05:16:01,840 --> 05:16:04,878
value is going to be an empty string
8261
05:16:04,878 --> 05:16:07,200
now let's use that state variable and
8262
05:16:07,200 --> 05:16:09,600
put it right here instead of this static
8263
05:16:09,600 --> 05:16:12,320
test value and let's do something on
8264
05:16:12,320 --> 05:16:14,400
change so what do we have to do on
8265
05:16:14,400 --> 05:16:16,958
change well we get the event and then
8266
05:16:16,958 --> 05:16:20,878
inside of there we can say set search
8267
05:16:20,878 --> 05:16:24,080
is equal to e dot target that value
8268
05:16:24,080 --> 05:16:25,600
that's going to allow us to change the
8269
05:16:25,600 --> 05:16:27,360
value of the search
8270
05:16:27,360 --> 05:16:29,280
one thing that i often prefer is not
8271
05:16:29,280 --> 05:16:31,520
having the need for a button so we also
8272
05:16:31,520 --> 05:16:33,200
want to make something happen if we
8273
05:16:33,200 --> 05:16:35,200
click the enter key so let me show you
8274
05:16:35,200 --> 05:16:36,798
how we can do that
8275
05:16:36,798 --> 05:16:39,280
we can add just one more thing onto this
8276
05:16:39,280 --> 05:16:41,040
text field which is going to be called
8277
05:16:41,040 --> 05:16:44,160
on key press and then on key press we're
8278
05:16:44,160 --> 05:16:46,878
going to create a function called handle
8279
05:16:46,878 --> 05:16:48,798
key press
8280
05:16:48,798 --> 05:16:50,798
that function is going to be just right
8281
05:16:50,798 --> 05:16:52,878
here below this use effect
8282
05:16:52,878 --> 05:16:55,760
const handle key press it's going to
8283
05:16:55,760 --> 05:16:57,120
accept an event
8284
05:16:57,120 --> 05:16:59,360
that's going to be a key press event and
8285
05:16:59,360 --> 05:17:03,200
we can simply say if e dot key code
8286
05:17:03,200 --> 05:17:06,480
is equal to 13 in that case we want to
8287
05:17:06,480 --> 05:17:08,638
do something we'll want to search for
8288
05:17:08,638 --> 05:17:11,040
the post for now we don't have to logic
8289
05:17:11,040 --> 05:17:13,200
to search for the post but i'm going to
8290
05:17:13,200 --> 05:17:16,000
add the comment search post right here
8291
05:17:16,000 --> 05:17:18,638
so we can add it later on
8292
05:17:18,638 --> 05:17:21,280
key code 13 simply means the enter key
8293
05:17:21,280 --> 05:17:23,840
so to know if the key code is 13 you
8294
05:17:23,840 --> 05:17:25,440
press enter
8295
05:17:25,440 --> 05:17:28,080
great now before we add the search post
8296
05:17:28,080 --> 05:17:31,200
functionality i also want to add the jsx
8297
05:17:31,200 --> 05:17:33,680
for the search by tags
8298
05:17:33,680 --> 05:17:36,400
to do that we're going to use the chip
8299
05:17:36,400 --> 05:17:39,280
input we imported at the top our chip
8300
05:17:39,280 --> 05:17:41,920
input is also going to be a self-closing
8301
05:17:41,920 --> 05:17:43,840
tag and it's going to accept a few
8302
05:17:43,840 --> 05:17:45,280
properties
8303
05:17:45,280 --> 05:17:47,680
first it's going to accept some styles
8304
05:17:47,680 --> 05:17:49,680
you can have an object in there and say
8305
05:17:49,680 --> 05:17:52,240
margin is equal to and then a string of
8306
05:17:52,240 --> 05:17:54,958
10 pixels top and bottom and 0 pixels
8307
05:17:54,958 --> 05:17:56,560
left and right
8308
05:17:56,560 --> 05:17:59,840
we also need a value for our search and
8309
05:17:59,840 --> 05:18:02,638
this value also needs to be dynamic it
8310
05:18:02,638 --> 05:18:05,040
has to be from state so let's scroll to
8311
05:18:05,040 --> 05:18:07,680
the top and let's create a tags state
8312
05:18:07,680 --> 05:18:10,320
field i'm going to simply copy this line
8313
05:18:10,320 --> 05:18:12,320
replace this for tags
8314
05:18:12,320 --> 05:18:14,638
and set tags
8315
05:18:14,638 --> 05:18:16,480
and at the start it's not going to be an
8316
05:18:16,480 --> 05:18:18,480
empty string it's rather going to be an
8317
05:18:18,480 --> 05:18:20,080
empty array because we want to have
8318
05:18:20,080 --> 05:18:21,680
multiple tags
8319
05:18:21,680 --> 05:18:24,480
now scrolling down we can add the value
8320
05:18:24,480 --> 05:18:26,480
of tax
8321
05:18:26,480 --> 05:18:28,560
we're also going to have the on add
8322
05:18:28,560 --> 05:18:32,638
function and the on the lead function
8323
05:18:32,638 --> 05:18:34,000
for now we're going to leave this as
8324
05:18:34,000 --> 05:18:35,600
empty and we're going to populate them
8325
05:18:35,600 --> 05:18:36,560
soon
8326
05:18:36,560 --> 05:18:38,798
we also need to have the label which is
8327
05:18:38,798 --> 05:18:41,280
going to be equal to search
8328
05:18:41,280 --> 05:18:42,638
tags
8329
05:18:42,638 --> 05:18:44,718
and we also need to have a variant which
8330
05:18:44,718 --> 05:18:47,280
is going to be equal to outlined just so
8331
05:18:47,280 --> 05:18:49,680
it matches the text field
8332
05:18:49,680 --> 05:18:51,680
on add we're going to call the function
8333
05:18:51,680 --> 05:18:54,718
called handle add and then on delete
8334
05:18:54,718 --> 05:18:56,400
we're going to call a function called
8335
05:18:56,400 --> 05:18:58,080
handle delete
8336
05:18:58,080 --> 05:18:59,840
and of course we have to create those
8337
05:18:59,840 --> 05:19:03,040
functions so scrolling a bit up right in
8338
05:19:03,040 --> 05:19:04,958
here we're going to create that function
8339
05:19:04,958 --> 05:19:07,360
const handle add
8340
05:19:07,360 --> 05:19:09,760
and it's going to accept a tag as a
8341
05:19:09,760 --> 05:19:11,120
parameter
8342
05:19:11,120 --> 05:19:13,360
finally what we have to do is we have to
8343
05:19:13,360 --> 05:19:16,320
call the set tags state and then inside
8344
05:19:16,320 --> 05:19:18,480
of there we're going to have an array as
8345
05:19:18,480 --> 05:19:19,760
the state
8346
05:19:19,760 --> 05:19:21,680
when you have an array with states you
8347
05:19:21,680 --> 05:19:24,400
first have to spread the previous tags
8348
05:19:24,400 --> 05:19:27,520
and then add the new tag onto it this is
8349
05:19:27,520 --> 05:19:30,080
how we're going to handle the add tag
8350
05:19:30,080 --> 05:19:32,718
and finally let's also handle the delete
8351
05:19:32,718 --> 05:19:34,480
so we can say handle
8352
05:19:34,480 --> 05:19:36,798
delete
8353
05:19:36,798 --> 05:19:39,360
that is also going to accept a tag but
8354
05:19:39,360 --> 05:19:42,638
more specifically tag to delete
8355
05:19:42,638 --> 05:19:44,878
and what we have to do is we also have
8356
05:19:44,878 --> 05:19:46,638
to set tags
8357
05:19:46,638 --> 05:19:48,400
but inside of there we're going to say
8358
05:19:48,400 --> 05:19:50,480
tags dot filter
8359
05:19:50,480 --> 05:19:53,040
where we get one specific tag we can put
8360
05:19:53,040 --> 05:19:54,878
it like this tag
8361
05:19:54,878 --> 05:19:56,958
and we want to filter out the tag to
8362
05:19:56,958 --> 05:19:59,680
delete so we can say if tag is not equal
8363
05:19:59,680 --> 05:20:02,400
to tag to delete we're going to keep all
8364
05:20:02,400 --> 05:20:04,000
the other ones but we're only going to
8365
05:20:04,000 --> 05:20:06,320
delete the tag we want to delete
8366
05:20:06,320 --> 05:20:08,560
now let's add that there we have a
8367
05:20:08,560 --> 05:20:11,760
handle delete and a handle add function
8368
05:20:11,760 --> 05:20:13,760
let's save it and see how does it look
8369
05:20:13,760 --> 05:20:16,000
like as you can see we have search
8370
05:20:16,000 --> 05:20:18,560
memories and we have search tags let's
8371
05:20:18,560 --> 05:20:20,320
try adding test
8372
05:20:20,320 --> 05:20:23,280
a tag appears let's try test one test
8373
05:20:23,280 --> 05:20:25,600
one appears and we can finally test it
8374
05:20:25,600 --> 05:20:28,638
by deleting them which also works final
8375
05:20:28,638 --> 05:20:30,558
thing of course is to add a button
8376
05:20:30,558 --> 05:20:32,240
that's going to do something with those
8377
05:20:32,240 --> 05:20:34,320
inputs a button is not going to be a
8378
05:20:34,320 --> 05:20:36,558
self-closing tag it's going to have the
8379
05:20:36,558 --> 05:20:38,878
on click property
8380
05:20:38,878 --> 05:20:41,680
and on click we want to do something and
8381
05:20:41,680 --> 05:20:44,558
that something is search post search
8382
05:20:44,558 --> 05:20:46,558
post is a function we haven't created
8383
05:20:46,558 --> 05:20:47,520
yet
8384
05:20:47,520 --> 05:20:50,240
so then we can create a class name which
8385
05:20:50,240 --> 05:20:52,558
is going to be equal to classes dot
8386
05:20:52,558 --> 05:20:53,600
search
8387
05:20:53,600 --> 05:20:54,638
button
8388
05:20:54,638 --> 05:20:56,798
and we can also have a caller which is
8389
05:20:56,798 --> 05:20:58,480
equal to primary
8390
05:20:58,480 --> 05:21:00,480
finally inside of the button we're going
8391
05:21:00,480 --> 05:21:02,320
to say search
8392
05:21:02,320 --> 05:21:04,400
now if we save this and take a look it's
8393
05:21:04,400 --> 05:21:06,080
not going to work because this is
8394
05:21:06,080 --> 05:21:08,878
undefined so we have to create that
8395
05:21:08,878 --> 05:21:11,120
search post function
8396
05:21:11,120 --> 05:21:13,280
we can create a search post right here
8397
05:21:13,280 --> 05:21:16,718
below the use effect so const search
8398
05:21:16,718 --> 05:21:19,520
post is equal to a function which
8399
05:21:19,520 --> 05:21:21,920
doesn't accept any parameters
8400
05:21:21,920 --> 05:21:24,000
inside of there we want to see if we
8401
05:21:24,000 --> 05:21:26,958
have a search term so we can say if
8402
05:21:26,958 --> 05:21:28,240
search
8403
05:21:28,240 --> 05:21:30,080
this search maybe is not the best name a
8404
05:21:30,080 --> 05:21:31,520
better name would be something like
8405
05:21:31,520 --> 05:21:33,920
search term or title or something like
8406
05:21:33,920 --> 05:21:36,240
that but in this case it's search and we
8407
05:21:36,240 --> 05:21:38,000
also want to trim it to make sure there
8408
05:21:38,000 --> 05:21:39,920
are no empty spaces
8409
05:21:39,920 --> 05:21:42,160
so if we have a search term then finally
8410
05:21:42,160 --> 05:21:45,280
we want to dispatch some logic to fetch
8411
05:21:45,280 --> 05:21:48,480
our searched post so fetch search
8412
05:21:48,480 --> 05:21:49,520
post
8413
05:21:49,520 --> 05:21:50,718
great
8414
05:21:50,718 --> 05:21:52,240
of course to dispatch this we're going
8415
05:21:52,240 --> 05:21:54,160
to have to use redux and we're going to
8416
05:21:54,160 --> 05:21:56,638
have to modify our database to only
8417
05:21:56,638 --> 05:21:59,120
search for specific posts
8418
05:21:59,120 --> 05:22:02,240
and else if we don't have a search term
8419
05:22:02,240 --> 05:22:03,520
then we're going to simply use the
8420
05:22:03,520 --> 05:22:06,240
history object and call the push method
8421
05:22:06,240 --> 05:22:07,840
to forward slash we just want to
8422
05:22:07,840 --> 05:22:09,840
redirect back because we have searched
8423
05:22:09,840 --> 05:22:11,600
for nothing
8424
05:22:11,600 --> 05:22:13,600
and as you can see here we have this
8425
05:22:13,600 --> 05:22:15,600
comment we also want to call for the
8426
05:22:15,600 --> 05:22:17,920
search post function right inside of
8427
05:22:17,920 --> 05:22:20,240
here if we press the enter key
8428
05:22:20,240 --> 05:22:22,160
now that we have this let's take a look
8429
05:22:22,160 --> 05:22:24,480
in the browser we have our two inputs
8430
05:22:24,480 --> 05:22:26,718
but the search button doesn't look right
8431
05:22:26,718 --> 05:22:28,718
looks like we're missing something the
8432
05:22:28,718 --> 05:22:30,320
thing are missing is going to be the
8433
05:22:30,320 --> 05:22:33,200
variant equal to contained
8434
05:22:33,200 --> 05:22:35,120
let's save it
8435
05:22:35,120 --> 05:22:37,600
and take a look search memories search
8436
05:22:37,600 --> 05:22:39,680
tags and they search button but of
8437
05:22:39,680 --> 05:22:41,520
course if we search something right now
8438
05:22:41,520 --> 05:22:43,680
nothing is going to happen
8439
05:22:43,680 --> 05:22:45,760
but now comes the hard part we have to
8440
05:22:45,760 --> 05:22:48,160
tell the database to only return us the
8441
05:22:48,160 --> 05:22:50,480
posts that match our query to send a
8442
05:22:50,480 --> 05:22:52,718
nice message to the database we can use
8443
05:22:52,718 --> 05:22:55,520
redux so let's first create a redux
8444
05:22:55,520 --> 05:23:00,160
action and a reducer to manage our posts
8445
05:23:00,160 --> 05:23:02,558
this patch is the verb that we use with
8446
05:23:02,558 --> 05:23:05,440
actions so to have something to dispatch
8447
05:23:05,440 --> 05:23:07,440
we need to create an action for
8448
05:23:07,440 --> 05:23:09,680
searching the posts to do that we can
8449
05:23:09,680 --> 05:23:12,958
open up our file explorer go to actions
8450
05:23:12,958 --> 05:23:15,280
posts and then inside of there just
8451
05:23:15,280 --> 05:23:18,080
below our previous get posts we want to
8452
05:23:18,080 --> 05:23:20,480
create an action called get post by
8453
05:23:20,480 --> 05:23:21,440
search
8454
05:23:21,440 --> 05:23:26,400
so we can say export const get posts by
8455
05:23:26,400 --> 05:23:28,400
search
8456
05:23:28,400 --> 05:23:31,280
that is going to be a function and it's
8457
05:23:31,280 --> 05:23:33,280
going to have the syntax as you can see
8458
05:23:33,280 --> 05:23:35,600
as all the previous actions do we're
8459
05:23:35,600 --> 05:23:38,000
using redux tank for asynchronous
8460
05:23:38,000 --> 05:23:39,440
actions
8461
05:23:39,440 --> 05:23:42,400
and again tab 9 does a wonderful job of
8462
05:23:42,400 --> 05:23:44,798
just immediately filling this out for me
8463
05:23:44,798 --> 05:23:46,480
again the link is going to be down in
8464
05:23:46,480 --> 05:23:48,240
the description so you can check out tab
8465
05:23:48,240 --> 05:23:50,080
9 maybe install it as a visual studio
8466
05:23:50,080 --> 05:23:51,600
code extension
8467
05:23:51,600 --> 05:23:53,680
and let's see how do we want to fetch
8468
05:23:53,680 --> 05:23:56,320
the data well as before we're going to
8469
05:23:56,320 --> 05:23:58,718
have a try and catch block
8470
05:23:58,718 --> 05:24:01,520
so let's create it try and catch and
8471
05:24:01,520 --> 05:24:03,680
then in the try we want to do something
8472
05:24:03,680 --> 05:24:06,878
like const where we get the data
8473
05:24:06,878 --> 05:24:09,040
inside of the try block we want to try
8474
05:24:09,040 --> 05:24:11,600
to communicate to our back end so we can
8475
05:24:11,600 --> 05:24:12,958
say cons
8476
05:24:12,958 --> 05:24:15,040
we destructure the data and that is
8477
05:24:15,040 --> 05:24:17,600
going to be equal to await
8478
05:24:17,600 --> 05:24:23,040
api dot fetch posts by search which is a
8479
05:24:23,040 --> 05:24:24,798
function
8480
05:24:24,798 --> 05:24:27,360
of course our get post by search is
8481
05:24:27,360 --> 05:24:30,080
going to accept a search query as a
8482
05:24:30,080 --> 05:24:32,320
parameter and we're going to pass that
8483
05:24:32,320 --> 05:24:36,480
same search query to our api request
8484
05:24:36,480 --> 05:24:38,638
for now we're simply going to console
8485
05:24:38,638 --> 05:24:40,558
log the data
8486
05:24:40,558 --> 05:24:42,320
and we're also going to console log the
8487
05:24:42,320 --> 05:24:45,440
error if there ever is one now we have
8488
05:24:45,440 --> 05:24:48,958
to go to our file explorer api and then
8489
05:24:48,958 --> 05:24:51,920
index.js because we have to create the
8490
05:24:51,920 --> 05:24:54,718
api endpoint called fetch posts by
8491
05:24:54,718 --> 05:24:55,600
search
8492
05:24:55,600 --> 05:24:57,760
so let's go right there
8493
05:24:57,760 --> 05:25:00,080
and then just below fetch posts we can
8494
05:25:00,080 --> 05:25:03,040
create our fetch posts by search
8495
05:25:03,040 --> 05:25:04,798
it's going to be really similar so we
8496
05:25:04,798 --> 05:25:08,958
can say export const fetch posts by
8497
05:25:08,958 --> 05:25:10,320
search
8498
05:25:10,320 --> 05:25:12,638
this time it's going to be a function
8499
05:25:12,638 --> 05:25:15,360
but it's going to accept a parameter of
8500
05:25:15,360 --> 05:25:17,680
search query
8501
05:25:17,680 --> 05:25:20,878
finally we'll be making an api dot get
8502
05:25:20,878 --> 05:25:24,558
request to forward slash posts but we
8503
05:25:24,558 --> 05:25:26,638
have to provide some information about
8504
05:25:26,638 --> 05:25:29,040
our search the endpoint is going to be
8505
05:25:29,040 --> 05:25:32,638
for slash posts for slash search but now
8506
05:25:32,638 --> 05:25:35,200
inside of there we're going to use query
8507
05:25:35,200 --> 05:25:36,558
parameters
8508
05:25:36,558 --> 05:25:39,040
query parameters start with a question
8509
05:25:39,040 --> 05:25:41,520
mark and then you specify a variable
8510
05:25:41,520 --> 05:25:45,440
name search query
8511
05:25:45,440 --> 05:25:47,760
and that search query has to be equal to
8512
05:25:47,760 --> 05:25:49,840
something so for example you can pass a
8513
05:25:49,840 --> 05:25:52,240
static value of test but we're going to
8514
05:25:52,240 --> 05:25:55,520
make a dynamic so i'm going to make this
8515
05:25:55,520 --> 05:25:58,160
into a dynamic template string and then
8516
05:25:58,160 --> 05:25:59,920
we're going to use the dollar sign curly
8517
05:25:59,920 --> 05:26:02,718
braces syntax to make this equal to b
8518
05:26:02,718 --> 05:26:04,558
search query
8519
05:26:04,558 --> 05:26:06,400
dot and then we're going to use the
8520
05:26:06,400 --> 05:26:07,840
search
8521
05:26:07,840 --> 05:26:10,320
and if there is no search then we can
8522
05:26:10,320 --> 05:26:14,718
say or simply do a string of none but we
8523
05:26:14,718 --> 05:26:17,520
also want to add something else so after
8524
05:26:17,520 --> 05:26:20,080
that we can say end and now we want to
8525
05:26:20,080 --> 05:26:22,160
handle our tags it's going to be the
8526
05:26:22,160 --> 05:26:23,760
similar situation
8527
05:26:23,760 --> 05:26:26,160
tags are equal to and then inside of
8528
05:26:26,160 --> 05:26:29,920
there we can say search query dot tags
8529
05:26:29,920 --> 05:26:32,638
now we're sending all of the information
8530
05:26:32,638 --> 05:26:36,000
to our search endpoint
8531
05:26:36,000 --> 05:26:38,718
great now we have our endpoint and we
8532
05:26:38,718 --> 05:26:41,520
also have our action so we can go back
8533
05:26:41,520 --> 05:26:44,240
to where we were to home and then from
8534
05:26:44,240 --> 05:26:47,120
here we can dispatch that action
8535
05:26:47,120 --> 05:26:49,440
we can first import it right here it's
8536
05:26:49,440 --> 05:26:53,200
called get posts by search
8537
05:26:53,200 --> 05:26:55,920
and finally we can use it right here in
8538
05:26:55,920 --> 05:26:58,400
the same way we used get posts that is
8539
05:26:58,400 --> 05:27:00,080
going to be equal to
8540
05:27:00,080 --> 05:27:02,958
this patch we pass the get post by
8541
05:27:02,958 --> 05:27:06,160
search and we call it as a function
8542
05:27:06,160 --> 05:27:08,558
but remember this action is going to
8543
05:27:08,558 --> 05:27:10,480
take in some parameters
8544
05:27:10,480 --> 05:27:12,320
more specifically it is going to take
8545
05:27:12,320 --> 05:27:15,840
the search query object as we specified
8546
05:27:15,840 --> 05:27:18,000
inside of that search query we have to
8547
05:27:18,000 --> 05:27:20,240
provide our search which is coming from
8548
05:27:20,240 --> 05:27:22,320
state as you can see here
8549
05:27:22,320 --> 05:27:25,280
but we also have to provide tags which
8550
05:27:25,280 --> 05:27:28,000
we have to render into a string because
8551
05:27:28,000 --> 05:27:30,638
we cannot pass an array through the url
8552
05:27:30,638 --> 05:27:32,080
parameters
8553
05:27:32,080 --> 05:27:34,480
because of that we're gonna do tags is
8554
05:27:34,480 --> 05:27:35,600
equal to
8555
05:27:35,600 --> 05:27:37,920
tags that join and we're going to join
8556
05:27:37,920 --> 05:27:39,840
them by a comma
8557
05:27:39,840 --> 05:27:42,320
that way if we have an array of
8558
05:27:42,320 --> 05:27:44,320
something like let's say that we have
8559
05:27:44,320 --> 05:27:48,240
tags of europe and let's say usa that is
8560
05:27:48,240 --> 05:27:50,638
going to be equal to a string of europe
8561
05:27:50,638 --> 05:27:52,718
and then comma usa
8562
05:27:52,718 --> 05:27:55,040
if that way we'll be able to more easily
8563
05:27:55,040 --> 05:27:56,958
pass the data from the front end to the
8564
05:27:56,958 --> 05:27:59,360
back end now we have the action we are
8565
05:27:59,360 --> 05:28:01,840
dispatching that action we're making an
8566
05:28:01,840 --> 05:28:03,360
api request
8567
05:28:03,360 --> 05:28:06,320
let's just remember to which endpoint to
8568
05:28:06,320 --> 05:28:09,040
forward slash posts forward slash search
8569
05:28:09,040 --> 05:28:11,520
with some parameters so let's create
8570
05:28:11,520 --> 05:28:14,400
that endpoint on the back-end side
8571
05:28:14,400 --> 05:28:16,558
we can go to our server folder
8572
05:28:16,558 --> 05:28:19,520
and inside of our server we are going to
8573
05:28:19,520 --> 05:28:20,798
go
8574
05:28:20,798 --> 05:28:23,520
into routes we first have to create that
8575
05:28:23,520 --> 05:28:25,280
post route
8576
05:28:25,280 --> 05:28:27,360
we can create it just above our get
8577
05:28:27,360 --> 05:28:30,558
posts and remember it is going to be to
8578
05:28:30,558 --> 05:28:32,558
forward slash search
8579
05:28:32,558 --> 05:28:34,000
before we said it's going to be forward
8580
05:28:34,000 --> 05:28:36,718
slash posts forward slash search but
8581
05:28:36,718 --> 05:28:39,280
remember since we are in the post.js
8582
05:28:39,280 --> 05:28:42,878
file all of these routes begin with post
8583
05:28:42,878 --> 05:28:45,360
even though you cannot see it right here
8584
05:28:45,360 --> 05:28:48,718
okay and on the search route we want to
8585
05:28:48,718 --> 05:28:51,520
call a specific controller so just at
8586
05:28:51,520 --> 05:28:54,798
the top we can import it get posts
8587
05:28:54,798 --> 05:28:55,760
by
8588
05:28:55,760 --> 05:28:56,958
search
8589
05:28:56,958 --> 05:28:59,120
and we're going to call it once we get
8590
05:28:59,120 --> 05:29:01,040
to that route
8591
05:29:01,040 --> 05:29:02,878
and finally we have to create ad
8592
05:29:02,878 --> 05:29:04,798
controller where most of the logic will
8593
05:29:04,798 --> 05:29:07,600
happen so let's go to the controllers
8594
05:29:07,600 --> 05:29:08,638
posts
8595
05:29:08,638 --> 05:29:10,480
and then just below the get posts we
8596
05:29:10,480 --> 05:29:12,320
currently have we're going to create a
8597
05:29:12,320 --> 05:29:17,520
new controller export const get posts by
8598
05:29:17,520 --> 05:29:19,200
search
8599
05:29:19,200 --> 05:29:21,600
that is going to be an async function of
8600
05:29:21,600 --> 05:29:23,440
course we have the rec and the rest in
8601
05:29:23,440 --> 05:29:27,520
there and we also have a try and catch
8602
05:29:27,520 --> 05:29:29,120
block
8603
05:29:29,120 --> 05:29:31,120
now we want to retrieve that data from
8604
05:29:31,120 --> 05:29:33,840
the rec.query remember that i mentioned
8605
05:29:33,840 --> 05:29:37,120
params but params and query are actually
8606
05:29:37,120 --> 05:29:39,280
two different things let me explain what
8607
05:29:39,280 --> 05:29:41,920
i mean when we have a query that usually
8608
05:29:41,920 --> 05:29:44,638
means that the route goes like this
8609
05:29:44,638 --> 05:29:47,760
forward slash posts question mark page
8610
05:29:47,760 --> 05:29:50,080
is equal to one in this case we would
8611
05:29:50,080 --> 05:29:52,958
have a query where page variable is
8612
05:29:52,958 --> 05:29:54,480
equal to one
8613
05:29:54,480 --> 05:29:58,000
but if we have something known as params
8614
05:29:58,000 --> 05:30:00,160
in that case our route would look like
8615
05:30:00,160 --> 05:30:02,798
this forward slash posts and then
8616
05:30:02,798 --> 05:30:05,360
forward slash colon iv
8617
05:30:05,360 --> 05:30:07,680
in in here you would be able to put your
8618
05:30:07,680 --> 05:30:10,480
specific id let's say one two three and
8619
05:30:10,480 --> 05:30:13,680
in that case the populated variable id
8620
05:30:13,680 --> 05:30:16,638
would be equal to one two three
8621
05:30:16,638 --> 05:30:20,320
both ways are fully okay usually we use
8622
05:30:20,320 --> 05:30:22,480
query if you want to well query some
8623
05:30:22,480 --> 05:30:25,200
data like search and we use params if
8624
05:30:25,200 --> 05:30:27,440
you want to get some specific resource
8625
05:30:27,440 --> 05:30:30,000
like posts and then forward slash id of
8626
05:30:30,000 --> 05:30:32,558
the post i hope that makes sense and
8627
05:30:32,558 --> 05:30:34,480
that i explained it in a nice and easy
8628
05:30:34,480 --> 05:30:36,080
to understand way
8629
05:30:36,080 --> 05:30:37,920
if you like the way i explain things
8630
05:30:37,920 --> 05:30:39,360
make sure to leave a like and let me
8631
05:30:39,360 --> 05:30:41,200
know in the comments i really appreciate
8632
05:30:41,200 --> 05:30:43,280
that and if you really want to be
8633
05:30:43,280 --> 05:30:45,520
generous there is a buy me a coffee link
8634
05:30:45,520 --> 05:30:47,440
in the description and that really
8635
05:30:47,440 --> 05:30:49,600
allows me to keep creating more great
8636
05:30:49,600 --> 05:30:51,520
videos for you guys
8637
05:30:51,520 --> 05:30:53,280
with that said let's keep creating the
8638
05:30:53,280 --> 05:30:54,958
logic for the search
8639
05:30:54,958 --> 05:30:56,878
right at the top we're going to get the
8640
05:30:56,878 --> 05:30:59,520
data from the query we just talked about
8641
05:30:59,520 --> 05:31:00,798
const
8642
05:31:00,798 --> 05:31:03,360
we get the search query and we also get
8643
05:31:03,360 --> 05:31:05,120
the tags because these are the two
8644
05:31:05,120 --> 05:31:06,558
things that we passed
8645
05:31:06,558 --> 05:31:09,440
and we get them from reg.query
8646
05:31:09,440 --> 05:31:11,920
now let's convert our title into a
8647
05:31:11,920 --> 05:31:14,480
regular expression by saying cons title
8648
05:31:14,480 --> 05:31:17,520
is equal to new reg
8649
05:31:17,520 --> 05:31:19,920
x and then in there we're going to pass
8650
05:31:19,920 --> 05:31:23,840
our search query and a flag of i
8651
05:31:23,840 --> 05:31:26,400
i stands for ignore case that means that
8652
05:31:26,400 --> 05:31:29,440
if you search for something like test or
8653
05:31:29,440 --> 05:31:30,320
test
8654
05:31:30,320 --> 05:31:33,120
or test it's all going to be the same
8655
05:31:33,120 --> 05:31:35,040
it's not going to matter and we want to
8656
05:31:35,040 --> 05:31:37,040
do that we want to get all the posts
8657
05:31:37,040 --> 05:31:39,440
that match our search term
8658
05:31:39,440 --> 05:31:41,040
we converted it into a regular
8659
05:31:41,040 --> 05:31:42,878
expression in the first place because
8660
05:31:42,878 --> 05:31:45,200
that way it's easier for mongodb and
8661
05:31:45,200 --> 05:31:48,000
mongoose to search the database
8662
05:31:48,000 --> 05:31:50,240
so let's search the database for posts
8663
05:31:50,240 --> 05:31:52,958
by saying const posts is equal to a
8664
05:31:52,958 --> 05:31:54,240
weight
8665
05:31:54,240 --> 05:31:57,040
postmessage dot find
8666
05:31:57,040 --> 05:31:59,440
now in here we have to write that query
8667
05:31:59,440 --> 05:32:01,360
which is a bit more complicated but
8668
05:32:01,360 --> 05:32:02,718
don't worry
8669
05:32:02,718 --> 05:32:05,120
inside of here we can open a block of
8670
05:32:05,120 --> 05:32:06,958
code where we're going to have the
8671
05:32:06,958 --> 05:32:08,878
dollar sign or
8672
05:32:08,878 --> 05:32:11,200
that or stands for either find me the
8673
05:32:11,200 --> 05:32:13,600
title or find me the tags we want to
8674
05:32:13,600 --> 05:32:16,320
find the posts that match either or
8675
05:32:16,320 --> 05:32:18,638
so we can make that into an array and
8676
05:32:18,638 --> 05:32:20,400
then the first thing in there is going
8677
05:32:20,400 --> 05:32:23,360
to be the title so we're matching for
8678
05:32:23,360 --> 05:32:24,480
the title
8679
05:32:24,480 --> 05:32:26,558
the second thing in that array is going
8680
05:32:26,558 --> 05:32:30,080
to be the tags we want to match the tags
8681
05:32:30,080 --> 05:32:32,080
but keep in mind there is an array of
8682
05:32:32,080 --> 05:32:34,480
tags so inside of there we're going to
8683
05:32:34,480 --> 05:32:37,120
open another object i know it's getting
8684
05:32:37,120 --> 05:32:38,638
a bit complicated
8685
05:32:38,638 --> 05:32:40,638
and we're going to say in
8686
05:32:40,638 --> 05:32:43,200
is there a tag in this specific array of
8687
05:32:43,200 --> 05:32:45,760
tags that matches our query
8688
05:32:45,760 --> 05:32:47,840
in there we can say in
8689
05:32:47,840 --> 05:32:49,440
tags.split
8690
05:32:49,440 --> 05:32:51,760
and now we have to split them by a comma
8691
05:32:51,760 --> 05:32:54,080
because remember before we joined it
8692
05:32:54,080 --> 05:32:56,160
just so we can send it over in a nice
8693
05:32:56,160 --> 05:32:57,440
string
8694
05:32:57,440 --> 05:32:59,200
i know it's a bit complicated but let me
8695
05:32:59,200 --> 05:33:01,440
try to explain it one more time in plain
8696
05:33:01,440 --> 05:33:02,798
english
8697
05:33:02,798 --> 05:33:04,000
find me
8698
05:33:04,000 --> 05:33:06,798
all the posts that match
8699
05:33:06,798 --> 05:33:10,320
one of those two criteria the first one
8700
05:33:10,320 --> 05:33:12,798
is the title is the title the same as we
8701
05:33:12,798 --> 05:33:14,718
typed it on the front end and the second
8702
05:33:14,718 --> 05:33:17,760
one is is one of the tags in the array
8703
05:33:17,760 --> 05:33:21,280
of tags equal to our tags if that's the
8704
05:33:21,280 --> 05:33:24,400
case then we want to display those posts
8705
05:33:24,400 --> 05:33:26,558
finally once we have the posts we have
8706
05:33:26,558 --> 05:33:29,440
to say res.json and then in there we'll
8707
05:33:29,440 --> 05:33:32,080
have an object where data is going to be
8708
05:33:32,080 --> 05:33:35,040
equal to posts and we're sending that
8709
05:33:35,040 --> 05:33:37,360
back to the front end
8710
05:33:37,360 --> 05:33:39,440
finally if we have an error we can
8711
05:33:39,440 --> 05:33:41,120
simply return it to the front end by
8712
05:33:41,120 --> 05:33:43,280
saying press that status that's going to
8713
05:33:43,280 --> 05:33:44,798
be a 404
8714
05:33:44,798 --> 05:33:46,160
dot json
8715
05:33:46,160 --> 05:33:48,400
and there we're going to have an object
8716
05:33:48,400 --> 05:33:51,760
that has a message equal to error dot
8717
05:33:51,760 --> 05:33:53,120
message
8718
05:33:53,120 --> 05:33:54,878
so now let's go back to our action
8719
05:33:54,878 --> 05:33:57,040
creator and check if we're receiving the
8720
05:33:57,040 --> 05:33:59,200
data
8721
05:33:59,200 --> 05:34:01,520
that's going to be right back to our
8722
05:34:01,520 --> 05:34:03,840
actions in here
8723
05:34:03,840 --> 05:34:05,440
and we'll have to destructure the data
8724
05:34:05,440 --> 05:34:07,040
two times first time because we're
8725
05:34:07,040 --> 05:34:09,200
making an axios request and the second
8726
05:34:09,200 --> 05:34:11,280
time because we put it in a new object
8727
05:34:11,280 --> 05:34:13,680
where it has the data property just like
8728
05:34:13,680 --> 05:34:14,878
this
8729
05:34:14,878 --> 05:34:16,798
finally we're console logging the data
8730
05:34:16,798 --> 05:34:18,558
right here so let's go back to the
8731
05:34:18,558 --> 05:34:21,120
browser and see if all the logic that we
8732
05:34:21,120 --> 05:34:24,558
wrote right now actually works usually
8733
05:34:24,558 --> 05:34:27,040
it is a miracle when everything works
8734
05:34:27,040 --> 05:34:29,360
first time but who knows let's give it a
8735
05:34:29,360 --> 05:34:30,878
shot
8736
05:34:30,878 --> 05:34:33,360
so i opened up a console i am on my
8737
05:34:33,360 --> 05:34:35,120
localhost three thousand four slash
8738
05:34:35,120 --> 05:34:38,240
posts and i wanna search for the word
8739
05:34:38,240 --> 05:34:40,400
the because we have it a few times in
8740
05:34:40,400 --> 05:34:43,760
the coliseum we also have the acropolis
8741
05:34:43,760 --> 05:34:46,000
so let's see if it finds it i'm going to
8742
05:34:46,000 --> 05:34:47,920
click search
8743
05:34:47,920 --> 05:34:49,040
and
8744
05:34:49,040 --> 05:34:52,320
take a look at that we got back an array
8745
05:34:52,320 --> 05:34:55,120
with four different objects representing
8746
05:34:55,120 --> 05:34:57,600
four different posts the first one has
8747
05:34:57,600 --> 05:35:00,160
the message of the most famous as you
8748
05:35:00,160 --> 05:35:02,638
can see it right there the colosseum
8749
05:35:02,638 --> 05:35:05,040
then we have the acropolis
8750
05:35:05,040 --> 05:35:07,760
taj mahal also has the word the
8751
05:35:07,760 --> 05:35:10,400
and finally the most famous paris museum
8752
05:35:10,400 --> 05:35:13,360
also has the word the so that means that
8753
05:35:13,360 --> 05:35:14,400
it works
8754
05:35:14,400 --> 05:35:16,798
now let's see if our tags work i'm going
8755
05:35:16,798 --> 05:35:19,040
to remove this and then in there i'm
8756
05:35:19,040 --> 05:35:21,120
going to type something like europe
8757
05:35:21,120 --> 05:35:22,718
if i search now
8758
05:35:22,718 --> 05:35:25,280
we should get one more response
8759
05:35:25,280 --> 05:35:29,040
but unfortunately i don't see anything
8760
05:35:29,040 --> 05:35:31,200
so let's go back to our search form and
8761
05:35:31,200 --> 05:35:33,440
see why is our search button not working
8762
05:35:33,440 --> 05:35:35,840
for tags this is going to be a good
8763
05:35:35,840 --> 05:35:37,440
debugging session
8764
05:35:37,440 --> 05:35:39,520
opponent responsible for our search is
8765
05:35:39,520 --> 05:35:42,080
going to be our home and right in here
8766
05:35:42,080 --> 05:35:43,680
we can see that we indeed are
8767
05:35:43,680 --> 05:35:46,160
dispatching the get post by search
8768
05:35:46,160 --> 05:35:48,480
but it's a simple logical error i'm
8769
05:35:48,480 --> 05:35:50,798
simply saying if there is a search term
8770
05:35:50,798 --> 05:35:53,120
then this patch but we have to make it
8771
05:35:53,120 --> 05:35:55,600
if there is a search term or if there
8772
05:35:55,600 --> 05:35:57,280
are tags
8773
05:35:57,280 --> 05:35:59,280
if we just add that that should fix the
8774
05:35:59,280 --> 05:36:02,558
problem search for something like usa
8775
05:36:02,558 --> 05:36:04,240
and click search
8776
05:36:04,240 --> 05:36:06,798
we get three posts back this is going to
8777
05:36:06,798 --> 05:36:09,520
be the statue of liberty in new york
8778
05:36:09,520 --> 05:36:11,840
then we have the grand canyon and
8779
05:36:11,840 --> 05:36:14,080
finally we have niagara falls which is
8780
05:36:14,080 --> 05:36:17,120
bordering the usa and canada
8781
05:36:17,120 --> 05:36:19,440
if that means that our tag completely
8782
05:36:19,440 --> 05:36:20,798
works we're seeing the data in the
8783
05:36:20,798 --> 05:36:23,360
console but now let's make it display
8784
05:36:23,360 --> 05:36:26,000
that data and change the url on the
8785
05:36:26,000 --> 05:36:27,920
client side
8786
05:36:27,920 --> 05:36:30,480
doing that won't be that hard because we
8787
05:36:30,480 --> 05:36:33,440
already set up most the logic
8788
05:36:33,440 --> 05:36:35,680
to finalize it you can go to the
8789
05:36:35,680 --> 05:36:37,760
components and then to our home
8790
05:36:37,760 --> 05:36:39,200
component
8791
05:36:39,200 --> 05:36:42,160
inside of here after the dispatch get
8792
05:36:42,160 --> 05:36:44,958
posed by search we only want to do one
8793
05:36:44,958 --> 05:36:46,080
more thing
8794
05:36:46,080 --> 05:36:49,120
and that is to use the history dot push
8795
05:36:49,120 --> 05:36:51,920
method to push our website to a specific
8796
05:36:51,920 --> 05:36:53,040
url
8797
05:36:53,040 --> 05:36:55,360
more specifically we want to go to
8798
05:36:55,360 --> 05:36:58,878
forward slash posts for slash search
8799
05:36:58,878 --> 05:37:01,600
question mark remember question mark is
8800
05:37:01,600 --> 05:37:04,558
for the search query parameters we can
8801
05:37:04,558 --> 05:37:06,638
say search query
8802
05:37:06,638 --> 05:37:09,600
is going to be equal to search right the
8803
05:37:09,600 --> 05:37:11,280
search is something that's coming from
8804
05:37:11,280 --> 05:37:12,480
the state
8805
05:37:12,480 --> 05:37:13,840
or
8806
05:37:13,840 --> 05:37:16,320
none we can use a string of none if
8807
05:37:16,320 --> 05:37:18,480
there is no search and then finally
8808
05:37:18,480 --> 05:37:21,280
we're going to use the and symbol
8809
05:37:21,280 --> 05:37:22,878
to set the tags
8810
05:37:22,878 --> 05:37:24,958
tags are going to be equal to
8811
05:37:24,958 --> 05:37:27,360
tags dot join
8812
05:37:27,360 --> 05:37:29,440
and we're going to join the tags by
8813
05:37:29,440 --> 05:37:32,080
using a comma remember we had to split
8814
05:37:32,080 --> 05:37:34,000
them and then join them to use them
8815
05:37:34,000 --> 05:37:35,040
again
8816
05:37:35,040 --> 05:37:37,520
great this seems okay to me so let's see
8817
05:37:37,520 --> 05:37:39,600
if we are being pushed to that specific
8818
05:37:39,600 --> 05:37:40,798
url
8819
05:37:40,798 --> 05:37:43,120
if i go in here and use the as an
8820
05:37:43,120 --> 05:37:46,000
example as well as the europe as a tag
8821
05:37:46,000 --> 05:37:48,000
let's see what are we going to be
8822
05:37:48,000 --> 05:37:50,000
pointed to
8823
05:37:50,000 --> 05:37:53,200
as you can see our url changed
8824
05:37:53,200 --> 05:37:55,600
search query is now equal to the and
8825
05:37:55,600 --> 05:37:57,760
tags is equal to europe
8826
05:37:57,760 --> 05:37:59,520
we're going to use that to form our
8827
05:37:59,520 --> 05:38:00,878
posts
8828
05:38:00,878 --> 05:38:03,440
but why does this url on the client side
8829
05:38:03,440 --> 05:38:05,760
even matter we formed it for the backend
8830
05:38:05,760 --> 05:38:08,558
okay but why do we need on the front end
8831
05:38:08,558 --> 05:38:10,000
well let's say that you search for some
8832
05:38:10,000 --> 05:38:13,360
memories and you want to send to your
8833
05:38:13,360 --> 05:38:15,920
friend only the specific search term
8834
05:38:15,920 --> 05:38:18,480
with the terms and the tags already put
8835
05:38:18,480 --> 05:38:19,280
there
8836
05:38:19,280 --> 05:38:21,920
that way you can simply copy this paste
8837
05:38:21,920 --> 05:38:23,680
it over and he's going to get the
8838
05:38:23,680 --> 05:38:26,240
relevant information that's why we need
8839
05:38:26,240 --> 05:38:28,240
client-side routing
8840
05:38:28,240 --> 05:38:31,200
okay so now let's display those posts
8841
05:38:31,200 --> 05:38:32,958
to display the posts we're going to go
8842
05:38:32,958 --> 05:38:35,600
back to actions and we have them right
8843
05:38:35,600 --> 05:38:38,400
in here so we have to do a similar thing
8844
05:38:38,400 --> 05:38:40,558
to what we are doing right here in get
8845
05:38:40,558 --> 05:38:43,440
posts i'm going to copy this
8846
05:38:43,440 --> 05:38:45,680
and we're going to do it in the same way
8847
05:38:45,680 --> 05:38:48,160
but we're going to rename our dispatch
8848
05:38:48,160 --> 05:38:49,600
action type
8849
05:38:49,600 --> 05:38:53,120
so let's create a new one by going to
8850
05:38:53,120 --> 05:38:55,920
constants action types i'm going to copy
8851
05:38:55,920 --> 05:38:58,000
this line and we're going to change it
8852
05:38:58,000 --> 05:39:01,920
just a bit to something like fetch by
8853
05:39:01,920 --> 05:39:03,040
search
8854
05:39:03,040 --> 05:39:05,600
great so now we have one more constant
8855
05:39:05,600 --> 05:39:07,440
we're going to go back in here i'm going
8856
05:39:07,440 --> 05:39:11,120
to import it fetch by search
8857
05:39:11,120 --> 05:39:13,680
and we're going to use it right inside
8858
05:39:13,680 --> 05:39:14,958
of here
8859
05:39:14,958 --> 05:39:17,680
and again we're sending the data to our
8860
05:39:17,680 --> 05:39:19,120
reducers
8861
05:39:19,120 --> 05:39:21,600
so let's go to our reducers
8862
05:39:21,600 --> 05:39:24,080
posts and then right inside of here
8863
05:39:24,080 --> 05:39:27,040
we're going to add one more case
8864
05:39:27,040 --> 05:39:29,360
that case is going to be
8865
05:39:29,360 --> 05:39:31,520
fetch
8866
05:39:31,520 --> 05:39:32,480
by
8867
05:39:32,480 --> 05:39:33,520
search
8868
05:39:33,520 --> 05:39:35,760
and we can also import it right at the
8869
05:39:35,760 --> 05:39:36,718
top
8870
05:39:36,718 --> 05:39:38,558
now that we have that case of course we
8871
05:39:38,558 --> 05:39:41,280
want to return something if that happens
8872
05:39:41,280 --> 05:39:43,840
so we're going to return a similar thing
8873
05:39:43,840 --> 05:39:45,760
in this case it's just going to be
8874
05:39:45,760 --> 05:39:48,320
action dot payload
8875
05:39:48,320 --> 05:39:49,120
now
8876
05:39:49,120 --> 05:39:51,200
let's see what we're missing if i add
8877
05:39:51,200 --> 05:39:54,160
that there that should be working let's
8878
05:39:54,160 --> 05:39:56,320
go check it out we're back to just
8879
05:39:56,320 --> 05:39:58,558
forward slash posts but i'm gonna search
8880
05:39:58,558 --> 05:40:00,638
for something like asia i'm going to
8881
05:40:00,638 --> 05:40:02,480
click search
8882
05:40:02,480 --> 05:40:04,638
and as you can see
8883
05:40:04,638 --> 05:40:07,040
now we got all the posts that have
8884
05:40:07,040 --> 05:40:09,280
something to do with asia asia right
8885
05:40:09,280 --> 05:40:12,558
there there there and everywhere
8886
05:40:12,558 --> 05:40:15,040
that is great that means that our search
8887
05:40:15,040 --> 05:40:18,000
works we can also try it by the title
8888
05:40:18,000 --> 05:40:20,080
if i click search it's going to take a
8889
05:40:20,080 --> 05:40:22,958
bit and as you can see we have all the
8890
05:40:22,958 --> 05:40:26,400
posts that have the in the title
8891
05:40:26,400 --> 05:40:28,798
that means that our search works
8892
05:40:28,798 --> 05:40:30,400
perfectly
8893
05:40:30,400 --> 05:40:32,558
now that we have that done before moving
8894
05:40:32,558 --> 05:40:34,878
on to pagination and loading states
8895
05:40:34,878 --> 05:40:37,120
let's do something easier like changing
8896
05:40:37,120 --> 05:40:40,558
the background and the logo at the top
8897
05:40:40,558 --> 05:40:42,558
and then inside of there i'm gonna
8898
05:40:42,558 --> 05:40:45,520
remove both of these background colors
8899
05:40:45,520 --> 05:40:48,798
and we're simply going to say background
8900
05:40:48,798 --> 05:40:51,440
color is going to be equal to
8901
05:40:51,440 --> 05:40:56,638
hash symbol and then f7 f8 fc
8902
05:40:56,638 --> 05:40:58,958
this is a whitish color almost pure
8903
05:40:58,958 --> 05:40:59,840
white
8904
05:40:59,840 --> 05:41:02,320
now if we save that and go back
8905
05:41:02,320 --> 05:41:03,920
you can see it looks a bit more
8906
05:41:03,920 --> 05:41:07,280
professional maybe a bit plain right now
8907
05:41:07,280 --> 05:41:09,440
both the background and the card are
8908
05:41:09,440 --> 05:41:11,760
pretty much white so to make some
8909
05:41:11,760 --> 05:41:14,480
separation let's add some elevation to
8910
05:41:14,480 --> 05:41:15,600
the card
8911
05:41:15,600 --> 05:41:18,798
we can do that by going to posts
8912
05:41:18,798 --> 05:41:22,080
post and then finally pose.js
8913
05:41:22,080 --> 05:41:24,400
finding our card component
8914
05:41:24,400 --> 05:41:25,920
and then inside of there we're going to
8915
05:41:25,920 --> 05:41:29,600
say raised and also set the elevation to
8916
05:41:29,600 --> 05:41:31,440
be equal to 6.
8917
05:41:31,440 --> 05:41:33,280
that's going to add this nice shadow
8918
05:41:33,280 --> 05:41:34,638
that you can see at the bottom of the
8919
05:41:34,638 --> 05:41:36,840
card which separates it from the
8920
05:41:36,840 --> 05:41:39,440
background i can also see that we still
8921
05:41:39,440 --> 05:41:41,760
have only two cards per row so let's
8922
05:41:41,760 --> 05:41:43,520
make it like it was at the start of the
8923
05:41:43,520 --> 05:41:44,798
video
8924
05:41:44,798 --> 05:41:47,600
to do that we're going to go to post js
8925
05:41:47,600 --> 05:41:49,760
this time and then inside of here we're
8926
05:41:49,760 --> 05:41:50,878
gonna say
8927
05:41:50,878 --> 05:41:53,680
show one which is twelve spaces on extra
8928
05:41:53,680 --> 05:41:56,638
small devices still show one on small
8929
05:41:56,638 --> 05:41:59,760
devices on medium devices show two per
8930
05:41:59,760 --> 05:42:03,680
row but on large devices show four per
8931
05:42:03,680 --> 05:42:06,080
that's three times four is equal to 12
8932
05:42:06,080 --> 05:42:08,878
and that is 4 per row let's see how does
8933
05:42:08,878 --> 05:42:10,638
it look like
8934
05:42:10,638 --> 05:42:12,878
this is already so much better in my
8935
05:42:12,878 --> 05:42:15,840
opinion everything fits on one screen as
8936
05:42:15,840 --> 05:42:17,360
you can see we currently have no
8937
05:42:17,360 --> 05:42:20,160
pagination so we have quite a long list
8938
05:42:20,160 --> 05:42:21,440
of posts
8939
05:42:21,440 --> 05:42:23,120
now that we've changed the color the
8940
05:42:23,120 --> 05:42:25,200
things that jumps out a bit too much is
8941
05:42:25,200 --> 05:42:27,600
the memories logo at the top so let's
8942
05:42:27,600 --> 05:42:29,520
change that as well
8943
05:42:29,520 --> 05:42:31,200
to change it we're going to go to the
8944
05:42:31,200 --> 05:42:32,320
nav bar
8945
05:42:32,320 --> 05:42:34,000
and right in here instead of this
8946
05:42:34,000 --> 05:42:38,080
memories png we have to add two new ones
8947
05:42:38,080 --> 05:42:40,000
i'm simply going to open the images
8948
05:42:40,000 --> 05:42:42,958
folder and then i'm going to paste it
8949
05:42:42,958 --> 05:42:44,798
the links to download both of these are
8950
05:42:44,798 --> 05:42:46,558
going to be in the description so that
8951
05:42:46,558 --> 05:42:48,400
you can download them as well
8952
05:42:48,400 --> 05:42:51,200
but once you actually do that we can go
8953
05:42:51,200 --> 05:42:53,040
back to our nav bar
8954
05:42:53,040 --> 05:42:55,200
and inside of there we're gonna have
8955
05:42:55,200 --> 05:42:57,440
memories logo
8956
05:42:57,440 --> 05:43:01,200
imported from memories logo and we're
8957
05:43:01,200 --> 05:43:03,760
also going to have memories text
8958
05:43:03,760 --> 05:43:06,638
imported from memories text
8959
05:43:06,638 --> 05:43:08,798
now we can use those two images to make
8960
05:43:08,798 --> 05:43:10,958
it look a bit more professional
8961
05:43:10,958 --> 05:43:13,520
we can do that right in here we are no
8962
05:43:13,520 --> 05:43:15,520
longer going to use this typography so
8963
05:43:15,520 --> 05:43:17,600
we can delete it
8964
05:43:17,600 --> 05:43:20,558
we are going to use two different images
8965
05:43:20,558 --> 05:43:23,120
the first one is going to be the image
8966
05:43:23,120 --> 05:43:25,120
self closing tag
8967
05:43:25,120 --> 05:43:27,440
right inside of there the src of that
8968
05:43:27,440 --> 05:43:30,878
image is going to be memories
8969
05:43:30,878 --> 05:43:31,760
text
8970
05:43:31,760 --> 05:43:32,878
like that
8971
05:43:32,878 --> 05:43:34,958
and it's also going to have the alt tag
8972
05:43:34,958 --> 05:43:36,480
of icon
8973
05:43:36,480 --> 05:43:38,718
it's going to have the alt tag
8974
05:43:38,718 --> 05:43:41,040
of icon and it's going to have the
8975
05:43:41,040 --> 05:43:44,638
height equal to 45 pixels
8976
05:43:44,638 --> 05:43:46,558
the one on the bottom is going to have
8977
05:43:46,558 --> 05:43:49,200
the class name of classes.image it's
8978
05:43:49,200 --> 05:43:51,440
going to have the source of memory's
8979
05:43:51,440 --> 05:43:52,558
logo
8980
05:43:52,558 --> 05:43:54,878
and the height is going to be equal to
8981
05:43:54,878 --> 05:43:56,878
40 pixels
8982
05:43:56,878 --> 05:43:59,120
finally once we have that we want to
8983
05:43:59,120 --> 05:44:01,840
transform this div into a link so i'm
8984
05:44:01,840 --> 05:44:03,520
going to say link there
8985
05:44:03,520 --> 05:44:06,558
more specifically link 2 and then we
8986
05:44:06,558 --> 05:44:08,878
want to point to just slash
8987
05:44:08,878 --> 05:44:11,280
now let's save that and see how does it
8988
05:44:11,280 --> 05:44:13,760
look like in the browser
8989
05:44:13,760 --> 05:44:15,280
there we go
8990
05:44:15,280 --> 05:44:17,760
this custom logo just gives this project
8991
05:44:17,760 --> 05:44:19,680
a bit more character
8992
05:44:19,680 --> 05:44:21,680
i can see that the second image is a bit
8993
05:44:21,680 --> 05:44:24,160
higher up than the first one
8994
05:44:24,160 --> 05:44:27,040
so we can go to the styles and i'm gonna
8995
05:44:27,040 --> 05:44:29,440
leave this style as well you can simply
8996
05:44:29,440 --> 05:44:31,440
override it it's going to be down in the
8997
05:44:31,440 --> 05:44:34,000
description i've made only a few small
8998
05:44:34,000 --> 05:44:36,240
changes since the last video but then
8999
05:44:36,240 --> 05:44:38,240
again make sure to copy and paste it
9000
05:44:38,240 --> 05:44:40,718
just we're on the same page if you go
9001
05:44:40,718 --> 05:44:42,638
back to the browser you'll notice that
9002
05:44:42,638 --> 05:44:44,320
now they are aligned
9003
05:44:44,320 --> 05:44:46,000
now that they've made the application
9004
05:44:46,000 --> 05:44:48,160
look a bit better and now that the
9005
05:44:48,160 --> 05:44:50,718
search is fully functional we can focus
9006
05:44:50,718 --> 05:44:53,520
on pagination and loading states
9007
05:44:53,520 --> 05:44:56,080
currently if i go back to the home there
9008
05:44:56,080 --> 05:44:57,760
won't be any loading it's going to
9009
05:44:57,760 --> 05:45:00,320
happen instantly but take a look we only
9010
05:45:00,320 --> 05:45:03,040
have three posts and now they pop up so
9011
05:45:03,040 --> 05:45:05,440
something doesn't seem right it's always
9012
05:45:05,440 --> 05:45:07,760
good to have proper loading states so
9013
05:45:07,760 --> 05:45:09,360
your user knows that something is
9014
05:45:09,360 --> 05:45:10,718
happening
9015
05:45:10,718 --> 05:45:13,600
so pagination and loading states coming
9016
05:45:13,600 --> 05:45:14,480
up
9017
05:45:14,480 --> 05:45:16,638
we already worked a bit with getting the
9018
05:45:16,638 --> 05:45:19,840
page and the search query from the query
9019
05:45:19,840 --> 05:45:21,120
parameters
9020
05:45:21,120 --> 05:45:24,080
but now let's put that page to use
9021
05:45:24,080 --> 05:45:26,240
we will no longer fetch the post from
9022
05:45:26,240 --> 05:45:28,320
here from the home
9023
05:45:28,320 --> 05:45:30,638
we're going to remove this use effect
9024
05:45:30,638 --> 05:45:32,878
and we're going to pass our page
9025
05:45:32,878 --> 05:45:36,480
straight to our pagination as a prop so
9026
05:45:36,480 --> 05:45:39,280
in here we're gonna say page is equal to
9027
05:45:39,280 --> 05:45:40,400
page
9028
05:45:40,400 --> 05:45:43,120
that way we can go to our pagination and
9029
05:45:43,120 --> 05:45:46,718
we can get it straight from props
9030
05:45:46,718 --> 05:45:49,680
now inside of here we can create a new
9031
05:45:49,680 --> 05:45:52,638
use effect so i'm going to say import
9032
05:45:52,638 --> 05:45:55,440
use effect from react and we're going to
9033
05:45:55,440 --> 05:45:57,760
declare it right in here
9034
05:45:57,760 --> 05:45:59,600
our use effect
9035
05:45:59,600 --> 05:46:01,840
as always it is a function with a
9036
05:46:01,840 --> 05:46:04,638
callback function and has a dependency
9037
05:46:04,638 --> 05:46:05,840
array
9038
05:46:05,840 --> 05:46:08,080
so what do we want to do inside of that
9039
05:46:08,080 --> 05:46:09,440
use effect
9040
05:46:09,440 --> 05:46:12,080
well we want to fetch the posts any time
9041
05:46:12,080 --> 05:46:15,040
that the page changes to fetch the posts
9042
05:46:15,040 --> 05:46:16,638
as always we're going to use our
9043
05:46:16,638 --> 05:46:19,520
dispatch with redux so we need to import
9044
05:46:19,520 --> 05:46:22,320
a few things from react redux
9045
05:46:22,320 --> 05:46:24,160
we can say import
9046
05:46:24,160 --> 05:46:27,200
and that is going to be use dispatch so
9047
05:46:27,200 --> 05:46:30,878
we can dispatch things and also use
9048
05:46:30,878 --> 05:46:33,040
selector so that we can select some
9049
05:46:33,040 --> 05:46:34,638
things from our state
9050
05:46:34,638 --> 05:46:36,878
and this is going to be imported from
9051
05:46:36,878 --> 05:46:39,840
react dash redux
9052
05:46:39,840 --> 05:46:42,798
we also want to import an action called
9053
05:46:42,798 --> 05:46:45,200
get posts so we're going to say import
9054
05:46:45,200 --> 05:46:47,680
get posts and that's going to be coming
9055
05:46:47,680 --> 05:46:49,680
from dot slash
9056
05:46:49,680 --> 05:46:53,040
actions forward slash posts of course to
9057
05:46:53,040 --> 05:46:55,440
get our dispatch function we can say
9058
05:46:55,440 --> 05:46:56,480
const
9059
05:46:56,480 --> 05:47:00,160
this patch is equal to use dispatch and
9060
05:47:00,160 --> 05:47:02,638
then call it as a function
9061
05:47:02,638 --> 05:47:04,958
now instead of this use effect we can
9062
05:47:04,958 --> 05:47:08,480
say run it every time that the page
9063
05:47:08,480 --> 05:47:09,600
changes
9064
05:47:09,600 --> 05:47:12,718
so every time that the page changes if
9065
05:47:12,718 --> 05:47:15,840
there is a page in that case we want to
9066
05:47:15,840 --> 05:47:18,878
dispatch a specific action
9067
05:47:18,878 --> 05:47:22,160
in this case we want to dispatch the get
9068
05:47:22,160 --> 05:47:24,320
post action
9069
05:47:24,320 --> 05:47:26,400
and let's open up that action side by
9070
05:47:26,400 --> 05:47:28,160
side here just so you can see how it
9071
05:47:28,160 --> 05:47:29,280
looks like
9072
05:47:29,280 --> 05:47:32,000
get post you can see it accepts no
9073
05:47:32,000 --> 05:47:34,798
parameters and in its current form it
9074
05:47:34,798 --> 05:47:37,360
fetches all of the posts
9075
05:47:37,360 --> 05:47:40,480
but that's not how we want it to behave
9076
05:47:40,480 --> 05:47:43,120
we want to pass a page to it so that it
9077
05:47:43,120 --> 05:47:45,760
only fetches the posts for that specific
9078
05:47:45,760 --> 05:47:46,718
page
9079
05:47:46,718 --> 05:47:49,200
and that's exactly what we're going to
9080
05:47:49,200 --> 05:47:50,000
do
9081
05:47:50,000 --> 05:47:51,760
right inside of there we're going to
9082
05:47:51,760 --> 05:47:53,920
pass that page in
9083
05:47:53,920 --> 05:47:56,958
now inside of our actions we can accept
9084
05:47:56,958 --> 05:48:00,000
that page as a parameter
9085
05:48:00,000 --> 05:48:02,400
once we have it as a parameter we can
9086
05:48:02,400 --> 05:48:05,520
then pass it over to our api as well so
9087
05:48:05,520 --> 05:48:08,160
inside of here we can pass that page
9088
05:48:08,160 --> 05:48:11,200
of course now we have to go to that api
9089
05:48:11,200 --> 05:48:13,120
and make some use of it
9090
05:48:13,120 --> 05:48:16,160
so let's open it up in full screen
9091
05:48:16,160 --> 05:48:19,120
as you can see fetch posts but now that
9092
05:48:19,120 --> 05:48:22,320
function accepts a page and we can use
9093
05:48:22,320 --> 05:48:25,120
that page to then filter it out so we
9094
05:48:25,120 --> 05:48:28,558
can say api.get forward slash posts and
9095
05:48:28,558 --> 05:48:30,718
then it's going to be question mark
9096
05:48:30,718 --> 05:48:33,680
page is equal to dollar sign curly
9097
05:48:33,680 --> 05:48:36,080
braces page that we're getting from the
9098
05:48:36,080 --> 05:48:38,400
parameters of course we have to turn
9099
05:48:38,400 --> 05:48:40,878
this into a template string
9100
05:48:40,878 --> 05:48:43,840
and that's it now we're passing the data
9101
05:48:43,840 --> 05:48:46,080
to the backend just so we know on which
9102
05:48:46,080 --> 05:48:48,320
page are we currently on
9103
05:48:48,320 --> 05:48:50,958
now let's go to the back end and modify
9104
05:48:50,958 --> 05:48:53,520
our controller so that it returns only
9105
05:48:53,520 --> 05:48:56,320
the data for that specific page
9106
05:48:56,320 --> 05:48:58,638
that's going to be inside of the server
9107
05:48:58,638 --> 05:49:01,200
and then finally inside of the posts
9108
05:49:01,200 --> 05:49:03,040
controllers
9109
05:49:03,040 --> 05:49:05,760
inside of here we have our get posts but
9110
05:49:05,760 --> 05:49:08,480
that's basically now get post by page we
9111
05:49:08,480 --> 05:49:10,878
can do similar thing that we did with
9112
05:49:10,878 --> 05:49:14,080
reg.query we're going to say const this
9113
05:49:14,080 --> 05:49:16,160
structure the page and that's going to
9114
05:49:16,160 --> 05:49:18,718
be coming from rec.query because we're
9115
05:49:18,718 --> 05:49:20,480
passing it through the query from the
9116
05:49:20,480 --> 05:49:21,680
front end
9117
05:49:21,680 --> 05:49:23,680
now we're going to have some extra logic
9118
05:49:23,680 --> 05:49:26,240
in here just before we call the dot find
9119
05:49:26,240 --> 05:49:27,600
on post
9120
05:49:27,600 --> 05:49:30,000
first we're going to declare a variable
9121
05:49:30,000 --> 05:49:32,240
called limit the limit is going to be
9122
05:49:32,240 --> 05:49:34,400
the number of posts per page
9123
05:49:34,400 --> 05:49:36,400
i decided to make it eight but you can
9124
05:49:36,400 --> 05:49:39,040
change it if you want to
9125
05:49:39,040 --> 05:49:42,240
then we have to get a start index of a
9126
05:49:42,240 --> 05:49:46,000
post on a specific page for example the
9127
05:49:46,000 --> 05:49:48,400
start index of the first post on the
9128
05:49:48,400 --> 05:49:51,360
third page would be 8 plus 8 plus 8
9129
05:49:51,360 --> 05:49:53,760
minus 1 because we start from 0 and that
9130
05:49:53,760 --> 05:49:57,680
would be 23 so let's create that
9131
05:49:57,680 --> 05:49:59,760
first we have to convert our page into a
9132
05:49:59,760 --> 05:50:02,480
number using the number constructor even
9133
05:50:02,480 --> 05:50:04,718
though the page is a number on the front
9134
05:50:04,718 --> 05:50:06,160
end when we pass it through the
9135
05:50:06,160 --> 05:50:08,718
reg.query it becomes a string so we have
9136
05:50:08,718 --> 05:50:10,400
to convert it back
9137
05:50:10,400 --> 05:50:14,000
and now we are simply going to deduct -1
9138
05:50:14,000 --> 05:50:17,040
from that and finally multiply all of
9139
05:50:17,040 --> 05:50:19,680
that by the limit this way we're always
9140
05:50:19,680 --> 05:50:22,320
going to get start index of the post on
9141
05:50:22,320 --> 05:50:24,718
a specific page we can even write a
9142
05:50:24,718 --> 05:50:28,160
comment right there get the starting
9143
05:50:28,160 --> 05:50:31,200
index of every page
9144
05:50:31,200 --> 05:50:33,120
great
9145
05:50:33,120 --> 05:50:35,920
finally we also need one more variable
9146
05:50:35,920 --> 05:50:38,080
which is const total
9147
05:50:38,080 --> 05:50:41,040
that is going to be equal to await
9148
05:50:41,040 --> 05:50:43,760
postmessage dot
9149
05:50:43,760 --> 05:50:44,798
count
9150
05:50:44,798 --> 05:50:48,080
documents we want to count up all the
9151
05:50:48,080 --> 05:50:50,400
documents so that we always know how
9152
05:50:50,400 --> 05:50:52,480
many documents do we have how many posts
9153
05:50:52,480 --> 05:50:53,520
do we have
9154
05:50:53,520 --> 05:50:55,360
why do we need to know that well
9155
05:50:55,360 --> 05:50:57,040
depending on that we're going to have a
9156
05:50:57,040 --> 05:50:59,600
specific number of pages and we always
9157
05:50:59,600 --> 05:51:01,520
need to know what is the last page we
9158
05:51:01,520 --> 05:51:03,200
can scroll to
9159
05:51:03,200 --> 05:51:06,400
that's why we have the total variable
9160
05:51:06,400 --> 05:51:08,558
now that we have the total finally is
9161
05:51:08,558 --> 05:51:11,120
the time to fetch the post so i'm going
9162
05:51:11,120 --> 05:51:13,760
to rename this from post messages to
9163
05:51:13,760 --> 05:51:17,280
posts and let's see how we can do it
9164
05:51:17,280 --> 05:51:20,240
well we can say await post message dot
9165
05:51:20,240 --> 05:51:21,440
find
9166
05:51:21,440 --> 05:51:23,440
then i want to get the posts from the
9167
05:51:23,440 --> 05:51:25,920
newest to the oldest because of that we
9168
05:51:25,920 --> 05:51:28,240
have to sort them by id so i'm going to
9169
05:51:28,240 --> 05:51:30,638
say dot sort we're going to pass in an
9170
05:51:30,638 --> 05:51:34,000
object and say underscore id is equal to
9171
05:51:34,000 --> 05:51:35,280
-1
9172
05:51:35,280 --> 05:51:37,360
this is going to give us the newest post
9173
05:51:37,360 --> 05:51:38,798
first
9174
05:51:38,798 --> 05:51:41,040
then i also want to limit them so we're
9175
05:51:41,040 --> 05:51:43,440
going to call the dot limit on that and
9176
05:51:43,440 --> 05:51:44,958
we're going to pass in the limit
9177
05:51:44,958 --> 05:51:46,400
variable
9178
05:51:46,400 --> 05:51:48,320
this is going to make sure to only give
9179
05:51:48,320 --> 05:51:51,600
us for example 8 or 10 or or however
9180
05:51:51,600 --> 05:51:53,760
many you choose per page
9181
05:51:53,760 --> 05:51:56,240
and then finally we need to skip all the
9182
05:51:56,240 --> 05:51:57,840
previous pages
9183
05:51:57,840 --> 05:52:00,000
for example if you're on the page 2 you
9184
05:52:00,000 --> 05:52:02,480
don't want to fetch first 16 posts again
9185
05:52:02,480 --> 05:52:05,040
you want to skip the first 8 and because
9186
05:52:05,040 --> 05:52:07,680
of that we're going to use the dot skip
9187
05:52:07,680 --> 05:52:09,760
and we're going to skip all the way to
9188
05:52:09,760 --> 05:52:11,600
the start index
9189
05:52:11,600 --> 05:52:13,520
now we understand why did we have to
9190
05:52:13,520 --> 05:52:16,000
create all these variables it all makes
9191
05:52:16,000 --> 05:52:18,000
sense hopefully
9192
05:52:18,000 --> 05:52:20,080
finally for all of this to make sense to
9193
05:52:20,080 --> 05:52:22,400
the front end we have to pass a bit more
9194
05:52:22,400 --> 05:52:25,440
data than simply posts so i'm going to
9195
05:52:25,440 --> 05:52:27,520
pass a new object
9196
05:52:27,520 --> 05:52:29,520
and inside of that object we're going to
9197
05:52:29,520 --> 05:52:31,040
say data
9198
05:52:31,040 --> 05:52:32,878
and the data is going to be equal to
9199
05:52:32,878 --> 05:52:34,080
posts
9200
05:52:34,080 --> 05:52:36,878
we also have to pass the current page
9201
05:52:36,878 --> 05:52:38,958
which is going to be equal to
9202
05:52:38,958 --> 05:52:41,600
number of the page so we just make it
9203
05:52:41,600 --> 05:52:44,558
into a number and we pass that page
9204
05:52:44,558 --> 05:52:47,280
and finally we also want to have the
9205
05:52:47,280 --> 05:52:50,878
total number of pages
9206
05:52:50,878 --> 05:52:53,160
that is going to be equal to
9207
05:52:53,160 --> 05:52:54,798
math.seal
9208
05:52:54,798 --> 05:52:56,718
and in there we're going to divide the
9209
05:52:56,718 --> 05:52:57,760
total
9210
05:52:57,760 --> 05:52:59,520
by the limit
9211
05:52:59,520 --> 05:53:01,760
great that is going to give us the total
9212
05:53:01,760 --> 05:53:05,600
number of pages now we're passing all of
9213
05:53:05,600 --> 05:53:08,160
that data back to the front end and we
9214
05:53:08,160 --> 05:53:10,160
have to make use of it
9215
05:53:10,160 --> 05:53:12,558
of course where better to do that than
9216
05:53:12,558 --> 05:53:14,638
in the action that we declared so i'm
9217
05:53:14,638 --> 05:53:17,360
going to go back to our posts action
9218
05:53:17,360 --> 05:53:19,280
before we were getting just the data for
9219
05:53:19,280 --> 05:53:21,520
the posts and we're immediately passing
9220
05:53:21,520 --> 05:53:22,798
it here
9221
05:53:22,798 --> 05:53:25,200
but now that data is going to contain
9222
05:53:25,200 --> 05:53:28,718
more info so let's see what is contained
9223
05:53:28,718 --> 05:53:30,958
in that data object
9224
05:53:30,958 --> 05:53:33,120
if i open up the console
9225
05:53:33,120 --> 05:53:35,840
the current page is 1. the total number
9226
05:53:35,840 --> 05:53:38,558
of pages is 3 and this should have been
9227
05:53:38,558 --> 05:53:40,558
pages plural
9228
05:53:40,558 --> 05:53:42,958
so i'm going to go back and just change
9229
05:53:42,958 --> 05:53:45,120
that to pages
9230
05:53:45,120 --> 05:53:48,000
what we had before was simply a variable
9231
05:53:48,000 --> 05:53:50,400
that contained all the posts that means
9232
05:53:50,400 --> 05:53:52,080
that we have to change the way our
9233
05:53:52,080 --> 05:53:55,360
reducers view and render things
9234
05:53:55,360 --> 05:53:56,958
the only thing you have to keep in mind
9235
05:53:56,958 --> 05:53:59,280
here is that our payload is no longer
9236
05:53:59,280 --> 05:54:01,600
just posts it's an object that contains
9237
05:54:01,600 --> 05:54:03,360
three different things as we saw
9238
05:54:03,360 --> 05:54:05,840
previously it contains the data of posts
9239
05:54:05,840 --> 05:54:08,000
it contains the current page and also
9240
05:54:08,000 --> 05:54:09,920
the number of pages
9241
05:54:09,920 --> 05:54:11,920
let's go to our reducers which is going
9242
05:54:11,920 --> 05:54:13,520
to be in here
9243
05:54:13,520 --> 05:54:16,798
reducers and posts instead of simply
9244
05:54:16,798 --> 05:54:19,520
returning the action.payload we have to
9245
05:54:19,520 --> 05:54:22,558
return an object so right there i'll say
9246
05:54:22,558 --> 05:54:23,520
return
9247
05:54:23,520 --> 05:54:25,840
put that into an object
9248
05:54:25,840 --> 05:54:28,320
and then i want to spread the current
9249
05:54:28,320 --> 05:54:29,520
state
9250
05:54:29,520 --> 05:54:31,440
these posts here are going to be more
9251
05:54:31,440 --> 05:54:33,600
than simply an array of posts so i'm
9252
05:54:33,600 --> 05:54:35,920
going to rename this to state you'll see
9253
05:54:35,920 --> 05:54:37,440
soon enough why
9254
05:54:37,440 --> 05:54:40,160
first we want to always spread the state
9255
05:54:40,160 --> 05:54:42,878
when we're working with objects then we
9256
05:54:42,878 --> 05:54:45,760
want to get our posts and now posts are
9257
05:54:45,760 --> 05:54:48,558
not simply action.payload posts are
9258
05:54:48,558 --> 05:54:52,520
equal to action.payload.data
9259
05:54:53,040 --> 05:54:56,400
then we also have a current page our
9260
05:54:56,400 --> 05:54:58,558
current page is going to be equal to
9261
05:54:58,558 --> 05:55:02,798
action dot payload dot current page
9262
05:55:02,798 --> 05:55:05,280
and finally we have number
9263
05:55:05,280 --> 05:55:08,000
of pages which is going to be equal to
9264
05:55:08,000 --> 05:55:10,480
number of pages again tab 9 really
9265
05:55:10,480 --> 05:55:12,400
autofilled that easily
9266
05:55:12,400 --> 05:55:14,718
great so now we're turning something
9267
05:55:14,718 --> 05:55:17,680
different from the fetch all reducer it
9268
05:55:17,680 --> 05:55:20,000
would be great if we immediately also
9269
05:55:20,000 --> 05:55:22,240
accommodated the fetch by search in the
9270
05:55:22,240 --> 05:55:23,520
same fashion
9271
05:55:23,520 --> 05:55:25,760
so in the fetch by search we also have
9272
05:55:25,760 --> 05:55:27,520
to make it an object
9273
05:55:27,520 --> 05:55:29,040
inside of that object we're going to
9274
05:55:29,040 --> 05:55:31,200
spread the state and then we're going to
9275
05:55:31,200 --> 05:55:32,878
say posts
9276
05:55:32,878 --> 05:55:34,240
are equal to
9277
05:55:34,240 --> 05:55:38,638
action dot payload dot data you can also
9278
05:55:38,638 --> 05:55:40,400
see that we are mentioning the posts a
9279
05:55:40,400 --> 05:55:42,320
few more times here i'm just going to
9280
05:55:42,320 --> 05:55:44,638
replace all of these with state
9281
05:55:44,638 --> 05:55:47,440
considering that i renamed it at the top
9282
05:55:47,440 --> 05:55:49,040
right here
9283
05:55:49,040 --> 05:55:51,040
in our fetch by search this is not going
9284
05:55:51,040 --> 05:55:52,798
to be action payload data it's going to
9285
05:55:52,798 --> 05:55:55,360
be simply action payload that's where we
9286
05:55:55,360 --> 05:55:57,440
have our posts
9287
05:55:57,440 --> 05:55:59,040
considering that we've changed the
9288
05:55:59,040 --> 05:56:01,760
structure of our state we have to find
9289
05:56:01,760 --> 05:56:04,000
where are we actually using that state
9290
05:56:04,000 --> 05:56:06,240
so that we can change it there as well
9291
05:56:06,240 --> 05:56:09,360
let's search for use selector that
9292
05:56:09,360 --> 05:56:11,680
points to us using the state
9293
05:56:11,680 --> 05:56:13,520
we can see that it's being used in
9294
05:56:13,520 --> 05:56:15,840
pagination but we're never actually
9295
05:56:15,840 --> 05:56:17,520
doing anything with it
9296
05:56:17,520 --> 05:56:20,798
then it's also being used right in here
9297
05:56:20,798 --> 05:56:23,920
in the form and finally this is the one
9298
05:56:23,920 --> 05:56:26,320
we're looking for use selector in the
9299
05:56:26,320 --> 05:56:29,040
posts here is where we are using the
9300
05:56:29,040 --> 05:56:30,320
posts
9301
05:56:30,320 --> 05:56:33,920
before we simply had an array of posts
9302
05:56:33,920 --> 05:56:36,718
but now we have an object where we have
9303
05:56:36,718 --> 05:56:40,080
a property of posts inside of there so
9304
05:56:40,080 --> 05:56:42,638
we have to destructure the posts right
9305
05:56:42,638 --> 05:56:43,920
in here
9306
05:56:43,920 --> 05:56:45,680
and the main reason we change this from
9307
05:56:45,680 --> 05:56:47,920
an array to an object with multiple
9308
05:56:47,920 --> 05:56:50,320
things is because we are gonna have
9309
05:56:50,320 --> 05:56:52,638
multiple things we're gonna have the is
9310
05:56:52,638 --> 05:56:54,558
loading property we're gonna have the
9311
05:56:54,558 --> 05:56:57,760
number of pages and so on but for now we
9312
05:56:57,760 --> 05:56:59,840
simply need to destructure this
9313
05:56:59,840 --> 05:57:02,160
we can also add a question mark here to
9314
05:57:02,160 --> 05:57:04,480
make sure to not throw an error if we
9315
05:57:04,480 --> 05:57:06,958
don't have any posts available
9316
05:57:06,958 --> 05:57:09,200
now let's save that and let's see if
9317
05:57:09,200 --> 05:57:10,958
this is going to work
9318
05:57:10,958 --> 05:57:13,360
we are back on our memories project and
9319
05:57:13,360 --> 05:57:15,760
would you look at that we can see all of
9320
05:57:15,760 --> 05:57:18,480
our posts that's great let's see if we
9321
05:57:18,480 --> 05:57:20,320
can also see the posts that we search
9322
05:57:20,320 --> 05:57:22,638
for for example let's search for europe
9323
05:57:22,638 --> 05:57:24,958
again
9324
05:57:26,160 --> 05:57:29,040
and it works we waited a bit but it
9325
05:57:29,040 --> 05:57:30,958
finally gave me the posts
9326
05:57:30,958 --> 05:57:33,040
that's great but that's something that
9327
05:57:33,040 --> 05:57:35,600
worked before right so why even check it
9328
05:57:35,600 --> 05:57:37,440
well we checked it because we changed a
9329
05:57:37,440 --> 05:57:38,958
lot of things and you want to make sure
9330
05:57:38,958 --> 05:57:40,718
that when you change some big things you
9331
05:57:40,718 --> 05:57:43,120
check if you've broken anything
9332
05:57:43,120 --> 05:57:44,558
but now is the time to check the
9333
05:57:44,558 --> 05:57:45,840
pagination
9334
05:57:45,840 --> 05:57:47,840
it's not going to work yet because even
9335
05:57:47,840 --> 05:57:49,760
though we implemented all the logic for
9336
05:57:49,760 --> 05:57:52,400
it we have to do one more step and that
9337
05:57:52,400 --> 05:57:54,638
is use that logic inside of the
9338
05:57:54,638 --> 05:57:57,200
pagination component so let's go back to
9339
05:57:57,200 --> 05:57:58,878
it and do it
9340
05:57:58,878 --> 05:58:01,040
inside of the pagination component we
9341
05:58:01,040 --> 05:58:03,520
can now use this use selector to get the
9342
05:58:03,520 --> 05:58:06,558
number of pages to do that we can say
9343
05:58:06,558 --> 05:58:08,638
const the structure
9344
05:58:08,638 --> 05:58:11,200
number of pages
9345
05:58:11,200 --> 05:58:13,360
and make that equal to
9346
05:58:13,360 --> 05:58:15,840
use selector
9347
05:58:15,840 --> 05:58:17,840
inside of there we have a callback
9348
05:58:17,840 --> 05:58:20,080
function where we get the posts and we
9349
05:58:20,080 --> 05:58:23,680
want to retrieve state dot posts
9350
05:58:23,680 --> 05:58:25,520
that is the reducer that's going to have
9351
05:58:25,520 --> 05:58:28,240
the access to the number of pages
9352
05:58:28,240 --> 05:58:30,320
finally we want to use that number of
9353
05:58:30,320 --> 05:58:34,240
pages variable right in here under count
9354
05:58:34,240 --> 05:58:36,878
number of pages
9355
05:58:36,878 --> 05:58:39,360
only that we also have the access to the
9356
05:58:39,360 --> 05:58:41,840
current page right in here so we can
9357
05:58:41,840 --> 05:58:43,680
simply say right there
9358
05:58:43,680 --> 05:58:44,958
number
9359
05:58:44,958 --> 05:58:46,638
and page
9360
05:58:46,638 --> 05:58:48,400
and just to be sure if we don't have a
9361
05:58:48,400 --> 05:58:51,520
page we want to render or 1 to be on the
9362
05:58:51,520 --> 05:58:53,200
first page
9363
05:58:53,200 --> 05:58:55,280
i also noticed that right now i
9364
05:58:55,280 --> 05:58:57,680
specified right there you select that is
9365
05:58:57,680 --> 05:59:00,958
going to be use selector
9366
05:59:00,958 --> 05:59:04,878
also in here posts page one
9367
05:59:04,878 --> 05:59:08,000
in there we want to navigate to item dot
9368
05:59:08,000 --> 05:59:09,120
page
9369
05:59:09,120 --> 05:59:11,440
with these small changes we've made our
9370
05:59:11,440 --> 05:59:14,558
pagination component completely dynamic
9371
05:59:14,558 --> 05:59:16,878
let's see if it works
9372
05:59:16,878 --> 05:59:18,718
going back to the browser first thing
9373
05:59:18,718 --> 05:59:21,120
that you can see is that we have only
9374
05:59:21,120 --> 05:59:23,280
three pages and that is correct because
9375
05:59:23,280 --> 05:59:26,480
we have less than 24 posts let's try to
9376
05:59:26,480 --> 05:59:28,718
go to the second page
9377
05:59:28,718 --> 05:59:31,760
as you can see the url changed and even
9378
05:59:31,760 --> 05:59:33,520
though it seemed for a second that
9379
05:59:33,520 --> 05:59:35,040
nothing is happening because we don't
9380
05:59:35,040 --> 05:59:37,760
have the loading yet it worked we got
9381
05:59:37,760 --> 05:59:40,558
the second batch of posts finally let's
9382
05:59:40,558 --> 05:59:42,958
go to the third page
9383
05:59:42,958 --> 05:59:46,798
and there we go we got five more posts
9384
05:59:46,798 --> 05:59:49,920
that means that our pagination fully
9385
05:59:49,920 --> 05:59:52,000
works so we can switch between the pages
9386
05:59:52,000 --> 05:59:54,798
and we can render only eight at a time
9387
05:59:54,798 --> 05:59:57,040
this is incredibly important imagine
9388
05:59:57,040 --> 05:59:58,558
that your application is used by
9389
05:59:58,558 --> 06:00:01,040
hundreds or even thousands of users and
9390
06:00:01,040 --> 06:00:03,360
that each user creates tens or even
9391
06:00:03,360 --> 06:00:04,958
hundreds of posts
9392
06:00:04,958 --> 06:00:07,600
imagine if your application had to load
9393
06:00:07,600 --> 06:00:10,480
all 10 000 posts at once it would take
9394
06:00:10,480 --> 06:00:12,958
so much time but why even do it when you
9395
06:00:12,958 --> 06:00:16,080
can only see 8 at once that is why we
9396
06:00:16,080 --> 06:00:18,718
use the pagination to cut down on low
9397
06:00:18,718 --> 06:00:19,760
time
9398
06:00:19,760 --> 06:00:22,558
anyway great work pagination seems to be
9399
06:00:22,558 --> 06:00:24,240
done at least from what i can see right
9400
06:00:24,240 --> 06:00:26,718
now hopefully there aren't any bugs and
9401
06:00:26,718 --> 06:00:29,360
now we can move to the loading states
9402
06:00:29,360 --> 06:00:32,240
to start working on our loading states
9403
06:00:32,240 --> 06:00:35,360
you can head to posts actions redux
9404
06:00:35,360 --> 06:00:38,400
makes it quite easy to have one central
9405
06:00:38,400 --> 06:00:41,280
source of truth so we always know are we
9406
06:00:41,280 --> 06:00:44,080
loading or are we not that's why we're
9407
06:00:44,080 --> 06:00:45,600
going to use redux to accomplish the
9408
06:00:45,600 --> 06:00:47,120
loading states
9409
06:00:47,120 --> 06:00:49,520
so right inside of here we're first
9410
06:00:49,520 --> 06:00:52,638
going to create two more constants
9411
06:00:52,638 --> 06:00:54,160
action types
9412
06:00:54,160 --> 06:00:56,480
right in here i'm going to create the
9413
06:00:56,480 --> 06:00:59,200
export const start
9414
06:00:59,200 --> 06:01:01,440
loading that's going to be equal to a
9415
06:01:01,440 --> 06:01:04,080
string of start loading and then finally
9416
06:01:04,080 --> 06:01:05,680
we're going to have one more which is
9417
06:01:05,680 --> 06:01:08,958
going to be called end loading
9418
06:01:08,958 --> 06:01:10,878
we're going to use this action types so
9419
06:01:10,878 --> 06:01:13,120
that our redux reducers know when to
9420
06:01:13,120 --> 06:01:15,760
start and stop loading we can go back to
9421
06:01:15,760 --> 06:01:17,760
our action posts and we're going to
9422
06:01:17,760 --> 06:01:20,080
import them right here at the top
9423
06:01:20,080 --> 06:01:21,920
start loading
9424
06:01:21,920 --> 06:01:24,558
and end loading
9425
06:01:24,558 --> 06:01:26,320
the only question we have to ask
9426
06:01:26,320 --> 06:01:28,718
ourselves is when do we want to start
9427
06:01:28,718 --> 06:01:31,440
loading well as soon as we call get
9428
06:01:31,440 --> 06:01:34,240
posts just before we try fetching the
9429
06:01:34,240 --> 06:01:36,718
posts right in there we can say this
9430
06:01:36,718 --> 06:01:38,080
patch
9431
06:01:38,080 --> 06:01:40,080
and the only thing we want to dispatch
9432
06:01:40,080 --> 06:01:42,558
is an action that has a type
9433
06:01:42,558 --> 06:01:43,680
of
9434
06:01:43,680 --> 06:01:44,798
start
9435
06:01:44,798 --> 06:01:47,280
loading that's going to be it
9436
06:01:47,280 --> 06:01:49,360
and when do we want to end the loading
9437
06:01:49,360 --> 06:01:52,000
well it definitely has to be after we
9438
06:01:52,000 --> 06:01:53,920
fetched all the posts which is going to
9439
06:01:53,920 --> 06:01:58,798
be right here dispatch type and loading
9440
06:01:58,798 --> 06:02:00,160
of course we want to have the loading
9441
06:02:00,160 --> 06:02:02,638
states in a few more places also when we
9442
06:02:02,638 --> 06:02:05,120
try to get post by search and so on so
9443
06:02:05,120 --> 06:02:07,360
we can copy this start loading and we
9444
06:02:07,360 --> 06:02:10,480
can paste it in a few more places before
9445
06:02:10,480 --> 06:02:12,878
we try to get post by search we want to
9446
06:02:12,878 --> 06:02:14,320
have it there
9447
06:02:14,320 --> 06:02:16,558
we also want to have it before we create
9448
06:02:16,558 --> 06:02:17,760
a post
9449
06:02:17,760 --> 06:02:19,760
on update we're not going to go anywhere
9450
06:02:19,760 --> 06:02:21,200
we're just going to update it so we
9451
06:02:21,200 --> 06:02:22,798
don't have to have it there
9452
06:02:22,798 --> 06:02:24,958
for the like does the same thing and for
9453
06:02:24,958 --> 06:02:27,680
the delete that's also the same thing
9454
06:02:27,680 --> 06:02:29,520
these actions by themselves are not
9455
06:02:29,520 --> 06:02:31,200
doing anything we have to go to our
9456
06:02:31,200 --> 06:02:33,840
reducers and then there create the logic
9457
06:02:33,840 --> 06:02:36,080
for our loading states instead of here
9458
06:02:36,080 --> 06:02:39,120
we're gonna also import start loading
9459
06:02:39,120 --> 06:02:42,400
and end loading or is it stop loading
9460
06:02:42,400 --> 06:02:44,558
let me see how i called it it start
9461
06:02:44,558 --> 06:02:46,958
loading and then we have the end loading
9462
06:02:46,958 --> 06:02:49,120
i also forgot to put it here at the end
9463
06:02:49,120 --> 06:02:50,958
of the fetch by search
9464
06:02:50,958 --> 06:02:53,680
so i'm gonna import it and loading and
9465
06:02:53,680 --> 06:02:56,400
now we have to add it here at the top so
9466
06:02:56,400 --> 06:02:59,120
what happens if we want to start the
9467
06:02:59,120 --> 06:03:00,080
loading
9468
06:03:00,080 --> 06:03:03,360
also what happens if we want to end the
9469
06:03:03,360 --> 06:03:06,160
loading we're gonna prepare those cases
9470
06:03:06,160 --> 06:03:08,480
and let's see what data do we want to
9471
06:03:08,480 --> 06:03:09,600
return
9472
06:03:09,600 --> 06:03:10,958
well of course we want to return an
9473
06:03:10,958 --> 06:03:14,480
object as we always do we want to spread
9474
06:03:14,480 --> 06:03:17,520
the state that we currently have and we
9475
06:03:17,520 --> 06:03:19,520
only want to change the state of one
9476
06:03:19,520 --> 06:03:20,400
variable
9477
06:03:20,400 --> 06:03:22,798
and that is is loading is going to be
9478
06:03:22,798 --> 06:03:24,798
equal to true if we want to start the
9479
06:03:24,798 --> 06:03:27,360
loading and then we can copy this and
9480
06:03:27,360 --> 06:03:29,920
paste it below same thing but we're
9481
06:03:29,920 --> 06:03:31,840
going to change the is loading state to
9482
06:03:31,840 --> 06:03:33,520
be false
9483
06:03:33,520 --> 06:03:36,000
that's all that you have to do to manage
9484
06:03:36,000 --> 06:03:38,958
loading in redux you don't have to do it
9485
06:03:38,958 --> 06:03:40,958
everywhere in your code in all of the
9486
06:03:40,958 --> 06:03:42,878
components whenever you do something
9487
06:03:42,878 --> 06:03:45,120
cover the logic with changing the state
9488
06:03:45,120 --> 06:03:47,520
you only do it here and everything is
9489
06:03:47,520 --> 06:03:50,080
being done right here inside of actions
9490
06:03:50,080 --> 06:03:52,080
it might be a bit confusing at the start
9491
06:03:52,080 --> 06:03:54,080
but as you get more into it you'll start
9492
06:03:54,080 --> 06:03:56,638
seeing the purpose in that
9493
06:03:56,638 --> 06:03:58,878
back in reducers considering we change
9494
06:03:58,878 --> 06:04:01,360
the way we view data we also have to
9495
06:04:01,360 --> 06:04:04,160
change this state the state is no longer
9496
06:04:04,160 --> 06:04:06,400
just going to be an array of posts it's
9497
06:04:06,400 --> 06:04:08,000
going to be an object that's going to
9498
06:04:08,000 --> 06:04:10,320
have the is loading property
9499
06:04:10,320 --> 06:04:12,160
and that is loading is initially going
9500
06:04:12,160 --> 06:04:13,760
to be set to true
9501
06:04:13,760 --> 06:04:15,920
and also we're going to have the posts
9502
06:04:15,920 --> 06:04:17,680
array great
9503
06:04:17,680 --> 06:04:19,520
now that we've changed this we also have
9504
06:04:19,520 --> 06:04:22,240
to change how do we get the data back
9505
06:04:22,240 --> 06:04:25,120
once we like create update or delete
9506
06:04:25,120 --> 06:04:26,400
posts
9507
06:04:26,400 --> 06:04:28,878
the only thing we have to do is we have
9508
06:04:28,878 --> 06:04:32,000
to everywhere spread the state so right
9509
06:04:32,000 --> 06:04:34,000
here i'm gonna try to select multiple
9510
06:04:34,000 --> 06:04:36,558
lines by holding alt and clicking at the
9511
06:04:36,558 --> 06:04:38,480
start of each line
9512
06:04:38,480 --> 06:04:41,680
right there i'm gonna open up an object
9513
06:04:41,680 --> 06:04:43,520
and first thing that we want to do
9514
06:04:43,520 --> 06:04:45,600
everywhere is we want to spread the
9515
06:04:45,600 --> 06:04:49,120
state as we always did
9516
06:04:49,120 --> 06:04:51,280
finally once we spread the state we want
9517
06:04:51,280 --> 06:04:54,000
to set the posts inside of that state to
9518
06:04:54,000 --> 06:04:56,840
be equal to what we previously had right
9519
06:04:56,840 --> 06:04:59,840
there and then finally make sure to add
9520
06:04:59,840 --> 06:05:03,920
the closing bracket at the end here here
9521
06:05:03,920 --> 06:05:05,760
here and here
9522
06:05:05,760 --> 06:05:07,600
i know that it might be just a bit
9523
06:05:07,600 --> 06:05:10,798
confusing but we are basically switching
9524
06:05:10,798 --> 06:05:13,680
from using just posts as an array to
9525
06:05:13,680 --> 06:05:15,920
having an object that now contains the
9526
06:05:15,920 --> 06:05:18,160
is loading state as well as the post
9527
06:05:18,160 --> 06:05:20,558
state so that's why we have to turn this
9528
06:05:20,558 --> 06:05:23,200
into an object spread the entire state
9529
06:05:23,200 --> 06:05:27,120
and then finally only return the posts
9530
06:05:27,120 --> 06:05:29,520
and also notice how we're doing stay.map
9531
06:05:29,520 --> 06:05:31,200
spreading the state state.map
9532
06:05:31,200 --> 06:05:33,520
state.filter that's no longer going to
9533
06:05:33,520 --> 06:05:37,760
be possible rather we have to do state
9534
06:05:37,760 --> 06:05:41,920
and then dot post and then dot map or
9535
06:05:41,920 --> 06:05:44,558
that filter or anything so
9536
06:05:44,558 --> 06:05:48,160
just to fix this we're doing state dot
9537
06:05:48,160 --> 06:05:50,798
posts that map we're spreading stay that
9538
06:05:50,798 --> 06:05:53,440
post stay that post set map and state of
9539
06:05:53,440 --> 06:05:55,840
post filter these are going to be all
9540
06:05:55,840 --> 06:05:57,280
the changes that we have to do with
9541
06:05:57,280 --> 06:05:58,320
redux
9542
06:05:58,320 --> 06:06:00,480
it is sometimes confusing i completely
9543
06:06:00,480 --> 06:06:02,320
understand that but it's going to help
9544
06:06:02,320 --> 06:06:04,958
us in the long run by maintaining one
9545
06:06:04,958 --> 06:06:06,718
state of truth
9546
06:06:06,718 --> 06:06:08,718
that said we can now actually start
9547
06:06:08,718 --> 06:06:11,840
using the is loading state we can start
9548
06:06:11,840 --> 06:06:15,200
in the posts.js file so we can open up
9549
06:06:15,200 --> 06:06:19,600
our components and go to postspost.js
9550
06:06:19,600 --> 06:06:23,040
inside of here from the u selector we're
9551
06:06:23,040 --> 06:06:26,160
also going to grab that state is loading
9552
06:06:26,160 --> 06:06:28,400
and now based on that state we can show
9553
06:06:28,400 --> 06:06:31,280
some data so we're going to replace this
9554
06:06:31,280 --> 06:06:32,080
for
9555
06:06:32,080 --> 06:06:33,360
if
9556
06:06:33,360 --> 06:06:34,718
is loading
9557
06:06:34,718 --> 06:06:36,718
in that case we want to show our
9558
06:06:36,718 --> 06:06:38,878
circular progress which is that loading
9559
06:06:38,878 --> 06:06:41,520
state and we also want to add one if
9560
06:06:41,520 --> 06:06:44,160
statement and say if there are no posts
9561
06:06:44,160 --> 06:06:45,920
that length meaning if there are no
9562
06:06:45,920 --> 06:06:47,680
posts in the array
9563
06:06:47,680 --> 06:06:51,280
and if we're not loading in that case we
9564
06:06:51,280 --> 06:06:54,320
want to return something that says no
9565
06:06:54,320 --> 06:06:56,958
post because if we don't have any posts
9566
06:06:56,958 --> 06:06:58,958
and we are not currently loading that
9567
06:06:58,958 --> 06:07:00,718
means we're not going to load them they
9568
06:07:00,718 --> 06:07:02,320
don't exist
9569
06:07:02,320 --> 06:07:04,480
great so let's save that and see it in
9570
06:07:04,480 --> 06:07:05,520
action
9571
06:07:05,520 --> 06:07:07,920
i'm gonna reload the page and take a
9572
06:07:07,920 --> 06:07:10,000
look at that we have this nice loading
9573
06:07:10,000 --> 06:07:12,240
state at the top left of course you can
9574
06:07:12,240 --> 06:07:14,480
style it a bit more make it big and make
9575
06:07:14,480 --> 06:07:16,878
it in the center of the whole post but
9576
06:07:16,878 --> 06:07:19,040
it's fine enough we see it on the top
9577
06:07:19,040 --> 06:07:21,040
right corner now take a look what
9578
06:07:21,040 --> 06:07:23,280
happens when i paginate
9579
06:07:23,280 --> 06:07:25,760
it simply tells the user hey i'm doing
9580
06:07:25,760 --> 06:07:28,080
something wait a second and it's so nice
9581
06:07:28,080 --> 06:07:29,920
to have that visual feedback it makes
9582
06:07:29,920 --> 06:07:33,120
your app look so much more professional
9583
06:07:33,120 --> 06:07:34,558
same thing happens if you search
9584
06:07:34,558 --> 06:07:36,718
something like the here
9585
06:07:36,718 --> 06:07:39,760
and also same thing happens if we go for
9586
06:07:39,760 --> 06:07:42,160
tags like asia
9587
06:07:42,160 --> 06:07:44,320
there we go we have nice loading and
9588
06:07:44,320 --> 06:07:47,040
only then do we see our posts
9589
06:07:47,040 --> 06:07:48,558
and as you can see it works with
9590
06:07:48,558 --> 06:07:50,878
searching the titles searching the tags
9591
06:07:50,878 --> 06:07:53,280
doing the pagination or simply getting
9592
06:07:53,280 --> 06:07:55,280
all the posts
9593
06:07:55,280 --> 06:07:57,280
that's why i like using redux when you
9594
06:07:57,280 --> 06:07:59,680
want to have one source of truth of
9595
06:07:59,680 --> 06:08:01,360
course you could have used context as
9596
06:08:01,360 --> 06:08:03,360
well and if that's your preference feel
9597
06:08:03,360 --> 06:08:05,440
free to refactor this application from
9598
06:08:05,440 --> 06:08:08,160
redux to using context
9599
06:08:08,160 --> 06:08:10,240
we can go to the home component
9600
06:08:10,240 --> 06:08:12,000
and right in here you should see that
9601
06:08:12,000 --> 06:08:14,240
this is the place where we are rendering
9602
06:08:14,240 --> 06:08:17,680
our pagination and the paper wrapping it
9603
06:08:17,680 --> 06:08:19,920
first things first i don't always want
9604
06:08:19,920 --> 06:08:22,240
to display the pagination i only want to
9605
06:08:22,240 --> 06:08:24,558
display it if we are viewing the content
9606
06:08:24,558 --> 06:08:25,840
by pages
9607
06:08:25,840 --> 06:08:27,680
but if you're doing the search i'm gonna
9608
06:08:27,680 --> 06:08:29,760
assume that the user doesn't have a lot
9609
06:08:29,760 --> 06:08:32,558
of posts to search from like 10 20 or
9610
06:08:32,558 --> 06:08:34,480
30. in that way we don't need to
9611
06:08:34,480 --> 06:08:37,600
paginate so what we can do is we can
9612
06:08:37,600 --> 06:08:40,400
wrap this into a dynamic block and say
9613
06:08:40,400 --> 06:08:43,600
if we don't currently have a search
9614
06:08:43,600 --> 06:08:44,638
query
9615
06:08:44,638 --> 06:08:47,200
and if we don't have any tags so we can
9616
06:08:47,200 --> 06:08:51,200
do tags.length in that case we want to
9617
06:08:51,200 --> 06:08:54,638
show our pagination so one more time let
9618
06:08:54,638 --> 06:08:56,480
me just put this in here
9619
06:08:56,480 --> 06:09:00,480
if we don't have a search query
9620
06:09:00,480 --> 06:09:03,760
and if we don't have tags in that case i
9621
06:09:03,760 --> 06:09:06,080
want to render the pagination if we have
9622
06:09:06,080 --> 06:09:08,160
the search or the tags in that case we
9623
06:09:08,160 --> 06:09:10,480
don't want to render the pagination
9624
06:09:10,480 --> 06:09:12,878
now that we have that we can also add
9625
06:09:12,878 --> 06:09:14,878
the class name here and that's going to
9626
06:09:14,878 --> 06:09:18,080
be classes dot pagination
9627
06:09:18,080 --> 06:09:20,320
let's save that and see it in action
9628
06:09:20,320 --> 06:09:22,480
there we go as soon as i save that you
9629
06:09:22,480 --> 06:09:24,638
can see it looks just a bit better it is
9630
06:09:24,638 --> 06:09:26,798
displayed right there let's try to sign
9631
06:09:26,798 --> 06:09:28,480
in to see how does it look like with the
9632
06:09:28,480 --> 06:09:29,600
form
9633
06:09:29,600 --> 06:09:32,958
there we go looks extremely good
9634
06:09:32,958 --> 06:09:36,160
there we go it looks extremely good i
9635
06:09:36,160 --> 06:09:38,718
also see that we don't have this shadow
9636
06:09:38,718 --> 06:09:41,360
on the form so let's add it as well
9637
06:09:41,360 --> 06:09:43,120
right there i'm gonna search for
9638
06:09:43,120 --> 06:09:45,200
creating a memory
9639
06:09:45,200 --> 06:09:48,240
right in here search creating a memory
9640
06:09:48,240 --> 06:09:50,240
and there i can see that that is in the
9641
06:09:50,240 --> 06:09:52,320
form file so i can just add the
9642
06:09:52,320 --> 06:09:55,840
elevation equal to 6 to this specific
9643
06:09:55,840 --> 06:09:57,120
paper
9644
06:09:57,120 --> 06:09:59,520
if i go back you can see that even the
9645
06:09:59,520 --> 06:10:02,240
form now has the shadow and all the
9646
06:10:02,240 --> 06:10:04,080
elements in the page have shadow to make
9647
06:10:04,080 --> 06:10:06,400
them pop up from the background
9648
06:10:06,400 --> 06:10:09,680
our pagination is working our search is
9649
06:10:09,680 --> 06:10:12,878
working our design is good what else do
9650
06:10:12,878 --> 06:10:14,320
we have to do
9651
06:10:14,320 --> 06:10:16,400
well of course we have to implement the
9652
06:10:16,400 --> 06:10:19,360
dynamic memory details page when you
9653
06:10:19,360 --> 06:10:21,360
click on the memory we want to display
9654
06:10:21,360 --> 06:10:23,520
everything about that memory in a nice
9655
06:10:23,520 --> 06:10:24,638
view
9656
06:10:24,638 --> 06:10:26,958
so let's do that right now
9657
06:10:26,958 --> 06:10:29,120
to implement the memory details page we
9658
06:10:29,120 --> 06:10:31,120
first have to see what's going to happen
9659
06:10:31,120 --> 06:10:34,080
when we click on a specific post to
9660
06:10:34,080 --> 06:10:36,480
figure that out we're going to go to
9661
06:10:36,480 --> 06:10:40,320
posts and then post right in here that's
9662
06:10:40,320 --> 06:10:42,320
not post details it's just the post
9663
06:10:42,320 --> 06:10:44,400
component so we have to make something
9664
06:10:44,400 --> 06:10:47,520
happen once we click on this card
9665
06:10:47,520 --> 06:10:49,280
and the thing we're going to do is we're
9666
06:10:49,280 --> 06:10:50,878
going to import one more thing from
9667
06:10:50,878 --> 06:10:53,280
material ui which is going to be the
9668
06:10:53,280 --> 06:10:55,200
button base
9669
06:10:55,200 --> 06:10:57,280
and just below this card we're going to
9670
06:10:57,280 --> 06:11:00,798
add a button base component
9671
06:11:00,798 --> 06:11:02,718
we're gonna give it a class name which
9672
06:11:02,718 --> 06:11:06,878
is equal to classes dot card action
9673
06:11:06,878 --> 06:11:09,280
and it's also going to have the on click
9674
06:11:09,280 --> 06:11:10,320
property
9675
06:11:10,320 --> 06:11:12,480
on click what do we want to do well we
9676
06:11:12,480 --> 06:11:14,958
want to open the post
9677
06:11:14,958 --> 06:11:16,400
we don't have a lot of props so i'm
9678
06:11:16,400 --> 06:11:18,320
going to put them in one line
9679
06:11:18,320 --> 06:11:21,680
and finally the ending tag of this has
9680
06:11:21,680 --> 06:11:24,240
to go below the card content so i'm
9681
06:11:24,240 --> 06:11:26,480
going to copy it and put it just below
9682
06:11:26,480 --> 06:11:28,480
the card content but still above the
9683
06:11:28,480 --> 06:11:31,680
card actions great
9684
06:11:31,680 --> 06:11:34,080
open post is not defined so let's define
9685
06:11:34,080 --> 06:11:37,040
it const open post is going to be equal
9686
06:11:37,040 --> 06:11:39,680
to to an arrow function for now
9687
06:11:39,680 --> 06:11:42,080
so how is this open post gonna work well
9688
06:11:42,080 --> 06:11:44,558
it's going to be incredibly simple the
9689
06:11:44,558 --> 06:11:46,400
only thing we want to do is we want to
9690
06:11:46,400 --> 06:11:49,840
navigate to a specific url so to do that
9691
06:11:49,840 --> 06:11:52,798
we have to import use history from react
9692
06:11:52,798 --> 06:11:54,400
router dom
9693
06:11:54,400 --> 06:11:55,520
import
9694
06:11:55,520 --> 06:11:56,840
use
9695
06:11:56,840 --> 06:11:59,520
history and that's going to be from
9696
06:11:59,520 --> 06:12:01,920
react router dom
9697
06:12:01,920 --> 06:12:03,760
we already learned that we have to call
9698
06:12:03,760 --> 06:12:06,160
it as a hook which is going to be const
9699
06:12:06,160 --> 06:12:09,680
history is equal to use history
9700
06:12:09,680 --> 06:12:12,400
and then finally inside of the open post
9701
06:12:12,400 --> 06:12:15,920
we can simply say history dot push
9702
06:12:15,920 --> 06:12:18,160
and then we're gonna push to
9703
06:12:18,160 --> 06:12:20,958
forward slash posts forward slash and
9704
06:12:20,958 --> 06:12:24,400
then post dot underscore id so you're
9705
06:12:24,400 --> 06:12:26,638
pushing to the id of the post that is
9706
06:12:26,638 --> 06:12:28,718
currently selected
9707
06:12:28,718 --> 06:12:31,200
and everything else is going to be done
9708
06:12:31,200 --> 06:12:35,120
for us in that post details page so we
9709
06:12:35,120 --> 06:12:37,680
know that if we go to forward slash post
9710
06:12:37,680 --> 06:12:39,280
forward slash id
9711
06:12:39,280 --> 06:12:43,520
if we go to our app you can see posts id
9712
06:12:43,520 --> 06:12:46,558
belongs to the post details page so
9713
06:12:46,558 --> 06:12:48,558
everything else will have to be done in
9714
06:12:48,558 --> 06:12:51,840
here and in redux actions
9715
06:12:51,840 --> 06:12:53,840
this is going to be a fairly large
9716
06:12:53,840 --> 06:12:56,400
component so let's start right away we
9717
06:12:56,400 --> 06:12:59,040
first want to import react and we also
9718
06:12:59,040 --> 06:13:02,718
want to import the use effect from react
9719
06:13:02,718 --> 06:13:04,718
then we want to import a few things from
9720
06:13:04,718 --> 06:13:06,638
material ui and those things are going
9721
06:13:06,638 --> 06:13:09,920
to be paper typography
9722
06:13:09,920 --> 06:13:11,840
we also want to have the circular
9723
06:13:11,840 --> 06:13:14,240
progress for loading and finally the
9724
06:13:14,240 --> 06:13:15,760
divider
9725
06:13:15,760 --> 06:13:17,680
and this is going to be imported from
9726
06:13:17,680 --> 06:13:20,878
add material ui for slash core
9727
06:13:20,878 --> 06:13:23,120
then we also want to import a few things
9728
06:13:23,120 --> 06:13:25,120
from react redux and that's going to be
9729
06:13:25,120 --> 06:13:27,520
the use dispatch as well as the use
9730
06:13:27,520 --> 06:13:30,878
selector to get the data about the post
9731
06:13:30,878 --> 06:13:33,600
as i said these are coming from react
9732
06:13:33,600 --> 06:13:35,360
dash redux
9733
06:13:35,360 --> 06:13:37,840
then we want to import moment
9734
06:13:37,840 --> 06:13:38,718
from
9735
06:13:38,718 --> 06:13:39,920
moment
9736
06:13:39,920 --> 06:13:42,080
moment is a js library that deals with
9737
06:13:42,080 --> 06:13:43,280
time
9738
06:13:43,280 --> 06:13:45,120
and finally we want to import a few
9739
06:13:45,120 --> 06:13:47,120
things from react router dom those
9740
06:13:47,120 --> 06:13:50,558
things are used params and also use
9741
06:13:50,558 --> 06:13:51,920
history
9742
06:13:51,920 --> 06:13:55,200
from react router dom
9743
06:13:55,200 --> 06:13:56,400
great
9744
06:13:56,400 --> 06:13:58,558
we also have some styles so we can say
9745
06:13:58,558 --> 06:14:00,878
import use styles and that's going to be
9746
06:14:00,878 --> 06:14:03,280
from that slash styles
9747
06:14:03,280 --> 06:14:05,200
as you can see we don't yet have that
9748
06:14:05,200 --> 06:14:08,280
file so let's create a new file
9749
06:14:08,280 --> 06:14:10,000
styles.js
9750
06:14:10,000 --> 06:14:12,000
and as always i'm going to provide you
9751
06:14:12,000 --> 06:14:14,320
with all the styles you can simply copy
9752
06:14:14,320 --> 06:14:16,240
and paste them right in here they're
9753
06:14:16,240 --> 06:14:18,400
going to be down in the description
9754
06:14:18,400 --> 06:14:19,280
while they're scrolling to the
9755
06:14:19,280 --> 06:14:21,280
description make sure to leave a like
9756
06:14:21,280 --> 06:14:22,798
and leave a comment for the youtube
9757
06:14:22,798 --> 06:14:24,080
algorithm
9758
06:14:24,080 --> 06:14:26,400
now that we have the hooks for dispatch
9759
06:14:26,400 --> 06:14:29,760
selector params history use styles let's
9760
06:14:29,760 --> 06:14:32,638
declare all of those hooks at the top
9761
06:14:32,638 --> 06:14:34,320
first we're going to get the data about
9762
06:14:34,320 --> 06:14:37,120
the post from use selector that's going
9763
06:14:37,120 --> 06:14:39,600
to be const post
9764
06:14:39,600 --> 06:14:42,160
posts and is loading
9765
06:14:42,160 --> 06:14:44,320
and that's going to be coming from use
9766
06:14:44,320 --> 06:14:45,600
selector
9767
06:14:45,600 --> 06:14:47,040
in there we have a callback function
9768
06:14:47,040 --> 06:14:49,680
with a state and finally we want to get
9769
06:14:49,680 --> 06:14:53,520
the state dot posts reducer
9770
06:14:53,520 --> 06:14:55,920
then we want to say cons dispatch is
9771
06:14:55,920 --> 06:14:59,440
going to be equal to use dispatch
9772
06:14:59,440 --> 06:15:01,440
we also want to get the history so we're
9773
06:15:01,440 --> 06:15:03,600
going to say cons history is going to be
9774
06:15:03,600 --> 06:15:06,160
equal to use history
9775
06:15:06,160 --> 06:15:08,480
we also want to get the classes so cons
9776
06:15:08,480 --> 06:15:11,280
classes is going to be equal to use
9777
06:15:11,280 --> 06:15:12,878
styles
9778
06:15:12,878 --> 06:15:15,600
and finally we also want to get that id
9779
06:15:15,600 --> 06:15:17,520
so we're going to say cons the structure
9780
06:15:17,520 --> 06:15:21,040
the id and that's coming from use params
9781
06:15:21,040 --> 06:15:23,280
remember our url we're going to forward
9782
06:15:23,280 --> 06:15:26,718
slash posts forward slash an id great
9783
06:15:26,718 --> 06:15:28,400
we've prepared everything to start
9784
06:15:28,400 --> 06:15:30,638
creating the jsx layout of our post
9785
06:15:30,638 --> 06:15:32,798
details component considering that the
9786
06:15:32,798 --> 06:15:34,798
post details component is incredibly
9787
06:15:34,798 --> 06:15:36,958
similar to the post component it's gonna
9788
06:15:36,958 --> 06:15:38,878
be a card or a paper that's gonna have
9789
06:15:38,878 --> 06:15:40,878
some typography where we display all of
9790
06:15:40,878 --> 06:15:42,480
the post details
9791
06:15:42,480 --> 06:15:44,000
because of that i'm gonna give you the
9792
06:15:44,000 --> 06:15:47,360
jsx code for some parts of our post
9793
06:15:47,360 --> 06:15:49,680
details component i'm going to paste it
9794
06:15:49,680 --> 06:15:51,680
in here you're going to find this in the
9795
06:15:51,680 --> 06:15:53,760
description down below the only thing
9796
06:15:53,760 --> 06:15:56,080
that's not in here is the most important
9797
06:15:56,080 --> 06:15:58,000
one and that is the logic for the
9798
06:15:58,000 --> 06:16:00,160
recommended
9799
06:16:00,160 --> 06:16:01,520
posts
9800
06:16:01,520 --> 06:16:03,600
we're going to code that together
9801
06:16:03,600 --> 06:16:05,920
but before we do that we have to create
9802
06:16:05,920 --> 06:16:09,040
the data for getting a single post
9803
06:16:09,040 --> 06:16:11,360
if you remember correctly so far we've
9804
06:16:11,360 --> 06:16:14,160
only worked with posts plural now we
9805
06:16:14,160 --> 06:16:16,400
have to create the logic for fetching
9806
06:16:16,400 --> 06:16:20,240
only a single post based on the id
9807
06:16:20,240 --> 06:16:22,558
so let's start with that we first of
9808
06:16:22,558 --> 06:16:25,200
course have to dispatch some actions
9809
06:16:25,200 --> 06:16:27,120
we're going to do it inside of the use
9810
06:16:27,120 --> 06:16:28,400
effect
9811
06:16:28,400 --> 06:16:30,878
and that use effect is going to happen
9812
06:16:30,878 --> 06:16:34,480
whenever the id of the post changes
9813
06:16:34,480 --> 06:16:36,798
right inside of there we can dispatch an
9814
06:16:36,798 --> 06:16:37,760
action
9815
06:16:37,760 --> 06:16:39,600
and the action we're going to dispatch
9816
06:16:39,600 --> 06:16:43,440
is going to be called get post action
9817
06:16:43,440 --> 06:16:45,520
of course we want to get the post by
9818
06:16:45,520 --> 06:16:48,558
some specific id so we can pass that id
9819
06:16:48,558 --> 06:16:49,680
in there
9820
06:16:49,680 --> 06:16:52,400
as you can see our get post id is not
9821
06:16:52,400 --> 06:16:54,878
yet defined so let's import it we can
9822
06:16:54,878 --> 06:16:57,120
say import
9823
06:16:57,120 --> 06:16:59,680
get post and that is going to be coming
9824
06:16:59,680 --> 06:17:03,840
from dot dot slash dot slash actions
9825
06:17:03,840 --> 06:17:07,040
forward slash posts now is the time that
9826
06:17:07,040 --> 06:17:09,840
we create this action and make an api to
9827
06:17:09,840 --> 06:17:11,760
the backend that's going to serve us all
9828
06:17:11,760 --> 06:17:14,798
the details about our specific post
9829
06:17:14,798 --> 06:17:16,638
let's go to our posts
9830
06:17:16,638 --> 06:17:19,280
and more specifically actions
9831
06:17:19,280 --> 06:17:21,280
right inside of here we're going to
9832
06:17:21,280 --> 06:17:24,000
create one more action creator and we
9833
06:17:24,000 --> 06:17:26,638
can actually do that by copying the get
9834
06:17:26,638 --> 06:17:27,760
posts
9835
06:17:27,760 --> 06:17:29,920
i'm going to paste it at the top and
9836
06:17:29,920 --> 06:17:32,240
there are a few changes we have to make
9837
06:17:32,240 --> 06:17:34,080
first of all it's not going to be get
9838
06:17:34,080 --> 06:17:36,320
posts it's going to be get post
9839
06:17:36,320 --> 06:17:38,160
second thing is that we'll be getting an
9840
06:17:38,160 --> 06:17:41,440
id as a parameter and that id is the
9841
06:17:41,440 --> 06:17:44,400
thing who will pass into our api call
9842
06:17:44,400 --> 06:17:46,958
then the actual api call is not going to
9843
06:17:46,958 --> 06:17:49,520
be done to fetch posts rather it's going
9844
06:17:49,520 --> 06:17:52,080
to be to fetch post
9845
06:17:52,080 --> 06:17:54,558
and finally our type is not going to be
9846
06:17:54,558 --> 06:17:57,200
fetch all it's simply going to be fetch
9847
06:17:57,200 --> 06:18:00,480
post so let's go to our constants
9848
06:18:00,480 --> 06:18:02,958
and then right inside of here we can
9849
06:18:02,958 --> 06:18:05,600
create a fetch post
9850
06:18:05,600 --> 06:18:07,120
action constant
9851
06:18:07,120 --> 06:18:08,400
great
9852
06:18:08,400 --> 06:18:10,958
going back now we can import it fetch
9853
06:18:10,958 --> 06:18:13,360
post
9854
06:18:13,680 --> 06:18:16,480
and now everything is ready besides the
9855
06:18:16,480 --> 06:18:19,760
actual api endpoint so let's go to our
9856
06:18:19,760 --> 06:18:22,798
api index.js and then inside of here
9857
06:18:22,798 --> 06:18:25,440
we'll have to create the fetch post
9858
06:18:25,440 --> 06:18:27,840
that's going to be export const fetch
9859
06:18:27,840 --> 06:18:29,040
post
9860
06:18:29,040 --> 06:18:31,360
as we discussed we're getting the id as
9861
06:18:31,360 --> 06:18:34,160
a parameter and then we have to call the
9862
06:18:34,160 --> 06:18:38,080
api dot get and we have to navigate to
9863
06:18:38,080 --> 06:18:40,160
forward slash posts and then forward
9864
06:18:40,160 --> 06:18:42,160
slash id
9865
06:18:42,160 --> 06:18:44,000
now everything is ready and we have to
9866
06:18:44,000 --> 06:18:46,638
go back to our back inside and bring us
9867
06:18:46,638 --> 06:18:48,000
the data
9868
06:18:48,000 --> 06:18:50,000
so let's go to our server
9869
06:18:50,000 --> 06:18:53,360
more specifically routes and then posts
9870
06:18:53,360 --> 06:18:56,000
inside of here we have to create a route
9871
06:18:56,000 --> 06:18:58,878
for getting a single post we can do that
9872
06:18:58,878 --> 06:19:00,160
right in here
9873
06:19:00,160 --> 06:19:03,280
router dot get and the route is going to
9874
06:19:03,280 --> 06:19:04,400
be to
9875
06:19:04,400 --> 06:19:06,160
forward slash
9876
06:19:06,160 --> 06:19:07,680
column id
9877
06:19:07,680 --> 06:19:09,360
this means that it's going to be a
9878
06:19:09,360 --> 06:19:11,040
dynamic id
9879
06:19:11,040 --> 06:19:13,280
and what controller do we want to call
9880
06:19:13,280 --> 06:19:15,360
well we want to call the get post
9881
06:19:15,360 --> 06:19:17,600
controller of course we need to import
9882
06:19:17,600 --> 06:19:20,878
it at the top get post and finally we
9883
06:19:20,878 --> 06:19:22,878
have to create it under posts
9884
06:19:22,878 --> 06:19:24,160
controllers
9885
06:19:24,160 --> 06:19:26,478
if we scroll down you'll notice that we
9886
06:19:26,478 --> 06:19:30,080
already have a good get post controller
9887
06:19:30,080 --> 06:19:31,760
we created this in one of the previous
9888
06:19:31,760 --> 06:19:34,000
parts and we never actually used it but
9889
06:19:34,000 --> 06:19:36,080
now is a good time to do so
9890
06:19:36,080 --> 06:19:38,320
let's see if everything seems right
9891
06:19:38,320 --> 06:19:40,400
we're doing the const id from wreck
9892
06:19:40,400 --> 06:19:42,718
params and we're trying to find that
9893
06:19:42,718 --> 06:19:45,920
post by id and finally we have res that
9894
06:19:45,920 --> 06:19:48,558
status 200 and we're sending that post
9895
06:19:48,558 --> 06:19:51,520
back that seems all right to me
9896
06:19:51,520 --> 06:19:53,440
with that said let's go back to our
9897
06:19:53,440 --> 06:19:54,478
front-end side
9898
06:19:54,478 --> 06:19:56,718
to our actions
9899
06:19:56,718 --> 06:19:58,320
now we know that we are getting that
9900
06:19:58,320 --> 06:20:00,798
post back right here as the data and we
9901
06:20:00,798 --> 06:20:03,120
are sending it as the payload to our
9902
06:20:03,120 --> 06:20:04,718
reducers
9903
06:20:04,718 --> 06:20:07,760
so let's go to our reducers
9904
06:20:07,760 --> 06:20:10,958
post we have to import that action type
9905
06:20:10,958 --> 06:20:13,360
which is going to be fetch
9906
06:20:13,360 --> 06:20:14,400
post
9907
06:20:14,400 --> 06:20:16,638
our fetch post is going to be similar to
9908
06:20:16,638 --> 06:20:19,840
fetch by search so we can copy it
9909
06:20:19,840 --> 06:20:21,360
the only thing we have to change of
9910
06:20:21,360 --> 06:20:23,360
course is the case it's going to be
9911
06:20:23,360 --> 06:20:26,558
fetch post and then in here instead of
9912
06:20:26,558 --> 06:20:29,200
setting the posts to be action.payload
9913
06:20:29,200 --> 06:20:31,440
in this case we're getting a single post
9914
06:20:31,440 --> 06:20:33,920
so we're going to set it right in here
9915
06:20:33,920 --> 06:20:37,120
post is equal to action.payload
9916
06:20:37,120 --> 06:20:39,040
i know that we've written a lot of code
9917
06:20:39,040 --> 06:20:41,200
without actually seeing what's happening
9918
06:20:41,200 --> 06:20:43,840
but we've been dealing with data mostly
9919
06:20:43,840 --> 06:20:46,080
now we're going back to our post details
9920
06:20:46,080 --> 06:20:49,120
and see now we have this post right in
9921
06:20:49,120 --> 06:20:51,520
here if it says right there that we
9922
06:20:51,520 --> 06:20:53,680
don't have get posts make sure to close
9923
06:20:53,680 --> 06:20:56,160
the file and then reopen it and it's
9924
06:20:56,160 --> 06:20:57,920
going to be all good
9925
06:20:57,920 --> 06:21:00,000
looks like our posts are not currently
9926
06:21:00,000 --> 06:21:03,040
rendering let's open up the console
9927
06:21:03,040 --> 06:21:05,920
and it says that we have failed to get a
9928
06:21:05,920 --> 06:21:08,878
connection there is a network error so
9929
06:21:08,878 --> 06:21:11,120
let's go back to our code and open up
9930
06:21:11,120 --> 06:21:14,000
the console as you can see here it says
9931
06:21:14,000 --> 06:21:15,920
identifier getpost has already been
9932
06:21:15,920 --> 06:21:17,120
declared
9933
06:21:17,120 --> 06:21:20,240
and the error is in routes post js line
9934
06:21:20,240 --> 06:21:21,120
three
9935
06:21:21,120 --> 06:21:23,600
and indeed you might have noticed it but
9936
06:21:23,600 --> 06:21:25,440
we are already importing the get post
9937
06:21:25,440 --> 06:21:27,520
here and here so we don't have to do it
9938
06:21:27,520 --> 06:21:28,478
twice
9939
06:21:28,478 --> 06:21:31,120
i'm gonna save that and now we're gonna
9940
06:21:31,120 --> 06:21:33,760
go back to post details component
9941
06:21:33,760 --> 06:21:35,680
let's go back to the browser and see if
9942
06:21:35,680 --> 06:21:36,958
it works
9943
06:21:36,958 --> 06:21:40,400
okay our posts are loading right now and
9944
06:21:40,400 --> 06:21:42,160
take a look at that if you hover over
9945
06:21:42,160 --> 06:21:44,400
them you can click them
9946
06:21:44,400 --> 06:21:46,558
so let's try by clicking one memory and
9947
06:21:46,558 --> 06:21:49,280
see what happens
9948
06:21:49,280 --> 06:21:52,080
okay there is an error it says cannot
9949
06:21:52,080 --> 06:21:54,958
read property title of undefined this
9950
06:21:54,958 --> 06:21:56,320
usually means that we were trying to
9951
06:21:56,320 --> 06:21:58,400
render something before the data was
9952
06:21:58,400 --> 06:22:01,600
actually fetched so if we go back into
9953
06:22:01,600 --> 06:22:03,600
our post details we just have to add
9954
06:22:03,600 --> 06:22:05,920
some checks well first of all we're
9955
06:22:05,920 --> 06:22:09,280
going to say if there is no post then
9956
06:22:09,280 --> 06:22:12,160
return null and then second of all we're
9957
06:22:12,160 --> 06:22:15,120
going to add the loading state so if it
9958
06:22:15,120 --> 06:22:17,360
is loading in that case we want to
9959
06:22:17,360 --> 06:22:18,958
return
9960
06:22:18,958 --> 06:22:20,958
a paper component
9961
06:22:20,958 --> 06:22:22,478
that paper is going to have the
9962
06:22:22,478 --> 06:22:26,080
elevation equal to 6 and it's also going
9963
06:22:26,080 --> 06:22:28,400
to have a class name equal to
9964
06:22:28,400 --> 06:22:30,478
classes dot
9965
06:22:30,478 --> 06:22:32,558
loading paper
9966
06:22:32,558 --> 06:22:34,718
and finally inside of that we want to
9967
06:22:34,718 --> 06:22:38,240
have the circular progress component
9968
06:22:38,240 --> 06:22:40,558
it's going to be quite a big one so i'm
9969
06:22:40,558 --> 06:22:43,920
going to set the size to be equal to 7
9970
06:22:43,920 --> 06:22:45,360
em
9971
06:22:45,360 --> 06:22:47,680
we're gonna save that and now these two
9972
06:22:47,680 --> 06:22:49,840
checks are making sure that we don't try
9973
06:22:49,840 --> 06:22:52,558
to render this jsx where we depend on
9974
06:22:52,558 --> 06:22:54,478
the post if we don't have the post
9975
06:22:54,478 --> 06:22:55,520
already
9976
06:22:55,520 --> 06:22:57,600
if we now save this and go back to the
9977
06:22:57,600 --> 06:22:58,638
browser
9978
06:22:58,638 --> 06:23:02,400
you can see that our post opens up
9979
06:23:02,400 --> 06:23:04,558
it has a nice title we're listing the
9980
06:23:04,558 --> 06:23:07,200
tags right there description who created
9981
06:23:07,200 --> 06:23:10,080
the post and when the post was created i
9982
06:23:10,080 --> 06:23:12,000
also added some paragraphs here to let
9983
06:23:12,000 --> 06:23:13,840
you know that real time chat and
9984
06:23:13,840 --> 06:23:16,080
comments could be coming soon let me
9985
06:23:16,080 --> 06:23:18,080
know which one you'd like to see first i
9986
06:23:18,080 --> 06:23:19,840
would really like to create the fifth
9987
06:23:19,840 --> 06:23:21,920
part of the series if there is a lot of
9988
06:23:21,920 --> 06:23:24,878
demand for it from you guys with that
9989
06:23:24,878 --> 06:23:27,440
said let's check out our loading states
9990
06:23:27,440 --> 06:23:29,360
if we now go back you can see there is
9991
06:23:29,360 --> 06:23:31,440
loading right there and if i click on
9992
06:23:31,440 --> 06:23:33,440
one of these specific memories you can
9993
06:23:33,440 --> 06:23:35,920
see this nice loading at the center but
9994
06:23:35,920 --> 06:23:37,680
we can see it for less than half a
9995
06:23:37,680 --> 06:23:39,920
second that's because i have a really
9996
06:23:39,920 --> 06:23:42,320
quick internet connection but what would
9997
06:23:42,320 --> 06:23:44,000
happen if somebody is running this with
9998
06:23:44,000 --> 06:23:46,400
a slow internet connection well we can
9999
06:23:46,400 --> 06:23:48,400
actually check that out and that's one
10000
06:23:48,400 --> 06:23:50,558
more trick i want to show you you can
10001
06:23:50,558 --> 06:23:52,400
open up your developer tools
10002
06:23:52,400 --> 06:23:54,160
go to the network tab
10003
06:23:54,160 --> 06:23:55,680
and then there you'll see the no
10004
06:23:55,680 --> 06:23:57,920
throttling thing you want to switch that
10005
06:23:57,920 --> 06:23:59,520
to fast3g
10006
06:23:59,520 --> 06:24:01,840
now this is going to emulate a slower
10007
06:24:01,840 --> 06:24:04,320
internet connection so let's go to
10008
06:24:04,320 --> 06:24:05,680
memories
10009
06:24:05,680 --> 06:24:07,840
and see how nice it is to have a visual
10010
06:24:07,840 --> 06:24:11,280
representation that something is loading
10011
06:24:11,280 --> 06:24:13,520
there we go our posts are there and
10012
06:24:13,520 --> 06:24:17,200
let's try to load a specific memory
10013
06:24:17,200 --> 06:24:19,680
this loading looked really good
10014
06:24:19,680 --> 06:24:21,120
now let's bring this back to no
10015
06:24:21,120 --> 06:24:23,680
throttling and let's close it and let's
10016
06:24:23,680 --> 06:24:25,840
for a moment admire this nice memory
10017
06:24:25,840 --> 06:24:29,040
details component and the prague hassle
10018
06:24:29,040 --> 06:24:30,878
once we're done with that we can focus
10019
06:24:30,878 --> 06:24:33,200
on the most important part of our memory
10020
06:24:33,200 --> 06:24:34,400
details component
10021
06:24:34,400 --> 06:24:36,400
and that is the recommended posts
10022
06:24:36,400 --> 06:24:38,878
section let's code that out together
10023
06:24:38,878 --> 06:24:40,240
right now
10024
06:24:40,240 --> 06:24:42,400
let's code out the recommended posts
10025
06:24:42,400 --> 06:24:45,120
part i'm going to close all the tabs
10026
06:24:45,120 --> 06:24:47,520
besides the post details which we're
10027
06:24:47,520 --> 06:24:49,440
going to have open
10028
06:24:49,440 --> 06:24:51,520
right here and i'm going to collapse it
10029
06:24:51,520 --> 06:24:53,760
and close the file explorer just so we
10030
06:24:53,760 --> 06:24:55,840
have more space to work with
10031
06:24:55,840 --> 06:24:57,280
the interesting thing about the
10032
06:24:57,280 --> 06:24:59,120
recommended posts is that we're going to
10033
06:24:59,120 --> 06:25:02,000
use the same endpoint we created before
10034
06:25:02,000 --> 06:25:05,360
get posts by search so i'm going to
10035
06:25:05,360 --> 06:25:08,240
create one more use effect here
10036
06:25:08,240 --> 06:25:09,920
and yes you can have multiple user
10037
06:25:09,920 --> 06:25:12,638
effects per functional component
10038
06:25:12,638 --> 06:25:14,320
inside of there we're going to have a
10039
06:25:14,320 --> 06:25:16,240
dependency array that's going to have
10040
06:25:16,240 --> 06:25:17,360
the post
10041
06:25:17,360 --> 06:25:20,000
so whenever the id changes the post
10042
06:25:20,000 --> 06:25:22,798
changes and then we want to do something
10043
06:25:22,798 --> 06:25:25,760
for each post we want to see if the post
10044
06:25:25,760 --> 06:25:27,040
exists
10045
06:25:27,040 --> 06:25:30,400
and if it does we want to dispatch a new
10046
06:25:30,400 --> 06:25:31,920
action creator
10047
06:25:31,920 --> 06:25:34,400
that action creator is not going to be
10048
06:25:34,400 --> 06:25:37,680
get post anymore it is going to be
10049
06:25:37,680 --> 06:25:38,478
get
10050
06:25:38,478 --> 06:25:42,000
posts by search the one we created
10051
06:25:42,000 --> 06:25:43,120
before
10052
06:25:43,120 --> 06:25:46,478
so we can say right here dispatch get
10053
06:25:46,478 --> 06:25:48,160
post by search
10054
06:25:48,160 --> 06:25:49,920
and finally we have to provide that
10055
06:25:49,920 --> 06:25:52,240
search query in there so we can say
10056
06:25:52,240 --> 06:25:54,080
something like search is going to be
10057
06:25:54,080 --> 06:25:56,478
equal to none because we're not looking
10058
06:25:56,478 --> 06:25:59,200
for search we're looking only for tags
10059
06:25:59,200 --> 06:26:01,120
that's what we're gonna use to recommend
10060
06:26:01,120 --> 06:26:04,798
the posts so we can say tags is equal to
10061
06:26:04,798 --> 06:26:07,840
post question mark dot tags
10062
06:26:07,840 --> 06:26:11,040
dot join and we want to join it by a
10063
06:26:11,040 --> 06:26:13,920
comma now if we save that this is going
10064
06:26:13,920 --> 06:26:17,040
to populate the posts at the top
10065
06:26:17,040 --> 06:26:19,520
finally we can use those posts to create
10066
06:26:19,520 --> 06:26:22,320
a list of recommended posts
10067
06:26:22,320 --> 06:26:24,478
const recommended
10068
06:26:24,478 --> 06:26:27,760
posts is going to be equal to posts dot
10069
06:26:27,760 --> 06:26:28,718
filter
10070
06:26:28,718 --> 06:26:30,478
inside of there we want to get our
10071
06:26:30,478 --> 06:26:32,958
specific post but more specifically we
10072
06:26:32,958 --> 06:26:35,440
want to destructure the underscore id
10073
06:26:35,440 --> 06:26:36,718
from there
10074
06:26:36,718 --> 06:26:39,520
and then we want to keep all the posts
10075
06:26:39,520 --> 06:26:42,000
but delete the one where the underscore
10076
06:26:42,000 --> 06:26:45,200
id is equal to to the current post that
10077
06:26:45,200 --> 06:26:46,718
underscore id
10078
06:26:46,718 --> 06:26:49,040
of course the current post cannot be in
10079
06:26:49,040 --> 06:26:51,680
its own recommended posts that's why we
10080
06:26:51,680 --> 06:26:53,120
are doing this
10081
06:26:53,120 --> 06:26:55,600
so in here we can say if we have
10082
06:26:55,600 --> 06:26:58,000
recommended posts so if recommended
10083
06:26:58,000 --> 06:27:01,120
posts that length if that is the case
10084
06:27:01,120 --> 06:27:03,760
then we want to display something
10085
06:27:03,760 --> 06:27:05,520
that something we want to display is
10086
06:27:05,520 --> 06:27:08,718
going to be a div with a class name
10087
06:27:08,718 --> 06:27:10,320
equal to
10088
06:27:10,320 --> 06:27:13,280
classes dot section
10089
06:27:13,280 --> 06:27:15,280
inside of that div we're gonna have a
10090
06:27:15,280 --> 06:27:17,600
typography
10091
06:27:17,600 --> 06:27:20,080
and that typography is going to have a
10092
06:27:20,080 --> 06:27:22,718
gutter bottom to give it some margin at
10093
06:27:22,718 --> 06:27:24,320
the bottom it's going to have the
10094
06:27:24,320 --> 06:27:27,920
variant of h5 and then inside of there
10095
06:27:27,920 --> 06:27:30,798
we can say you might
10096
06:27:30,798 --> 06:27:33,840
also like finally below that we're going
10097
06:27:33,840 --> 06:27:36,000
to have a divider
10098
06:27:36,000 --> 06:27:38,160
and below that divider we're gonna have
10099
06:27:38,160 --> 06:27:39,520
a div
10100
06:27:39,520 --> 06:27:42,000
and that div is going to have a class
10101
06:27:42,000 --> 06:27:46,080
name equal to classes dot recommended
10102
06:27:46,080 --> 06:27:47,680
posts
10103
06:27:47,680 --> 06:27:49,760
inside of that div we want to open a
10104
06:27:49,760 --> 06:27:52,160
dynamic block and we want to map over
10105
06:27:52,160 --> 06:27:53,840
the recommended posts by doing
10106
06:27:53,840 --> 06:27:56,638
recommendedposts.map
10107
06:27:56,638 --> 06:27:58,558
inside of that div we want to open a
10108
06:27:58,558 --> 06:28:00,000
dynamic block and say
10109
06:28:00,000 --> 06:28:02,478
recommendedposts.map
10110
06:28:02,478 --> 06:28:05,200
so we want to get a post in there
10111
06:28:05,200 --> 06:28:07,200
and we can immediately destructure all
10112
06:28:07,200 --> 06:28:09,520
the things we need from the post like
10113
06:28:09,520 --> 06:28:12,558
title we also need a message
10114
06:28:12,558 --> 06:28:14,478
we need a name
10115
06:28:14,478 --> 06:28:17,280
the number of likes and the selected
10116
06:28:17,280 --> 06:28:20,558
file as well as the underscore id
10117
06:28:20,558 --> 06:28:22,400
now with all of those things we want to
10118
06:28:22,400 --> 06:28:24,320
return something so we just use a pair
10119
06:28:24,320 --> 06:28:26,080
of parentheses
10120
06:28:26,080 --> 06:28:27,920
and the thing we want to return is going
10121
06:28:27,920 --> 06:28:30,160
to be another div
10122
06:28:30,160 --> 06:28:31,680
now let's see how this is going to look
10123
06:28:31,680 --> 06:28:33,760
like if we just try to render out the
10124
06:28:33,760 --> 06:28:34,798
title
10125
06:28:34,798 --> 06:28:37,280
i'm going to save it
10126
06:28:37,280 --> 06:28:39,200
and let's go to a specific memory
10127
06:28:39,200 --> 06:28:41,760
details
10128
06:28:42,558 --> 06:28:44,638
and if we scroll down we can see you
10129
06:28:44,638 --> 06:28:46,958
might also like this
10130
06:28:46,958 --> 06:28:50,320
let's see if we have something different
10131
06:28:50,320 --> 06:28:51,600
in here
10132
06:28:51,600 --> 06:28:53,440
it only gives us the pose that we're
10133
06:28:53,440 --> 06:28:55,440
currently on which is not a good thing
10134
06:28:55,440 --> 06:28:58,000
we need to get all the other posts so i
10135
06:28:58,000 --> 06:29:00,400
might have made a logical mistake in
10136
06:29:00,400 --> 06:29:03,200
here we need to have a not equal to
10137
06:29:03,200 --> 06:29:05,440
because we want to filter out only the
10138
06:29:05,440 --> 06:29:07,760
one that is equal to and we want to keep
10139
06:29:07,760 --> 06:29:11,680
all the other ones so now if we go back
10140
06:29:11,680 --> 06:29:13,040
as you can see
10141
06:29:13,040 --> 06:29:14,958
now we get all the posts that have
10142
06:29:14,958 --> 06:29:17,360
something to do with europe that's good
10143
06:29:17,360 --> 06:29:19,600
but let's make them into a nicely styled
10144
06:29:19,600 --> 06:29:21,680
cards
10145
06:29:21,680 --> 06:29:23,440
this div is going to have a style
10146
06:29:23,440 --> 06:29:24,718
property
10147
06:29:24,718 --> 06:29:25,920
inside of there we're going to have an
10148
06:29:25,920 --> 06:29:30,638
object with a margin of 20 pixels
10149
06:29:30,638 --> 06:29:33,680
and we're also going to give it a cursor
10150
06:29:33,680 --> 06:29:36,320
which is equal to pointer that means
10151
06:29:36,320 --> 06:29:37,840
that it's going to show a pointer once
10152
06:29:37,840 --> 06:29:40,160
we hover over it
10153
06:29:40,160 --> 06:29:42,160
finally we need to have an on click
10154
06:29:42,160 --> 06:29:44,000
function what's going to happen once we
10155
06:29:44,000 --> 06:29:46,798
click it and on click we simply want to
10156
06:29:46,798 --> 06:29:48,878
open up the post
10157
06:29:48,878 --> 06:29:50,878
so i'm going to put a callback function
10158
06:29:50,878 --> 06:29:53,440
right here and we want to call the open
10159
06:29:53,440 --> 06:29:56,718
post function and pass it the underscore
10160
06:29:56,718 --> 06:29:59,120
id of the current post
10161
06:29:59,120 --> 06:30:01,680
finally we also need to specify a key
10162
06:30:01,680 --> 06:30:05,200
and the key is going to be underscore id
10163
06:30:05,200 --> 06:30:07,200
of course we don't yet have this open
10164
06:30:07,200 --> 06:30:09,200
post function but we're going to create
10165
06:30:09,200 --> 06:30:10,558
it right now
10166
06:30:10,558 --> 06:30:13,680
so let's go to the top
10167
06:30:13,680 --> 06:30:16,958
and let's create it const open post is
10168
06:30:16,958 --> 06:30:18,878
going to be equal to we're getting in
10169
06:30:18,878 --> 06:30:20,160
the id
10170
06:30:20,160 --> 06:30:22,160
and what do we want to do with that
10171
06:30:22,160 --> 06:30:23,680
specific id
10172
06:30:23,680 --> 06:30:26,240
well we simply want to push to it so we
10173
06:30:26,240 --> 06:30:28,878
can use the history dot push
10174
06:30:28,878 --> 06:30:30,718
and we want to push to forward slash
10175
06:30:30,718 --> 06:30:33,360
post forward slash and then to that
10176
06:30:33,360 --> 06:30:36,638
specific dynamic id i see the id is
10177
06:30:36,638 --> 06:30:38,320
being used currently so i'm going to use
10178
06:30:38,320 --> 06:30:41,840
the underscore id here as well
10179
06:30:41,840 --> 06:30:44,160
now if we save that let's see if that's
10180
06:30:44,160 --> 06:30:46,400
going to work
10181
06:30:46,400 --> 06:30:48,160
we're currently on dubrovnik we can see
10182
06:30:48,160 --> 06:30:50,240
that we added some margin and let's go
10183
06:30:50,240 --> 06:30:53,840
to for example the coliseum
10184
06:30:53,920 --> 06:30:55,680
that's great it gives you the feeling
10185
06:30:55,680 --> 06:30:57,520
that you can browse through the page and
10186
06:30:57,520 --> 06:30:59,120
see all the different things that are
10187
06:30:59,120 --> 06:31:00,240
available
10188
06:31:00,240 --> 06:31:02,798
great now let's style those cards a bit
10189
06:31:02,798 --> 06:31:04,798
more and show some more details with
10190
06:31:04,798 --> 06:31:07,200
each one of them instead of displaying
10191
06:31:07,200 --> 06:31:09,200
simply the title we're going to display
10192
06:31:09,200 --> 06:31:11,680
a typography component
10193
06:31:11,680 --> 06:31:14,080
that typography is going to have gutter
10194
06:31:14,080 --> 06:31:15,760
bottom
10195
06:31:15,760 --> 06:31:18,958
it's also going to have variant equal to
10196
06:31:18,958 --> 06:31:20,718
h6
10197
06:31:20,718 --> 06:31:22,718
and in there we're simply going to
10198
06:31:22,718 --> 06:31:24,638
render our title
10199
06:31:24,638 --> 06:31:27,440
now i'm going to copy this one four more
10200
06:31:27,440 --> 06:31:29,760
times or rather three more times
10201
06:31:29,760 --> 06:31:32,080
the first one is going to be h6 because
10202
06:31:32,080 --> 06:31:33,520
it's the title
10203
06:31:33,520 --> 06:31:35,520
and the second one is going to be of a
10204
06:31:35,520 --> 06:31:36,718
variant
10205
06:31:36,718 --> 06:31:40,080
subtitle 2 and it's going to render our
10206
06:31:40,080 --> 06:31:41,200
name
10207
06:31:41,200 --> 06:31:43,760
then we're going to have the message
10208
06:31:43,760 --> 06:31:44,718
below
10209
06:31:44,718 --> 06:31:48,638
and message is also going to be subtitle
10210
06:31:48,638 --> 06:31:49,520
2
10211
06:31:49,520 --> 06:31:52,160
and finally we're going to have the last
10212
06:31:52,160 --> 06:31:53,920
one which is the likes and that's going
10213
06:31:53,920 --> 06:31:56,958
to be subtitle 1 and we can just add in
10214
06:31:56,958 --> 06:32:00,320
front of it likes is equal to and then
10215
06:32:00,320 --> 06:32:04,000
we can say likes dot length
10216
06:32:04,000 --> 06:32:05,600
and let's not forget about the most
10217
06:32:05,600 --> 06:32:07,840
important part and that is the image so
10218
06:32:07,840 --> 06:32:10,638
we can add a self closing image tag
10219
06:32:10,638 --> 06:32:12,638
right here the image is going to have
10220
06:32:12,638 --> 06:32:15,760
the source equal to selected file and
10221
06:32:15,760 --> 06:32:17,760
it's also going to have the width equal
10222
06:32:17,760 --> 06:32:19,760
to 200 pixels
10223
06:32:19,760 --> 06:32:21,760
if i now save this let's take a look in
10224
06:32:21,760 --> 06:32:23,600
the browser
10225
06:32:23,600 --> 06:32:26,638
that looks so much better as you can see
10226
06:32:26,638 --> 06:32:29,040
we can see all of the posts recommended
10227
06:32:29,040 --> 06:32:30,320
right there that have something to do
10228
06:32:30,320 --> 06:32:32,878
with europe but let's go back and let's
10229
06:32:32,878 --> 06:32:35,520
see if we go to niagara falls if we can
10230
06:32:35,520 --> 06:32:37,840
see some different recommended posts now
10231
06:32:37,840 --> 06:32:40,320
we have the statue of liberty and grand
10232
06:32:40,320 --> 06:32:41,520
canyon
10233
06:32:41,520 --> 06:32:43,760
let's check out the statue of liberty
10234
06:32:43,760 --> 06:32:46,160
looks great we can see all the info the
10235
06:32:46,160 --> 06:32:48,558
image on the right and we have the idea
10236
06:32:48,558 --> 06:32:50,240
that we can actually navigate through
10237
06:32:50,240 --> 06:32:52,558
the page
10238
06:32:52,558 --> 06:32:54,878
with recommended posts being done our
10239
06:32:54,878 --> 06:32:57,440
whole memory details page is done as
10240
06:32:57,440 --> 06:32:59,440
well
10241
06:32:59,440 --> 06:33:01,040
with all of the changes that they've
10242
06:33:01,040 --> 06:33:03,120
made there are also some things that are
10243
06:33:03,120 --> 06:33:05,200
not working right now because of the way
10244
06:33:05,200 --> 06:33:08,240
how we structure the data in our redux
10245
06:33:08,240 --> 06:33:11,200
for example before as you know the posts
10246
06:33:11,200 --> 06:33:14,240
were simply an array of posts but right
10247
06:33:14,240 --> 06:33:16,400
now we're getting an object where then
10248
06:33:16,400 --> 06:33:19,760
we have the array of posts so there are
10249
06:33:19,760 --> 06:33:22,718
a few places where we have to fix that
10250
06:33:22,718 --> 06:33:24,478
one of those places is going to be the
10251
06:33:24,478 --> 06:33:25,520
form
10252
06:33:25,520 --> 06:33:27,920
formjs component and then just at the
10253
06:33:27,920 --> 06:33:32,400
top see here state dot post dot find in
10254
06:33:32,400 --> 06:33:34,320
this case it's going to be state dot
10255
06:33:34,320 --> 06:33:37,760
posts and one more time dot posts so you
10256
06:33:37,760 --> 06:33:40,798
can find a specific post
10257
06:33:40,798 --> 06:33:42,638
now let's go back to our app and let's
10258
06:33:42,638 --> 06:33:46,000
see if all the functionality still works
10259
06:33:46,000 --> 06:33:48,638
let's search for something like let's do
10260
06:33:48,638 --> 06:33:51,120
sydney
10261
06:33:52,400 --> 06:33:56,240
that works let's search for something
10262
06:33:56,240 --> 06:33:59,360
like let's do usa
10263
06:33:59,360 --> 06:34:01,120
that works as well
10264
06:34:01,120 --> 06:34:04,240
let's see if the pagination works
10265
06:34:04,240 --> 06:34:07,920
it works perfectly that is always great
10266
06:34:07,920 --> 06:34:10,000
and let's try to sign in to see what
10267
06:34:10,000 --> 06:34:11,600
else can we do
10268
06:34:11,600 --> 06:34:13,520
i've just signed in let's see if we can
10269
06:34:13,520 --> 06:34:15,520
like the post
10270
06:34:15,520 --> 06:34:18,240
we can so that's good there we go we
10271
06:34:18,240 --> 06:34:20,000
liked a few posts the liking
10272
06:34:20,000 --> 06:34:22,400
functionality still works let's try
10273
06:34:22,400 --> 06:34:25,120
creating a new memory let's go with test
10274
06:34:25,120 --> 06:34:29,360
test test and i'm going to add a file
10275
06:34:29,360 --> 06:34:33,440
the file is there and if we click submit
10276
06:34:33,440 --> 06:34:35,600
it's just going to stand there
10277
06:34:35,600 --> 06:34:38,160
we have to use history.push to navigate
10278
06:34:38,160 --> 06:34:42,240
it to that newly created memory detail
10279
06:34:42,240 --> 06:34:44,718
we can do that by going to form
10280
06:34:44,718 --> 06:34:46,878
finding where we are actually creating
10281
06:34:46,878 --> 06:34:49,120
that post and then just after that
10282
06:34:49,120 --> 06:34:53,520
dispatch we want to use history dot push
10283
06:34:53,520 --> 06:34:55,120
it's going to be pushed like this and
10284
06:34:55,120 --> 06:34:56,958
looks like we are not importing it so
10285
06:34:56,958 --> 06:35:00,718
just at the top we have to import
10286
06:35:00,718 --> 06:35:02,478
use history
10287
06:35:02,478 --> 06:35:05,840
from react router dom
10288
06:35:05,840 --> 06:35:07,680
and you know the drill we have to say
10289
06:35:07,680 --> 06:35:11,200
cons history is equal to use history and
10290
06:35:11,200 --> 06:35:14,080
finally history dot push forward slash
10291
06:35:14,080 --> 06:35:15,120
posts
10292
06:35:15,120 --> 06:35:17,440
where are we going to get the id well we
10293
06:35:17,440 --> 06:35:19,520
cannot get it in here we have to get it
10294
06:35:19,520 --> 06:35:22,400
just after we created this specific post
10295
06:35:22,400 --> 06:35:25,600
so what we can do is remove this
10296
06:35:25,600 --> 06:35:28,320
and not call the history.push here
10297
06:35:28,320 --> 06:35:31,840
rather pass one more parameter to the
10298
06:35:31,840 --> 06:35:35,120
create post action reducer we're passing
10299
06:35:35,120 --> 06:35:37,840
the history object we can go into our
10300
06:35:37,840 --> 06:35:39,440
action creator
10301
06:35:39,440 --> 06:35:42,080
accept that history object and then
10302
06:35:42,080 --> 06:35:44,878
after the post is created what we can do
10303
06:35:44,878 --> 06:35:47,360
is we can say history dot push and then
10304
06:35:47,360 --> 06:35:50,718
in here we can use data dot underscore
10305
06:35:50,718 --> 06:35:51,760
id
10306
06:35:51,760 --> 06:35:54,558
that's going to re-navigate it for us
10307
06:35:54,558 --> 06:35:56,878
let's save everything
10308
06:35:56,878 --> 06:36:00,478
and let's create one more post test test
10309
06:36:00,478 --> 06:36:01,520
test
10310
06:36:01,520 --> 06:36:03,200
and i've added the image i'm going to
10311
06:36:03,200 --> 06:36:04,798
click submit
10312
06:36:04,798 --> 06:36:07,360
and there we go we are immediately
10313
06:36:07,360 --> 06:36:10,320
re-navigated to a newly created post and
10314
06:36:10,320 --> 06:36:12,558
if we go back you can see it right in
10315
06:36:12,558 --> 06:36:15,558
here
10316
06:36:16,160 --> 06:36:18,160
let's start implementing our comments
10317
06:36:18,160 --> 06:36:19,280
section
10318
06:36:19,280 --> 06:36:21,200
that's going to be inside of the client
10319
06:36:21,200 --> 06:36:24,558
folder inside of the source components
10320
06:36:24,558 --> 06:36:27,040
post details and then inside of here
10321
06:36:27,040 --> 06:36:29,760
we're going to create one more component
10322
06:36:29,760 --> 06:36:32,160
that component is going to be called
10323
06:36:32,160 --> 06:36:33,680
comment
10324
06:36:33,680 --> 06:36:36,160
section.jsx
10325
06:36:36,160 --> 06:36:38,160
this is a new component that contains
10326
06:36:38,160 --> 06:36:40,958
all the logic about comments
10327
06:36:40,958 --> 06:36:45,600
so let's import react from react and
10328
06:36:45,600 --> 06:36:48,400
we're also going to import a few hooks
10329
06:36:48,400 --> 06:36:52,080
these are going to be use state and also
10330
06:36:52,080 --> 06:36:54,478
use ref
10331
06:36:54,478 --> 06:36:56,478
right below we're going to import
10332
06:36:56,478 --> 06:36:58,958
everything we need from material ui and
10333
06:36:58,958 --> 06:37:02,400
that's going to be typography
10334
06:37:02,400 --> 06:37:05,440
we also need the text field
10335
06:37:05,440 --> 06:37:08,000
and finally we need a button component
10336
06:37:08,000 --> 06:37:10,478
that's coming from add material ui
10337
06:37:10,478 --> 06:37:12,638
forward slash core
10338
06:37:12,638 --> 06:37:15,120
also we're going to use the use dispatch
10339
06:37:15,120 --> 06:37:15,920
hook
10340
06:37:15,920 --> 06:37:18,240
belonging to redux so we can see that's
10341
06:37:18,240 --> 06:37:22,160
coming from react dash redux
10342
06:37:22,160 --> 06:37:24,080
and we're also going to use some styles
10343
06:37:24,080 --> 06:37:27,040
so we can say import use styles and
10344
06:37:27,040 --> 06:37:30,638
that's coming from dot slash styles
10345
06:37:30,638 --> 06:37:32,478
great now we have the majority of
10346
06:37:32,478 --> 06:37:34,240
imports that we have to do and let's
10347
06:37:34,240 --> 06:37:35,840
create our component
10348
06:37:35,840 --> 06:37:38,718
const comment section and that's going
10349
06:37:38,718 --> 06:37:41,040
to be equal to a functional react
10350
06:37:41,040 --> 06:37:42,400
component
10351
06:37:42,400 --> 06:37:44,320
inside of there for now let's simply
10352
06:37:44,320 --> 06:37:47,840
console.log something like comment
10353
06:37:47,840 --> 06:37:48,958
section
10354
06:37:48,958 --> 06:37:51,600
and also let's simply return
10355
06:37:51,600 --> 06:37:53,840
an h1 just so we can see something on
10356
06:37:53,840 --> 06:37:56,558
the page for now
10357
06:37:57,200 --> 06:38:00,400
great of course we have to do export
10358
06:38:00,400 --> 06:38:01,600
default
10359
06:38:01,600 --> 06:38:04,478
comment section
10360
06:38:04,478 --> 06:38:06,478
of course we have to use this comment
10361
06:38:06,478 --> 06:38:09,040
section somewhere and that's going to be
10362
06:38:09,040 --> 06:38:11,440
inside of the post details when we open
10363
06:38:11,440 --> 06:38:14,400
more details about a specific memory
10364
06:38:14,400 --> 06:38:16,478
so in here at the top
10365
06:38:16,478 --> 06:38:18,840
let's import comment
10366
06:38:18,840 --> 06:38:20,638
section
10367
06:38:20,638 --> 06:38:22,798
from dot slash
10368
06:38:22,798 --> 06:38:26,478
comment section just like this
10369
06:38:26,478 --> 06:38:27,920
and where are we going to render that
10370
06:38:27,920 --> 06:38:30,400
component well the last time we even
10371
06:38:30,400 --> 06:38:32,718
left a typography saying where we need
10372
06:38:32,718 --> 06:38:36,160
to put it comments coming soon so let's
10373
06:38:36,160 --> 06:38:38,478
simply delete that and then instead of
10374
06:38:38,478 --> 06:38:40,638
that typography we're going to render
10375
06:38:40,638 --> 06:38:43,840
out our comment section
10376
06:38:43,840 --> 06:38:46,080
it is going to be self-closing component
10377
06:38:46,080 --> 06:38:48,878
and let's pass one prop to it and that
10378
06:38:48,878 --> 06:38:51,120
prop is going to be the data for the
10379
06:38:51,120 --> 06:38:53,360
specific post we're currently on we're
10380
06:38:53,360 --> 06:38:55,200
going to use that inside of the comment
10381
06:38:55,200 --> 06:38:57,600
section
10382
06:38:58,320 --> 06:39:00,478
so let's go back to our comment section
10383
06:39:00,478 --> 06:39:02,958
and let's immediately this structure our
10384
06:39:02,958 --> 06:39:05,280
post data just so we know what's coming
10385
06:39:05,280 --> 06:39:07,840
through there and maybe instead of this
10386
06:39:07,840 --> 06:39:09,920
comment section let's simply console log
10387
06:39:09,920 --> 06:39:11,360
the post
10388
06:39:11,360 --> 06:39:13,280
great we already have enough code that
10389
06:39:13,280 --> 06:39:15,680
we can see it on the website
10390
06:39:15,680 --> 06:39:19,120
let's open up a specific memory page
10391
06:39:19,120 --> 06:39:20,958
and then inside of here you can see that
10392
06:39:20,958 --> 06:39:23,760
we have a big comment section now let's
10393
06:39:23,760 --> 06:39:26,798
create the jsx for our comments
10394
06:39:26,798 --> 06:39:28,798
inside of that div we're going to have
10395
06:39:28,798 --> 06:39:31,440
yet another div and this div is going to
10396
06:39:31,440 --> 06:39:33,600
have a class name
10397
06:39:33,600 --> 06:39:37,760
equal to classes dot comments
10398
06:39:37,760 --> 06:39:38,878
outer
10399
06:39:38,878 --> 06:39:40,320
container
10400
06:39:40,320 --> 06:39:42,558
right now our classes are not defined
10401
06:39:42,558 --> 06:39:44,400
and to use the classes coming from the
10402
06:39:44,400 --> 06:39:47,120
used styles we can simply say collins
10403
06:39:47,120 --> 06:39:50,958
classes is equal to use styles and then
10404
06:39:50,958 --> 06:39:53,680
we call it as a hook
10405
06:39:53,680 --> 06:39:56,000
great now inside of this div we're going
10406
06:39:56,000 --> 06:39:59,360
to have yet another div and this div is
10407
06:39:59,360 --> 06:40:01,760
going to have a class name equal to
10408
06:40:01,760 --> 06:40:04,320
classes dot comments
10409
06:40:04,320 --> 06:40:05,200
inner
10410
06:40:05,200 --> 06:40:06,558
container
10411
06:40:06,558 --> 06:40:09,120
great now we have our layout
10412
06:40:09,120 --> 06:40:11,200
now inside of our inner container we're
10413
06:40:11,200 --> 06:40:14,320
going to have a typography component
10414
06:40:14,320 --> 06:40:16,558
and that typography is going to have a
10415
06:40:16,558 --> 06:40:19,120
prop of gutter bottom which is simply
10416
06:40:19,120 --> 06:40:21,360
going to give it some bottom margin
10417
06:40:21,360 --> 06:40:24,240
and it's going to be a variant equal to
10418
06:40:24,240 --> 06:40:25,680
h6
10419
06:40:25,680 --> 06:40:27,680
now inside of there we can simply say
10420
06:40:27,680 --> 06:40:29,360
something like comments
10421
06:40:29,360 --> 06:40:31,280
now we have to loop through the comments
10422
06:40:31,280 --> 06:40:34,798
of our specific post of course right now
10423
06:40:34,798 --> 06:40:36,320
none of the memories or none of the
10424
06:40:36,320 --> 06:40:38,718
posts have the actual comments because
10425
06:40:38,718 --> 06:40:40,478
we haven't implemented the feature of
10426
06:40:40,478 --> 06:40:43,280
adding them yet but let's create the jsx
10427
06:40:43,280 --> 06:40:45,360
code that's going to loop over them once
10428
06:40:45,360 --> 06:40:47,440
we have them populated
10429
06:40:47,440 --> 06:40:50,080
let's open a dynamic jsx block and then
10430
06:40:50,080 --> 06:40:51,360
in here we're going to loop over the
10431
06:40:51,360 --> 06:40:54,080
comments for now let's create a use
10432
06:40:54,080 --> 06:40:56,400
state field so const
10433
06:40:56,400 --> 06:40:58,958
comments and set
10434
06:40:58,958 --> 06:41:00,558
comments
10435
06:41:00,558 --> 06:41:02,878
and that is going to be equal to use
10436
06:41:02,878 --> 06:41:05,680
state for now in here we can leave
10437
06:41:05,680 --> 06:41:08,160
simply an empty array because we have no
10438
06:41:08,160 --> 06:41:09,600
comments yet
10439
06:41:09,600 --> 06:41:11,520
now that we have comments let's loop
10440
06:41:11,520 --> 06:41:14,320
over them comments dot map
10441
06:41:14,320 --> 06:41:16,240
in here we're gonna get a comment i'm
10442
06:41:16,240 --> 06:41:18,558
gonna put c for short and also we're
10443
06:41:18,558 --> 06:41:20,240
gonna get the index
10444
06:41:20,240 --> 06:41:22,080
then we want to instantly return
10445
06:41:22,080 --> 06:41:24,878
something so don't put the curly braces
10446
06:41:24,878 --> 06:41:27,520
here but simply a parenthesis
10447
06:41:27,520 --> 06:41:29,520
now for each specific comment we're
10448
06:41:29,520 --> 06:41:32,320
going to show a typography
10449
06:41:32,320 --> 06:41:34,798
and that typography is going to have a
10450
06:41:34,798 --> 06:41:37,280
key equal to i
10451
06:41:37,280 --> 06:41:40,000
it's also going to have a gutter bottom
10452
06:41:40,000 --> 06:41:42,320
and it's going to have a variant equal
10453
06:41:42,320 --> 06:41:44,798
to subtitle 1.
10454
06:41:44,798 --> 06:41:47,520
for now we can simply say something like
10455
06:41:47,520 --> 06:41:49,920
comment in there or we can even make it
10456
06:41:49,920 --> 06:41:52,240
dynamic and say comment
10457
06:41:52,240 --> 06:41:54,958
i this is going to give it a number
10458
06:41:54,958 --> 06:41:57,360
now just so we can see this let's add
10459
06:41:57,360 --> 06:42:00,000
one two three four here that should
10460
06:42:00,000 --> 06:42:02,878
render out four demo comments i'm gonna
10461
06:42:02,878 --> 06:42:05,760
save that
10462
06:42:05,760 --> 06:42:08,080
and in the browser we can already see
10463
06:42:08,080 --> 06:42:09,920
our comments header and we can see
10464
06:42:09,920 --> 06:42:13,200
comment zero one two three so we're
10465
06:42:13,200 --> 06:42:14,958
already starting to create the layout
10466
06:42:14,958 --> 06:42:17,360
for our comments later on of course
10467
06:42:17,360 --> 06:42:18,958
we're going to have real comments in
10468
06:42:18,958 --> 06:42:21,520
here but for now let's keep moving
10469
06:42:21,520 --> 06:42:22,638
forward
10470
06:42:22,638 --> 06:42:24,558
below the comments in our div we're
10471
06:42:24,558 --> 06:42:26,718
going to have one more div
10472
06:42:26,718 --> 06:42:28,798
and this div is not going to have
10473
06:42:28,798 --> 06:42:30,558
classes it's simply going to have some
10474
06:42:30,558 --> 06:42:33,680
inline styles we can say style is equal
10475
06:42:33,680 --> 06:42:36,080
to width is equal to
10476
06:42:36,080 --> 06:42:37,200
a string
10477
06:42:37,200 --> 06:42:39,360
of 70 percent
10478
06:42:39,360 --> 06:42:41,280
inside of there we're going to have one
10479
06:42:41,280 --> 06:42:43,840
more typography we can even copy this
10480
06:42:43,840 --> 06:42:45,040
one
10481
06:42:45,040 --> 06:42:46,798
everything is going to be the same
10482
06:42:46,798 --> 06:42:49,520
gutter bottom variant h6 and we can
10483
06:42:49,520 --> 06:42:50,718
simply say
10484
06:42:50,718 --> 06:42:52,080
write
10485
06:42:52,080 --> 06:42:53,200
a
10486
06:42:53,200 --> 06:42:55,120
comment
10487
06:42:55,120 --> 06:42:57,680
now as we have our comments below our
10488
06:42:57,680 --> 06:42:59,360
comments typography we're going to have
10489
06:42:59,360 --> 06:43:01,840
the code to write a comment below this
10490
06:43:01,840 --> 06:43:03,040
typography
10491
06:43:03,040 --> 06:43:06,320
so to do that let's create a material ui
10492
06:43:06,320 --> 06:43:08,320
text field which is basically just an
10493
06:43:08,320 --> 06:43:09,440
input
10494
06:43:09,440 --> 06:43:12,000
it is a self-closing tag we can expand
10495
06:43:12,000 --> 06:43:14,400
it in multiple rows and let's give it a
10496
06:43:14,400 --> 06:43:15,760
few properties
10497
06:43:15,760 --> 06:43:18,638
the first one is going to be full width
10498
06:43:18,638 --> 06:43:20,718
this means that the text field is going
10499
06:43:20,718 --> 06:43:22,718
to span across the entire width of the
10500
06:43:22,718 --> 06:43:23,840
screen
10501
06:43:23,840 --> 06:43:25,840
then we're also going to have a property
10502
06:43:25,840 --> 06:43:28,000
called rows and we're going to set that
10503
06:43:28,000 --> 06:43:30,798
to 4. usually text fields are simple
10504
06:43:30,798 --> 06:43:33,040
fields for like name or last name or
10505
06:43:33,040 --> 06:43:34,400
something like that
10506
06:43:34,400 --> 06:43:36,558
but our comments can be longer and
10507
06:43:36,558 --> 06:43:39,360
that's why we can have multiple rows
10508
06:43:39,360 --> 06:43:41,440
our variant is going to be equal to
10509
06:43:41,440 --> 06:43:42,958
outlined
10510
06:43:42,958 --> 06:43:45,360
and we can also have a label which is
10511
06:43:45,360 --> 06:43:47,920
going to simply say comment
10512
06:43:47,920 --> 06:43:49,680
we are going to make our text field
10513
06:43:49,680 --> 06:43:52,320
multi-line and then in there we have to
10514
06:43:52,320 --> 06:43:54,798
keep track of the value of our specific
10515
06:43:54,798 --> 06:43:57,280
comment to keep track of the value of
10516
06:43:57,280 --> 06:43:59,440
our text field we have to create another
10517
06:43:59,440 --> 06:44:02,320
state so we can say const
10518
06:44:02,320 --> 06:44:04,798
comment now singular and then set
10519
06:44:04,798 --> 06:44:08,798
comment is equal to use state
10520
06:44:08,798 --> 06:44:10,400
in there we're going to pass an empty
10521
06:44:10,400 --> 06:44:12,160
string because we want to start with
10522
06:44:12,160 --> 06:44:13,920
empty string
10523
06:44:13,920 --> 06:44:16,160
now that we have that we can specify a
10524
06:44:16,160 --> 06:44:18,558
value and the value of this text field
10525
06:44:18,558 --> 06:44:20,718
is going to be a comment
10526
06:44:20,718 --> 06:44:22,798
then we can specify the onchange
10527
06:44:22,798 --> 06:44:25,200
property and to change something we have
10528
06:44:25,200 --> 06:44:27,760
to get the event like this
10529
06:44:27,760 --> 06:44:30,478
and we have to call the set comment and
10530
06:44:30,478 --> 06:44:34,840
more specifically event.target.value
10531
06:44:35,120 --> 06:44:36,718
that's going to set the value of our
10532
06:44:36,718 --> 06:44:39,280
text field let's save it and look at it
10533
06:44:39,280 --> 06:44:40,558
in the browser
10534
06:44:40,558 --> 06:44:42,400
okay that's already starting to look
10535
06:44:42,400 --> 06:44:44,478
just a bit better we have our comments
10536
06:44:44,478 --> 06:44:47,440
and we have our write a comment section
10537
06:44:47,440 --> 06:44:49,200
of course we still haven't implemented
10538
06:44:49,200 --> 06:44:51,040
the styles for our containers we're
10539
06:44:51,040 --> 06:44:52,798
going to do that really quickly now
10540
06:44:52,798 --> 06:44:54,558
let's add a button
10541
06:44:54,558 --> 06:44:56,718
below our text field i'm going to create
10542
06:44:56,718 --> 06:44:58,080
a button
10543
06:44:58,080 --> 06:44:59,920
that button is going to have a few
10544
06:44:59,920 --> 06:45:02,160
properties first it's going to have an
10545
06:45:02,160 --> 06:45:05,360
inline style equal to margin top and
10546
06:45:05,360 --> 06:45:07,680
then in there we can simply say 10
10547
06:45:07,680 --> 06:45:10,240
pixels that's going to separate it a bit
10548
06:45:10,240 --> 06:45:11,840
from the text field
10549
06:45:11,840 --> 06:45:14,400
then it's also going to be full
10550
06:45:14,400 --> 06:45:15,840
width
10551
06:45:15,840 --> 06:45:17,920
it's also going to be disabled so we can
10552
06:45:17,920 --> 06:45:20,478
say disabled and it's only going to be
10553
06:45:20,478 --> 06:45:22,798
disabled if we haven't yet typed in our
10554
06:45:22,798 --> 06:45:26,320
comment so we can say if no comment
10555
06:45:26,320 --> 06:45:27,760
our button is also going to have a
10556
06:45:27,760 --> 06:45:30,638
variant equal to contained and it's
10557
06:45:30,638 --> 06:45:32,638
going to have just one more property
10558
06:45:32,638 --> 06:45:35,360
which is the on click handler what do we
10559
06:45:35,360 --> 06:45:37,680
want to do on click well we want to call
10560
06:45:37,680 --> 06:45:40,478
a special handle click function which
10561
06:45:40,478 --> 06:45:42,638
we're going to create right now but
10562
06:45:42,638 --> 06:45:44,400
before we do that let's simply put
10563
06:45:44,400 --> 06:45:46,840
something in our button and that is
10564
06:45:46,840 --> 06:45:49,920
comment great so now we can see that we
10565
06:45:49,920 --> 06:45:51,600
are missing our handle click let's
10566
06:45:51,600 --> 06:45:53,680
create it at the top
10567
06:45:53,680 --> 06:45:55,600
for now we can simply leave it as a
10568
06:45:55,600 --> 06:45:57,120
blank function
10569
06:45:57,120 --> 06:46:00,320
const handle click is equal to an arrow
10570
06:46:00,320 --> 06:46:01,360
function
10571
06:46:01,360 --> 06:46:03,920
great now we finish the better part of
10572
06:46:03,920 --> 06:46:05,440
our comment section
10573
06:46:05,440 --> 06:46:07,600
let's implement our styles
10574
06:46:07,600 --> 06:46:09,440
to do that i'm going to open up the
10575
06:46:09,440 --> 06:46:11,120
styles.js
10576
06:46:11,120 --> 06:46:13,120
and we have to add just a few class
10577
06:46:13,120 --> 06:46:15,680
names below the loading paper i'm going
10578
06:46:15,680 --> 06:46:17,840
to add the comments
10579
06:46:17,840 --> 06:46:20,320
outer container
10580
06:46:20,320 --> 06:46:22,320
that is going to be equal to an object
10581
06:46:22,320 --> 06:46:24,320
and then in there we simply want to say
10582
06:46:24,320 --> 06:46:27,280
display is equal to flex and also
10583
06:46:27,280 --> 06:46:29,760
justify content is going to be equal to
10584
06:46:29,760 --> 06:46:32,240
space between
10585
06:46:32,240 --> 06:46:34,558
below our comments outer container we're
10586
06:46:34,558 --> 06:46:38,080
also going to have our comments
10587
06:46:38,080 --> 06:46:39,040
inner
10588
06:46:39,040 --> 06:46:40,478
container
10589
06:46:40,478 --> 06:46:42,638
and then in there we can simply specify
10590
06:46:42,638 --> 06:46:44,558
the height which is going to be equal to
10591
06:46:44,558 --> 06:46:46,080
200 pixels
10592
06:46:46,080 --> 06:46:49,200
we can also specify the overflow y which
10593
06:46:49,200 --> 06:46:51,280
is going to make our div scrollable so
10594
06:46:51,280 --> 06:46:53,120
we can set that to auto
10595
06:46:53,120 --> 06:46:55,840
and we can also give it a margin right
10596
06:46:55,840 --> 06:46:59,440
which is going to be equal to 30 pixels
10597
06:46:59,440 --> 06:47:01,680
great these are all the styles that we
10598
06:47:01,680 --> 06:47:04,000
need for now so let's go back to our
10599
06:47:04,000 --> 06:47:06,000
comment section and let's actually check
10600
06:47:06,000 --> 06:47:07,600
it out in the browser
10601
06:47:07,600 --> 06:47:10,240
that already looks so much better
10602
06:47:10,240 --> 06:47:12,400
technically we've finished the entire
10603
06:47:12,400 --> 06:47:14,000
frontend part of our comments
10604
06:47:14,000 --> 06:47:16,320
functionality now is the time that we
10605
06:47:16,320 --> 06:47:18,558
connect it to redux and then send that
10606
06:47:18,558 --> 06:47:21,360
data to the backend and finally create a
10607
06:47:21,360 --> 06:47:24,080
comment in the database so let's do that
10608
06:47:24,080 --> 06:47:25,680
right now
10609
06:47:25,680 --> 06:47:28,638
now let's grab this data from a specific
10610
06:47:28,638 --> 06:47:31,040
comment and when we click comment let's
10611
06:47:31,040 --> 06:47:32,798
submit it to redux
10612
06:47:32,798 --> 06:47:34,958
also i've just noticed that our button
10613
06:47:34,958 --> 06:47:37,680
is gray and it has to be blue
10614
06:47:37,680 --> 06:47:40,478
so let's change it right inside of here
10615
06:47:40,478 --> 06:47:42,718
we're going to give it a property called
10616
06:47:42,718 --> 06:47:46,400
color which is equal to primary
10617
06:47:46,400 --> 06:47:48,638
now if you type test you can see it has
10618
06:47:48,638 --> 06:47:50,718
this nice primary color
10619
06:47:50,718 --> 06:47:52,798
with that said you already know that we
10620
06:47:52,798 --> 06:47:55,200
created our handleclick function but we
10621
06:47:55,200 --> 06:47:57,520
have no code inside of there
10622
06:47:57,520 --> 06:48:00,878
so let's implement the code right here
10623
06:48:00,878 --> 06:48:02,638
when we click comment we have to
10624
06:48:02,638 --> 06:48:06,000
dispatch a new action to our redux
10625
06:48:06,000 --> 06:48:08,240
more specifically we can dispatch a
10626
06:48:08,240 --> 06:48:09,280
comment
10627
06:48:09,280 --> 06:48:11,680
post action
10628
06:48:11,680 --> 06:48:13,680
our comment also needs to contain the
10629
06:48:13,680 --> 06:48:16,080
information about who is creating that
10630
06:48:16,080 --> 06:48:19,120
specific comment so let's grab our user
10631
06:48:19,120 --> 06:48:22,000
from the local storage
10632
06:48:22,000 --> 06:48:24,478
i'm going to remove this console log and
10633
06:48:24,478 --> 06:48:27,040
i'm going to grab our user we can say
10634
06:48:27,040 --> 06:48:29,400
const user is equal to
10635
06:48:29,400 --> 06:48:31,040
json.parse
10636
06:48:31,040 --> 06:48:33,440
and then we can go inside of the local
10637
06:48:33,440 --> 06:48:37,600
storage and simply call get item user
10638
06:48:37,600 --> 06:48:39,600
this is going to populate our user from
10639
06:48:39,600 --> 06:48:41,680
the local storage we've already done
10640
06:48:41,680 --> 06:48:43,200
this a few times throughout our
10641
06:48:43,200 --> 06:48:45,120
application
10642
06:48:45,120 --> 06:48:47,120
now that we have the user's data let's
10643
06:48:47,120 --> 06:48:49,520
form our comment to include the user's
10644
06:48:49,520 --> 06:48:52,240
name and also the comment itself let's
10645
06:48:52,240 --> 06:48:56,000
say something like const final comment
10646
06:48:56,000 --> 06:48:57,920
and that is going to be equal to a
10647
06:48:57,920 --> 06:49:00,400
template string the first thing in there
10648
06:49:00,400 --> 06:49:04,080
is going to be user dot result dot name
10649
06:49:04,080 --> 06:49:06,400
and then we can say colon and then in
10650
06:49:06,400 --> 06:49:08,400
here we render the
10651
06:49:08,400 --> 06:49:09,760
comment
10652
06:49:09,760 --> 06:49:12,718
so just one more time we are getting the
10653
06:49:12,718 --> 06:49:14,718
user's name here and we're saying who
10654
06:49:14,718 --> 06:49:17,280
wrote that comment that user and what is
10655
06:49:17,280 --> 06:49:19,520
the context of the comment well it is
10656
06:49:19,520 --> 06:49:21,760
the actual state we are going to write
10657
06:49:21,760 --> 06:49:24,240
that in that specific text field right
10658
06:49:24,240 --> 06:49:26,240
in here
10659
06:49:26,240 --> 06:49:28,240
now that we have the contents of the
10660
06:49:28,240 --> 06:49:30,558
comment and we can put that here
10661
06:49:30,558 --> 06:49:32,400
final comment
10662
06:49:32,400 --> 06:49:34,718
and also as the second parameter we have
10663
06:49:34,718 --> 06:49:36,958
to say which post does this comment
10664
06:49:36,958 --> 06:49:39,840
belong to so let's add a comma and let's
10665
06:49:39,840 --> 06:49:43,440
say post dot underscore id
10666
06:49:43,440 --> 06:49:46,000
great but as you can see our dispatch is
10667
06:49:46,000 --> 06:49:48,798
currently not here and also our comment
10668
06:49:48,798 --> 06:49:52,478
post is not defined so let's fix that
10669
06:49:52,478 --> 06:49:54,638
first we can get the dispatch by saying
10670
06:49:54,638 --> 06:49:57,280
cons dispatch is equal to
10671
06:49:57,280 --> 06:49:59,680
use dispatch hook
10672
06:49:59,680 --> 06:50:02,798
but now we have to create that action
10673
06:50:02,798 --> 06:50:04,718
for now let's simply import it even
10674
06:50:04,718 --> 06:50:07,040
though it's not yet created we can say
10675
06:50:07,040 --> 06:50:08,478
import
10676
06:50:08,478 --> 06:50:10,478
comment post
10677
06:50:10,478 --> 06:50:12,958
and that's going to be imported from
10678
06:50:12,958 --> 06:50:15,360
dot slash dot dot slash
10679
06:50:15,360 --> 06:50:18,718
actions forward slash posts
10680
06:50:18,718 --> 06:50:21,440
great now let's create it right inside
10681
06:50:21,440 --> 06:50:24,320
of there we're gonna go to actions and
10682
06:50:24,320 --> 06:50:25,600
then posts
10683
06:50:25,600 --> 06:50:29,520
and right inside of here below the like
10684
06:50:29,520 --> 06:50:31,680
post we're going to implement the
10685
06:50:31,680 --> 06:50:33,040
comment post
10686
06:50:33,040 --> 06:50:35,360
so let's follow the same structure
10687
06:50:35,360 --> 06:50:36,478
export
10688
06:50:36,478 --> 06:50:38,558
const comment
10689
06:50:38,558 --> 06:50:39,920
post
10690
06:50:39,920 --> 06:50:41,840
and in this case we'll be getting two
10691
06:50:41,840 --> 06:50:44,240
different parameters the first one is
10692
06:50:44,240 --> 06:50:46,718
the value and the second one is the id
10693
06:50:46,718 --> 06:50:48,240
of the post
10694
06:50:48,240 --> 06:50:50,080
then considering that data is
10695
06:50:50,080 --> 06:50:52,400
asynchronous we have to use redux thunk
10696
06:50:52,400 --> 06:50:54,798
and it has this weird structure which is
10697
06:50:54,798 --> 06:50:58,240
the async this patch and then after that
10698
06:50:58,240 --> 06:51:01,920
one more function call like that
10699
06:51:01,920 --> 06:51:03,440
now how can you know that we are getting
10700
06:51:03,440 --> 06:51:06,000
the value and the id well we can know
10701
06:51:06,000 --> 06:51:08,000
that because in here that's exactly what
10702
06:51:08,000 --> 06:51:10,160
we're passing the comment including the
10703
06:51:10,160 --> 06:51:13,200
name and the comment and the id
10704
06:51:13,200 --> 06:51:16,000
great so let's create the actual comment
10705
06:51:16,000 --> 06:51:17,600
post action
10706
06:51:17,600 --> 06:51:19,280
we're going to create a try and catch
10707
06:51:19,280 --> 06:51:21,520
block as we always do
10708
06:51:21,520 --> 06:51:23,440
now we have to make an api call and
10709
06:51:23,440 --> 06:51:25,600
that's going to look something like this
10710
06:51:25,600 --> 06:51:29,600
await api dot comment and then in there
10711
06:51:29,600 --> 06:51:33,280
we're going to pass the value and the id
10712
06:51:33,280 --> 06:51:35,600
of course we don't yet have that api
10713
06:51:35,600 --> 06:51:37,840
comment as we have the like post or
10714
06:51:37,840 --> 06:51:40,638
delete post or all other so we have to
10715
06:51:40,638 --> 06:51:43,520
create that and let's do that right away
10716
06:51:43,520 --> 06:51:47,200
let's go to our api and then index.js
10717
06:51:47,200 --> 06:51:49,760
inside of here we can even copy the
10718
06:51:49,760 --> 06:51:52,478
entire like post just above
10719
06:51:52,478 --> 06:51:54,718
now let's change it we're going to
10720
06:51:54,718 --> 06:51:57,040
change this to comment
10721
06:51:57,040 --> 06:51:58,638
our comment accepts two different
10722
06:51:58,638 --> 06:52:01,040
parameters and that is the value and the
10723
06:52:01,040 --> 06:52:02,400
id
10724
06:52:02,400 --> 06:52:04,160
the method of the request is going to be
10725
06:52:04,160 --> 06:52:05,920
the post because we want to create a
10726
06:52:05,920 --> 06:52:08,320
comment and the route is going to be
10727
06:52:08,320 --> 06:52:11,120
forward slash posts forward slash id and
10728
06:52:11,120 --> 06:52:13,360
then forward slash comment
10729
06:52:13,360 --> 06:52:15,280
post
10730
06:52:15,280 --> 06:52:17,360
but of course we also have to pass some
10731
06:52:17,360 --> 06:52:19,520
value with it so just next to that we're
10732
06:52:19,520 --> 06:52:21,200
gonna pass an object that's going to
10733
06:52:21,200 --> 06:52:24,080
contain the value of our comment great
10734
06:52:24,080 --> 06:52:26,400
we're now ready to make api calls from
10735
06:52:26,400 --> 06:52:29,040
the front end to the back end but this
10736
06:52:29,040 --> 06:52:31,120
route doesn't yet exist in our back end
10737
06:52:31,120 --> 06:52:33,520
so let's create it right away
10738
06:52:33,520 --> 06:52:35,280
to start creating our backend let's
10739
06:52:35,280 --> 06:52:37,360
first close all of our tabs just so we
10740
06:52:37,360 --> 06:52:39,520
have a cleaner workspace
10741
06:52:39,520 --> 06:52:42,400
and let's collapse our files and folders
10742
06:52:42,400 --> 06:52:44,878
now let's go to our server and then
10743
06:52:44,878 --> 06:52:48,240
we're going to go to routes posts.js
10744
06:52:48,240 --> 06:52:50,878
we can duplicate our like post route and
10745
06:52:50,878 --> 06:52:52,638
in here it's going to be incredibly
10746
06:52:52,638 --> 06:52:55,040
similar but just a bit different instead
10747
06:52:55,040 --> 06:52:57,200
of the patch request it is going to be a
10748
06:52:57,200 --> 06:52:59,520
post request and instead of the like
10749
06:52:59,520 --> 06:53:02,878
post it's going to be comment post need
10750
06:53:02,878 --> 06:53:04,958
to be logged in to be able to comment
10751
06:53:04,958 --> 06:53:06,240
the post so we're going to leave this
10752
06:53:06,240 --> 06:53:08,878
here and then also we're going to change
10753
06:53:08,878 --> 06:53:11,120
this to comment post
10754
06:53:11,120 --> 06:53:13,360
of course we have to first create that
10755
06:53:13,360 --> 06:53:15,760
controller and let's import it right
10756
06:53:15,760 --> 06:53:16,638
there
10757
06:53:16,638 --> 06:53:18,718
comment post
10758
06:53:18,718 --> 06:53:20,718
this here is going to be a function on
10759
06:53:20,718 --> 06:53:22,320
the backend that's going to handle the
10760
06:53:22,320 --> 06:53:25,360
creation of the comment so let's go to
10761
06:53:25,360 --> 06:53:29,280
our controllers and our posts.js
10762
06:53:29,280 --> 06:53:32,400
now let's scroll all the way down below
10763
06:53:32,400 --> 06:53:35,360
our like post and let's create a new
10764
06:53:35,360 --> 06:53:37,040
controller that's going to handle the
10765
06:53:37,040 --> 06:53:39,360
post creation
10766
06:53:39,360 --> 06:53:41,600
it's going to look something like this
10767
06:53:41,600 --> 06:53:43,200
export const
10768
06:53:43,200 --> 06:53:45,120
comment post
10769
06:53:45,120 --> 06:53:46,878
and that is going to be an async
10770
06:53:46,878 --> 06:53:48,798
function it's going to accept the
10771
06:53:48,798 --> 06:53:51,040
request and the response as all of the
10772
06:53:51,040 --> 06:53:52,718
controllers do
10773
06:53:52,718 --> 06:53:55,280
and in there we want to get a few values
10774
06:53:55,280 --> 06:53:57,280
from our front end first we're going to
10775
06:53:57,280 --> 06:54:00,400
get the id of the post by saying const
10776
06:54:00,400 --> 06:54:03,320
and then we can destructure the id from
10777
06:54:03,320 --> 06:54:05,120
rec.params
10778
06:54:05,120 --> 06:54:07,520
and then also we're going to get const
10779
06:54:07,520 --> 06:54:10,000
the structure the value but this time
10780
06:54:10,000 --> 06:54:12,638
it's going to be coming from rec.body
10781
06:54:12,638 --> 06:54:15,520
now let me specify how is that working
10782
06:54:15,520 --> 06:54:18,798
i'm going to open the client source api
10783
06:54:18,798 --> 06:54:20,160
index.js
10784
06:54:20,160 --> 06:54:22,160
and put it on the side
10785
06:54:22,160 --> 06:54:24,240
the request for rams is something coming
10786
06:54:24,240 --> 06:54:28,000
through here take a look we have this id
10787
06:54:28,000 --> 06:54:30,160
right there and if you take a look at
10788
06:54:30,160 --> 06:54:32,080
our routes you can see that the id
10789
06:54:32,080 --> 06:54:34,718
coming in is dynamic and that's what
10790
06:54:34,718 --> 06:54:37,280
populates the id right here in direct
10791
06:54:37,280 --> 06:54:38,400
params
10792
06:54:38,400 --> 06:54:40,080
so where does this value from the
10793
06:54:40,080 --> 06:54:41,760
reg.body come from
10794
06:54:41,760 --> 06:54:43,760
well if you take a look at our request
10795
06:54:43,760 --> 06:54:45,680
one more time you can see that we're
10796
06:54:45,680 --> 06:54:47,520
passing that object that contains the
10797
06:54:47,520 --> 06:54:49,920
value and that's what's populating this
10798
06:54:49,920 --> 06:54:52,000
value that's basically the value of our
10799
06:54:52,000 --> 06:54:53,520
comment
10800
06:54:53,520 --> 06:54:55,840
great now let's use those values to
10801
06:54:55,840 --> 06:54:58,080
create a comment in the database
10802
06:54:58,080 --> 06:55:00,160
first we have to fetch the post that we
10803
06:55:00,160 --> 06:55:02,638
have to put our comment on so we can say
10804
06:55:02,638 --> 06:55:06,160
const post is equal to a wait
10805
06:55:06,160 --> 06:55:07,120
post
10806
06:55:07,120 --> 06:55:11,600
message dot find by id and then in there
10807
06:55:11,600 --> 06:55:13,760
we can simply pass that id
10808
06:55:13,760 --> 06:55:16,638
finally we're going to go into our post
10809
06:55:16,638 --> 06:55:18,958
into the comments of our post and we're
10810
06:55:18,958 --> 06:55:21,840
simply going to push the contents of our
10811
06:55:21,840 --> 06:55:22,958
comment
10812
06:55:22,958 --> 06:55:25,120
that is going to update our post but we
10813
06:55:25,120 --> 06:55:26,718
have to actually update it in the
10814
06:55:26,718 --> 06:55:29,280
database as well so let's say const
10815
06:55:29,280 --> 06:55:33,200
updated post is equal to await
10816
06:55:33,200 --> 06:55:34,240
post
10817
06:55:34,240 --> 06:55:38,478
message dot find by id
10818
06:55:38,478 --> 06:55:41,360
and update and then inside of there we
10819
06:55:41,360 --> 06:55:43,840
have to pass the id we have to pass the
10820
06:55:43,840 --> 06:55:46,478
new post and we have to pass the options
10821
06:55:46,478 --> 06:55:48,000
object that's going to contain the
10822
06:55:48,000 --> 06:55:51,200
message new is equal to true
10823
06:55:51,200 --> 06:55:52,958
i know this is a long line so let me
10824
06:55:52,958 --> 06:55:54,798
repeat it one more time
10825
06:55:54,798 --> 06:55:56,160
we are getting the post from the
10826
06:55:56,160 --> 06:55:58,240
database we are adding the comments to
10827
06:55:58,240 --> 06:56:00,478
that post and then we are updating the
10828
06:56:00,478 --> 06:56:02,798
database so that the new post contains
10829
06:56:02,798 --> 06:56:05,360
that new comment finally we are storing
10830
06:56:05,360 --> 06:56:07,920
the value of that post in our updated
10831
06:56:07,920 --> 06:56:09,360
post variable
10832
06:56:09,360 --> 06:56:11,600
once we have that we can simply return
10833
06:56:11,600 --> 06:56:15,120
rest.json that updated post and we can
10834
06:56:15,120 --> 06:56:17,280
receive that back on the front end
10835
06:56:17,280 --> 06:56:20,080
great but there is one more thing we
10836
06:56:20,080 --> 06:56:23,040
also have to update the model of our
10837
06:56:23,040 --> 06:56:26,240
post so we can go to our server and then
10838
06:56:26,240 --> 06:56:29,200
models and then our post message
10839
06:56:29,200 --> 06:56:31,680
inside of here just below the likes we
10840
06:56:31,680 --> 06:56:33,680
can create the comments part
10841
06:56:33,680 --> 06:56:36,080
and we can say the type of the comments
10842
06:56:36,080 --> 06:56:38,558
is going to be an array of strings and
10843
06:56:38,558 --> 06:56:40,798
the default value is going to be an
10844
06:56:40,798 --> 06:56:43,280
empty array because by default we don't
10845
06:56:43,280 --> 06:56:45,600
have any comments in
10846
06:56:45,600 --> 06:56:47,760
great now we can come back to our front
10847
06:56:47,760 --> 06:56:50,320
end let's first close all of this go to
10848
06:56:50,320 --> 06:56:53,440
client side source and then actions
10849
06:56:53,440 --> 06:56:56,080
posts inside of here now we know that
10850
06:56:56,080 --> 06:56:57,920
we're receiving something back once we
10851
06:56:57,920 --> 06:57:00,638
actually call this api so let's retrieve
10852
06:57:00,638 --> 06:57:03,120
that value we can do that by saying cost
10853
06:57:03,120 --> 06:57:06,320
response is equal to this and then in
10854
06:57:06,320 --> 06:57:08,400
here we can destructure and simply take
10855
06:57:08,400 --> 06:57:11,200
the data from that response for now we
10856
06:57:11,200 --> 06:57:14,558
can simply console.log that data
10857
06:57:14,558 --> 06:57:16,400
so what are we expecting to see with
10858
06:57:16,400 --> 06:57:17,840
this console.log
10859
06:57:17,840 --> 06:57:20,558
well if our database functionality for
10860
06:57:20,558 --> 06:57:22,878
creating the comment works and if our
10861
06:57:22,878 --> 06:57:25,280
frontend is sending the correct data in
10862
06:57:25,280 --> 06:57:27,440
the first place that means that this
10863
06:57:27,440 --> 06:57:29,120
data should return
10864
06:57:29,120 --> 06:57:32,478
a new post and that post should have the
10865
06:57:32,478 --> 06:57:33,600
comments
10866
06:57:33,600 --> 06:57:35,600
that are going to be an array and it
10867
06:57:35,600 --> 06:57:38,080
should have a new comment that we type
10868
06:57:38,080 --> 06:57:38,798
in
10869
06:57:38,798 --> 06:57:41,600
so now let's test it out i'm right here
10870
06:57:41,600 --> 06:57:43,600
on a specific memory page i'm going to
10871
06:57:43,600 --> 06:57:46,080
click inspect and then i'm going to open
10872
06:57:46,080 --> 06:57:47,440
the console
10873
06:57:47,440 --> 06:57:49,440
now we'll keep track of her console and
10874
06:57:49,440 --> 06:57:51,520
we'll see if her comment gets there
10875
06:57:51,520 --> 06:57:53,520
let's write something like test and
10876
06:57:53,520 --> 06:57:55,520
click comment
10877
06:57:55,520 --> 06:57:58,638
okay something is red and looks like we
10878
06:57:58,638 --> 06:58:01,520
have the result of null that means that
10879
06:58:01,520 --> 06:58:03,680
the user hasn't been fetched yet or we
10880
06:58:03,680 --> 06:58:05,840
haven't properly been logged in so let's
10881
06:58:05,840 --> 06:58:08,080
close this reload
10882
06:58:08,080 --> 06:58:11,040
and yeah it's true we are not logged in
10883
06:58:11,040 --> 06:58:12,878
and we cannot post comments if you're
10884
06:58:12,878 --> 06:58:13,680
not
10885
06:58:13,680 --> 06:58:16,798
so we have to properly handle that error
10886
06:58:16,798 --> 06:58:19,600
let's go back and then inside of here
10887
06:58:19,600 --> 06:58:22,718
we're gonna check if we have a user
10888
06:58:22,718 --> 06:58:24,798
so how are we going to solve this well
10889
06:58:24,798 --> 06:58:26,320
there are a few ways
10890
06:58:26,320 --> 06:58:28,478
first of all we can in here once the
10891
06:58:28,478 --> 06:58:30,478
user clicks we can then check if the
10892
06:58:30,478 --> 06:58:32,958
user is logged in and then if he's not
10893
06:58:32,958 --> 06:58:34,878
we can disable that
10894
06:58:34,878 --> 06:58:36,878
but there's a better way
10895
06:58:36,878 --> 06:58:39,280
why would the user even need to see the
10896
06:58:39,280 --> 06:58:41,280
part for creating the post if that's
10897
06:58:41,280 --> 06:58:43,280
gonna result in failure let's
10898
06:58:43,280 --> 06:58:45,920
immediately remove this from his view
10899
06:58:45,920 --> 06:58:48,400
that way he's not gonna run into errors
10900
06:58:48,400 --> 06:58:51,920
so right inside of here we can take this
10901
06:58:51,920 --> 06:58:54,080
we can check if there's a user we can do
10902
06:58:54,080 --> 06:58:56,638
that by simply putting our code here
10903
06:58:56,638 --> 06:58:58,798
opening a dynamic block and we're going
10904
06:58:58,798 --> 06:59:03,040
to check if there is a user.result.name
10905
06:59:03,040 --> 06:59:05,520
then we're going to show this so we can
10906
06:59:05,520 --> 06:59:09,520
say end end and then open a parenthesis
10907
06:59:09,520 --> 06:59:11,360
then at the end of our div we can close
10908
06:59:11,360 --> 06:59:13,360
that parentheses and close our dynamic
10909
06:59:13,360 --> 06:59:14,400
block
10910
06:59:14,400 --> 06:59:16,878
this code is basically saying if the
10911
06:59:16,878 --> 06:59:18,878
user has a name basically meaning if
10912
06:59:18,878 --> 06:59:21,360
there is a user then display this piece
10913
06:59:21,360 --> 06:59:24,558
of code otherwise show nothing our user
10914
06:59:24,558 --> 06:59:26,878
is sometimes going to be undefined and
10915
06:59:26,878 --> 06:59:28,638
we cannot do the dot notation with
10916
06:59:28,638 --> 06:59:30,400
undefined it's not possible to do
10917
06:59:30,400 --> 06:59:32,638
undefined.result.name
10918
06:59:32,638 --> 06:59:34,638
so we have to handle that i'm going to
10919
06:59:34,638 --> 06:59:36,320
bring the user back and i'm going to say
10920
06:59:36,320 --> 06:59:39,360
user question mark dot result and then
10921
06:59:39,360 --> 06:59:41,680
question mark dot name this is the new
10922
06:59:41,680 --> 06:59:43,360
javascript syntax that's going to allow
10923
06:59:43,360 --> 06:59:45,840
us to simply not throw an error if the
10924
06:59:45,840 --> 06:59:47,600
user doesn't exist
10925
06:59:47,600 --> 06:59:49,200
let's save it and check it out in the
10926
06:59:49,200 --> 06:59:50,320
browser
10927
06:59:50,320 --> 06:59:52,798
and would you look at that now we have
10928
06:59:52,798 --> 06:59:55,120
only our comments but since our user is
10929
06:59:55,120 --> 06:59:57,280
not logged in he cannot create any
10930
06:59:57,280 --> 07:00:00,240
that's great he can view but not create
10931
07:00:00,240 --> 07:00:01,760
now let's sign in
10932
07:00:01,760 --> 07:00:03,360
i'm gonna sign in with google and
10933
07:00:03,360 --> 07:00:05,760
apparently even if i'm logged in i
10934
07:00:05,760 --> 07:00:07,760
cannot see the post creation part so
10935
07:00:07,760 --> 07:00:09,600
let's see what that is about
10936
07:00:09,600 --> 07:00:11,840
i'm going to console log our user just
10937
07:00:11,840 --> 07:00:14,000
at the top right here
10938
07:00:14,000 --> 07:00:16,558
and our user seems to be null
10939
07:00:16,558 --> 07:00:19,040
and looks like i've made a small mistake
10940
07:00:19,040 --> 07:00:21,120
instead of specifying user here we
10941
07:00:21,120 --> 07:00:23,200
should be specifying profile i'm going
10942
07:00:23,200 --> 07:00:25,280
to copy this and search for this across
10943
07:00:25,280 --> 07:00:27,040
the entire code base
10944
07:00:27,040 --> 07:00:28,558
as you can see we've been calling
10945
07:00:28,558 --> 07:00:31,680
profile many times and never user i just
10946
07:00:31,680 --> 07:00:33,120
made a typo
10947
07:00:33,120 --> 07:00:35,360
so let's go back to our comment section
10948
07:00:35,360 --> 07:00:37,680
and see if this works right now
10949
07:00:37,680 --> 07:00:39,440
we can even remove this console log
10950
07:00:39,440 --> 07:00:41,760
because i'm quite certain it will work
10951
07:00:41,760 --> 07:00:43,600
and since we're logged in we should be
10952
07:00:43,600 --> 07:00:45,440
able to see the part where we can create
10953
07:00:45,440 --> 07:00:47,120
a comment
10954
07:00:47,120 --> 07:00:49,520
and there we go now we can see this part
10955
07:00:49,520 --> 07:00:51,600
where we can write a comment if we were
10956
07:00:51,600 --> 07:00:53,840
logged out we wouldn't be able to see it
10957
07:00:53,840 --> 07:00:55,760
but now let's see if this comment is
10958
07:00:55,760 --> 07:00:57,440
going to come through i'm going to say
10959
07:00:57,440 --> 07:00:59,920
test and click comment
10960
07:00:59,920 --> 07:01:02,000
now we're expecting akanza log with the
10961
07:01:02,000 --> 07:01:04,320
post now including the comment
10962
07:01:04,320 --> 07:01:05,840
and there it is
10963
07:01:05,840 --> 07:01:07,600
considering that i've used this before
10964
07:01:07,600 --> 07:01:09,360
it's going to have a few more comments
10965
07:01:09,360 --> 07:01:11,520
yours should have just a few but there
10966
07:01:11,520 --> 07:01:14,160
it is our comments are there now we have
10967
07:01:14,160 --> 07:01:16,240
to fetch them and using redux and
10968
07:01:16,240 --> 07:01:18,558
reducers we have to bring them back into
10969
07:01:18,558 --> 07:01:20,958
our jsx view
10970
07:01:20,958 --> 07:01:23,440
and we can do that like this let's go
10971
07:01:23,440 --> 07:01:25,680
back to our actions where we left off
10972
07:01:25,680 --> 07:01:28,798
that's going to be actions post.js
10973
07:01:28,798 --> 07:01:30,638
and then inside of here
10974
07:01:30,638 --> 07:01:32,878
we're getting the data about a specific
10975
07:01:32,878 --> 07:01:35,680
post now including the comments so the
10976
07:01:35,680 --> 07:01:38,718
question is what do we do with it
10977
07:01:38,718 --> 07:01:40,558
well we're going to simply dispatch a
10978
07:01:40,558 --> 07:01:43,760
new action we can say dispatch
10979
07:01:43,760 --> 07:01:45,840
and we're going to specify the type of
10980
07:01:45,840 --> 07:01:49,680
our action to be equal to comment
10981
07:01:49,680 --> 07:01:51,840
also we're going to specify the payload
10982
07:01:51,840 --> 07:01:54,558
to be that new post with now included
10983
07:01:54,558 --> 07:01:56,000
comment
10984
07:01:56,000 --> 07:01:58,478
also after we dispatch that specific
10985
07:01:58,478 --> 07:02:01,360
comment we're also going to return the
10986
07:02:01,360 --> 07:02:03,840
data dot comments that means that we're
10987
07:02:03,840 --> 07:02:05,440
returning the newest comment that's
10988
07:02:05,440 --> 07:02:07,120
coming in
10989
07:02:07,120 --> 07:02:09,200
and inside of here inside of the catch
10990
07:02:09,200 --> 07:02:12,638
we can simply console log the error
10991
07:02:12,638 --> 07:02:15,040
one more thing see how we've used the
10992
07:02:15,040 --> 07:02:17,840
constants for all of our action types
10993
07:02:17,840 --> 07:02:20,000
right there start loading and loading
10994
07:02:20,000 --> 07:02:21,360
and so on
10995
07:02:21,360 --> 07:02:23,600
let's now create the constant for the
10996
07:02:23,600 --> 07:02:26,400
comment as well so right here next to
10997
07:02:26,400 --> 07:02:28,080
the like i'm going to create a new
10998
07:02:28,080 --> 07:02:31,840
constant and that's going to be comment
10999
07:02:31,840 --> 07:02:33,600
of course this is just the import we
11000
07:02:33,600 --> 07:02:35,840
have to create that constant right here
11001
07:02:35,840 --> 07:02:38,080
under the source and then constants
11002
07:02:38,080 --> 07:02:40,798
action types
11003
07:02:40,798 --> 07:02:42,958
just below like i'm going to create the
11004
07:02:42,958 --> 07:02:45,040
comment and that's basically just a
11005
07:02:45,040 --> 07:02:46,718
string again if you're not sure why
11006
07:02:46,718 --> 07:02:48,558
we're using this for maybe watch some of
11007
07:02:48,558 --> 07:02:50,320
the previous videos but the quick
11008
07:02:50,320 --> 07:02:53,440
explanation is that if you scroll down
11009
07:02:53,440 --> 07:02:54,878
here
11010
07:02:54,878 --> 07:02:56,638
if you write something like comment here
11011
07:02:56,638 --> 07:02:58,638
and maybe miss one letter like this
11012
07:02:58,638 --> 07:03:00,638
comment that's gonna cause a lot of
11013
07:03:00,638 --> 07:03:02,638
problems in your app but it's not gonna
11014
07:03:02,638 --> 07:03:04,320
throw an error so you might be able to
11015
07:03:04,320 --> 07:03:07,440
miss it but like this if we write it as
11016
07:03:07,440 --> 07:03:08,718
a constant
11017
07:03:08,718 --> 07:03:11,120
then if you miss just one letter it's
11018
07:03:11,120 --> 07:03:13,360
immediately gonna throw you an error
11019
07:03:13,360 --> 07:03:15,440
that's the reason why we do it that part
11020
07:03:15,440 --> 07:03:17,600
is now done the last part we have to do
11021
07:03:17,600 --> 07:03:20,638
is deal with this data in our redux and
11022
07:03:20,638 --> 07:03:22,478
then send our comments back to our
11023
07:03:22,478 --> 07:03:24,558
comment section where we'll be able to
11024
07:03:24,558 --> 07:03:26,558
fetch them and then loop over the real
11025
07:03:26,558 --> 07:03:29,360
comments not just the dummy data so
11026
07:03:29,360 --> 07:03:31,280
let's do that right away
11027
07:03:31,280 --> 07:03:32,958
to get this data to our component
11028
07:03:32,958 --> 07:03:34,878
there's one last step of the entire
11029
07:03:34,878 --> 07:03:37,680
redux workflow that we have to do
11030
07:03:37,680 --> 07:03:40,160
first we're dispatching an action
11031
07:03:40,160 --> 07:03:42,400
then inside of that action creator we
11032
07:03:42,400 --> 07:03:45,520
are making an api call and finally we
11033
07:03:45,520 --> 07:03:48,160
have to go to our reducers so let's go
11034
07:03:48,160 --> 07:03:51,920
to reducers and then post.js
11035
07:03:51,920 --> 07:03:54,320
now inside of our reducers we also need
11036
07:03:54,320 --> 07:03:57,600
to pull our comment action type
11037
07:03:57,600 --> 07:04:00,478
and we have to add one more case
11038
07:04:00,478 --> 07:04:03,120
to create a new case let's go under our
11039
07:04:03,120 --> 07:04:06,160
like right there and let's say case
11040
07:04:06,160 --> 07:04:08,320
comment so what are we going to do on
11041
07:04:08,320 --> 07:04:10,798
the comment well we're also going to
11042
07:04:10,798 --> 07:04:13,120
return an object so let's specify it
11043
07:04:13,120 --> 07:04:16,080
like this and in every object we want to
11044
07:04:16,080 --> 07:04:18,080
first spread the state and we're going
11045
07:04:18,080 --> 07:04:20,000
to do that here as well
11046
07:04:20,000 --> 07:04:21,600
once we spread the state we want to
11047
07:04:21,600 --> 07:04:24,878
define posts so we can say posts is
11048
07:04:24,878 --> 07:04:26,000
equal to
11049
07:04:26,000 --> 07:04:29,360
state dot posts
11050
07:04:29,360 --> 07:04:32,000
so we want to map over all of the posts
11051
07:04:32,000 --> 07:04:33,840
and we're going to get each individual
11052
07:04:33,840 --> 07:04:34,878
post
11053
07:04:34,878 --> 07:04:37,760
then we want to do something
11054
07:04:37,760 --> 07:04:40,478
so what do we want to do here well we
11055
07:04:40,478 --> 07:04:43,760
want to return all the other posts
11056
07:04:43,760 --> 07:04:45,600
normally i'm going to explain quickly
11057
07:04:45,600 --> 07:04:48,080
what do i mean by that and then we want
11058
07:04:48,080 --> 07:04:52,320
to change the post that just received a
11059
07:04:52,320 --> 07:04:53,520
comment
11060
07:04:53,520 --> 07:04:55,840
great so we want to return all the posts
11061
07:04:55,840 --> 07:04:58,240
normally and then change only the post
11062
07:04:58,240 --> 07:05:00,558
that recently received a comment how can
11063
07:05:00,558 --> 07:05:02,320
we do that
11064
07:05:02,320 --> 07:05:06,080
well we can check if post dot underscore
11065
07:05:06,080 --> 07:05:07,040
id
11066
07:05:07,040 --> 07:05:10,638
and if that is equal to action dot
11067
07:05:10,638 --> 07:05:13,840
payload dot underscore id that is our
11068
07:05:13,840 --> 07:05:16,558
new post that just received a comment so
11069
07:05:16,558 --> 07:05:18,638
if that's the case then we want to
11070
07:05:18,638 --> 07:05:21,920
return only that new specific post which
11071
07:05:21,920 --> 07:05:24,878
is equal to action.payload and in other
11072
07:05:24,878 --> 07:05:28,160
case we simply want to return a post
11073
07:05:28,160 --> 07:05:30,798
one more time return all the other posts
11074
07:05:30,798 --> 07:05:33,440
normally like this and then if the post
11075
07:05:33,440 --> 07:05:35,440
is that specific post that just received
11076
07:05:35,440 --> 07:05:37,840
a comment then return it changed with
11077
07:05:37,840 --> 07:05:40,798
that comment included great so this is
11078
07:05:40,798 --> 07:05:43,120
how this works we can now remove these
11079
07:05:43,120 --> 07:05:45,760
comments put this in one line
11080
07:05:45,760 --> 07:05:48,718
and that is it this is how our common
11081
07:05:48,718 --> 07:05:51,040
functionality in redux works i know it
11082
07:05:51,040 --> 07:05:52,718
might be a bit confusing we're spreading
11083
07:05:52,718 --> 07:05:55,280
the state and then setting the posts but
11084
07:05:55,280 --> 07:05:57,120
keep in mind we want to return all of
11085
07:05:57,120 --> 07:06:00,000
the posts and only change the one that
11086
07:06:00,000 --> 07:06:03,200
just received the comment that is it
11087
07:06:03,200 --> 07:06:04,878
great so with that we're done with the
11088
07:06:04,878 --> 07:06:06,958
reducers and now we can actually take a
11089
07:06:06,958 --> 07:06:09,440
look at our data inside of our comment
11090
07:06:09,440 --> 07:06:12,000
section component so let's do just that
11091
07:06:12,000 --> 07:06:13,600
i'm going to close all of our other
11092
07:06:13,600 --> 07:06:16,320
files go inside of here and now we want
11093
07:06:16,320 --> 07:06:18,320
to fetch our comments
11094
07:06:18,320 --> 07:06:20,478
the comments are of course stored in a
11095
07:06:20,478 --> 07:06:22,798
post and we are already getting a post
11096
07:06:22,798 --> 07:06:25,680
right there so what we can do is we can
11097
07:06:25,680 --> 07:06:28,320
initially set our comments to be the
11098
07:06:28,320 --> 07:06:30,878
comments of that specific post so right
11099
07:06:30,878 --> 07:06:33,360
in here i'm gonna say post question mark
11100
07:06:33,360 --> 07:06:35,040
dot comments
11101
07:06:35,040 --> 07:06:37,360
okay so now we're getting our redux data
11102
07:06:37,360 --> 07:06:39,120
from the backend right in here and
11103
07:06:39,120 --> 07:06:41,920
putting it in our comments and of course
11104
07:06:41,920 --> 07:06:43,600
we have to change it right there so
11105
07:06:43,600 --> 07:06:46,558
we're not gonna simply say comment i we
11106
07:06:46,558 --> 07:06:48,558
are going to return
11107
07:06:48,558 --> 07:06:51,040
the c the comment of course so for every
11108
07:06:51,040 --> 07:06:52,958
comment we want to return a comment
11109
07:06:52,958 --> 07:06:55,840
inside of a typography let's save it and
11110
07:06:55,840 --> 07:06:57,120
check it out
11111
07:06:57,120 --> 07:06:59,280
and would you look at that we have our
11112
07:06:59,280 --> 07:07:01,440
comments i've been there last year the
11113
07:07:01,440 --> 07:07:04,240
best experience ever and we have all of
11114
07:07:04,240 --> 07:07:06,160
the comments we can scroll through and
11115
07:07:06,160 --> 07:07:07,920
read them
11116
07:07:07,920 --> 07:07:10,160
now let's sign in as well
11117
07:07:10,160 --> 07:07:12,478
okay as we're signed in we can now write
11118
07:07:12,478 --> 07:07:14,320
a comment and the functionality should
11119
07:07:14,320 --> 07:07:17,200
be fully functional so let's say
11120
07:07:17,200 --> 07:07:19,200
grade memory
11121
07:07:19,200 --> 07:07:21,280
okay let's try seeing great memory a few
11122
07:07:21,280 --> 07:07:22,798
times
11123
07:07:22,798 --> 07:07:25,600
and nothing seems to be happening but if
11124
07:07:25,600 --> 07:07:27,760
you reload the page
11125
07:07:27,760 --> 07:07:29,680
you'll be able to see that we clicked it
11126
07:07:29,680 --> 07:07:32,240
quite a few times so we just have to
11127
07:07:32,240 --> 07:07:34,638
make it update immediately as soon as we
11128
07:07:34,638 --> 07:07:36,400
leave the comment and that's what we'll
11129
07:07:36,400 --> 07:07:38,000
do right now
11130
07:07:38,000 --> 07:07:39,840
but with that said you can see the
11131
07:07:39,840 --> 07:07:41,520
comments actually work you can write a
11132
07:07:41,520 --> 07:07:43,440
comment click comment right there and
11133
07:07:43,440 --> 07:07:45,840
you just left it on this specific memory
11134
07:07:45,840 --> 07:07:47,760
let's go to another one let's go for
11135
07:07:47,760 --> 07:07:49,920
example to the eiffel tower
11136
07:07:49,920 --> 07:07:51,920
let's write something meaningful
11137
07:07:51,920 --> 07:07:55,040
looks great at night great and let's
11138
07:07:55,040 --> 07:07:56,240
comment it
11139
07:07:56,240 --> 07:07:57,760
and as you can see nothing happened
11140
07:07:57,760 --> 07:08:00,718
right away but if we reload the comment
11141
07:08:00,718 --> 07:08:04,240
should actually be there and there it is
11142
07:08:04,240 --> 07:08:06,240
okay so now let's make it update
11143
07:08:06,240 --> 07:08:08,320
automatically we can do that by
11144
07:08:08,320 --> 07:08:10,240
immediately receiving all the new
11145
07:08:10,240 --> 07:08:12,718
comments from the dispatched action
11146
07:08:12,718 --> 07:08:14,400
if i go into this specific action
11147
07:08:14,400 --> 07:08:16,080
creator you can notice that we are
11148
07:08:16,080 --> 07:08:18,798
returning all the new comments and we
11149
07:08:18,798 --> 07:08:20,958
can actually make use of that let's make
11150
07:08:20,958 --> 07:08:23,120
this function asynchronous
11151
07:08:23,120 --> 07:08:25,120
and right inside of here we can say
11152
07:08:25,120 --> 07:08:27,040
const new
11153
07:08:27,040 --> 07:08:29,280
comments and new comments are going to
11154
07:08:29,280 --> 07:08:32,320
be equal to a weight dispatch
11155
07:08:32,320 --> 07:08:34,478
great so now we can use those comments
11156
07:08:34,478 --> 07:08:37,840
more specifically to set the comments to
11157
07:08:37,840 --> 07:08:39,200
new comments
11158
07:08:39,200 --> 07:08:40,958
that's going to update our state of
11159
07:08:40,958 --> 07:08:42,958
comments and then render them down
11160
07:08:42,958 --> 07:08:44,400
immediately
11161
07:08:44,400 --> 07:08:46,878
what we can also do is we can set our
11162
07:08:46,878 --> 07:08:48,958
singular comment that's the data inside
11163
07:08:48,958 --> 07:08:51,280
of the text field we can set that to be
11164
07:08:51,280 --> 07:08:53,600
an empty string and that should clear it
11165
07:08:53,600 --> 07:08:56,240
out so let's check it out
11166
07:08:56,240 --> 07:08:59,200
anybody else been here let's do
11167
07:08:59,200 --> 07:09:01,840
something like that leave a comment
11168
07:09:01,840 --> 07:09:03,680
and take a look
11169
07:09:03,680 --> 07:09:05,680
amazing we can successfully post
11170
07:09:05,680 --> 07:09:08,160
comments on our memories
11171
07:09:08,160 --> 07:09:10,718
now what if i keep adding comments like
11172
07:09:10,718 --> 07:09:11,760
test
11173
07:09:11,760 --> 07:09:14,478
and then maybe one more let's add test
11174
07:09:14,478 --> 07:09:15,920
one
11175
07:09:15,920 --> 07:09:18,798
as you can see test one has been added
11176
07:09:18,798 --> 07:09:20,718
but you cannot automatically see it
11177
07:09:20,718 --> 07:09:22,638
which is not a great user experience you
11178
07:09:22,638 --> 07:09:25,040
have to scroll down so let's make it
11179
07:09:25,040 --> 07:09:26,878
scroll automatically
11180
07:09:26,878 --> 07:09:28,240
you might think that you're gonna have
11181
07:09:28,240 --> 07:09:30,400
to use some external libraries or that
11182
07:09:30,400 --> 07:09:32,638
it's gonna be hard to do it but it's not
11183
07:09:32,638 --> 07:09:34,558
really that hard we just have to know
11184
07:09:34,558 --> 07:09:37,120
how to use refs so we're importing the
11185
07:09:37,120 --> 07:09:40,080
usref hook right there and right here we
11186
07:09:40,080 --> 07:09:41,600
can say const
11187
07:09:41,600 --> 07:09:45,280
comments ref is going to be equal to use
11188
07:09:45,280 --> 07:09:46,718
ref
11189
07:09:46,718 --> 07:09:48,320
so we've just created this specific
11190
07:09:48,320 --> 07:09:50,080
reference that we're going to hook onto
11191
07:09:50,080 --> 07:09:51,760
a specific element
11192
07:09:51,760 --> 07:09:54,478
more specifically we are going to create
11193
07:09:54,478 --> 07:09:57,360
an empty div just below all of our
11194
07:09:57,360 --> 07:09:58,958
messages so
11195
07:09:58,958 --> 07:10:01,760
right here we can create a self-closing
11196
07:10:01,760 --> 07:10:03,680
div like this and the only thing it's
11197
07:10:03,680 --> 07:10:06,160
going to have is going to be a ref equal
11198
07:10:06,160 --> 07:10:09,280
to comments ref
11199
07:10:09,280 --> 07:10:11,360
this is going to be our anchor point
11200
07:10:11,360 --> 07:10:12,958
we're always going to scroll to this
11201
07:10:12,958 --> 07:10:15,200
specific div because we know that that
11202
07:10:15,200 --> 07:10:18,240
div is at the end of all of our messages
11203
07:10:18,240 --> 07:10:20,000
so let's scroll up
11204
07:10:20,000 --> 07:10:22,320
when do we want to scroll well we want
11205
07:10:22,320 --> 07:10:25,760
to scroll once we add a new comment so
11206
07:10:25,760 --> 07:10:27,360
right here let's create the
11207
07:10:27,360 --> 07:10:30,240
functionality to scroll down
11208
07:10:30,240 --> 07:10:32,798
we can do that by saying comments
11209
07:10:32,798 --> 07:10:33,840
ref
11210
07:10:33,840 --> 07:10:35,200
dot current
11211
07:10:35,200 --> 07:10:36,718
dot scroll
11212
07:10:36,718 --> 07:10:37,600
into
11213
07:10:37,600 --> 07:10:38,638
view
11214
07:10:38,638 --> 07:10:41,200
i'm missing an n here
11215
07:10:41,200 --> 07:10:43,920
and you call that as a function pass in
11216
07:10:43,920 --> 07:10:46,080
an object and then in here we can say
11217
07:10:46,080 --> 07:10:49,440
behavior is equal to smooth that's going
11218
07:10:49,440 --> 07:10:51,120
to make it scroll slowly to that
11219
07:10:51,120 --> 07:10:52,478
specific point
11220
07:10:52,478 --> 07:10:54,478
and with that our scroll should be done
11221
07:10:54,478 --> 07:10:56,798
let's check it out
11222
07:10:56,798 --> 07:10:59,040
let's add a new comment let's do test
11223
07:10:59,040 --> 07:11:01,360
two
11224
07:11:01,360 --> 07:11:03,600
and as you can see it just scrolled
11225
07:11:03,600 --> 07:11:05,520
let's try adding a few more
11226
07:11:05,520 --> 07:11:08,160
test four
11227
07:11:08,160 --> 07:11:10,638
it scrolled as well great this
11228
07:11:10,638 --> 07:11:13,840
functionality is now done
11229
07:11:13,840 --> 07:11:15,600
last thing that we can do is make the
11230
07:11:15,600 --> 07:11:19,040
username bold and leave this as it is
11231
07:11:19,040 --> 07:11:21,760
we can do that right inside of here by
11232
07:11:21,760 --> 07:11:24,638
splitting our comment into two parts
11233
07:11:24,638 --> 07:11:27,200
more specifically we can say comment dot
11234
07:11:27,200 --> 07:11:29,920
split and we want to split it by that
11235
07:11:29,920 --> 07:11:33,040
colon and we want to get the username so
11236
07:11:33,040 --> 07:11:35,680
if you do 0 this here is only the
11237
07:11:35,680 --> 07:11:37,040
username
11238
07:11:37,040 --> 07:11:38,718
now that we have just the username we
11239
07:11:38,718 --> 07:11:41,840
can put that into a strong tag and that
11240
07:11:41,840 --> 07:11:43,840
is going to make the text bold
11241
07:11:43,840 --> 07:11:46,400
so if i do it like this and just below
11242
07:11:46,400 --> 07:11:48,240
we can now get the second part which is
11243
07:11:48,240 --> 07:11:50,558
the actual comment so we can say c dot
11244
07:11:50,558 --> 07:11:53,200
split we want to again split by the same
11245
07:11:53,200 --> 07:11:54,000
thing
11246
07:11:54,000 --> 07:11:56,478
and in here we want to get the first
11247
07:11:56,478 --> 07:11:59,680
part which is the actual comment
11248
07:11:59,680 --> 07:12:01,680
on the bottom part make sure not to put
11249
07:12:01,680 --> 07:12:03,440
the space in here
11250
07:12:03,440 --> 07:12:06,080
great let's save it and check it out
11251
07:12:06,080 --> 07:12:07,920
great that works now we can
11252
07:12:07,920 --> 07:12:09,840
differentiate the username from the
11253
07:12:09,840 --> 07:12:11,040
comment
11254
07:12:11,040 --> 07:12:13,200
this is a seemingly easy feature that we
11255
07:12:13,200 --> 07:12:14,878
just implemented but it's really going
11256
07:12:14,878 --> 07:12:17,280
to add a lot to the entire application
11257
07:12:17,280 --> 07:12:19,280
our memories project really needed the
11258
07:12:19,280 --> 07:12:21,600
comments section because now people can
11259
07:12:21,600 --> 07:12:23,440
comment on each other's memories and
11260
07:12:23,440 --> 07:12:25,360
share their stories
11261
07:12:25,360 --> 07:12:27,840
but that's not it for this video i've
11262
07:12:27,840 --> 07:12:29,840
decided to do one more thing some of you
11263
07:12:29,840 --> 07:12:32,478
guys requested of me in the comments
11264
07:12:32,478 --> 07:12:34,798
and that is to fix the speed of liking
11265
07:12:34,798 --> 07:12:37,520
something right now if you click like
11266
07:12:37,520 --> 07:12:39,760
it's gonna take maybe a second a second
11267
07:12:39,760 --> 07:12:42,400
and a half or maybe four to five seconds
11268
07:12:42,400 --> 07:12:44,478
in areas with slower internet connection
11269
07:12:44,478 --> 07:12:46,718
so if i click it notice how long is it
11270
07:12:46,718 --> 07:12:49,200
gonna take
11271
07:12:49,920 --> 07:12:52,558
yep that was quite a long pause so what
11272
07:12:52,558 --> 07:12:54,000
we're going to implement for the end of
11273
07:12:54,000 --> 07:12:56,558
this video is the ability to immediately
11274
07:12:56,558 --> 07:12:58,558
give the user feedback once he likes
11275
07:12:58,558 --> 07:13:00,320
something the actual liking on the
11276
07:13:00,320 --> 07:13:02,638
backend is going to take 2-3 seconds but
11277
07:13:02,638 --> 07:13:04,638
we can immediately display the action to
11278
07:13:04,638 --> 07:13:07,280
the user let's do that
11279
07:13:07,280 --> 07:13:09,360
and this is not useful only for liking
11280
07:13:09,360 --> 07:13:10,958
you can use this technique with
11281
07:13:10,958 --> 07:13:13,280
everything where you want to give users
11282
07:13:13,280 --> 07:13:15,520
the quick user feedback
11283
07:13:15,520 --> 07:13:17,440
if you appreciate these little tips that
11284
07:13:17,440 --> 07:13:19,360
i like giving to you definitely make
11285
07:13:19,360 --> 07:13:21,920
sure to like and comment down below
11286
07:13:21,920 --> 07:13:23,680
comment if you'd like to see the entire
11287
07:13:23,680 --> 07:13:25,760
map of all the memories implemented in
11288
07:13:25,760 --> 07:13:27,440
the next video
11289
07:13:27,440 --> 07:13:29,840
to implement our let's call it quick
11290
07:13:29,840 --> 07:13:33,440
liking we can go to client source
11291
07:13:33,440 --> 07:13:35,680
components and we're going to go to
11292
07:13:35,680 --> 07:13:38,638
posts and finally a singular post
11293
07:13:38,638 --> 07:13:41,120
component
11294
07:13:41,120 --> 07:13:43,120
from here we can see that we have some
11295
07:13:43,120 --> 07:13:44,958
imports that we're not using so we can
11296
07:13:44,958 --> 07:13:48,638
delete that right here get post
11297
07:13:48,638 --> 07:13:51,680
and we can also immediately import use
11298
07:13:51,680 --> 07:13:53,280
state from react because we'll
11299
07:13:53,280 --> 07:13:56,160
definitely be using that
11300
07:13:56,160 --> 07:13:58,558
great so now let's see what do we have
11301
07:13:58,558 --> 07:13:59,600
to do
11302
07:13:59,600 --> 07:14:02,000
right now we're getting the post passed
11303
07:14:02,000 --> 07:14:04,320
through props and that's fine we're then
11304
07:14:04,320 --> 07:14:06,638
using that post to find out the length
11305
07:14:06,638 --> 07:14:08,400
of the likes in that specific likes
11306
07:14:08,400 --> 07:14:10,240
array and then based on that we're
11307
07:14:10,240 --> 07:14:12,878
showing our likes
11308
07:14:12,878 --> 07:14:15,440
but we can make that just a bit better
11309
07:14:15,440 --> 07:14:17,520
first of all we're going to implement a
11310
07:14:17,520 --> 07:14:20,558
new state field called likes
11311
07:14:20,558 --> 07:14:22,478
and of course it's going to have a set
11312
07:14:22,478 --> 07:14:25,040
likes which is going to be equal to use
11313
07:14:25,040 --> 07:14:27,520
state and then inside of there we want
11314
07:14:27,520 --> 07:14:30,160
to immediately set that to the post
11315
07:14:30,160 --> 07:14:32,478
question mark dot likes
11316
07:14:32,478 --> 07:14:34,558
so we're taking the same value from our
11317
07:14:34,558 --> 07:14:36,718
post right there post question mark that
11318
07:14:36,718 --> 07:14:39,280
likes and we're setting that into likes
11319
07:14:39,280 --> 07:14:42,080
so so far not a lot has changed
11320
07:14:42,080 --> 07:14:44,400
but there is a specific reason why we
11321
07:14:44,400 --> 07:14:46,798
need to do that because now we can set
11322
07:14:46,798 --> 07:14:49,040
them again we can reset them we can
11323
07:14:49,040 --> 07:14:50,878
change them because now we have this
11324
07:14:50,878 --> 07:14:52,878
setter function
11325
07:14:52,878 --> 07:14:55,680
so we're going to scroll down and find
11326
07:14:55,680 --> 07:14:57,760
where we're actually liking something
11327
07:14:57,760 --> 07:15:00,558
here's the delete and right here
11328
07:15:00,558 --> 07:15:02,478
is the like button
11329
07:15:02,478 --> 07:15:04,798
so right now we are doing something on
11330
07:15:04,798 --> 07:15:07,040
click but we need to do a bit more stuff
11331
07:15:07,040 --> 07:15:09,600
so i'm going to copy this and simply
11332
07:15:09,600 --> 07:15:12,160
create a new function called handle
11333
07:15:12,160 --> 07:15:13,200
click
11334
07:15:13,200 --> 07:15:15,680
we're going to use that function above
11335
07:15:15,680 --> 07:15:17,520
right inside of here
11336
07:15:17,520 --> 07:15:19,600
so right here let's create a new
11337
07:15:19,600 --> 07:15:20,478
function
11338
07:15:20,478 --> 07:15:22,000
const handle
11339
07:15:22,000 --> 07:15:25,680
like is equal to an async function
11340
07:15:25,680 --> 07:15:27,840
like this and then let's simply paste
11341
07:15:27,840 --> 07:15:29,920
what we had of course we don't need this
11342
07:15:29,920 --> 07:15:31,520
callback function
11343
07:15:31,520 --> 07:15:33,200
it says our handle like is not being
11344
07:15:33,200 --> 07:15:35,680
used so let me see why i called it
11345
07:15:35,680 --> 07:15:37,840
handle click a more appropriate name
11346
07:15:37,840 --> 07:15:40,718
would be handle like
11347
07:15:40,718 --> 07:15:44,000
great so now we've changed nothing we've
11348
07:15:44,000 --> 07:15:46,000
added a new handler function which is
11349
07:15:46,000 --> 07:15:47,520
still doing the same thing it used to do
11350
07:15:47,520 --> 07:15:49,840
before and we also just added these
11351
07:15:49,840 --> 07:15:52,320
likes which again is not being used so
11352
07:15:52,320 --> 07:15:55,520
now what can we do to start using them
11353
07:15:55,520 --> 07:15:57,760
inside of here we're going to add an if
11354
07:15:57,760 --> 07:16:00,160
check i'm going to copy this entire part
11355
07:16:00,160 --> 07:16:01,558
right there
11356
07:16:01,558 --> 07:16:04,000
pose.lice.find and then we're checking
11357
07:16:04,000 --> 07:16:07,440
if the like is equal to to the google id
11358
07:16:07,440 --> 07:16:09,920
or the result underscore id we are
11359
07:16:09,920 --> 07:16:12,718
basically checking in this whole thing
11360
07:16:12,718 --> 07:16:15,360
did the current user like the post so we
11361
07:16:15,360 --> 07:16:16,638
can say if
11362
07:16:16,638 --> 07:16:19,280
this and that's basically saying did the
11363
07:16:19,280 --> 07:16:22,320
current user like the post or not
11364
07:16:22,320 --> 07:16:24,478
usually when you have lines as long as
11365
07:16:24,478 --> 07:16:26,000
this one it's good to put them to a
11366
07:16:26,000 --> 07:16:29,440
specific variable so let's say const has
11367
07:16:29,440 --> 07:16:31,520
liked post
11368
07:16:31,520 --> 07:16:33,840
and that is going to be equal to this
11369
07:16:33,840 --> 07:16:35,280
now that we have this we know that
11370
07:16:35,280 --> 07:16:37,360
that's going to be a boolean value and
11371
07:16:37,360 --> 07:16:40,558
we can say if has like the post then we
11372
07:16:40,558 --> 07:16:42,160
want to do something
11373
07:16:42,160 --> 07:16:44,638
else if the user hasn't liked the post
11374
07:16:44,638 --> 07:16:47,280
then we want to do something else
11375
07:16:47,280 --> 07:16:49,760
so what do we want to do if the current
11376
07:16:49,760 --> 07:16:52,240
user has liked the post
11377
07:16:52,240 --> 07:16:54,958
well we want to set the likes
11378
07:16:54,958 --> 07:16:56,718
to be equal to
11379
07:16:56,718 --> 07:17:00,000
post dot likes dot filter
11380
07:17:00,000 --> 07:17:02,958
inside of here we get a specific id
11381
07:17:02,958 --> 07:17:05,520
and we want to filter out the id of that
11382
07:17:05,520 --> 07:17:08,160
specific person so we can say if id is
11383
07:17:08,160 --> 07:17:11,120
not equal to and that's going to be
11384
07:17:11,120 --> 07:17:12,558
in parentheses
11385
07:17:12,558 --> 07:17:16,638
user question mark dot result
11386
07:17:16,638 --> 07:17:19,920
dot result dot google id
11387
07:17:19,920 --> 07:17:23,360
or we can also have user question mark
11388
07:17:23,360 --> 07:17:26,718
dot result question mark dot underscore
11389
07:17:26,718 --> 07:17:27,760
id
11390
07:17:27,760 --> 07:17:30,160
so we're basically simply filtering out
11391
07:17:30,160 --> 07:17:32,320
the like because if the user has liked
11392
07:17:32,320 --> 07:17:34,478
the post and he or she clicks the button
11393
07:17:34,478 --> 07:17:36,398
again that means that they want to
11394
07:17:36,398 --> 07:17:37,600
unlike it
11395
07:17:37,600 --> 07:17:39,280
but now i figured out we're mentioning
11396
07:17:39,280 --> 07:17:41,760
this part quite often so i want to take
11397
07:17:41,760 --> 07:17:43,600
that entire part and put it in a new
11398
07:17:43,600 --> 07:17:46,638
variable let's copy it and i'm going to
11399
07:17:46,638 --> 07:17:48,558
call it something like
11400
07:17:48,558 --> 07:17:51,600
const user id
11401
07:17:51,600 --> 07:17:53,360
and that's going to be equal to all of
11402
07:17:53,360 --> 07:17:56,478
this we no longer have to repeat it i'm
11403
07:17:56,478 --> 07:17:58,320
going to put it above and we can use
11404
07:17:58,320 --> 07:18:00,958
that id in a few places we can use it
11405
07:18:00,958 --> 07:18:02,878
right here because you can notice is the
11406
07:18:02,878 --> 07:18:04,000
same thing
11407
07:18:04,000 --> 07:18:06,320
we can also use it right here that's
11408
07:18:06,320 --> 07:18:07,840
also the same thing
11409
07:18:07,840 --> 07:18:10,160
and we can also use it right here
11410
07:18:10,160 --> 07:18:11,840
because it's the same thing
11411
07:18:11,840 --> 07:18:13,840
this was a great little lesson where we
11412
07:18:13,840 --> 07:18:16,398
used two new variables to simplify our
11413
07:18:16,398 --> 07:18:18,878
code and make it more readable now
11414
07:18:18,878 --> 07:18:20,718
everybody can understand that this whole
11415
07:18:20,718 --> 07:18:22,878
part is either getting the user's id
11416
07:18:22,878 --> 07:18:25,280
from google or a normal id from the
11417
07:18:25,280 --> 07:18:27,360
database and it's storing it in the user
11418
07:18:27,360 --> 07:18:29,600
id variable
11419
07:18:29,600 --> 07:18:31,920
and then in here we're simply figuring
11420
07:18:31,920 --> 07:18:33,840
out if the user has currently liked a
11421
07:18:33,840 --> 07:18:35,600
post or not
11422
07:18:35,600 --> 07:18:37,840
finally if the user has liked the post
11423
07:18:37,840 --> 07:18:39,840
that means that they want to unlike it
11424
07:18:39,840 --> 07:18:41,920
and then we filter out their specific
11425
07:18:41,920 --> 07:18:43,040
like
11426
07:18:43,040 --> 07:18:45,280
but if the situation is different if
11427
07:18:45,280 --> 07:18:47,440
they haven't liked it we can say set
11428
07:18:47,440 --> 07:18:49,360
likes equal to
11429
07:18:49,360 --> 07:18:51,760
we spread all of the current likes and
11430
07:18:51,760 --> 07:18:54,638
then we want to add one new one and that
11431
07:18:54,638 --> 07:18:57,520
is that same user id variable we created
11432
07:18:57,520 --> 07:18:59,440
right here
11433
07:18:59,440 --> 07:19:01,200
and why is this faster than simply
11434
07:19:01,200 --> 07:19:02,958
updating the database immediately
11435
07:19:02,958 --> 07:19:04,718
because updates to the database are
11436
07:19:04,718 --> 07:19:07,360
asynchronous and they take time but this
11437
07:19:07,360 --> 07:19:09,840
is gonna happen instantly
11438
07:19:09,840 --> 07:19:12,878
finally we need to use our likes as you
11439
07:19:12,878 --> 07:19:15,040
can see they're not currently being used
11440
07:19:15,040 --> 07:19:17,520
right here we have to exchange every
11441
07:19:17,520 --> 07:19:19,360
single place where we mention pose that
11442
07:19:19,360 --> 07:19:22,160
likes and simply use our likes so that's
11443
07:19:22,160 --> 07:19:23,520
happening here
11444
07:19:23,520 --> 07:19:26,080
you can do control f and then find pose
11445
07:19:26,080 --> 07:19:28,638
that likes and simply replace that with
11446
07:19:28,638 --> 07:19:30,798
likes then you can click enter a few
11447
07:19:30,798 --> 07:19:33,280
times and it's going to fill that for
11448
07:19:33,280 --> 07:19:34,798
you
11449
07:19:34,798 --> 07:19:37,040
there we go so in all of the places
11450
07:19:37,040 --> 07:19:39,360
where we used pose.likes you simply have
11451
07:19:39,360 --> 07:19:42,320
to switch it to simply likes
11452
07:19:42,320 --> 07:19:44,000
there might have been a lot of code in
11453
07:19:44,000 --> 07:19:45,600
here so if you mess something up that's
11454
07:19:45,600 --> 07:19:47,920
completely fine you already know where
11455
07:19:47,920 --> 07:19:49,920
the link to the entire github repository
11456
07:19:49,920 --> 07:19:52,160
is so make sure to copy the file if it's
11457
07:19:52,160 --> 07:19:54,478
not working for you okay before we
11458
07:19:54,478 --> 07:19:56,240
finally take a look in the browser i
11459
07:19:56,240 --> 07:19:58,240
think we also have to replace this part
11460
07:19:58,240 --> 07:20:01,120
here with only likes and let's check it
11461
07:20:01,120 --> 07:20:02,160
out
11462
07:20:02,160 --> 07:20:05,200
okay let me try to like something
11463
07:20:05,200 --> 07:20:07,520
that was instantaneous take a look at
11464
07:20:07,520 --> 07:20:10,240
that i can like and unlike all the posts
11465
07:20:10,240 --> 07:20:12,718
incredibly quickly
11466
07:20:12,718 --> 07:20:15,840
and if i refresh the page
11467
07:20:15,840 --> 07:20:17,920
as you can see it's going to be updated
11468
07:20:17,920 --> 07:20:20,160
in the database i can try unliking
11469
07:20:20,160 --> 07:20:21,760
everything now
11470
07:20:21,760 --> 07:20:24,798
and then we're going to reload the page
11471
07:20:24,798 --> 07:20:27,040
and as you can see everything was
11472
07:20:27,040 --> 07:20:29,440
successfully stored in the database
11473
07:20:29,440 --> 07:20:31,360
but now the feedback that the user
11474
07:20:31,360 --> 07:20:34,958
receives is instantaneous look at that i
11475
07:20:34,958 --> 07:20:36,558
would say it's less than 100
11476
07:20:36,558 --> 07:20:38,080
milliseconds it's basically
11477
07:20:38,080 --> 07:20:39,680
instantaneous
11478
07:20:39,680 --> 07:20:41,600
at this point everything seems to be
11479
07:20:41,600 --> 07:20:43,600
working great and the last thing that we
11480
07:20:43,600 --> 07:20:45,360
have to do to make this a complete
11481
07:20:45,360 --> 07:20:47,840
project is to deploy it on the internet
11482
07:20:47,840 --> 07:20:49,520
so you can share it with your potential
11483
07:20:49,520 --> 07:20:52,320
employers and friends
11484
07:20:52,320 --> 07:20:54,798
so let's do that right away
11485
07:20:54,798 --> 07:20:56,320
to deploy the back inside of our
11486
07:20:56,320 --> 07:20:59,200
application we're going to use heroku
11487
07:20:59,200 --> 07:21:01,120
when you log in you should see something
11488
07:21:01,120 --> 07:21:02,638
similar to this
11489
07:21:02,638 --> 07:21:04,798
at the top right you can see a new
11490
07:21:04,798 --> 07:21:07,600
button where you can create a new app
11491
07:21:07,600 --> 07:21:09,440
we can choose the app name i'm going to
11492
07:21:09,440 --> 07:21:11,840
use something really random like mirn
11493
07:21:11,840 --> 07:21:14,558
memories js mastery that should be
11494
07:21:14,558 --> 07:21:15,600
available
11495
07:21:15,600 --> 07:21:17,680
and you can choose your region
11496
07:21:17,680 --> 07:21:19,360
and then in here you'll get all the
11497
07:21:19,360 --> 07:21:21,360
instructions of how you can deploy your
11498
07:21:21,360 --> 07:21:24,958
application using the heroku cli
11499
07:21:24,958 --> 07:21:26,558
the first thing you have to do is
11500
07:21:26,558 --> 07:21:29,440
download and install the heroku cli for
11501
07:21:29,440 --> 07:21:31,920
your own operating system
11502
07:21:31,920 --> 07:21:33,600
after you do that you can follow my
11503
07:21:33,600 --> 07:21:34,558
steps
11504
07:21:34,558 --> 07:21:36,478
before we start writing the commands we
11505
07:21:36,478 --> 07:21:38,160
just have to make sure to change just a
11506
07:21:38,160 --> 07:21:40,478
few things in the code
11507
07:21:40,478 --> 07:21:43,280
first of all go to the package.json off
11508
07:21:43,280 --> 07:21:44,398
the server side
11509
07:21:44,398 --> 07:21:46,080
and then in there change this from
11510
07:21:46,080 --> 07:21:48,478
nodemon to simplynode when you run a
11511
07:21:48,478 --> 07:21:50,080
server you don't have to constantly
11512
07:21:50,080 --> 07:21:53,280
restart it so we're only gonna need node
11513
07:21:53,280 --> 07:21:54,798
and then the second thing is if you
11514
07:21:54,798 --> 07:21:56,638
don't already have it make sure to
11515
07:21:56,638 --> 07:21:59,840
rename this to dot proc file we need to
11516
07:21:59,840 --> 07:22:02,320
have the dot proc file where we say web
11517
07:22:02,320 --> 07:22:05,120
is equal to npm run start
11518
07:22:05,120 --> 07:22:06,798
great
11519
07:22:06,798 --> 07:22:08,638
and the last thing just so we know if
11520
07:22:08,638 --> 07:22:10,398
our server is running we can go to our
11521
07:22:10,398 --> 07:22:13,520
index.js and then in there we can create
11522
07:22:13,520 --> 07:22:15,520
the app.getrout
11523
07:22:15,520 --> 07:22:16,958
app.get
11524
07:22:16,958 --> 07:22:19,440
it's going to be just slash and then in
11525
07:22:19,440 --> 07:22:22,000
there we can get the rec and the res and
11526
07:22:22,000 --> 07:22:25,440
we can do something like res.send
11527
07:22:25,440 --> 07:22:28,000
app is running this is just the message
11528
07:22:28,000 --> 07:22:29,520
that we're going to get in the browser
11529
07:22:29,520 --> 07:22:31,200
so that we know that their server is
11530
07:22:31,200 --> 07:22:32,478
running
11531
07:22:32,478 --> 07:22:34,718
okay with those changes ready i'm going
11532
07:22:34,718 --> 07:22:36,958
to close these files and let's follow
11533
07:22:36,958 --> 07:22:38,558
the process
11534
07:22:38,558 --> 07:22:41,680
run the heroku login
11535
07:22:41,680 --> 07:22:44,558
press any key
11536
07:22:44,878 --> 07:22:47,280
that's going to prompt you to log in
11537
07:22:47,280 --> 07:22:49,600
after you're logged in make sure to run
11538
07:22:49,600 --> 07:22:53,398
heroku git clone
11539
07:22:54,080 --> 07:22:56,320
and make sure that you are in the server
11540
07:22:56,320 --> 07:22:58,398
directory so you need to be in the
11541
07:22:58,398 --> 07:23:00,160
server directory
11542
07:23:00,160 --> 07:23:01,920
the process for you might be just a bit
11543
07:23:01,920 --> 07:23:04,080
different but just follow the steps you
11544
07:23:04,080 --> 07:23:06,240
need to be in the server folder run the
11545
07:23:06,240 --> 07:23:08,878
heroku remote to add and finally we need
11546
07:23:08,878 --> 07:23:11,120
to push all of our changes by running
11547
07:23:11,120 --> 07:23:14,558
git add we can also do a git commit and
11548
07:23:14,558 --> 07:23:18,958
finally git bush heroku master
11549
07:23:19,200 --> 07:23:21,520
this is going to deploy all the changes
11550
07:23:21,520 --> 07:23:23,440
so that your server is going to be live
11551
07:23:23,440 --> 07:23:25,920
and deployed on the internet
11552
07:23:25,920 --> 07:23:28,080
okay we got a confirmation message
11553
07:23:28,080 --> 07:23:30,320
verifying done
11554
07:23:30,320 --> 07:23:32,718
we can clear the terminal and expand our
11555
07:23:32,718 --> 07:23:34,160
browser a bit
11556
07:23:34,160 --> 07:23:37,120
now if i unzoom this a bit on the top
11557
07:23:37,120 --> 07:23:41,280
right side you should see open app
11558
07:23:41,680 --> 07:23:43,920
once you click that at the top left you
11559
07:23:43,920 --> 07:23:46,240
should see app is running
11560
07:23:46,240 --> 07:23:48,000
that means that we are ready to take
11561
07:23:48,000 --> 07:23:51,040
this url and connect our frontend to it
11562
07:23:51,040 --> 07:23:53,200
so copy that url
11563
07:23:53,200 --> 07:23:55,840
let's go back to our code this time to
11564
07:23:55,840 --> 07:23:57,280
the client side
11565
07:23:57,280 --> 07:23:59,280
we're going to go to the client
11566
07:23:59,280 --> 07:24:00,558
source
11567
07:24:00,558 --> 07:24:03,040
api index.js
11568
07:24:03,040 --> 07:24:04,798
and then in here you're going to replace
11569
07:24:04,798 --> 07:24:08,558
this base url with your own new deployed
11570
07:24:08,558 --> 07:24:10,398
server url
11571
07:24:10,398 --> 07:24:12,718
that is one thing that we have to do and
11572
07:24:12,718 --> 07:24:14,798
the second thing is that you have to go
11573
07:24:14,798 --> 07:24:17,200
to the public folder and then in here
11574
07:24:17,200 --> 07:24:19,920
you have to add one special file we have
11575
07:24:19,920 --> 07:24:24,080
to add the underscore redirects file so
11576
07:24:24,080 --> 07:24:26,478
just underscore redirect
11577
07:24:26,478 --> 07:24:28,398
then you can put forward slash and then
11578
07:24:28,398 --> 07:24:31,440
the asterisk finally forward slash
11579
07:24:31,440 --> 07:24:33,440
index.html
11580
07:24:33,440 --> 07:24:36,958
and finally 200.
11581
07:24:36,958 --> 07:24:38,558
this is the only thing you have to have
11582
07:24:38,558 --> 07:24:39,760
in this file
11583
07:24:39,760 --> 07:24:41,360
once you do that you can close
11584
07:24:41,360 --> 07:24:44,080
everything and we're now going to cd
11585
07:24:44,080 --> 07:24:47,040
back into the root and then finally cd
11586
07:24:47,040 --> 07:24:48,798
into the client side
11587
07:24:48,798 --> 07:24:50,478
when you're in the client side we want
11588
07:24:50,478 --> 07:24:52,558
to build out our entire react
11589
07:24:52,558 --> 07:24:57,120
application by running npm run build
11590
07:24:57,120 --> 07:24:59,280
while our build is running we can go
11591
07:24:59,280 --> 07:25:01,520
back to the hostingers dashboard
11592
07:25:01,520 --> 07:25:03,760
to visit it you can click the link down
11593
07:25:03,760 --> 07:25:06,000
in the description and then log in or
11594
07:25:06,000 --> 07:25:09,600
simply go to hpanel.hostinger.com
11595
07:25:09,600 --> 07:25:11,280
if you purchase the premium shared
11596
07:25:11,280 --> 07:25:13,440
hosting then you should be able to see
11597
07:25:13,440 --> 07:25:16,000
it here under setup and you can click
11598
07:25:16,000 --> 07:25:18,080
this yellow setup button
11599
07:25:18,080 --> 07:25:20,878
then you'll see a startup process you
11600
07:25:20,878 --> 07:25:22,558
can click start now
11601
07:25:22,558 --> 07:25:24,718
you're going to be asked a few questions
11602
07:25:24,718 --> 07:25:26,878
but the most important thing here is at
11603
07:25:26,878 --> 07:25:28,798
the bottom there's going to be a text
11604
07:25:28,798 --> 07:25:31,120
that's going to say skip i'll start from
11605
07:25:31,120 --> 07:25:34,080
scratch so you can click that to fully
11606
07:25:34,080 --> 07:25:36,718
set up your own domain
11607
07:25:36,718 --> 07:25:39,280
after you've done that you should see a
11608
07:25:39,280 --> 07:25:41,440
page that looks like this
11609
07:25:41,440 --> 07:25:44,160
your domain name is going to be here and
11610
07:25:44,160 --> 07:25:46,080
you'll have a lot of different tools
11611
07:25:46,080 --> 07:25:48,160
that you can use right here on your
11612
07:25:48,160 --> 07:25:49,680
disposal
11613
07:25:49,680 --> 07:25:52,080
there are two different things we need
11614
07:25:52,080 --> 07:25:55,760
the first one is ssl certificate so you
11615
07:25:55,760 --> 07:25:58,478
can type ssl click right here
11616
07:25:58,478 --> 07:26:00,240
and then the only thing you have to do
11617
07:26:00,240 --> 07:26:03,120
is simply click install ssl
11618
07:26:03,120 --> 07:26:05,760
and there we go ssl is being installed
11619
07:26:05,760 --> 07:26:09,680
this is going to allow https security as
11620
07:26:09,680 --> 07:26:11,600
soon as i've reloaded the page the
11621
07:26:11,600 --> 07:26:14,558
status changed to active it cannot be
11622
07:26:14,558 --> 07:26:16,558
any simpler than this
11623
07:26:16,558 --> 07:26:18,718
then we can go to dashboard
11624
07:26:18,718 --> 07:26:21,120
and the only thing we need is going to
11625
07:26:21,120 --> 07:26:24,398
be the file manager right here let's
11626
07:26:24,398 --> 07:26:25,440
click it
11627
07:26:25,440 --> 07:26:28,160
inside of a new window you're going to
11628
07:26:28,160 --> 07:26:31,200
see your hpanel dashboard and you can go
11629
07:26:31,200 --> 07:26:34,080
to public underscore html
11630
07:26:34,080 --> 07:26:36,040
there for now you'll see a
11631
07:26:36,040 --> 07:26:40,398
default.php site which you can delete
11632
07:26:40,478 --> 07:26:43,040
and then you can go back to your code
11633
07:26:43,040 --> 07:26:45,360
and open up that build folder we've
11634
07:26:45,360 --> 07:26:46,878
created
11635
07:26:46,878 --> 07:26:48,478
once you open it up in your file
11636
07:26:48,478 --> 07:26:50,638
explorer that's going to be memories and
11637
07:26:50,638 --> 07:26:53,200
then client you should be able to see
11638
07:26:53,200 --> 07:26:55,440
your build folder right here
11639
07:26:55,440 --> 07:26:57,840
the only thing you have to do is open it
11640
07:26:57,840 --> 07:26:58,638
up
11641
07:26:58,638 --> 07:27:01,520
and then simply drag and drop all the
11642
07:27:01,520 --> 07:27:02,478
files
11643
07:27:02,478 --> 07:27:05,360
right inside of here
11644
07:27:05,360 --> 07:27:07,920
there we go in just a few seconds they
11645
07:27:07,920 --> 07:27:09,920
were uploaded
11646
07:27:09,920 --> 07:27:12,878
now we can go back to your hosting panel
11647
07:27:12,878 --> 07:27:15,440
reload the page and we can type our
11648
07:27:15,440 --> 07:27:17,920
domain name in the navigation bar and
11649
07:27:17,920 --> 07:27:20,718
simply press enter
11650
07:27:20,718 --> 07:27:23,120
our memories are loading and we can see
11651
07:27:23,120 --> 07:27:25,360
the posts which means that our front end
11652
07:27:25,360 --> 07:27:28,240
is not only hosted online but also
11653
07:27:28,240 --> 07:27:30,638
connected to our backend which is also
11654
07:27:30,638 --> 07:27:32,320
hosted online
11655
07:27:32,320 --> 07:27:35,840
this works perfectly we can paginate we
11656
07:27:35,840 --> 07:27:37,600
can see all of the great features that
11657
07:27:37,600 --> 07:27:40,718
we just created in this video
11658
07:27:40,718 --> 07:27:42,320
it's so good seeing the application
11659
07:27:42,320 --> 07:27:43,760
you've been working on for quite some
11660
07:27:43,760 --> 07:27:46,478
time now being almost done
11661
07:27:46,478 --> 07:27:48,000
of course there are always some new
11662
07:27:48,000 --> 07:27:49,360
features that you can keep adding to
11663
07:27:49,360 --> 07:27:51,280
your applications and keep improving
11664
07:27:51,280 --> 07:27:52,160
them
11665
07:27:52,160 --> 07:27:54,398
that was it for this video if it was
11666
07:27:54,398 --> 07:27:56,320
helpful definitely feel free to leave a
11667
07:27:56,320 --> 07:27:58,320
like and comment and if you'd like to
11668
07:27:58,320 --> 07:28:00,558
build more projects with me make sure to
11669
07:28:00,558 --> 07:28:02,320
subscribe and turn on the bell
11670
07:28:02,320 --> 07:28:03,840
notifications
11671
07:28:03,840 --> 07:28:05,680
if you turn on the bell notifications
11672
07:28:05,680 --> 07:28:07,200
you can be sure that you're not going to
11673
07:28:07,200 --> 07:28:09,280
miss the fifth part of the mirn series
11674
07:28:09,280 --> 07:28:11,360
when i upload it with that said thank
11675
07:28:11,360 --> 07:28:13,280
you so much for watching see you in the
11676
07:28:13,280 --> 07:28:17,840
next one and have a wonderful day
11677
07:28:23,030 --> 07:28:30,200
[Music]
803275
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.