Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,000 --> 00:00:02,580
mobile development is for children all
2
00:00:02,580 --> 00:00:04,019
the cool people are doing web
3
00:00:04,019 --> 00:00:06,420
development so in this free course we
4
00:00:06,420 --> 00:00:08,280
will learn how to build a moon app from
5
00:00:08,280 --> 00:00:12,259
scratch Merin stands for mongodb Express
6
00:00:12,259 --> 00:00:16,500
react and node and those four together
7
00:00:16,500 --> 00:00:19,500
make a very popular text deck to build
8
00:00:19,500 --> 00:00:22,140
full stack web applications full stack
9
00:00:22,140 --> 00:00:23,760
means that we are building both the
10
00:00:23,760 --> 00:00:25,859
front end which is the part that you see
11
00:00:25,859 --> 00:00:28,560
on the screen in your web browser and
12
00:00:28,560 --> 00:00:31,019
the back end which is the server that
13
00:00:31,019 --> 00:00:33,660
your website communicates with and the
14
00:00:33,660 --> 00:00:35,160
server on the other hand has a
15
00:00:35,160 --> 00:00:37,320
connection to the database and so on but
16
00:00:37,320 --> 00:00:38,700
we will learn the details of this
17
00:00:38,700 --> 00:00:40,800
throughout this course we will also be
18
00:00:40,800 --> 00:00:43,620
using typescript in this course instead
19
00:00:43,620 --> 00:00:46,920
of vanilla JavaScript typescript is a
20
00:00:46,920 --> 00:00:49,260
superset of JavaScript so it's basically
21
00:00:49,260 --> 00:00:52,020
a like a layer on top of it you can use
22
00:00:52,020 --> 00:00:53,579
it anywhere you can use normal
23
00:00:53,579 --> 00:00:56,760
JavaScript but it adds strong typing to
24
00:00:56,760 --> 00:00:59,399
the language because if you build a
25
00:00:59,399 --> 00:01:02,100
medium size or a larger project in pure
26
00:01:02,100 --> 00:01:03,899
JavaScript than you are a lunatic
27
00:01:03,899 --> 00:01:05,880
because a normal JavaScript you don't
28
00:01:05,880 --> 00:01:07,979
have types and you can basically put
29
00:01:07,979 --> 00:01:10,619
every value into any variable at any
30
00:01:10,619 --> 00:01:12,240
time and it's just a matter of time
31
00:01:12,240 --> 00:01:14,100
until this whole thing explodes into
32
00:01:14,100 --> 00:01:16,439
your face typescript is absolutely
33
00:01:16,439 --> 00:01:18,840
Superior it's an awesome language and
34
00:01:18,840 --> 00:01:20,100
this is why we will use it in this
35
00:01:20,100 --> 00:01:22,619
course so before we continue let's take
36
00:01:22,619 --> 00:01:24,000
a look at the project we will actually
37
00:01:24,000 --> 00:01:26,159
be building here now as you can see the
38
00:01:26,159 --> 00:01:28,860
project is relatively simple but it will
39
00:01:28,860 --> 00:01:31,200
build a full Foundation that you need to
40
00:01:31,200 --> 00:01:33,360
build your own burn applications and I'm
41
00:01:33,360 --> 00:01:35,040
actually not a fan of these tutorials
42
00:01:35,040 --> 00:01:36,720
that adds too many features because
43
00:01:36,720 --> 00:01:39,720
usually no single topic is explained
44
00:01:39,720 --> 00:01:41,700
really properly the tutorial is really
45
00:01:41,700 --> 00:01:43,619
bloated and lengthy and no one actually
46
00:01:43,619 --> 00:01:46,200
finishes it so I like to focus on the
47
00:01:46,200 --> 00:01:48,060
important parts make sure that you
48
00:01:48,060 --> 00:01:50,100
understand these Concepts and then you
49
00:01:50,100 --> 00:01:52,020
can add more features on top of it later
50
00:01:52,020 --> 00:01:53,939
the goal of this course is to actually
51
00:01:53,939 --> 00:01:56,100
have you finished the project and we
52
00:01:56,100 --> 00:01:58,799
will even deploy it to real web server
53
00:01:58,799 --> 00:02:00,780
so you can run it on the internet and
54
00:02:00,780 --> 00:02:02,460
not just your local machine we will do
55
00:02:02,460 --> 00:02:03,720
that in the second video I will talk
56
00:02:03,720 --> 00:02:05,759
about this later and if you ever had
57
00:02:05,759 --> 00:02:07,500
trouble understanding the moon stack
58
00:02:07,500 --> 00:02:09,479
then this video will fix it because I
59
00:02:09,479 --> 00:02:11,400
will explain all the concepts that you
60
00:02:11,400 --> 00:02:13,319
need to know properly and as you can
61
00:02:13,319 --> 00:02:15,000
already tell we are building a Tinder
62
00:02:15,000 --> 00:02:17,160
clone no just kidding we are building a
63
00:02:17,160 --> 00:02:18,959
note-taking application and it will
64
00:02:18,959 --> 00:02:21,540
teach you all the crud operations so we
65
00:02:21,540 --> 00:02:25,340
will learn how to create newer nodes
66
00:02:27,780 --> 00:02:29,580
and add it to the database and the
67
00:02:29,580 --> 00:02:31,560
screen we will learn how to update
68
00:02:31,560 --> 00:02:34,580
existing notes
69
00:02:35,220 --> 00:02:37,620
how to delete nodes and of course how to
70
00:02:37,620 --> 00:02:39,780
display all these nodes on the screen we
71
00:02:39,780 --> 00:02:41,760
will also do some other stuff like
72
00:02:41,760 --> 00:02:43,920
simple form validation which is
73
00:02:43,920 --> 00:02:46,019
important so for example this title
74
00:02:46,019 --> 00:02:48,720
field here is required and only when we
75
00:02:48,720 --> 00:02:51,120
actually type something in we can save
76
00:02:51,120 --> 00:02:53,580
this node when we update a node this
77
00:02:53,580 --> 00:02:55,739
little text down here changes from
78
00:02:55,739 --> 00:02:58,080
created
79
00:02:58,080 --> 00:03:00,780
to the last updated timestamp we will
80
00:03:00,780 --> 00:03:02,760
learn how to navigate between different
81
00:03:02,760 --> 00:03:06,060
pages using react router Dom and for the
82
00:03:06,060 --> 00:03:08,400
UI we will use bootstrap which is a
83
00:03:08,400 --> 00:03:10,019
package that you can use for example
84
00:03:10,019 --> 00:03:11,580
with react
85
00:03:11,580 --> 00:03:14,099
that gives you a different UI components
86
00:03:14,099 --> 00:03:16,500
and design features
87
00:03:16,500 --> 00:03:19,080
and I know that Tailwind CSS is very
88
00:03:19,080 --> 00:03:21,300
popular at the moment but I still prefer
89
00:03:21,300 --> 00:03:23,220
bootstrap because I think it's easier to
90
00:03:23,220 --> 00:03:26,400
use it already brings stuff like modal
91
00:03:26,400 --> 00:03:28,500
dialogues for example that you have to
92
00:03:28,500 --> 00:03:31,560
build yourself when using Tailwind
93
00:03:31,560 --> 00:03:34,739
like this it's completely free it's in
94
00:03:34,739 --> 00:03:38,099
my opinion just easier to use and if we
95
00:03:38,099 --> 00:03:41,099
use CSS modules which we will do in this
96
00:03:41,099 --> 00:03:43,500
course then we don't have problems with
97
00:03:43,500 --> 00:03:46,680
clashes between different CSS classes so
98
00:03:46,680 --> 00:03:48,840
this makes the organizational of the CSS
99
00:03:48,840 --> 00:03:51,180
pretty simple in my opinion but if you
100
00:03:51,180 --> 00:03:52,680
don't know what any of this means don't
101
00:03:52,680 --> 00:03:54,180
worry we will learn all of this
102
00:03:54,180 --> 00:03:56,519
throughout this course
103
00:03:56,519 --> 00:03:58,980
so the navigation bar up here these
104
00:03:58,980 --> 00:04:01,080
buttons this model dialog that opens
105
00:04:01,080 --> 00:04:03,000
when we click a node all of this comes
106
00:04:03,000 --> 00:04:05,340
from bootstrap and we will also make our
107
00:04:05,340 --> 00:04:07,620
layout responsive so that when the
108
00:04:07,620 --> 00:04:09,599
screen size changes the layout changes
109
00:04:09,599 --> 00:04:12,239
which again is made easier by bootstrap
110
00:04:12,239 --> 00:04:14,580
so as you can see here at the full size
111
00:04:14,580 --> 00:04:17,940
we have three columns of nodes but when
112
00:04:17,940 --> 00:04:20,279
the screen gets smaller we have only two
113
00:04:20,279 --> 00:04:22,320
and then eventually we only have one
114
00:04:22,320 --> 00:04:24,360
column and also the menu up here
115
00:04:24,360 --> 00:04:26,520
collapses and as you can see this is a
116
00:04:26,520 --> 00:04:29,280
really nice view for displaying this
117
00:04:29,280 --> 00:04:31,139
website on a mobile phone
118
00:04:31,139 --> 00:04:33,720
so it's completely mobile responsive
119
00:04:33,720 --> 00:04:36,060
which bootstrap again makes pretty easy
120
00:04:36,060 --> 00:04:38,580
in my opinion entry will learn some CSS
121
00:04:38,580 --> 00:04:40,560
tricks like fading out this text here
122
00:04:40,560 --> 00:04:42,960
when the note gets too long
123
00:04:42,960 --> 00:04:44,759
so some of these little tricks and of
124
00:04:44,759 --> 00:04:46,979
course this Harbor Shadow effect and
125
00:04:46,979 --> 00:04:49,020
since this is supposed to be a full
126
00:04:49,020 --> 00:04:51,360
Foundation to build your own Moon apps
127
00:04:51,360 --> 00:04:53,340
we also handle authentication in this
128
00:04:53,340 --> 00:04:56,580
course using sessions and cookies so we
129
00:04:56,580 --> 00:04:58,560
can log out of our current account which
130
00:04:58,560 --> 00:05:01,259
makes all our nodes disappear and we can
131
00:05:01,259 --> 00:05:03,300
log into a different account or create a
132
00:05:03,300 --> 00:05:05,400
whole new one
133
00:05:05,400 --> 00:05:08,040
so because of my insane creativity I'm
134
00:05:08,040 --> 00:05:12,120
gonna call it ASD ASD the email will be
135
00:05:12,120 --> 00:05:15,960
similar and the password is ASD ASD okay
136
00:05:15,960 --> 00:05:17,580
this is a password suggestion this is
137
00:05:17,580 --> 00:05:19,440
not my actual password just making sure
138
00:05:19,440 --> 00:05:21,300
that I'm not leaking my personal data
139
00:05:21,300 --> 00:05:24,180
here anyway we can create a new account
140
00:05:24,180 --> 00:05:26,340
or this email address already exists so
141
00:05:26,340 --> 00:05:28,139
we also have validation for this on the
142
00:05:28,139 --> 00:05:29,639
back end
143
00:05:29,639 --> 00:05:32,400
so let's choose a different email
144
00:05:32,400 --> 00:05:33,780
address
145
00:05:33,780 --> 00:05:36,240
and try it again
146
00:05:36,240 --> 00:05:39,479
and we have our new account yeah let it
147
00:05:39,479 --> 00:05:40,979
go man
148
00:05:40,979 --> 00:05:43,020
which doesn't have any notes here so
149
00:05:43,020 --> 00:05:45,900
let's add a new note
150
00:05:45,900 --> 00:05:48,120
here as well
151
00:05:48,120 --> 00:05:50,580
which now belongs to this account when
152
00:05:50,580 --> 00:05:53,520
we log out and into our other account
153
00:05:53,520 --> 00:05:56,039
we won't see this node because each node
154
00:05:56,039 --> 00:05:58,800
is tied to a particular user isn't that
155
00:05:58,800 --> 00:06:01,139
amazing and again this is the foundation
156
00:06:01,139 --> 00:06:03,180
for you to later build more stuff on top
157
00:06:03,180 --> 00:06:06,060
of it this is the important pieces now
158
00:06:06,060 --> 00:06:07,740
we will talk about all these different
159
00:06:07,740 --> 00:06:09,960
features later in more detail and if
160
00:06:09,960 --> 00:06:11,880
you're wondering why I use cookie
161
00:06:11,880 --> 00:06:15,060
sessions here instead of JWT tokens I
162
00:06:15,060 --> 00:06:16,560
explain my reasoning behind this
163
00:06:16,560 --> 00:06:18,660
throughout the series but it's a nice
164
00:06:18,660 --> 00:06:20,940
and easy way to handle user sessions and
165
00:06:20,940 --> 00:06:23,759
it doesn't have some downsides that jwts
166
00:06:23,759 --> 00:06:25,860
have and after we have finished our
167
00:06:25,860 --> 00:06:27,720
project here we will deploy everything
168
00:06:27,720 --> 00:06:30,300
to a real web server only node which is
169
00:06:30,300 --> 00:06:32,400
a hosting provider you will find the
170
00:06:32,400 --> 00:06:34,800
second video only notes Channel and I
171
00:06:34,800 --> 00:06:36,300
will put a link to it into the video
172
00:06:36,300 --> 00:06:38,220
description below after you have
173
00:06:38,220 --> 00:06:39,660
finished this make sure to also watch
174
00:06:39,660 --> 00:06:41,819
the second video it will not be as long
175
00:06:41,819 --> 00:06:43,680
we will just deploy this whole thing and
176
00:06:43,680 --> 00:06:45,840
then you will have a real full stackdap
177
00:06:45,840 --> 00:06:48,180
application on the real web hosting then
178
00:06:48,180 --> 00:06:49,860
you can connect it to a domain and then
179
00:06:49,860 --> 00:06:51,479
you have your own website for your
180
00:06:51,479 --> 00:06:54,000
portfolio or you can build your own and
181
00:06:54,000 --> 00:06:55,620
project out of it whatever you want
182
00:06:55,620 --> 00:06:58,860
entry note was also so nice to provide a
183
00:06:58,860 --> 00:07:01,500
free credit for my viewers so you can go
184
00:07:01,500 --> 00:07:05,220
to leno.com coding and flow and you get
185
00:07:05,220 --> 00:07:07,919
a hundred dollars free credit available
186
00:07:07,919 --> 00:07:10,440
for 60 days again I will put a link to
187
00:07:10,440 --> 00:07:12,000
this into the video description below
188
00:07:12,000 --> 00:07:14,280
together with the link to the second
189
00:07:14,280 --> 00:07:16,319
video where we deploy everything now
190
00:07:16,319 --> 00:07:18,660
let's talk about the prerequisites of
191
00:07:18,660 --> 00:07:20,759
this course because this is not a foia
192
00:07:20,759 --> 00:07:22,740
beginner course we will not learn the
193
00:07:22,740 --> 00:07:24,840
absolute JavaScript Basics so we will
194
00:07:24,840 --> 00:07:26,819
not learn what the for Loop is or what a
195
00:07:26,819 --> 00:07:28,680
variable is I assume that you already
196
00:07:28,680 --> 00:07:30,419
know that but you don't have to know
197
00:07:30,419 --> 00:07:32,460
JavaScript for that to be honest if you
198
00:07:32,460 --> 00:07:34,080
know any programming language you will
199
00:07:34,080 --> 00:07:35,580
be able to follow along because they're
200
00:07:35,580 --> 00:07:37,860
very similar and I will explain the more
201
00:07:37,860 --> 00:07:39,720
advanced language features that we will
202
00:07:39,720 --> 00:07:41,819
use I will also explain how typescript
203
00:07:41,819 --> 00:07:43,500
works and we will install everything
204
00:07:43,500 --> 00:07:45,840
together if you know a language like
205
00:07:45,840 --> 00:07:48,900
kotlin for example also I think Swift is
206
00:07:48,900 --> 00:07:50,759
similar or Java then it will be very
207
00:07:50,759 --> 00:07:52,919
easy for you to understand typescript
208
00:07:52,919 --> 00:07:53,759
because
209
00:07:53,759 --> 00:07:55,620
the same thing at the end it's very
210
00:07:55,620 --> 00:07:57,479
similar we will also be using
211
00:07:57,479 --> 00:07:59,699
asynchronous JavaScript features so
212
00:07:59,699 --> 00:08:02,639
promises and async and the weight and I
213
00:08:02,639 --> 00:08:04,800
will again not expand this from scratch
214
00:08:04,800 --> 00:08:06,960
but you can just follow along in my
215
00:08:06,960 --> 00:08:08,460
opinion because these features are very
216
00:08:08,460 --> 00:08:10,380
straightforward and when you don't
217
00:08:10,380 --> 00:08:12,180
understand something either just follow
218
00:08:12,180 --> 00:08:14,580
along or Google it which is even better
219
00:08:14,580 --> 00:08:16,979
and again if you know kotlin for example
220
00:08:16,979 --> 00:08:19,259
then async await is very similar to
221
00:08:19,259 --> 00:08:21,599
kotlin coroutines and there are similar
222
00:08:21,599 --> 00:08:24,180
features in other languages as well the
223
00:08:24,180 --> 00:08:27,180
same goes for HTML and CSS I don't
224
00:08:27,180 --> 00:08:28,800
expand everything from the absolute
225
00:08:28,800 --> 00:08:30,960
ground up but you just need to follow
226
00:08:30,960 --> 00:08:33,120
along and unless you are a complete
227
00:08:33,120 --> 00:08:34,559
beginner and have never touched any
228
00:08:34,559 --> 00:08:36,360
programming language then I think you
229
00:08:36,360 --> 00:08:38,219
will be able to understand everything
230
00:08:38,219 --> 00:08:40,200
and whatever you don't understand just
231
00:08:40,200 --> 00:08:43,679
Google it I'm also a fan of not learning
232
00:08:43,679 --> 00:08:46,200
everything from the absolute Basics
233
00:08:46,200 --> 00:08:48,600
upwards because I think it's a boring
234
00:08:48,600 --> 00:08:50,459
approach to learning programming I much
235
00:08:50,459 --> 00:08:52,980
rather create a project and be okay with
236
00:08:52,980 --> 00:08:55,380
not understanding in every detail at the
237
00:08:55,380 --> 00:08:57,060
beginning and then you can still fill in
238
00:08:57,060 --> 00:08:58,740
the gaps later for example I never
239
00:08:58,740 --> 00:09:01,200
formally learned CSS I never learned the
240
00:09:01,200 --> 00:09:03,839
basics of it but I built actual projects
241
00:09:03,839 --> 00:09:05,940
that use CSS and when I later was
242
00:09:05,940 --> 00:09:08,880
wondering what a specific keyword or
243
00:09:08,880 --> 00:09:11,519
feature meant then I Googled it and
244
00:09:11,519 --> 00:09:13,680
learned it when I actually needed it I
245
00:09:13,680 --> 00:09:15,600
think that's a much more fun approach to
246
00:09:15,600 --> 00:09:17,100
learning programming than going through
247
00:09:17,100 --> 00:09:18,839
these beginner courses where you spent
248
00:09:18,839 --> 00:09:20,220
three weeks just learning the
249
00:09:20,220 --> 00:09:22,440
fundamentals of CSS that's super boring
250
00:09:22,440 --> 00:09:24,660
we will also install all the necessary
251
00:09:24,660 --> 00:09:26,459
software together so we will install
252
00:09:26,459 --> 00:09:29,220
node.js all the necessary packages we
253
00:09:29,220 --> 00:09:31,260
will set up our mongodb database you
254
00:09:31,260 --> 00:09:33,300
don't need to know anything about it and
255
00:09:33,300 --> 00:09:34,980
if something is unclear either Google it
256
00:09:34,980 --> 00:09:36,480
or you can also leave a comment below
257
00:09:36,480 --> 00:09:38,700
and ask other people or ask me I'm
258
00:09:38,700 --> 00:09:40,860
reading all of my comments basically I
259
00:09:40,860 --> 00:09:43,080
don't respond to all of them but I read
260
00:09:43,080 --> 00:09:45,360
all of them just one more learning tip
261
00:09:45,360 --> 00:09:47,279
for this course instead of just
262
00:09:47,279 --> 00:09:49,200
following along everything step by step
263
00:09:49,200 --> 00:09:51,240
I recommend that you build a project
264
00:09:51,240 --> 00:09:53,339
that's slightly different for example
265
00:09:53,339 --> 00:09:55,740
create a slightly different layout use
266
00:09:55,740 --> 00:09:57,660
different data types maybe even don't
267
00:09:57,660 --> 00:10:00,000
build a node app like I do here maybe
268
00:10:00,000 --> 00:10:02,339
build a to-do list app so that you use
269
00:10:02,339 --> 00:10:04,140
the same Concepts that you learn here
270
00:10:04,140 --> 00:10:06,240
but you apply them in a slightly
271
00:10:06,240 --> 00:10:07,800
different way I really like this
272
00:10:07,800 --> 00:10:09,720
approach because it forces you to
273
00:10:09,720 --> 00:10:12,120
understand what I'm explaining here and
274
00:10:12,120 --> 00:10:14,580
not just blindly follow along if you are
275
00:10:14,580 --> 00:10:16,200
a beginner and it's too difficult for
276
00:10:16,200 --> 00:10:17,760
you then you can also just follow along
277
00:10:17,760 --> 00:10:20,580
step by step no problem it's just a
278
00:10:20,580 --> 00:10:22,440
little learning tip I also want to
279
00:10:22,440 --> 00:10:24,660
quickly explain how a react Works
280
00:10:24,660 --> 00:10:27,360
overall so let's take a look at the app
281
00:10:27,360 --> 00:10:30,660
again when you use a react website then
282
00:10:30,660 --> 00:10:32,459
you will notice that it feels more like
283
00:10:32,459 --> 00:10:34,740
a mobile app almost rather than a
284
00:10:34,740 --> 00:10:37,320
classical website for example when we
285
00:10:37,320 --> 00:10:39,779
navigate to a different page you'll
286
00:10:39,779 --> 00:10:41,399
notice that the navbar here at the top
287
00:10:41,399 --> 00:10:44,579
stays in place and it doesn't reload so
288
00:10:44,579 --> 00:10:46,140
we are not actually reloading the whole
289
00:10:46,140 --> 00:10:49,019
page because a react app by default is a
290
00:10:49,019 --> 00:10:51,240
single page basically and all the
291
00:10:51,240 --> 00:10:52,800
contents within the single page are
292
00:10:52,800 --> 00:10:54,959
loaded by JavaScript script this is why
293
00:10:54,959 --> 00:10:56,579
when we switch back to the node screen
294
00:10:56,579 --> 00:10:58,260
you see this progress Bar for a short
295
00:10:58,260 --> 00:10:59,820
moment this is because we are not
296
00:10:59,820 --> 00:11:01,860
actually getting the full page sent from
297
00:11:01,860 --> 00:11:03,839
the server we are only getting the shell
298
00:11:03,839 --> 00:11:06,540
which is this page with all the static
299
00:11:06,540 --> 00:11:08,880
elements but without the data that we
300
00:11:08,880 --> 00:11:10,380
are loading from the server so without
301
00:11:10,380 --> 00:11:12,060
our nodes
302
00:11:12,060 --> 00:11:14,820
and then with JavaScript code we are
303
00:11:14,820 --> 00:11:16,860
loading these nodes and this is why we
304
00:11:16,860 --> 00:11:18,660
see a short progress bar and then all
305
00:11:18,660 --> 00:11:20,579
this pops up here on the screen just
306
00:11:20,579 --> 00:11:22,920
like a normal app our code is sending a
307
00:11:22,920 --> 00:11:25,260
HTTP request to the server and it gets
308
00:11:25,260 --> 00:11:27,480
back Json and then turns it into these
309
00:11:27,480 --> 00:11:28,920
nodes which you can see on the screen
310
00:11:28,920 --> 00:11:30,899
you don't need to understand this right
311
00:11:30,899 --> 00:11:32,760
now we will take a closer look at this
312
00:11:32,760 --> 00:11:34,500
later but if you're coming from mobile
313
00:11:34,500 --> 00:11:36,720
app development for example then this
314
00:11:36,720 --> 00:11:38,760
will feel very familiar to you because
315
00:11:38,760 --> 00:11:41,399
react websites are basically apps on the
316
00:11:41,399 --> 00:11:44,279
internet now since JavaScript is used to
317
00:11:44,279 --> 00:11:46,680
load the data this also means that react
318
00:11:46,680 --> 00:11:48,600
apps by default don't work without
319
00:11:48,600 --> 00:11:50,820
JavaScript so in the Chrome developer
320
00:11:50,820 --> 00:11:52,560
twitch we can actually disable
321
00:11:52,560 --> 00:11:54,000
JavaScript
322
00:11:54,000 --> 00:11:56,640
reload the page and you can see this
323
00:11:56,640 --> 00:11:58,860
little text here you need to enable
324
00:11:58,860 --> 00:12:01,320
JavaScript to run this app but this is
325
00:12:01,320 --> 00:12:04,019
usually not a problem because about 98
326
00:12:04,019 --> 00:12:06,360
of all websites on the internet use
327
00:12:06,360 --> 00:12:08,459
JavaScript so it's very aware for our
328
00:12:08,459 --> 00:12:10,380
client to not have JavaScript enabled
329
00:12:10,380 --> 00:12:13,140
because it's required to view almost any
330
00:12:13,140 --> 00:12:15,600
website on the internet so let's turn
331
00:12:15,600 --> 00:12:18,500
this back on here
332
00:12:19,440 --> 00:12:22,019
and then we can see our website again
333
00:12:22,019 --> 00:12:24,660
now the so-called client-side rendering
334
00:12:24,660 --> 00:12:27,420
approach where the website loads more
335
00:12:27,420 --> 00:12:29,579
data from the server after it is already
336
00:12:29,579 --> 00:12:31,800
displayed in the browser has benefits
337
00:12:31,800 --> 00:12:33,779
and downsides you can Google them and
338
00:12:33,779 --> 00:12:35,640
read up on them if you want and there
339
00:12:35,640 --> 00:12:37,380
are also Frameworks that combine the
340
00:12:37,380 --> 00:12:40,260
best of both worlds for example next JS
341
00:12:40,260 --> 00:12:42,240
which is another framework on top of
342
00:12:42,240 --> 00:12:44,160
react where you have more control over
343
00:12:44,160 --> 00:12:46,620
what data you will load client-side and
344
00:12:46,620 --> 00:12:49,320
what pages you load servers out where
345
00:12:49,320 --> 00:12:51,839
all the data is already in the page that
346
00:12:51,839 --> 00:12:54,060
comes from the server I'm a huge fan of
347
00:12:54,060 --> 00:12:56,579
next JS it's my favorite framework for
348
00:12:56,579 --> 00:12:58,680
my own projects but it's quite an
349
00:12:58,680 --> 00:13:01,320
advanced framework and again it's built
350
00:13:01,320 --> 00:13:03,060
on top of react so you should learn
351
00:13:03,060 --> 00:13:05,339
normally react before putting hands on
352
00:13:05,339 --> 00:13:07,680
xjs and this course here will teach you
353
00:13:07,680 --> 00:13:09,120
all the foundations that you need to
354
00:13:09,120 --> 00:13:10,920
know okay and now the actual course
355
00:13:10,920 --> 00:13:13,079
starts and we will focus on the back end
356
00:13:13,079 --> 00:13:15,240
first so we'll build the server and the
357
00:13:15,240 --> 00:13:17,519
endpoints first then we will build our
358
00:13:17,519 --> 00:13:19,079
react website and connect everything
359
00:13:19,079 --> 00:13:21,540
together I will also put a link to the
360
00:13:21,540 --> 00:13:23,579
project on GitHub into the video
361
00:13:23,579 --> 00:13:25,380
description below so you can check out
362
00:13:25,380 --> 00:13:27,300
the code there and then I wish you a lot
363
00:13:27,300 --> 00:13:28,920
of fun and don't forget to like this
364
00:13:28,920 --> 00:13:30,420
video please and share it with someone
365
00:13:30,420 --> 00:13:32,519
that could need it it really helps the
366
00:13:32,519 --> 00:13:34,139
channel and it makes it more interesting
367
00:13:34,139 --> 00:13:36,540
for me to create more free courses in
368
00:13:36,540 --> 00:13:38,459
the future so it's a win for both of us
369
00:13:38,459 --> 00:13:41,480
alright have fun
370
00:13:44,519 --> 00:13:46,920
all right in order to write code we need
371
00:13:46,920 --> 00:13:49,500
a code editor and a very popular choice
372
00:13:49,500 --> 00:13:52,019
among web developers is Visual Studio
373
00:13:52,019 --> 00:13:54,720
code or short vs code which is a free
374
00:13:54,720 --> 00:13:56,880
code editor by Microsoft you can use
375
00:13:56,880 --> 00:13:58,320
this one you can use a different one
376
00:13:58,320 --> 00:14:00,420
it's up to you if you want to use vs
377
00:14:00,420 --> 00:14:02,480
code just go to a
378
00:14:02,480 --> 00:14:04,800
code.visualstudio.com or type the S code
379
00:14:04,800 --> 00:14:07,320
into Google download it install it the
380
00:14:07,320 --> 00:14:09,660
installation process is not difficult
381
00:14:09,660 --> 00:14:12,899
next we need node.js installed node is
382
00:14:12,899 --> 00:14:15,300
the n in the mirn acronym and node
383
00:14:15,300 --> 00:14:17,940
allows JavaScript code to run in a
384
00:14:17,940 --> 00:14:19,800
server environment so normally a
385
00:14:19,800 --> 00:14:21,959
JavaScript is meant as a language for
386
00:14:21,959 --> 00:14:24,240
web browsers so it only runs like in
387
00:14:24,240 --> 00:14:26,519
like Chrome for example but node.js
388
00:14:26,519 --> 00:14:29,040
brought JavaScript to a server so we can
389
00:14:29,040 --> 00:14:31,680
write our backend in JavaScript tool so
390
00:14:31,680 --> 00:14:33,180
again this is of course completely free
391
00:14:33,180 --> 00:14:36,420
you can go to nodejs.org or just type
392
00:14:36,420 --> 00:14:38,700
node.js into Google and here we want to
393
00:14:38,700 --> 00:14:41,459
download the latest ATS version ATS
394
00:14:41,459 --> 00:14:43,560
stands for long-term support and it
395
00:14:43,560 --> 00:14:45,120
basically means that you can use this
396
00:14:45,120 --> 00:14:46,980
version for many years without having to
397
00:14:46,980 --> 00:14:49,079
update it alright so we click this and
398
00:14:49,079 --> 00:14:50,820
download this and when it's finished
399
00:14:50,820 --> 00:14:53,519
downloading we click to install our file
400
00:14:53,519 --> 00:14:55,740
okay so now we just have to follow this
401
00:14:55,740 --> 00:14:57,060
wizard here
402
00:14:57,060 --> 00:15:01,699
next we read all the license agreement
403
00:15:01,699 --> 00:15:05,220
okay I've read everything next
404
00:15:05,220 --> 00:15:07,860
then we can keep this as it is
405
00:15:07,860 --> 00:15:10,980
next we can keep the defaults here as
406
00:15:10,980 --> 00:15:12,420
well
407
00:15:12,420 --> 00:15:15,180
add to path yeah looks fine to me
408
00:15:15,180 --> 00:15:17,399
particularly important is the node
409
00:15:17,399 --> 00:15:19,680
package manager which we later need to
410
00:15:19,680 --> 00:15:21,959
install packages and stuff like that but
411
00:15:21,959 --> 00:15:24,180
the default options here are shuts the
412
00:15:24,180 --> 00:15:25,139
files
413
00:15:25,139 --> 00:15:27,120
we click next again
414
00:15:27,120 --> 00:15:29,339
this option here should not be necessary
415
00:15:29,339 --> 00:15:31,860
for us so we can skip this click on next
416
00:15:31,860 --> 00:15:33,899
and then install and then we wait until
417
00:15:33,899 --> 00:15:35,699
this is finished
418
00:15:35,699 --> 00:15:37,920
all right looks like this succeeded so
419
00:15:37,920 --> 00:15:39,540
we click on finish
420
00:15:39,540 --> 00:15:42,480
and to check if this actually worked we
421
00:15:42,480 --> 00:15:44,880
want to open the command line
422
00:15:44,880 --> 00:15:47,040
so when Windows just hit the Windows key
423
00:15:47,040 --> 00:15:50,100
and type CMD and on a Mac it works
424
00:15:50,100 --> 00:15:52,139
similarly you probably already know how
425
00:15:52,139 --> 00:15:54,540
otherwise Google is and if you use Linux
426
00:15:54,540 --> 00:15:56,639
then you are a nerd anyway and you
427
00:15:56,639 --> 00:15:58,199
probably use the command line all the
428
00:15:58,199 --> 00:16:00,959
time we just want to check if note was
429
00:16:00,959 --> 00:16:02,760
installed properly so what you do is we
430
00:16:02,760 --> 00:16:05,760
type in node minus minus version
431
00:16:05,760 --> 00:16:08,339
and this should show us the latest
432
00:16:08,339 --> 00:16:10,860
version here we installed if it says
433
00:16:10,860 --> 00:16:13,199
something like node is not a recognized
434
00:16:13,199 --> 00:16:15,959
keyword or command then try to install
435
00:16:15,959 --> 00:16:17,940
it again maybe restart your computer
436
00:16:17,940 --> 00:16:19,860
maybe this helps otherwise Google
437
00:16:19,860 --> 00:16:21,660
installation instructions or leave a
438
00:16:21,660 --> 00:16:23,760
comment below so others can help you but
439
00:16:23,760 --> 00:16:25,079
if you followed along it should have
440
00:16:25,079 --> 00:16:27,660
installed it properly and when we type
441
00:16:27,660 --> 00:16:31,740
in npm dash dash version then it should
442
00:16:31,740 --> 00:16:34,560
show us the version of npm that we
443
00:16:34,560 --> 00:16:36,120
installed which is again the node
444
00:16:36,120 --> 00:16:38,820
package manager okay we can close this
445
00:16:38,820 --> 00:16:41,279
close this here as well
446
00:16:41,279 --> 00:16:43,259
before we start building our server
447
00:16:43,259 --> 00:16:45,300
let's first understand why we need a
448
00:16:45,300 --> 00:16:47,279
back-end server in the first place in
449
00:16:47,279 --> 00:16:49,139
short we use a back-end server for
450
00:16:49,139 --> 00:16:51,180
everything that we can't trust the front
451
00:16:51,180 --> 00:16:54,300
end to handle because whatever code we
452
00:16:54,300 --> 00:16:56,699
are right for the front end in our case
453
00:16:56,699 --> 00:16:59,279
our react code later is loaded into the
454
00:16:59,279 --> 00:17:01,740
web browser and everyone who is a bit
455
00:17:01,740 --> 00:17:04,199
technical or a programmer is able to
456
00:17:04,199 --> 00:17:06,480
actually look into this code even though
457
00:17:06,480 --> 00:17:08,939
it will be minified they can look into
458
00:17:08,939 --> 00:17:11,339
it and they can Tinker with it which
459
00:17:11,339 --> 00:17:13,799
means that we can't put anything secret
460
00:17:13,799 --> 00:17:16,679
or critical in there for example we
461
00:17:16,679 --> 00:17:18,299
can't put the credentials for our
462
00:17:18,299 --> 00:17:21,119
database into this code because users
463
00:17:21,119 --> 00:17:22,740
would be able to see this if they
464
00:17:22,740 --> 00:17:25,079
inspect the code we also can't trust
465
00:17:25,079 --> 00:17:27,240
form validation on the front end because
466
00:17:27,240 --> 00:17:29,220
even though it will be enforced if they
467
00:17:29,220 --> 00:17:31,320
use the website the normal way as it's
468
00:17:31,320 --> 00:17:33,179
intended they can still change it by
469
00:17:33,179 --> 00:17:34,740
tingering with the code and this way
470
00:17:34,740 --> 00:17:37,140
circumvent the restrictions we set up
471
00:17:37,140 --> 00:17:39,360
the back end our server on the other
472
00:17:39,360 --> 00:17:41,820
hand is like a black box that people
473
00:17:41,820 --> 00:17:43,860
can't look into because it runs on a
474
00:17:43,860 --> 00:17:45,780
different machine it doesn't run in the
475
00:17:45,780 --> 00:17:47,760
browser and the only way to get data
476
00:17:47,760 --> 00:17:49,380
from the server is by sending a message
477
00:17:49,380 --> 00:17:51,360
there sending a request and saying Hey
478
00:17:51,360 --> 00:17:53,280
zobo I want this piece of information
479
00:17:53,280 --> 00:17:55,679
and then the server says okay but do you
480
00:17:55,679 --> 00:17:57,480
have the correct credentials for that
481
00:17:57,480 --> 00:18:00,419
are you authenticated then the front end
482
00:18:00,419 --> 00:18:02,160
can send the password or whatever
483
00:18:02,160 --> 00:18:04,440
authentication method we use as an
484
00:18:04,440 --> 00:18:06,600
example and then the server ultimately
485
00:18:06,600 --> 00:18:08,940
decides if it fulfills the request or
486
00:18:08,940 --> 00:18:11,580
not but users can't go into our server
487
00:18:11,580 --> 00:18:13,620
and change code there or anything like
488
00:18:13,620 --> 00:18:15,960
that for Outsiders it's just a black box
489
00:18:15,960 --> 00:18:18,840
so to summarize our front end can't
490
00:18:18,840 --> 00:18:21,179
contain anything that's critical or
491
00:18:21,179 --> 00:18:23,400
anything that's secret as all goes into
492
00:18:23,400 --> 00:18:26,520
our backend the front end is only the
493
00:18:26,520 --> 00:18:28,320
user interface that communicates with
494
00:18:28,320 --> 00:18:30,480
the backend but it's not safe it's not
495
00:18:30,480 --> 00:18:31,799
secure
496
00:18:31,799 --> 00:18:34,080
all right now let's set up our node
497
00:18:34,080 --> 00:18:36,960
server and the first step is to create a
498
00:18:36,960 --> 00:18:38,640
folder somewhere on your computer where
499
00:18:38,640 --> 00:18:40,380
you want to save this project that's up
500
00:18:40,380 --> 00:18:42,299
to you and in here we create another
501
00:18:42,299 --> 00:18:44,220
folder
502
00:18:44,220 --> 00:18:46,860
which I'm gonna cut back end
503
00:18:46,860 --> 00:18:48,780
this will contain the code of our node
504
00:18:48,780 --> 00:18:52,080
backend project we go into this folder
505
00:18:52,080 --> 00:18:53,940
and in here we want to open the command
506
00:18:53,940 --> 00:18:54,720
line
507
00:18:54,720 --> 00:18:56,820
on Windows you do this by holding shift
508
00:18:56,820 --> 00:18:59,340
down and then right clicking and then
509
00:18:59,340 --> 00:19:01,919
open Powershell window here depending on
510
00:19:01,919 --> 00:19:03,900
your configuration this might open the
511
00:19:03,900 --> 00:19:06,360
older command line but it doesn't matter
512
00:19:06,360 --> 00:19:09,480
both are fine for what we want to do
513
00:19:09,480 --> 00:19:12,900
if you are on Mac or Linux you will also
514
00:19:12,900 --> 00:19:14,520
have a command line available just look
515
00:19:14,520 --> 00:19:16,740
up how you open it if you don't know how
516
00:19:16,740 --> 00:19:18,539
to do it there are instructions in
517
00:19:18,539 --> 00:19:19,320
Google
518
00:19:19,320 --> 00:19:21,840
okay let's make this here A bit bigger
519
00:19:21,840 --> 00:19:23,820
so we can see everything
520
00:19:23,820 --> 00:19:27,059
and in here we type in npm
521
00:19:27,059 --> 00:19:28,500
in it
522
00:19:28,500 --> 00:19:31,080
Dash wire and whenever we write
523
00:19:31,080 --> 00:19:32,700
something in the command line you have
524
00:19:32,700 --> 00:19:34,620
to write it the exact same way as I do
525
00:19:34,620 --> 00:19:36,660
or otherwise it won't work the command
526
00:19:36,660 --> 00:19:39,000
line is not very forgiving okay we type
527
00:19:39,000 --> 00:19:40,799
this in and press enter
528
00:19:40,799 --> 00:19:43,980
and it looks like it did something it
529
00:19:43,980 --> 00:19:46,200
created this package file here we don't
530
00:19:46,200 --> 00:19:47,820
see the file extension here in Windows
531
00:19:47,820 --> 00:19:50,100
right now but this is a Json file so the
532
00:19:50,100 --> 00:19:52,919
full name is package.json next we want
533
00:19:52,919 --> 00:19:54,960
to open this project with this file we
534
00:19:54,960 --> 00:19:57,480
just created and we could be old boring
535
00:19:57,480 --> 00:19:59,580
Boomers and open it through the escort
536
00:19:59,580 --> 00:20:02,520
but cool modern people open projects
537
00:20:02,520 --> 00:20:04,919
over the command line so we stay in here
538
00:20:04,919 --> 00:20:06,360
because as you can see we are still
539
00:20:06,360 --> 00:20:07,740
inside this backend folder that's
540
00:20:07,740 --> 00:20:10,140
important and then we could type in code
541
00:20:10,140 --> 00:20:15,539
and then space and period so code Dot
542
00:20:15,539 --> 00:20:18,840
enter and this opens this project in vs
543
00:20:18,840 --> 00:20:19,679
code
544
00:20:19,679 --> 00:20:22,380
now if this window here pops up we want
545
00:20:22,380 --> 00:20:24,960
to trust this project but this might not
546
00:20:24,960 --> 00:20:26,400
pop up for you and here's our
547
00:20:26,400 --> 00:20:28,440
package.json file which we created a
548
00:20:28,440 --> 00:20:30,960
moment ago
549
00:20:30,960 --> 00:20:33,600
this package.json file is basically the
550
00:20:33,600 --> 00:20:35,760
configuration for our project where we
551
00:20:35,760 --> 00:20:37,260
will also later have all the
552
00:20:37,260 --> 00:20:39,480
dependencies listed that we need all the
553
00:20:39,480 --> 00:20:42,059
packages that we use now since we typed
554
00:20:42,059 --> 00:20:44,100
in dash wire it used the default
555
00:20:44,100 --> 00:20:47,160
settings for all this meta data here and
556
00:20:47,160 --> 00:20:49,919
actually this stuff or most of it is
557
00:20:49,919 --> 00:20:52,200
only important if you want to release a
558
00:20:52,200 --> 00:20:53,940
library if you want to release a package
559
00:20:53,940 --> 00:20:56,460
for our server project that we use
560
00:20:56,460 --> 00:20:58,380
ourselves this is actually not important
561
00:20:58,380 --> 00:21:00,539
we don't need a description and the name
562
00:21:00,539 --> 00:21:02,340
and version doesn't matter keyword
563
00:21:02,340 --> 00:21:04,080
authors you can fill this out if you
564
00:21:04,080 --> 00:21:06,360
want but you don't need to we also don't
565
00:21:06,360 --> 00:21:08,580
need to specify a license because no one
566
00:21:08,580 --> 00:21:10,860
else will have access to our code and
567
00:21:10,860 --> 00:21:13,200
now it's actually time to install our
568
00:21:13,200 --> 00:21:16,080
first dependency oh that's exciting our
569
00:21:16,080 --> 00:21:17,640
first dependency will be typescript
570
00:21:17,640 --> 00:21:19,860
which we have to install in order to use
571
00:21:19,860 --> 00:21:22,500
this language instead of JavaScript so
572
00:21:22,500 --> 00:21:25,080
we want to open a command line again and
573
00:21:25,080 --> 00:21:27,059
we can do this directly inside the S
574
00:21:27,059 --> 00:21:29,400
code as well so we click up here on
575
00:21:29,400 --> 00:21:32,220
Terminal and on new terminal and here
576
00:21:32,220 --> 00:21:34,260
next to it you can see the shortcut for
577
00:21:34,260 --> 00:21:37,140
me it's Ctrl shift ER if you're not a
578
00:21:37,140 --> 00:21:38,580
German you probably don't have an ear
579
00:21:38,580 --> 00:21:42,480
can you use whatever shows up there okay
580
00:21:42,480 --> 00:21:44,820
now we have a terminal here and now we
581
00:21:44,820 --> 00:21:48,240
type in npm install
582
00:21:48,240 --> 00:21:53,100
dash dash save Dash Dev like this
583
00:21:53,100 --> 00:21:54,860
just type it the exact same way
584
00:21:54,860 --> 00:21:58,559
typescript or lowercase
585
00:21:58,559 --> 00:22:00,480
enter
586
00:22:00,480 --> 00:22:03,059
and this installs typescript in this
587
00:22:03,059 --> 00:22:04,140
project
588
00:22:04,140 --> 00:22:06,780
and as we can see this new entry here
589
00:22:06,780 --> 00:22:09,720
popped up in our package.json under this
590
00:22:09,720 --> 00:22:12,480
Dev dependencies queue dependencies are
591
00:22:12,480 --> 00:22:14,280
just packages that we use for our
592
00:22:14,280 --> 00:22:16,620
project for example later bootstrap is a
593
00:22:16,620 --> 00:22:18,720
dependency as well for our layout all
594
00:22:18,720 --> 00:22:20,580
these different libraries that others
595
00:22:20,580 --> 00:22:22,440
have made for us and that we can use and
596
00:22:22,440 --> 00:22:23,820
there are two types of dependencies
597
00:22:23,820 --> 00:22:26,100
normal dependencies and Dev dependencies
598
00:22:26,100 --> 00:22:28,320
def dependencies are the ones that we
599
00:22:28,320 --> 00:22:30,299
don't actually need in production we
600
00:22:30,299 --> 00:22:31,980
only need them while we are writing the
601
00:22:31,980 --> 00:22:34,080
code so we put this into the separate
602
00:22:34,080 --> 00:22:36,480
key here so that we later save space
603
00:22:36,480 --> 00:22:38,159
when we deploy this project to
604
00:22:38,159 --> 00:22:39,840
production because those ones here will
605
00:22:39,840 --> 00:22:41,220
be skipped those are only for
606
00:22:41,220 --> 00:22:43,320
development okay down here it also says
607
00:22:43,320 --> 00:22:46,140
new Metro version of npm available it's
608
00:22:46,140 --> 00:22:48,780
always good to update this so let's copy
609
00:22:48,780 --> 00:22:50,640
this command here
610
00:22:50,640 --> 00:22:52,620
and type it in
611
00:22:52,620 --> 00:22:55,559
you can paste it with a right click and
612
00:22:55,559 --> 00:22:57,600
press enter to install the latest
613
00:22:57,600 --> 00:22:59,580
version of npm
614
00:22:59,580 --> 00:23:01,559
we also see that it created two new
615
00:23:01,559 --> 00:23:04,440
entries appear in our file directory one
616
00:23:04,440 --> 00:23:06,480
is the package log Json which is related
617
00:23:06,480 --> 00:23:08,760
to the package.json we don't have to
618
00:23:08,760 --> 00:23:10,320
worry about this this is generated
619
00:23:10,320 --> 00:23:13,080
automatically and node modules contains
620
00:23:13,080 --> 00:23:15,000
the packages that we install the
621
00:23:15,000 --> 00:23:16,980
dependencies which is typescript in this
622
00:23:16,980 --> 00:23:18,539
case but there will be more in there
623
00:23:18,539 --> 00:23:21,659
later alright and a typescript project
624
00:23:21,659 --> 00:23:24,059
needs a typescript configuration file
625
00:23:24,059 --> 00:23:26,100
which contains some information on how a
626
00:23:26,100 --> 00:23:27,900
typescript should behave otherwise it
627
00:23:27,900 --> 00:23:30,240
won't work so we go into the terminal
628
00:23:30,240 --> 00:23:33,840
again and we type in npx this time it's
629
00:23:33,840 --> 00:23:36,600
not npm it's npx and the difference is
630
00:23:36,600 --> 00:23:39,900
that npx doesn't install packages it
631
00:23:39,900 --> 00:23:42,539
executes packages basically that's all
632
00:23:42,539 --> 00:23:45,240
we need to know so we type in npx TSC
633
00:23:45,240 --> 00:23:47,760
which is short for typescript but we
634
00:23:47,760 --> 00:23:50,700
have to type in TSC not typescript it
635
00:23:50,700 --> 00:23:53,220
might be short for typescript compiler
636
00:23:53,220 --> 00:23:56,580
I'm not sure actually just type in npxts
637
00:23:56,580 --> 00:23:59,580
here dash dash init
638
00:23:59,580 --> 00:24:01,260
enter
639
00:24:01,260 --> 00:24:03,840
and this creates a new file for us which
640
00:24:03,840 --> 00:24:07,200
is this tsconfig.json here let's open it
641
00:24:07,200 --> 00:24:09,419
and take a look in it a lot of scary
642
00:24:09,419 --> 00:24:11,820
stuff most of the options in here are
643
00:24:11,820 --> 00:24:13,679
commented out so they are inactive
644
00:24:13,679 --> 00:24:15,900
because they are optional and the ones
645
00:24:15,900 --> 00:24:17,640
here if you want to know what they are
646
00:24:17,640 --> 00:24:19,679
for you can read up on them we don't
647
00:24:19,679 --> 00:24:21,059
have to change anything in here right
648
00:24:21,059 --> 00:24:24,179
now it's all set up properly for us but
649
00:24:24,179 --> 00:24:26,520
we have to make some changes here later
650
00:24:26,520 --> 00:24:28,440
then we will get back into this fire
651
00:24:28,440 --> 00:24:31,440
okay now it's time to install Express
652
00:24:31,440 --> 00:24:34,080
and express is the e in the moon stack
653
00:24:34,080 --> 00:24:36,480
it's the framework that allows us to
654
00:24:36,480 --> 00:24:38,880
build the server basically so node.js is
655
00:24:38,880 --> 00:24:41,760
just the foundation where the code runs
656
00:24:41,760 --> 00:24:44,520
on and expresses the actual library that
657
00:24:44,520 --> 00:24:46,320
allows us to write the server code and
658
00:24:46,320 --> 00:24:48,480
the endpoints and everything so again in
659
00:24:48,480 --> 00:24:51,720
the terminal we type in npm so not npx
660
00:24:51,720 --> 00:24:54,179
this time npm again
661
00:24:54,179 --> 00:24:56,700
install and by the way there's a short
662
00:24:56,700 --> 00:24:59,580
form for install which is just I let's
663
00:24:59,580 --> 00:25:02,400
use this in the future without safe Dev
664
00:25:02,400 --> 00:25:04,320
this time because this is not a Dev
665
00:25:04,320 --> 00:25:05,640
dependency
666
00:25:05,640 --> 00:25:07,860
that goes in here like typescript it's a
667
00:25:07,860 --> 00:25:09,659
normal dependency which we need in
668
00:25:09,659 --> 00:25:10,740
production
669
00:25:10,740 --> 00:25:12,720
and then we type in Express
670
00:25:12,720 --> 00:25:15,200
enter
671
00:25:16,020 --> 00:25:18,299
and this installed Express here under
672
00:25:18,299 --> 00:25:20,520
this dependencies keyer for normal
673
00:25:20,520 --> 00:25:22,980
JavaScript project this is enough in a
674
00:25:22,980 --> 00:25:24,960
typescript project we often have to
675
00:25:24,960 --> 00:25:27,659
install also a package that contains the
676
00:25:27,659 --> 00:25:29,940
type so that our code works properly and
677
00:25:29,940 --> 00:25:31,500
typescript knows what types the
678
00:25:31,500 --> 00:25:33,120
different variables and functions have
679
00:25:33,120 --> 00:25:36,000
so once again we type in npm I for
680
00:25:36,000 --> 00:25:37,320
install
681
00:25:37,320 --> 00:25:39,960
types are def dependencies because we
682
00:25:39,960 --> 00:25:42,059
don't need them in production so again
683
00:25:42,059 --> 00:25:45,779
dash dash save Dash Dev
684
00:25:45,779 --> 00:25:50,340
add types slash Express type it the
685
00:25:50,340 --> 00:25:52,200
exact same way I did and press enter
686
00:25:52,200 --> 00:25:53,400
again
687
00:25:53,400 --> 00:25:55,080
which adds another entry in the dev
688
00:25:55,080 --> 00:25:57,720
dependencies the express types
689
00:25:57,720 --> 00:26:00,779
now this always installs the latest
690
00:26:00,779 --> 00:26:03,000
version of these dependencies if you
691
00:26:03,000 --> 00:26:04,860
watch this tutorial sometime in the
692
00:26:04,860 --> 00:26:06,900
future then these version numbers will
693
00:26:06,900 --> 00:26:08,640
be higher than the ones you can see on
694
00:26:08,640 --> 00:26:10,440
the screen and sometimes when these
695
00:26:10,440 --> 00:26:12,600
packages get updated things break
696
00:26:12,600 --> 00:26:14,820
especially when the number here at the
697
00:26:14,820 --> 00:26:17,580
very front changes if this happens some
698
00:26:17,580 --> 00:26:19,620
things I show in the tutorial might stop
699
00:26:19,620 --> 00:26:21,299
working because they change this
700
00:26:21,299 --> 00:26:24,000
meanwhile then the best way for you to
701
00:26:24,000 --> 00:26:26,400
go about this is to Google the changes
702
00:26:26,400 --> 00:26:28,919
you can read the release notes for the
703
00:26:28,919 --> 00:26:31,260
dependency on GitHub for example and try
704
00:26:31,260 --> 00:26:33,360
to implement the changes yourself if you
705
00:26:33,360 --> 00:26:35,340
can't get this to work and you can't
706
00:26:35,340 --> 00:26:37,320
figure out how to use the latest version
707
00:26:37,320 --> 00:26:39,480
then you can always install the version
708
00:26:39,480 --> 00:26:41,700
I used here in this tutorial so for
709
00:26:41,700 --> 00:26:46,020
example instead of just npmi Express
710
00:26:46,020 --> 00:26:49,679
you can also add at the end and add and
711
00:26:49,679 --> 00:26:51,539
then the version number which in this
712
00:26:51,539 --> 00:26:52,760
case would be
713
00:26:52,760 --> 00:26:55,679
4.18.2 if you do it like this it will
714
00:26:55,679 --> 00:26:57,539
install this exact version and
715
00:26:57,539 --> 00:26:59,340
everything will work like it works in my
716
00:26:59,340 --> 00:27:01,080
tutorial but again it's better for you
717
00:27:01,080 --> 00:27:03,120
if you actually try to figure this out
718
00:27:03,120 --> 00:27:04,679
by yourself
719
00:27:04,679 --> 00:27:06,840
and now let's actually create our first
720
00:27:06,840 --> 00:27:09,179
little server so here on the left side
721
00:27:09,179 --> 00:27:11,279
in this file explorer we right click and
722
00:27:11,279 --> 00:27:14,000
create a new file
723
00:27:15,600 --> 00:27:18,240
TS stands for typescript if it was a
724
00:27:18,240 --> 00:27:20,460
normal JavaScript file then it would be
725
00:27:20,460 --> 00:27:23,640
dot JS but we are not peasants we use
726
00:27:23,640 --> 00:27:26,220
typescript here alright and for now just
727
00:27:26,220 --> 00:27:29,039
type exactly what I type here first we
728
00:27:29,039 --> 00:27:32,100
have to import Express into this file
729
00:27:32,100 --> 00:27:34,620
expresses the package we just installed
730
00:27:34,620 --> 00:27:38,220
so we type in Port Express from
731
00:27:38,220 --> 00:27:41,580
and then in quotation marks and in
732
00:27:41,580 --> 00:27:43,500
JavaScript and typescript code we can
733
00:27:43,500 --> 00:27:46,200
use single quotes and double quotes in
734
00:27:46,200 --> 00:27:48,480
most situations it doesn't matter I will
735
00:27:48,480 --> 00:27:50,460
use double quotes here just to keep it
736
00:27:50,460 --> 00:27:51,659
consistent
737
00:27:51,659 --> 00:27:54,000
and here we type in Express and
738
00:27:54,000 --> 00:27:55,919
semicolons at the end of a line are
739
00:27:55,919 --> 00:27:58,140
optional in JavaScript as well but it's
740
00:27:58,140 --> 00:28:00,120
good to practice to add them so that's
741
00:28:00,120 --> 00:28:02,039
what we're going to do here in the next
742
00:28:02,039 --> 00:28:04,919
line we write const app with const we
743
00:28:04,919 --> 00:28:06,720
create a variable that we can't change
744
00:28:06,720 --> 00:28:09,179
the value of later but again those are
745
00:28:09,179 --> 00:28:11,039
JavaScript basics
746
00:28:11,039 --> 00:28:14,039
and then we can't express like this and
747
00:28:14,039 --> 00:28:16,559
the app is basically our server this is
748
00:28:16,559 --> 00:28:18,120
where we later at different endpoints
749
00:28:18,120 --> 00:28:20,039
and everything
750
00:28:20,039 --> 00:28:23,279
we also have to define a port
751
00:28:23,279 --> 00:28:26,159
which is that to 5000 Port is basically
752
00:28:26,159 --> 00:28:28,620
a connection point on a server and you
753
00:28:28,620 --> 00:28:30,120
have to set this to a number some
754
00:28:30,120 --> 00:28:32,279
numbers are occupied already so there
755
00:28:32,279 --> 00:28:33,840
are certain numbers that people use by
756
00:28:33,840 --> 00:28:37,620
convention like 5000 or 8 000 but a lot
757
00:28:37,620 --> 00:28:39,659
of different ones will work you could
758
00:28:39,659 --> 00:28:41,299
probably type in
759
00:28:41,299 --> 00:28:44,340
4993 and it would work as well but you
760
00:28:44,340 --> 00:28:47,220
will go fine with five thousand and also
761
00:28:47,220 --> 00:28:49,799
some web hosting Services require a
762
00:28:49,799 --> 00:28:52,020
specific port number so this always has
763
00:28:52,020 --> 00:28:54,539
to go into a consideration react for
764
00:28:54,539 --> 00:28:57,779
example users Port 3000 by default so we
765
00:28:57,779 --> 00:28:59,520
want to use different ports for both of
766
00:28:59,520 --> 00:29:01,320
them otherwise we can't run them
767
00:29:01,320 --> 00:29:04,080
simultaneously on our local machine okay
768
00:29:04,080 --> 00:29:06,480
and then below we take our app wherever
769
00:29:06,480 --> 00:29:09,659
there again and car.get and between the
770
00:29:09,659 --> 00:29:13,020
parentheses repairs a slash as a string
771
00:29:13,020 --> 00:29:14,940
so in quotation marks
772
00:29:14,940 --> 00:29:16,740
comma
773
00:29:16,740 --> 00:29:18,720
and then we pass an error function here
774
00:29:18,720 --> 00:29:20,340
which looks like this another pair of
775
00:29:20,340 --> 00:29:23,880
parentheses Rec comma address those are
776
00:29:23,880 --> 00:29:25,380
the two arguments that this function
777
00:29:25,380 --> 00:29:27,659
gets passed
778
00:29:27,659 --> 00:29:29,760
then we make a right arrow like this
779
00:29:29,760 --> 00:29:32,220
with an equal sign and a greater than
780
00:29:32,220 --> 00:29:35,100
symbol curly braces
781
00:29:35,100 --> 00:29:37,320
and in here we can write the code of
782
00:29:37,320 --> 00:29:38,760
this function so this is an error
783
00:29:38,760 --> 00:29:40,679
function here it's like a function
784
00:29:40,679 --> 00:29:42,419
without a name there's a similar feature
785
00:29:42,419 --> 00:29:44,820
in kotlin and other languages as well at
786
00:29:44,820 --> 00:29:47,340
the end of this get Curl here we put a
787
00:29:47,340 --> 00:29:49,980
semicolon before we forget it and in
788
00:29:49,980 --> 00:29:52,380
here we take this res
789
00:29:52,380 --> 00:29:55,440
cow dot sent
790
00:29:55,440 --> 00:29:58,500
and pass hello world as a string
791
00:29:58,500 --> 00:30:00,179
semicolon
792
00:30:00,179 --> 00:30:02,520
this is our first endpoint which we will
793
00:30:02,520 --> 00:30:04,080
call in a moment and we will get the
794
00:30:04,080 --> 00:30:06,419
string back but before this works we
795
00:30:06,419 --> 00:30:08,940
have to start the server which we do the
796
00:30:08,940 --> 00:30:10,140
following way
797
00:30:10,140 --> 00:30:13,320
below we call app.listen
798
00:30:13,320 --> 00:30:17,220
pass the port we defined up there 5000.
799
00:30:17,220 --> 00:30:19,140
and another error function but this time
800
00:30:19,140 --> 00:30:20,700
it doesn't take any arguments so it
801
00:30:20,700 --> 00:30:21,960
looks like this
802
00:30:21,960 --> 00:30:24,480
and this will start the server and this
803
00:30:24,480 --> 00:30:26,520
is basically just a callback and in this
804
00:30:26,520 --> 00:30:28,220
callback we want to show a lock message
805
00:30:28,220 --> 00:30:30,659
just so that we can see okay we actually
806
00:30:30,659 --> 00:30:32,460
started the server and there was no
807
00:30:32,460 --> 00:30:34,620
crash or anything
808
00:30:34,620 --> 00:30:36,659
so let's just write the log message in
809
00:30:36,659 --> 00:30:40,440
here which in JavaScript we do like this
810
00:30:40,440 --> 00:30:45,120
server running on Port colon
811
00:30:45,120 --> 00:30:47,279
and then we append the port number at
812
00:30:47,279 --> 00:30:50,220
the end again not the pulse port
813
00:30:50,220 --> 00:30:53,520
okay nice and nvs code by default you
814
00:30:53,520 --> 00:30:55,260
actually have to save your files it
815
00:30:55,260 --> 00:30:57,360
doesn't happen automatically so I press
816
00:30:57,360 --> 00:30:58,559
Ctrl s
817
00:30:58,559 --> 00:31:00,480
and now we want to start this whole
818
00:31:00,480 --> 00:31:01,620
Observer
819
00:31:01,620 --> 00:31:03,840
and normally a note if you want to start
820
00:31:03,840 --> 00:31:06,299
a file you type in node in the command
821
00:31:06,299 --> 00:31:08,399
line and then the name of the file so
822
00:31:08,399 --> 00:31:10,260
this would be server.ts
823
00:31:10,260 --> 00:31:11,820
the problem is this only works with
824
00:31:11,820 --> 00:31:13,860
JavaScript files because node doesn't
825
00:31:13,860 --> 00:31:15,899
know how to execute typescript by
826
00:31:15,899 --> 00:31:17,820
default it has to be compiled to
827
00:31:17,820 --> 00:31:20,220
JavaScript first and we do this with the
828
00:31:20,220 --> 00:31:25,200
following command npx again TSU enter
829
00:31:25,200 --> 00:31:27,960
and this turns this typescript code that
830
00:31:27,960 --> 00:31:29,760
we have in our project into JavaScript
831
00:31:29,760 --> 00:31:32,100
code as you can see here on the left it
832
00:31:32,100 --> 00:31:35,039
now created the server.js file and this
833
00:31:35,039 --> 00:31:37,440
is the JavaScript version of our server
834
00:31:37,440 --> 00:31:40,020
TS file this is automatically generated
835
00:31:40,020 --> 00:31:42,360
we don't have to understand this this is
836
00:31:42,360 --> 00:31:44,279
not how a human would write JavaScript
837
00:31:44,279 --> 00:31:46,200
it's how the compiler reads it with
838
00:31:46,200 --> 00:31:48,179
optimizations and everything we don't
839
00:31:48,179 --> 00:31:49,620
actually have to understand this file
840
00:31:49,620 --> 00:31:51,600
it's just interesting to take a look at
841
00:31:51,600 --> 00:31:53,700
it because this is the file we now can
842
00:31:53,700 --> 00:31:54,840
run
843
00:31:54,840 --> 00:31:57,320
when we type in node
844
00:31:57,320 --> 00:31:59,460
server.js this time
845
00:31:59,460 --> 00:32:01,679
and as we can see it now says server
846
00:32:01,679 --> 00:32:03,779
running on Port which is our own text
847
00:32:03,779 --> 00:32:05,820
that we typed into the Callback
848
00:32:05,820 --> 00:32:07,980
and here on Windows I also have to allow
849
00:32:07,980 --> 00:32:10,020
access that's fine
850
00:32:10,020 --> 00:32:12,480
okay and now our server is running on
851
00:32:12,480 --> 00:32:15,720
Port 5000 and now you can just open the
852
00:32:15,720 --> 00:32:18,779
web browser and type in localhost Colon
853
00:32:18,779 --> 00:32:22,200
5000 into the URL bar
854
00:32:22,200 --> 00:32:24,539
and this is now our server here it says
855
00:32:24,539 --> 00:32:26,399
hello world congratulations you are now
856
00:32:26,399 --> 00:32:28,860
a note back-end developer the tutorial
857
00:32:28,860 --> 00:32:30,539
is over happy birthday
858
00:32:30,539 --> 00:32:32,520
no I'm just kidding of course this is
859
00:32:32,520 --> 00:32:34,559
just the beginning a real web server is
860
00:32:34,559 --> 00:32:36,539
much more complex than what you have set
861
00:32:36,539 --> 00:32:38,940
up here and by the way what we have said
862
00:32:38,940 --> 00:32:42,720
up here let's close this is a an
863
00:32:42,720 --> 00:32:45,779
endpoint for an HTTP get request if we
864
00:32:45,779 --> 00:32:48,000
open a website in a web browser it will
865
00:32:48,000 --> 00:32:51,000
do a get request if you have built apps
866
00:32:51,000 --> 00:32:52,740
with networking features before then you
867
00:32:52,740 --> 00:32:54,299
might know that there's also for example
868
00:32:54,299 --> 00:32:57,059
post request to send data to a server
869
00:32:57,059 --> 00:32:59,220
patch delete and so on we will use this
870
00:32:59,220 --> 00:33:01,980
other HTTP methods later to modify our
871
00:33:01,980 --> 00:33:04,200
nodes but yeah this is a get request and
872
00:33:04,200 --> 00:33:05,640
this is how we set it up by a
873
00:33:05,640 --> 00:33:08,940
culling.get on our Express app it's also
874
00:33:08,940 --> 00:33:10,799
a good idea to organize our code better
875
00:33:10,799 --> 00:33:12,899
because otherwise later when we compile
876
00:33:12,899 --> 00:33:14,880
our code we have this huge mix between
877
00:33:14,880 --> 00:33:17,640
JavaScript and typescript files
878
00:33:17,640 --> 00:33:19,799
for now let's delete this we don't need
879
00:33:19,799 --> 00:33:21,659
this right now we only need this when we
880
00:33:21,659 --> 00:33:23,220
actually run our server later in
881
00:33:23,220 --> 00:33:24,779
production
882
00:33:24,779 --> 00:33:27,360
so we delete this and for organization
883
00:33:27,360 --> 00:33:29,159
we create a folder in here which we call
884
00:33:29,159 --> 00:33:32,580
sr0 which is short for Source that's the
885
00:33:32,580 --> 00:33:34,500
source code of our project that's just a
886
00:33:34,500 --> 00:33:35,940
naming convention
887
00:33:35,940 --> 00:33:38,100
and we put the server TS file in there
888
00:33:38,100 --> 00:33:40,679
everything that's configuration for our
889
00:33:40,679 --> 00:33:43,080
project goes outside of source and into
890
00:33:43,080 --> 00:33:45,240
a source we put actual typescript files
891
00:33:45,240 --> 00:33:46,980
that contain the code for the different
892
00:33:46,980 --> 00:33:48,659
endpoints and so on
893
00:33:48,659 --> 00:33:51,120
and next we want to tell the typescript
894
00:33:51,120 --> 00:33:53,519
compiler that it should put these
895
00:33:53,519 --> 00:33:56,220
generated JavaScript files also into a
896
00:33:56,220 --> 00:33:58,500
its own folder so our project isn't
897
00:33:58,500 --> 00:34:00,179
clevered with it
898
00:34:00,179 --> 00:34:04,019
so we open the TS config file again
899
00:34:04,019 --> 00:34:07,200
and we search for the option
900
00:34:07,200 --> 00:34:10,619
that's called out dir so output
901
00:34:10,619 --> 00:34:13,020
directory we remove the comment here at
902
00:34:13,020 --> 00:34:14,399
the beginning
903
00:34:14,399 --> 00:34:16,500
so we activate this basically
904
00:34:16,500 --> 00:34:18,839
and we are append dist here at the end
905
00:34:18,839 --> 00:34:21,119
which puts the generated code into a
906
00:34:21,119 --> 00:34:23,040
folder called dist which is short for
907
00:34:23,040 --> 00:34:25,560
distribution and just because I have OCD
908
00:34:25,560 --> 00:34:27,480
and let's align this here again properly
909
00:34:27,480 --> 00:34:30,599
and save this file let's try running our
910
00:34:30,599 --> 00:34:34,800
npx TSC command again but first we have
911
00:34:34,800 --> 00:34:36,960
to stop our server while it's running we
912
00:34:36,960 --> 00:34:39,839
can't add any commands here so we press
913
00:34:39,839 --> 00:34:43,080
Ctrl 0 which stops our server no it's
914
00:34:43,080 --> 00:34:45,719
not running anymore and again we try our
915
00:34:45,719 --> 00:34:47,820
npx TSC
916
00:34:47,820 --> 00:34:49,739
to compile our typescript code you don't
917
00:34:49,739 --> 00:34:51,060
have to do this right now you can just
918
00:34:51,060 --> 00:34:52,500
watch me do it
919
00:34:52,500 --> 00:34:54,719
and now our code is properly organized
920
00:34:54,719 --> 00:34:57,240
into this this folder here which mimics
921
00:34:57,240 --> 00:34:59,580
the shape of our source folder and now
922
00:34:59,580 --> 00:35:02,160
if we want to run this file we have to
923
00:35:02,160 --> 00:35:05,520
type in node like before but this time
924
00:35:05,520 --> 00:35:09,599
just slash server.js because it's inside
925
00:35:09,599 --> 00:35:12,240
this this folder right so let's try it
926
00:35:12,240 --> 00:35:14,400
again the server is running localhost
927
00:35:14,400 --> 00:35:18,240
5000 it's still there nice oh he was the
928
00:35:18,240 --> 00:35:19,619
old window
929
00:35:19,619 --> 00:35:22,079
but of course it's annoying to repeat
930
00:35:22,079 --> 00:35:24,000
this whole process every time we make a
931
00:35:24,000 --> 00:35:25,920
change to our code every time compile
932
00:35:25,920 --> 00:35:28,200
the project type in noise This Server
933
00:35:28,200 --> 00:35:30,660
and so on so we can install some
934
00:35:30,660 --> 00:35:32,760
dependencies that make this process much
935
00:35:32,760 --> 00:35:35,400
easier for us so let's stop the server
936
00:35:35,400 --> 00:35:36,180
again
937
00:35:36,180 --> 00:35:38,040
and delete this list folder here because
938
00:35:38,040 --> 00:35:40,260
we will only need this compiled code in
939
00:35:40,260 --> 00:35:41,579
production
940
00:35:41,579 --> 00:35:43,619
in development we will use something
941
00:35:43,619 --> 00:35:44,940
different
942
00:35:44,940 --> 00:35:47,520
the package we are used to automatically
943
00:35:47,520 --> 00:35:49,859
restart our server whenever we make
944
00:35:49,859 --> 00:35:52,140
changes to the code is called nodemon
945
00:35:52,140 --> 00:35:55,020
and as you can see on the npm Page by
946
00:35:55,020 --> 00:35:56,700
the weekly downloads it's a very popular
947
00:35:56,700 --> 00:35:58,859
package if you want to learn more
948
00:35:58,859 --> 00:36:00,420
details about this package you can check
949
00:36:00,420 --> 00:36:03,119
out its page here but it's pretty
950
00:36:03,119 --> 00:36:05,339
straightforward to use as you can see
951
00:36:05,339 --> 00:36:07,320
here we install it as a Dev dependency
952
00:36:07,320 --> 00:36:09,540
because we are only needed in
953
00:36:09,540 --> 00:36:10,800
development
954
00:36:10,800 --> 00:36:13,260
so let's copy this
955
00:36:13,260 --> 00:36:16,740
and paste it into command line
956
00:36:16,740 --> 00:36:19,500
and again it adds another entry in our
957
00:36:19,500 --> 00:36:21,359
Dev dependencies here
958
00:36:21,359 --> 00:36:24,480
now this only works with JavaScript by
959
00:36:24,480 --> 00:36:26,280
default but luckily there's another
960
00:36:26,280 --> 00:36:28,140
dependence here
961
00:36:28,140 --> 00:36:30,900
that automatically works together with
962
00:36:30,900 --> 00:36:34,440
nodemon to a transpile our typescript
963
00:36:34,440 --> 00:36:37,140
code into JavaScript on the Fly
964
00:36:37,140 --> 00:36:40,079
which is called TS node so again we want
965
00:36:40,079 --> 00:36:42,359
to install this as well and here's the
966
00:36:42,359 --> 00:36:44,760
command for that and by the way it is
967
00:36:44,760 --> 00:36:48,119
minus D with an uppercase D is the short
968
00:36:48,119 --> 00:36:52,079
form for dash dash safe Dev so we copy
969
00:36:52,079 --> 00:36:53,460
this
970
00:36:53,460 --> 00:36:57,540
paste it in here and style this as well
971
00:36:57,540 --> 00:37:00,540
and now instead of compiling our server
972
00:37:00,540 --> 00:37:03,359
TS file and then starting this server.js
973
00:37:03,359 --> 00:37:05,700
file that's generated we can just start
974
00:37:05,700 --> 00:37:08,760
our server TS file directly and we do
975
00:37:08,760 --> 00:37:12,960
this by typing in npx nodemon to execute
976
00:37:12,960 --> 00:37:16,140
nodemon and then our server TS file but
977
00:37:16,140 --> 00:37:19,020
it's inside our source folder so we type
978
00:37:19,020 --> 00:37:23,400
srz slash servo.ts
979
00:37:23,400 --> 00:37:26,880
and this now starts our server fire it
980
00:37:26,880 --> 00:37:28,980
works because we have TS node installed
981
00:37:28,980 --> 00:37:31,680
which compiles the typescript code on
982
00:37:31,680 --> 00:37:33,960
the flyer and not only that now if we
983
00:37:33,960 --> 00:37:36,300
make changes to any file the server
984
00:37:36,300 --> 00:37:38,760
automatically restarts so let's add the
985
00:37:38,760 --> 00:37:40,980
comma here between hello world because I
986
00:37:40,980 --> 00:37:42,420
think this is the correct way to write
987
00:37:42,420 --> 00:37:45,060
it save it with Ctrl s and as you can
988
00:37:45,060 --> 00:37:46,680
see down here it automatically restarts
989
00:37:46,680 --> 00:37:48,300
our server
990
00:37:48,300 --> 00:37:50,760
so now when I open localhost again we
991
00:37:50,760 --> 00:37:52,980
see the new version we don't have to
992
00:37:52,980 --> 00:37:55,560
compile anything manually it just works
993
00:37:55,560 --> 00:37:58,320
but we can make this even shorter we can
994
00:37:58,320 --> 00:38:00,540
go into our package.json file and add
995
00:38:00,540 --> 00:38:02,940
the script here a script is basically a
996
00:38:02,940 --> 00:38:05,460
shorthand to execute a command
997
00:38:05,460 --> 00:38:08,400
to be called this script start
998
00:38:08,400 --> 00:38:11,220
which is just a convention I think and
999
00:38:11,220 --> 00:38:13,260
then in quotation marks we type in the
1000
00:38:13,260 --> 00:38:15,300
command that we want to execute and when
1001
00:38:15,300 --> 00:38:17,280
we do it in here we don't need the npx
1002
00:38:17,280 --> 00:38:20,880
we can just type in node mon
1003
00:38:20,880 --> 00:38:26,400
and the same as before srz server.ts
1004
00:38:27,359 --> 00:38:29,640
let's save this and try it out so I'm
1005
00:38:29,640 --> 00:38:31,380
gonna stop the server down here with
1006
00:38:31,380 --> 00:38:33,780
Ctrl Z and then I have to confirm it
1007
00:38:33,780 --> 00:38:36,720
with a wire short for yes and now when
1008
00:38:36,720 --> 00:38:40,140
we type in npm start we execute the
1009
00:38:40,140 --> 00:38:41,400
script here
1010
00:38:41,400 --> 00:38:43,619
which now starts not one the same as
1011
00:38:43,619 --> 00:38:44,339
before
1012
00:38:44,339 --> 00:38:47,640
nice we can also specify the main entry
1013
00:38:47,640 --> 00:38:50,640
point of our server up here for our use
1014
00:38:50,640 --> 00:38:53,040
case that's not necessary because on our
1015
00:38:53,040 --> 00:38:54,960
Productions over later we will just run
1016
00:38:54,960 --> 00:38:57,180
our server file directly but I think
1017
00:38:57,180 --> 00:38:59,400
some web hostings need this so it's
1018
00:38:59,400 --> 00:39:01,380
probably a good idea to still Define it
1019
00:39:01,380 --> 00:39:04,640
and our main entry point will be a
1020
00:39:04,640 --> 00:39:07,140
server.js file in the dist folder later
1021
00:39:07,140 --> 00:39:09,900
this is where our server is actually
1022
00:39:09,900 --> 00:39:12,960
started because nodemon NTS node are not
1023
00:39:12,960 --> 00:39:15,780
suitable for production use let's save
1024
00:39:15,780 --> 00:39:17,940
this again and there's one more thing we
1025
00:39:17,940 --> 00:39:20,280
want to set up in this part and that's
1026
00:39:20,280 --> 00:39:23,160
eslint so once again in the command line
1027
00:39:23,160 --> 00:39:26,099
we stop our running server
1028
00:39:26,099 --> 00:39:31,200
type npm install short I minus upper
1029
00:39:31,200 --> 00:39:34,320
Castillo short for safe Dev
1030
00:39:34,320 --> 00:39:37,680
eslint so we installed eslint to the dev
1031
00:39:37,680 --> 00:39:39,480
dependencies
1032
00:39:39,480 --> 00:39:41,700
enter
1033
00:39:41,700 --> 00:39:43,740
and there it is with the latest version
1034
00:39:43,740 --> 00:39:46,820
and then we type in npx
1035
00:39:46,820 --> 00:39:50,339
eslint dash dash init
1036
00:39:50,339 --> 00:39:52,440
which creates a new file which is a
1037
00:39:52,440 --> 00:39:55,079
configuration file for eslint but we
1038
00:39:55,079 --> 00:39:57,240
have to go through some short steps here
1039
00:39:57,240 --> 00:39:59,280
in order to set this up need to install
1040
00:39:59,280 --> 00:40:01,200
the following packages okay to proceed
1041
00:40:01,200 --> 00:40:04,920
we type in y for yes to install this
1042
00:40:04,920 --> 00:40:07,740
and then we configure this file and you
1043
00:40:07,740 --> 00:40:09,720
switch options with the arrow keys on
1044
00:40:09,720 --> 00:40:11,579
your keyboard how would you like to use
1045
00:40:11,579 --> 00:40:14,760
eslint to check syntax and find problems
1046
00:40:14,760 --> 00:40:17,099
that's reasonable
1047
00:40:17,099 --> 00:40:19,020
what type of modules does your project
1048
00:40:19,020 --> 00:40:20,220
use
1049
00:40:20,220 --> 00:40:23,579
JavaScript modules import export this is
1050
00:40:23,579 --> 00:40:25,619
what we use
1051
00:40:25,619 --> 00:40:28,040
which framework does your project use
1052
00:40:28,040 --> 00:40:31,079
react vue.js or none of these none of
1053
00:40:31,079 --> 00:40:33,359
these because we are in node here
1054
00:40:33,359 --> 00:40:36,119
does your project use typescript yes of
1055
00:40:36,119 --> 00:40:36,900
course
1056
00:40:36,900 --> 00:40:39,480
where does your code run browser or node
1057
00:40:39,480 --> 00:40:41,640
as explained earlier this is running
1058
00:40:41,640 --> 00:40:44,940
with node.js so we selected
1059
00:40:44,940 --> 00:40:47,280
what format do you want your config file
1060
00:40:47,280 --> 00:40:49,560
to be in and here we select JavaScript
1061
00:40:49,560 --> 00:40:50,460
again
1062
00:40:50,460 --> 00:40:52,140
and now it has to install some new
1063
00:40:52,140 --> 00:40:54,300
dependencies which of course we do which
1064
00:40:54,300 --> 00:40:56,579
package manager do you want to use npm
1065
00:40:56,579 --> 00:40:58,920
which we have been using all the time
1066
00:40:58,920 --> 00:41:02,160
and then it installs some stuff as we
1067
00:41:02,160 --> 00:41:04,560
can see some new dependencies pop up and
1068
00:41:04,560 --> 00:41:08,700
this newer.eslint rz.js file which is
1069
00:41:08,700 --> 00:41:11,700
the configuration for the excellent now
1070
00:41:11,700 --> 00:41:14,040
even though we selected node in the
1071
00:41:14,040 --> 00:41:16,560
configuration it adds browser column
1072
00:41:16,560 --> 00:41:18,240
true I don't know why I don't know if
1073
00:41:18,240 --> 00:41:20,040
this is a bug but
1074
00:41:20,040 --> 00:41:22,920
we should change this to node and save
1075
00:41:22,920 --> 00:41:24,780
this
1076
00:41:24,780 --> 00:41:27,900
and lint helps us find problems in our
1077
00:41:27,900 --> 00:41:29,940
code some of those problems are errors
1078
00:41:29,940 --> 00:41:32,339
where we just write invalid syntax and
1079
00:41:32,339 --> 00:41:34,440
we can't run our code at all but some of
1080
00:41:34,440 --> 00:41:36,480
these problems are just bad habits that
1081
00:41:36,480 --> 00:41:38,339
we should avoid because they can cause
1082
00:41:38,339 --> 00:41:40,560
problems later let's see an example of
1083
00:41:40,560 --> 00:41:43,440
this one such bad habit is circumventing
1084
00:41:43,440 --> 00:41:46,079
typescripts Knight safety or undefined
1085
00:41:46,079 --> 00:41:47,099
safety
1086
00:41:47,099 --> 00:41:49,260
so for example let's say the sport could
1087
00:41:49,260 --> 00:41:51,540
be undefined for example because we get
1088
00:41:51,540 --> 00:41:53,220
this value from somewhere else and we
1089
00:41:53,220 --> 00:41:55,260
don't know if this actually contains a
1090
00:41:55,260 --> 00:41:57,180
value or not this would work here
1091
00:41:57,180 --> 00:41:59,339
because this method can work with an
1092
00:41:59,339 --> 00:42:02,099
undefined Port but some functions would
1093
00:42:02,099 --> 00:42:04,740
complain and I would say hey we don't
1094
00:42:04,740 --> 00:42:07,200
accept and possibly undefined value here
1095
00:42:07,200 --> 00:42:09,900
we need a number and one way to affix
1096
00:42:09,900 --> 00:42:11,760
this is to actually check if the value
1097
00:42:11,760 --> 00:42:13,619
is undefined or not just with a normal
1098
00:42:13,619 --> 00:42:16,619
alt if statement but another way is to
1099
00:42:16,619 --> 00:42:19,200
attend a compiler hey relax we know that
1100
00:42:19,200 --> 00:42:21,180
this value will never be undefined which
1101
00:42:21,180 --> 00:42:22,980
we do with this non-nine assertion
1102
00:42:22,980 --> 00:42:25,200
operator which is an exclamation mark
1103
00:42:25,200 --> 00:42:27,420
behind the variable this way we tell the
1104
00:42:27,420 --> 00:42:29,520
compiler here we know that this port
1105
00:42:29,520 --> 00:42:31,619
number will always have a value but as
1106
00:42:31,619 --> 00:42:33,300
we can see here we don't actually know
1107
00:42:33,300 --> 00:42:35,640
this we can make mistakes this way this
1108
00:42:35,640 --> 00:42:38,220
is why it's a bad habit and eslint will
1109
00:42:38,220 --> 00:42:40,320
tell us that this is a bad habit by
1110
00:42:40,320 --> 00:42:44,520
running it so we type in npx eslint
1111
00:42:44,520 --> 00:42:47,220
then the dot which executes eslin in
1112
00:42:47,220 --> 00:42:48,960
this folder if I'm not mistaken right
1113
00:42:48,960 --> 00:42:49,920
now
1114
00:42:49,920 --> 00:42:52,740
dash dash exterior this way we Define
1115
00:42:52,740 --> 00:42:55,140
the extensions of the files that we want
1116
00:42:55,140 --> 00:42:57,839
to check and we want to check our DOT TS
1117
00:42:57,839 --> 00:43:00,300
files so this is what we type in we
1118
00:43:00,300 --> 00:43:02,819
press enter and now this checks our code
1119
00:43:02,819 --> 00:43:04,880
and as you can see it found one problem
1120
00:43:04,880 --> 00:43:07,680
forbidden online assertion this is just
1121
00:43:07,680 --> 00:43:09,900
a warning it's not an error so our code
1122
00:43:09,900 --> 00:43:12,359
would still run but this tells us here
1123
00:43:12,359 --> 00:43:14,819
this is probably not a good idea and we
1124
00:43:14,819 --> 00:43:17,280
should do something else with it so
1125
00:43:17,280 --> 00:43:19,079
let's change the spec
1126
00:43:19,079 --> 00:43:22,020
run it again and this problem will not
1127
00:43:22,020 --> 00:43:24,300
show up again now of course it's a good
1128
00:43:24,300 --> 00:43:27,359
idea again to create a script for this
1129
00:43:27,359 --> 00:43:29,400
so we don't have to type this out all
1130
00:43:29,400 --> 00:43:33,060
the time so now package.json file
1131
00:43:33,060 --> 00:43:36,680
let's add a lint command
1132
00:43:37,500 --> 00:43:39,960
again we don't have to type the npx this
1133
00:43:39,960 --> 00:43:41,040
time
1134
00:43:41,040 --> 00:43:43,140
just eslint
1135
00:43:43,140 --> 00:43:45,180
period
1136
00:43:45,180 --> 00:43:49,280
EXT dot TS
1137
00:43:50,099 --> 00:43:54,359
and then we can run this with npm run
1138
00:43:54,359 --> 00:43:55,859
lint
1139
00:43:55,859 --> 00:43:58,200
now why do we have to enter run now and
1140
00:43:58,200 --> 00:44:00,540
before we just typed npm start at node
1141
00:44:00,540 --> 00:44:03,599
npm Run start this is because start has
1142
00:44:03,599 --> 00:44:05,880
the shorthand so npm start is a
1143
00:44:05,880 --> 00:44:08,400
shorthand for npm Run start however for
1144
00:44:08,400 --> 00:44:10,440
other types of commands like lint we
1145
00:44:10,440 --> 00:44:12,300
have to type it out like this
1146
00:44:12,300 --> 00:44:14,579
npm run lint
1147
00:44:14,579 --> 00:44:16,200
let's add our
1148
00:44:16,200 --> 00:44:18,960
problem here again to see if it still
1149
00:44:18,960 --> 00:44:21,380
works
1150
00:44:21,900 --> 00:44:24,599
and yeah it works change this back
1151
00:44:24,599 --> 00:44:27,660
because we are not bad callers right but
1152
00:44:27,660 --> 00:44:29,040
it's still a bit annoying to always
1153
00:44:29,040 --> 00:44:31,380
execute this over the command line it
1154
00:44:31,380 --> 00:44:33,780
would be much nicer if the IDE or the
1155
00:44:33,780 --> 00:44:35,940
code editor would show us this directly
1156
00:44:35,940 --> 00:44:39,000
and there's an IDE extension for this so
1157
00:44:39,000 --> 00:44:41,160
here NBS chord on the left side we can
1158
00:44:41,160 --> 00:44:43,200
open this extensions tab
1159
00:44:43,200 --> 00:44:45,540
and it's already here under the popular
1160
00:44:45,540 --> 00:44:48,359
extensions eslint
1161
00:44:48,359 --> 00:44:50,940
and this extension will check our code
1162
00:44:50,940 --> 00:44:53,700
directly with our eslint configuration
1163
00:44:53,700 --> 00:44:55,920
so we don't have to execute this in the
1164
00:44:55,920 --> 00:44:59,359
command line so let's install those
1165
00:44:59,520 --> 00:45:02,640
and see if this changed anything make
1166
00:45:02,640 --> 00:45:05,160
this here bad once again and as you can
1167
00:45:05,160 --> 00:45:06,660
see now we have this yellows quickly
1168
00:45:06,660 --> 00:45:09,119
line and the eslint warning shows up
1169
00:45:09,119 --> 00:45:12,140
directly in our code
1170
00:45:12,300 --> 00:45:15,300
nice so this will save our behind in the
1171
00:45:15,300 --> 00:45:17,280
future of course you can check out the
1172
00:45:17,280 --> 00:45:19,140
other extensions as well there are some
1173
00:45:19,140 --> 00:45:21,060
really cool ones in there we will get
1174
00:45:21,060 --> 00:45:22,859
back to this later but for now we are
1175
00:45:22,859 --> 00:45:25,440
done with this now there's one more file
1176
00:45:25,440 --> 00:45:27,839
we want to add to our project and that's
1177
00:45:27,839 --> 00:45:30,359
the git ignore file if you already have
1178
00:45:30,359 --> 00:45:32,099
programming experience then you probably
1179
00:45:32,099 --> 00:45:34,560
already know what git ignore is it's a
1180
00:45:34,560 --> 00:45:36,420
file that defines other files and
1181
00:45:36,420 --> 00:45:38,160
folders in our project that we don't
1182
00:45:38,160 --> 00:45:40,260
want to commit to git this is important
1183
00:45:40,260 --> 00:45:42,060
for example if you want to push your
1184
00:45:42,060 --> 00:45:44,280
project to GitHub for others to see
1185
00:45:44,280 --> 00:45:47,040
because some folders are just big and we
1186
00:45:47,040 --> 00:45:48,839
don't need them so we save space and
1187
00:45:48,839 --> 00:45:51,060
bandwidth by not committing them but
1188
00:45:51,060 --> 00:45:52,680
there are also certain files that we
1189
00:45:52,680 --> 00:45:54,540
must not commit because they contain for
1190
00:45:54,540 --> 00:45:57,000
example database passwords and all these
1191
00:45:57,000 --> 00:45:58,619
files have to be added to a disk it
1192
00:45:58,619 --> 00:46:00,599
ignore file so we create a new file here
1193
00:46:00,599 --> 00:46:02,460
in our project outside of the source
1194
00:46:02,460 --> 00:46:05,460
folder and the name is dot that's
1195
00:46:05,460 --> 00:46:07,400
important it has to start with this dot
1196
00:46:07,400 --> 00:46:11,220
git ignore just as one word like this
1197
00:46:11,220 --> 00:46:13,200
now how do we know what we have to put
1198
00:46:13,200 --> 00:46:14,940
in there either we can design this
1199
00:46:14,940 --> 00:46:16,800
ourselves but there are templates for
1200
00:46:16,800 --> 00:46:18,780
this available on GitHub and as you can
1201
00:46:18,780 --> 00:46:20,339
see it's from github's web your own
1202
00:46:20,339 --> 00:46:22,380
account so those are basically official
1203
00:46:22,380 --> 00:46:23,940
files
1204
00:46:23,940 --> 00:46:27,720
we can copy this here copy raw contents
1205
00:46:27,720 --> 00:46:30,300
and you can either look this up yourself
1206
00:46:30,300 --> 00:46:33,240
or you can go to my GitHub project in
1207
00:46:33,240 --> 00:46:35,579
the description below and go into the
1208
00:46:35,579 --> 00:46:37,440
git ignore file there and copy its
1209
00:46:37,440 --> 00:46:39,060
contents
1210
00:46:39,060 --> 00:46:41,339
and then we just paste this whole stuff
1211
00:46:41,339 --> 00:46:43,800
in here and save it
1212
00:46:43,800 --> 00:46:46,260
this contains more than we actually need
1213
00:46:46,260 --> 00:46:48,599
but that's not a problem it's better to
1214
00:46:48,599 --> 00:46:50,280
have too much in here than too little
1215
00:46:50,280 --> 00:46:52,680
and accidentally expose stuff that we
1216
00:46:52,680 --> 00:46:54,119
don't want to expose
1217
00:46:54,119 --> 00:46:56,640
as you can see the node modules folder
1218
00:46:56,640 --> 00:46:58,740
is in here as well so if we Commit This
1219
00:46:58,740 --> 00:47:01,140
to get then this will be ignored and
1220
00:47:01,140 --> 00:47:03,420
it's not necessary because this contains
1221
00:47:03,420 --> 00:47:05,819
only the code of the packages we
1222
00:47:05,819 --> 00:47:07,800
installed but we can always install
1223
00:47:07,800 --> 00:47:10,500
these packages from our package.json
1224
00:47:10,500 --> 00:47:12,359
file here so here we have defined what
1225
00:47:12,359 --> 00:47:14,339
packages we need and later for example
1226
00:47:14,339 --> 00:47:16,680
when we pull this code on our web server
1227
00:47:16,680 --> 00:47:19,079
without the node modules folder we just
1228
00:47:19,079 --> 00:47:21,119
type in npm install and all the latest
1229
00:47:21,119 --> 00:47:23,460
versions here will be installed and this
1230
00:47:23,460 --> 00:47:25,500
node modules folder will be recreated
1231
00:47:25,500 --> 00:47:27,480
and the main reason why we don't push
1232
00:47:27,480 --> 00:47:29,579
this is just because it's a huge boiler
1233
00:47:29,579 --> 00:47:32,160
and we don't actually need it our eslint
1234
00:47:32,160 --> 00:47:34,380
and TS config files on the other hand
1235
00:47:34,380 --> 00:47:36,660
are pushed to GitHub because they
1236
00:47:36,660 --> 00:47:38,880
contain configuration that we want to
1237
00:47:38,880 --> 00:47:40,920
keep when we work in the team right then
1238
00:47:40,920 --> 00:47:42,780
everyone should use the same lint rules
1239
00:47:42,780 --> 00:47:45,060
and typescript configuration so those
1240
00:47:45,060 --> 00:47:47,220
are not in the skid ignore file because
1241
00:47:47,220 --> 00:47:49,260
we want to push them usually
1242
00:47:49,260 --> 00:47:51,300
okay the next step is to set up the
1243
00:47:51,300 --> 00:47:53,819
mongodb database and connect this to our
1244
00:47:53,819 --> 00:47:55,500
backend server but there's one more
1245
00:47:55,500 --> 00:47:57,660
thing I want to clarify before we start
1246
00:47:57,660 --> 00:47:59,339
with this and that is you might be
1247
00:47:59,339 --> 00:48:01,140
wondering is this what we are building
1248
00:48:01,140 --> 00:48:04,200
here a restful API is this a so-called
1249
00:48:04,200 --> 00:48:06,540
rest API and if not what's the
1250
00:48:06,540 --> 00:48:08,339
difference between a rest API and our
1251
00:48:08,339 --> 00:48:10,140
server that we are building here a rest
1252
00:48:10,140 --> 00:48:12,599
API is also a back-end server but it
1253
00:48:12,599 --> 00:48:15,300
fulfills certain constraints and rest
1254
00:48:15,300 --> 00:48:18,180
apis are usually servers that are
1255
00:48:18,180 --> 00:48:20,640
accessed by many different clients and
1256
00:48:20,640 --> 00:48:22,380
this is why they follow certain rules
1257
00:48:22,380 --> 00:48:25,140
one of those rules is that they don't
1258
00:48:25,140 --> 00:48:27,540
contain any state our server will later
1259
00:48:27,540 --> 00:48:29,579
contains States because users will be
1260
00:48:29,579 --> 00:48:32,040
able to log in and for each login there
1261
00:48:32,040 --> 00:48:35,160
will be a session in our database that
1262
00:48:35,160 --> 00:48:37,380
connects this user and lets the server
1263
00:48:37,380 --> 00:48:39,540
know that this user is logged in and
1264
00:48:39,540 --> 00:48:41,160
this is a violation of the restful
1265
00:48:41,160 --> 00:48:43,740
constraints because this is not a public
1266
00:48:43,740 --> 00:48:45,720
rest API we are building here it's our
1267
00:48:45,720 --> 00:48:48,240
own backend server for only our own
1268
00:48:48,240 --> 00:48:50,579
singer client by but you can build a
1269
00:48:50,579 --> 00:48:52,980
restful API with the exact same stuff we
1270
00:48:52,980 --> 00:48:55,380
are using here with Express and node the
1271
00:48:55,380 --> 00:48:57,720
same code the same endpoints you can
1272
00:48:57,720 --> 00:48:59,579
also use this to build a public API
1273
00:48:59,579 --> 00:49:01,560
maybe that's an interesting project for
1274
00:49:01,560 --> 00:49:03,900
you to build a public API that serves
1275
00:49:03,900 --> 00:49:06,180
some data that people can get from your
1276
00:49:06,180 --> 00:49:08,760
server just as an idea okay and now in
1277
00:49:08,760 --> 00:49:12,740
the next section we will install mongodb
1278
00:49:16,319 --> 00:49:19,140
okay the next step is to set up our
1279
00:49:19,140 --> 00:49:22,560
mongodb database mongodb is the m in the
1280
00:49:22,560 --> 00:49:25,200
moon acronym and it's the place where we
1281
00:49:25,200 --> 00:49:28,200
put our data later like our nodes and
1282
00:49:28,200 --> 00:49:31,319
also our user data and mongodb is a
1283
00:49:31,319 --> 00:49:33,960
so-called schema-less database which
1284
00:49:33,960 --> 00:49:36,180
basically means that we can put any data
1285
00:49:36,180 --> 00:49:38,040
into any document we want in any
1286
00:49:38,040 --> 00:49:40,380
combination and it will work that's the
1287
00:49:40,380 --> 00:49:42,780
opposite of a database like SQL where
1288
00:49:42,780 --> 00:49:44,579
you have a certain schema that you have
1289
00:49:44,579 --> 00:49:47,040
to adhere to and all entries within the
1290
00:49:47,040 --> 00:49:48,960
table basically have the same columns
1291
00:49:48,960 --> 00:49:50,819
and the same data types and if you want
1292
00:49:50,819 --> 00:49:52,500
to change this structure then you have
1293
00:49:52,500 --> 00:49:54,660
to migrate this table a schema-less
1294
00:49:54,660 --> 00:49:56,940
database is a bit like the wild west of
1295
00:49:56,940 --> 00:49:59,339
databases but it's also how firebases
1296
00:49:59,339 --> 00:50:01,200
database works for example it's also
1297
00:50:01,200 --> 00:50:03,000
schema-less so it's a very popular
1298
00:50:03,000 --> 00:50:05,280
concept and it works in many situations
1299
00:50:05,280 --> 00:50:07,500
it has downsides and benefits like
1300
00:50:07,500 --> 00:50:09,359
everything in life and if you want to
1301
00:50:09,359 --> 00:50:10,980
learn more about the differences then
1302
00:50:10,980 --> 00:50:13,079
you can look them up in Google but we
1303
00:50:13,079 --> 00:50:14,940
will use this here and again it's a very
1304
00:50:14,940 --> 00:50:17,460
popular choice and it works for many big
1305
00:50:17,460 --> 00:50:20,520
companies and to host this database you
1306
00:50:20,520 --> 00:50:22,260
basically have two choices you can
1307
00:50:22,260 --> 00:50:24,359
either self-host it on your own server
1308
00:50:24,359 --> 00:50:26,520
or you can use a service like mongodb
1309
00:50:26,520 --> 00:50:29,099
Atlas which is a hosting provider for
1310
00:50:29,099 --> 00:50:31,800
mongodb databases the benefit of using a
1311
00:50:31,800 --> 00:50:34,260
service like Atlas is that it takes care
1312
00:50:34,260 --> 00:50:35,940
of many things that you would have to do
1313
00:50:35,940 --> 00:50:38,839
yourself otherwise like scaling security
1314
00:50:38,839 --> 00:50:42,300
concerns encryption backups and so on so
1315
00:50:42,300 --> 00:50:43,680
I'm a fan of using these Services
1316
00:50:43,680 --> 00:50:46,020
because it saves us time the downside is
1317
00:50:46,020 --> 00:50:47,760
that in production we have to pay money
1318
00:50:47,760 --> 00:50:49,559
for this we don't have to pay money for
1319
00:50:49,559 --> 00:50:51,059
this tutorial because they have a free
1320
00:50:51,059 --> 00:50:52,680
tier but if you want to scale up of
1321
00:50:52,680 --> 00:50:54,599
course this costs money but I think it's
1322
00:50:54,599 --> 00:50:56,640
worth the convenience you get that's why
1323
00:50:56,640 --> 00:50:58,559
we will use mongodb Atlas in this
1324
00:50:58,559 --> 00:51:00,420
tutorial and to create an account there
1325
00:51:00,420 --> 00:51:03,660
we go to mongodb.com Cloud slash Atlas
1326
00:51:03,660 --> 00:51:06,000
or just type mongodb Atlas into Google
1327
00:51:06,000 --> 00:51:08,160
it's probably the faster way
1328
00:51:08,160 --> 00:51:10,200
and we click on startria and by the way
1329
00:51:10,200 --> 00:51:12,960
this is the most ugly button animation
1330
00:51:12,960 --> 00:51:15,900
I've ever seen but it's probably a rich
1331
00:51:15,900 --> 00:51:17,819
company and it works for them so maybe
1332
00:51:17,819 --> 00:51:20,160
we should take an example of that all
1333
00:51:20,160 --> 00:51:21,900
right so we have to fill out some data
1334
00:51:21,900 --> 00:51:27,240
here first name last name no company
1335
00:51:27,240 --> 00:51:29,700
and here I'm gonna use email address
1336
00:51:29,700 --> 00:51:31,980
that I've prepared just for recording so
1337
00:51:31,980 --> 00:51:34,619
we have a completely new setup which is
1338
00:51:34,619 --> 00:51:38,400
recording it's coding in floor punct.com
1339
00:51:38,400 --> 00:51:41,760
and we pick a secure password which I've
1340
00:51:41,760 --> 00:51:44,700
already prepared
1341
00:51:44,700 --> 00:51:47,400
all right we agree the terms of servers
1342
00:51:47,400 --> 00:51:49,680
and privacy policy which you can read if
1343
00:51:49,680 --> 00:51:52,619
you want and create an atlas account
1344
00:51:52,619 --> 00:51:55,260
and we have to verify our email so let's
1345
00:51:55,260 --> 00:51:57,900
do that real quick okay so I've verified
1346
00:51:57,900 --> 00:52:01,319
the email so let's go back to mongodb
1347
00:52:01,319 --> 00:52:04,140
account remove this part here from the
1348
00:52:04,140 --> 00:52:08,180
URL to get to the login screen
1349
00:52:08,460 --> 00:52:10,980
it locks Us in automatically otherwise
1350
00:52:10,980 --> 00:52:12,900
just type in your email and password you
1351
00:52:12,900 --> 00:52:14,400
just signed up with
1352
00:52:14,400 --> 00:52:16,680
and another queer thing about mongodb
1353
00:52:16,680 --> 00:52:18,900
Atlas is that they give you this back
1354
00:52:18,900 --> 00:52:21,300
end this UI to work with and you don't
1355
00:52:21,300 --> 00:52:23,400
have to do this over the command line so
1356
00:52:23,400 --> 00:52:25,319
let's start setting this up first of all
1357
00:52:25,319 --> 00:52:27,119
as they say here the current IP address
1358
00:52:27,119 --> 00:52:29,520
should be added to a white list
1359
00:52:29,520 --> 00:52:31,319
so let's do this by clicking on this
1360
00:52:31,319 --> 00:52:32,880
button here and it does this
1361
00:52:32,880 --> 00:52:35,099
automatically we later have to go to the
1362
00:52:35,099 --> 00:52:37,380
network settings again and add the IP
1363
00:52:37,380 --> 00:52:39,900
address of our server but not for now
1364
00:52:39,900 --> 00:52:42,059
and now we can actually create a new
1365
00:52:42,059 --> 00:52:44,940
database by clicking on this button
1366
00:52:44,940 --> 00:52:47,520
and then we have to do some setup here
1367
00:52:47,520 --> 00:52:50,460
we want to start with a free account so
1368
00:52:50,460 --> 00:52:52,980
we have to select the shared hosting
1369
00:52:52,980 --> 00:52:54,720
option later if you are in production
1370
00:52:54,720 --> 00:52:56,940
you can decide to use a dedicated or
1371
00:52:56,940 --> 00:52:59,040
serverless configuration most likely
1372
00:52:59,040 --> 00:53:01,859
dedicated but for now we use shared so
1373
00:53:01,859 --> 00:53:04,140
we click on Create and we have to do
1374
00:53:04,140 --> 00:53:06,660
some setup here we have to select where
1375
00:53:06,660 --> 00:53:08,460
our database will be hosted because
1376
00:53:08,460 --> 00:53:10,980
mongodb Atlas works together with these
1377
00:53:10,980 --> 00:53:12,839
big hosting providers you can select
1378
00:53:12,839 --> 00:53:14,940
whatever you want I'm gonna stick to
1379
00:53:14,940 --> 00:53:16,319
Amazon
1380
00:53:16,319 --> 00:53:19,079
a location for our database and it makes
1381
00:53:19,079 --> 00:53:21,240
sense to pick a location that's close to
1382
00:53:21,240 --> 00:53:23,040
the location of your deployment server
1383
00:53:23,040 --> 00:53:25,200
later I'm gonna stick with Frankfurt
1384
00:53:25,200 --> 00:53:27,960
which is here in Germany where I live
1385
00:53:27,960 --> 00:53:30,780
cluster tier m010 box is the free tier
1386
00:53:30,780 --> 00:53:33,420
which of course has some limitations but
1387
00:53:33,420 --> 00:53:35,579
it's enough for playing around and it's
1388
00:53:35,579 --> 00:53:37,619
also enough even for small production
1389
00:53:37,619 --> 00:53:39,720
projects
1390
00:53:39,720 --> 00:53:43,559
backup is not enabled in m0 clusters but
1391
00:53:43,559 --> 00:53:45,780
that's fine and termination protection
1392
00:53:45,780 --> 00:53:47,940
is a relatively new feature
1393
00:53:47,940 --> 00:53:49,800
it makes sense it just adds an
1394
00:53:49,800 --> 00:53:51,720
additional step before you can delete
1395
00:53:51,720 --> 00:53:53,940
your cluster which is always good to
1396
00:53:53,940 --> 00:53:55,619
have because you never want to delete
1397
00:53:55,619 --> 00:53:57,599
this by accident we can give it a name
1398
00:53:57,599 --> 00:53:59,460
the name doesn't really matter in my
1399
00:53:59,460 --> 00:54:00,599
opinion you can give it something
1400
00:54:00,599 --> 00:54:02,640
descriptive but I'm just gonna stick to
1401
00:54:02,640 --> 00:54:04,680
a cluster zero
1402
00:54:04,680 --> 00:54:07,380
and we click on create cluster and now
1403
00:54:07,380 --> 00:54:09,960
it charges your credit card with 120
1404
00:54:09,960 --> 00:54:11,579
dollars no I'm just kidding it's
1405
00:54:11,579 --> 00:54:13,680
completely free unless you upgrade to a
1406
00:54:13,680 --> 00:54:16,020
higher cluster and now we have to do
1407
00:54:16,020 --> 00:54:18,240
some more setup here how would you like
1408
00:54:18,240 --> 00:54:21,180
to authenticate your connection via keep
1409
00:54:21,180 --> 00:54:23,819
username and password so here we type in
1410
00:54:23,819 --> 00:54:25,680
our username I'm gonna call it Florian
1411
00:54:25,680 --> 00:54:27,900
and I'm gonna Auto generate a secure
1412
00:54:27,900 --> 00:54:29,099
password
1413
00:54:29,099 --> 00:54:31,680
copy it because we later need this in
1414
00:54:31,680 --> 00:54:34,020
our code of course and we create this
1415
00:54:34,020 --> 00:54:35,099
user
1416
00:54:35,099 --> 00:54:37,980
now the user is created here
1417
00:54:37,980 --> 00:54:39,540
we want to connect from a local
1418
00:54:39,540 --> 00:54:40,980
environment
1419
00:54:40,980 --> 00:54:43,020
and we already added our current IP
1420
00:54:43,020 --> 00:54:44,880
address to this list so there should be
1421
00:54:44,880 --> 00:54:47,040
enough and then we click finish and
1422
00:54:47,040 --> 00:54:49,140
close
1423
00:54:49,140 --> 00:54:52,920
and here is our shiny new database
1424
00:54:52,920 --> 00:54:55,680
can you smell that that's the smell of a
1425
00:54:55,680 --> 00:54:58,380
fresh database we can browse collections
1426
00:54:58,380 --> 00:55:00,540
but we don't have any yet but we want to
1427
00:55:00,540 --> 00:55:03,300
connect our code our backend server to
1428
00:55:03,300 --> 00:55:04,920
this database so we click on connect
1429
00:55:04,920 --> 00:55:07,440
then we have different options you can
1430
00:55:07,440 --> 00:55:09,660
connect over the command line with a
1431
00:55:09,660 --> 00:55:11,760
tool they have which is called Compass
1432
00:55:11,760 --> 00:55:13,680
but we want to click on connect your
1433
00:55:13,680 --> 00:55:14,700
application
1434
00:55:14,700 --> 00:55:16,859
and then we want to copy the string here
1435
00:55:16,859 --> 00:55:18,720
by clicking on this button because we
1436
00:55:18,720 --> 00:55:20,160
need this in our code
1437
00:55:20,160 --> 00:55:23,339
and then we open our code again and this
1438
00:55:23,339 --> 00:55:26,280
time I will open the project one layer
1439
00:55:26,280 --> 00:55:28,440
higher the last time we were inside this
1440
00:55:28,440 --> 00:55:30,420
backend folder and open the project from
1441
00:55:30,420 --> 00:55:32,819
here but now I want to open it from this
1442
00:55:32,819 --> 00:55:34,559
folder here where we can see the backend
1443
00:55:34,559 --> 00:55:36,660
folder the only reason for this is that
1444
00:55:36,660 --> 00:55:38,520
this way we have the back end and later
1445
00:55:38,520 --> 00:55:41,280
the front end folders both opened in vs
1446
00:55:41,280 --> 00:55:43,020
code you don't have to do this you can
1447
00:55:43,020 --> 00:55:45,000
also open them separately I do this
1448
00:55:45,000 --> 00:55:46,800
because I want to push them to the same
1449
00:55:46,800 --> 00:55:48,780
GitHub repository so you can look at the
1450
00:55:48,780 --> 00:55:50,520
code later this is also where there is
1451
00:55:50,520 --> 00:55:52,380
this dot get folder which probably
1452
00:55:52,380 --> 00:55:54,480
doesn't exist on your side it doesn't
1453
00:55:54,480 --> 00:55:56,460
have to you can ignore this so again I'm
1454
00:55:56,460 --> 00:55:58,440
going to open this by shift right
1455
00:55:58,440 --> 00:56:02,099
clicking to open the command line here
1456
00:56:02,099 --> 00:56:04,859
and then I just type in code
1457
00:56:04,859 --> 00:56:07,380
then the space and then a period
1458
00:56:07,380 --> 00:56:09,720
to open this folder
1459
00:56:09,720 --> 00:56:11,880
and we can close this
1460
00:56:11,880 --> 00:56:13,800
and this is what I meant now we open
1461
00:56:13,800 --> 00:56:16,260
this whole coding and flow Mount folder
1462
00:56:16,260 --> 00:56:18,000
here so we can see the back end and
1463
00:56:18,000 --> 00:56:20,220
later the front end both inside this
1464
00:56:20,220 --> 00:56:23,400
project view now our mongodb credentials
1465
00:56:23,400 --> 00:56:25,680
are of course data that we don't want to
1466
00:56:25,680 --> 00:56:28,440
give to anybody willy-nilly it's
1467
00:56:28,440 --> 00:56:30,480
sensitive data that's why it's a good
1468
00:56:30,480 --> 00:56:32,099
idea to also not push this information
1469
00:56:32,099 --> 00:56:34,920
into the GitHub repository in other
1470
00:56:34,920 --> 00:56:37,079
words we want to put it somewhere in the
1471
00:56:37,079 --> 00:56:39,480
file that's included in our git ignore
1472
00:56:39,480 --> 00:56:41,760
and the correct place for this kind of
1473
00:56:41,760 --> 00:56:45,119
stuff is the NF file which is included
1474
00:56:45,119 --> 00:56:47,819
in the skid ignore we pasted earlier and
1475
00:56:47,819 --> 00:56:49,859
in there you put environment variables
1476
00:56:49,859 --> 00:56:51,599
which can be configurational that
1477
00:56:51,599 --> 00:56:53,760
changes depending on where you run your
1478
00:56:53,760 --> 00:56:56,280
server for example on localhost or on a
1479
00:56:56,280 --> 00:56:58,500
real deployment but it's also the place
1480
00:56:58,500 --> 00:57:00,660
where people usually put sensitive data
1481
00:57:00,660 --> 00:57:03,059
like database credentials so what we do
1482
00:57:03,059 --> 00:57:04,920
is here in the root folder we right
1483
00:57:04,920 --> 00:57:07,260
click and create a new file which we
1484
00:57:07,260 --> 00:57:10,079
call.nf the same name as you can read
1485
00:57:10,079 --> 00:57:12,119
here on the git ignore
1486
00:57:12,119 --> 00:57:14,339
and in here we want to put our mongodb
1487
00:57:14,339 --> 00:57:17,160
connection string so we put a variable
1488
00:57:17,160 --> 00:57:19,260
in here that we have to give a name you
1489
00:57:19,260 --> 00:57:21,300
can color whatever you want I'm going to
1490
00:57:21,300 --> 00:57:24,780
name it mongoa underscore
1491
00:57:24,780 --> 00:57:27,599
connection underscore string and the
1492
00:57:27,599 --> 00:57:29,819
convention for this kind of constants is
1493
00:57:29,819 --> 00:57:31,740
to write them in our uppercase with
1494
00:57:31,740 --> 00:57:33,960
underscores but you can also use candle
1495
00:57:33,960 --> 00:57:36,240
case or whatever you want then an equal
1496
00:57:36,240 --> 00:57:38,220
sign and then we paste the connection
1497
00:57:38,220 --> 00:57:40,619
string we just copied and since this is
1498
00:57:40,619 --> 00:57:42,780
just one string without spaces we don't
1499
00:57:42,780 --> 00:57:45,660
have to add quotation marks here
1500
00:57:45,660 --> 00:57:48,059
we can if we want
1501
00:57:48,059 --> 00:57:50,760
but we can also keep it like this this
1502
00:57:50,760 --> 00:57:52,500
part here in front of the colon is our
1503
00:57:52,500 --> 00:57:54,780
username we just defined when we set up
1504
00:57:54,780 --> 00:57:56,220
our cluster
1505
00:57:56,220 --> 00:57:58,079
and we have to replace this part here
1506
00:57:58,079 --> 00:58:00,540
for the password I'm just going to paste
1507
00:58:00,540 --> 00:58:02,640
the password here because it's just for
1508
00:58:02,640 --> 00:58:05,040
this course I don't actually use this in
1509
00:58:05,040 --> 00:58:06,780
production but of course you should keep
1510
00:58:06,780 --> 00:58:09,119
your password secret and only share it
1511
00:58:09,119 --> 00:58:10,859
with team members or whoever is allowed
1512
00:58:10,859 --> 00:58:13,260
to know it and let's also put our port
1513
00:58:13,260 --> 00:58:15,660
number in here because this can also
1514
00:58:15,660 --> 00:58:17,760
change between deployments so you
1515
00:58:17,760 --> 00:58:19,619
usually want to put this into your end
1516
00:58:19,619 --> 00:58:20,760
file
1517
00:58:20,760 --> 00:58:24,420
so we just write Port equals and 5000.
1518
00:58:24,420 --> 00:58:26,520
then we save this with Ctrl s remember
1519
00:58:26,520 --> 00:58:28,440
in the S code by default you have to
1520
00:58:28,440 --> 00:58:31,020
save your files and by the way you can
1521
00:58:31,020 --> 00:58:33,119
see that node modules here is grayed out
1522
00:58:33,119 --> 00:58:35,640
now which indicates that this folder is
1523
00:58:35,640 --> 00:58:38,040
not pushed to a GitHub it might not be
1524
00:58:38,040 --> 00:58:39,900
grayed out on your side if you didn't
1525
00:58:39,900 --> 00:58:41,940
actually deploy this project to GitHub
1526
00:58:41,940 --> 00:58:44,940
just as a node and NF will later also be
1527
00:58:44,940 --> 00:58:46,740
grayed out on my side because it's
1528
00:58:46,740 --> 00:58:49,200
included in the git ignore file but I
1529
00:58:49,200 --> 00:58:50,880
did a little mistake we have to put this
1530
00:58:50,880 --> 00:58:52,920
end file into our backend folder not
1531
00:58:52,920 --> 00:58:55,020
here on the root folder because it
1532
00:58:55,020 --> 00:58:57,180
belongs to our backend code so let's
1533
00:58:57,180 --> 00:58:59,400
move it in here drag and drop very
1534
00:58:59,400 --> 00:59:00,540
simple
1535
00:59:00,540 --> 00:59:02,220
and now you can also see that it's
1536
00:59:02,220 --> 00:59:04,260
actually grayed out because now it
1537
00:59:04,260 --> 00:59:06,119
recognizes the git ignore file in the
1538
00:59:06,119 --> 00:59:08,339
same folder but this is only necessary
1539
00:59:08,339 --> 00:59:10,500
if you open this parent folder like I
1540
00:59:10,500 --> 00:59:12,599
did if the backend folder is your root
1541
00:59:12,599 --> 00:59:14,819
folder then of course the end file goes
1542
00:59:14,819 --> 00:59:16,920
in there so just put the end file in
1543
00:59:16,920 --> 00:59:19,500
your backend folder basically and now we
1544
00:59:19,500 --> 00:59:21,180
need to install another package that
1545
00:59:21,180 --> 00:59:23,400
allows us to load this end file because
1546
00:59:23,400 --> 00:59:25,859
nodes doesn't do this by default so we
1547
00:59:25,859 --> 00:59:27,900
want to open the terminal again but it's
1548
00:59:27,900 --> 00:59:29,700
important that we are inside the backend
1549
00:59:29,700 --> 00:59:30,720
folder
1550
00:59:30,720 --> 00:59:32,880
so we type in ZD and it should be the
1551
00:59:32,880 --> 00:59:36,660
same on Windows Mac and Linux backend CD
1552
00:59:36,660 --> 00:59:39,540
stands for change directory we go in
1553
00:59:39,540 --> 00:59:41,339
here and in here we want to install our
1554
00:59:41,339 --> 00:59:42,720
new package
1555
00:59:42,720 --> 00:59:46,140
so the same as before we type npmi
1556
00:59:46,140 --> 00:59:48,359
.nf
1557
00:59:48,359 --> 00:59:50,460
which is the name of the package that
1558
00:59:50,460 --> 00:59:52,980
allows us to load n files and if you
1559
00:59:52,980 --> 00:59:54,359
want to learn more about this package
1560
00:59:54,359 --> 00:59:56,400
there is a page for basically all of
1561
00:59:56,400 --> 01:00:01,319
them on this npm js.com website you can
1562
01:00:01,319 --> 01:00:04,079
basically read about any package here as
1563
01:00:04,079 --> 01:00:05,579
you can see it's a popular package as
1564
01:00:05,579 --> 01:00:07,440
well it shows us the configuration
1565
01:00:07,440 --> 01:00:10,319
options and so on but don't probably I
1566
01:00:10,319 --> 01:00:12,119
will show you how this works but first
1567
01:00:12,119 --> 01:00:14,579
we want to install another dependency
1568
01:00:14,579 --> 01:00:19,680
npm IO this one is called mongoose
1569
01:00:19,680 --> 01:00:24,480
and Mongoose is a object modeling for
1570
01:00:24,480 --> 01:00:26,880
node.js so it's basically a package that
1571
01:00:26,880 --> 01:00:29,339
makes it easier to work with the mongodb
1572
01:00:29,339 --> 01:00:31,500
database so it's less of an Arcane
1573
01:00:31,500 --> 01:00:33,540
language as the original as you can see
1574
01:00:33,540 --> 01:00:35,700
it creates these objects and types and
1575
01:00:35,700 --> 01:00:36,900
it's really cool
1576
01:00:36,900 --> 01:00:39,660
it has a humongous documentation which
1577
01:00:39,660 --> 01:00:42,059
can make you lose the will to live if
1578
01:00:42,059 --> 01:00:43,619
you see how much information is in there
1579
01:00:43,619 --> 01:00:45,240
but you don't have to know all of this
1580
01:00:45,240 --> 01:00:47,400
and I'll show you the stuff you have to
1581
01:00:47,400 --> 01:00:50,040
know of course in this course and just
1582
01:00:50,040 --> 01:00:52,440
as a reminder all the packages we
1583
01:00:52,440 --> 01:00:55,559
installed are inside our package.json
1584
01:00:55,559 --> 01:00:56,700
file here
1585
01:00:56,700 --> 01:00:58,920
they get added to the dependencies block
1586
01:00:58,920 --> 01:01:00,660
and by the way in the last video before
1587
01:01:00,660 --> 01:01:03,059
I push the code I swapped the order of
1588
01:01:03,059 --> 01:01:04,559
Dev dependencies and dependencies
1589
01:01:04,559 --> 01:01:06,059
because I think they should be at the
1590
01:01:06,059 --> 01:01:08,160
top but it's unnecessary the order here
1591
01:01:08,160 --> 01:01:10,140
doesn't matter okay and now let's go
1592
01:01:10,140 --> 01:01:13,559
into our server TS file again and start
1593
01:01:13,559 --> 01:01:15,839
this connection to our database so we
1594
01:01:15,839 --> 01:01:17,700
want to add two more dependencies here
1595
01:01:17,700 --> 01:01:20,280
at the top the first one is the dot nth
1596
01:01:20,280 --> 01:01:22,319
dependency we just installed and we
1597
01:01:22,319 --> 01:01:24,660
should import it as early as possible
1598
01:01:24,660 --> 01:01:26,520
that's why I put it in the very first
1599
01:01:26,520 --> 01:01:28,140
line this makes sure that our
1600
01:01:28,140 --> 01:01:30,420
environment variables are available when
1601
01:01:30,420 --> 01:01:33,960
the app starts so up here we type import
1602
01:01:33,960 --> 01:01:36,240
and then in quotation marks
1603
01:01:36,240 --> 01:01:40,020
Dot N slash config which is basically
1604
01:01:40,020 --> 01:01:42,140
all that's necessary to set up and
1605
01:01:42,140 --> 01:01:45,240
configure.nf in our nth file
1606
01:01:45,240 --> 01:01:48,059
and we also want to import Mongoose to
1607
01:01:48,059 --> 01:01:49,500
use it
1608
01:01:49,500 --> 01:01:52,140
so again import but this time without
1609
01:01:52,140 --> 01:01:54,119
the quotation marks here the same way we
1610
01:01:54,119 --> 01:01:56,880
imported Express because this up here is
1611
01:01:56,880 --> 01:01:59,720
actually just a shorthand over
1612
01:01:59,720 --> 01:02:01,740
writingimport.if like we did for Express
1613
01:02:01,740 --> 01:02:03,839
and then calling config on it by
1614
01:02:03,839 --> 01:02:05,760
importing it like this we are
1615
01:02:05,760 --> 01:02:07,740
automatically call this config I found
1616
01:02:07,740 --> 01:02:09,599
it somewhere in stack Overflow so
1617
01:02:09,599 --> 01:02:11,700
Mongoose will import the following way
1618
01:02:11,700 --> 01:02:14,160
import Mongoose and it auto completes it
1619
01:02:14,160 --> 01:02:16,319
by default isn't that amazing otherwise
1620
01:02:16,319 --> 01:02:19,140
type it out as you can see it here and
1621
01:02:19,140 --> 01:02:20,880
this allows us to use the Mongoose
1622
01:02:20,880 --> 01:02:23,400
package in this file here in the server
1623
01:02:23,400 --> 01:02:26,339
TS file and now we connect Mongoose to
1624
01:02:26,339 --> 01:02:28,440
our mongodb database the following way
1625
01:02:28,440 --> 01:02:30,299
and again I have this from the
1626
01:02:30,299 --> 01:02:32,880
documentation or other tutorials but you
1627
01:02:32,880 --> 01:02:36,059
can just follow my instructions here
1628
01:02:36,059 --> 01:02:38,220
we call Mongoose
1629
01:02:38,220 --> 01:02:40,319
dot connect
1630
01:02:40,319 --> 01:02:42,000
and as you can see we have nice Auto
1631
01:02:42,000 --> 01:02:44,460
completion in vs code and here we want
1632
01:02:44,460 --> 01:02:46,559
to insert our mongodb connection string
1633
01:02:46,559 --> 01:02:49,920
which is inside our end file so we copy
1634
01:02:49,920 --> 01:02:51,839
the name of this variable
1635
01:02:51,839 --> 01:02:54,839
and then we type process.nf to access
1636
01:02:54,839 --> 01:02:56,940
our environment variables
1637
01:02:56,940 --> 01:02:59,339
and the name of this variable
1638
01:02:59,339 --> 01:03:02,220
now this shows us a warning because this
1639
01:03:02,220 --> 01:03:04,380
environment variable could be undefined
1640
01:03:04,380 --> 01:03:06,780
but connect is expecting a string that
1641
01:03:06,780 --> 01:03:08,760
actually has a value we will take care
1642
01:03:08,760 --> 01:03:10,920
of this in a moment
1643
01:03:10,920 --> 01:03:13,740
here we call Dot then because connect
1644
01:03:13,740 --> 01:03:16,200
returns a promise which is basically an
1645
01:03:16,200 --> 01:03:18,240
asynchronous operation an operation that
1646
01:03:18,240 --> 01:03:20,760
takes a moment and brief dot then we can
1647
01:03:20,760 --> 01:03:22,380
Define what we want to do after this
1648
01:03:22,380 --> 01:03:24,839
succeeded now later we will use async
1649
01:03:24,839 --> 01:03:26,940
await which is some synthetic sugar
1650
01:03:26,940 --> 01:03:29,640
around promises which makes them a bit
1651
01:03:29,640 --> 01:03:32,160
nicer to use but we can't use async
1652
01:03:32,160 --> 01:03:34,140
already on the top level in case you're
1653
01:03:34,140 --> 01:03:36,119
wondering if you don't know yet what
1654
01:03:36,119 --> 01:03:38,640
this all means just follow along so
1655
01:03:38,640 --> 01:03:40,680
inside then we have to pass a function
1656
01:03:40,680 --> 01:03:43,859
so we add another pair of parentheses
1657
01:03:43,859 --> 01:03:45,599
then the right arrow as we did before
1658
01:03:45,599 --> 01:03:47,819
and a pair of curly braces
1659
01:03:47,819 --> 01:03:49,859
and we can also put a semicolon down
1660
01:03:49,859 --> 01:03:51,960
here and here we Define what we want to
1661
01:03:51,960 --> 01:03:54,359
do after this connection is exceeded
1662
01:03:54,359 --> 01:03:55,980
let's write a log message with
1663
01:03:55,980 --> 01:03:58,140
console.log
1664
01:03:58,140 --> 01:04:02,220
which says monk was connected
1665
01:04:02,220 --> 01:04:04,619
and we also want to start our Observer
1666
01:04:04,619 --> 01:04:06,720
here after our mongodb connection was
1667
01:04:06,720 --> 01:04:08,579
successful we could keep it like this
1668
01:04:08,579 --> 01:04:11,040
following one after another but if the
1669
01:04:11,040 --> 01:04:12,660
connection failed for some reason we
1670
01:04:12,660 --> 01:04:14,160
don't want to start our server because
1671
01:04:14,160 --> 01:04:15,780
it wouldn't work properly without the
1672
01:04:15,780 --> 01:04:18,480
database right so we can actually cut
1673
01:04:18,480 --> 01:04:20,760
this out and put it in here
1674
01:04:20,760 --> 01:04:23,040
and to let the code editor format our
1675
01:04:23,040 --> 01:04:26,040
code we can press shift alt F which
1676
01:04:26,040 --> 01:04:28,799
would align it properly okay but we also
1677
01:04:28,799 --> 01:04:30,720
added our port to the environment
1678
01:04:30,720 --> 01:04:32,339
variable files so we shouldn't be
1679
01:04:32,339 --> 01:04:34,020
getting it from here we should get it
1680
01:04:34,020 --> 01:04:37,980
from the process end here as well
1681
01:04:37,980 --> 01:04:39,839
so let's create a variable for our Port
1682
01:04:39,839 --> 01:04:42,299
so we don't have to repeat process.nf
1683
01:04:42,299 --> 01:04:43,619
all the time
1684
01:04:43,619 --> 01:04:45,359
we write it as a const because this
1685
01:04:45,359 --> 01:04:48,200
value shouldn't change
1686
01:04:48,359 --> 01:04:51,960
and the port is process dot NF dot port
1687
01:04:51,960 --> 01:04:53,700
and all uppercase
1688
01:04:53,700 --> 01:04:56,160
but again as you can see the value could
1689
01:04:56,160 --> 01:04:58,260
be undefined because we never have to
1690
01:04:58,260 --> 01:05:00,059
guarantee that we actually put these
1691
01:05:00,059 --> 01:05:02,940
values into our environment variables
1692
01:05:02,940 --> 01:05:05,160
now app.listen and console.log they
1693
01:05:05,160 --> 01:05:07,140
don't show a warning because they would
1694
01:05:07,140 --> 01:05:09,540
accept an undefined Port the code will
1695
01:05:09,540 --> 01:05:11,460
still run Mongols is not so forgiving
1696
01:05:11,460 --> 01:05:13,380
they want the real value now there are
1697
01:05:13,380 --> 01:05:15,420
different ways to handle this one way is
1698
01:05:15,420 --> 01:05:18,059
to do what I already showed earlier when
1699
01:05:18,059 --> 01:05:20,700
we took a look at eslint is adding this
1700
01:05:20,700 --> 01:05:23,099
non-nine assertion operator this will be
1701
01:05:23,099 --> 01:05:24,900
tell the compiler here we know that this
1702
01:05:24,900 --> 01:05:26,760
value is not neither please just accept
1703
01:05:26,760 --> 01:05:29,099
it as you can see here eslin doesn't
1704
01:05:29,099 --> 01:05:30,720
like this because it's a bit dangerous
1705
01:05:30,720 --> 01:05:33,180
but our code would run so let's try it
1706
01:05:33,180 --> 01:05:36,359
out let's save the file
1707
01:05:36,359 --> 01:05:39,780
enter an npm start
1708
01:05:39,780 --> 01:05:41,940
and if everything is successful we
1709
01:05:41,940 --> 01:05:43,740
should see Mongoose connected and then
1710
01:05:43,740 --> 01:05:45,599
server running on the port blah blah
1711
01:05:45,599 --> 01:05:47,819
blah we should also add a catch block
1712
01:05:47,819 --> 01:05:50,700
here after then so before the semicolon
1713
01:05:50,700 --> 01:05:51,900
actually
1714
01:05:51,900 --> 01:05:54,119
we call Dot catch
1715
01:05:54,119 --> 01:05:56,099
which is basically the opposite of the
1716
01:05:56,099 --> 01:05:58,559
dot then for promises so we have then
1717
01:05:58,559 --> 01:06:00,900
for when the promise was successful and
1718
01:06:00,900 --> 01:06:02,579
we have catch for when there was an
1719
01:06:02,579 --> 01:06:04,940
error and in here we can just call the
1720
01:06:04,940 --> 01:06:06,599
console.error but without the
1721
01:06:06,599 --> 01:06:08,220
parentheses because we are not actually
1722
01:06:08,220 --> 01:06:10,140
calling this function here we are
1723
01:06:10,140 --> 01:06:12,359
referencing it so catch will call this
1724
01:06:12,359 --> 01:06:14,400
error function and pass the arguments
1725
01:06:14,400 --> 01:06:16,200
that it gets and this argument is the
1726
01:06:16,200 --> 01:06:18,540
error itself so this way it will print
1727
01:06:18,540 --> 01:06:20,220
this to the arrow console which is
1728
01:06:20,220 --> 01:06:22,380
basically the same as console.log just
1729
01:06:22,380 --> 01:06:24,839
at its red text basically and a
1730
01:06:24,839 --> 01:06:26,160
different place where we can see it
1731
01:06:26,160 --> 01:06:29,160
depending on where we deploy it now what
1732
01:06:29,160 --> 01:06:30,839
happens if we forget to Define this
1733
01:06:30,839 --> 01:06:32,640
value here let's remove it for a moment
1734
01:06:32,640 --> 01:06:34,380
save this file
1735
01:06:34,380 --> 01:06:36,420
and try to render this without the valid
1736
01:06:36,420 --> 01:06:37,920
connection string
1737
01:06:37,920 --> 01:06:40,140
as you can see we get an error here the
1738
01:06:40,140 --> 01:06:42,180
UI parameter must be a string and
1739
01:06:42,180 --> 01:06:44,400
undefined and this is good we want to
1740
01:06:44,400 --> 01:06:46,799
crash we don't want our server to start
1741
01:06:46,799 --> 01:06:48,900
with an invalid configuration so it's
1742
01:06:48,900 --> 01:06:50,579
good that this actually crashes right
1743
01:06:50,579 --> 01:06:51,839
away
1744
01:06:51,839 --> 01:06:56,099
however if we delete the port here
1745
01:06:56,099 --> 01:06:58,799
and run this again with an undefined
1746
01:06:58,799 --> 01:07:00,119
port
1747
01:07:00,119 --> 01:07:02,940
then our server actually runs Mongoose
1748
01:07:02,940 --> 01:07:04,619
connected server running on Port
1749
01:07:04,619 --> 01:07:06,780
undefined and this is a problem because
1750
01:07:06,780 --> 01:07:08,700
this will not work but our code still
1751
01:07:08,700 --> 01:07:11,160
runs and there are situations where this
1752
01:07:11,160 --> 01:07:12,960
could be even more problematic where an
1753
01:07:12,960 --> 01:07:14,819
undefined value could do some real harm
1754
01:07:14,819 --> 01:07:16,319
and we don't know where it's coming from
1755
01:07:16,319 --> 01:07:18,539
so this is why eslint complains about
1756
01:07:18,539 --> 01:07:20,400
these non-known assertion operators we
1757
01:07:20,400 --> 01:07:22,680
should avoid them one way to handle this
1758
01:07:22,680 --> 01:07:24,839
would be to just have checks for each of
1759
01:07:24,839 --> 01:07:26,039
them like
1760
01:07:26,039 --> 01:07:28,559
this port with an exclamation mark in
1761
01:07:28,559 --> 01:07:30,180
front of it which basically means it's
1762
01:07:30,180 --> 01:07:31,920
false here which is the case if it's
1763
01:07:31,920 --> 01:07:34,260
undefined then we want to throw an error
1764
01:07:34,260 --> 01:07:37,020
here to crash our server right away
1765
01:07:37,020 --> 01:07:39,240
but this is a bit verbose and we still
1766
01:07:39,240 --> 01:07:41,220
don't have a warning here because this
1767
01:07:41,220 --> 01:07:42,780
listen function doesn't complain about
1768
01:07:42,780 --> 01:07:45,059
the possibly undefined value so better
1769
01:07:45,059 --> 01:07:46,440
way to handle this is use another
1770
01:07:46,440 --> 01:07:49,380
package which is called invalid
1771
01:07:49,380 --> 01:07:51,420
it's another popular package that is
1772
01:07:51,420 --> 01:07:53,160
used to define how our environment
1773
01:07:53,160 --> 01:07:55,260
variables should look which one we have
1774
01:07:55,260 --> 01:07:58,200
in the dot end file and then it enforces
1775
01:07:58,200 --> 01:08:00,839
this schema so if one is missing it
1776
01:08:00,839 --> 01:08:02,760
actually throws an error and our server
1777
01:08:02,760 --> 01:08:04,500
won't start
1778
01:08:04,500 --> 01:08:06,359
so let's install this we have to stop
1779
01:08:06,359 --> 01:08:08,880
our server with Ctrl 0 and then the wire
1780
01:08:08,880 --> 01:08:11,180
to confirm it
1781
01:08:11,180 --> 01:08:14,280
npmi and valid
1782
01:08:14,280 --> 01:08:16,380
and it's a normal dependency because we
1783
01:08:16,380 --> 01:08:17,939
want to use this in production as well
1784
01:08:17,939 --> 01:08:20,580
and now what we do is inside our source
1785
01:08:20,580 --> 01:08:23,580
folder we create another folder
1786
01:08:23,580 --> 01:08:26,580
which we call uter short for utility
1787
01:08:26,580 --> 01:08:30,420
code and utility functions
1788
01:08:30,420 --> 01:08:33,179
and in here we create a new file
1789
01:08:33,179 --> 01:08:36,479
validate nth dot TS you can write it
1790
01:08:36,479 --> 01:08:38,399
like this in candid case but the name of
1791
01:08:38,399 --> 01:08:40,979
this file doesn't matter it's up to you
1792
01:08:40,979 --> 01:08:43,380
and in here we want to import
1793
01:08:43,380 --> 01:08:46,738
then between curly braces we write
1794
01:08:46,738 --> 01:08:49,140
clean and
1795
01:08:49,140 --> 01:08:50,640
from
1796
01:08:50,640 --> 01:08:52,979
the invalid package
1797
01:08:52,979 --> 01:08:54,960
now why do we write this in curly braces
1798
01:08:54,960 --> 01:08:57,000
here and not just the name as before
1799
01:08:57,000 --> 01:08:59,580
this depends on how this clean end is
1800
01:08:59,580 --> 01:09:01,920
exposed inside this package to explain
1801
01:09:01,920 --> 01:09:04,140
it in simple terms sometimes there is a
1802
01:09:04,140 --> 01:09:06,420
single default export which is when we
1803
01:09:06,420 --> 01:09:08,219
import it like Mongoose with this one
1804
01:09:08,219 --> 01:09:10,080
name and sometimes there are multiple
1805
01:09:10,080 --> 01:09:12,120
things that are exported from a module
1806
01:09:12,120 --> 01:09:14,279
like in this case where we can list them
1807
01:09:14,279 --> 01:09:16,738
in curly braces and if there's another
1808
01:09:16,738 --> 01:09:18,540
thing then we write the comma and Export
1809
01:09:18,540 --> 01:09:20,279
this other thing as well entry will
1810
01:09:20,279 --> 01:09:21,839
actually see an example of this right
1811
01:09:21,839 --> 01:09:23,880
now because we will do a default export
1812
01:09:23,880 --> 01:09:26,100
ourselves because from this file we want
1813
01:09:26,100 --> 01:09:28,080
to export one single thing which is
1814
01:09:28,080 --> 01:09:30,000
basically the zenitized version of our
1815
01:09:30,000 --> 01:09:32,460
environment variables so here we write
1816
01:09:32,460 --> 01:09:36,140
export default
1817
01:09:36,359 --> 01:09:38,399
we want to export the same clean end
1818
01:09:38,399 --> 01:09:40,859
that we imported here
1819
01:09:40,859 --> 01:09:43,439
but we want to call it because this is a
1820
01:09:43,439 --> 01:09:45,120
function so we don't actually export
1821
01:09:45,120 --> 01:09:47,580
this function we export its return value
1822
01:09:47,580 --> 01:09:49,620
again if this is confusing to you just
1823
01:09:49,620 --> 01:09:51,540
follow along the first argument we pass
1824
01:09:51,540 --> 01:09:54,299
here are our normal environment
1825
01:09:54,299 --> 01:09:56,940
variables process.enth
1826
01:09:56,940 --> 01:09:59,640
comma and still inside the parenthesis
1827
01:09:59,640 --> 01:10:01,980
we add a pair of curly braces
1828
01:10:01,980 --> 01:10:03,239
because then here we do the
1829
01:10:03,239 --> 01:10:04,620
configuration
1830
01:10:04,620 --> 01:10:07,620
to be more exact we want to list all the
1831
01:10:07,620 --> 01:10:10,560
environment variables that we expect
1832
01:10:10,560 --> 01:10:12,780
which is our Mongol connection string
1833
01:10:12,780 --> 01:10:14,160
and our port
1834
01:10:14,160 --> 01:10:16,320
so we paste the name Seal
1835
01:10:16,320 --> 01:10:18,500
colon and then we call this Str
1836
01:10:18,500 --> 01:10:21,600
shortfall string function
1837
01:10:21,600 --> 01:10:23,640
which will automatically import this
1838
01:10:23,640 --> 01:10:25,020
dependency here
1839
01:10:25,020 --> 01:10:27,179
and this comes from the invalid Library
1840
01:10:27,179 --> 01:10:29,460
as well this basically defines that
1841
01:10:29,460 --> 01:10:31,199
connection string should be of
1842
01:10:31,199 --> 01:10:33,980
type string later
1843
01:10:34,260 --> 01:10:36,540
we also add the port after a comma here
1844
01:10:36,540 --> 01:10:39,239
because this is a Json object
1845
01:10:39,239 --> 01:10:42,420
and this time we call the port
1846
01:10:42,420 --> 01:10:44,760
we could also use number or string but
1847
01:10:44,760 --> 01:10:47,040
Port has a special type I think it makes
1848
01:10:47,040 --> 01:10:49,560
sure that this has a certain length or
1849
01:10:49,560 --> 01:10:52,980
shape and now since we export this clean
1850
01:10:52,980 --> 01:10:55,620
nth curl here we can import it in
1851
01:10:55,620 --> 01:10:57,840
another file and this will give us our
1852
01:10:57,840 --> 01:11:00,360
cleaned up and sanitized environment
1853
01:11:00,360 --> 01:11:02,940
variables so let's save this file go
1854
01:11:02,940 --> 01:11:04,620
back to server TS
1855
01:11:04,620 --> 01:11:08,780
and we want to import I put it up here
1856
01:11:09,960 --> 01:11:12,060
and from
1857
01:11:12,060 --> 01:11:14,460
from our validate and file here and now
1858
01:11:14,460 --> 01:11:16,080
we have to pass the location of this
1859
01:11:16,080 --> 01:11:17,880
file relative to the file we are
1860
01:11:17,880 --> 01:11:20,159
currently in so we want to go into the
1861
01:11:20,159 --> 01:11:22,080
parent folder which is Source then
1862
01:11:22,080 --> 01:11:24,179
inside util invalidate INF
1863
01:11:24,179 --> 01:11:25,800
so we write the dot to go into the
1864
01:11:25,800 --> 01:11:27,960
parent folder and auto completion
1865
01:11:27,960 --> 01:11:31,500
already suggests uter validate INF this
1866
01:11:31,500 --> 01:11:33,600
is where we want to import it but this
1867
01:11:33,600 --> 01:11:35,219
depends always on your file structure
1868
01:11:35,219 --> 01:11:37,020
but other computation will help you with
1869
01:11:37,020 --> 01:11:38,880
finding the correct one
1870
01:11:38,880 --> 01:11:41,219
now since this is the default export
1871
01:11:41,219 --> 01:11:43,500
here we can give this import any name we
1872
01:11:43,500 --> 01:11:45,179
want and it will recognize it because
1873
01:11:45,179 --> 01:11:48,060
there's only this one export we could
1874
01:11:48,060 --> 01:11:49,739
also called as banana or chocolate
1875
01:11:49,739 --> 01:11:51,540
doesn't matter but NF makes sense
1876
01:11:51,540 --> 01:11:53,280
because it mirrors the name of the
1877
01:11:53,280 --> 01:11:55,980
normal environment variable here
1878
01:11:55,980 --> 01:11:58,040
and now it's pretty simple instead of
1879
01:11:58,040 --> 01:12:00,540
process.enf we just want to call NF
1880
01:12:00,540 --> 01:12:04,080
because this now relates to our clean
1881
01:12:04,080 --> 01:12:06,300
end setup we did here
1882
01:12:06,300 --> 01:12:09,360
now the port is definitely a number it's
1883
01:12:09,360 --> 01:12:11,580
not possibly undefined anymore
1884
01:12:11,580 --> 01:12:13,500
because this is what cleanenf takes care
1885
01:12:13,500 --> 01:12:16,140
of I can also now remove this
1886
01:12:16,140 --> 01:12:19,140
exclamation mark and the IDE will stop
1887
01:12:19,140 --> 01:12:20,699
complaining because this is now
1888
01:12:20,699 --> 01:12:22,620
guaranteed to be a string and now what
1889
01:12:22,620 --> 01:12:24,300
happens if we actually forget one of
1890
01:12:24,300 --> 01:12:26,340
these values let's try deleting the port
1891
01:12:26,340 --> 01:12:28,199
again as we did before
1892
01:12:28,199 --> 01:12:31,500
and restart our server
1893
01:12:31,500 --> 01:12:34,080
and now it crashes right away with a
1894
01:12:34,080 --> 01:12:35,520
nice error message here missing
1895
01:12:35,520 --> 01:12:37,440
environment variable support undefined
1896
01:12:37,440 --> 01:12:39,300
and this is exactly what we want because
1897
01:12:39,300 --> 01:12:41,040
now we make sure that our server doesn't
1898
01:12:41,040 --> 01:12:43,380
run with a wrong configuration instead
1899
01:12:43,380 --> 01:12:45,480
it crashes right away so it doesn't even
1900
01:12:45,480 --> 01:12:46,380
start
1901
01:12:46,380 --> 01:12:48,780
this is better than having it used by
1902
01:12:48,780 --> 01:12:50,820
someone already in production and then
1903
01:12:50,820 --> 01:12:52,620
it just misbehaves
1904
01:12:52,620 --> 01:12:54,420
okay so this is why this is a nice
1905
01:12:54,420 --> 01:12:57,000
package let's restart our server with
1906
01:12:57,000 --> 01:13:00,000
the port in here
1907
01:13:00,000 --> 01:13:02,940
and everything should run
1908
01:13:02,940 --> 01:13:05,100
Mongoose connectors are running on Port
1909
01:13:05,100 --> 01:13:08,219
5000 by the way this warning here is
1910
01:13:08,219 --> 01:13:10,140
just a change that's coming up in the
1911
01:13:10,140 --> 01:13:12,179
next major Mongoose version you don't
1912
01:13:12,179 --> 01:13:13,800
have to care about this it doesn't play
1913
01:13:13,800 --> 01:13:15,659
any role for our project we are building
1914
01:13:15,659 --> 01:13:18,060
here let's actually try if we can still
1915
01:13:18,060 --> 01:13:20,460
access our endpoint here at localhost
1916
01:13:20,460 --> 01:13:23,159
5000 we can still see hello world if the
1917
01:13:23,159 --> 01:13:24,900
server is running
1918
01:13:24,900 --> 01:13:27,060
however our code organization is not
1919
01:13:27,060 --> 01:13:29,159
really nice here we should separate the
1920
01:13:29,159 --> 01:13:31,560
parts that contains all the endpoints
1921
01:13:31,560 --> 01:13:34,199
and the part that sets up Mongoose and
1922
01:13:34,199 --> 01:13:36,659
actually starts the silver one reason
1923
01:13:36,659 --> 01:13:38,940
for that is just good old separation of
1924
01:13:38,940 --> 01:13:41,219
concerns but the other reason is that
1925
01:13:41,219 --> 01:13:44,460
this allows us to test our server code
1926
01:13:44,460 --> 01:13:46,739
everything that's contained in this
1927
01:13:46,739 --> 01:13:49,140
Express app basically without connecting
1928
01:13:49,140 --> 01:13:51,480
to the real production database we won't
1929
01:13:51,480 --> 01:13:52,980
be writing tests in this tutorial
1930
01:13:52,980 --> 01:13:55,020
because this is boring as hell but when
1931
01:13:55,020 --> 01:13:56,580
you write tests in production you
1932
01:13:56,580 --> 01:13:58,800
usually insert some dummy data into the
1933
01:13:58,800 --> 01:14:00,780
database and then you will just wipe the
1934
01:14:00,780 --> 01:14:02,159
whole database and of course you
1935
01:14:02,159 --> 01:14:03,600
shouldn't do this in production unless
1936
01:14:03,600 --> 01:14:05,280
you want to get fired this is why it's
1937
01:14:05,280 --> 01:14:07,679
necessary to separate this code so what
1938
01:14:07,679 --> 01:14:09,360
we do is at the same place where our
1939
01:14:09,360 --> 01:14:11,219
Observer TS is we want to create another
1940
01:14:11,219 --> 01:14:14,100
file so inside the source folder here
1941
01:14:14,100 --> 01:14:17,340
we create an app.ts file
1942
01:14:17,340 --> 01:14:19,440
and then we want to move some code from
1943
01:14:19,440 --> 01:14:21,540
our server TS file over into this new
1944
01:14:21,540 --> 01:14:25,260
appts file so we want to move the first
1945
01:14:25,260 --> 01:14:27,420
line here Dot and slash config over
1946
01:14:27,420 --> 01:14:28,620
there
1947
01:14:28,620 --> 01:14:30,719
so we load the environment variables
1948
01:14:30,719 --> 01:14:32,820
also if we start this app without
1949
01:14:32,820 --> 01:14:35,820
starting the server next we want to move
1950
01:14:35,820 --> 01:14:38,040
these two lines over there the express
1951
01:14:38,040 --> 01:14:41,760
import and the express call here
1952
01:14:41,760 --> 01:14:44,100
which creates our server app
1953
01:14:44,100 --> 01:14:46,080
let's actually put this into a new line
1954
01:14:46,080 --> 01:14:48,300
because this is not an import it's a
1955
01:14:48,300 --> 01:14:49,739
function call
1956
01:14:49,739 --> 01:14:51,719
and then we want to move this endpoint
1957
01:14:51,719 --> 01:14:54,480
over there so we cut this out and paste
1958
01:14:54,480 --> 01:14:56,520
it here as well because this is a zero
1959
01:14:56,520 --> 01:14:59,880
endpoint this belongs to our server code
1960
01:14:59,880 --> 01:15:02,520
and then in order to use this app in our
1961
01:15:02,520 --> 01:15:05,280
server to call it and run it we have to
1962
01:15:05,280 --> 01:15:07,260
export it here and again this is a
1963
01:15:07,260 --> 01:15:09,540
default export which basically means
1964
01:15:09,540 --> 01:15:11,400
that we just export one single thing
1965
01:15:11,400 --> 01:15:12,840
from this file
1966
01:15:12,840 --> 01:15:15,120
which is our Express app where we add
1967
01:15:15,120 --> 01:15:17,159
all the endpoints on if you want to
1968
01:15:17,159 --> 01:15:19,080
export this here
1969
01:15:19,080 --> 01:15:21,540
and now we want to import this in our
1970
01:15:21,540 --> 01:15:23,280
server TS file
1971
01:15:23,280 --> 01:15:25,260
so we do this at the very top of our
1972
01:15:25,260 --> 01:15:27,060
Imports
1973
01:15:27,060 --> 01:15:29,880
import app again we could give this any
1974
01:15:29,880 --> 01:15:31,560
name we want because it's a default
1975
01:15:31,560 --> 01:15:35,280
export but app makes sense
1976
01:15:35,280 --> 01:15:38,040
and it's inside the source
1977
01:15:38,040 --> 01:15:40,620
folder as the app file and whenever we
1978
01:15:40,620 --> 01:15:42,719
import a file we don't have to import
1979
01:15:42,719 --> 01:15:44,460
the file extension as you might have
1980
01:15:44,460 --> 01:15:45,840
already noticed
1981
01:15:45,840 --> 01:15:48,000
and you have the rest basically stays
1982
01:15:48,000 --> 01:15:50,699
the same its format this properly we
1983
01:15:50,699 --> 01:15:53,159
still call app.listen now we call it on
1984
01:15:53,159 --> 01:15:55,620
its imported app to spin up our server
1985
01:15:55,620 --> 01:15:58,860
let's save the server TS file we don't
1986
01:15:58,860 --> 01:16:01,500
have to export anything here and let's
1987
01:16:01,500 --> 01:16:03,300
see if it still works
1988
01:16:03,300 --> 01:16:06,239
so we can still access our endpoint if
1989
01:16:06,239 --> 01:16:09,620
the server is running nice
1990
01:16:13,620 --> 01:16:16,620
okay our mongodb is connected and now we
1991
01:16:16,620 --> 01:16:18,300
actually want to put some stuff in there
1992
01:16:18,300 --> 01:16:20,820
right and when we use Mongols we have to
1993
01:16:20,820 --> 01:16:23,100
define a model for the data that we want
1994
01:16:23,100 --> 01:16:25,440
to put in our database so what we do is
1995
01:16:25,440 --> 01:16:28,080
inside our source folder here
1996
01:16:28,080 --> 01:16:31,320
we create a folder called model
1997
01:16:31,320 --> 01:16:33,900
or models actually
1998
01:16:33,900 --> 01:16:37,020
because it will be multiple ones
1999
01:16:37,020 --> 01:16:41,040
and in here we create a node.ts file and
2000
01:16:41,040 --> 01:16:42,780
as you can tell this will contain our
2001
01:16:42,780 --> 01:16:43,980
node model
2002
01:16:43,980 --> 01:16:46,080
let me close this terminal don't need it
2003
01:16:46,080 --> 01:16:48,300
right now of course this whole process
2004
01:16:48,300 --> 01:16:50,580
of setting up models and schemas and
2005
01:16:50,580 --> 01:16:52,140
everything is explained in this
2006
01:16:52,140 --> 01:16:54,780
humongous documentation
2007
01:16:54,780 --> 01:16:57,659
if you have some weeks to spare and want
2008
01:16:57,659 --> 01:16:59,520
to learn is in more detail but I will
2009
01:16:59,520 --> 01:17:00,780
show you everything you need to know
2010
01:17:00,780 --> 01:17:03,780
right now so we need to define a schema
2011
01:17:03,780 --> 01:17:06,000
and the schema defines what kind of data
2012
01:17:06,000 --> 01:17:08,400
our node should contain
2013
01:17:08,400 --> 01:17:13,800
so we write const node schema equals
2014
01:17:13,800 --> 01:17:17,699
new schema which is this mongoose
2015
01:17:17,699 --> 01:17:20,100
I guess Constructor or function here so
2016
01:17:20,100 --> 01:17:22,080
it should import this party at the top
2017
01:17:22,080 --> 01:17:24,179
and between these parentheses we add a
2018
01:17:24,179 --> 01:17:26,040
pair of curly braces
2019
01:17:26,040 --> 01:17:27,239
because this is where we put the
2020
01:17:27,239 --> 01:17:29,340
configuration for the schema
2021
01:17:29,340 --> 01:17:31,679
okay what data should our nodes contain
2022
01:17:31,679 --> 01:17:33,900
each of them should have a title and a
2023
01:17:33,900 --> 01:17:36,540
text and also this timestamp that
2024
01:17:36,540 --> 01:17:38,520
indicates when the note was created or
2025
01:17:38,520 --> 01:17:39,840
last updated
2026
01:17:39,840 --> 01:17:42,420
so we put these fields in here
2027
01:17:42,420 --> 01:17:44,159
title column
2028
01:17:44,159 --> 01:17:46,980
then another pair of curly braces and
2029
01:17:46,980 --> 01:17:48,480
you just have to get used to it when
2030
01:17:48,480 --> 01:17:50,880
using JavaScript there are curly braces
2031
01:17:50,880 --> 01:17:53,100
everywhere it's just curly braces inside
2032
01:17:53,100 --> 01:17:55,380
curly braces inside curly braces but
2033
01:17:55,380 --> 01:17:57,840
this is how JS works
2034
01:17:57,840 --> 01:18:00,060
here we have to define the type so this
2035
01:18:00,060 --> 01:18:02,040
is basically a configuration for this
2036
01:18:02,040 --> 01:18:04,560
title field in our node schema
2037
01:18:04,560 --> 01:18:06,480
the type will be string and you have to
2038
01:18:06,480 --> 01:18:08,760
write this in uppercase not lowercase
2039
01:18:08,760 --> 01:18:11,040
and we have to write it in uppercase for
2040
01:18:11,040 --> 01:18:13,620
all data types in these mongodb schemas
2041
01:18:13,620 --> 01:18:15,600
even though a normal code revitamin
2042
01:18:15,600 --> 01:18:17,340
lower case this is because those are
2043
01:18:17,340 --> 01:18:20,040
actually the constructors of these types
2044
01:18:20,040 --> 01:18:21,780
I don't know why exactly but doesn't
2045
01:18:21,780 --> 01:18:24,679
matter after type string red comma
2046
01:18:24,679 --> 01:18:29,400
we quiet I can't write required call on
2047
01:18:29,400 --> 01:18:30,659
true
2048
01:18:30,659 --> 01:18:32,460
as the name implies this is another
2049
01:18:32,460 --> 01:18:34,620
configuration that defines that each
2050
01:18:34,620 --> 01:18:36,540
node has to have a title otherwise
2051
01:18:36,540 --> 01:18:39,060
mongodb will not accept it
2052
01:18:39,060 --> 01:18:43,760
comma and the next property text
2053
01:18:43,920 --> 01:18:46,380
which is another string
2054
01:18:46,380 --> 01:18:48,120
but this time it doesn't have to be
2055
01:18:48,120 --> 01:18:49,860
required because we want to make the
2056
01:18:49,860 --> 01:18:51,300
text optional
2057
01:18:51,300 --> 01:18:53,580
and in JavaScript objects you can add
2058
01:18:53,580 --> 01:18:56,219
trailing commas even if we don't add
2059
01:18:56,219 --> 01:18:58,199
another field which is usually a good
2060
01:18:58,199 --> 01:19:00,239
habit because it has some benefits When
2061
01:19:00,239 --> 01:19:02,400
We compare changes in Version Control
2062
01:19:02,400 --> 01:19:04,440
doesn't matter you can edit your can
2063
01:19:04,440 --> 01:19:07,140
leave it out but I like to edit but the
2064
01:19:07,140 --> 01:19:09,000
timestamps are still missing right so
2065
01:19:09,000 --> 01:19:10,980
now we could add another field created
2066
01:19:10,980 --> 01:19:14,640
it make it of type timestamp but
2067
01:19:14,640 --> 01:19:16,380
Mongoose can actually add these fields
2068
01:19:16,380 --> 01:19:17,820
for us and also maintain them
2069
01:19:17,820 --> 01:19:20,520
automatically so we don't add them
2070
01:19:20,520 --> 01:19:22,679
inside these curly braces here inside
2071
01:19:22,679 --> 01:19:25,320
the schema configuration instead we go
2072
01:19:25,320 --> 01:19:27,960
outside of this closing curly brace but
2073
01:19:27,960 --> 01:19:30,659
still inside the parentheses add another
2074
01:19:30,659 --> 01:19:33,480
pair of curly braces and write time
2075
01:19:33,480 --> 01:19:36,000
stamps and we already get a suggestion
2076
01:19:36,000 --> 01:19:38,340
column drawer this will automatically
2077
01:19:38,340 --> 01:19:40,260
add the timestamps fields for created
2078
01:19:40,260 --> 01:19:42,719
and updated to the schema rather than us
2079
01:19:42,719 --> 01:19:44,699
having to manage them ourselves but
2080
01:19:44,699 --> 01:19:46,560
again you have to put that in the
2081
01:19:46,560 --> 01:19:48,780
correct place outside of these first
2082
01:19:48,780 --> 01:19:50,460
curly braces here
2083
01:19:50,460 --> 01:19:52,260
now for normal JavaScript this is
2084
01:19:52,260 --> 01:19:54,179
everything that's necessary to set up
2085
01:19:54,179 --> 01:19:57,179
the schema but in typescript we have to
2086
01:19:57,179 --> 01:19:59,280
add another step because we later want
2087
01:19:59,280 --> 01:20:01,620
to have a type for this node in our code
2088
01:20:01,620 --> 01:20:04,440
available so that we get type safety and
2089
01:20:04,440 --> 01:20:06,239
other completion for all the fields in
2090
01:20:06,239 --> 01:20:08,460
here title and text and everything so
2091
01:20:08,460 --> 01:20:11,400
below we write type which is basically a
2092
01:20:11,400 --> 01:20:13,320
way to add another type alias in
2093
01:20:13,320 --> 01:20:15,900
typescript so to create another type so
2094
01:20:15,900 --> 01:20:16,980
to speak
2095
01:20:16,980 --> 01:20:18,900
and the name of this type should be node
2096
01:20:18,900 --> 01:20:20,580
but again you can name this anything you
2097
01:20:20,580 --> 01:20:22,080
want it makes sense because those are
2098
01:20:22,080 --> 01:20:25,320
notes and then we write equal sign and
2099
01:20:25,320 --> 01:20:27,719
first schema type which is this name
2100
01:20:27,719 --> 01:20:29,699
here this type we get from Mongoose
2101
01:20:29,699 --> 01:20:31,440
again so it should add another import
2102
01:20:31,440 --> 01:20:34,260
then we make anger brackets to add a
2103
01:20:34,260 --> 01:20:36,540
type argument to this infill schema type
2104
01:20:36,540 --> 01:20:37,920
type
2105
01:20:37,920 --> 01:20:40,500
and in here we write type off as one
2106
01:20:40,500 --> 01:20:42,960
word or lowercase that's important note
2107
01:20:42,960 --> 01:20:44,820
schema
2108
01:20:44,820 --> 01:20:47,040
in other words this creates this node
2109
01:20:47,040 --> 01:20:49,440
type by looking at our node schema here
2110
01:20:49,440 --> 01:20:51,960
and then we are creating a drumet this
2111
01:20:51,960 --> 01:20:53,820
is something Mongoose does automatically
2112
01:20:53,820 --> 01:20:55,440
for us
2113
01:20:55,440 --> 01:20:57,719
and now we just have to export this
2114
01:20:57,719 --> 01:21:00,000
whole schema so we can use it in our
2115
01:21:00,000 --> 01:21:01,440
remaining code
2116
01:21:01,440 --> 01:21:04,920
so again export default
2117
01:21:04,920 --> 01:21:06,719
then we have to call model which is
2118
01:21:06,719 --> 01:21:08,820
another Mongoose function so it adds
2119
01:21:08,820 --> 01:21:10,800
another import
2120
01:21:10,800 --> 01:21:13,020
we add another type argument so it
2121
01:21:13,020 --> 01:21:16,980
creates a model of type node parentheses
2122
01:21:16,980 --> 01:21:19,800
then a string which is the name of this
2123
01:21:19,800 --> 01:21:21,659
collection
2124
01:21:21,659 --> 01:21:24,480
and again we write note in uppercase
2125
01:21:24,480 --> 01:21:26,820
this will later create a collection in
2126
01:21:26,820 --> 01:21:29,040
our mongodb database but it actually
2127
01:21:29,040 --> 01:21:31,620
turns this into a plural so by calling
2128
01:21:31,620 --> 01:21:33,480
this node it will later create a
2129
01:21:33,480 --> 01:21:35,400
collection with the name nodes you will
2130
01:21:35,400 --> 01:21:37,380
see this in a moment just follow along
2131
01:21:37,380 --> 01:21:39,540
combine the second argument as the node
2132
01:21:39,540 --> 01:21:40,980
schema
2133
01:21:40,980 --> 01:21:43,140
save this
2134
01:21:43,140 --> 01:21:45,420
so to recap this code here creates a
2135
01:21:45,420 --> 01:21:47,640
schema for our notes which each contain
2136
01:21:47,640 --> 01:21:50,159
a title and a text and the timestamps
2137
01:21:50,159 --> 01:21:52,020
and then recreate the type for
2138
01:21:52,020 --> 01:21:54,540
typescript and Export this model so we
2139
01:21:54,540 --> 01:21:56,820
can use it in our code and now let's go
2140
01:21:56,820 --> 01:21:59,699
into our FTS file which is our server
2141
01:21:59,699 --> 01:22:02,400
code and import this new node model we
2142
01:22:02,400 --> 01:22:04,380
just created
2143
01:22:04,380 --> 01:22:07,500
so import node model
2144
01:22:07,500 --> 01:22:09,600
again we can give it any name since it's
2145
01:22:09,600 --> 01:22:11,880
a default export
2146
01:22:11,880 --> 01:22:14,340
from
2147
01:22:14,340 --> 01:22:19,440
period slash models slash node
2148
01:22:19,440 --> 01:22:21,840
and now let's create an endpoint that
2149
01:22:21,840 --> 01:22:24,000
Returns the nodes that we put into our
2150
01:22:24,000 --> 01:22:26,580
database just so we can see how we can
2151
01:22:26,580 --> 01:22:28,739
interact with the database and let's
2152
01:22:28,739 --> 01:22:30,840
reuse our existing endpoint here because
2153
01:22:30,840 --> 01:22:32,640
we don't need this hello world in the
2154
01:22:32,640 --> 01:22:33,960
future
2155
01:22:33,960 --> 01:22:37,199
so instead of rest.send hello world we
2156
01:22:37,199 --> 01:22:38,940
want to get the notes out of our
2157
01:22:38,940 --> 01:22:41,400
database and then return them so we
2158
01:22:41,400 --> 01:22:44,640
write cons notes equals
2159
01:22:44,640 --> 01:22:47,219
then await
2160
01:22:47,219 --> 01:22:51,000
note model dot find we call this without
2161
01:22:51,000 --> 01:22:52,860
any arguments
2162
01:22:52,860 --> 01:22:56,100
and carl.exec behind it
2163
01:22:56,100 --> 01:22:58,140
okay so that's a lot of code what is
2164
01:22:58,140 --> 01:23:01,219
going on here so first of all node
2165
01:23:01,219 --> 01:23:03,360
model.finds.exec executes this find
2166
01:23:03,360 --> 01:23:05,699
operation and then exact returns a
2167
01:23:05,699 --> 01:23:07,860
promise we already worked with promises
2168
01:23:07,860 --> 01:23:10,679
earlier here but here we use this then
2169
01:23:10,679 --> 01:23:13,020
and catch combination as I already
2170
01:23:13,020 --> 01:23:15,600
explained a weight is syntactic sugar
2171
01:23:15,600 --> 01:23:18,420
around promises so instead of having to
2172
01:23:18,420 --> 01:23:21,420
use these then and catch carbacks which
2173
01:23:21,420 --> 01:23:24,179
adds this ugly nesting we can just write
2174
01:23:24,179 --> 01:23:26,219
it in one line below each other like
2175
01:23:26,219 --> 01:23:29,340
normal synchronous code and again I
2176
01:23:29,340 --> 01:23:30,840
didn't use this here because we can't
2177
01:23:30,840 --> 01:23:34,080
use this at the top level in express by
2178
01:23:34,080 --> 01:23:36,659
default but we can use it inside this
2179
01:23:36,659 --> 01:23:39,120
endpoint functions here but for this we
2180
01:23:39,120 --> 01:23:41,400
also have to add the async keyword in
2181
01:23:41,400 --> 01:23:43,860
front of this error function so the code
2182
01:23:43,860 --> 01:23:45,719
knows that this is an asynchronous
2183
01:23:45,719 --> 01:23:47,340
function and now this error here
2184
01:23:47,340 --> 01:23:50,280
disappeared so in other words this will
2185
01:23:50,280 --> 01:23:53,159
execute this find operation which is an
2186
01:23:53,159 --> 01:23:55,440
asynchronous operation meaning that it
2187
01:23:55,440 --> 01:23:57,540
takes a moment because we actually have
2188
01:23:57,540 --> 01:24:00,060
to go into our database look it up and
2189
01:24:00,060 --> 01:24:01,860
then return it and this can take a few
2190
01:24:01,860 --> 01:24:04,500
milliseconds or maybe even a second this
2191
01:24:04,500 --> 01:24:05,880
is why this whole process is
2192
01:24:05,880 --> 01:24:08,580
asynchronous we don't want to have the
2193
01:24:08,580 --> 01:24:10,320
rest of the server to wait just because
2194
01:24:10,320 --> 01:24:12,480
we do this database operation this is
2195
01:24:12,480 --> 01:24:15,060
where we use promises and isn't a way to
2196
01:24:15,060 --> 01:24:17,219
make it a bit simpler again you don't
2197
01:24:17,219 --> 01:24:18,840
need to understand this in detail you
2198
01:24:18,840 --> 01:24:20,940
can just follow along and it's important
2199
01:24:20,940 --> 01:24:22,739
that you don't forget this await
2200
01:24:22,739 --> 01:24:25,440
otherwise we will not get the value of
2201
01:24:25,440 --> 01:24:27,659
our nodes we will get this promise which
2202
01:24:27,659 --> 01:24:30,060
can cause weird errors sometimes if
2203
01:24:30,060 --> 01:24:31,800
you're not aware of this so yeah make
2204
01:24:31,800 --> 01:24:33,600
sure to add this away here otherwise it
2205
01:24:33,600 --> 01:24:35,940
will not work but we don't only want to
2206
01:24:35,940 --> 01:24:37,739
get them from the database we also want
2207
01:24:37,739 --> 01:24:40,980
to return them to the user So Below
2208
01:24:40,980 --> 01:24:42,900
we write something similar than before
2209
01:24:42,900 --> 01:24:45,900
we take our response object but instead
2210
01:24:45,900 --> 01:24:47,040
of send
2211
01:24:47,040 --> 01:24:49,199
we call Dot status
2212
01:24:49,199 --> 01:24:51,420
and pass 200 here
2213
01:24:51,420 --> 01:24:54,480
which sets the response status to a 200
2214
01:24:54,480 --> 01:24:57,659
which is the HTTP status for okay so
2215
01:24:57,659 --> 01:24:59,880
everything went fine basically and when
2216
01:24:59,880 --> 01:25:02,040
we work with our front and later we use
2217
01:25:02,040 --> 01:25:04,739
different HTTP calls to indicate if
2218
01:25:04,739 --> 01:25:06,780
there was an error or not and we don't
2219
01:25:06,780 --> 01:25:09,120
send a response just in form of a text
2220
01:25:09,120 --> 01:25:12,120
we send it in form of a Json which is
2221
01:25:12,120 --> 01:25:14,520
basically a way to transfer JavaScript
2222
01:25:14,520 --> 01:25:16,920
objects between the backend and the
2223
01:25:16,920 --> 01:25:18,780
client you have probably already worked
2224
01:25:18,780 --> 01:25:21,239
with Json before and in here we simply
2225
01:25:21,239 --> 01:25:23,100
pass our nodes
2226
01:25:23,100 --> 01:25:25,560
this will turn our notes into a Json
2227
01:25:25,560 --> 01:25:28,080
that resents to the front end and now we
2228
01:25:28,080 --> 01:25:29,699
can actually try it out
2229
01:25:29,699 --> 01:25:31,679
we can access our
2230
01:25:31,679 --> 01:25:34,500
localhost again
2231
01:25:34,500 --> 01:25:37,199
we get a successful response and we see
2232
01:25:37,199 --> 01:25:39,360
this empty pair of square brackets which
2233
01:25:39,360 --> 01:25:42,360
means that this is an empty array which
2234
01:25:42,360 --> 01:25:44,040
makes sense because we haven't put any
2235
01:25:44,040 --> 01:25:46,860
nodes into our database yet
2236
01:25:46,860 --> 01:25:50,100
so let's go into mongodb Atlas again
2237
01:25:50,100 --> 01:25:53,219
and know something magical happened when
2238
01:25:53,219 --> 01:25:55,620
we click on browse collections there
2239
01:25:55,620 --> 01:25:59,159
certainly is a collection oh wow we just
2240
01:25:59,159 --> 01:26:01,739
cut notes why is it called notes again
2241
01:26:01,739 --> 01:26:04,800
because we cut our node schema node
2242
01:26:04,800 --> 01:26:08,340
and mongodb or mongoose automatically
2243
01:26:08,340 --> 01:26:10,620
creates this collection from it and it
2244
01:26:10,620 --> 01:26:12,659
also automatically pluralizes the name
2245
01:26:12,659 --> 01:26:15,540
so note turns into notes because the
2246
01:26:15,540 --> 01:26:17,340
collection contains multiple documents
2247
01:26:17,340 --> 01:26:19,920
now the database here is called test
2248
01:26:19,920 --> 01:26:22,020
because this is what we defined in our
2249
01:26:22,020 --> 01:26:24,060
connection string let's actually remove
2250
01:26:24,060 --> 01:26:26,639
this and give this a proper name so we
2251
01:26:26,639 --> 01:26:28,679
drop this
2252
01:26:28,679 --> 01:26:31,679
go back into our code into our
2253
01:26:31,679 --> 01:26:34,760
connection string and we go here behind
2254
01:26:34,760 --> 01:26:37,620
mongodb.net but before the question mark
2255
01:26:37,620 --> 01:26:40,260
retry writes and here we can set the
2256
01:26:40,260 --> 01:26:42,239
name for our database
2257
01:26:42,239 --> 01:26:45,540
let's call it cool underscore nodes
2258
01:26:45,540 --> 01:26:48,120
underscore app
2259
01:26:48,120 --> 01:26:49,679
save this
2260
01:26:49,679 --> 01:26:51,480
and I think when we change the
2261
01:26:51,480 --> 01:26:53,040
environment variables we actually have
2262
01:26:53,040 --> 01:26:55,679
to restart the whole thing manually
2263
01:26:55,679 --> 01:26:57,600
I'm not sure right now but just to make
2264
01:26:57,600 --> 01:27:00,540
sure stop the server and run MPN start
2265
01:27:00,540 --> 01:27:01,620
again
2266
01:27:01,620 --> 01:27:03,840
let's access our endpoint again which
2267
01:27:03,840 --> 01:27:05,580
would trigger the creation of the
2268
01:27:05,580 --> 01:27:07,620
collection
2269
01:27:07,620 --> 01:27:09,360
and then take another look in here
2270
01:27:09,360 --> 01:27:11,219
refresh this
2271
01:27:11,219 --> 01:27:14,100
and that is now our database here is
2272
01:27:14,100 --> 01:27:16,199
called quillnotes app and we still have
2273
01:27:16,199 --> 01:27:19,199
our nodes collection which is empty now
2274
01:27:19,199 --> 01:27:20,880
we don't have an endpoint yet where we
2275
01:27:20,880 --> 01:27:22,679
can create nodes but we can create them
2276
01:27:22,679 --> 01:27:25,620
here manually so let's do that in the
2277
01:27:25,620 --> 01:27:28,020
back end here of Atlas it automatically
2278
01:27:28,020 --> 01:27:30,360
adds this idea because each node needs
2279
01:27:30,360 --> 01:27:31,679
an idea
2280
01:27:31,679 --> 01:27:34,199
and now we want to add the title
2281
01:27:34,199 --> 01:27:36,900
let's call it my first note
2282
01:27:36,900 --> 01:27:39,120
and the text we click this plus here at
2283
01:27:39,120 --> 01:27:40,139
another field
2284
01:27:40,139 --> 01:27:43,739
color text and rewrite subscribe to
2285
01:27:43,739 --> 01:27:47,520
coding in flow and then we go ahead and
2286
01:27:47,520 --> 01:27:49,080
click the Subscribe button under this
2287
01:27:49,080 --> 01:27:50,340
video
2288
01:27:50,340 --> 01:27:52,800
after you have done this we click insert
2289
01:27:52,800 --> 01:27:55,560
to add our first node here
2290
01:27:55,560 --> 01:27:57,960
and now when we access our localhost
2291
01:27:57,960 --> 01:27:59,460
endpoint again
2292
01:27:59,460 --> 01:28:02,159
we return our array with our one node in
2293
01:28:02,159 --> 01:28:04,320
it nice and this is what we will later
2294
01:28:04,320 --> 01:28:07,020
display in our front end
2295
01:28:07,020 --> 01:28:10,320
amazing but before we go ahead and add
2296
01:28:10,320 --> 01:28:12,179
more endpoints we should have a clue of
2297
01:28:12,179 --> 01:28:15,659
how error handling Works in Express
2298
01:28:15,659 --> 01:28:18,659
so let's just pretend something goes
2299
01:28:18,659 --> 01:28:20,520
wrong in here which of course can always
2300
01:28:20,520 --> 01:28:22,679
be the case maybe the database is down
2301
01:28:22,679 --> 01:28:26,159
maybe we wrote some bad code whatever
2302
01:28:26,159 --> 01:28:29,100
so to simulate this we're gonna throw
2303
01:28:29,100 --> 01:28:32,460
and around here like this it's the same
2304
01:28:32,460 --> 01:28:35,159
in Java and kotlin and so on
2305
01:28:35,159 --> 01:28:37,920
with an arbitrary message
2306
01:28:37,920 --> 01:28:40,260
we saved us and then we try to access
2307
01:28:40,260 --> 01:28:43,679
our endpoint again which now shows us an
2308
01:28:43,679 --> 01:28:45,480
error message the website is not
2309
01:28:45,480 --> 01:28:47,940
available in beautiful German the
2310
01:28:47,940 --> 01:28:49,100
website is
2311
01:28:49,100 --> 01:28:51,360
and even worse
2312
01:28:51,360 --> 01:28:53,460
this crashed our whole server when we
2313
01:28:53,460 --> 01:28:55,380
look into the console here's our error
2314
01:28:55,380 --> 01:28:58,320
and the app crashed and now our whole
2315
01:28:58,320 --> 01:29:01,320
server is done we can't access any other
2316
01:29:01,320 --> 01:29:04,440
endpoints anymore we get fired our life
2317
01:29:04,440 --> 01:29:06,960
is over our wife leaves us all because
2318
01:29:06,960 --> 01:29:09,179
we didn't handle errors properly so
2319
01:29:09,179 --> 01:29:11,760
let's learn how to do this so we want to
2320
01:29:11,760 --> 01:29:13,500
wrap this whole content here of this
2321
01:29:13,500 --> 01:29:15,840
endpoint into a try catch block
2322
01:29:15,840 --> 01:29:18,120
which looks like this dry curly braces
2323
01:29:18,120 --> 01:29:20,880
catch Arrow curly braces and our USB s
2324
01:29:20,880 --> 01:29:23,699
chords are the completion
2325
01:29:23,699 --> 01:29:25,860
while the normal chord goes into the dry
2326
01:29:25,860 --> 01:29:28,260
block and in catch we put whatever
2327
01:29:28,260 --> 01:29:30,420
should happen if something up here goes
2328
01:29:30,420 --> 01:29:32,580
wrong so if this throws an hour leg in
2329
01:29:32,580 --> 01:29:35,400
our case what do we want to do
2330
01:29:35,400 --> 01:29:37,320
first of all of course we want to lock
2331
01:29:37,320 --> 01:29:38,520
this error
2332
01:29:38,520 --> 01:29:41,040
so we write console.log and pass the
2333
01:29:41,040 --> 01:29:43,199
arrow here if this error contains an
2334
01:29:43,199 --> 01:29:45,600
error message we want to show it to the
2335
01:29:45,600 --> 01:29:47,699
user or whoever accesses this endpoint
2336
01:29:47,699 --> 01:29:49,620
if there is no message in there we want
2337
01:29:49,620 --> 01:29:51,840
to show a generic error message so what
2338
01:29:51,840 --> 01:29:53,820
we do is we create a variable and this
2339
01:29:53,820 --> 01:29:55,560
time it's a let because we want to
2340
01:29:55,560 --> 01:29:57,420
change its value later
2341
01:29:57,420 --> 01:29:59,760
colored error message
2342
01:29:59,760 --> 01:30:05,040
and rewrite an unknown error occurred
2343
01:30:05,040 --> 01:30:07,679
and whenever I write this I feel like a
2344
01:30:07,679 --> 01:30:09,840
real programmer because this is such a
2345
01:30:09,840 --> 01:30:12,480
pro camera sounding message don't get
2346
01:30:12,480 --> 01:30:14,219
confused by this error here this will
2347
01:30:14,219 --> 01:30:16,920
disappear in a moment it's just lint
2348
01:30:16,920 --> 01:30:19,139
complaining that this is a lead which we
2349
01:30:19,139 --> 01:30:21,120
are not changing right now but next we
2350
01:30:21,120 --> 01:30:23,940
check if error
2351
01:30:23,940 --> 01:30:26,580
instance of error
2352
01:30:26,580 --> 01:30:28,440
so we check if this error is actually
2353
01:30:28,440 --> 01:30:30,060
the arrow type and this is necessary
2354
01:30:30,060 --> 01:30:32,040
because this has the type unknown which
2355
01:30:32,040 --> 01:30:33,900
means this can be anything because we
2356
01:30:33,900 --> 01:30:35,940
can throw anything in our normal code we
2357
01:30:35,940 --> 01:30:37,440
can throw an error but we could also
2358
01:30:37,440 --> 01:30:39,540
throw Naya and so on this is why we have
2359
01:30:39,540 --> 01:30:41,699
to check that this is actually the type
2360
01:30:41,699 --> 01:30:44,639
error if this is an error then we want
2361
01:30:44,639 --> 01:30:46,860
to set the error message to
2362
01:30:46,860 --> 01:30:49,500
arrow.message
2363
01:30:49,500 --> 01:30:53,040
because every instance of type error has
2364
01:30:53,040 --> 01:30:55,320
this message property
2365
01:30:55,320 --> 01:30:57,960
and then we want to return this it's a
2366
01:30:57,960 --> 01:30:59,340
rest dot status
2367
01:30:59,340 --> 01:31:02,040
this time the status is 500 which means
2368
01:31:02,040 --> 01:31:04,320
internal server error
2369
01:31:04,320 --> 01:31:06,600
and we want to return a Json just like
2370
01:31:06,600 --> 01:31:08,580
we do for the happy path but this time
2371
01:31:08,580 --> 01:31:10,800
we have to add curly braces to define a
2372
01:31:10,800 --> 01:31:12,960
Json we didn't have to do this up here
2373
01:31:12,960 --> 01:31:15,480
because nodes is an array and the Json
2374
01:31:15,480 --> 01:31:17,580
method knows how to turn this into a
2375
01:31:17,580 --> 01:31:19,620
Json array automatically but now we
2376
01:31:19,620 --> 01:31:21,540
write this Json manually so we also have
2377
01:31:21,540 --> 01:31:24,000
to add the curly braces
2378
01:31:24,000 --> 01:31:26,580
we add an error field which will contain
2379
01:31:26,580 --> 01:31:28,020
the error message
2380
01:31:28,020 --> 01:31:30,600
so the user can later see this
2381
01:31:30,600 --> 01:31:32,699
so now let's try this out again so our
2382
01:31:32,699 --> 01:31:34,560
error is still in here we still have
2383
01:31:34,560 --> 01:31:37,139
this Bazinga error that we are throwing
2384
01:31:37,139 --> 01:31:39,360
but now when we open this endpoint we
2385
01:31:39,360 --> 01:31:41,219
see this error message here because this
2386
01:31:41,219 --> 01:31:43,320
is what we return
2387
01:31:43,320 --> 01:31:44,820
but the difference is that now our
2388
01:31:44,820 --> 01:31:46,679
server doesn't crash because we have
2389
01:31:46,679 --> 01:31:48,659
this try catch block so we can still
2390
01:31:48,659 --> 01:31:51,000
access our other endpoints our boss is
2391
01:31:51,000 --> 01:31:53,820
happier our wife won't leave us and our
2392
01:31:53,820 --> 01:31:56,100
life is just so much better but of
2393
01:31:56,100 --> 01:31:57,780
course we don't want to repeat this for
2394
01:31:57,780 --> 01:31:59,520
every endpoint because this is quite a
2395
01:31:59,520 --> 01:32:01,500
lot of code so what we can do is we can
2396
01:32:01,500 --> 01:32:03,659
set up an error Handler and the error
2397
01:32:03,659 --> 01:32:05,699
Handler will automatically kick in when
2398
01:32:05,699 --> 01:32:07,320
we have an error so it works the
2399
01:32:07,320 --> 01:32:09,659
following way we go below here and it
2400
01:32:09,659 --> 01:32:12,480
should be below our normal endpoint
2401
01:32:12,480 --> 01:32:15,380
here we write app.use
2402
01:32:15,380 --> 01:32:18,060
parentheses and to add another pair of
2403
01:32:18,060 --> 01:32:19,920
parentheses because we want to pass an
2404
01:32:19,920 --> 01:32:23,520
error function here so this error and
2405
01:32:23,520 --> 01:32:25,739
curly braces
2406
01:32:25,739 --> 01:32:27,540
but this function we'll take some
2407
01:32:27,540 --> 01:32:30,600
arguments and in Express error Handler
2408
01:32:30,600 --> 01:32:33,239
has to take a very specific set of
2409
01:32:33,239 --> 01:32:35,340
arguments and it has to be these same
2410
01:32:35,340 --> 01:32:37,199
arguments otherwise Express will not
2411
01:32:37,199 --> 01:32:39,540
recognize it as an error Handler the
2412
01:32:39,540 --> 01:32:41,880
first argument is the error itself
2413
01:32:41,880 --> 01:32:44,580
and we have to set this to unknown
2414
01:32:44,580 --> 01:32:47,760
just like the type up here
2415
01:32:47,760 --> 01:32:50,040
now why do we have to Define types here
2416
01:32:50,040 --> 01:32:52,260
but we didn't have to Define types up
2417
01:32:52,260 --> 01:32:54,780
here for our rack and rest arguments
2418
01:32:54,780 --> 01:32:57,239
this is because since we passed this as
2419
01:32:57,239 --> 01:32:58,920
an argument to this get function
2420
01:32:58,920 --> 01:33:01,440
typescript is smart enough to infer
2421
01:33:01,440 --> 01:33:03,719
these types so it knows from the
2422
01:33:03,719 --> 01:33:05,760
signature okay this is a request type
2423
01:33:05,760 --> 01:33:07,739
this is a response type down here we
2424
01:33:07,739 --> 01:33:09,420
don't have this type inference because
2425
01:33:09,420 --> 01:33:11,760
what we pass here could be anything so
2426
01:33:11,760 --> 01:33:13,560
we have to declare these types ourselves
2427
01:33:13,560 --> 01:33:15,659
this is necessary in typescript but not
2428
01:33:15,659 --> 01:33:17,940
a normal JavaScript the second argument
2429
01:33:17,940 --> 01:33:20,400
is the request just like up here but
2430
01:33:20,400 --> 01:33:22,500
again this time we have to define the
2431
01:33:22,500 --> 01:33:23,400
type
2432
01:33:23,400 --> 01:33:25,679
which is request and it's important that
2433
01:33:25,679 --> 01:33:28,380
you import this one here from the
2434
01:33:28,380 --> 01:33:30,719
Express package this is the request we
2435
01:33:30,719 --> 01:33:32,639
want to be using and this automatically
2436
01:33:32,639 --> 01:33:35,340
adds this request import up here Express
2437
01:33:35,340 --> 01:33:37,679
is the default export this is why this
2438
01:33:37,679 --> 01:33:39,600
is separate and those are additional
2439
01:33:39,600 --> 01:33:42,000
exported types this is why they are in
2440
01:33:42,000 --> 01:33:43,679
these curly braces if it didn't import
2441
01:33:43,679 --> 01:33:47,100
it automatically type it out by hand
2442
01:33:47,100 --> 01:33:50,219
the third argument is response again
2443
01:33:50,219 --> 01:33:53,100
from the Express package
2444
01:33:53,100 --> 01:33:55,620
and the third argument
2445
01:33:55,620 --> 01:33:58,800
card next and it's of type next function
2446
01:33:58,800 --> 01:34:01,440
from the Express package
2447
01:34:01,440 --> 01:34:04,080
again the error Handler has to take
2448
01:34:04,080 --> 01:34:06,239
exactly these four arguments with these
2449
01:34:06,239 --> 01:34:09,060
four types and then what we do is we cut
2450
01:34:09,060 --> 01:34:11,340
out this part here and paste it down
2451
01:34:11,340 --> 01:34:12,480
here
2452
01:34:12,480 --> 01:34:14,820
now a lint will complain that we are not
2453
01:34:14,820 --> 01:34:17,219
using next and that we could remove it
2454
01:34:17,219 --> 01:34:19,560
this is kind of a false positive because
2455
01:34:19,560 --> 01:34:22,139
if we remove it express will not
2456
01:34:22,139 --> 01:34:23,940
recognize this as an error handle
2457
01:34:23,940 --> 01:34:25,980
anymore so we have to ignore this one
2458
01:34:25,980 --> 01:34:28,139
here and just keep it the one thing we
2459
01:34:28,139 --> 01:34:31,380
could do is we could click Quick Fix and
2460
01:34:31,380 --> 01:34:33,719
disable this eslint rule for this line
2461
01:34:33,719 --> 01:34:36,420
which adds this comment here and I think
2462
01:34:36,420 --> 01:34:37,860
it's a good idea so you don't get
2463
01:34:37,860 --> 01:34:39,780
confused later this way we know that
2464
01:34:39,780 --> 01:34:42,600
this was on purpose okay so now how do
2465
01:34:42,600 --> 01:34:45,600
we forward this catch block here to our
2466
01:34:45,600 --> 01:34:46,860
error Handler
2467
01:34:46,860 --> 01:34:48,960
works the following way we add the third
2468
01:34:48,960 --> 01:34:52,020
argument up here which is next
2469
01:34:52,020 --> 01:34:54,239
and it's the same type as down here it's
2470
01:34:54,239 --> 01:34:56,460
this next function type
2471
01:34:56,460 --> 01:34:59,100
in every route we either have to send a
2472
01:34:59,100 --> 01:35:01,920
response the way we did up here or we
2473
01:35:01,920 --> 01:35:03,600
have to call next
2474
01:35:03,600 --> 01:35:05,760
which forwards this request to the next
2475
01:35:05,760 --> 01:35:08,400
middleware middleware is basically a
2476
01:35:08,400 --> 01:35:10,620
piece of code that knows how to handle a
2477
01:35:10,620 --> 01:35:11,760
request
2478
01:35:11,760 --> 01:35:14,520
on our Express server it's a concept
2479
01:35:14,520 --> 01:35:16,980
from the express Library you can read
2480
01:35:16,980 --> 01:35:19,320
more about this in the documentation
2481
01:35:19,320 --> 01:35:22,679
but both app.get and app.juice create
2482
01:35:22,679 --> 01:35:24,719
such a middleware the difference is that
2483
01:35:24,719 --> 01:35:27,000
app.get and also post and these other
2484
01:35:27,000 --> 01:35:29,820
HTTP verbs create middlewares that
2485
01:35:29,820 --> 01:35:31,800
create these endpoints that we can call
2486
01:35:31,800 --> 01:35:34,320
like slash and later slash nodes or
2487
01:35:34,320 --> 01:35:37,080
slash user app.us are other types of
2488
01:35:37,080 --> 01:35:39,300
metal Wells that we just use in our
2489
01:35:39,300 --> 01:35:41,580
codes to either prepare our response in
2490
01:35:41,580 --> 01:35:43,260
a certain way or handle this like our
2491
01:35:43,260 --> 01:35:45,060
error Handler here and the error Handler
2492
01:35:45,060 --> 01:35:47,100
is a very specific kind of middleware
2493
01:35:47,100 --> 01:35:49,199
that basically kicks in when we have an
2494
01:35:49,199 --> 01:35:51,480
error we will see more examples of these
2495
01:35:51,480 --> 01:35:53,280
middleware Slater so if this confuses
2496
01:35:53,280 --> 01:35:55,620
you don't worry just follow along and
2497
01:35:55,620 --> 01:35:57,659
again to cut the next middle where we
2498
01:35:57,659 --> 01:35:59,639
call this next function this is why we
2499
01:35:59,639 --> 01:36:01,800
have this next argument up here but we
2500
01:36:01,800 --> 01:36:03,540
don't want to call any middleware we
2501
01:36:03,540 --> 01:36:04,920
want to call our error Handler
2502
01:36:04,920 --> 01:36:07,440
middleware this is where we pass the
2503
01:36:07,440 --> 01:36:09,239
error as an argument to this next
2504
01:36:09,239 --> 01:36:10,320
function
2505
01:36:10,320 --> 01:36:12,780
so let's save this and see if it still
2506
01:36:12,780 --> 01:36:14,820
works
2507
01:36:14,820 --> 01:36:16,980
so we access our localhost endpoint
2508
01:36:16,980 --> 01:36:18,540
again we still have our error that's
2509
01:36:18,540 --> 01:36:19,500
handled
2510
01:36:19,500 --> 01:36:21,719
but now our error Handler here handles
2511
01:36:21,719 --> 01:36:23,639
this because the error Handler handles
2512
01:36:23,639 --> 01:36:25,380
the errors that have to be handled just
2513
01:36:25,380 --> 01:36:27,179
sounded cool in my opinion and now we
2514
01:36:27,179 --> 01:36:29,100
can reuse this because in every endpoint
2515
01:36:29,100 --> 01:36:30,900
in the catch block we just call next
2516
01:36:30,900 --> 01:36:32,400
Arrow and that's it
2517
01:36:32,400 --> 01:36:34,920
and these middlewares are checked in the
2518
01:36:34,920 --> 01:36:37,080
order that we Define them here this is
2519
01:36:37,080 --> 01:36:39,719
where we have to put the error Handler
2520
01:36:39,719 --> 01:36:41,820
at the bottom otherwise this is the
2521
01:36:41,820 --> 01:36:43,739
first one that would kick in but we only
2522
01:36:43,739 --> 01:36:45,239
want to get to this one if we actually
2523
01:36:45,239 --> 01:36:47,639
have an arrow now one more thing I want
2524
01:36:47,639 --> 01:36:49,739
to mention here if this was synchronous
2525
01:36:49,739 --> 01:36:52,020
code meaning that we don't have this
2526
01:36:52,020 --> 01:36:53,699
async and there's a weight in here
2527
01:36:53,699 --> 01:36:55,860
because we don't do a database
2528
01:36:55,860 --> 01:36:57,840
operations or other long running tasks
2529
01:36:57,840 --> 01:37:00,360
then we actually don't need try catch
2530
01:37:00,360 --> 01:37:02,699
because Express is smart enough to
2531
01:37:02,699 --> 01:37:04,440
forward this error to the error Handler
2532
01:37:04,440 --> 01:37:06,719
automatically however this does not work
2533
01:37:06,719 --> 01:37:08,699
right now for asynchronous code this is
2534
01:37:08,699 --> 01:37:10,380
why we have to add this try catch block
2535
01:37:10,380 --> 01:37:13,199
and call next ourselves however they are
2536
01:37:13,199 --> 01:37:14,820
going to change this in the next major
2537
01:37:14,820 --> 01:37:16,500
version of Express
2538
01:37:16,500 --> 01:37:19,100
right now we have Express
2539
01:37:19,100 --> 01:37:22,380
4.18.2 installed when this is five point
2540
01:37:22,380 --> 01:37:24,840
something or higher then you can remove
2541
01:37:24,840 --> 01:37:26,400
this try catch block because it
2542
01:37:26,400 --> 01:37:28,139
shouldn't be necessary anymore this is
2543
01:37:28,139 --> 01:37:30,179
an update they are planning so let's
2544
01:37:30,179 --> 01:37:32,400
remove this throw error otherwise this
2545
01:37:32,400 --> 01:37:34,800
endpoint will never work I'm going to
2546
01:37:34,800 --> 01:37:36,179
comment it out
2547
01:37:36,179 --> 01:37:38,100
so I leave it in here as a warning and
2548
01:37:38,100 --> 01:37:39,960
that's a reminder for you
2549
01:37:39,960 --> 01:37:41,699
and just to drive home this whole
2550
01:37:41,699 --> 01:37:43,739
concept of middleware slits actually add
2551
01:37:43,739 --> 01:37:44,880
another one
2552
01:37:44,880 --> 01:37:46,800
right now when we try to access an
2553
01:37:46,800 --> 01:37:48,739
endpoint that doesn't exist for example
2554
01:37:48,739 --> 01:37:52,320
localhost 5000 slash blah we get this
2555
01:37:52,320 --> 01:37:54,120
error message here but it might be a
2556
01:37:54,120 --> 01:37:56,100
good idea to create our own error
2557
01:37:56,100 --> 01:37:58,080
message that just has a better text than
2558
01:37:58,080 --> 01:38:00,120
this one here and we can do this with
2559
01:38:00,120 --> 01:38:02,580
another middleware that catches our
2560
01:38:02,580 --> 01:38:04,500
requests that go to an endpoint where we
2561
01:38:04,500 --> 01:38:07,380
don't have an actual route set up for it
2562
01:38:07,380 --> 01:38:09,060
so again we add the middleware with
2563
01:38:09,060 --> 01:38:10,380
app.use
2564
01:38:10,380 --> 01:38:12,659
and again we want to have this below our
2565
01:38:12,659 --> 01:38:14,820
normal routes because this is just a
2566
01:38:14,820 --> 01:38:17,760
fallback but before our error Handler
2567
01:38:17,760 --> 01:38:19,739
because we actually want to forward an
2568
01:38:19,739 --> 01:38:22,020
error in here to our error Handler so it
2569
01:38:22,020 --> 01:38:24,360
goes in between these two and in here we
2570
01:38:24,360 --> 01:38:26,639
pass another Handler function so another
2571
01:38:26,639 --> 01:38:28,739
pair of parentheses
2572
01:38:28,739 --> 01:38:32,639
Rick risk next leg up here but without
2573
01:38:32,639 --> 01:38:34,380
the end points because you want to use
2574
01:38:34,380 --> 01:38:36,239
this everywhere that we don't have an
2575
01:38:36,239 --> 01:38:39,060
endpoint set up for
2576
01:38:39,060 --> 01:38:42,480
right arrow curly braces
2577
01:38:42,480 --> 01:38:44,400
now this time we don't have to add types
2578
01:38:44,400 --> 01:38:46,139
again because they are inferred
2579
01:38:46,139 --> 01:38:48,000
automatically and to be honest I don't
2580
01:38:48,000 --> 01:38:50,400
know right now why it works for these
2581
01:38:50,400 --> 01:38:51,960
normal routes but not for the error
2582
01:38:51,960 --> 01:38:54,000
routes but it doesn't matter we don't
2583
01:38:54,000 --> 01:38:55,920
have to add these types here if we would
2584
01:38:55,920 --> 01:38:57,780
have to add them the compiler would
2585
01:38:57,780 --> 01:39:00,179
complain so to recap we get to this
2586
01:39:00,179 --> 01:39:02,580
middle wheel if none of our rods up here
2587
01:39:02,580 --> 01:39:05,219
fits so if we try to access an endpoint
2588
01:39:05,219 --> 01:39:07,199
that we haven't set up so what do we
2589
01:39:07,199 --> 01:39:08,880
want to do in this case well we
2590
01:39:08,880 --> 01:39:10,440
basically just want to throw an error
2591
01:39:10,440 --> 01:39:13,380
with a root not found message or
2592
01:39:13,380 --> 01:39:15,300
endpoint not found
2593
01:39:15,300 --> 01:39:18,179
so recall next and by passing an error
2594
01:39:18,179 --> 01:39:19,860
we forward this to our error Handler
2595
01:39:19,860 --> 01:39:22,500
right the same as we did up here
2596
01:39:22,500 --> 01:39:25,440
so we create an error like this
2597
01:39:25,440 --> 01:39:27,540
and we insert a message
2598
01:39:27,540 --> 01:39:31,040
and point not found
2599
01:39:32,400 --> 01:39:34,620
and now when we try to access an
2600
01:39:34,620 --> 01:39:37,860
endpoint that doesn't exist again
2601
01:39:37,860 --> 01:39:39,960
it forwards this to our error Handler
2602
01:39:39,960 --> 01:39:43,440
and we see our endpoint error message
2603
01:39:43,440 --> 01:39:45,900
now at the moment this will have a 500
2604
01:39:45,900 --> 01:39:48,420
response which is not quite correct they
2605
01:39:48,420 --> 01:39:50,460
should have a 404 response which means
2606
01:39:50,460 --> 01:39:52,440
not found rather than internal server
2607
01:39:52,440 --> 01:39:54,840
error but we will improve this later for
2608
01:39:54,840 --> 01:39:57,300
now this is sufficient alright and in
2609
01:39:57,300 --> 01:39:59,100
the next chapter we will set up another
2610
01:39:59,100 --> 01:40:01,560
endpoint for creating nodes so we don't
2611
01:40:01,560 --> 01:40:03,600
have to do this over the atlas console
2612
01:40:03,600 --> 01:40:05,580
anymore and we will organize our code
2613
01:40:05,580 --> 01:40:07,860
better into a routes and controller
2614
01:40:07,860 --> 01:40:10,280
files
2615
01:40:13,920 --> 01:40:16,500
okay now let's organize our code a
2616
01:40:16,500 --> 01:40:18,300
little bit better because if we keep
2617
01:40:18,300 --> 01:40:20,340
putting these endpoints into this FTS
2618
01:40:20,340 --> 01:40:22,199
file then it will be huge after a while
2619
01:40:22,199 --> 01:40:24,239
right and we need more endpoints with
2620
01:40:24,239 --> 01:40:27,000
more complex logic than this
2621
01:40:27,000 --> 01:40:29,639
so we cut out this endpoint here
2622
01:40:29,639 --> 01:40:31,440
and what we do is we create another
2623
01:40:31,440 --> 01:40:34,440
folder inside source
2624
01:40:34,440 --> 01:40:36,780
which we call routes
2625
01:40:36,780 --> 01:40:40,260
and inside routes we created nodes dot
2626
01:40:40,260 --> 01:40:43,679
TS file for our nodes routes
2627
01:40:43,679 --> 01:40:46,139
and here we are paste the code we just
2628
01:40:46,139 --> 01:40:48,120
cut out now before we take care of the
2629
01:40:48,120 --> 01:40:50,280
import statements and everything we want
2630
01:40:50,280 --> 01:40:52,980
to extract it even further because still
2631
01:40:52,980 --> 01:40:54,900
this file could be difficult to read in
2632
01:40:54,900 --> 01:40:56,580
the future because we have our endpoints
2633
01:40:56,580 --> 01:40:58,620
and then all this huge logic in this
2634
01:40:58,620 --> 01:41:00,420
Handler function
2635
01:41:00,420 --> 01:41:02,699
so what we do is we cut out this whole
2636
01:41:02,699 --> 01:41:05,219
part after the comma here
2637
01:41:05,219 --> 01:41:08,880
and put this into yet another file
2638
01:41:08,880 --> 01:41:10,560
so inside source we create another
2639
01:41:10,560 --> 01:41:13,800
folder which we call controllers and
2640
01:41:13,800 --> 01:41:16,739
this is a popular way of separating this
2641
01:41:16,739 --> 01:41:18,780
and here we create another file called
2642
01:41:18,780 --> 01:41:20,760
nodes.ears
2643
01:41:20,760 --> 01:41:22,199
um you could give them different names
2644
01:41:22,199 --> 01:41:24,780
you could call this nodes and this one
2645
01:41:24,780 --> 01:41:26,639
notes controllers doesn't matter it's up
2646
01:41:26,639 --> 01:41:28,440
to you but since this already has
2647
01:41:28,440 --> 01:41:30,480
different folders I'm actually fine with
2648
01:41:30,480 --> 01:41:33,060
having them have the same name and here
2649
01:41:33,060 --> 01:41:35,460
we paste the piece we just cut out
2650
01:41:35,460 --> 01:41:37,380
so this Handler function
2651
01:41:37,380 --> 01:41:39,719
and we want to export this function from
2652
01:41:39,719 --> 01:41:41,639
this file so that we can use it in our
2653
01:41:41,639 --> 01:41:43,500
routes file
2654
01:41:43,500 --> 01:41:46,619
so we write export const and this is
2655
01:41:46,619 --> 01:41:48,600
what I explained earlier it's a
2656
01:41:48,600 --> 01:41:50,159
different way of exporting something
2657
01:41:50,159 --> 01:41:52,860
from a JavaScript module but instead of
2658
01:41:52,860 --> 01:41:55,020
our export default where we export just
2659
01:41:55,020 --> 01:41:57,239
one thing this way we export multiple
2660
01:41:57,239 --> 01:41:59,699
things so we don't add default we just
2661
01:41:59,699 --> 01:42:02,040
add export and then the variable of
2662
01:42:02,040 --> 01:42:03,659
function or whatever we want to export
2663
01:42:03,659 --> 01:42:05,699
we have to give this a name
2664
01:42:05,699 --> 01:42:07,980
let's call it get notes because this is
2665
01:42:07,980 --> 01:42:10,739
what this endpoint does equals and this
2666
01:42:10,739 --> 01:42:11,880
function here
2667
01:42:11,880 --> 01:42:13,860
and this time again typescript can't
2668
01:42:13,860 --> 01:42:15,659
infer these types here because it
2669
01:42:15,659 --> 01:42:17,219
doesn't know what kind of function this
2670
01:42:17,219 --> 01:42:20,100
is so now we could either add requests
2671
01:42:20,100 --> 01:42:21,960
and response and everything to each of
2672
01:42:21,960 --> 01:42:24,179
these arguments separately but there's
2673
01:42:24,179 --> 01:42:25,860
actually a more concise way of doing
2674
01:42:25,860 --> 01:42:28,139
this we can give a type to this variable
2675
01:42:28,139 --> 01:42:30,300
itself the variable that contains our
2676
01:42:30,300 --> 01:42:32,699
function and then typescript can infer
2677
01:42:32,699 --> 01:42:35,100
the types of these three arguments so
2678
01:42:35,100 --> 01:42:37,860
behind get nodes but before the equal
2679
01:42:37,860 --> 01:42:40,800
sign we put a column to define the type
2680
01:42:40,800 --> 01:42:42,780
of this variable
2681
01:42:42,780 --> 01:42:45,540
which is type request Handler here from
2682
01:42:45,540 --> 01:42:48,719
the Express package we import this which
2683
01:42:48,719 --> 01:42:50,280
adds this import statement here at the
2684
01:42:50,280 --> 01:42:52,560
top and now typescript can infer the
2685
01:42:52,560 --> 01:42:54,719
types of these arguments as request
2686
01:42:54,719 --> 01:42:56,880
response and next function
2687
01:42:56,880 --> 01:42:58,860
and we also have to import this node
2688
01:42:58,860 --> 01:43:00,960
model here so let's add another import
2689
01:43:00,960 --> 01:43:02,580
statement
2690
01:43:02,580 --> 01:43:05,340
the same as we did before import node
2691
01:43:05,340 --> 01:43:06,360
model
2692
01:43:06,360 --> 01:43:07,500
from
2693
01:43:07,500 --> 01:43:10,199
and the destination of this model file
2694
01:43:10,199 --> 01:43:13,199
here by this time we write two dots
2695
01:43:13,199 --> 01:43:15,540
because one dot would go into the
2696
01:43:15,540 --> 01:43:17,820
controllers folder two dots goes into
2697
01:43:17,820 --> 01:43:20,580
the parent file of that which is source
2698
01:43:20,580 --> 01:43:23,280
so now we are inside source
2699
01:43:23,280 --> 01:43:25,980
you want to go inside the noddle models
2700
01:43:25,980 --> 01:43:29,040
folder and import our node model
2701
01:43:29,040 --> 01:43:31,739
now the error here disappeared entry can
2702
01:43:31,739 --> 01:43:33,780
also get rid of this
2703
01:43:33,780 --> 01:43:35,880
because we don't need it anymore
2704
01:43:35,880 --> 01:43:37,920
now we want to use this get nodes
2705
01:43:37,920 --> 01:43:40,739
function in our nodes routes
2706
01:43:40,739 --> 01:43:43,199
so here we import this function from our
2707
01:43:43,199 --> 01:43:46,320
nodes controller the following way
2708
01:43:46,320 --> 01:43:48,960
import then we make an asterisk so this
2709
01:43:48,960 --> 01:43:50,940
little star icon which means that we
2710
01:43:50,940 --> 01:43:53,760
want to import all functions that we put
2711
01:43:53,760 --> 01:43:57,320
into this file there will be more later
2712
01:43:57,900 --> 01:44:02,219
s nodes controller
2713
01:44:02,219 --> 01:44:06,119
from and the destination which is
2714
01:44:06,119 --> 01:44:09,480
the controllers folder
2715
01:44:09,480 --> 01:44:12,840
and then here we call nodes controller
2716
01:44:12,840 --> 01:44:14,280
get nodes
2717
01:44:14,280 --> 01:44:16,139
but we don't add a parenthesis because
2718
01:44:16,139 --> 01:44:18,480
we pass the function declaration itself
2719
01:44:18,480 --> 01:44:21,420
we don't pass the function call And to
2720
01:44:21,420 --> 01:44:23,880
clarify something we could also import
2721
01:44:23,880 --> 01:44:25,920
it like we did before with these curly
2722
01:44:25,920 --> 01:44:28,500
braces get notes like this and then we
2723
01:44:28,500 --> 01:44:30,659
could remove this nodes controller dot
2724
01:44:30,659 --> 01:44:32,820
this would also work you can do it like
2725
01:44:32,820 --> 01:44:34,560
this if you want the problem I have with
2726
01:44:34,560 --> 01:44:36,900
this is that this line here grows huge
2727
01:44:36,900 --> 01:44:38,760
because there can be many functions in
2728
01:44:38,760 --> 01:44:40,080
the controller
2729
01:44:40,080 --> 01:44:42,540
so with this way of importing it we add
2730
01:44:42,540 --> 01:44:44,280
them to this notes controller namespace
2731
01:44:44,280 --> 01:44:47,400
and keep the support statement short but
2732
01:44:47,400 --> 01:44:49,739
appear still shows us an error because
2733
01:44:49,739 --> 01:44:52,440
the compiler doesn't know what app means
2734
01:44:52,440 --> 01:44:54,179
because we are not inside this folder
2735
01:44:54,179 --> 01:44:55,560
here anymore where we have this Express
2736
01:44:55,560 --> 01:44:57,360
app
2737
01:44:57,360 --> 01:45:00,119
so the same as an app we want to import
2738
01:45:00,119 --> 01:45:02,580
Express in here
2739
01:45:02,580 --> 01:45:05,340
from the Express package
2740
01:45:05,340 --> 01:45:07,320
shouldn't matter if you put it below or
2741
01:45:07,320 --> 01:45:09,300
above notes controller but this is the
2742
01:45:09,300 --> 01:45:12,060
organization I like
2743
01:45:12,060 --> 01:45:13,980
and then we have to instantiate the app
2744
01:45:13,980 --> 01:45:15,840
but this time we don't want to call
2745
01:45:15,840 --> 01:45:17,520
Express because we don't want to create
2746
01:45:17,520 --> 01:45:19,920
a whole not Observer instead we want to
2747
01:45:19,920 --> 01:45:22,199
use the one that we already have so
2748
01:45:22,199 --> 01:45:24,659
instead of instantiating Express
2749
01:45:24,659 --> 01:45:28,020
we create a router
2750
01:45:28,020 --> 01:45:31,980
with Express dot router you can read
2751
01:45:31,980 --> 01:45:33,900
more about this in the documentation but
2752
01:45:33,900 --> 01:45:36,239
this is basically a way to set endpoints
2753
01:45:36,239 --> 01:45:38,159
on a router and then we later set this
2754
01:45:38,159 --> 01:45:40,920
router on our Express app here to add
2755
01:45:40,920 --> 01:45:42,480
these endpoints it's pretty
2756
01:45:42,480 --> 01:45:44,760
straightforward and then we want to
2757
01:45:44,760 --> 01:45:48,000
replace app.get for router.get and again
2758
01:45:48,000 --> 01:45:50,340
get means that this is an HTTP get
2759
01:45:50,340 --> 01:45:51,719
request
2760
01:45:51,719 --> 01:45:53,639
then we make this router the default
2761
01:45:53,639 --> 01:45:55,980
export so we can use it in a different
2762
01:45:55,980 --> 01:45:57,900
file and that's the only thing we need
2763
01:45:57,900 --> 01:46:00,000
to export from this module
2764
01:46:00,000 --> 01:46:03,060
so we write export default router
2765
01:46:03,060 --> 01:46:05,520
semicolon
2766
01:46:05,520 --> 01:46:07,139
save this
2767
01:46:07,139 --> 01:46:09,300
and then where do we want to use this we
2768
01:46:09,300 --> 01:46:11,880
want to use this in our FTS file
2769
01:46:11,880 --> 01:46:14,520
we want to import our new router appeal
2770
01:46:14,520 --> 01:46:16,560
and by the way we can delete this node
2771
01:46:16,560 --> 01:46:19,199
model because we now handle this in our
2772
01:46:19,199 --> 01:46:21,119
controllers
2773
01:46:21,119 --> 01:46:22,739
import
2774
01:46:22,739 --> 01:46:25,380
s again you can give this any name you
2775
01:46:25,380 --> 01:46:27,619
want
2776
01:46:27,659 --> 01:46:29,940
from the routes folder
2777
01:46:29,940 --> 01:46:32,580
our notes routes file
2778
01:46:32,580 --> 01:46:35,340
and then here between const app and our
2779
01:46:35,340 --> 01:46:37,920
endpoint node font Handler the same
2780
01:46:37,920 --> 01:46:39,480
place where we had this endpoint before
2781
01:46:39,480 --> 01:46:42,960
we simply call s dot us to add another
2782
01:46:42,960 --> 01:46:44,639
middleware
2783
01:46:44,639 --> 01:46:47,520
we Define an endpoint and this endpoint
2784
01:46:47,520 --> 01:46:49,619
will be added in front of this endpoint
2785
01:46:49,619 --> 01:46:52,560
here right now we have localhost 5000
2786
01:46:52,560 --> 01:46:55,560
and this gives us our notes back but in
2787
01:46:55,560 --> 01:46:57,360
production we want to have them at an
2788
01:46:57,360 --> 01:46:59,219
endpoint that's a bit more specific
2789
01:46:59,219 --> 01:47:02,400
right so the endpoint will appear
2790
01:47:02,400 --> 01:47:04,800
slash API
2791
01:47:04,800 --> 01:47:06,780
slash notes
2792
01:47:06,780 --> 01:47:09,199
so later let's say our website is called
2793
01:47:09,199 --> 01:47:11,639
quillnotesapp.com then we will get the
2794
01:47:11,639 --> 01:47:15,360
notes from coolnotesapp.com API slash
2795
01:47:15,360 --> 01:47:16,500
nodes
2796
01:47:16,500 --> 01:47:18,780
and we want to forward this endpoint to
2797
01:47:18,780 --> 01:47:20,340
our nodes routes
2798
01:47:20,340 --> 01:47:22,380
so this is basically like a puzzle
2799
01:47:22,380 --> 01:47:24,540
that's puts together we have this
2800
01:47:24,540 --> 01:47:27,060
middleware that catches any request that
2801
01:47:27,060 --> 01:47:28,739
goes to this endpoint
2802
01:47:28,739 --> 01:47:31,020
which then checks the notes routes
2803
01:47:31,020 --> 01:47:32,880
endpoints we have set up here and look
2804
01:47:32,880 --> 01:47:34,860
which one fits in here we will later
2805
01:47:34,860 --> 01:47:36,840
have more endpoints in here as well for
2806
01:47:36,840 --> 01:47:39,300
example router.get for a specific node
2807
01:47:39,300 --> 01:47:42,060
which then adds additional endpoints on
2808
01:47:42,060 --> 01:47:44,340
this API node's endpoint if this is
2809
01:47:44,340 --> 01:47:45,659
confusing don't worry you will
2810
01:47:45,659 --> 01:47:47,460
understand it when we later actually use
2811
01:47:47,460 --> 01:47:50,100
this let's just save every file that's
2812
01:47:50,100 --> 01:47:52,800
still opened and then
2813
01:47:52,800 --> 01:47:55,440
access this new endpoint so now the end
2814
01:47:55,440 --> 01:47:58,260
point directly at localhost 5000 isn't
2815
01:47:58,260 --> 01:48:00,179
found anymore because this is now not
2816
01:48:00,179 --> 01:48:02,580
where we get our nodes from we now get
2817
01:48:02,580 --> 01:48:05,639
it from slash API slash nodes and
2818
01:48:05,639 --> 01:48:08,639
there's our node the same as before
2819
01:48:08,639 --> 01:48:11,400
but it's better organized now and now we
2820
01:48:11,400 --> 01:48:13,320
also want to add an endpoint where we
2821
01:48:13,320 --> 01:48:15,600
can create nodes without having to do
2822
01:48:15,600 --> 01:48:18,000
this manually in our mongodb Atlas
2823
01:48:18,000 --> 01:48:20,280
backend now first of all we need to tell
2824
01:48:20,280 --> 01:48:22,320
Express what kinds of data we want to
2825
01:48:22,320 --> 01:48:23,520
accept
2826
01:48:23,520 --> 01:48:26,400
as messages to the server and we do this
2827
01:48:26,400 --> 01:48:30,000
in our FTS file before this route here
2828
01:48:30,000 --> 01:48:32,280
at this exact point we add another
2829
01:48:32,280 --> 01:48:35,280
middleware app.use
2830
01:48:35,280 --> 01:48:36,860
and pass
2831
01:48:36,860 --> 01:48:39,980
express.json and call this as a function
2832
01:48:39,980 --> 01:48:42,900
exactly like this and this basically
2833
01:48:42,900 --> 01:48:45,960
sets up Express so that it accepts Json
2834
01:48:45,960 --> 01:48:49,080
bodies so we can now not only retrieve
2835
01:48:49,080 --> 01:48:51,719
Json from our get endpoint we can also
2836
01:48:51,719 --> 01:48:54,239
send Json to our server when we then add
2837
01:48:54,239 --> 01:48:55,739
a post endpoint
2838
01:48:55,739 --> 01:48:58,199
so let's save this and add another
2839
01:48:58,199 --> 01:49:01,260
endpoint and our notes controllers file
2840
01:49:01,260 --> 01:49:04,080
So Below get notes we add another export
2841
01:49:04,080 --> 01:49:05,699
const
2842
01:49:05,699 --> 01:49:08,880
which we this time call create node and
2843
01:49:08,880 --> 01:49:10,619
the same as before
2844
01:49:10,619 --> 01:49:13,860
it's a request Handler
2845
01:49:13,860 --> 01:49:15,719
equal sign
2846
01:49:15,719 --> 01:49:18,360
it's an asynchronous function
2847
01:49:18,360 --> 01:49:21,840
because it does database operations
2848
01:49:21,840 --> 01:49:23,639
curly braces
2849
01:49:23,639 --> 01:49:26,280
entry can already add and try catch book
2850
01:49:26,280 --> 01:49:29,100
here because we always need this when we
2851
01:49:29,100 --> 01:49:32,400
do something that could throw an error
2852
01:49:32,400 --> 01:49:34,619
and in catch we want to do the same as
2853
01:49:34,619 --> 01:49:37,260
up here pass it to our error Handler
2854
01:49:37,260 --> 01:49:38,580
middleware
2855
01:49:38,580 --> 01:49:40,500
before we set up the body of this
2856
01:49:40,500 --> 01:49:44,300
endpoint let's add it to our routes
2857
01:49:44,340 --> 01:49:47,159
So Below router.get will write router
2858
01:49:47,159 --> 01:49:49,800
dot post this time because this will be
2859
01:49:49,800 --> 01:49:52,139
a post request we are sending data to
2860
01:49:52,139 --> 01:49:54,780
the server in this case our node it will
2861
01:49:54,780 --> 01:49:56,699
be at the same endpoint and this is
2862
01:49:56,699 --> 01:49:58,320
possible because those are different
2863
01:49:58,320 --> 01:50:01,320
HTTP methods so they will not interfere
2864
01:50:01,320 --> 01:50:02,940
with each other even if they have the
2865
01:50:02,940 --> 01:50:04,800
same endpoint if we wanted to get
2866
01:50:04,800 --> 01:50:06,600
different kinds of data then of course
2867
01:50:06,600 --> 01:50:08,580
we can't use the same endpoint otherwise
2868
01:50:08,580 --> 01:50:10,739
the server wouldn't know what endpoint
2869
01:50:10,739 --> 01:50:12,840
this is supposed to be and here we want
2870
01:50:12,840 --> 01:50:15,840
to use our newer nodes controller create
2871
01:50:15,840 --> 01:50:17,460
node function
2872
01:50:17,460 --> 01:50:20,520
save this and go back in here
2873
01:50:20,520 --> 01:50:22,800
to finish setting this up
2874
01:50:22,800 --> 01:50:25,619
we present the title and text in the
2875
01:50:25,619 --> 01:50:28,139
body of this request so first we have to
2876
01:50:28,139 --> 01:50:30,300
get these values out of the body we can
2877
01:50:30,300 --> 01:50:32,520
do this outside of the try block or
2878
01:50:32,520 --> 01:50:34,800
inside it doesn't really matter but this
2879
01:50:34,800 --> 01:50:37,020
shouldn't throw anything so I think it's
2880
01:50:37,020 --> 01:50:39,960
more correct to put it outside
2881
01:50:39,960 --> 01:50:42,900
create a cons title and we get this
2882
01:50:42,900 --> 01:50:44,580
value that we send to the server from
2883
01:50:44,580 --> 01:50:47,219
the request body so we take this request
2884
01:50:47,219 --> 01:50:49,219
object
2885
01:50:49,219 --> 01:50:51,000
foreign
2886
01:50:51,000 --> 01:50:53,159
and then the name of the variable that
2887
01:50:53,159 --> 01:50:55,739
we present to the server entry will call
2888
01:50:55,739 --> 01:50:56,940
it title
2889
01:50:56,940 --> 01:50:59,639
just like the field in the node itself
2890
01:50:59,639 --> 01:51:02,159
and the same for the text
2891
01:51:02,159 --> 01:51:05,760
we will send it as the text field in the
2892
01:51:05,760 --> 01:51:07,679
request body
2893
01:51:07,679 --> 01:51:09,780
then insert try block because this is
2894
01:51:09,780 --> 01:51:12,000
now the parts that can throw an error we
2895
01:51:12,000 --> 01:51:14,100
want to use Mongoose to create our node
2896
01:51:14,100 --> 01:51:17,400
and we also want to save this new node
2897
01:51:17,400 --> 01:51:20,280
in a variable so that we can send it
2898
01:51:20,280 --> 01:51:22,320
back to the client and we do this so
2899
01:51:22,320 --> 01:51:24,239
that we can later put this newer node
2900
01:51:24,239 --> 01:51:26,880
into the UI and update the UI on the
2901
01:51:26,880 --> 01:51:29,040
client and it's also useful to see the
2902
01:51:29,040 --> 01:51:31,139
data we created when we get our response
2903
01:51:31,139 --> 01:51:34,020
back so here we call await and again the
2904
01:51:34,020 --> 01:51:36,119
await is very important
2905
01:51:36,119 --> 01:51:39,560
node model dot create
2906
01:51:39,560 --> 01:51:42,900
parentheses curly braces
2907
01:51:42,900 --> 01:51:46,080
and I already add the semicolon down
2908
01:51:46,080 --> 01:51:46,860
here
2909
01:51:46,860 --> 01:51:49,139
and in here we create our notes and we
2910
01:51:49,139 --> 01:51:50,760
actually have Auto completion for this
2911
01:51:50,760 --> 01:51:53,639
why because this refers to our node
2912
01:51:53,639 --> 01:51:55,440
schema we set up here so it already
2913
01:51:55,440 --> 01:51:57,960
knows what kind of data this expects
2914
01:51:57,960 --> 01:52:00,659
it expects a title which is our title up
2915
01:52:00,659 --> 01:52:02,340
here
2916
01:52:02,340 --> 01:52:03,659
comma
2917
01:52:03,659 --> 01:52:05,639
and the text value
2918
01:52:05,639 --> 01:52:08,219
and again the timestamps will be set
2919
01:52:08,219 --> 01:52:09,719
automatically
2920
01:52:09,719 --> 01:52:12,360
now create returns A Promise by default
2921
01:52:12,360 --> 01:52:14,460
which means that we don't have to append
2922
01:52:14,460 --> 01:52:16,800
this dot exec at the end which we had to
2923
01:52:16,800 --> 01:52:19,080
do up here I don't know why they made
2924
01:52:19,080 --> 01:52:20,699
this design decision that we have to
2925
01:52:20,699 --> 01:52:23,520
edit up here but not down here but yeah
2926
01:52:23,520 --> 01:52:25,560
it's not necessary and then if this
2927
01:52:25,560 --> 01:52:27,659
should fail for any reason we get into
2928
01:52:27,659 --> 01:52:29,760
our catch block if it doesn't fail we
2929
01:52:29,760 --> 01:52:31,980
continue with the drive block so we want
2930
01:52:31,980 --> 01:52:35,040
to send a response back to the client we
2931
01:52:35,040 --> 01:52:38,100
set the code to 201 which is the HTTP
2932
01:52:38,100 --> 01:52:40,800
code for a new resource created we could
2933
01:52:40,800 --> 01:52:42,780
other than 200 doesn't really matter but
2934
01:52:42,780 --> 01:52:45,659
it's better to distinguish them properly
2935
01:52:45,659 --> 01:52:48,780
entry one is sent a new note back
2936
01:52:48,780 --> 01:52:51,239
as a Json
2937
01:52:51,239 --> 01:52:52,800
save
2938
01:52:52,800 --> 01:52:54,900
okay and now this endpoint should be
2939
01:52:54,900 --> 01:52:57,179
configured properly because we already
2940
01:52:57,179 --> 01:53:00,179
referenced it in our nodes routes file
2941
01:53:00,179 --> 01:53:02,400
and this on the other hand is connected
2942
01:53:02,400 --> 01:53:04,860
in our FTS file
2943
01:53:04,860 --> 01:53:06,840
so this should work now the problem is
2944
01:53:06,840 --> 01:53:09,239
we don't have a client yet to send data
2945
01:53:09,239 --> 01:53:11,340
to our server and with a web browser we
2946
01:53:11,340 --> 01:53:13,440
can only do get requests we can send
2947
01:53:13,440 --> 01:53:15,780
something to the server this is why we
2948
01:53:15,780 --> 01:53:18,000
will use a tool called Postman which is
2949
01:53:18,000 --> 01:53:20,460
really popular for they are working on
2950
01:53:20,460 --> 01:53:22,920
server stuff in development because it
2951
01:53:22,920 --> 01:53:25,139
allows you to do all kinds of HTTP
2952
01:53:25,139 --> 01:53:26,699
requests without the need of a real
2953
01:53:26,699 --> 01:53:29,100
client now don't get scared because it
2954
01:53:29,100 --> 01:53:31,199
says pricing we can actually use it for
2955
01:53:31,199 --> 01:53:32,100
free
2956
01:53:32,100 --> 01:53:36,380
so download it for your operating system
2957
01:53:36,420 --> 01:53:38,280
run this file and then you have to
2958
01:53:38,280 --> 01:53:39,600
create an account
2959
01:53:39,600 --> 01:53:42,360
before you can use it which is fine I'm
2960
01:53:42,360 --> 01:53:44,520
gonna sign up with my recording account
2961
01:53:44,520 --> 01:53:46,800
here again again you can use it for free
2962
01:53:46,800 --> 01:53:48,780
and it's very popular tool among
2963
01:53:48,780 --> 01:53:50,760
developers so you don't have to be
2964
01:53:50,760 --> 01:53:52,020
afraid of this
2965
01:53:52,020 --> 01:53:55,320
all right then log in to postman follow
2966
01:53:55,320 --> 01:53:57,659
this sign up process here and then we
2967
01:53:57,659 --> 01:54:00,060
see us on the other side then we want to
2968
01:54:00,060 --> 01:54:02,219
create a new HTTP request and they have
2969
01:54:02,219 --> 01:54:03,840
the shorthand here maybe in the future
2970
01:54:03,840 --> 01:54:06,480
it looks different but somewhere there's
2971
01:54:06,480 --> 01:54:09,540
an option to get a new HTTP request and
2972
01:54:09,540 --> 01:54:11,100
it should look like this
2973
01:54:11,100 --> 01:54:13,619
and here we select post because we want
2974
01:54:13,619 --> 01:54:15,659
to send a post request
2975
01:54:15,659 --> 01:54:18,840
we type in our localhost address
2976
01:54:18,840 --> 01:54:22,500
localhost 5000 slash API slash nodes
2977
01:54:22,500 --> 01:54:24,480
because this is the endpoint we set up
2978
01:54:24,480 --> 01:54:26,880
and we want to send a Json body to the
2979
01:54:26,880 --> 01:54:27,900
server
2980
01:54:27,900 --> 01:54:29,880
so we click on body
2981
01:54:29,880 --> 01:54:32,159
on Raw
2982
01:54:32,159 --> 01:54:35,880
and then we select not text but Json
2983
01:54:35,880 --> 01:54:37,739
and then in curly braces we have to
2984
01:54:37,739 --> 01:54:40,080
define the Json here which should
2985
01:54:40,080 --> 01:54:42,420
contain the title and a text right so we
2986
01:54:42,420 --> 01:54:44,880
write it like this in quotation marks
2987
01:54:44,880 --> 01:54:48,020
title colon
2988
01:54:48,080 --> 01:54:53,480
my first endpoint note
2989
01:54:53,760 --> 01:54:55,560
and the text
2990
01:54:55,560 --> 01:54:58,260
this is fun
2991
01:54:58,260 --> 01:55:00,360
and then we want to click Send and if we
2992
01:55:00,360 --> 01:55:02,520
set up everything properly this post
2993
01:55:02,520 --> 01:55:05,100
request should be handled on our server
2994
01:55:05,100 --> 01:55:07,260
and it looks good because this is the
2995
01:55:07,260 --> 01:55:09,480
response we got back as you can see here
2996
01:55:09,480 --> 01:55:12,719
status 201 created this is what we
2997
01:55:12,719 --> 01:55:15,119
defined in our endpoint and we get our
2998
01:55:15,119 --> 01:55:17,820
new note back which contains an idea and
2999
01:55:17,820 --> 01:55:19,440
all the data
3000
01:55:19,440 --> 01:55:22,920
so let's look into a mongodb again
3001
01:55:22,920 --> 01:55:25,920
refresh this
3002
01:55:25,920 --> 01:55:29,699
and excitement there's our new note
3003
01:55:29,699 --> 01:55:31,800
this time we created it through our own
3004
01:55:31,800 --> 01:55:34,500
endpoint isn't that amazing and if you
3005
01:55:34,500 --> 01:55:36,000
made it until here in this tutorial
3006
01:55:36,000 --> 01:55:38,219
please go below and leave a like it's
3007
01:55:38,219 --> 01:55:40,080
really important to me
3008
01:55:40,080 --> 01:55:42,719
of course we can also run our get nodes
3009
01:55:42,719 --> 01:55:46,260
endpoint as a get request here which
3010
01:55:46,260 --> 01:55:48,239
gives us an array of our notes just like
3011
01:55:48,239 --> 01:55:49,619
we got when we did this in the web
3012
01:55:49,619 --> 01:55:51,780
browser let's also try creating a note
3013
01:55:51,780 --> 01:55:54,300
where the title is missing because we
3014
01:55:54,300 --> 01:55:57,840
defined in our mongodb schema
3015
01:55:57,840 --> 01:56:00,480
that's the title is required right so
3016
01:56:00,480 --> 01:56:02,400
this should give us an error if the
3017
01:56:02,400 --> 01:56:05,219
title is missing let's remove this
3018
01:56:05,219 --> 01:56:06,900
send it again
3019
01:56:06,900 --> 01:56:09,060
and we get an error note validation
3020
01:56:09,060 --> 01:56:12,000
failed title path title is required now
3021
01:56:12,000 --> 01:56:14,159
this is not a very readable error
3022
01:56:14,159 --> 01:56:16,020
message because this comes directly from
3023
01:56:16,020 --> 01:56:18,119
mongodb but we will improve this later
3024
01:56:18,119 --> 01:56:21,119
for this is nice so here that our schema
3025
01:56:21,119 --> 01:56:24,960
is actually enforced however we can
3026
01:56:24,960 --> 01:56:27,420
create a notificed the should work
3027
01:56:27,420 --> 01:56:29,219
because this is not required in our
3028
01:56:29,219 --> 01:56:30,480
schema
3029
01:56:30,480 --> 01:56:33,119
and the succeeded so let's take a look
3030
01:56:33,119 --> 01:56:36,260
into the database again
3031
01:56:37,380 --> 01:56:39,900
and there's our third node without the
3032
01:56:39,900 --> 01:56:42,000
text this time let's add one more
3033
01:56:42,000 --> 01:56:44,460
endpoint for now to get a single note
3034
01:56:44,460 --> 01:56:46,800
because it's interesting to see how we
3035
01:56:46,800 --> 01:56:49,860
can access a single node so we go into
3036
01:56:49,860 --> 01:56:52,260
our nodes controller again
3037
01:56:52,260 --> 01:56:54,420
and just for organization let's put it
3038
01:56:54,420 --> 01:56:57,119
here below get nodes
3039
01:56:57,119 --> 01:57:02,100
export const get node singular this time
3040
01:57:02,100 --> 01:57:06,020
of type request Handler
3041
01:57:06,239 --> 01:57:09,239
an async error function with regress
3042
01:57:09,239 --> 01:57:10,980
next
3043
01:57:10,980 --> 01:57:12,719
semicolon
3044
01:57:12,719 --> 01:57:15,540
try catch this is just the kind of stuff
3045
01:57:15,540 --> 01:57:19,280
you repeat over and over again
3046
01:57:19,860 --> 01:57:21,780
then before we set this up we go into
3047
01:57:21,780 --> 01:57:24,179
the routes file
3048
01:57:24,179 --> 01:57:26,520
add another endpoint here
3049
01:57:26,520 --> 01:57:28,440
router.get
3050
01:57:28,440 --> 01:57:31,080
this time we write slash and after that
3051
01:57:31,080 --> 01:57:32,880
we'll write a colon
3052
01:57:32,880 --> 01:57:35,760
note idea comma
3053
01:57:35,760 --> 01:57:37,860
notes controller
3054
01:57:37,860 --> 01:57:39,599
get node
3055
01:57:39,599 --> 01:57:42,000
now this node ID is basically a variable
3056
01:57:42,000 --> 01:57:44,219
which means that whatever we put behind
3057
01:57:44,219 --> 01:57:46,739
the slash will be read by Express and
3058
01:57:46,739 --> 01:57:49,320
put into the request object so we can
3059
01:57:49,320 --> 01:57:51,719
read it in our get node endpoint and
3060
01:57:51,719 --> 01:57:53,639
then look up the specific node for this
3061
01:57:53,639 --> 01:57:55,980
idea so let's save this file and go into
3062
01:57:55,980 --> 01:57:56,940
the
3063
01:57:56,940 --> 01:57:59,760
get node function here again entry get
3064
01:57:59,760 --> 01:58:01,800
this value is similar to how we got the
3065
01:58:01,800 --> 01:58:04,020
values out of the body let's put it
3066
01:58:04,020 --> 01:58:06,800
above the dry block
3067
01:58:06,800 --> 01:58:10,380
const node ID equals we take our request
3068
01:58:10,380 --> 01:58:12,540
object but this time we don't want to
3069
01:58:12,540 --> 01:58:13,980
exist the board here
3070
01:58:13,980 --> 01:58:16,800
we want to access the params and the
3071
01:58:16,800 --> 01:58:19,080
params are these variables that we put
3072
01:58:19,080 --> 01:58:21,900
here into the URL directly
3073
01:58:21,900 --> 01:58:24,780
those are our strings basically and we
3074
01:58:24,780 --> 01:58:27,119
want to take the note idea
3075
01:58:27,119 --> 01:58:29,099
so it has to have the same name as we
3076
01:58:29,099 --> 01:58:31,080
defined here with the exact same
3077
01:58:31,080 --> 01:58:32,159
spelling
3078
01:58:32,159 --> 01:58:34,560
and then we use Mongoose inside the try
3079
01:58:34,560 --> 01:58:37,199
block to get this specific note so we
3080
01:58:37,199 --> 01:58:40,080
create a const node equals
3081
01:58:40,080 --> 01:58:42,000
a weight
3082
01:58:42,000 --> 01:58:45,960
node model dot finds by idea which is a
3083
01:58:45,960 --> 01:58:47,639
special Mongoose function where we can
3084
01:58:47,639 --> 01:58:49,980
pass the idea of this model and it will
3085
01:58:49,980 --> 01:58:52,800
find the specific document so here we
3086
01:58:52,800 --> 01:58:55,500
simply paste the node idea
3087
01:58:55,500 --> 01:58:58,020
this time we have to call.exec again to
3088
01:58:58,020 --> 01:59:00,599
turn this into a real promise and then
3089
01:59:00,599 --> 01:59:03,360
we want to do the same as up here
3090
01:59:03,360 --> 01:59:06,360
return this node with a status code of
3091
01:59:06,360 --> 01:59:08,580
200
3092
01:59:08,580 --> 01:59:10,800
in the Json body
3093
01:59:10,800 --> 01:59:13,320
but this time it's the singular node and
3094
01:59:13,320 --> 01:59:14,699
not nodes
3095
01:59:14,699 --> 01:59:16,139
save
3096
01:59:16,139 --> 01:59:18,659
and now let's try this out we can either
3097
01:59:18,659 --> 01:59:20,940
do this in the web browser or in Postman
3098
01:59:20,940 --> 01:59:23,760
but Postman just formats this a bit
3099
01:59:23,760 --> 01:59:26,280
better so I prefer this first of all we
3100
01:59:26,280 --> 01:59:28,800
need an ID of a node so let's get all
3101
01:59:28,800 --> 01:59:29,880
nodes
3102
01:59:29,880 --> 01:59:33,000
and let's get this one here my first
3103
01:59:33,000 --> 01:59:37,159
endpoint node we copy this idea
3104
01:59:37,159 --> 01:59:41,040
add notes slash and then simply the idea
3105
01:59:41,040 --> 01:59:42,840
because this is the structure of the
3106
01:59:42,840 --> 01:59:45,060
endpoint we just set up
3107
01:59:45,060 --> 01:59:47,219
and as you can see here this returns
3108
01:59:47,219 --> 01:59:49,560
this exact node
3109
01:59:49,560 --> 01:59:51,719
if we change a letter in here to a
3110
01:59:51,719 --> 01:59:54,659
non-existing idea this fails for one
3111
01:59:54,659 --> 01:59:56,639
because the snow doesn't exist but also
3112
01:59:56,639 --> 01:59:58,560
because this is not a valid object idea
3113
01:59:58,560 --> 02:00:00,360
we will add some checks for this later
3114
02:00:00,360 --> 02:00:02,940
the important part is that this gives us
3115
02:00:02,940 --> 02:00:05,219
the exact node for this idea later of
3116
02:00:05,219 --> 02:00:07,020
course a user should only be able to
3117
02:00:07,020 --> 02:00:09,179
access their own nodes but we will take
3118
02:00:09,179 --> 02:00:11,040
care of authentication and stuff later
3119
02:00:11,040 --> 02:00:13,500
and by the way these errors we get here
3120
02:00:13,500 --> 02:00:15,480
we also can see them in the console
3121
02:00:15,480 --> 02:00:17,280
because we are locking them
3122
02:00:17,280 --> 02:00:19,260
which is especially important in
3123
02:00:19,260 --> 02:00:21,060
production because if something goes
3124
02:00:21,060 --> 02:00:22,980
wrong for a user you want to be able to
3125
02:00:22,980 --> 02:00:25,320
go back into the log and see what went
3126
02:00:25,320 --> 02:00:27,119
wrong so we see the error here as well
3127
02:00:27,119 --> 02:00:29,280
now there's one more package I want to
3128
02:00:29,280 --> 02:00:31,260
install here which is just a nice tool
3129
02:00:31,260 --> 02:00:33,840
to have this one is called Morgan and
3130
02:00:33,840 --> 02:00:36,300
it's just a locking tool so we can see
3131
02:00:36,300 --> 02:00:38,099
all the requests to the different
3132
02:00:38,099 --> 02:00:40,139
endpoints that were made on our server
3133
02:00:40,139 --> 02:00:43,800
which is just nice information to know
3134
02:00:43,800 --> 02:00:47,099
so let's stop our server once again and
3135
02:00:47,099 --> 02:00:51,000
install Morgan the usual way
3136
02:00:51,000 --> 02:00:53,580
and there's not much setup and worth in
3137
02:00:53,580 --> 02:00:55,560
this it's just a simple middleware that
3138
02:00:55,560 --> 02:00:59,040
we install in our FTS file and above
3139
02:00:59,040 --> 02:01:01,800
Express Json right here we add another
3140
02:01:01,800 --> 02:01:04,679
middleware with app.use but we have to
3141
02:01:04,679 --> 02:01:07,619
import it first so up here we import
3142
02:01:07,619 --> 02:01:09,420
Morgan
3143
02:01:09,420 --> 02:01:13,320
from the Morgan package like this
3144
02:01:13,320 --> 02:01:15,599
and this shows us an error here because
3145
02:01:15,599 --> 02:01:17,940
it needs some type files this is
3146
02:01:17,940 --> 02:01:19,260
sometimes the case when you use
3147
02:01:19,260 --> 02:01:21,420
typescript with certain packages hover
3148
02:01:21,420 --> 02:01:23,699
over this error and if you see something
3149
02:01:23,699 --> 02:01:27,659
like this try npm is FDF if it exists or
3150
02:01:27,659 --> 02:01:30,060
add new declaration type blah blah blah
3151
02:01:30,060 --> 02:01:32,880
then you have to copy this part here
3152
02:01:32,880 --> 02:01:35,400
is just installation command for another
3153
02:01:35,400 --> 02:01:38,099
dependency
3154
02:01:38,099 --> 02:01:40,260
which we install right now
3155
02:01:40,260 --> 02:01:45,239
npm IR safe Dev adds types slash Morgan
3156
02:01:45,239 --> 02:01:48,300
then this error should disappear
3157
02:01:48,300 --> 02:01:51,420
we can start our server again and finish
3158
02:01:51,420 --> 02:01:54,300
setting this up we simply want to call
3159
02:01:54,300 --> 02:01:57,540
Morgan as a function
3160
02:01:57,540 --> 02:01:59,880
and pass Dev as a string this just
3161
02:01:59,880 --> 02:02:02,400
defines the type of information and the
3162
02:02:02,400 --> 02:02:03,960
amount of information we printed the
3163
02:02:03,960 --> 02:02:05,820
console you can read more about this in
3164
02:02:05,820 --> 02:02:07,920
the documentation but this is pretty
3165
02:02:07,920 --> 02:02:11,340
cool because it prints a concise log of
3166
02:02:11,340 --> 02:02:14,159
all the endpoints that we access
3167
02:02:14,159 --> 02:02:16,500
so let's try this out
3168
02:02:16,500 --> 02:02:20,159
let's make a get request again
3169
02:02:20,159 --> 02:02:22,679
and as you can see we now have this log
3170
02:02:22,679 --> 02:02:24,900
message down here get request on this
3171
02:02:24,900 --> 02:02:27,300
endpoint what status code resent back
3172
02:02:27,300 --> 02:02:29,340
and so on and this is just nice
3173
02:02:29,340 --> 02:02:31,440
information to have especially in
3174
02:02:31,440 --> 02:02:32,400
production
3175
02:02:32,400 --> 02:02:34,800
this is the endpoint with all the nodes
3176
02:02:34,800 --> 02:02:35,820
yeah
3177
02:02:35,820 --> 02:02:37,619
this is just something I wanted to add
3178
02:02:37,619 --> 02:02:39,900
here and now we already have a real
3179
02:02:39,900 --> 02:02:41,940
little server with different endpoints
3180
02:02:41,940 --> 02:02:44,219
where we can get information from we can
3181
02:02:44,219 --> 02:02:46,980
create new information really amazing of
3182
02:02:46,980 --> 02:02:48,060
course we still want to add more
3183
02:02:48,060 --> 02:02:50,580
endpoints but it's already a success but
3184
02:02:50,580 --> 02:02:52,560
in the next section before we start
3185
02:02:52,560 --> 02:02:54,480
adding more endpoints we will improve
3186
02:02:54,480 --> 02:02:56,460
our error handling a little bit because
3187
02:02:56,460 --> 02:02:59,460
as I already noted at some places we
3188
02:02:59,460 --> 02:03:02,400
don't always return the optimal status
3189
02:03:02,400 --> 02:03:04,440
code like here for our endpoint not
3190
02:03:04,440 --> 02:03:07,619
found error sometimes the error message
3191
02:03:07,619 --> 02:03:09,960
is not really good and really readable
3192
02:03:09,960 --> 02:03:11,760
like the one we get from Mongoose by
3193
02:03:11,760 --> 02:03:13,920
default so next we will take a look at
3194
02:03:13,920 --> 02:03:16,260
how we can improve this because it's
3195
02:03:16,260 --> 02:03:18,599
important and after that we will add new
3196
02:03:18,599 --> 02:03:21,060
endpoints to update existing nodes and
3197
02:03:21,060 --> 02:03:23,659
delete nodes
3198
02:03:27,179 --> 02:03:30,119
okay so as I already said before we add
3199
02:03:30,119 --> 02:03:32,159
more endpoints to our server we want to
3200
02:03:32,159 --> 02:03:34,139
improve our error handling here because
3201
02:03:34,139 --> 02:03:37,320
that's not quite optimal yet for one our
3202
02:03:37,320 --> 02:03:39,960
endpoint not found middleware here which
3203
02:03:39,960 --> 02:03:41,940
kicks in whenever we try to access the
3204
02:03:41,940 --> 02:03:43,800
endpoint that we didn't set up like
3205
02:03:43,800 --> 02:03:46,800
slash API slash blah for example at the
3206
02:03:46,800 --> 02:03:49,500
moment returns a 500 status code because
3207
02:03:49,500 --> 02:03:51,480
that's what we hard coded in our error
3208
02:03:51,480 --> 02:03:53,400
Handler here so we basically use it for
3209
02:03:53,400 --> 02:03:55,619
every error but different errors require
3210
02:03:55,619 --> 02:03:58,440
different status codes 500 is the status
3211
02:03:58,440 --> 02:04:00,900
code for an internal server error which
3212
02:04:00,900 --> 02:04:02,820
basically means that something in our
3213
02:04:02,820 --> 02:04:05,159
code went wrong and maybe we don't even
3214
02:04:05,159 --> 02:04:07,320
really know what but an endpoint not
3215
02:04:07,320 --> 02:04:10,260
found error should return a 404 code
3216
02:04:10,260 --> 02:04:12,719
because that's the HTTP status code for
3217
02:04:12,719 --> 02:04:14,760
resource not found so it's just more
3218
02:04:14,760 --> 02:04:16,679
appropriate and it's better for the
3219
02:04:16,679 --> 02:04:19,080
clients to work with secondly when we
3220
02:04:19,080 --> 02:04:21,480
try to create a node without the title
3221
02:04:21,480 --> 02:04:23,760
which mongodb doesn't like because we
3222
02:04:23,760 --> 02:04:26,219
said required to draw for the title and
3223
02:04:26,219 --> 02:04:29,099
mongodbier at Mongoose and fosters but
3224
02:04:29,099 --> 02:04:31,619
if we try to do this it also gives us an
3225
02:04:31,619 --> 02:04:34,199
error but with the default mongodb error
3226
02:04:34,199 --> 02:04:36,000
message which might be difficult to
3227
02:04:36,000 --> 02:04:37,920
understand for users so we want to
3228
02:04:37,920 --> 02:04:39,840
provide our own error message instead
3229
02:04:39,840 --> 02:04:42,480
and for this kind of HTTP errors there
3230
02:04:42,480 --> 02:04:45,239
is a convenient package we can use so we
3231
02:04:45,239 --> 02:04:47,639
open our terminal and ignore these error
3232
02:04:47,639 --> 02:04:49,080
messages down here this is only on my
3233
02:04:49,080 --> 02:04:51,239
site because I tried something a moment
3234
02:04:51,239 --> 02:04:53,280
ago and did a mistake but if you don't
3235
02:04:53,280 --> 02:04:55,500
know how to open the already existing
3236
02:04:55,500 --> 02:04:56,460
terminal
3237
02:04:56,460 --> 02:04:59,400
you can click on viewer and here on
3238
02:04:59,400 --> 02:05:01,080
Terminal or use the shortcut that's
3239
02:05:01,080 --> 02:05:03,060
right next to it this will open the
3240
02:05:03,060 --> 02:05:05,639
terminal that you already created before
3241
02:05:05,639 --> 02:05:07,560
if there is none you can also create a
3242
02:05:07,560 --> 02:05:09,840
new terminal over this menu point up
3243
02:05:09,840 --> 02:05:11,520
here that we used earlier
3244
02:05:11,520 --> 02:05:14,280
anyway just open the terminal
3245
02:05:14,280 --> 02:05:16,260
and then important we want to go into
3246
02:05:16,260 --> 02:05:18,900
our backend folder again not into the
3247
02:05:18,900 --> 02:05:20,760
root folder where we are right now this
3248
02:05:20,760 --> 02:05:21,900
is where you see these error messages
3249
02:05:21,900 --> 02:05:23,520
here I'm actually recording this a
3250
02:05:23,520 --> 02:05:25,320
second time because I messed this up
3251
02:05:25,320 --> 02:05:28,139
before so this is very important so we
3252
02:05:28,139 --> 02:05:31,560
type CD slash back end to go into our
3253
02:05:31,560 --> 02:05:33,960
backend folder okay actually without the
3254
02:05:33,960 --> 02:05:36,659
slash just CD backend and then you need
3255
02:05:36,659 --> 02:05:38,820
to make sure that the path looks correct
3256
02:05:38,820 --> 02:05:41,460
so we are in our coding and floor learn
3257
02:05:41,460 --> 02:05:43,800
course folder or however you called it
3258
02:05:43,800 --> 02:05:45,599
earlier and then inside the backend
3259
02:05:45,599 --> 02:05:46,920
folder here
3260
02:05:46,920 --> 02:05:48,719
because this is where we want to install
3261
02:05:48,719 --> 02:05:52,739
this new package so we type npm IO short
3262
02:05:52,739 --> 02:05:54,300
for install
3263
02:05:54,300 --> 02:05:57,360
HTTP minus errors
3264
02:05:57,360 --> 02:05:59,460
and as usual you have to type the
3265
02:05:59,460 --> 02:06:01,860
package name correctly otherwise npm
3266
02:06:01,860 --> 02:06:04,080
will not recognize it
3267
02:06:04,080 --> 02:06:05,699
we installed this
3268
02:06:05,699 --> 02:06:08,400
and then we want to install a second one
3269
02:06:08,400 --> 02:06:10,320
which are the type declarations again
3270
02:06:10,320 --> 02:06:13,080
that we need for typescript to work with
3271
02:06:13,080 --> 02:06:14,400
this package
3272
02:06:14,400 --> 02:06:16,560
so again npm IR
3273
02:06:16,560 --> 02:06:20,940
minus uppercase deal for def dependency
3274
02:06:20,940 --> 02:06:25,800
add types slash HTTP errors type it out
3275
02:06:25,800 --> 02:06:29,179
correctly and then press enter
3276
02:06:30,000 --> 02:06:32,340
and this should have added two new
3277
02:06:32,340 --> 02:06:35,340
entries here on our package.json HTTP
3278
02:06:35,340 --> 02:06:37,199
errors in the normal dependencies and
3279
02:06:37,199 --> 02:06:40,619
the types here and the dev dependencies
3280
02:06:40,619 --> 02:06:43,380
we could also create our own HTTP errors
3281
02:06:43,380 --> 02:06:45,960
instead of using this package but again
3282
02:06:45,960 --> 02:06:47,940
this makes it more convenient so let's
3283
02:06:47,940 --> 02:06:49,560
close the terminal with the same
3284
02:06:49,560 --> 02:06:52,500
shortcut and then improve our code here
3285
02:06:52,500 --> 02:06:55,619
we want to start with this part here
3286
02:06:55,619 --> 02:06:58,260
instead of returning a normal error with
3287
02:06:58,260 --> 02:07:00,060
this error Constructor
3288
02:07:00,060 --> 02:07:03,540
we want to create a new HTTP error which
3289
02:07:03,540 --> 02:07:05,340
we do with this function here
3290
02:07:05,340 --> 02:07:07,679
from this package we are installed
3291
02:07:07,679 --> 02:07:09,960
if this doesn't import automatically or
3292
02:07:09,960 --> 02:07:11,880
if other completion isn't there then
3293
02:07:11,880 --> 02:07:14,699
type in the import up here manually
3294
02:07:14,699 --> 02:07:17,099
and this function also takes an error
3295
02:07:17,099 --> 02:07:19,619
message that we already entered but it
3296
02:07:19,619 --> 02:07:22,199
also takes a second argument which comes
3297
02:07:22,199 --> 02:07:24,239
before the error message which is the
3298
02:07:24,239 --> 02:07:26,639
HTTP status code that we want to return
3299
02:07:26,639 --> 02:07:28,260
with this error
3300
02:07:28,260 --> 02:07:30,840
and as I already said we wanna have a
3301
02:07:30,840 --> 02:07:34,080
404 status code for this one because 404
3302
02:07:34,080 --> 02:07:36,000
means resource not found which is
3303
02:07:36,000 --> 02:07:37,619
appropriate for an endpoint that doesn't
3304
02:07:37,619 --> 02:07:40,199
exist comma and the zinc argument is the
3305
02:07:40,199 --> 02:07:42,540
error message just as before but the
3306
02:07:42,540 --> 02:07:44,340
problem right now is that we hardcore
3307
02:07:44,340 --> 02:07:46,260
the status codes down here on our error
3308
02:07:46,260 --> 02:07:48,300
Handler so we have to change this code
3309
02:07:48,300 --> 02:07:49,980
here as well
3310
02:07:49,980 --> 02:07:52,860
below let error message we also create a
3311
02:07:52,860 --> 02:07:55,080
new variable again a let
3312
02:07:55,080 --> 02:07:57,420
for the status code
3313
02:07:57,420 --> 02:08:00,060
we initialize this with 500 because this
3314
02:08:00,060 --> 02:08:01,860
will be our file back if we don't
3315
02:08:01,860 --> 02:08:05,460
specify a more specific status code then
3316
02:08:05,460 --> 02:08:07,920
we delete this line here instead of
3317
02:08:07,920 --> 02:08:10,380
checking if error is an instance of the
3318
02:08:10,380 --> 02:08:12,239
normal error class we want to check if
3319
02:08:12,239 --> 02:08:14,699
it's an instance of this new HTTP error
3320
02:08:14,699 --> 02:08:16,500
because we basically want to use this
3321
02:08:16,500 --> 02:08:19,020
throughout our whole server code so we
3322
02:08:19,020 --> 02:08:21,780
remove this and write if and in between
3323
02:08:21,780 --> 02:08:24,599
the parentheses of the if block we write
3324
02:08:24,599 --> 02:08:29,040
is HTTP here which is another function
3325
02:08:29,040 --> 02:08:31,619
of the HTTP Eros package but this one
3326
02:08:31,619 --> 02:08:34,020
didn't import automatically so we do it
3327
02:08:34,020 --> 02:08:37,080
manually so after create HTTP error we
3328
02:08:37,080 --> 02:08:39,599
write a comma and again create HTTP
3329
02:08:39,599 --> 02:08:41,520
error as the default import the other
3330
02:08:41,520 --> 02:08:43,739
one is a non-default import so we have
3331
02:08:43,739 --> 02:08:46,860
to add it between curly braces like this
3332
02:08:46,860 --> 02:08:48,780
and then paste the name of this function
3333
02:08:48,780 --> 02:08:51,179
again you don't have to understand the
3334
02:08:51,179 --> 02:08:53,159
syntax here in detail just make sure to
3335
02:08:53,159 --> 02:08:55,679
type it all the same way if autocomplete
3336
02:08:55,679 --> 02:08:56,820
didn't work
3337
02:08:56,820 --> 02:09:00,360
okay so is HTTP error down here is a
3338
02:09:00,360 --> 02:09:02,219
function which takes an argument with a
3339
02:09:02,219 --> 02:09:04,619
steroid serve this will check if this
3340
02:09:04,619 --> 02:09:08,159
error is an instance of this HTTP error
3341
02:09:08,159 --> 02:09:11,520
from the HTTP errors package
3342
02:09:11,520 --> 02:09:14,400
if this is the case we want to take our
3343
02:09:14,400 --> 02:09:17,119
status code variable and set it to
3344
02:09:17,119 --> 02:09:19,560
arrow.status which we now have available
3345
02:09:19,560 --> 02:09:22,500
because status is a field in the HTTP
3346
02:09:22,500 --> 02:09:23,820
error class
3347
02:09:23,820 --> 02:09:25,980
and the same as before we also want to
3348
02:09:25,980 --> 02:09:27,480
set the error message
3349
02:09:27,480 --> 02:09:30,780
so we take our error message variable
3350
02:09:30,780 --> 02:09:33,599
and set it to error.message
3351
02:09:33,599 --> 02:09:36,540
which the HTTP error class contains just
3352
02:09:36,540 --> 02:09:38,400
like normal errors
3353
02:09:38,400 --> 02:09:40,860
Alright and then the only change left is
3354
02:09:40,860 --> 02:09:43,020
replacing our status code that we
3355
02:09:43,020 --> 02:09:45,300
hardcoded down here
3356
02:09:45,300 --> 02:09:48,000
for the status code variable which is
3357
02:09:48,000 --> 02:09:51,000
either whatever status is in the HTTP
3358
02:09:51,000 --> 02:09:54,659
error or a fallback of 500 for internal
3359
02:09:54,659 --> 02:09:57,060
server error alright and now let's try
3360
02:09:57,060 --> 02:10:00,960
this out let's save the code with Ctrl s
3361
02:10:00,960 --> 02:10:04,080
open the terminal again and Run npm
3362
02:10:04,080 --> 02:10:06,900
start to start our server and then I'm
3363
02:10:06,900 --> 02:10:08,580
going to open an endpoint that doesn't
3364
02:10:08,580 --> 02:10:11,099
exist again and as we can already see
3365
02:10:11,099 --> 02:10:12,480
them because we installed Morgan
3366
02:10:12,480 --> 02:10:15,480
previously we now have this 404 response
3367
02:10:15,480 --> 02:10:17,340
here and we will see the same when we
3368
02:10:17,340 --> 02:10:19,199
look into the Chrome Dev tools when we
3369
02:10:19,199 --> 02:10:21,300
try to access an endpoint that doesn't
3370
02:10:21,300 --> 02:10:24,659
exist status 404 which is just a better
3371
02:10:24,659 --> 02:10:27,360
status code to return and displays an
3372
02:10:27,360 --> 02:10:29,040
even more important role when we have
3373
02:10:29,040 --> 02:10:31,320
different kind of Errors later now let's
3374
02:10:31,320 --> 02:10:33,480
also improve the error messages for our
3375
02:10:33,480 --> 02:10:37,020
node routes so we close our terminal
3376
02:10:37,020 --> 02:10:40,080
go into the notes controller and let's
3377
02:10:40,080 --> 02:10:42,300
start with create node here as I already
3378
02:10:42,300 --> 02:10:44,159
explained at the moment when we don't
3379
02:10:44,159 --> 02:10:46,679
pass a title we get mongosa's default
3380
02:10:46,679 --> 02:10:48,659
error Throne which has an error message
3381
02:10:48,659 --> 02:10:50,760
that might not be readable enough for
3382
02:10:50,760 --> 02:10:52,860
the average user and also it doesn't
3383
02:10:52,860 --> 02:10:55,500
have an HTTP status code associated with
3384
02:10:55,500 --> 02:10:58,800
it so it will fall back to our 500 code
3385
02:10:58,800 --> 02:11:01,800
for this reason instead of relying on
3386
02:11:01,800 --> 02:11:04,080
Mongoose to check the title and throw an
3387
02:11:04,080 --> 02:11:05,699
error we want to check the title
3388
02:11:05,699 --> 02:11:08,219
ourselves in here and any other field
3389
02:11:08,219 --> 02:11:10,860
that might be required this gives us
3390
02:11:10,860 --> 02:11:13,080
more control over the error message and
3391
02:11:13,080 --> 02:11:15,480
the status code and then the required
3392
02:11:15,480 --> 02:11:17,520
field of mongoose itself is basically
3393
02:11:17,520 --> 02:11:19,440
just a far back in case we mess
3394
02:11:19,440 --> 02:11:21,119
something up but the first line of
3395
02:11:21,119 --> 02:11:23,460
defense is the code in our own endpoint
3396
02:11:23,460 --> 02:11:25,679
Handler okay but the problem right now
3397
02:11:25,679 --> 02:11:27,719
is that the types of our title and text
3398
02:11:27,719 --> 02:11:30,000
here is Nia because typescript doesn't
3399
02:11:30,000 --> 02:11:32,400
know the type of these variables in the
3400
02:11:32,400 --> 02:11:34,619
body here it also doesn't have to be a
3401
02:11:34,619 --> 02:11:36,300
string that could also be a number for
3402
02:11:36,300 --> 02:11:38,940
example but we can tell typescript what
3403
02:11:38,940 --> 02:11:42,179
type of variables we expect in the body
3404
02:11:42,179 --> 02:11:46,199
we do this by declaring an interface
3405
02:11:46,199 --> 02:11:49,199
which we call create note body
3406
02:11:49,199 --> 02:11:51,360
and then we make a pair of curly braces
3407
02:11:51,360 --> 02:11:52,820
like this
3408
02:11:52,820 --> 02:11:55,440
interfaces are very similar to types
3409
02:11:55,440 --> 02:11:57,599
like we declared with this type keyword
3410
02:11:57,599 --> 02:12:00,060
here in Mongoose the difference between
3411
02:12:00,060 --> 02:12:02,520
interface and types are very specific
3412
02:12:02,520 --> 02:12:04,800
and I don't want to explain all of them
3413
02:12:04,800 --> 02:12:06,599
here but generally you want to use
3414
02:12:06,599 --> 02:12:08,580
interfaces whenever possible because
3415
02:12:08,580 --> 02:12:10,860
they are more flexible and there are
3416
02:12:10,860 --> 02:12:12,239
certain situations where you can't use
3417
02:12:12,239 --> 02:12:14,219
interfaces then you use types instead
3418
02:12:14,219 --> 02:12:16,139
for example here in Mongoose we have to
3419
02:12:16,139 --> 02:12:18,659
use a type because this is what in first
3420
02:12:18,659 --> 02:12:21,179
schema type expects from us we can't use
3421
02:12:21,179 --> 02:12:23,820
interface here but to declare the types
3422
02:12:23,820 --> 02:12:25,860
of the single fields in our create note
3423
02:12:25,860 --> 02:12:29,040
body here we use an interface and in
3424
02:12:29,040 --> 02:12:31,199
here we just declare what variables we
3425
02:12:31,199 --> 02:12:33,179
expect when we make this create note
3426
02:12:33,179 --> 02:12:35,219
request and what types they should have
3427
02:12:35,219 --> 02:12:37,199
and we already know what we expect we
3428
02:12:37,199 --> 02:12:39,119
expected title
3429
02:12:39,119 --> 02:12:41,460
of type string
3430
02:12:41,460 --> 02:12:44,040
and a text of type string
3431
02:12:44,040 --> 02:12:45,960
now we already know that the text is
3432
02:12:45,960 --> 02:12:48,780
optional we don't have to send one so we
3433
02:12:48,780 --> 02:12:50,820
add a question mark here which basically
3434
02:12:50,820 --> 02:12:53,219
means that yeah we can send this text if
3435
02:12:53,219 --> 02:12:54,599
we want or not so this might be
3436
02:12:54,599 --> 02:12:57,480
undefined but we actually set the title
3437
02:12:57,480 --> 02:13:00,119
to optional as well why because whenever
3438
02:13:00,119 --> 02:13:02,159
someone makes a request to this endpoint
3439
02:13:02,159 --> 02:13:04,260
we can't know if they actually send this
3440
02:13:04,260 --> 02:13:06,420
data even though the title is required
3441
02:13:06,420 --> 02:13:08,579
it might be missing from the request and
3442
02:13:08,579 --> 02:13:10,980
then we want to decline it right but we
3443
02:13:10,980 --> 02:13:12,719
still have these any types here because
3444
02:13:12,719 --> 02:13:15,480
we have to actually use this interface
3445
02:13:15,480 --> 02:13:18,239
in our request Handler here which we do
3446
02:13:18,239 --> 02:13:20,760
by adding a type argument after request
3447
02:13:20,760 --> 02:13:23,099
Handler which we do like this with a
3448
02:13:23,099 --> 02:13:25,320
pair of angle brackets and then we have
3449
02:13:25,320 --> 02:13:27,659
to declare four different types because
3450
02:13:27,659 --> 02:13:30,119
this is all or nothing either we declare
3451
02:13:30,119 --> 02:13:33,119
no type or all and by all I mean for the
3452
02:13:33,119 --> 02:13:35,940
body for the URL arguments the fourth
3453
02:13:35,940 --> 02:13:38,639
one are the URL params so there's a
3454
02:13:38,639 --> 02:13:41,400
difference between a UI argument like
3455
02:13:41,400 --> 02:13:43,739
our node idea and URL params which we
3456
02:13:43,739 --> 02:13:46,560
have after this question mark and the
3457
02:13:46,560 --> 02:13:48,659
fourth one I don't remember right now so
3458
02:13:48,659 --> 02:13:51,840
and the body is the third argument for
3459
02:13:51,840 --> 02:13:53,699
the other ones we want to leave them
3460
02:13:53,699 --> 02:13:55,260
untouched basically
3461
02:13:55,260 --> 02:13:57,480
which we do by passing unknown so
3462
02:13:57,480 --> 02:13:59,579
unknown is used implicitly for all of
3463
02:13:59,579 --> 02:14:01,560
them if we don't pass a type argument
3464
02:14:01,560 --> 02:14:03,840
here we want to leave the type of the
3465
02:14:03,840 --> 02:14:06,540
URL params and so on as unknown because
3466
02:14:06,540 --> 02:14:09,540
we don't use these arguments here but we
3467
02:14:09,540 --> 02:14:11,699
want to set the type for the body so
3468
02:14:11,699 --> 02:14:14,579
unknown comma unknown
3469
02:14:14,579 --> 02:14:17,520
comma create note body and then again
3470
02:14:17,520 --> 02:14:19,079
comma unknown
3471
02:14:19,079 --> 02:14:21,119
and when you hover over request Handler
3472
02:14:21,119 --> 02:14:22,860
you can see at the order of these
3473
02:14:22,860 --> 02:14:25,079
arguments we are passing here the params
3474
02:14:25,079 --> 02:14:27,780
the response body request body and the
3475
02:14:27,780 --> 02:14:28,739
query
3476
02:14:28,739 --> 02:14:31,980
anyway and you might be wondering why do
3477
02:14:31,980 --> 02:14:34,320
we pass unknown here and not any because
3478
02:14:34,320 --> 02:14:37,260
any is unsafe any basically allows us to
3479
02:14:37,260 --> 02:14:40,199
call anything on these variables unknown
3480
02:14:40,199 --> 02:14:42,960
is the opposite it's restrictive so I
3481
02:14:42,960 --> 02:14:44,940
think if we try to use the query params
3482
02:14:44,940 --> 02:14:46,860
now which has Type unknown then we could
3483
02:14:46,860 --> 02:14:48,780
not call anything on it I'm not sure it
3484
02:14:48,780 --> 02:14:50,579
doesn't matter because if we needed the
3485
02:14:50,579 --> 02:14:52,800
query params we would pass a type for
3486
02:14:52,800 --> 02:14:55,079
them and not unknown but we only need a
3487
02:14:55,079 --> 02:14:57,300
body right now but the magic is not
3488
02:14:57,300 --> 02:14:59,280
visible when we hover over title and
3489
02:14:59,280 --> 02:15:01,500
text because now they have types they
3490
02:15:01,500 --> 02:15:04,099
are both string or undefined and again
3491
02:15:04,099 --> 02:15:06,540
undefined is a possibility for both of
3492
02:15:06,540 --> 02:15:08,340
them because we don't have a guarantee
3493
02:15:08,340 --> 02:15:10,560
that whoever uses this endpoint actually
3494
02:15:10,560 --> 02:15:12,900
sends both values over the difference is
3495
02:15:12,900 --> 02:15:14,820
that if the text is undefined we don't
3496
02:15:14,820 --> 02:15:16,980
care because it's not required but we
3497
02:15:16,980 --> 02:15:19,619
care if the title is undefined so we go
3498
02:15:19,619 --> 02:15:22,500
into our try block here because now we
3499
02:15:22,500 --> 02:15:24,300
are playing around with errors and
3500
02:15:24,300 --> 02:15:26,579
whenever we undo stuff with arrows we
3501
02:15:26,579 --> 02:15:29,099
have to catch them so that our silver
3502
02:15:29,099 --> 02:15:31,440
doesn't crash and we'll lose our job and
3503
02:15:31,440 --> 02:15:34,260
everything remember here we check if
3504
02:15:34,260 --> 02:15:36,420
and in between the parentheses we write
3505
02:15:36,420 --> 02:15:38,400
exclamation mark title
3506
02:15:38,400 --> 02:15:41,760
which means title is falzia which is the
3507
02:15:41,760 --> 02:15:44,699
case if title is undefined then we want
3508
02:15:44,699 --> 02:15:46,440
to throw
3509
02:15:46,440 --> 02:15:50,099
a create HTTP error the same one we
3510
02:15:50,099 --> 02:15:52,739
called in our endpoint.font
3511
02:15:52,739 --> 02:15:54,360
Handler here
3512
02:15:54,360 --> 02:15:57,060
because this creates an HTTP error which
3513
02:15:57,060 --> 02:15:59,639
we then throw which then gets caught by
3514
02:15:59,639 --> 02:16:01,800
our try catch block and forward it to
3515
02:16:01,800 --> 02:16:03,659
our error Handler alright and what
3516
02:16:03,659 --> 02:16:06,060
status code we want to use we want to
3517
02:16:06,060 --> 02:16:09,179
use 400 this time and in case you're
3518
02:16:09,179 --> 02:16:11,579
wondering where I'm pulling this from
3519
02:16:11,579 --> 02:16:14,159
you can basically just type HTTP status
3520
02:16:14,159 --> 02:16:16,020
codes into Google and you will find
3521
02:16:16,020 --> 02:16:18,060
different lists with explanations for
3522
02:16:18,060 --> 02:16:19,619
these different chords so every
3523
02:16:19,619 --> 02:16:21,540
situation every kind of error has an
3524
02:16:21,540 --> 02:16:23,460
appropriate status code and as you can
3525
02:16:23,460 --> 02:16:25,560
see here for example 400 means bet
3526
02:16:25,560 --> 02:16:27,960
request which is often used if there is
3527
02:16:27,960 --> 02:16:29,880
an argument for example missing which is
3528
02:16:29,880 --> 02:16:32,218
the case here we could use any stereos
3529
02:16:32,218 --> 02:16:34,439
code here using the correct one as just
3530
02:16:34,439 --> 02:16:37,500
a matter of making our API better usable
3531
02:16:37,500 --> 02:16:39,478
for clients so it's a good idea to get
3532
02:16:39,478 --> 02:16:41,879
this right and not use any arbitrary
3533
02:16:41,879 --> 02:16:44,700
code entry also want to return an error
3534
02:16:44,700 --> 02:16:45,718
method Shield
3535
02:16:45,718 --> 02:16:50,160
note must have a title
3536
02:16:50,160 --> 02:16:52,439
we don't need a return statement here to
3537
02:16:52,439 --> 02:16:54,660
leave our code because a thrower leaves
3538
02:16:54,660 --> 02:16:57,120
the try block anywhere and goes right to
3539
02:16:57,120 --> 02:16:58,320
the catch block
3540
02:16:58,320 --> 02:17:01,558
all right so let's try out our new error
3541
02:17:01,558 --> 02:17:03,179
message here
3542
02:17:03,179 --> 02:17:05,340
with Postman
3543
02:17:05,340 --> 02:17:07,500
let's make a post request to our notes
3544
02:17:07,500 --> 02:17:08,638
endpoint
3545
02:17:08,638 --> 02:17:10,439
we still have the title in the board
3546
02:17:10,439 --> 02:17:12,478
here but we want to put the text in the
3547
02:17:12,478 --> 02:17:14,939
body and have the title missing
3548
02:17:14,939 --> 02:17:17,040
which now should give us a better error
3549
02:17:17,040 --> 02:17:19,019
message and that's not it because I
3550
02:17:19,019 --> 02:17:21,299
didn't save the code yet so let's save
3551
02:17:21,299 --> 02:17:24,058
it and try this again
3552
02:17:24,058 --> 02:17:26,519
now it must have a title nice with the
3553
02:17:26,519 --> 02:17:28,920
status code 400 so now we handle this
3554
02:17:28,920 --> 02:17:31,620
error for this endpoint properly and not
3555
02:17:31,620 --> 02:17:34,320
nearly villier like we did before okay
3556
02:17:34,320 --> 02:17:37,019
but our get node endpoint here also has
3557
02:17:37,019 --> 02:17:39,120
still room for improvement when it comes
3558
02:17:39,120 --> 02:17:41,340
to error handling for one we don't
3559
02:17:41,340 --> 02:17:43,200
really know if the node ID we passed
3560
02:17:43,200 --> 02:17:45,599
here is even a valid node idea or if we
3561
02:17:45,599 --> 02:17:48,718
are trying to use find by idea with some
3562
02:17:48,718 --> 02:17:50,879
random value that doesn't make sense but
3563
02:17:50,879 --> 02:17:53,218
even if the node ID is valid but doesn't
3564
02:17:53,218 --> 02:17:56,160
exist the error we get back is just no
3565
02:17:56,160 --> 02:17:58,859
but the short return of 404 response and
3566
02:17:58,859 --> 02:18:01,019
a proper error message so let's improve
3567
02:18:01,019 --> 02:18:02,398
this
3568
02:18:02,398 --> 02:18:05,040
so let's start with checking if we
3569
02:18:05,040 --> 02:18:07,260
actually get the notes back here or if
3570
02:18:07,260 --> 02:18:09,718
it doesn't exist so we simply do an if
3571
02:18:09,718 --> 02:18:12,898
check if exclamation mark node so if the
3572
02:18:12,898 --> 02:18:16,019
node is neither or undefined
3573
02:18:16,019 --> 02:18:18,840
then we want to throw a create HTTP
3574
02:18:18,840 --> 02:18:20,040
error
3575
02:18:20,040 --> 02:18:22,260
with a 404 code
3576
02:18:22,260 --> 02:18:25,138
that says node not found
3577
02:18:25,138 --> 02:18:27,120
which is much more readable than this
3578
02:18:27,120 --> 02:18:29,580
noise we had before
3579
02:18:29,580 --> 02:18:32,218
let's try it out let's save it
3580
02:18:32,218 --> 02:18:34,679
and try to access the same endpoint as
3581
02:18:34,679 --> 02:18:35,939
before
3582
02:18:35,939 --> 02:18:38,280
which now shows the appropriate error
3583
02:18:38,280 --> 02:18:39,959
message
3584
02:18:39,959 --> 02:18:43,080
with a 404 code okay so this is already
3585
02:18:43,080 --> 02:18:44,340
much better
3586
02:18:44,340 --> 02:18:47,638
but what happens if we pass a value for
3587
02:18:47,638 --> 02:18:49,080
the node ID that doesn't make sense
3588
02:18:49,080 --> 02:18:51,478
because it doesn't adhere to the shape
3589
02:18:51,478 --> 02:18:53,160
that these nodes IDs have to have
3590
02:18:53,160 --> 02:18:55,320
because they need a certain length and
3591
02:18:55,320 --> 02:18:56,939
certain kind of characters are allowed
3592
02:18:56,939 --> 02:18:59,340
so if we just remove some of these
3593
02:18:59,340 --> 02:19:01,320
characters here and try to access it
3594
02:19:01,320 --> 02:19:03,478
like this we get our good old and
3595
02:19:03,478 --> 02:19:06,179
unknown error occurred in a 500 response
3596
02:19:06,179 --> 02:19:09,500
this happens because
3597
02:19:10,040 --> 02:19:13,978
doesn't know how to uh handle this oddly
3598
02:19:13,978 --> 02:19:16,019
shaped note idea it doesn't have the
3599
02:19:16,019 --> 02:19:18,959
expected format here for find by idea
3600
02:19:18,959 --> 02:19:21,478
but we can check it ourselves and then
3601
02:19:21,478 --> 02:19:23,879
return an appropriate error message and
3602
02:19:23,879 --> 02:19:26,218
Status quote if the shape is not what we
3603
02:19:26,218 --> 02:19:29,099
expected so above the line where we call
3604
02:19:29,099 --> 02:19:30,599
finds by idea
3605
02:19:30,599 --> 02:19:32,580
we check if
3606
02:19:32,580 --> 02:19:36,120
exclamation mark then we take mongoose
3607
02:19:36,120 --> 02:19:39,420
is a valid object idea
3608
02:19:39,420 --> 02:19:42,120
we could also import a spelled object ID
3609
02:19:42,120 --> 02:19:44,099
directly then we don't need this
3610
02:19:44,099 --> 02:19:45,840
stored in front of it but this way I
3611
02:19:45,840 --> 02:19:47,580
think it's more readable because we know
3612
02:19:47,580 --> 02:19:49,680
okay this is a function from the
3613
02:19:49,680 --> 02:19:51,240
Mongoose package
3614
02:19:51,240 --> 02:19:54,300
here we pass the note idea
3615
02:19:54,300 --> 02:19:56,700
which is the URL argument if you
3616
02:19:56,700 --> 02:19:58,920
remember it's this part here and this
3617
02:19:58,920 --> 02:20:00,840
time we actually don't have to check if
3618
02:20:00,840 --> 02:20:03,060
this value is present or not because if
3619
02:20:03,060 --> 02:20:05,160
we don't enter anything here behind the
3620
02:20:05,160 --> 02:20:07,020
slash then we don't get to this endpoint
3621
02:20:07,020 --> 02:20:08,880
in the first place then we get to the
3622
02:20:08,880 --> 02:20:11,040
normal get endpoint so we don't have to
3623
02:20:11,040 --> 02:20:13,080
check if this value is null or not
3624
02:20:13,080 --> 02:20:15,300
because if we end up in this Handler
3625
02:20:15,300 --> 02:20:17,460
then we know that there is a node ID and
3626
02:20:17,460 --> 02:20:19,320
it also has the type string by default
3627
02:20:19,320 --> 02:20:21,540
so typescript knows that there is a
3628
02:20:21,540 --> 02:20:23,760
value when we get to this endpoint but
3629
02:20:23,760 --> 02:20:26,399
if this is not a valid object idea which
3630
02:20:26,399 --> 02:20:28,020
is what this function here checks for
3631
02:20:28,020 --> 02:20:30,420
returns true if Mongoose can cast the
3632
02:20:30,420 --> 02:20:33,300
given value to an object idea or false
3633
02:20:33,300 --> 02:20:35,820
otherwise so if it's misshaped like we
3634
02:20:35,820 --> 02:20:38,520
saw before then we want to throw another
3635
02:20:38,520 --> 02:20:41,220
create HTTP error
3636
02:20:41,220 --> 02:20:44,939
a 400 response here is appropriate
3637
02:20:44,939 --> 02:20:47,880
and error message will be a invalid node
3638
02:20:47,880 --> 02:20:49,920
idea
3639
02:20:49,920 --> 02:20:52,200
okay let's save this and try it out once
3640
02:20:52,200 --> 02:20:54,000
again so we go to the same wrong
3641
02:20:54,000 --> 02:20:55,500
endpoint here
3642
02:20:55,500 --> 02:20:57,000
refresh this
3643
02:20:57,000 --> 02:20:58,740
and this time we have a good error
3644
02:20:58,740 --> 02:21:01,500
message invalid node idea and the 400
3645
02:21:01,500 --> 02:21:02,880
response
3646
02:21:02,880 --> 02:21:04,979
so all of this is not really necessary
3647
02:21:04,979 --> 02:21:08,040
but it's got practice because otherwise
3648
02:21:08,040 --> 02:21:09,899
we end up with a silver with error
3649
02:21:09,899 --> 02:21:11,880
messages that we don't understand and we
3650
02:21:11,880 --> 02:21:13,920
never really know what's going wrong and
3651
02:21:13,920 --> 02:21:16,080
it's also good to not let any values
3652
02:21:16,080 --> 02:21:17,640
through it that we can't really work
3653
02:21:17,640 --> 02:21:20,160
with because even though nothing really
3654
02:21:20,160 --> 02:21:22,260
bad happens here besides an error that's
3655
02:21:22,260 --> 02:21:24,060
not really readable it could cause
3656
02:21:24,060 --> 02:21:26,160
problems in different parts of the code
3657
02:21:26,160 --> 02:21:28,260
because we are working with values that
3658
02:21:28,260 --> 02:21:30,420
don't have the type or the shape that we
3659
02:21:30,420 --> 02:21:33,000
expect okay that's it for the error
3660
02:21:33,000 --> 02:21:34,800
handling setup for now in the next
3661
02:21:34,800 --> 02:21:36,899
section we will add endpoints to update
3662
02:21:36,899 --> 02:21:41,240
and delete existing nodes very exciting
3663
02:21:44,460 --> 02:21:47,160
okay now let's add end points for
3664
02:21:47,160 --> 02:21:49,140
updating and deleting notes to our
3665
02:21:49,140 --> 02:21:51,540
server so we go into our notes
3666
02:21:51,540 --> 02:21:53,399
controller file
3667
02:21:53,399 --> 02:21:55,020
and where we put it doesn't really
3668
02:21:55,020 --> 02:21:57,960
matter but let's organize them in an
3669
02:21:57,960 --> 02:22:00,000
order that makes sense so I put it at
3670
02:22:00,000 --> 02:22:02,760
the bottom below create node we create
3671
02:22:02,760 --> 02:22:06,060
an export const
3672
02:22:06,060 --> 02:22:09,140
update node
3673
02:22:09,240 --> 02:22:14,359
of type request Handler just as before
3674
02:22:14,399 --> 02:22:18,420
and it's an async function
3675
02:22:18,420 --> 02:22:21,359
that takes a request response and next
3676
02:22:21,359 --> 02:22:25,020
function as arguments
3677
02:22:25,020 --> 02:22:27,660
just like the other ones
3678
02:22:27,660 --> 02:22:29,460
and in here we will also need a try
3679
02:22:29,460 --> 02:22:31,020
catch block
3680
02:22:31,020 --> 02:22:32,760
and this one will be interesting because
3681
02:22:32,760 --> 02:22:35,160
it's basically a combination of our
3682
02:22:35,160 --> 02:22:37,859
create note endpoint because we need to
3683
02:22:37,859 --> 02:22:40,380
pass a title in the text but also our
3684
02:22:40,380 --> 02:22:42,780
get node endpoint because we need the
3685
02:22:42,780 --> 02:22:45,780
node idea that we want to update
3686
02:22:45,780 --> 02:22:48,660
so let's next go into the routes file
3687
02:22:48,660 --> 02:22:50,700
and add this new endpoint here before we
3688
02:22:50,700 --> 02:22:55,680
forget it let's put it below router.post
3689
02:22:55,680 --> 02:22:58,380
this time it's a patch request
3690
02:22:58,380 --> 02:23:01,319
in HTTP patch request is used whenever
3691
02:23:01,319 --> 02:23:03,600
you want to update a resource
3692
02:23:03,600 --> 02:23:06,420
we set an endpoint for it
3693
02:23:06,420 --> 02:23:09,300
which will be slash and then the same as
3694
02:23:09,300 --> 02:23:10,500
up here
3695
02:23:10,500 --> 02:23:12,899
call on node idea because we need to
3696
02:23:12,899 --> 02:23:15,180
specify which node we want to update and
3697
02:23:15,180 --> 02:23:17,939
we do this over this URL argument comma
3698
02:23:17,939 --> 02:23:20,460
and then repairs notescontroller dot
3699
02:23:20,460 --> 02:23:22,560
update node
3700
02:23:22,560 --> 02:23:24,540
we saved this but of course we haven't
3701
02:23:24,540 --> 02:23:27,240
implemented this function here
3702
02:23:27,240 --> 02:23:30,540
so let's go back to our routes file
3703
02:23:30,540 --> 02:23:32,880
we want to declare the body here the
3704
02:23:32,880 --> 02:23:35,040
same as we did for create node so that
3705
02:23:35,040 --> 02:23:37,680
we have types for title and text and we
3706
02:23:37,680 --> 02:23:40,200
can check them properly
3707
02:23:40,200 --> 02:23:42,899
so above this function
3708
02:23:42,899 --> 02:23:46,200
we create an interface which we call
3709
02:23:46,200 --> 02:23:49,620
update node audio and then here we
3710
02:23:49,620 --> 02:23:52,140
Define the variables that we expect in
3711
02:23:52,140 --> 02:23:55,020
the body which the same as before is an
3712
02:23:55,020 --> 02:23:56,880
optional title
3713
02:23:56,880 --> 02:23:59,700
and an optional text because we can't be
3714
02:23:59,700 --> 02:24:01,620
sure that we actually get past these
3715
02:24:01,620 --> 02:24:04,380
values by whoever calls this endpoint so
3716
02:24:04,380 --> 02:24:06,540
I basically always make this body
3717
02:24:06,540 --> 02:24:08,880
variables optional with this question
3718
02:24:08,880 --> 02:24:11,700
mark they can always be undefined okay
3719
02:24:11,700 --> 02:24:14,880
and I set up here
3720
02:24:14,880 --> 02:24:17,640
in get node we don't have to Define an
3721
02:24:17,640 --> 02:24:19,859
interface for the node idea because
3722
02:24:19,859 --> 02:24:21,960
typescript knows that this is definitely
3723
02:24:21,960 --> 02:24:24,720
a string because it's contained in the
3724
02:24:24,720 --> 02:24:27,120
URL that we are writing out
3725
02:24:27,120 --> 02:24:29,640
now we have to declare an interface for
3726
02:24:29,640 --> 02:24:32,100
it why because we want to pass the
3727
02:24:32,100 --> 02:24:34,260
update node body as the type to request
3728
02:24:34,260 --> 02:24:36,899
Handler but we can't pass unknown for
3729
02:24:36,899 --> 02:24:38,939
the URL params as we did up here because
3730
02:24:38,939 --> 02:24:40,979
now we want to use them now we want to
3731
02:24:40,979 --> 02:24:43,560
use the node idea that we pass via the
3732
02:24:43,560 --> 02:24:46,380
URL so we can't set this type to unknown
3733
02:24:46,380 --> 02:24:48,540
this time we have to set it to something
3734
02:24:48,540 --> 02:24:50,760
specifically because again this is
3735
02:24:50,760 --> 02:24:52,859
either all or nothing either we declare
3736
02:24:52,859 --> 02:24:55,140
no type here in this request Handler or
3737
02:24:55,140 --> 02:24:57,120
we have to Define all that we want to
3738
02:24:57,120 --> 02:25:00,260
use so we call this update node
3739
02:25:00,260 --> 02:25:04,740
params because those are the URL params
3740
02:25:04,740 --> 02:25:07,560
and we expected the node idea to be
3741
02:25:07,560 --> 02:25:10,080
passed over the URL programs of type
3742
02:25:10,080 --> 02:25:12,780
string it has to have the same name as
3743
02:25:12,780 --> 02:25:15,180
the argument here that we put in the URL
3744
02:25:15,180 --> 02:25:18,000
and again we don't have to make it
3745
02:25:18,000 --> 02:25:19,920
possibly undefined we don't have to add
3746
02:25:19,920 --> 02:25:22,500
this question mark here because if we
3747
02:25:22,500 --> 02:25:24,359
would not pass this value here we
3748
02:25:24,359 --> 02:25:25,800
wouldn't get to this endpoint in the
3749
02:25:25,800 --> 02:25:27,899
first place we wouldn't end up here
3750
02:25:27,899 --> 02:25:30,780
because this requires a slash and a
3751
02:25:30,780 --> 02:25:33,000
value for the node ID Alright and then
3752
02:25:33,000 --> 02:25:35,640
we put these two new types on our
3753
02:25:35,640 --> 02:25:37,800
request Handler so again a pair of angle
3754
02:25:37,800 --> 02:25:40,380
brackets the first one is update node
3755
02:25:40,380 --> 02:25:41,700
params
3756
02:25:41,700 --> 02:25:43,800
comma unknown
3757
02:25:43,800 --> 02:25:46,920
because the second one is the response
3758
02:25:46,920 --> 02:25:48,840
body which we don't want to add a type
3759
02:25:48,840 --> 02:25:51,300
to because we don't need it the third
3760
02:25:51,300 --> 02:25:53,819
one is the update node body and the
3761
02:25:53,819 --> 02:25:56,399
fourth one is unknown yet again because
3762
02:25:56,399 --> 02:26:00,060
the fourth one is the URL query params
3763
02:26:00,060 --> 02:26:02,160
that we are not using here either all
3764
02:26:02,160 --> 02:26:04,319
right that now we can get these three
3765
02:26:04,319 --> 02:26:08,160
values out of our request objects
3766
02:26:08,160 --> 02:26:10,800
we create a variable node idea which is
3767
02:26:10,800 --> 02:26:12,420
the part in the URL
3768
02:26:12,420 --> 02:26:14,840
which we get over
3769
02:26:14,840 --> 02:26:18,540
rec.params dot nodeid Auto completion
3770
02:26:18,540 --> 02:26:20,819
works because we are defined it in this
3771
02:26:20,819 --> 02:26:24,140
update node params type
3772
02:26:24,600 --> 02:26:27,359
cons let's call it new title just to
3773
02:26:27,359 --> 02:26:29,040
make the variable name a bit more
3774
02:26:29,040 --> 02:26:31,319
descriptive because we are updating we
3775
02:26:31,319 --> 02:26:33,200
are not creating a new node
3776
02:26:33,200 --> 02:26:36,240
rec.body this time not params but body
3777
02:26:36,240 --> 02:26:39,120
because we send the title in the body
3778
02:26:39,120 --> 02:26:42,180
dot title
3779
02:26:42,180 --> 02:26:43,920
and by the way in case you're wondering
3780
02:26:43,920 --> 02:26:46,260
what kind of data you should pass over
3781
02:26:46,260 --> 02:26:48,899
the body and what you pass over the URL
3782
02:26:48,899 --> 02:26:51,180
it depends a bit and also it depends on
3783
02:26:51,180 --> 02:26:53,819
convention we could also send the title
3784
02:26:53,819 --> 02:26:56,040
and the text theoretically over the URL
3785
02:26:56,040 --> 02:26:58,319
but this would be very unwieldy because
3786
02:26:58,319 --> 02:27:00,479
it gets very long and we have to URL
3787
02:27:00,479 --> 02:27:03,600
encoded and stuff like that so it's more
3788
02:27:03,600 --> 02:27:05,520
appropriate to send the title and the
3789
02:27:05,520 --> 02:27:07,979
text via the body and use the note idea
3790
02:27:07,979 --> 02:27:10,380
as the identifier in the URL to a
3791
02:27:10,380 --> 02:27:12,600
pinpoint to our exact nodes that we want
3792
02:27:12,600 --> 02:27:13,680
to update
3793
02:27:13,680 --> 02:27:16,500
okay and another variable for the text
3794
02:27:16,500 --> 02:27:21,120
which is contained in request.body.txt
3795
02:27:21,120 --> 02:27:23,580
so the first one is a string and the
3796
02:27:23,580 --> 02:27:25,979
other ones are string or undefined
3797
02:27:25,979 --> 02:27:27,660
and then we want to do some checks here
3798
02:27:27,660 --> 02:27:29,280
to have appropriate error handling
3799
02:27:29,280 --> 02:27:31,740
similar checks as we already did up here
3800
02:27:31,740 --> 02:27:34,439
so we can actually copy some stuff
3801
02:27:34,439 --> 02:27:36,600
we want to check if the node ID that we
3802
02:27:36,600 --> 02:27:39,000
pass in the UL is actually a valid node
3803
02:27:39,000 --> 02:27:41,580
idea just like we did for get nodes so
3804
02:27:41,580 --> 02:27:43,260
let's copy this part
3805
02:27:43,260 --> 02:27:45,960
and pastity on the try block
3806
02:27:45,960 --> 02:27:47,700
then we also want to check if we
3807
02:27:47,700 --> 02:27:49,979
actually passed the title
3808
02:27:49,979 --> 02:27:52,560
which we already did in create node so
3809
02:27:52,560 --> 02:27:54,359
let's copy this line
3810
02:27:54,359 --> 02:27:56,939
paste it below but we have to change
3811
02:27:56,939 --> 02:27:58,439
this to a
3812
02:27:58,439 --> 02:28:00,479
new title because this is the variable
3813
02:28:00,479 --> 02:28:02,340
name we can keep the error message to
3814
02:28:02,340 --> 02:28:04,800
the same you can also change it like you
3815
02:28:04,800 --> 02:28:06,720
can't update it node without the title
3816
02:28:06,720 --> 02:28:09,300
or whatever but this here is fine then
3817
02:28:09,300 --> 02:28:10,680
we want to get the node from the
3818
02:28:10,680 --> 02:28:14,340
database so we write const node equals
3819
02:28:14,340 --> 02:28:16,080
await
3820
02:28:16,080 --> 02:28:19,920
note model dot find by ideal
3821
02:28:19,920 --> 02:28:22,500
and repairs the node ID
3822
02:28:22,500 --> 02:28:24,899
and let's not forget the exact color to
3823
02:28:24,899 --> 02:28:27,359
turn this into a real promise
3824
02:28:27,359 --> 02:28:31,200
the same as we did up here already in
3825
02:28:31,200 --> 02:28:33,479
the get node endpoint and the same as up
3826
02:28:33,479 --> 02:28:35,100
here we also want to check if this node
3827
02:28:35,100 --> 02:28:37,319
exists
3828
02:28:37,319 --> 02:28:40,080
and show a different error message if it
3829
02:28:40,080 --> 02:28:40,859
doesn't
3830
02:28:40,859 --> 02:28:42,899
so as you can see there's a lot of error
3831
02:28:42,899 --> 02:28:44,700
handling involved here but this just
3832
02:28:44,700 --> 02:28:46,620
makes sure that we always return an
3833
02:28:46,620 --> 02:28:48,600
appropriate error message and code for
3834
02:28:48,600 --> 02:28:50,520
all the different situations
3835
02:28:50,520 --> 02:28:52,620
if we end up down here then everything
3836
02:28:52,620 --> 02:28:55,020
was fine we can go ahead and update the
3837
02:28:55,020 --> 02:28:57,300
node so we can take the node object that
3838
02:28:57,300 --> 02:28:59,580
we already have here
3839
02:28:59,580 --> 02:29:04,080
and just set the title to a newer title
3840
02:29:04,080 --> 02:29:09,300
NZ node.txt to a new text
3841
02:29:09,300 --> 02:29:12,720
and then below we'll save the changes
3842
02:29:12,720 --> 02:29:15,240
but we store the changes in a variable
3843
02:29:15,240 --> 02:29:17,880
again so that we can return it to the
3844
02:29:17,880 --> 02:29:20,160
color of this endpoint which allows us
3845
02:29:20,160 --> 02:29:22,140
later to update the node on the screen
3846
02:29:22,140 --> 02:29:24,240
in our react app because we get the
3847
02:29:24,240 --> 02:29:26,280
updated note back we don't have to fetch
3848
02:29:26,280 --> 02:29:28,560
it again so we create a cons updated
3849
02:29:28,560 --> 02:29:29,580
node
3850
02:29:29,580 --> 02:29:31,800
equals a weight
3851
02:29:31,800 --> 02:29:34,560
note dot safe
3852
02:29:34,560 --> 02:29:36,180
like this
3853
02:29:36,180 --> 02:29:38,580
this as the name implies saves the
3854
02:29:38,580 --> 02:29:40,319
changes that we made to the node up here
3855
02:29:40,319 --> 02:29:42,359
if there is any error that happens then
3856
02:29:42,359 --> 02:29:44,100
it will throw we will catch it and
3857
02:29:44,100 --> 02:29:45,660
return it to our error Handler there's
3858
02:29:45,660 --> 02:29:48,600
also another way to update the node we
3859
02:29:48,600 --> 02:29:50,580
could also do it with
3860
02:29:50,580 --> 02:29:53,760
node model dot finds by ID and update
3861
02:29:53,760 --> 02:29:56,160
then you can pass the node idea and it
3862
02:29:56,160 --> 02:29:58,140
changes as a second argument this is
3863
02:29:58,140 --> 02:30:00,359
also an option but this will look up the
3864
02:30:00,359 --> 02:30:02,399
note again but we already have a handle
3865
02:30:02,399 --> 02:30:04,560
to the node because we are fetched it to
3866
02:30:04,560 --> 02:30:06,780
check if it exists right so there's no
3867
02:30:06,780 --> 02:30:08,819
reason to fetch the node again and we
3868
02:30:08,819 --> 02:30:11,700
can just use the save method here all
3869
02:30:11,700 --> 02:30:14,880
right and now we want to return the
3870
02:30:14,880 --> 02:30:17,520
updated nodes to the color so recall
3871
02:30:17,520 --> 02:30:19,859
rest dot status
3872
02:30:19,859 --> 02:30:22,140
pass a 200 code
3873
02:30:22,140 --> 02:30:26,640
dot Json and SD Json body we return the
3874
02:30:26,640 --> 02:30:30,000
updated note and let's also
3875
02:30:30,000 --> 02:30:33,000
to our next error call down here to a
3876
02:30:33,000 --> 02:30:35,040
card error Handler in case there is an
3877
02:30:35,040 --> 02:30:38,100
error but here I did a typo I passed
3878
02:30:38,100 --> 02:30:40,380
update node which is the name of this
3879
02:30:40,380 --> 02:30:42,780
function it's important that we pass
3880
02:30:42,780 --> 02:30:45,300
updated node which is the actual node
3881
02:30:45,300 --> 02:30:47,160
that we get back after saving the
3882
02:30:47,160 --> 02:30:49,920
changes okay let's also save the changes
3883
02:30:49,920 --> 02:30:52,620
to this file and then try it out
3884
02:30:52,620 --> 02:30:55,560
let's open Postman
3885
02:30:55,560 --> 02:30:58,260
change this to a patch request
3886
02:30:58,260 --> 02:31:00,899
and then let's take any idea of our
3887
02:31:00,899 --> 02:31:03,500
notes Here
3888
02:31:03,540 --> 02:31:06,420
let's use my first endpoint note we need
3889
02:31:06,420 --> 02:31:08,160
the object idea because we want to
3890
02:31:08,160 --> 02:31:10,260
update this
3891
02:31:10,260 --> 02:31:13,620
we pass this here as the URL argument
3892
02:31:13,620 --> 02:31:16,979
and we need to pass a body so now if the
3893
02:31:16,979 --> 02:31:18,780
title is missing it should still show
3894
02:31:18,780 --> 02:31:21,000
the error message that we need a title
3895
02:31:21,000 --> 02:31:23,640
for the node node must have a title so
3896
02:31:23,640 --> 02:31:26,040
this works as expected
3897
02:31:26,040 --> 02:31:28,800
what other errors did we Define here
3898
02:31:28,800 --> 02:31:31,380
invalid node ID Let's test this as well
3899
02:31:31,380 --> 02:31:33,780
let's put
3900
02:31:33,780 --> 02:31:36,859
a title in here
3901
02:31:37,560 --> 02:31:40,380
let's change this to updated title but
3902
02:31:40,380 --> 02:31:43,680
usern invalid idea
3903
02:31:43,680 --> 02:31:46,859
send invalid node idea let's use one
3904
02:31:46,859 --> 02:31:48,780
that is valid but
3905
02:31:48,780 --> 02:31:51,120
not found so I replace the last letter
3906
02:31:51,120 --> 02:31:54,000
for idea node not found
3907
02:31:54,000 --> 02:31:56,819
but if we use the correct node idea then
3908
02:31:56,819 --> 02:31:59,220
the update should go through
3909
02:31:59,220 --> 02:32:01,500
and we should see the update reflected
3910
02:32:01,500 --> 02:32:04,680
in our database so one more time
3911
02:32:04,680 --> 02:32:06,899
the update went through we get our
3912
02:32:06,899 --> 02:32:09,240
updated note back with the updated title
3913
02:32:09,240 --> 02:32:11,460
and now when we look into mongodb again
3914
02:32:11,460 --> 02:32:13,560
and refresh this
3915
02:32:13,560 --> 02:32:15,720
we should see the changes update the
3916
02:32:15,720 --> 02:32:18,060
title isn't that amazing but of course
3917
02:32:18,060 --> 02:32:19,680
in the future we want to do this over
3918
02:32:19,680 --> 02:32:22,859
our react app over our real website but
3919
02:32:22,859 --> 02:32:25,319
right now we do it via Postman and this
3920
02:32:25,319 --> 02:32:27,960
is already pretty cool okay then next
3921
02:32:27,960 --> 02:32:30,240
let's add the delete endpoint which will
3922
02:32:30,240 --> 02:32:33,000
be a bit shorter so let's put it below
3923
02:32:33,000 --> 02:32:34,740
our update node
3924
02:32:34,740 --> 02:32:36,780
endpoint function
3925
02:32:36,780 --> 02:32:38,880
export const
3926
02:32:38,880 --> 02:32:40,920
and by now it is the second nature for
3927
02:32:40,920 --> 02:32:41,939
us
3928
02:32:41,939 --> 02:32:45,840
delete node of type request Handler
3929
02:32:45,840 --> 02:32:49,260
it's an async error function
3930
02:32:49,260 --> 02:32:53,720
with our good old three arguments here
3931
02:32:54,960 --> 02:32:58,140
we will also identify the node we want
3932
02:32:58,140 --> 02:33:01,080
to delete by its ID in the URL
3933
02:33:01,080 --> 02:33:05,300
so we create a const node idea equals
3934
02:33:05,300 --> 02:33:07,740
request.params.note idea and this time
3935
02:33:07,740 --> 02:33:09,479
since we don't need a body for the
3936
02:33:09,479 --> 02:33:11,520
delete color we don't have to pass any
3937
02:33:11,520 --> 02:33:13,620
type arguments here and we can use the
3938
02:33:13,620 --> 02:33:17,580
default string type for URL Paramus
3939
02:33:17,580 --> 02:33:19,680
try catch
3940
02:33:19,680 --> 02:33:22,859
in the catch block because our error
3941
02:33:22,859 --> 02:33:25,740
Handler over the next function
3942
02:33:25,740 --> 02:33:28,439
and then in the try block we still have
3943
02:33:28,439 --> 02:33:30,780
to check if it's a valid idea that we
3944
02:33:30,780 --> 02:33:32,280
are passing here
3945
02:33:32,280 --> 02:33:36,140
so let's copy this part here again
3946
02:33:38,280 --> 02:33:39,899
then we want to check if the node
3947
02:33:39,899 --> 02:33:42,540
actually exists you can do this step you
3948
02:33:42,540 --> 02:33:44,520
could also just return a success
3949
02:33:44,520 --> 02:33:46,800
response in both cases either if it was
3950
02:33:46,800 --> 02:33:48,780
deleted successfully or if it didn't
3951
02:33:48,780 --> 02:33:50,340
exist in the first place there are
3952
02:33:50,340 --> 02:33:52,080
different opinions about this but I
3953
02:33:52,080 --> 02:33:54,120
think it makes sense to throw an error
3954
02:33:54,120 --> 02:33:55,500
message if this node doesn't actually
3955
02:33:55,500 --> 02:33:57,960
exist because we expected that it was
3956
02:33:57,960 --> 02:33:59,520
there but it wasn't
3957
02:33:59,520 --> 02:34:01,800
so as the first step we look up this
3958
02:34:01,800 --> 02:34:04,319
node and save it in the variable so
3959
02:34:04,319 --> 02:34:06,060
await node model
3960
02:34:06,060 --> 02:34:08,460
and again these awaits are important if
3961
02:34:08,460 --> 02:34:10,080
you forget them then the return type
3962
02:34:10,080 --> 02:34:12,979
will not be correct
3963
02:34:14,220 --> 02:34:17,040
node model finds by ideal
3964
02:34:17,040 --> 02:34:19,560
replace the node ID param
3965
02:34:19,560 --> 02:34:21,780
and carl.exec
3966
02:34:21,780 --> 02:34:24,780
then below we check if exclamation mark
3967
02:34:24,780 --> 02:34:27,359
node so if this node is null or
3968
02:34:27,359 --> 02:34:29,100
undefined
3969
02:34:29,100 --> 02:34:32,160
then we want to throw a create HTTP
3970
02:34:32,160 --> 02:34:34,680
error and since the note wasn't found we
3971
02:34:34,680 --> 02:34:37,680
set the status code to 404
3972
02:34:37,680 --> 02:34:42,740
and the message will be a note not found
3973
02:34:43,620 --> 02:34:45,899
if the note was found then we go ahead
3974
02:34:45,899 --> 02:34:47,420
and call away
3975
02:34:47,420 --> 02:34:50,880
node.remove which deletes it again there
3976
02:34:50,880 --> 02:34:52,620
is an alternative for that which would
3977
02:34:52,620 --> 02:34:55,200
be a node model dot find by ID and
3978
02:34:55,200 --> 02:34:58,200
delete or remove I'm not sure right now
3979
02:34:58,200 --> 02:35:00,300
which is the appropriate one you have to
3980
02:35:00,300 --> 02:35:01,859
look into the documentation I think it's
3981
02:35:01,859 --> 02:35:04,080
delete but since we already have a
3982
02:35:04,080 --> 02:35:06,600
reference to the node it would be an
3983
02:35:06,600 --> 02:35:08,520
unnecessary step towards hatchet from
3984
02:35:08,520 --> 02:35:12,240
the database again okay so no dot remove
3985
02:35:12,240 --> 02:35:15,359
and then Ria return a status code this
3986
02:35:15,359 --> 02:35:17,640
time we don't need a body if we know on
3987
02:35:17,640 --> 02:35:19,319
the front end that the deletion was
3988
02:35:19,319 --> 02:35:21,660
successful we can just remove it we
3989
02:35:21,660 --> 02:35:23,580
don't need the body of the old node for
3990
02:35:23,580 --> 02:35:25,920
anything so instead of rest dot status
3991
02:35:25,920 --> 02:35:30,000
this time we call send status
3992
02:35:30,000 --> 02:35:33,240
with the code 204 which if I remember
3993
02:35:33,240 --> 02:35:35,939
correctly means deletion successful or
3994
02:35:35,939 --> 02:35:38,040
something like that it's important that
3995
02:35:38,040 --> 02:35:40,319
you use send status instead of status
3996
02:35:40,319 --> 02:35:42,180
this time like we used in these other
3997
02:35:42,180 --> 02:35:44,880
endpoints why because status itself
3998
02:35:44,880 --> 02:35:47,939
doesn't send a response Json is the card
3999
02:35:47,939 --> 02:35:49,680
that's responsible for actually sending
4000
02:35:49,680 --> 02:35:51,899
the response but since we don't send a
4001
02:35:51,899 --> 02:35:54,300
Json body down here we have to call Zen
4002
02:35:54,300 --> 02:35:57,720
status to set the status but also send
4003
02:35:57,720 --> 02:35:59,880
it at the same time so in other words if
4004
02:35:59,880 --> 02:36:02,280
you don't add Json then you send status
4005
02:36:02,280 --> 02:36:03,960
save
4006
02:36:03,960 --> 02:36:06,780
okay let's not forget to add this end
4007
02:36:06,780 --> 02:36:09,479
point in our routes file here
4008
02:36:09,479 --> 02:36:11,640
so our router dot delete this time
4009
02:36:11,640 --> 02:36:15,359
delete is another HTTP verb and the URL
4010
02:36:15,359 --> 02:36:19,939
will be slash and again the node ID
4011
02:36:19,939 --> 02:36:23,520
entify the skull to nodes controller dot
4012
02:36:23,520 --> 02:36:25,500
delete node
4013
02:36:25,500 --> 02:36:27,420
save this and then try it out with
4014
02:36:27,420 --> 02:36:29,460
Postman
4015
02:36:29,460 --> 02:36:31,740
so this node ID up here should still be
4016
02:36:31,740 --> 02:36:35,580
valid we change the verb to a delete and
4017
02:36:35,580 --> 02:36:38,280
let's go through our different arrows so
4018
02:36:38,280 --> 02:36:40,319
let's try and invalid
4019
02:36:40,319 --> 02:36:42,060
node idea
4020
02:36:42,060 --> 02:36:44,760
invalid node idea let's use a
4021
02:36:44,760 --> 02:36:47,819
non-existing one node not found
4022
02:36:47,819 --> 02:36:50,520
but as soon as we pass the correct node
4023
02:36:50,520 --> 02:36:53,040
idea this node should get deleted and
4024
02:36:53,040 --> 02:36:55,319
removed from our database
4025
02:36:55,319 --> 02:36:57,899
so zent we don't get a response back
4026
02:36:57,899 --> 02:37:00,060
because we only send a status code but
4027
02:37:00,060 --> 02:37:02,399
the status code tells us that this was
4028
02:37:02,399 --> 02:37:04,319
successful which means that on the front
4029
02:37:04,319 --> 02:37:06,300
end when we get this back we know okay
4030
02:37:06,300 --> 02:37:08,340
our note was deleted and we can remove
4031
02:37:08,340 --> 02:37:09,840
it from the UI
4032
02:37:09,840 --> 02:37:11,880
and when we look into the database and
4033
02:37:11,880 --> 02:37:13,680
refresh it this node here with the
4034
02:37:13,680 --> 02:37:15,840
updated title should be gone
4035
02:37:15,840 --> 02:37:18,660
so refresh this
4036
02:37:18,660 --> 02:37:21,600
and where is that moment our notice gun
4037
02:37:21,600 --> 02:37:24,720
farewell old friend okay in the future
4038
02:37:24,720 --> 02:37:26,939
of course we want to protect this via
4039
02:37:26,939 --> 02:37:29,280
authentication we don't want to allow
4040
02:37:29,280 --> 02:37:31,319
anyone to delete any nodes nearly
4041
02:37:31,319 --> 02:37:33,240
villier any user should only be allowed
4042
02:37:33,240 --> 02:37:34,920
to delete their own nodes but this is
4043
02:37:34,920 --> 02:37:37,859
something we will add in a later section
4044
02:37:37,859 --> 02:37:40,380
of this tutorial for now we will leave
4045
02:37:40,380 --> 02:37:42,780
our server aside a bit because it starts
4046
02:37:42,780 --> 02:37:44,640
getting boring right adding more and
4047
02:37:44,640 --> 02:37:46,740
more endpoints that's lame in the next
4048
02:37:46,740 --> 02:37:48,840
section we will actually start building
4049
02:37:48,840 --> 02:37:50,939
our react app we will get back to the
4050
02:37:50,939 --> 02:37:52,439
server here later because we aren't
4051
02:37:52,439 --> 02:37:54,420
finished yet but now it's time to write
4052
02:37:54,420 --> 02:37:58,460
some react code because that's fun
4053
02:38:01,740 --> 02:38:04,560
all right to create our react app let's
4054
02:38:04,560 --> 02:38:06,540
go into the folder where we also put the
4055
02:38:06,540 --> 02:38:08,700
backend code let's put it in here as
4056
02:38:08,700 --> 02:38:10,439
well you can also put it somewhere else
4057
02:38:10,439 --> 02:38:12,600
I do this so I can push them to the same
4058
02:38:12,600 --> 02:38:14,580
GitHub repository so you can find
4059
02:38:14,580 --> 02:38:16,560
everything in one place now when you set
4060
02:38:16,560 --> 02:38:19,140
up a newer vanilla react project then
4061
02:38:19,140 --> 02:38:21,240
you should do this over the command line
4062
02:38:21,240 --> 02:38:23,520
with this special create react app
4063
02:38:23,520 --> 02:38:25,740
command which you can find in the docs
4064
02:38:25,740 --> 02:38:28,319
or if you just type in create react app
4065
02:38:28,319 --> 02:38:30,479
in Google but we basically just need
4066
02:38:30,479 --> 02:38:32,640
this command here you can also type it
4067
02:38:32,640 --> 02:38:34,800
out by hand instead of copying it
4068
02:38:34,800 --> 02:38:37,680
let's do that let's go into this folder
4069
02:38:37,680 --> 02:38:39,660
open the command line here and again on
4070
02:38:39,660 --> 02:38:41,220
Windows you do this by holding shift
4071
02:38:41,220 --> 02:38:46,460
down right clicking open Powershell here
4072
02:38:46,740 --> 02:38:51,300
and then we type npx not M but X
4073
02:38:51,300 --> 02:38:55,979
create minus react minus app
4074
02:38:55,979 --> 02:38:57,540
then we have to give it the name I'm
4075
02:38:57,540 --> 02:38:59,760
just gonna call it front end for
4076
02:38:59,760 --> 02:39:01,080
organization
4077
02:39:01,080 --> 02:39:02,819
and then to initialize this with
4078
02:39:02,819 --> 02:39:06,180
typescript we also add Dash Dash
4079
02:39:06,180 --> 02:39:08,520
template
4080
02:39:08,520 --> 02:39:09,960
space
4081
02:39:09,960 --> 02:39:11,760
typescript
4082
02:39:11,760 --> 02:39:14,399
so the full command looks like this
4083
02:39:14,399 --> 02:39:17,700
npx create react app the name front-end
4084
02:39:17,700 --> 02:39:19,740
template typescript
4085
02:39:19,740 --> 02:39:23,399
enter and now we let the magic happen
4086
02:39:23,399 --> 02:39:25,080
it will install the following packages
4087
02:39:25,080 --> 02:39:27,660
create react app blah blah
4088
02:39:27,660 --> 02:39:31,460
we type in y to confirm this
4089
02:39:33,060 --> 02:39:35,640
okay looks like it went successful now
4090
02:39:35,640 --> 02:39:37,439
it saves 10 highest probability of
4091
02:39:37,439 --> 02:39:39,660
vulnerabilities so it's probably the
4092
02:39:39,660 --> 02:39:41,220
case that it doesn't install the latest
4093
02:39:41,220 --> 02:39:43,560
dependencies we can do this later we can
4094
02:39:43,560 --> 02:39:46,380
update them for now this is fine we
4095
02:39:46,380 --> 02:39:49,080
close this and as you can see it created
4096
02:39:49,080 --> 02:39:51,300
this newer front-end folder in our
4097
02:39:51,300 --> 02:39:53,760
project which contains a lot of files
4098
02:39:53,760 --> 02:39:56,520
that we know already from our backend
4099
02:39:56,520 --> 02:39:58,740
we can also see it here in our project
4100
02:39:58,740 --> 02:40:00,180
directly
4101
02:40:00,180 --> 02:40:02,580
so we have a package.json file with
4102
02:40:02,580 --> 02:40:05,160
dependencies we have a TS config file
4103
02:40:05,160 --> 02:40:08,100
which is already set up properly we have
4104
02:40:08,100 --> 02:40:09,600
a git ignore file with the most
4105
02:40:09,600 --> 02:40:12,060
important stuff and we have the source
4106
02:40:12,060 --> 02:40:13,859
folder here which contains the source
4107
02:40:13,859 --> 02:40:16,260
code of the react project create react
4108
02:40:16,260 --> 02:40:19,020
app also sets up some other stuff for
4109
02:40:19,020 --> 02:40:21,000
example the whole tool chain that allows
4110
02:40:21,000 --> 02:40:24,000
us to compile our code basically into
4111
02:40:24,000 --> 02:40:26,640
production code which also minifies it
4112
02:40:26,640 --> 02:40:28,620
so it runs more efficient and it can't
4113
02:40:28,620 --> 02:40:31,140
be reverse engineered as easily this is
4114
02:40:31,140 --> 02:40:33,359
all set up by this create react app
4115
02:40:33,359 --> 02:40:35,640
command now first of all let's go back
4116
02:40:35,640 --> 02:40:38,340
into the package.json file of our react
4117
02:40:38,340 --> 02:40:40,260
app and let's try updating these
4118
02:40:40,260 --> 02:40:42,479
dependencies here maybe we can get rid
4119
02:40:42,479 --> 02:40:44,280
of this warning about the 10 highest
4120
02:40:44,280 --> 02:40:46,680
rabbit here vulnerabilities first of all
4121
02:40:46,680 --> 02:40:48,600
I want to point something out as you can
4122
02:40:48,600 --> 02:40:51,180
see in react all dependencies are
4123
02:40:51,180 --> 02:40:53,040
installed under the dependencies block
4124
02:40:53,040 --> 02:40:55,800
even the types and so on and the
4125
02:40:55,800 --> 02:40:57,420
dependencies we only need for
4126
02:40:57,420 --> 02:40:59,580
development are here and not in the dev
4127
02:40:59,580 --> 02:41:01,800
dependencies block you can put them in
4128
02:41:01,800 --> 02:41:04,080
the dev dependencies block but I read
4129
02:41:04,080 --> 02:41:06,180
their reasoning behind putting all of
4130
02:41:06,180 --> 02:41:08,280
this here this is because when we later
4131
02:41:08,280 --> 02:41:10,319
built the project everything that we
4132
02:41:10,319 --> 02:41:12,060
don't use will be Stripped Away anyway
4133
02:41:12,060 --> 02:41:13,800
this is not the case for our back end
4134
02:41:13,800 --> 02:41:16,140
but it's the case for react apps this is
4135
02:41:16,140 --> 02:41:17,700
where you can just put everything into
4136
02:41:17,700 --> 02:41:19,560
the dependencies block and it will not
4137
02:41:19,560 --> 02:41:21,840
make a difference so we don't have to do
4138
02:41:21,840 --> 02:41:24,600
a install safe def we can just install
4139
02:41:24,600 --> 02:41:26,700
everything as a normal production
4140
02:41:26,700 --> 02:41:29,760
dependency and by the way eslint is also
4141
02:41:29,760 --> 02:41:31,740
configured by default so we don't have
4142
02:41:31,740 --> 02:41:33,600
to install this either but let's try
4143
02:41:33,600 --> 02:41:36,120
updating our dependencies here but we
4144
02:41:36,120 --> 02:41:37,740
are still in the backend folder we want
4145
02:41:37,740 --> 02:41:39,359
to get to the
4146
02:41:39,359 --> 02:41:42,479
front end photo as we go backwards one
4147
02:41:42,479 --> 02:41:45,120
step with CD and then two dots if I
4148
02:41:45,120 --> 02:41:46,979
remember correctly right and then we
4149
02:41:46,979 --> 02:41:49,080
want to go into the front-end folder and
4150
02:41:49,080 --> 02:41:50,960
I'm actually not sure where the
4151
02:41:50,960 --> 02:41:53,220
vulnerabilities are coming from so let's
4152
02:41:53,220 --> 02:41:55,500
run npm audit
4153
02:41:55,500 --> 02:41:58,260
which checks the dependencies so where
4154
02:41:58,260 --> 02:42:00,600
is this coming from I don't really know
4155
02:42:00,600 --> 02:42:02,399
because I think the dependencies are
4156
02:42:02,399 --> 02:42:04,380
actually up to date already I just
4157
02:42:04,380 --> 02:42:06,359
checked them a moment ago so let's run
4158
02:42:06,359 --> 02:42:10,620
npm audit fix to try to fix them
4159
02:42:10,620 --> 02:42:12,840
automatically
4160
02:42:12,840 --> 02:42:15,720
no it still didn't work but here it
4161
02:42:15,720 --> 02:42:18,720
seems to be caused by react scripts so
4162
02:42:18,720 --> 02:42:20,100
maybe there's something wrong in the
4163
02:42:20,100 --> 02:42:22,979
latest dependency we could run npm audit
4164
02:42:22,979 --> 02:42:25,620
fixed false which were basically set it
4165
02:42:25,620 --> 02:42:27,720
to an older version but since this is a
4166
02:42:27,720 --> 02:42:29,760
tutorial and you might watch this in the
4167
02:42:29,760 --> 02:42:31,020
future where this is not the case
4168
02:42:31,020 --> 02:42:32,760
anymore let's just keep it as it is
4169
02:42:32,760 --> 02:42:34,800
Let's ignore this for now if you watch
4170
02:42:34,800 --> 02:42:36,359
this in the future there might be not
4171
02:42:36,359 --> 02:42:38,939
these vulnerabilities anymore otherwise
4172
02:42:38,939 --> 02:42:41,760
you can run npm audit fix false which
4173
02:42:41,760 --> 02:42:43,680
will set it to the appropriate version
4174
02:42:43,680 --> 02:42:46,560
so this vulnerabilities disappear but
4175
02:42:46,560 --> 02:42:49,740
for this tutorial we will ignore this
4176
02:42:49,740 --> 02:42:51,960
but let's actually run this app because
4177
02:42:51,960 --> 02:42:54,720
we can run it with the default setup
4178
02:42:54,720 --> 02:42:58,520
we type npm start
4179
02:42:59,280 --> 02:43:02,700
and the react app runs on Port 3000 by
4180
02:43:02,700 --> 02:43:05,340
default as you can see here so now you
4181
02:43:05,340 --> 02:43:07,080
can either type in this localhost
4182
02:43:07,080 --> 02:43:09,720
address but it actually should open it
4183
02:43:09,720 --> 02:43:12,420
automatically and this is our brand new
4184
02:43:12,420 --> 02:43:14,220
react app this is how it looks by
4185
02:43:14,220 --> 02:43:15,359
default
4186
02:43:15,359 --> 02:43:17,819
the cool thing about developing react
4187
02:43:17,819 --> 02:43:20,280
apps is that we see changes that we make
4188
02:43:20,280 --> 02:43:22,920
to the code instantly so let's close the
4189
02:43:22,920 --> 02:43:25,260
console here let's actually close all
4190
02:43:25,260 --> 02:43:27,120
the tabs because most of them are from
4191
02:43:27,120 --> 02:43:28,740
our backend code
4192
02:43:28,740 --> 02:43:31,380
let's also collapse the back end folder
4193
02:43:31,380 --> 02:43:34,979
and then let's go into app.tsx before I
4194
02:43:34,979 --> 02:43:36,600
explain what all of this means let's
4195
02:43:36,600 --> 02:43:39,479
just make a little change in here
4196
02:43:39,479 --> 02:43:43,080
let's change this text to
4197
02:43:43,080 --> 02:43:46,979
subscribe to a coding in flow and as
4198
02:43:46,979 --> 02:43:49,380
soon as we save this it changes over
4199
02:43:49,380 --> 02:43:51,720
here so this is a lot of fun in
4200
02:43:51,720 --> 02:43:53,700
development because it's really fast you
4201
02:43:53,700 --> 02:43:55,439
can see the changes instantly in the
4202
02:43:55,439 --> 02:43:58,140
browser okay whenever we work with react
4203
02:43:58,140 --> 02:44:01,560
we have this TSX files if this was not a
4204
02:44:01,560 --> 02:44:03,359
typescript but a normal JavaScript
4205
02:44:03,359 --> 02:44:07,439
project this would be called jsx and jsx
4206
02:44:07,439 --> 02:44:11,399
or TSX is kind of a mix between HTML and
4207
02:44:11,399 --> 02:44:13,800
JavaScript so we get this weird mix here
4208
02:44:13,800 --> 02:44:17,040
where we have yeah HTML tags but we can
4209
02:44:17,040 --> 02:44:19,920
also write JavaScript in these files
4210
02:44:19,920 --> 02:44:22,740
entry act as a so-called declarative UI
4211
02:44:22,740 --> 02:44:24,899
framework to understand what this means
4212
02:44:24,899 --> 02:44:27,000
let's look at the old approach the
4213
02:44:27,000 --> 02:44:30,359
imperative approach so usually on
4214
02:44:30,359 --> 02:44:32,939
websites we have an HTML file that
4215
02:44:32,939 --> 02:44:34,859
contains the basically the structure of
4216
02:44:34,859 --> 02:44:36,960
our website where our buttons are where
4217
02:44:36,960 --> 02:44:39,060
text is and so on in this example here
4218
02:44:39,060 --> 02:44:40,920
we just have a button but of course a
4219
02:44:40,920 --> 02:44:42,899
real HTML file has a body and different
4220
02:44:42,899 --> 02:44:45,180
components and so on and then we have
4221
02:44:45,180 --> 02:44:47,520
separate JavaScript files so we don't
4222
02:44:47,520 --> 02:44:49,560
have this mix between HTM and JavaScript
4223
02:44:49,560 --> 02:44:51,960
that we just saw a moment ago we usually
4224
02:44:51,960 --> 02:44:53,700
have to separate it and in our
4225
02:44:53,700 --> 02:44:55,979
JavaScript files we can interact with
4226
02:44:55,979 --> 02:44:58,319
our HTML files and we can make changes
4227
02:44:58,319 --> 02:45:01,080
to the text of buttons or other elements
4228
02:45:01,080 --> 02:45:03,479
for example so in JavaScript we have
4229
02:45:03,479 --> 02:45:05,280
stuff like variables because JavaScript
4230
02:45:05,280 --> 02:45:07,319
is a real programming language and when
4231
02:45:07,319 --> 02:45:09,120
we want to change something we look it
4232
02:45:09,120 --> 02:45:11,340
up like in this example we look up the
4233
02:45:11,340 --> 02:45:13,500
counter button with this get element by
4234
02:45:13,500 --> 02:45:16,020
idea function we can set an on click
4235
02:45:16,020 --> 02:45:18,300
listener on it and here we can Define
4236
02:45:18,300 --> 02:45:21,060
what we do when we click the button and
4237
02:45:21,060 --> 02:45:23,580
imperative means that we are basically
4238
02:45:23,580 --> 02:45:25,979
micromanage what happens we say okay we
4239
02:45:25,979 --> 02:45:27,420
want to increment the counter variable
4240
02:45:27,420 --> 02:45:29,520
and then we want to go ahead take our
4241
02:45:29,520 --> 02:45:31,740
button change the text inside it with
4242
02:45:31,740 --> 02:45:34,020
this inner HTML card here by changing
4243
02:45:34,020 --> 02:45:36,120
this we change the text contained in
4244
02:45:36,120 --> 02:45:37,859
this button and then we Define what we
4245
02:45:37,859 --> 02:45:39,660
want to set this to we want to set this
4246
02:45:39,660 --> 02:45:42,479
to a current colon and a new count this
4247
02:45:42,479 --> 02:45:44,700
is how a programming worked in the past
4248
02:45:44,700 --> 02:45:47,220
basically the downside of this is that
4249
02:45:47,220 --> 02:45:49,800
it's easy to forget stuff like changing
4250
02:45:49,800 --> 02:45:52,020
the text properly of course this is a
4251
02:45:52,020 --> 02:45:53,520
simple example but if you have a real
4252
02:45:53,520 --> 02:45:55,560
app where many different things change
4253
02:45:55,560 --> 02:45:57,479
in many different places at different
4254
02:45:57,479 --> 02:46:00,000
situations then this is easy to get
4255
02:46:00,000 --> 02:46:01,979
wrong and add bugs in the future because
4256
02:46:01,979 --> 02:46:04,260
we always have to micromanage what we
4257
02:46:04,260 --> 02:46:07,140
change at which point in time right
4258
02:46:07,140 --> 02:46:09,660
declarative UI Frameworks changed this
4259
02:46:09,660 --> 02:46:11,939
concept a bit and react is one of them
4260
02:46:11,939 --> 02:46:14,340
jetpack composes another example of a
4261
02:46:14,340 --> 02:46:16,740
declarative UI framework on Android I
4262
02:46:16,740 --> 02:46:19,200
think Swift UI is one as well and
4263
02:46:19,200 --> 02:46:22,380
flutter too with declarative UI we are
4264
02:46:22,380 --> 02:46:25,020
defined one time what kind of data is an
4265
02:46:25,020 --> 02:46:26,939
element on the screenshot contain and
4266
02:46:26,939 --> 02:46:28,140
then it's basically updated
4267
02:46:28,140 --> 02:46:30,479
automatically so this is basically the
4268
02:46:30,479 --> 02:46:32,520
same button example as before but this
4269
02:46:32,520 --> 02:46:34,500
time it's react code so imagine that
4270
02:46:34,500 --> 02:46:37,500
this is a jsx file or TSX so we can put
4271
02:46:37,500 --> 02:46:39,359
all of this in the same file the
4272
02:46:39,359 --> 02:46:42,120
JavaScript and the HTML code and in a
4273
02:46:42,120 --> 02:46:44,700
declarative UI we are defined one time
4274
02:46:44,700 --> 02:46:46,439
what kind of text this button should
4275
02:46:46,439 --> 02:46:48,180
contain so as you can see we have this
4276
02:46:48,180 --> 02:46:50,280
button tag and here we have count colon
4277
02:46:50,280 --> 02:46:52,560
entry passes count variable now the
4278
02:46:52,560 --> 02:46:54,660
difference to imperative UI is that when
4279
02:46:54,660 --> 02:46:57,060
we now increment is counter and we
4280
02:46:57,060 --> 02:46:58,920
change this current variable this text
4281
02:46:58,920 --> 02:47:00,840
here will update automatically we don't
4282
02:47:00,840 --> 02:47:02,819
have to go ahead and there change the
4283
02:47:02,819 --> 02:47:04,800
text or the HTML of this button to this
4284
02:47:04,800 --> 02:47:06,660
new value nowhere here in this code does
4285
02:47:06,660 --> 02:47:07,859
this happen because it happens
4286
02:47:07,859 --> 02:47:10,020
automatically that's called declarative
4287
02:47:10,020 --> 02:47:12,840
because we declare how the layout looks
4288
02:47:12,840 --> 02:47:15,420
the UI and then react takes care of
4289
02:47:15,420 --> 02:47:17,760
keeping it in sync with the data it
4290
02:47:17,760 --> 02:47:19,439
depends on so whenever the current
4291
02:47:19,439 --> 02:47:21,540
changes react knows that the text of
4292
02:47:21,540 --> 02:47:23,580
this button has to change as well and it
4293
02:47:23,580 --> 02:47:25,560
basically recharge this whole button on
4294
02:47:25,560 --> 02:47:27,180
the screen and this is okay because
4295
02:47:27,180 --> 02:47:29,100
react takes care of making this
4296
02:47:29,100 --> 02:47:30,899
efficient under the hood and the main
4297
02:47:30,899 --> 02:47:32,640
benefit of this is really that it's
4298
02:47:32,640 --> 02:47:35,520
easier to keep our UI in sync so if we
4299
02:47:35,520 --> 02:47:37,319
have many different places and different
4300
02:47:37,319 --> 02:47:39,660
variables different states then we just
4301
02:47:39,660 --> 02:47:41,460
have to declare how the different
4302
02:47:41,460 --> 02:47:43,319
components look and what data they
4303
02:47:43,319 --> 02:47:45,300
should contain entry Architects care of
4304
02:47:45,300 --> 02:47:47,580
keeping everything up to date as long as
4305
02:47:47,580 --> 02:47:50,100
we declared it properly and because we
4306
02:47:50,100 --> 02:47:52,200
have this mix between HTML and
4307
02:47:52,200 --> 02:47:54,540
JavaScript we can also do stuff like
4308
02:47:54,540 --> 02:47:57,240
putting for Loops directly into our HTML
4309
02:47:57,240 --> 02:47:59,640
and saying okay depending on the size of
4310
02:47:59,640 --> 02:48:01,740
a variable we want to add more or less
4311
02:48:01,740 --> 02:48:04,140
items to our layout for example so we
4312
02:48:04,140 --> 02:48:06,300
can do some really cool stuff it also
4313
02:48:06,300 --> 02:48:08,220
allows us to work create reusable
4314
02:48:08,220 --> 02:48:10,020
components that we can use in different
4315
02:48:10,020 --> 02:48:12,240
places in our app because we don't have
4316
02:48:12,240 --> 02:48:15,120
this arbitrary separation between HTML
4317
02:48:15,120 --> 02:48:17,280
and JavaScript which actually was never
4318
02:48:17,280 --> 02:48:19,680
a good idea because it's a separation of
4319
02:48:19,680 --> 02:48:21,780
Technologies and not a separation of
4320
02:48:21,780 --> 02:48:25,319
concerns as it should be but jsx or TSX
4321
02:48:25,319 --> 02:48:28,020
fixes this by allowing us to work riding
4322
02:48:28,020 --> 02:48:31,380
this mix between HTML and JS and we will
4323
02:48:31,380 --> 02:48:33,180
actually rebuild this example in a
4324
02:48:33,180 --> 02:48:35,220
moment so let's go back to our react
4325
02:48:35,220 --> 02:48:36,600
project
4326
02:48:36,600 --> 02:48:39,780
first of all let's install bootstrap or
4327
02:48:39,780 --> 02:48:42,240
rather react bootstrap which is this UI
4328
02:48:42,240 --> 02:48:44,340
library that we want to use it just
4329
02:48:44,340 --> 02:48:46,020
makes it easier to create uis that
4330
02:48:46,020 --> 02:48:47,819
actually look good it's completely free
4331
02:48:47,819 --> 02:48:49,859
and I think it's easier to use than
4332
02:48:49,859 --> 02:48:52,680
Tailwind CSS for example so here are the
4333
02:48:52,680 --> 02:48:54,420
installation instructions we basically
4334
02:48:54,420 --> 02:48:56,880
want to copy this line npm install react
4335
02:48:56,880 --> 02:48:59,280
bootstrap and bootstrap so those are two
4336
02:48:59,280 --> 02:49:01,880
dependencies
4337
02:49:03,000 --> 02:49:05,580
so open the terminal here
4338
02:49:05,580 --> 02:49:08,700
cancel the execution with Ctrl Z make
4339
02:49:08,700 --> 02:49:10,200
sure that we are still in the front end
4340
02:49:10,200 --> 02:49:13,979
folder and paste this command
4341
02:49:13,979 --> 02:49:15,960
while this is installing we take a look
4342
02:49:15,960 --> 02:49:17,939
at the instructions again
4343
02:49:17,939 --> 02:49:21,060
we have to import this part here
4344
02:49:21,060 --> 02:49:24,120
this Imports the CSS so that our
4345
02:49:24,120 --> 02:49:26,399
elements actually look good there are
4346
02:49:26,399 --> 02:49:28,500
also different ways to install this or
4347
02:49:28,500 --> 02:49:31,200
customize this but the rest here doesn't
4348
02:49:31,200 --> 02:49:32,760
really matter for us we just want to
4349
02:49:32,760 --> 02:49:35,399
copy this line and you can get it from
4350
02:49:35,399 --> 02:49:38,220
this URL or take a look into the code in
4351
02:49:38,220 --> 02:49:40,319
the description below of my project on
4352
02:49:40,319 --> 02:49:43,979
GitHub there you can also copy it from
4353
02:49:43,979 --> 02:49:46,560
and Twitter reporters we put this into
4354
02:49:46,560 --> 02:49:49,620
the index TSX file here
4355
02:49:49,620 --> 02:49:52,260
which is another TSX file just like our
4356
02:49:52,260 --> 02:49:54,359
app TSX
4357
02:49:54,359 --> 02:49:57,540
now the order of the CSS Imports is
4358
02:49:57,540 --> 02:49:59,399
important so we put it all the way at
4359
02:49:59,399 --> 02:50:00,540
the top here
4360
02:50:00,540 --> 02:50:04,200
and import this bootstrap Min CSS
4361
02:50:04,200 --> 02:50:07,439
now index TSX basically initializes our
4362
02:50:07,439 --> 02:50:10,380
react app for this it uses another file
4363
02:50:10,380 --> 02:50:12,060
in the public folder here which is
4364
02:50:12,060 --> 02:50:14,819
called index.html as you can see this is
4365
02:50:14,819 --> 02:50:16,620
basically the structure of a normal
4366
02:50:16,620 --> 02:50:19,740
website this is a normal HTML file and
4367
02:50:19,740 --> 02:50:21,960
this is where our react app appears
4368
02:50:21,960 --> 02:50:24,780
later basically so this is our page that
4369
02:50:24,780 --> 02:50:27,420
we load when we load our react app down
4370
02:50:27,420 --> 02:50:30,540
here we have this root tag and index TSX
4371
02:50:30,540 --> 02:50:33,000
looks up this root tag
4372
02:50:33,000 --> 02:50:35,580
and it renders our react app at this
4373
02:50:35,580 --> 02:50:38,880
place and the react app is the app.tsx
4374
02:50:38,880 --> 02:50:40,740
file that we made changes to a moment
4375
02:50:40,740 --> 02:50:43,260
ago we don't really change much in this
4376
02:50:43,260 --> 02:50:45,120
file besides little things like
4377
02:50:45,120 --> 02:50:47,220
importing the CSS because we want to do
4378
02:50:47,220 --> 02:50:49,500
this very earlier in our code we could
4379
02:50:49,500 --> 02:50:52,260
also do this in the app TSX file but we
4380
02:50:52,260 --> 02:50:54,120
actually want to do it before we
4381
02:50:54,120 --> 02:50:56,520
initialize the app itself
4382
02:50:56,520 --> 02:50:58,680
and here we also see something that we
4383
02:50:58,680 --> 02:51:01,140
saw earlier already this text here you
4384
02:51:01,140 --> 02:51:02,880
need to enable JavaScript to run this
4385
02:51:02,880 --> 02:51:04,439
app if you remember in the beginning
4386
02:51:04,439 --> 02:51:07,140
when I disable JavaScript in the browser
4387
02:51:07,140 --> 02:51:09,000
without the same text
4388
02:51:09,000 --> 02:51:11,160
this is handled by this no script tag
4389
02:51:11,160 --> 02:51:13,740
here and if JavaScript does work then we
4390
02:51:13,740 --> 02:51:16,140
see our normal route where our app is
4391
02:51:16,140 --> 02:51:19,560
rendered in this index HTML file also
4392
02:51:19,560 --> 02:51:22,260
contains metadata about our website so
4393
02:51:22,260 --> 02:51:24,180
as you can see we have this title tag
4394
02:51:24,180 --> 02:51:27,000
here we have a description somewhere and
4395
02:51:27,000 --> 02:51:28,800
we also have stuff like the preview
4396
02:51:28,800 --> 02:51:31,380
images for social media now you can read
4397
02:51:31,380 --> 02:51:33,540
more about this by just Googling or
4398
02:51:33,540 --> 02:51:35,520
reading the documentation but let's make
4399
02:51:35,520 --> 02:51:37,680
some little changes in here let's change
4400
02:51:37,680 --> 02:51:39,840
the title to a
4401
02:51:39,840 --> 02:51:42,180
quill Notes app
4402
02:51:42,180 --> 02:51:44,520
and let's change the description as well
4403
02:51:44,520 --> 02:51:46,859
which is this meta tag here
4404
02:51:46,859 --> 02:51:49,080
website created using Create react app
4405
02:51:49,080 --> 02:51:51,660
this is what we see for example later in
4406
02:51:51,660 --> 02:51:53,700
Google search when our website shows up
4407
02:51:53,700 --> 02:51:56,580
there let's change this tool tutorial
4408
02:51:56,580 --> 02:52:00,180
project by coding in flow because of
4409
02:52:00,180 --> 02:52:01,380
course you always have to give
4410
02:52:01,380 --> 02:52:02,939
appropriate credit
4411
02:52:02,939 --> 02:52:04,920
then save this
4412
02:52:04,920 --> 02:52:09,560
now when we run npm start again
4413
02:52:09,660 --> 02:52:12,180
and look at the page again
4414
02:52:12,180 --> 02:52:14,160
after the test started
4415
02:52:14,160 --> 02:52:17,040
we can see the changes to our metadata
4416
02:52:17,040 --> 02:52:19,439
now the title says Square Notes app we
4417
02:52:19,439 --> 02:52:21,000
can't see the description but again we
4418
02:52:21,000 --> 02:52:22,979
would see it in Google search for
4419
02:52:22,979 --> 02:52:25,500
example the little fav icon here on the
4420
02:52:25,500 --> 02:52:27,300
top left this little icon you can see
4421
02:52:27,300 --> 02:52:29,700
here is also something you can change in
4422
02:52:29,700 --> 02:52:33,420
this index HTML file there's a path icon
4423
02:52:33,420 --> 02:52:36,660
text somewhere this one here
4424
02:52:36,660 --> 02:52:38,700
there are generators for this kind of
4425
02:52:38,700 --> 02:52:41,220
icons that you can find in Google but we
4426
02:52:41,220 --> 02:52:44,220
will leave the rest here untouched
4427
02:52:44,220 --> 02:52:46,380
also interesting to notice the public
4428
02:52:46,380 --> 02:52:48,660
folder here this is where you put files
4429
02:52:48,660 --> 02:52:50,399
like images that you want to use on your
4430
02:52:50,399 --> 02:52:52,859
website we won't add any images but
4431
02:52:52,859 --> 02:52:55,680
these react pictures here that we saw on
4432
02:52:55,680 --> 02:52:58,439
the page are inside here you can delete
4433
02:52:58,439 --> 02:53:00,359
them if you want you can also leave them
4434
02:53:00,359 --> 02:53:02,100
there it doesn't matter and if you want
4435
02:53:02,100 --> 02:53:03,479
to learn more about these different
4436
02:53:03,479 --> 02:53:06,479
folders and file types that I don't
4437
02:53:06,479 --> 02:53:08,399
explain here then of course everything
4438
02:53:08,399 --> 02:53:10,260
is explained in detail in the
4439
02:53:10,260 --> 02:53:11,760
documentation
4440
02:53:11,760 --> 02:53:14,460
and now as promised let's rebuild our
4441
02:53:14,460 --> 02:53:17,399
button example our button clicker that
4442
02:53:17,399 --> 02:53:19,859
we saw a moment ago on the slides for
4443
02:53:19,859 --> 02:53:22,080
this let's go into the app TSX file
4444
02:53:22,080 --> 02:53:23,939
because this is where the code of our
4445
02:53:23,939 --> 02:53:26,580
actual app lives basically of course
4446
02:53:26,580 --> 02:53:28,680
later we have different pages and
4447
02:53:28,680 --> 02:53:30,240
different components and we create
4448
02:53:30,240 --> 02:53:32,640
separate files for them but they are all
4449
02:53:32,640 --> 02:53:35,279
put here in one way or another we will
4450
02:53:35,279 --> 02:53:37,439
talk more about the syntax of these
4451
02:53:37,439 --> 02:53:39,779
react components later but for now let's
4452
02:53:39,779 --> 02:53:42,359
ignore this and the first thing we need
4453
02:53:42,359 --> 02:53:44,399
for our calendar is a variable of course
4454
02:53:44,399 --> 02:53:47,040
that maintains the current count now the
4455
02:53:47,040 --> 02:53:49,399
first idea would be to just create a let
4456
02:53:49,399 --> 02:53:52,140
counter and initialize this with zero
4457
02:53:52,140 --> 02:53:54,240
but in react we need a special type of
4458
02:53:54,240 --> 02:53:57,060
variable for the state of a component
4459
02:53:57,060 --> 02:54:00,060
because we need to notify react that it
4460
02:54:00,060 --> 02:54:02,580
has to redraw the UI to display the new
4461
02:54:02,580 --> 02:54:05,460
value so we don't use a normal variable
4462
02:54:05,460 --> 02:54:09,479
we create a state like this const
4463
02:54:09,479 --> 02:54:12,000
then we make a pair of scrap brackets
4464
02:54:12,000 --> 02:54:15,000
and then here we write click count which
4465
02:54:15,000 --> 02:54:16,700
is just the name we give this variable
4466
02:54:16,700 --> 02:54:20,279
comma and the second variable in here is
4467
02:54:20,279 --> 02:54:23,580
set click count so we always have a
4468
02:54:23,580 --> 02:54:25,740
variable and then we have basically a
4469
02:54:25,740 --> 02:54:28,319
function with the same name but just set
4470
02:54:28,319 --> 02:54:30,840
pre-appended to it outside of the scrap
4471
02:54:30,840 --> 02:54:32,880
brackets we write use
4472
02:54:32,880 --> 02:54:35,700
state which should add this import
4473
02:54:35,700 --> 02:54:38,460
statement up here if it didn't edit
4474
02:54:38,460 --> 02:54:40,680
manually and we initialize this with
4475
02:54:40,680 --> 02:54:41,760
zero
4476
02:54:41,760 --> 02:54:43,979
and because typescript has Type
4477
02:54:43,979 --> 02:54:46,439
inference as usual it knows that click
4478
02:54:46,439 --> 02:54:48,720
count is a number because we initialize
4479
02:54:48,720 --> 02:54:50,460
it with a number
4480
02:54:50,460 --> 02:54:52,979
so this weird syntax means that your
4481
02:54:52,979 --> 02:54:55,080
state basically returns us a little
4482
02:54:55,080 --> 02:54:56,939
array with two values and create
4483
02:54:56,939 --> 02:54:59,040
destructure it like this we take the
4484
02:54:59,040 --> 02:55:01,080
first value call it click count and the
4485
02:55:01,080 --> 02:55:02,939
second value which we call that click on
4486
02:55:02,939 --> 02:55:05,640
one is the value itself and one is used
4487
02:55:05,640 --> 02:55:08,040
to update the state and we do all of
4488
02:55:08,040 --> 02:55:09,899
this here at the top of this F function
4489
02:55:09,899 --> 02:55:12,120
before the return statement because the
4490
02:55:12,120 --> 02:55:14,220
return statement Returns the actual UI
4491
02:55:14,220 --> 02:55:16,319
element okay and then we want to put our
4492
02:55:16,319 --> 02:55:19,020
button somewhere let's remove this learn
4493
02:55:19,020 --> 02:55:21,200
react link we don't need it anymore
4494
02:55:21,200 --> 02:55:23,939
instead create an opening anchor bracket
4495
02:55:23,939 --> 02:55:26,700
button which creates the HTML element
4496
02:55:26,700 --> 02:55:28,979
entry One Import button here from the
4497
02:55:28,979 --> 02:55:31,680
react bootstrap package which adds this
4498
02:55:31,680 --> 02:55:33,120
import statement
4499
02:55:33,120 --> 02:55:35,939
we close this and in between this text
4500
02:55:35,939 --> 02:55:37,979
we can now Define the text contained
4501
02:55:37,979 --> 02:55:40,140
inside this button so for example if we
4502
02:55:40,140 --> 02:55:43,500
just write click zero times and save
4503
02:55:43,500 --> 02:55:44,760
this
4504
02:55:44,760 --> 02:55:47,100
we see the change in our UI this is the
4505
02:55:47,100 --> 02:55:49,680
bootstrap button just for comparison if
4506
02:55:49,680 --> 02:55:51,540
we didn't use bootstrap but the normal
4507
02:55:51,540 --> 02:55:55,140
HTML button it would look like 1993 but
4508
02:55:55,140 --> 02:55:57,960
we use these special styled bootstrap
4509
02:55:57,960 --> 02:56:00,000
buttons but of course we don't want to
4510
02:56:00,000 --> 02:56:02,700
hard code the zero here we wanna show
4511
02:56:02,700 --> 02:56:04,140
our click count
4512
02:56:04,140 --> 02:56:07,080
so instead of 0 we write a pair of curly
4513
02:56:07,080 --> 02:56:08,939
braces because this is how we put a
4514
02:56:08,939 --> 02:56:11,460
variable in between these two pieces of
4515
02:56:11,460 --> 02:56:12,960
text
4516
02:56:12,960 --> 02:56:16,260
and in here we pass the click on
4517
02:56:16,260 --> 02:56:18,840
when we save this it still says 0 but
4518
02:56:18,840 --> 02:56:21,180
this time it's coming from this state up
4519
02:56:21,180 --> 02:56:24,240
here and now as I explained the magic of
4520
02:56:24,240 --> 02:56:27,000
declarative UI is that when we update
4521
02:56:27,000 --> 02:56:29,520
this click count over this set click on
4522
02:56:29,520 --> 02:56:31,260
function the text in here will change
4523
02:56:31,260 --> 02:56:33,060
automatically we don't have to
4524
02:56:33,060 --> 02:56:35,340
micromanage this like in the imperative
4525
02:56:35,340 --> 02:56:37,319
approach and we want to change this
4526
02:56:37,319 --> 02:56:39,600
value on when we click the button so in
4527
02:56:39,600 --> 02:56:42,240
the button opening tag we can add an on
4528
02:56:42,240 --> 02:56:44,939
click function like this make sure that
4529
02:56:44,939 --> 02:56:46,800
you get the spelling right candid case
4530
02:56:46,800 --> 02:56:49,439
with an uppercase 0. and in here we want
4531
02:56:49,439 --> 02:56:52,020
to pass a function we pass an anonymous
4532
02:56:52,020 --> 02:56:53,700
function like this
4533
02:56:53,700 --> 02:56:57,060
pair of parentheses a right arrow like
4534
02:56:57,060 --> 02:56:59,939
we did in our server code and since this
4535
02:56:59,939 --> 02:57:02,100
is only one line we don't have to add
4536
02:57:02,100 --> 02:57:04,620
another pair of curly braces instead we
4537
02:57:04,620 --> 02:57:06,660
can just call the function that we want
4538
02:57:06,660 --> 02:57:09,180
to call directly we want a car that
4539
02:57:09,180 --> 02:57:10,920
click count
4540
02:57:10,920 --> 02:57:13,439
and we want to set this to a click
4541
02:57:13,439 --> 02:57:15,859
current
4542
02:57:15,899 --> 02:57:17,640
plus one
4543
02:57:17,640 --> 02:57:20,640
two add one to the value that we already
4544
02:57:20,640 --> 02:57:23,760
have let's save this and try it out
4545
02:57:23,760 --> 02:57:26,220
now when we click our button the state
4546
02:57:26,220 --> 02:57:29,040
gets set to a click count plus one so
4547
02:57:29,040 --> 02:57:31,319
zero plus one is one and since we
4548
02:57:31,319 --> 02:57:33,420
updated the state react notes that it
4549
02:57:33,420 --> 02:57:35,399
also has to update the content in the
4550
02:57:35,399 --> 02:57:37,020
button
4551
02:57:37,020 --> 02:57:39,000
and this time it's really difficult to
4552
02:57:39,000 --> 02:57:41,040
mess this up in the future with that how
4553
02:57:41,040 --> 02:57:42,840
this button should look what data it
4554
02:57:42,840 --> 02:57:43,920
should contain
4555
02:57:43,920 --> 02:57:45,960
and react takes care of updating it
4556
02:57:45,960 --> 02:57:48,180
properly okay we Now understand
4557
02:57:48,180 --> 02:57:51,000
declarative UI and you can put react
4558
02:57:51,000 --> 02:57:52,859
developer on your resume but of course
4559
02:57:52,859 --> 02:57:55,020
if you want to learn more about react in
4560
02:57:55,020 --> 02:57:56,819
your short stick with this tutorial
4561
02:57:56,819 --> 02:57:58,560
because the next step is to actually
4562
02:57:58,560 --> 02:58:00,899
fetch the nodes from our backend and
4563
02:58:00,899 --> 02:58:04,819
display them in the UI somehow
4564
02:58:08,520 --> 02:58:11,580
okay so before we can fetch the nodes to
4565
02:58:11,580 --> 02:58:13,380
the front end we need a model for the
4566
02:58:13,380 --> 02:58:15,660
nodes on the front end as well so we
4567
02:58:15,660 --> 02:58:17,580
create a new folder in our source folder
4568
02:58:17,580 --> 02:58:20,700
here in our front-end folder so in our
4569
02:58:20,700 --> 02:58:23,160
react code
4570
02:58:23,160 --> 02:58:27,540
and we call this lowercase models not
4571
02:58:27,540 --> 02:58:29,040
uppercase
4572
02:58:29,040 --> 02:58:31,140
and then we create a file in here we
4573
02:58:31,140 --> 02:58:33,720
right click new file
4574
02:58:33,720 --> 02:58:36,960
and we call it node.ts attention this is
4575
02:58:36,960 --> 02:58:39,359
not a TSX file this is not this weird
4576
02:58:39,359 --> 02:58:42,240
HTML JavaScript combination it's just a
4577
02:58:42,240 --> 02:58:44,520
normal typescript file because all we
4578
02:58:44,520 --> 02:58:46,439
put in here is one interface that we
4579
02:58:46,439 --> 02:58:48,779
want to export so that we can use it in
4580
02:58:48,779 --> 02:58:51,479
other places we call it node
4581
02:58:51,479 --> 02:58:54,060
and this will contain the shape the
4582
02:58:54,060 --> 02:58:56,279
structure of our node type that we
4583
02:58:56,279 --> 02:58:58,800
receive and use on the front end okay so
4584
02:58:58,800 --> 02:59:00,960
every note we will get from our server
4585
02:59:00,960 --> 02:59:03,600
will contain an idea with an underscore
4586
02:59:03,600 --> 02:59:05,520
because this is how a mongodb
4587
02:59:05,520 --> 02:59:09,840
automatically saves IDs underscore idea
4588
02:59:09,840 --> 02:59:12,479
and it will be of type string now
4589
02:59:12,479 --> 02:59:14,640
technically the idea in mongodb is a
4590
02:59:14,640 --> 02:59:16,620
different type it's this object ID type
4591
02:59:16,620 --> 02:59:19,140
that we used before but what we will
4592
02:59:19,140 --> 02:59:21,479
receive from the endpoint will just be a
4593
02:59:21,479 --> 02:59:23,100
string and that's all we need to work
4594
02:59:23,100 --> 02:59:25,800
with every note will have a title of
4595
02:59:25,800 --> 02:59:27,420
type string
4596
02:59:27,420 --> 02:59:29,700
the text of type string but the text
4597
02:59:29,700 --> 02:59:31,800
remember is optional so we add the
4598
02:59:31,800 --> 02:59:33,420
question mark here because there might
4599
02:59:33,420 --> 02:59:36,540
be a text in the node or not and then
4600
02:59:36,540 --> 02:59:39,359
the timestamps are created at and the
4601
02:59:39,359 --> 02:59:41,340
spelling has to be correct because this
4602
02:59:41,340 --> 02:59:43,800
is the name with which mongodbier stores
4603
02:59:43,800 --> 02:59:46,200
these timestamps and this will also be
4604
02:59:46,200 --> 02:59:48,840
of type string because rotary receive is
4605
02:59:48,840 --> 02:59:51,060
basically a string representation of the
4606
02:59:51,060 --> 02:59:52,979
timestamp and the reason why everything
4607
02:59:52,979 --> 02:59:55,319
is a string is because we receive Json
4608
02:59:55,319 --> 02:59:57,540
from the back end and Json doesn't have
4609
02:59:57,540 --> 02:59:59,640
where we elaborate data types there is
4610
02:59:59,640 --> 03:00:02,279
no date type in Json for example but
4611
03:00:02,279 --> 03:00:04,500
string will suffice because we can pass
4612
03:00:04,500 --> 03:00:06,600
everything from the string that we need
4613
03:00:06,600 --> 03:00:09,899
okay and then updated it
4614
03:00:09,899 --> 03:00:12,720
which is also of type string
4615
03:00:12,720 --> 03:00:14,939
let's save this because this is all we
4616
03:00:14,939 --> 03:00:16,200
need in here
4617
03:00:16,200 --> 03:00:18,060
all right and then let's go back into
4618
03:00:18,060 --> 03:00:21,240
our app TSX file because instead of a
4619
03:00:21,240 --> 03:00:23,100
borrowing counter we want to retrieve
4620
03:00:23,100 --> 03:00:24,779
our notes here and display them in the
4621
03:00:24,779 --> 03:00:27,240
UI so let's remove this line here with
4622
03:00:27,240 --> 03:00:29,340
the click count
4623
03:00:29,340 --> 03:00:32,279
and replace it for another state so
4624
03:00:32,279 --> 03:00:34,740
another const with the square brackets
4625
03:00:34,740 --> 03:00:37,560
and these two are variable names in here
4626
03:00:37,560 --> 03:00:41,399
we call it notes and the equivalent as
4627
03:00:41,399 --> 03:00:43,800
the zetter function is z nodes there's
4628
03:00:43,800 --> 03:00:46,020
always this pair between the variable
4629
03:00:46,020 --> 03:00:48,120
and the zero when you use State and
4630
03:00:48,120 --> 03:00:49,640
react
4631
03:00:49,640 --> 03:00:52,859
equals use state
4632
03:00:52,859 --> 03:00:55,080
and this will contain the nodes that we
4633
03:00:55,080 --> 03:00:58,020
display on the UI in form of a array we
4634
03:00:58,020 --> 03:00:59,760
want to initialize it with an empty
4635
03:00:59,760 --> 03:01:01,020
array
4636
03:01:01,020 --> 03:01:02,760
because at the beginning when the site
4637
03:01:02,760 --> 03:01:04,740
is opened there isn't any data in there
4638
03:01:04,740 --> 03:01:06,420
yet we have to fetch it from the back
4639
03:01:06,420 --> 03:01:08,399
end first but this way typescript
4640
03:01:08,399 --> 03:01:10,620
doesn't have enough information to infer
4641
03:01:10,620 --> 03:01:12,300
the type properly so when we hover over
4642
03:01:12,300 --> 03:01:14,760
nodes it's of type never array so we
4643
03:01:14,760 --> 03:01:16,680
have to tell typescript what type the
4644
03:01:16,680 --> 03:01:19,319
state has to be later and we do this as
4645
03:01:19,319 --> 03:01:22,200
usual with a pair of angle brackets and
4646
03:01:22,200 --> 03:01:24,300
the type will be our newer note type we
4647
03:01:24,300 --> 03:01:27,300
created in the models folder but it will
4648
03:01:27,300 --> 03:01:30,359
be an array an array of notes and now
4649
03:01:30,359 --> 03:01:32,580
nodes has the correct type here
4650
03:01:32,580 --> 03:01:35,100
okay but now the question is where do we
4651
03:01:35,100 --> 03:01:38,279
load these nodes we can't just put it
4652
03:01:38,279 --> 03:01:40,740
directly in here inside the body why
4653
03:01:40,740 --> 03:01:43,319
because if you remember I explained that
4654
03:01:43,319 --> 03:01:45,779
react basically redraws this whole
4655
03:01:45,779 --> 03:01:47,460
component whenever something in it
4656
03:01:47,460 --> 03:01:49,680
changes and it does this by executing
4657
03:01:49,680 --> 03:01:51,540
this function again so now when we do
4658
03:01:51,540 --> 03:01:53,340
something in the function body which is
4659
03:01:53,340 --> 03:01:55,200
called a zeit effect so something that
4660
03:01:55,200 --> 03:01:57,180
doesn't belong to the rendering itself
4661
03:01:57,180 --> 03:01:59,520
but something that happens in our normal
4662
03:01:59,520 --> 03:02:01,680
app flow and we do it directly here
4663
03:02:01,680 --> 03:02:04,080
inside this body then react will execute
4664
03:02:04,080 --> 03:02:05,880
it on every render every time this
4665
03:02:05,880 --> 03:02:07,560
function is called which of course isn't
4666
03:02:07,560 --> 03:02:09,660
what we want it's way too often instead
4667
03:02:09,660 --> 03:02:11,880
we want to load our nodes one single
4668
03:02:11,880 --> 03:02:13,979
time when the app starts in this on
4669
03:02:13,979 --> 03:02:15,540
click Handler down here we didn't have
4670
03:02:15,540 --> 03:02:17,279
this problem because this only gets
4671
03:02:17,279 --> 03:02:19,140
executed when we click the button we are
4672
03:02:19,140 --> 03:02:21,120
not calling this function in the body of
4673
03:02:21,120 --> 03:02:23,880
our app component we call it only when
4674
03:02:23,880 --> 03:02:25,680
we click something so this is an
4675
03:02:25,680 --> 03:02:27,479
appropriate place for these side effects
4676
03:02:27,479 --> 03:02:29,160
but we don't want to have to click
4677
03:02:29,160 --> 03:02:30,720
something before we load our nodes we
4678
03:02:30,720 --> 03:02:32,279
want this to happen automatically and
4679
03:02:32,279 --> 03:02:34,319
for this we use something called us
4680
03:02:34,319 --> 03:02:36,899
effect as usual if it didn't import
4681
03:02:36,899 --> 03:02:39,660
properly added up here next to you state
4682
03:02:39,660 --> 03:02:42,000
it's a react import and this is a
4683
03:02:42,000 --> 03:02:43,979
function that takes another function as
4684
03:02:43,979 --> 03:02:46,260
import so we make a pair of parentheses
4685
03:02:46,260 --> 03:02:47,880
and put another pair of parentheses
4686
03:02:47,880 --> 03:02:49,319
inside it
4687
03:02:49,319 --> 03:02:52,080
and the right arrow so an error function
4688
03:02:52,080 --> 03:02:54,720
and a pair of curly braces
4689
03:02:54,720 --> 03:02:56,279
like this
4690
03:02:56,279 --> 03:02:58,500
refuse effect we can execute side
4691
03:02:58,500 --> 03:03:01,080
effects outside of the Year rendering of
4692
03:03:01,080 --> 03:03:02,880
the component itself in here we have
4693
03:03:02,880 --> 03:03:05,100
control over when it executes and how
4694
03:03:05,100 --> 03:03:07,439
often as you can see the documentation
4695
03:03:07,439 --> 03:03:09,720
says accepts a function that contains
4696
03:03:09,720 --> 03:03:12,479
imperative possibly effectual code in
4697
03:03:12,479 --> 03:03:14,460
here we want to load our nodes so we
4698
03:03:14,460 --> 03:03:16,140
basically want to do
4699
03:03:16,140 --> 03:03:19,260
const response equals
4700
03:03:19,260 --> 03:03:21,660
await because it's an asynchronous
4701
03:03:21,660 --> 03:03:23,520
operation we have to load something from
4702
03:03:23,520 --> 03:03:25,800
the back end this can take a moment and
4703
03:03:25,800 --> 03:03:28,680
to fetch data in react or in JavaScript
4704
03:03:28,680 --> 03:03:30,960
in general we have this fetch function
4705
03:03:30,960 --> 03:03:33,420
available that takes the URL and some
4706
03:03:33,420 --> 03:03:35,520
configuration and here we pass a string
4707
03:03:35,520 --> 03:03:37,380
and the URL where we want to fetch the
4708
03:03:37,380 --> 03:03:41,700
data from that's HTTP colon slash slash
4709
03:03:41,700 --> 03:03:45,000
localhost colon 5000 because that's the
4710
03:03:45,000 --> 03:03:47,520
address of our server in development
4711
03:03:47,520 --> 03:03:50,580
slash API because this is how we
4712
03:03:50,580 --> 03:03:53,100
configure the endpoint right
4713
03:03:53,100 --> 03:03:54,660
slash nodes
4714
03:03:54,660 --> 03:03:56,399
and then after the string we write a
4715
03:03:56,399 --> 03:03:58,260
comma because here goes some
4716
03:03:58,260 --> 03:04:00,420
configuration for this request for this
4717
03:04:00,420 --> 03:04:02,819
fetch call and in between curly braces
4718
03:04:02,819 --> 03:04:06,420
we write method colon and then get as a
4719
03:04:06,420 --> 03:04:08,520
string in all uppercase
4720
03:04:08,520 --> 03:04:10,439
this configures that we make a get
4721
03:04:10,439 --> 03:04:12,840
request with this Fetch and not a post
4722
03:04:12,840 --> 03:04:15,479
or a patch but a weight complains
4723
03:04:15,479 --> 03:04:17,340
because we only can execute a weight
4724
03:04:17,340 --> 03:04:19,859
inside an async function but when we try
4725
03:04:19,859 --> 03:04:21,720
to make the function and use effect an
4726
03:04:21,720 --> 03:04:24,479
async function we get this lint warning
4727
03:04:24,479 --> 03:04:26,460
effect callbacks are synchronous to
4728
03:04:26,460 --> 03:04:28,319
prevent race conditions but they also
4729
03:04:28,319 --> 03:04:30,120
tell us the solution put the async
4730
03:04:30,120 --> 03:04:31,380
function inside
4731
03:04:31,380 --> 03:04:33,240
and then they are basically doing this
4732
03:04:33,240 --> 03:04:36,180
so we remove this async here again and
4733
03:04:36,180 --> 03:04:37,560
we simply have to wrap this inside
4734
03:04:37,560 --> 03:04:39,660
another function which we can then make
4735
03:04:39,660 --> 03:04:42,660
async so we create an async function
4736
03:04:42,660 --> 03:04:45,660
which we call load nodes doesn't take
4737
03:04:45,660 --> 03:04:48,359
any arguments and then we take our line
4738
03:04:48,359 --> 03:04:51,000
here cut it out and put it inside
4739
03:04:51,000 --> 03:04:52,680
and then we can simply call this
4740
03:04:52,680 --> 03:04:54,359
function below
4741
03:04:54,359 --> 03:04:57,000
to execute it this way we don't have to
4742
03:04:57,000 --> 03:04:59,340
make the use effect itself async and
4743
03:04:59,340 --> 03:05:02,160
display react is happier also this fetch
4744
03:05:02,160 --> 03:05:04,140
car can throw an error if something goes
4745
03:05:04,140 --> 03:05:06,840
wrong so we have to wrap this into a try
4746
03:05:06,840 --> 03:05:09,000
catch block so we create this try catch
4747
03:05:09,000 --> 03:05:11,100
block cut out this line once again put
4748
03:05:11,100 --> 03:05:13,920
it in here and in the catch block we
4749
03:05:13,920 --> 03:05:16,439
will simply lock this error and show a
4750
03:05:16,439 --> 03:05:18,540
message to the user we related to the
4751
03:05:18,540 --> 03:05:20,279
more elaborate error handling but for
4752
03:05:20,279 --> 03:05:22,859
now that's sufficient zor rewrites
4753
03:05:22,859 --> 03:05:25,140
console.erlaw
4754
03:05:25,140 --> 03:05:27,420
pass the error which we are printed to
4755
03:05:27,420 --> 03:05:29,220
the error console
4756
03:05:29,220 --> 03:05:32,160
and then we can also call alert and pass
4757
03:05:32,160 --> 03:05:34,859
the error here as well alert opens a
4758
03:05:34,859 --> 03:05:36,600
little pop-up window in the browser it's
4759
03:05:36,600 --> 03:05:38,640
not very beautiful and it's not a great
4760
03:05:38,640 --> 03:05:40,800
user experience but for development this
4761
03:05:40,800 --> 03:05:44,040
is sufficient okay then we go be a load
4762
03:05:44,040 --> 03:05:46,500
the fetch line again because now we want
4763
03:05:46,500 --> 03:05:48,779
to get our data out of this response we
4764
03:05:48,779 --> 03:05:50,160
are still inside the dry block so
4765
03:05:50,160 --> 03:05:51,899
everything went well
4766
03:05:51,899 --> 03:05:54,660
we create a const nodes
4767
03:05:54,660 --> 03:05:56,880
and then we have to add another weight
4768
03:05:56,880 --> 03:05:58,920
because passing the data out of the
4769
03:05:58,920 --> 03:06:00,479
response is another asynchronous
4770
03:06:00,479 --> 03:06:01,620
operation
4771
03:06:01,620 --> 03:06:04,620
then we take response and carl.json
4772
03:06:04,620 --> 03:06:07,260
which passes the Json body out of this
4773
03:06:07,260 --> 03:06:09,840
response and when we take a look into
4774
03:06:09,840 --> 03:06:10,979
our
4775
03:06:10,979 --> 03:06:14,040
nodes controller
4776
03:06:14,040 --> 03:06:17,340
we remember that real
4777
03:06:17,340 --> 03:06:19,140
return our nodes
4778
03:06:19,140 --> 03:06:22,140
as a Json to the front end so this is
4779
03:06:22,140 --> 03:06:24,000
what we get out of here when we pass
4780
03:06:24,000 --> 03:06:25,200
this Json
4781
03:06:25,200 --> 03:06:28,020
and then recall set notes which is our
4782
03:06:28,020 --> 03:06:30,899
state function here and paste in your
4783
03:06:30,899 --> 03:06:32,340
notes
4784
03:06:32,340 --> 03:06:34,620
this way we update our state and
4785
03:06:34,620 --> 03:06:36,600
wherever we use these nodes in the UI
4786
03:06:36,600 --> 03:06:39,899
this part now gets updated by react and
4787
03:06:39,899 --> 03:06:42,479
we can display this new state one more
4788
03:06:42,479 --> 03:06:44,580
thing that's important we have to pass a
4789
03:06:44,580 --> 03:06:46,740
dependency array to use effect we do
4790
03:06:46,740 --> 03:06:49,020
this down here after the function which
4791
03:06:49,020 --> 03:06:51,120
is the first argument that we passed
4792
03:06:51,120 --> 03:06:54,120
rewrite the comma and an array with two
4793
03:06:54,120 --> 03:06:56,160
scrap brackets in here you can pass
4794
03:06:56,160 --> 03:06:58,500
variables that whenever they change they
4795
03:06:58,500 --> 03:07:00,960
will execute this use effect again if we
4796
03:07:00,960 --> 03:07:02,939
pass an empty array like this then use
4797
03:07:02,939 --> 03:07:05,279
effect we'll only execute one time at
4798
03:07:05,279 --> 03:07:06,899
the beginning this is exactly what we
4799
03:07:06,899 --> 03:07:09,359
want however if we would pass no array
4800
03:07:09,359 --> 03:07:11,939
at all this would again execute on every
4801
03:07:11,939 --> 03:07:13,859
single render and this is pretty much
4802
03:07:13,859 --> 03:07:16,020
never what we want because this is the
4803
03:07:16,020 --> 03:07:17,939
same effect as if we put it directly
4804
03:07:17,939 --> 03:07:19,560
inside the body without the use effect
4805
03:07:19,560 --> 03:07:21,479
so if you want to execute something one
4806
03:07:21,479 --> 03:07:23,399
time you pass an empty array like this
4807
03:07:23,399 --> 03:07:26,220
okay and we haven't designed a layout
4808
03:07:26,220 --> 03:07:28,020
for our nodes yet we will do this later
4809
03:07:28,020 --> 03:07:30,960
for now let's just display the data that
4810
03:07:30,960 --> 03:07:33,000
we get back in the form of a string so
4811
03:07:33,000 --> 03:07:35,040
what we do is we remove this whole
4812
03:07:35,040 --> 03:07:37,380
header here because we will create our
4813
03:07:37,380 --> 03:07:38,880
own layout later
4814
03:07:38,880 --> 03:07:40,979
and we want to display the value of our
4815
03:07:40,979 --> 03:07:42,779
state variables so we make a pair of
4816
03:07:42,779 --> 03:07:45,359
curly braces and in between we write
4817
03:07:45,359 --> 03:07:48,060
Json and uppercase
4818
03:07:48,060 --> 03:07:50,760
dot stringifier which as the name
4819
03:07:50,760 --> 03:07:52,859
implies can turn a Json into a string
4820
03:07:52,859 --> 03:07:54,779
such as that we see that we actually get
4821
03:07:54,779 --> 03:07:56,819
some data back and here we pass our
4822
03:07:56,819 --> 03:07:58,620
nodes
4823
03:07:58,620 --> 03:08:01,200
then we save this and we are try to run
4824
03:08:01,200 --> 03:08:04,140
this first of all since I closed
4825
03:08:04,140 --> 03:08:06,120
everything I have to start the servers
4826
03:08:06,120 --> 03:08:07,800
again both the front and and the backend
4827
03:08:07,800 --> 03:08:11,880
so ICD into the backend code
4828
03:08:11,880 --> 03:08:15,779
Run npm start here to start a server and
4829
03:08:15,779 --> 03:08:17,520
then I'm gonna create a new terminal so
4830
03:08:17,520 --> 03:08:19,260
if we have a separate one for the front
4831
03:08:19,260 --> 03:08:21,720
end and again you can do it via this
4832
03:08:21,720 --> 03:08:25,260
command here a new terminal
4833
03:08:25,260 --> 03:08:29,100
here icdn to the front end
4834
03:08:29,100 --> 03:08:32,340
and Run npm start here as well
4835
03:08:32,340 --> 03:08:34,020
and then react will open this on
4836
03:08:34,020 --> 03:08:36,300
localhost 3000.
4837
03:08:36,300 --> 03:08:39,240
and let's see what happens so this is
4838
03:08:39,240 --> 03:08:42,720
the alert dialog that we wrote here
4839
03:08:42,720 --> 03:08:44,520
which means that there's an error that
4840
03:08:44,520 --> 03:08:46,080
happened so let's look into the
4841
03:08:46,080 --> 03:08:48,359
developer console which is a good habit
4842
03:08:48,359 --> 03:08:51,479
to get used to anyway as a web developer
4843
03:08:51,479 --> 03:08:55,200
let's refresh this to see the error
4844
03:08:55,200 --> 03:08:58,439
here it is in beautiful red
4845
03:08:58,439 --> 03:09:01,260
and it says course but we see the actual
4846
03:09:01,260 --> 03:09:03,899
error message in the console failed to
4847
03:09:03,899 --> 03:09:07,680
fetch access to a fetch at our server
4848
03:09:07,680 --> 03:09:10,979
endpoint from our front end address has
4849
03:09:10,979 --> 03:09:13,439
been blocked by course course stands for
4850
03:09:13,439 --> 03:09:15,899
cross origin resource sharing and it's
4851
03:09:15,899 --> 03:09:18,240
basically a security mechanism that
4852
03:09:18,240 --> 03:09:20,399
doesn't allow our front end to fetch the
4853
03:09:20,399 --> 03:09:22,380
data from our server because they are on
4854
03:09:22,380 --> 03:09:25,319
different addresses localhost 3000 and
4855
03:09:25,319 --> 03:09:28,260
5000 count as different addresses now
4856
03:09:28,260 --> 03:09:29,700
there are two ways to get around this
4857
03:09:29,700 --> 03:09:31,620
either we have to configure on our
4858
03:09:31,620 --> 03:09:34,319
server that we are allowed to fetch data
4859
03:09:34,319 --> 03:09:36,479
from this address but this problem
4860
03:09:36,479 --> 03:09:38,520
actually doesn't exist when both the
4861
03:09:38,520 --> 03:09:40,500
front end and the back end run on the
4862
03:09:40,500 --> 03:09:42,540
same address and later in production we
4863
03:09:42,540 --> 03:09:44,399
will actually deploy everything to the
4864
03:09:44,399 --> 03:09:46,680
same URL just that our server code will
4865
03:09:46,680 --> 03:09:49,859
be behind this slash API endpoints but
4866
03:09:49,859 --> 03:09:51,420
later in production we will not have
4867
03:09:51,420 --> 03:09:53,520
this course problem we only have it now
4868
03:09:53,520 --> 03:09:55,140
in development because we have these
4869
03:09:55,140 --> 03:09:57,240
different localhost addresses but there
4870
03:09:57,240 --> 03:09:59,340
is something we can configure in reacted
4871
03:09:59,340 --> 03:10:02,580
that will make it look as if the request
4872
03:10:02,580 --> 03:10:05,160
is going to our own address we will do
4873
03:10:05,160 --> 03:10:06,479
this in a moment with something called
4874
03:10:06,479 --> 03:10:08,640
proxy first I want to clarify a few
4875
03:10:08,640 --> 03:10:10,680
other things first of all why did
4876
03:10:10,680 --> 03:10:12,660
Postman not have this problem why was
4877
03:10:12,660 --> 03:10:14,939
Postman allowed to fetch these resources
4878
03:10:14,939 --> 03:10:17,040
this is because the security mechanism
4879
03:10:17,040 --> 03:10:19,200
is actually enforced not by our server
4880
03:10:19,200 --> 03:10:21,540
but by the browsers so Chrome for
4881
03:10:21,540 --> 03:10:23,819
example implements the security feature
4882
03:10:23,819 --> 03:10:25,859
and every browser can decide if they
4883
03:10:25,859 --> 03:10:27,840
want to adhere to this or not and
4884
03:10:27,840 --> 03:10:29,760
Postman just ignores it basically
4885
03:10:29,760 --> 03:10:31,800
because it's a development tool Postman
4886
03:10:31,800 --> 03:10:33,300
knows that we don't need a security
4887
03:10:33,300 --> 03:10:35,880
mechanism when we use it because it's
4888
03:10:35,880 --> 03:10:37,380
not a real browser we're not actually
4889
03:10:37,380 --> 03:10:39,540
observing the web like a normal user the
4890
03:10:39,540 --> 03:10:41,340
other thing I want to clarify is as you
4891
03:10:41,340 --> 03:10:43,680
can see everything gets executed twice
4892
03:10:43,680 --> 03:10:46,020
we see our error messages drives and
4893
03:10:46,020 --> 03:10:47,700
when we refresh this we should also see
4894
03:10:47,700 --> 03:10:50,279
the alert message twice don't get
4895
03:10:50,279 --> 03:10:52,740
confused by this react just renders
4896
03:10:52,740 --> 03:10:55,260
everything twice in development as a
4897
03:10:55,260 --> 03:10:56,819
reasoning behind it that is written
4898
03:10:56,819 --> 03:10:58,740
somewhere it makes debugging easier
4899
03:10:58,740 --> 03:11:00,779
apparently but yeah it executes
4900
03:11:00,779 --> 03:11:02,520
everything twice so don't get confused
4901
03:11:02,520 --> 03:11:04,080
by this this will not happen in
4902
03:11:04,080 --> 03:11:05,700
production it's just a development
4903
03:11:05,700 --> 03:11:08,279
feature but yeah now let's go back into
4904
03:11:08,279 --> 03:11:11,460
our project and implement this proxy I
4905
03:11:11,460 --> 03:11:13,380
was talking about for this we go into
4906
03:11:13,380 --> 03:11:17,340
the package.json of our front end
4907
03:11:17,340 --> 03:11:19,319
which was generated automatically
4908
03:11:19,319 --> 03:11:21,300
earlier and somewhere in here we put
4909
03:11:21,300 --> 03:11:23,160
another key I'm going to put it above
4910
03:11:23,160 --> 03:11:24,960
the dependencies
4911
03:11:24,960 --> 03:11:29,220
which is called proxy colon and then we
4912
03:11:29,220 --> 03:11:30,960
basically just pass the URL of our
4913
03:11:30,960 --> 03:11:34,680
server in form of a string so HTTP colon
4914
03:11:34,680 --> 03:11:36,540
slash slash
4915
03:11:36,540 --> 03:11:39,779
localhost colon 5000.
4916
03:11:39,779 --> 03:11:42,359
we saved this then we go back into the
4917
03:11:42,359 --> 03:11:45,420
ftsx file and just remove this part here
4918
03:11:45,420 --> 03:11:48,359
because now the proxy knows that these
4919
03:11:48,359 --> 03:11:51,720
relative URLs will be added to this URL
4920
03:11:51,720 --> 03:11:53,700
and this way it also doesn't have
4921
03:11:53,700 --> 03:11:55,800
problems with calls because it
4922
03:11:55,800 --> 03:11:58,439
recognizes this as its own URL basically
4923
03:11:58,439 --> 03:12:01,319
this is just how this proxy works then
4924
03:12:01,319 --> 03:12:03,060
we have to save everything and we have
4925
03:12:03,060 --> 03:12:05,279
to restart our front end otherwise this
4926
03:12:05,279 --> 03:12:07,859
proxy doesn't go into effect so we open
4927
03:12:07,859 --> 03:12:10,260
the terminal of the front end press Ctrl
4928
03:12:10,260 --> 03:12:12,000
Z confirm it
4929
03:12:12,000 --> 03:12:15,060
and Run npm start again
4930
03:12:15,060 --> 03:12:17,340
and lint complains about some unused
4931
03:12:17,340 --> 03:12:18,540
stuff
4932
03:12:18,540 --> 03:12:20,399
but it's just because we haven't deleted
4933
03:12:20,399 --> 03:12:23,040
it you can do this if you want but it
4934
03:12:23,040 --> 03:12:25,319
automatically opens the page again and
4935
03:12:25,319 --> 03:12:28,200
you can see our notes array from the
4936
03:12:28,200 --> 03:12:30,120
square bracket you can see that this is
4937
03:12:30,120 --> 03:12:31,680
a Json array
4938
03:12:31,680 --> 03:12:34,080
and it contains our first node and our
4939
03:12:34,080 --> 03:12:36,240
other one and even though our website
4940
03:12:36,240 --> 03:12:38,700
wouldn't win a design price yet it's
4941
03:12:38,700 --> 03:12:40,979
already a queer feeling that we receive
4942
03:12:40,979 --> 03:12:43,080
our notes from our own server on our
4943
03:12:43,080 --> 03:12:45,180
react front end right and this is the
4944
03:12:45,180 --> 03:12:47,220
raw data and we can then use this raw
4945
03:12:47,220 --> 03:12:48,720
data to put it into good looking
4946
03:12:48,720 --> 03:12:50,700
components and display them in the UI
4947
03:12:50,700 --> 03:12:52,380
this is what we will do in the rest of
4948
03:12:52,380 --> 03:12:54,300
this course one more thing I want to
4949
03:12:54,300 --> 03:12:56,640
clarify this proxy approach works
4950
03:12:56,640 --> 03:12:59,220
because we have our own server and our
4951
03:12:59,220 --> 03:13:01,380
own front end if you want to build a
4952
03:13:01,380 --> 03:13:04,200
public API where different clients can
4953
03:13:04,200 --> 03:13:06,180
access it then you might have to set up
4954
03:13:06,180 --> 03:13:07,859
course properly to allow these different
4955
03:13:07,859 --> 03:13:09,180
Origins
4956
03:13:09,180 --> 03:13:11,100
and you can do this very easily with
4957
03:13:11,100 --> 03:13:13,319
this course package here you can install
4958
03:13:13,319 --> 03:13:15,060
this on the back end with npm install
4959
03:13:15,060 --> 03:13:17,819
course and then the setup is explained
4960
03:13:17,819 --> 03:13:20,340
here it's a very popular package over 5
4961
03:13:20,340 --> 03:13:22,500
million weekly downloads
4962
03:13:22,500 --> 03:13:25,140
but we don't need it because we are not
4963
03:13:25,140 --> 03:13:27,540
building a public API but just in case
4964
03:13:27,540 --> 03:13:29,399
you want to build something like this
4965
03:13:29,399 --> 03:13:31,319
and by the way if you were got to this
4966
03:13:31,319 --> 03:13:32,819
point and haven't liked the video yet
4967
03:13:32,819 --> 03:13:34,140
then I really don't know what's wrong
4968
03:13:34,140 --> 03:13:36,660
with you leave some support I put out a
4969
03:13:36,660 --> 03:13:39,120
huge course for free and you can help by
4970
03:13:39,120 --> 03:13:40,979
just leaving a like on the video because
4971
03:13:40,979 --> 03:13:43,380
it helps this video grow better and
4972
03:13:43,380 --> 03:13:47,180
reach more people thank you very much
4973
03:13:50,760 --> 03:13:53,279
okay to make our notes better looking
4974
03:13:53,279 --> 03:13:56,939
than this string representation here
4975
03:13:56,939 --> 03:13:59,279
we have to create a component for our
4976
03:13:59,279 --> 03:14:01,260
nodes a component that contains the
4977
03:14:01,260 --> 03:14:04,740
layout of each node Zoom we go into a
4978
03:14:04,740 --> 03:14:07,140
desert bar here into our front-end
4979
03:14:07,140 --> 03:14:09,600
folder and inside source we want to
4980
03:14:09,600 --> 03:14:11,100
create a new folder which we call
4981
03:14:11,100 --> 03:14:12,779
components
4982
03:14:12,779 --> 03:14:15,060
and this contains all kinds of single
4983
03:14:15,060 --> 03:14:17,100
components as opposed to stuff like
4984
03:14:17,100 --> 03:14:19,800
models or later our Pages which also go
4985
03:14:19,800 --> 03:14:22,140
into a different folder we right click
4986
03:14:22,140 --> 03:14:24,359
on our new components folder create a
4987
03:14:24,359 --> 03:14:25,800
new file
4988
03:14:25,800 --> 03:14:29,340
and colored node.tsx this time so this
4989
03:14:29,340 --> 03:14:31,859
is this HTML JavaScript file and not
4990
03:14:31,859 --> 03:14:34,560
just a normal typescript file
4991
03:14:34,560 --> 03:14:37,439
and react components come in tour forms
4992
03:14:37,439 --> 03:14:40,140
either a class or a function I've
4993
03:14:40,140 --> 03:14:42,300
actually never built a component as a
4994
03:14:42,300 --> 03:14:44,279
class because this is the old approach
4995
03:14:44,279 --> 03:14:46,319
this is what I used in the past before I
4996
03:14:46,319 --> 03:14:48,000
started learning react now these
4997
03:14:48,000 --> 03:14:49,920
components are all created in form of
4998
03:14:49,920 --> 03:14:52,380
functions and the app itself which is
4999
03:14:52,380 --> 03:14:54,479
our front page basically is also a
5000
03:14:54,479 --> 03:14:57,000
function so such a function creates a
5001
03:14:57,000 --> 03:14:59,340
piece of UI basically and we want to do
5002
03:14:59,340 --> 03:15:01,260
the same thing for our node component
5003
03:15:01,260 --> 03:15:03,660
now we will type our dysfunction by hand
5004
03:15:03,660 --> 03:15:05,640
once and then we will install an
5005
03:15:05,640 --> 03:15:07,979
extension for the s-code that gives us a
5006
03:15:07,979 --> 03:15:10,020
shortcut for that so we can either write
5007
03:15:10,020 --> 03:15:12,479
it like this function or we can declare
5008
03:15:12,479 --> 03:15:14,279
it as an error function and the
5009
03:15:14,279 --> 03:15:16,260
extension we will later use also creates
5010
03:15:16,260 --> 03:15:18,300
error function so let's keep this style
5011
03:15:18,300 --> 03:15:20,760
so we create a const
5012
03:15:20,760 --> 03:15:22,620
uppercase node
5013
03:15:22,620 --> 03:15:24,359
equals
5014
03:15:24,359 --> 03:15:26,160
and then we create an error function
5015
03:15:26,160 --> 03:15:28,560
like this a pair of parentheses a right
5016
03:15:28,560 --> 03:15:33,359
arrow and a pair of curly braces
5017
03:15:33,359 --> 03:15:36,420
and then below we want to export this
5018
03:15:36,420 --> 03:15:40,380
function so we write export default node
5019
03:15:40,380 --> 03:15:42,420
and now in here we can declare the UI
5020
03:15:42,420 --> 03:15:44,520
for a node and then we can use this in
5021
03:15:44,520 --> 03:15:47,279
different places in our app but in order
5022
03:15:47,279 --> 03:15:49,979
to display the data of each node we have
5023
03:15:49,979 --> 03:15:51,960
to pass the nodes to this component
5024
03:15:51,960 --> 03:15:54,240
right otherwise where do we get the data
5025
03:15:54,240 --> 03:15:55,380
from
5026
03:15:55,380 --> 03:15:58,200
so to declare what types of data this
5027
03:15:58,200 --> 03:16:00,060
node should receive we have to create
5028
03:16:00,060 --> 03:16:02,340
another interface
5029
03:16:02,340 --> 03:16:04,080
again this is only necessary in
5030
03:16:04,080 --> 03:16:06,240
typescript and JavaScript you don't need
5031
03:16:06,240 --> 03:16:08,040
types so you can just pass everything
5032
03:16:08,040 --> 03:16:09,960
nearly Billy but we don't want to do
5033
03:16:09,960 --> 03:16:11,700
that we want to be a bit more
5034
03:16:11,700 --> 03:16:14,160
descriptive but safe and you can call
5035
03:16:14,160 --> 03:16:16,680
this interface anything you want I think
5036
03:16:16,680 --> 03:16:18,840
a good naming convention is the name of
5037
03:16:18,840 --> 03:16:20,580
the component itself
5038
03:16:20,580 --> 03:16:23,580
and then props appended to it props is
5039
03:16:23,580 --> 03:16:25,740
what we call the arguments that we pass
5040
03:16:25,740 --> 03:16:27,720
to our component I think it's short for
5041
03:16:27,720 --> 03:16:29,040
properties
5042
03:16:29,040 --> 03:16:32,640
and we want to pass a note to it what
5043
03:16:32,640 --> 03:16:35,460
kind of note our node model so we write
5044
03:16:35,460 --> 03:16:38,880
note and import this model's node type
5045
03:16:38,880 --> 03:16:41,700
here now it's a bit confusing that our
5046
03:16:41,700 --> 03:16:44,160
node model and our component here have
5047
03:16:44,160 --> 03:16:46,020
the same name and this can also cause
5048
03:16:46,020 --> 03:16:47,939
problems but I don't really want to name
5049
03:16:47,939 --> 03:16:50,460
them differently but we can use an alias
5050
03:16:50,460 --> 03:16:52,380
for the name so what we can do is we can
5051
03:16:52,380 --> 03:16:55,439
go up here behind import node
5052
03:16:55,439 --> 03:17:00,779
and write as note oops node model this
5053
03:17:00,779 --> 03:17:02,760
way we keep the name of our node
5054
03:17:02,760 --> 03:17:05,460
interface here as it is
5055
03:17:05,460 --> 03:17:07,560
but we use it under a different name
5056
03:17:07,560 --> 03:17:10,140
inside this component so in here we
5057
03:17:10,140 --> 03:17:12,479
change this to node model as well
5058
03:17:12,479 --> 03:17:14,279
gonna add a trailing comma it's not
5059
03:17:14,279 --> 03:17:16,620
necessary but I like that and then we
5060
03:17:16,620 --> 03:17:18,720
add this as an argument to our node
5061
03:17:18,720 --> 03:17:20,760
function here and we use this
5062
03:17:20,760 --> 03:17:23,040
destructuring syntax where we write a
5063
03:17:23,040 --> 03:17:25,920
pair of curly braces
5064
03:17:25,920 --> 03:17:29,040
the name of the variable the name of the
5065
03:17:29,040 --> 03:17:30,899
parameter which is the same as up here
5066
03:17:30,899 --> 03:17:33,779
node and outside of the curly braces we
5067
03:17:33,779 --> 03:17:38,040
write colon note props and if we add any
5068
03:17:38,040 --> 03:17:39,840
more arguments up here to the interface
5069
03:17:39,840 --> 03:17:42,060
then we also add them to this list so
5070
03:17:42,060 --> 03:17:44,279
comma and then the next one
5071
03:17:44,279 --> 03:17:46,500
and now we can use this node data inside
5072
03:17:46,500 --> 03:17:49,020
this node component and what we want to
5073
03:17:49,020 --> 03:17:51,300
do is we want to draw some cards on the
5074
03:17:51,300 --> 03:17:54,600
screen from the react bootstrap package
5075
03:17:54,600 --> 03:17:57,120
because they look pretty good
5076
03:17:57,120 --> 03:17:59,040
you can read the documentation with all
5077
03:17:59,040 --> 03:18:00,899
the features that are available all the
5078
03:18:00,899 --> 03:18:03,120
styling options
5079
03:18:03,120 --> 03:18:04,979
but for this tutorial you can just
5080
03:18:04,979 --> 03:18:07,859
follow what I write here so in this
5081
03:18:07,859 --> 03:18:10,140
function body here we want to return
5082
03:18:10,140 --> 03:18:12,899
something because what we return is the
5083
03:18:12,899 --> 03:18:15,180
UI that gets drawn on the screen
5084
03:18:15,180 --> 03:18:17,580
we put it between parentheses because
5085
03:18:17,580 --> 03:18:20,100
this allows us to write something below
5086
03:18:20,100 --> 03:18:22,920
it Beyond many lines and this just makes
5087
03:18:22,920 --> 03:18:25,140
these parentheses necessary so that the
5088
03:18:25,140 --> 03:18:27,120
return statement knows what belongs to
5089
03:18:27,120 --> 03:18:28,500
the return
5090
03:18:28,500 --> 03:18:30,420
here we write open the anchor bracket
5091
03:18:30,420 --> 03:18:33,479
uppercase cut and we import it from
5092
03:18:33,479 --> 03:18:35,340
react bootstrap
5093
03:18:35,340 --> 03:18:38,100
close this which automatically adds the
5094
03:18:38,100 --> 03:18:39,779
closing tag
5095
03:18:39,779 --> 03:18:42,899
and here we put a card dot body tag
5096
03:18:42,899 --> 03:18:45,300
close it as well
5097
03:18:45,300 --> 03:18:47,819
and then another one in here cards dot
5098
03:18:47,819 --> 03:18:49,439
title
5099
03:18:49,439 --> 03:18:51,779
and for the title we want to use the
5100
03:18:51,779 --> 03:18:54,000
title of the note so we make a pair of
5101
03:18:54,000 --> 03:18:56,939
curly braces and in here we write
5102
03:18:56,939 --> 03:18:59,939
note which is the node model that we
5103
03:18:59,939 --> 03:19:03,060
pass to the component dot title
5104
03:19:03,060 --> 03:19:05,520
let's leave it like that for now see how
5105
03:19:05,520 --> 03:19:07,560
it looks and then add the rest of the
5106
03:19:07,560 --> 03:19:10,680
notes to it arguments that you pass to a
5107
03:19:10,680 --> 03:19:12,779
component like our node here worked the
5108
03:19:12,779 --> 03:19:15,540
same as state whenever a state changes
5109
03:19:15,540 --> 03:19:18,180
react knows that it has to update the UI
5110
03:19:18,180 --> 03:19:20,700
that depends on the state whenever an
5111
03:19:20,700 --> 03:19:22,380
argument that we pass to a function
5112
03:19:22,380 --> 03:19:24,479
changes react knows that it has to
5113
03:19:24,479 --> 03:19:26,340
update this component as well which
5114
03:19:26,340 --> 03:19:28,319
means that if we make a change to a this
5115
03:19:28,319 --> 03:19:31,140
node object that we pass to this node
5116
03:19:31,140 --> 03:19:34,080
then react knows that it has to withdraw
5117
03:19:34,080 --> 03:19:36,660
this component and show the latest data
5118
03:19:36,660 --> 03:19:40,500
let's go over to the ftsx file again
5119
03:19:40,500 --> 03:19:43,080
first of all let's change the name of
5120
03:19:43,080 --> 03:19:44,880
our node model here as well so we don't
5121
03:19:44,880 --> 03:19:46,800
have this clash between the model type
5122
03:19:46,800 --> 03:19:48,840
and the component name
5123
03:19:48,840 --> 03:19:52,260
we change this to node model as well
5124
03:19:52,260 --> 03:19:55,560
and then we change the name here in your
5125
03:19:55,560 --> 03:19:56,700
state
5126
03:19:56,700 --> 03:19:58,979
and then instead of just stringifying
5127
03:19:58,979 --> 03:20:01,080
the notes we want to turn our notes into
5128
03:20:01,080 --> 03:20:03,960
this note card we just created
5129
03:20:03,960 --> 03:20:06,300
so we keep the div
5130
03:20:06,300 --> 03:20:09,540
add a pair of curly braces then we take
5131
03:20:09,540 --> 03:20:11,880
our notes which is our data the node
5132
03:20:11,880 --> 03:20:14,819
model array and Carl dot map
5133
03:20:14,819 --> 03:20:17,340
and map is not something specific to
5134
03:20:17,340 --> 03:20:19,560
react but if you're not familiar with it
5135
03:20:19,560 --> 03:20:22,439
map allows us to take some data like the
5136
03:20:22,439 --> 03:20:24,540
array of our nodes here and turn it into
5137
03:20:24,540 --> 03:20:26,819
something different this is exactly what
5138
03:20:26,819 --> 03:20:28,740
we want to do we want to take our raw
5139
03:20:28,740 --> 03:20:31,200
node models and turn them into a node
5140
03:20:31,200 --> 03:20:33,120
component objects and we do this with
5141
03:20:33,120 --> 03:20:36,359
map and since we are in this TSX or jsx
5142
03:20:36,359 --> 03:20:38,640
file we can write normal JavaScript in
5143
03:20:38,640 --> 03:20:40,859
here we can add Loops we can add map
5144
03:20:40,859 --> 03:20:43,080
calls and so on this makes this whole
5145
03:20:43,080 --> 03:20:45,060
thing very flexible because now we can
5146
03:20:45,060 --> 03:20:47,580
map our data directly to this HTML
5147
03:20:47,580 --> 03:20:49,979
components so map is a function so we
5148
03:20:49,979 --> 03:20:52,020
add a pair of parentheses and then we
5149
03:20:52,020 --> 03:20:54,120
have to pass an error function this
5150
03:20:54,120 --> 03:20:56,460
error function takes an argument which
5151
03:20:56,460 --> 03:20:59,399
is each node object in our array so we
5152
03:20:59,399 --> 03:21:02,279
write node and the right arrow so this
5153
03:21:02,279 --> 03:21:04,319
way we get past each single node of our
5154
03:21:04,319 --> 03:21:06,479
array and now we can decide what we want
5155
03:21:06,479 --> 03:21:08,460
to transform it into we want to write
5156
03:21:08,460 --> 03:21:10,439
this in a new line so we add a pair of
5157
03:21:10,439 --> 03:21:11,819
parentheses
5158
03:21:11,819 --> 03:21:13,680
just like we did over in our other
5159
03:21:13,680 --> 03:21:16,020
component in a return statement and now
5160
03:21:16,020 --> 03:21:17,880
here we want to create our node
5161
03:21:17,880 --> 03:21:20,279
component so we write opening angle
5162
03:21:20,279 --> 03:21:22,740
bracket node which comes now from our
5163
03:21:22,740 --> 03:21:24,960
components folder
5164
03:21:24,960 --> 03:21:27,479
this is the node TSX component we just
5165
03:21:27,479 --> 03:21:28,800
created
5166
03:21:28,800 --> 03:21:31,319
we close this with a slash and a closing
5167
03:21:31,319 --> 03:21:33,779
angle bracket and this still shows an
5168
03:21:33,779 --> 03:21:36,899
error because this expects an argument
5169
03:21:36,899 --> 03:21:39,240
which is the argument that we defined up
5170
03:21:39,240 --> 03:21:41,819
here it expects a note where do we get
5171
03:21:41,819 --> 03:21:43,620
this node from we get it from the map
5172
03:21:43,620 --> 03:21:46,319
card here so we pass it in between now
5173
03:21:46,319 --> 03:21:48,000
one more thing whenever we have a list
5174
03:21:48,000 --> 03:21:51,120
of stuff like this our list of nodes
5175
03:21:51,120 --> 03:21:53,520
we have to add a key
5176
03:21:53,520 --> 03:21:55,979
to the object as well this key property
5177
03:21:55,979 --> 03:21:57,899
is there automatically we didn't have to
5178
03:21:57,899 --> 03:22:00,479
add it to our node it's just added by
5179
03:22:00,479 --> 03:22:02,760
react by default now sometimes lint
5180
03:22:02,760 --> 03:22:04,680
complains about this but this time it
5181
03:22:04,680 --> 03:22:06,660
doesn't I'm not really sure why but we
5182
03:22:06,660 --> 03:22:08,340
should add the skill otherwise there
5183
03:22:08,340 --> 03:22:10,080
will be a warning in the console later
5184
03:22:10,080 --> 03:22:13,319
equal Zone curly braces and for the
5185
03:22:13,319 --> 03:22:14,939
value of the key we have to pass a
5186
03:22:14,939 --> 03:22:17,220
unique identifier for each node this is
5187
03:22:17,220 --> 03:22:19,560
just necessary for react to know when it
5188
03:22:19,560 --> 03:22:21,840
has to redraw this node and what is a
5189
03:22:21,840 --> 03:22:23,220
unique identifier
5190
03:22:23,220 --> 03:22:26,279
now the idea is always unique to each
5191
03:22:26,279 --> 03:22:28,500
node so let's use that one
5192
03:22:28,500 --> 03:22:32,100
so let's save this and see how it looks
5193
03:22:32,100 --> 03:22:35,399
so I open our localhost again and we
5194
03:22:35,399 --> 03:22:37,620
have our two nodes it's still not
5195
03:22:37,620 --> 03:22:39,420
beautiful but something happened on the
5196
03:22:39,420 --> 03:22:41,479
screen this is supposed to be a card
5197
03:22:41,479 --> 03:22:44,580
this is the card's title and this is our
5198
03:22:44,580 --> 03:22:46,560
two notes so now let's improve the
5199
03:22:46,560 --> 03:22:49,319
styling so let's go back into the node
5200
03:22:49,319 --> 03:22:52,080
TSX file and finish the styling here
5201
03:22:52,080 --> 03:22:54,479
first of all we don't only want to use
5202
03:22:54,479 --> 03:22:56,340
the title value in here we want to use
5203
03:22:56,340 --> 03:22:58,819
more values and instead of writing
5204
03:22:58,819 --> 03:23:02,160
notes.title note dot text and so on we
5205
03:23:02,160 --> 03:23:04,380
can use the same destructuring syntax we
5206
03:23:04,380 --> 03:23:06,899
used to appear to unpack these single
5207
03:23:06,899 --> 03:23:08,880
Fields basically that's just a simple
5208
03:23:08,880 --> 03:23:10,920
JavaScript feature what we do is we
5209
03:23:10,920 --> 03:23:12,300
write const
5210
03:23:12,300 --> 03:23:15,720
a pair of curly braces after the closing
5211
03:23:15,720 --> 03:23:18,840
curly brace you write equals note
5212
03:23:18,840 --> 03:23:21,000
so we are unpacking this node property
5213
03:23:21,000 --> 03:23:22,020
here
5214
03:23:22,020 --> 03:23:23,279
and let's see if we have other
5215
03:23:23,279 --> 03:23:25,020
completion yeah we have because I
5216
03:23:25,020 --> 03:23:27,600
already added this equals node and then
5217
03:23:27,600 --> 03:23:29,819
we want to list each field that we want
5218
03:23:29,819 --> 03:23:31,620
to use here in our component we don't
5219
03:23:31,620 --> 03:23:33,720
need the idea but the other ones
5220
03:23:33,720 --> 03:23:37,100
supervised title comma text
5221
03:23:37,100 --> 03:23:39,540
created at comma
5222
03:23:39,540 --> 03:23:43,260
updated at comma and now we can just
5223
03:23:43,260 --> 03:23:45,300
write title here instead of note or
5224
03:23:45,300 --> 03:23:47,700
title which I just like more because we
5225
03:23:47,700 --> 03:23:49,439
already know that we are inside the node
5226
03:23:49,439 --> 03:23:52,140
component so this word is enough in my
5227
03:23:52,140 --> 03:23:56,520
opinion so below a card dot title
5228
03:23:56,520 --> 03:24:00,620
we add the card dot text
5229
03:24:00,899 --> 03:24:03,060
again you can see these different texts
5230
03:24:03,060 --> 03:24:04,680
and the combinations of them in the
5231
03:24:04,680 --> 03:24:06,779
bootstrap documentation
5232
03:24:06,779 --> 03:24:09,239
here we want to write the text let's
5233
03:24:09,239 --> 03:24:10,859
save this and we see the changes
5234
03:24:10,859 --> 03:24:12,000
immediately
5235
03:24:12,000 --> 03:24:14,760
only this node here has a text but of
5236
03:24:14,760 --> 03:24:16,560
course we also want to style these notes
5237
03:24:16,560 --> 03:24:19,080
with CSS we're going to change the shape
5238
03:24:19,080 --> 03:24:21,600
the colors we want to add Shadows later
5239
03:24:21,600 --> 03:24:24,180
and so on so we have to write some CSS
5240
03:24:24,180 --> 03:24:27,260
now we could put the styling into the
5241
03:24:27,260 --> 03:24:30,120
app.css file that we already have
5242
03:24:30,120 --> 03:24:32,160
there's already some default code in
5243
03:24:32,160 --> 03:24:34,500
here that we got when we render create
5244
03:24:34,500 --> 03:24:36,779
react app command but I don't want to
5245
03:24:36,779 --> 03:24:39,660
write Global CSS because this later
5246
03:24:39,660 --> 03:24:41,760
makes it difficult to organize your CSS
5247
03:24:41,760 --> 03:24:43,739
properly because there can be clashes
5248
03:24:43,739 --> 03:24:45,960
between different classes with the same
5249
03:24:45,960 --> 03:24:48,540
names instead we want to use CSS modules
5250
03:24:48,540 --> 03:24:50,760
so what we do is first of all we delete
5251
03:24:50,760 --> 03:24:54,479
this app CSS file so we selected press
5252
03:24:54,479 --> 03:24:57,840
the delete key and delete the sucker we
5253
03:24:57,840 --> 03:24:59,700
also have to remove the import statement
5254
03:24:59,700 --> 03:25:03,479
here from the app TSX file
5255
03:25:03,479 --> 03:25:07,080
so it compiles again and then we want to
5256
03:25:07,080 --> 03:25:09,300
remove this class name app here because
5257
03:25:09,300 --> 03:25:12,060
we just deleted this whole CSS file so
5258
03:25:12,060 --> 03:25:14,040
let's remove that as well
5259
03:25:14,040 --> 03:25:17,160
and we still have this index CSS file
5260
03:25:17,160 --> 03:25:20,100
which is imported in the index TSS file
5261
03:25:20,100 --> 03:25:21,120
here
5262
03:25:21,120 --> 03:25:23,100
but we don't want to delete this we want
5263
03:25:23,100 --> 03:25:25,800
to make this our Global CSS file because
5264
03:25:25,800 --> 03:25:28,380
even when we use CSS modules we want to
5265
03:25:28,380 --> 03:25:31,080
have one single Global CSS file for some
5266
03:25:31,080 --> 03:25:33,300
stuff that we just want to set up once
5267
03:25:33,300 --> 03:25:35,640
globally and not for each component like
5268
03:25:35,640 --> 03:25:38,040
the font throughout our app for example
5269
03:25:38,040 --> 03:25:40,439
so what we do is we change the name of
5270
03:25:40,439 --> 03:25:42,960
index CSS by clicking on it and pressing
5271
03:25:42,960 --> 03:25:46,140
F2 on the keyboard or right click rename
5272
03:25:46,140 --> 03:25:48,960
whatever you want let's call This Global
5273
03:25:48,960 --> 03:25:51,600
CSS to make it clear that this is a
5274
03:25:51,600 --> 03:25:53,460
global CSS file
5275
03:25:53,460 --> 03:25:56,460
going to the index file change the name
5276
03:25:56,460 --> 03:25:58,380
here as well
5277
03:25:58,380 --> 03:26:00,239
and this will be the only place where we
5278
03:26:00,239 --> 03:26:03,420
are allowed to write Global CSS code and
5279
03:26:03,420 --> 03:26:05,520
let's actually add something new in here
5280
03:26:05,520 --> 03:26:07,620
let's set a background color for our
5281
03:26:07,620 --> 03:26:11,340
whole app so in the body tag
5282
03:26:11,340 --> 03:26:14,100
we add background colon
5283
03:26:14,100 --> 03:26:17,040
and then a pound symbol or for your
5284
03:26:17,040 --> 03:26:21,600
Zoomers out there a hashtag f-a-f-a-f-a
5285
03:26:21,600 --> 03:26:23,520
which is this very subtle light gray
5286
03:26:23,520 --> 03:26:25,500
that you can now see here
5287
03:26:25,500 --> 03:26:28,020
but now for our node component we create
5288
03:26:28,020 --> 03:26:31,080
the CSS module so that the CSS of our
5289
03:26:31,080 --> 03:26:33,060
node component can't clash with any
5290
03:26:33,060 --> 03:26:35,279
other CSS and let's create a separate
5291
03:26:35,279 --> 03:26:37,800
folder for our CSS files
5292
03:26:37,800 --> 03:26:40,020
so on source and our front-end folder we
5293
03:26:40,020 --> 03:26:42,540
create a new folder called styles
5294
03:26:42,540 --> 03:26:45,000
first of all let's put our Global CSS in
5295
03:26:45,000 --> 03:26:46,319
here
5296
03:26:46,319 --> 03:26:49,140
we might have to update the import in
5297
03:26:49,140 --> 03:26:51,600
our index file
5298
03:26:51,600 --> 03:26:56,040
to a slash Styles slash Globus CSS
5299
03:26:56,040 --> 03:26:59,460
okay it works again and then we create a
5300
03:26:59,460 --> 03:27:01,200
new file in here
5301
03:27:01,200 --> 03:27:04,760
which we call node Dot
5302
03:27:04,760 --> 03:27:08,160
module.css and as the name implies this
5303
03:27:08,160 --> 03:27:10,500
is how we create a CSS module with the
5304
03:27:10,500 --> 03:27:13,380
name Dot module.css
5305
03:27:13,380 --> 03:27:15,840
in here we want to create two classes
5306
03:27:15,840 --> 03:27:18,359
which we do with a DOT this is CSS
5307
03:27:18,359 --> 03:27:19,560
syntax
5308
03:27:19,560 --> 03:27:22,920
note card and candle case
5309
03:27:22,920 --> 03:27:26,340
and another one called card text this is
5310
03:27:26,340 --> 03:27:28,080
the tour pieces that we want to style
5311
03:27:28,080 --> 03:27:30,540
for now before we set this up let's save
5312
03:27:30,540 --> 03:27:34,140
this and add it to our node component so
5313
03:27:34,140 --> 03:27:36,960
in our node.tsx file we add another
5314
03:27:36,960 --> 03:27:39,120
import statement let's put it all the
5315
03:27:39,120 --> 03:27:41,640
way at the top
5316
03:27:41,640 --> 03:27:44,160
import styles
5317
03:27:44,160 --> 03:27:46,200
which will be the name of the variable
5318
03:27:46,200 --> 03:27:48,180
of our CSS modules you can also give
5319
03:27:48,180 --> 03:27:49,680
this a different name
5320
03:27:49,680 --> 03:27:51,060
from
5321
03:27:51,060 --> 03:27:53,939
and then the path of our node module CSS
5322
03:27:53,939 --> 03:27:57,300
file so dot dot slash
5323
03:27:57,300 --> 03:28:02,040
Styles slash node Dot module.css
5324
03:28:02,040 --> 03:28:04,620
and now we can use the CSS module in
5325
03:28:04,620 --> 03:28:06,720
here and it won't clash with any other
5326
03:28:06,720 --> 03:28:08,880
components it will be ignored by other
5327
03:28:08,880 --> 03:28:11,100
components unless we are imported there
5328
03:28:11,100 --> 03:28:13,260
as well we want to use one of the
5329
03:28:13,260 --> 03:28:15,479
classes here for the whole card
5330
03:28:15,479 --> 03:28:17,640
so we add something to this card opening
5331
03:28:17,640 --> 03:28:18,720
tag
5332
03:28:18,720 --> 03:28:21,600
class name but instead of a string we
5333
03:28:21,600 --> 03:28:25,620
add curly braces and pass Styles dot
5334
03:28:25,620 --> 03:28:28,560
note card which is the class name that
5335
03:28:28,560 --> 03:28:31,500
we adjust added here to the node module
5336
03:28:31,500 --> 03:28:32,700
CSS
5337
03:28:32,700 --> 03:28:34,560
and we want to use the other class here
5338
03:28:34,560 --> 03:28:36,000
on the text
5339
03:28:36,000 --> 03:28:39,000
so we add class name here as well
5340
03:28:39,000 --> 03:28:41,399
Styles Dot card text with the same
5341
03:28:41,399 --> 03:28:44,100
spelling as in our CSS file
5342
03:28:44,100 --> 03:28:46,260
we save this and then we go back into
5343
03:28:46,260 --> 03:28:48,960
the node module CSS file now we can
5344
03:28:48,960 --> 03:28:51,359
style our node in here let's start
5345
03:28:51,359 --> 03:28:53,700
simple let's set a background color on
5346
03:28:53,700 --> 03:28:56,239
each node
5347
03:28:56,399 --> 03:28:58,500
and you can use whatever you want I'm
5348
03:28:58,500 --> 03:29:00,420
going to use this conzig which is this
5349
03:29:00,420 --> 03:29:02,040
yellowish color
5350
03:29:02,040 --> 03:29:04,680
and I also want to style the text and to
5351
03:29:04,680 --> 03:29:06,660
show you what I want to do exactly let's
5352
03:29:06,660 --> 03:29:09,800
open the database again and I'm gonna
5353
03:29:09,800 --> 03:29:13,260
add a text here to the node that doesn't
5354
03:29:13,260 --> 03:29:14,700
have one yet
5355
03:29:14,700 --> 03:29:17,460
so text colon
5356
03:29:17,460 --> 03:29:19,859
and then I want to write a text over
5357
03:29:19,859 --> 03:29:24,180
multiple lines so this is line one
5358
03:29:24,180 --> 03:29:26,760
this is line tour let's keep one line
5359
03:29:26,760 --> 03:29:27,899
free
5360
03:29:27,899 --> 03:29:32,040
this is line four save this and see how
5361
03:29:32,040 --> 03:29:33,720
it looks in our front end when we
5362
03:29:33,720 --> 03:29:36,060
refresh this okay as you can see it's
5363
03:29:36,060 --> 03:29:37,979
all in one line even though I put it in
5364
03:29:37,979 --> 03:29:40,439
separate lines in our database but we
5365
03:29:40,439 --> 03:29:42,359
can fix this with this white space
5366
03:29:42,359 --> 03:29:45,239
attribute and we set this to pre-lions
5367
03:29:45,239 --> 03:29:48,060
if this and now it's formatted like we
5368
03:29:48,060 --> 03:29:50,520
added it in our database
5369
03:29:50,520 --> 03:29:53,340
now our cards don't have room between
5370
03:29:53,340 --> 03:29:55,260
them yet which of course we can do with
5371
03:29:55,260 --> 03:29:56,520
the margin
5372
03:29:56,520 --> 03:29:58,140
attribute
5373
03:29:58,140 --> 03:30:00,779
it will look like this later but we
5374
03:30:00,779 --> 03:30:02,399
actually want to add this margin in a
5375
03:30:02,399 --> 03:30:03,779
different place later so I'm going to
5376
03:30:03,779 --> 03:30:06,300
remove it again and if you want to know
5377
03:30:06,300 --> 03:30:08,160
where I learned these CSS attributes
5378
03:30:08,160 --> 03:30:09,600
from I just learned them through
5379
03:30:09,600 --> 03:30:12,660
Googling I never formally studied CSS I
5380
03:30:12,660 --> 03:30:13,859
didn't go through a list of all
5381
03:30:13,859 --> 03:30:15,660
attributes I just look up and Google
5382
03:30:15,660 --> 03:30:17,520
whatever I want to do like this white
5383
03:30:17,520 --> 03:30:19,560
space pre-line and then you can find it
5384
03:30:19,560 --> 03:30:21,479
somewhere the other thing I want to
5385
03:30:21,479 --> 03:30:23,819
point out is you might be wondering why
5386
03:30:23,819 --> 03:30:25,620
this is called kala's name and not just
5387
03:30:25,620 --> 03:30:27,779
class because a normal HTML the
5388
03:30:27,779 --> 03:30:30,000
attribute is just called class this is
5389
03:30:30,000 --> 03:30:32,760
because we are in this TSX file here and
5390
03:30:32,760 --> 03:30:34,859
class is a reserved name in JavaScript
5391
03:30:34,859 --> 03:30:37,260
code this is why they changed it to
5392
03:30:37,260 --> 03:30:39,000
class name instead otherwise this
5393
03:30:39,000 --> 03:30:40,920
wouldn't work alright so we already
5394
03:30:40,920 --> 03:30:43,739
learned some CSS styling our nodes at
5395
03:30:43,739 --> 03:30:45,779
least look a little bit more like notes
5396
03:30:45,779 --> 03:30:47,520
now we are not done yet we will finish
5397
03:30:47,520 --> 03:30:52,100
up the CSS in the remaining tutorial
5398
03:30:55,500 --> 03:30:58,319
okay to put our notes into a nice grid
5399
03:30:58,319 --> 03:31:00,960
we will use another component from react
5400
03:31:00,960 --> 03:31:03,479
bootstrap they provide these grid
5401
03:31:03,479 --> 03:31:04,800
helpers here
5402
03:31:04,800 --> 03:31:06,779
which are rows and columns and
5403
03:31:06,779 --> 03:31:09,120
containers that make it easy to create a
5404
03:31:09,120 --> 03:31:11,279
responsive design and automatically
5405
03:31:11,279 --> 03:31:14,040
adapts with the screen size
5406
03:31:14,040 --> 03:31:15,720
so again you can read the documentation
5407
03:31:15,720 --> 03:31:18,359
but you can also just follow along we go
5408
03:31:18,359 --> 03:31:21,540
into our app TSX file once again because
5409
03:31:21,540 --> 03:31:23,399
this is where we map the nodes and here
5410
03:31:23,399 --> 03:31:25,200
we want to shape them into a grid now
5411
03:31:25,200 --> 03:31:27,600
first of all we want to change the diff
5412
03:31:27,600 --> 03:31:30,479
to a container
5413
03:31:30,479 --> 03:31:33,060
which is a bootstrap component let's not
5414
03:31:33,060 --> 03:31:35,580
forget to change the closing tag as well
5415
03:31:35,580 --> 03:31:38,700
this just adds some padding and zenders
5416
03:31:38,700 --> 03:31:41,340
the notes on the screen then in here we
5417
03:31:41,340 --> 03:31:43,439
want to set up our grid which we do the
5418
03:31:43,439 --> 03:31:45,120
following way
5419
03:31:45,120 --> 03:31:47,580
we add a roll which again is coming from
5420
03:31:47,580 --> 03:31:50,040
the react bootstrap library and it's
5421
03:31:50,040 --> 03:31:51,720
adding all these import statements up
5422
03:31:51,720 --> 03:31:53,220
here
5423
03:31:53,220 --> 03:31:55,560
and we do some configuration in here we
5424
03:31:55,560 --> 03:31:58,020
have to Define at which screen size how
5425
03:31:58,020 --> 03:32:00,540
many rows we want to show in our grid
5426
03:32:00,540 --> 03:32:02,460
and you can change this however you want
5427
03:32:02,460 --> 03:32:05,279
but I tried it out and I found this the
5428
03:32:05,279 --> 03:32:07,200
best configuration at rapid small
5429
03:32:07,200 --> 03:32:10,020
screens we want to show one column
5430
03:32:10,020 --> 03:32:13,319
add medium-sized screens and we have
5431
03:32:13,319 --> 03:32:15,180
these different properties for these
5432
03:32:15,180 --> 03:32:17,760
different screen sizes MDM we want to
5433
03:32:17,760 --> 03:32:19,680
show two columns
5434
03:32:19,680 --> 03:32:22,500
and on large screens we want to show
5435
03:32:22,500 --> 03:32:25,020
three
5436
03:32:25,020 --> 03:32:28,680
we close this and we cut out the closing
5437
03:32:28,680 --> 03:32:31,920
tag and put it below our map call here
5438
03:32:31,920 --> 03:32:34,260
now when we save this I think we should
5439
03:32:34,260 --> 03:32:36,000
already see a difference here you can
5440
03:32:36,000 --> 03:32:37,920
see how it grows and shrinks
5441
03:32:37,920 --> 03:32:39,960
we don't have three nodes yet so let's
5442
03:32:39,960 --> 03:32:42,060
add a third one let's do that with
5443
03:32:42,060 --> 03:32:45,000
Postman real quick so we do a post
5444
03:32:45,000 --> 03:32:46,260
request
5445
03:32:46,260 --> 03:32:49,500
to our notes endpoint
5446
03:32:49,500 --> 03:32:51,239
we need the title
5447
03:32:51,239 --> 03:32:54,120
let's call it node three
5448
03:32:54,120 --> 03:32:57,920
let's also add some body text
5449
03:32:57,960 --> 03:33:01,319
this is line one let's just add one line
5450
03:33:01,319 --> 03:33:04,500
here the centers and let's create a
5451
03:33:04,500 --> 03:33:07,260
fourth note as well
5452
03:33:07,260 --> 03:33:10,260
and it says this is line one I actually
5453
03:33:10,260 --> 03:33:12,720
don't know how I can add multiple lines
5454
03:33:12,720 --> 03:33:15,479
here not sure if I can just do it like
5455
03:33:15,479 --> 03:33:17,399
this probably not
5456
03:33:17,399 --> 03:33:19,680
so I will add more lines later in our
5457
03:33:19,680 --> 03:33:21,180
database
5458
03:33:21,180 --> 03:33:23,760
create a fourth note
5459
03:33:23,760 --> 03:33:25,979
go into our database backend here
5460
03:33:25,979 --> 03:33:28,979
refresh this
5461
03:33:28,979 --> 03:33:31,620
I just want to make the fourth note a
5462
03:33:31,620 --> 03:33:33,239
bit bigger
5463
03:33:33,239 --> 03:33:36,600
so I go into the text here
5464
03:33:36,600 --> 03:33:39,560
this is line two
5465
03:33:39,560 --> 03:33:45,260
this is line three and save this
5466
03:33:45,479 --> 03:33:47,340
take a look at our
5467
03:33:47,340 --> 03:33:49,319
front end here again and now we have
5468
03:33:49,319 --> 03:33:50,520
more notes
5469
03:33:50,520 --> 03:33:54,300
and because of our row setup here
5470
03:33:54,300 --> 03:33:56,640
the layout changes depending on the size
5471
03:33:56,640 --> 03:33:58,439
of the screen
5472
03:33:58,439 --> 03:34:00,600
it's not perfect yet but we are getting
5473
03:34:00,600 --> 03:34:03,000
there to add some margin here between
5474
03:34:03,000 --> 03:34:06,479
our elements in the grid we add a class
5475
03:34:06,479 --> 03:34:07,560
name
5476
03:34:07,560 --> 03:34:09,660
to the row tag
5477
03:34:09,660 --> 03:34:12,720
which this time will just be a string we
5478
03:34:12,720 --> 03:34:16,260
use this g-4 class which is a class from
5479
03:34:16,260 --> 03:34:18,660
the bootstrap library that adds some
5480
03:34:18,660 --> 03:34:20,640
room between the single elements in a
5481
03:34:20,640 --> 03:34:22,859
grid we could also create our own class
5482
03:34:22,859 --> 03:34:26,040
in our own CSS component but we might as
5483
03:34:26,040 --> 03:34:27,720
well use these predefined ones here
5484
03:34:27,720 --> 03:34:30,180
let's again change the size and see how
5485
03:34:30,180 --> 03:34:31,800
this looks okay there's still no room
5486
03:34:31,800 --> 03:34:34,800
horizontally between the notes
5487
03:34:34,800 --> 03:34:37,739
we add this by wrapping the single nodes
5488
03:34:37,739 --> 03:34:40,800
that we map in here into column elements
5489
03:34:40,800 --> 03:34:43,439
so we go inside this map call here
5490
03:34:43,439 --> 03:34:47,220
inside the parentheses Andrea wrap the
5491
03:34:47,220 --> 03:34:50,700
note into a column again this is a react
5492
03:34:50,700 --> 03:34:53,520
bootstrap import cut out the closing tag
5493
03:34:53,520 --> 03:34:56,160
and put it below our node here
5494
03:34:56,160 --> 03:34:59,399
now we also have to move the key from
5495
03:34:59,399 --> 03:35:01,140
here to the outer element
5496
03:35:01,140 --> 03:35:03,300
so we cut this out and put it in the
5497
03:35:03,300 --> 03:35:04,439
column
5498
03:35:04,439 --> 03:35:06,420
now there should be some horizontal
5499
03:35:06,420 --> 03:35:08,939
spacing between our notes as well
5500
03:35:08,939 --> 03:35:10,680
as you can see the slices are different
5501
03:35:10,680 --> 03:35:12,779
so this doesn't look good yet we will
5502
03:35:12,779 --> 03:35:15,479
fix this in a moment we want to get all
5503
03:35:15,479 --> 03:35:17,399
of these notes at the same height the
5504
03:35:17,399 --> 03:35:20,160
question is where do we put the CSS of
5505
03:35:20,160 --> 03:35:22,620
course we could put it into the node
5506
03:35:22,620 --> 03:35:25,080
module CSS file here so it will be
5507
03:35:25,080 --> 03:35:27,420
applied to every node however I want to
5508
03:35:27,420 --> 03:35:29,460
keep the styling of these notes a bit
5509
03:35:29,460 --> 03:35:31,140
more flexible and give the outside
5510
03:35:31,140 --> 03:35:33,960
component control over how each node
5511
03:35:33,960 --> 03:35:36,300
looks the same is the case for stuff
5512
03:35:36,300 --> 03:35:39,060
like the Box shadow that appears when we
5513
03:35:39,060 --> 03:35:41,340
hover over a node I don't want to bake
5514
03:35:41,340 --> 03:35:43,140
this right into the node component
5515
03:35:43,140 --> 03:35:45,779
because then every place in our app has
5516
03:35:45,779 --> 03:35:47,819
to display it the exact same way instead
5517
03:35:47,819 --> 03:35:49,560
I want to declare The Styling on the
5518
03:35:49,560 --> 03:35:51,600
level of this page here and then just
5519
03:35:51,600 --> 03:35:53,700
pass it to the node also it's a good way
5520
03:35:53,700 --> 03:35:56,220
to learn how to pass class names to a
5521
03:35:56,220 --> 03:35:58,500
component so what we do is we create
5522
03:35:58,500 --> 03:36:03,239
another CSS module in our Styles folder
5523
03:36:03,239 --> 03:36:08,399
which we call notes page.module.css
5524
03:36:08,580 --> 03:36:10,319
we will use this on the page that
5525
03:36:10,319 --> 03:36:12,300
displays our nodes at the moment that's
5526
03:36:12,300 --> 03:36:14,760
the app TSX file but later this will be
5527
03:36:14,760 --> 03:36:16,920
a separate component in here we put a
5528
03:36:16,920 --> 03:36:19,460
node class
5529
03:36:20,160 --> 03:36:23,160
and a node colon hover class
5530
03:36:23,160 --> 03:36:25,500
this is how you define the styling of a
5531
03:36:25,500 --> 03:36:28,020
class in CSS when you hover over it with
5532
03:36:28,020 --> 03:36:30,420
the mouse now before we fill this with
5533
03:36:30,420 --> 03:36:33,479
actual styling let's use this in our app
5534
03:36:33,479 --> 03:36:37,380
TSX file so we go in here
5535
03:36:37,380 --> 03:36:42,180
we import The Styling import styles
5536
03:36:42,180 --> 03:36:44,720
from
5537
03:36:44,819 --> 03:36:48,359
and the path is here dot slash Styles
5538
03:36:48,359 --> 03:36:50,640
slash notes page
5539
03:36:50,640 --> 03:36:53,700
Dot module.css
5540
03:36:53,700 --> 03:36:56,520
and then we want to style our node from
5541
03:36:56,520 --> 03:36:58,979
in here this way as I explained this
5542
03:36:58,979 --> 03:37:01,260
page defines the styling of the node and
5543
03:37:01,260 --> 03:37:03,180
not the node itself or at least part of
5544
03:37:03,180 --> 03:37:05,939
it but first we need a way to pass a CSS
5545
03:37:05,939 --> 03:37:08,819
class to our node component so we go
5546
03:37:08,819 --> 03:37:11,160
inside the node component and we add
5547
03:37:11,160 --> 03:37:14,520
another property to the node props
5548
03:37:14,520 --> 03:37:16,800
which we give the same name as the
5549
03:37:16,800 --> 03:37:19,080
normal class name attribute here but we
5550
03:37:19,080 --> 03:37:21,600
make this optional so we can pass a
5551
03:37:21,600 --> 03:37:23,520
class name to this component or we can
5552
03:37:23,520 --> 03:37:24,779
omit it
5553
03:37:24,779 --> 03:37:26,939
and it will be of type string
5554
03:37:26,939 --> 03:37:28,979
and then we want to add this class to
5555
03:37:28,979 --> 03:37:31,319
our Cartier to the outermost component
5556
03:37:31,319 --> 03:37:33,720
so we can style it from the outside we
5557
03:37:33,720 --> 03:37:36,180
already have a class in here so how can
5558
03:37:36,180 --> 03:37:39,060
we add another one we can change this to
5559
03:37:39,060 --> 03:37:41,640
a string and we do this with a pair of
5560
03:37:41,640 --> 03:37:44,100
back ticks like this because backticks
5561
03:37:44,100 --> 03:37:46,200
allow us to put variables inside the
5562
03:37:46,200 --> 03:37:48,720
string so we wrap this styles.note cut
5563
03:37:48,720 --> 03:37:51,420
into backticks and then into a curly
5564
03:37:51,420 --> 03:37:54,180
braces and we add a dollar sign here
5565
03:37:54,180 --> 03:37:56,880
charging this way we put a variable in
5566
03:37:56,880 --> 03:37:59,160
here and this allows us to add another
5567
03:37:59,160 --> 03:38:01,680
one inside the string so again dollar
5568
03:38:01,680 --> 03:38:03,479
sign charging
5569
03:38:03,479 --> 03:38:08,220
curly braces and then we pass class name
5570
03:38:08,220 --> 03:38:11,580
which is this property here but we must
5571
03:38:11,580 --> 03:38:14,399
not forget to also edit up here to
5572
03:38:14,399 --> 03:38:16,260
actually add it as an argument to the
5573
03:38:16,260 --> 03:38:18,479
component
5574
03:38:18,479 --> 03:38:20,760
this now allows us to pass another class
5575
03:38:20,760 --> 03:38:22,739
name from the outside if we want to
5576
03:38:22,739 --> 03:38:25,140
allow the color of this node to style
5577
03:38:25,140 --> 03:38:28,260
this node then we go back to our app TSX
5578
03:38:28,260 --> 03:38:31,520
file after saving this
5579
03:38:31,560 --> 03:38:34,319
and now we can add this newer class name
5580
03:38:34,319 --> 03:38:36,600
property here which is optional so we
5581
03:38:36,600 --> 03:38:39,420
can edit or we cannot however we want
5582
03:38:39,420 --> 03:38:41,880
and here we now want to pass the class
5583
03:38:41,880 --> 03:38:44,580
that we defined in this notes page
5584
03:38:44,580 --> 03:38:46,560
module.css
5585
03:38:46,560 --> 03:38:48,779
let's take a look at it again we want to
5586
03:38:48,779 --> 03:38:51,840
use this node class here
5587
03:38:51,840 --> 03:38:57,560
so apts X let me move this over here
5588
03:38:58,020 --> 03:39:00,479
so that it's easier to reach
5589
03:39:00,479 --> 03:39:04,979
here we want to pass Styles dot note
5590
03:39:04,979 --> 03:39:06,840
save this and then we can style this
5591
03:39:06,840 --> 03:39:09,840
node from the notes page module
5592
03:39:09,840 --> 03:39:11,700
so what we'd want to do is first of all
5593
03:39:11,700 --> 03:39:13,739
we want to set all the nodes to a fixed
5594
03:39:13,739 --> 03:39:16,319
height of 200 pixels so it looks already
5595
03:39:16,319 --> 03:39:18,180
better in our grid
5596
03:39:18,180 --> 03:39:20,540
we want to give them a Min width as well
5597
03:39:20,540 --> 03:39:22,979
which means that when the screen gets
5598
03:39:22,979 --> 03:39:24,420
very small
5599
03:39:24,420 --> 03:39:27,779
they can't just shrink indefinitely
5600
03:39:27,779 --> 03:39:29,399
which at a certain point looks
5601
03:39:29,399 --> 03:39:31,380
ridiculous like this
5602
03:39:31,380 --> 03:39:34,920
we want to set the Min width to a 150
5603
03:39:34,920 --> 03:39:36,000
pixels
5604
03:39:36,000 --> 03:39:38,100
and now they can't shrink
5605
03:39:38,100 --> 03:39:41,239
below the size
5606
03:39:43,020 --> 03:39:45,660
I also want to add a box Shadow which
5607
03:39:45,660 --> 03:39:47,279
I'm going to copy paste
5608
03:39:47,279 --> 03:39:49,319
you can pause the video for a moment and
5609
03:39:49,319 --> 03:39:52,500
type it it out by hand so add box Shadow
5610
03:39:52,500 --> 03:39:55,380
colon and then pause the video and type
5611
03:39:55,380 --> 03:39:57,720
out this stuff here or copy it from the
5612
03:39:57,720 --> 03:40:00,300
GitHub repository below
5613
03:40:00,300 --> 03:40:02,460
this adds a shadow to the hover State
5614
03:40:02,460 --> 03:40:05,100
it's very subtle but it looks cool
5615
03:40:05,100 --> 03:40:07,739
but we also want to add an animation so
5616
03:40:07,739 --> 03:40:09,420
it's not so abrupt
5617
03:40:09,420 --> 03:40:12,120
we add this to the note tag
5618
03:40:12,120 --> 03:40:14,819
with the transition attribute and then
5619
03:40:14,819 --> 03:40:18,000
we write box shadow
5620
03:40:18,000 --> 03:40:20,939
the duration which is 200 milliseconds
5621
03:40:20,939 --> 03:40:24,779
so a point to a s and then we use this
5622
03:40:24,779 --> 03:40:27,660
ease in our animation here
5623
03:40:27,660 --> 03:40:30,420
and now it animates to this box Shadow
5624
03:40:30,420 --> 03:40:32,640
when we hover over a node
5625
03:40:32,640 --> 03:40:34,380
one more thing we also want to change
5626
03:40:34,380 --> 03:40:36,899
the mouse pointer toward this yeah this
5627
03:40:36,899 --> 03:40:39,000
little hand icon that indicates that we
5628
03:40:39,000 --> 03:40:40,920
can click something because later when
5629
03:40:40,920 --> 03:40:43,319
we click a note we want to open it
5630
03:40:43,319 --> 03:40:45,779
so we add another attribute cursor
5631
03:40:45,779 --> 03:40:47,939
pointer and now we have this pointer
5632
03:40:47,939 --> 03:40:51,600
cursor when we hover over a node nice
5633
03:40:51,600 --> 03:40:53,819
but we aren't done with the CSS yet
5634
03:40:53,819 --> 03:40:56,279
let's see what happens when the text of
5635
03:40:56,279 --> 03:40:58,439
the note gets longer than these 200
5636
03:40:58,439 --> 03:41:00,300
pixels
5637
03:41:00,300 --> 03:41:03,420
so once again into our DB
5638
03:41:03,420 --> 03:41:07,279
just going to add some more text here
5639
03:41:07,680 --> 03:41:11,040
this is line five this is line six line
5640
03:41:11,040 --> 03:41:13,319
seven let's add another empty line line
5641
03:41:13,319 --> 03:41:14,340
nine
5642
03:41:14,340 --> 03:41:17,220
that's a lot of lines
5643
03:41:17,220 --> 03:41:20,399
refresh this yeah and it goes beyond the
5644
03:41:20,399 --> 03:41:22,260
note which of course doesn't look great
5645
03:41:22,260 --> 03:41:25,319
we will fix this on the CSS level of our
5646
03:41:25,319 --> 03:41:28,439
single nodes so we go into the node TSX
5647
03:41:28,439 --> 03:41:30,600
file once again and we want to add
5648
03:41:30,600 --> 03:41:32,760
another class to the card body because
5649
03:41:32,760 --> 03:41:35,580
this is where we have to fix this
5650
03:41:35,580 --> 03:41:39,239
so class name will be Styles Dot card
5651
03:41:39,239 --> 03:41:41,580
body
5652
03:41:41,580 --> 03:41:43,500
we saved this
5653
03:41:43,500 --> 03:41:45,600
and go into the
5654
03:41:45,600 --> 03:41:49,260
CSS module of our node this one here
5655
03:41:49,260 --> 03:41:51,359
and add another class for the card body
5656
03:41:51,359 --> 03:41:53,279
I'm going to put it here just for
5657
03:41:53,279 --> 03:41:57,000
ordering we set overflow to Hidden
5658
03:41:57,000 --> 03:41:58,979
this removes the text that goes beyond
5659
03:41:58,979 --> 03:41:59,939
the body
5660
03:41:59,939 --> 03:42:01,859
but this now it doesn't really tell us
5661
03:42:01,859 --> 03:42:03,540
that there are small text in here right
5662
03:42:03,540 --> 03:42:06,300
so I want to add some nice effect which
5663
03:42:06,300 --> 03:42:08,520
we do the following way
5664
03:42:08,520 --> 03:42:11,100
mask image
5665
03:42:11,100 --> 03:42:12,600
colon
5666
03:42:12,600 --> 03:42:16,140
linear gradient and in here we can yet
5667
03:42:16,140 --> 03:42:18,560
specify a gradient
5668
03:42:18,560 --> 03:42:23,100
180 deg for degree
5669
03:42:23,100 --> 03:42:25,979
the color will be a hashtag zero zero
5670
03:42:25,979 --> 03:42:28,380
zero that may be black
5671
03:42:28,380 --> 03:42:32,700
but with a 60 gradient to it comma and
5672
03:42:32,700 --> 03:42:35,640
then we write transparent
5673
03:42:35,640 --> 03:42:37,739
which creates this gradient effect here
5674
03:42:37,739 --> 03:42:40,380
when we save it as you can see now this
5675
03:42:40,380 --> 03:42:42,239
kind of indicates that there's more text
5676
03:42:42,239 --> 03:42:44,939
below it I just like this effect this
5677
03:42:44,939 --> 03:42:46,680
way we can give each note the same
5678
03:42:46,680 --> 03:42:49,739
height but still not just cut off the
5679
03:42:49,739 --> 03:42:52,380
text in this ugly way next I want to add
5680
03:42:52,380 --> 03:42:54,600
the timestamp here at the bottom of each
5681
03:42:54,600 --> 03:42:57,479
note so let's go into the node TSX file
5682
03:42:57,479 --> 03:42:58,620
once again
5683
03:42:58,620 --> 03:43:00,960
and here I want to add a card photo
5684
03:43:00,960 --> 03:43:03,600
below card text we add another tag
5685
03:43:03,600 --> 03:43:06,600
cards.folder
5686
03:43:08,340 --> 03:43:11,580
and in here we want to see the created
5687
03:43:11,580 --> 03:43:13,979
add timestamp for now
5688
03:43:13,979 --> 03:43:16,080
okay this doesn't look great yet because
5689
03:43:16,080 --> 03:43:18,600
this is actually the wrong place
5690
03:43:18,600 --> 03:43:21,960
we have to put it below the body
5691
03:43:21,960 --> 03:43:24,720
here okay the first one doesn't have a
5692
03:43:24,720 --> 03:43:27,060
timestamp yet because we created it at
5693
03:43:27,060 --> 03:43:28,739
the very beginning directly in our
5694
03:43:28,739 --> 03:43:31,200
database also the timestamps are not
5695
03:43:31,200 --> 03:43:32,939
formatted yet so it's not really
5696
03:43:32,939 --> 03:43:35,279
readable and we also want to show the
5697
03:43:35,279 --> 03:43:37,319
updated timestamp if the note has been
5698
03:43:37,319 --> 03:43:39,300
updated so we still have to make some
5699
03:43:39,300 --> 03:43:40,500
changes
5700
03:43:40,500 --> 03:43:42,660
the first change is adding another class
5701
03:43:42,660 --> 03:43:45,120
to the footer which again is a class
5702
03:43:45,120 --> 03:43:47,279
from the bootstrap Library
5703
03:43:47,279 --> 03:43:50,100
which is called like this text muted it
5704
03:43:50,100 --> 03:43:51,600
just makes the text a bit more gray
5705
03:43:51,600 --> 03:43:53,819
again we could do this ourselves in our
5706
03:43:53,819 --> 03:43:56,040
CSS module but there's no reason to not
5707
03:43:56,040 --> 03:43:58,560
use bootstraps predefined classes for
5708
03:43:58,560 --> 03:44:01,200
that next I want to format the timestamp
5709
03:44:01,200 --> 03:44:03,180
properly but I don't want to do this
5710
03:44:03,180 --> 03:44:04,800
directly in this component because
5711
03:44:04,800 --> 03:44:06,420
that's not really a good separation of
5712
03:44:06,420 --> 03:44:08,279
concerns and we might want to use this
5713
03:44:08,279 --> 03:44:10,380
in a different place again later
5714
03:44:10,380 --> 03:44:13,020
so let's create another folder for that
5715
03:44:13,020 --> 03:44:15,899
and our front end folder
5716
03:44:15,899 --> 03:44:18,420
in Source we create
5717
03:44:18,420 --> 03:44:21,960
a utils folder for all kinds of utility
5718
03:44:21,960 --> 03:44:24,239
functions
5719
03:44:24,239 --> 03:44:27,060
let's add another file in here
5720
03:44:27,060 --> 03:44:30,899
which recall format date dot TS a normal
5721
03:44:30,899 --> 03:44:32,819
typescript file
5722
03:44:32,819 --> 03:44:35,040
and in here we export one single
5723
03:44:35,040 --> 03:44:36,120
function
5724
03:44:36,120 --> 03:44:38,100
with the same name
5725
03:44:38,100 --> 03:44:39,899
format date
5726
03:44:39,899 --> 03:44:42,180
it will take one argument which is the
5727
03:44:42,180 --> 03:44:44,460
date and form of a string so let's call
5728
03:44:44,460 --> 03:44:47,520
it date string of type string
5729
03:44:47,520 --> 03:44:50,340
and it will return a string
5730
03:44:50,340 --> 03:44:52,560
we don't have to declare the return type
5731
03:44:52,560 --> 03:44:54,600
in typescript but I like to do this
5732
03:44:54,600 --> 03:44:56,399
because it gives us a bit more safety
5733
03:44:56,399 --> 03:44:58,560
because now this will not compile until
5734
03:44:58,560 --> 03:45:00,540
we return a string from this function
5735
03:45:00,540 --> 03:45:03,000
and here we want to return our date but
5736
03:45:03,000 --> 03:45:05,520
in a nice format and again I found out
5737
03:45:05,520 --> 03:45:07,380
how to do this through Google and stack
5738
03:45:07,380 --> 03:45:09,239
overflow
5739
03:45:09,239 --> 03:45:11,939
return new date
5740
03:45:11,939 --> 03:45:13,859
we initialize this date with the date
5741
03:45:13,859 --> 03:45:16,020
string that we pass and then we can
5742
03:45:16,020 --> 03:45:20,340
format it with DOT tool locale
5743
03:45:20,340 --> 03:45:23,399
string this one here
5744
03:45:23,399 --> 03:45:24,899
and to this we can pass some
5745
03:45:24,899 --> 03:45:26,220
configuration
5746
03:45:26,220 --> 03:45:28,920
the first piece is a string
5747
03:45:28,920 --> 03:45:31,859
like this e n minus u s
5748
03:45:31,859 --> 03:45:34,560
the U.S in uppercase which if I remember
5749
03:45:34,560 --> 03:45:36,720
correctly formats this refer you as
5750
03:45:36,720 --> 03:45:39,180
styling there are also other languages
5751
03:45:39,180 --> 03:45:41,880
Chinese for example but in most
5752
03:45:41,880 --> 03:45:43,439
situations you want to use the US
5753
03:45:43,439 --> 03:45:47,040
styling comma and I put this in the new
5754
03:45:47,040 --> 03:45:48,239
line
5755
03:45:48,239 --> 03:45:50,640
take a look at the exact syntax I'm
5756
03:45:50,640 --> 03:45:52,739
using here because that's important the
5757
03:45:52,739 --> 03:45:54,239
first argument is a string the second
5758
03:45:54,239 --> 03:45:56,819
document is a JavaScript object with two
5759
03:45:56,819 --> 03:45:59,399
curly braces and here we can do a
5760
03:45:59,399 --> 03:46:01,620
further configuration of how we want to
5761
03:46:01,620 --> 03:46:04,140
style this date stream until you just
5762
03:46:04,140 --> 03:46:05,760
have to follow along
5763
03:46:05,760 --> 03:46:08,899
we Define year
5764
03:46:08,939 --> 03:46:11,939
as a string and we are set it to a
5765
03:46:11,939 --> 03:46:14,279
numeric
5766
03:46:14,279 --> 03:46:16,140
we set month
5767
03:46:16,140 --> 03:46:19,080
to short and those are just the
5768
03:46:19,080 --> 03:46:21,239
different options we have to display the
5769
03:46:21,239 --> 03:46:23,760
date for example a year with just the
5770
03:46:23,760 --> 03:46:26,279
last two digits or all four the long
5771
03:46:26,279 --> 03:46:28,439
name of a month or the short one this is
5772
03:46:28,439 --> 03:46:31,859
just the combination I found looks good
5773
03:46:31,859 --> 03:46:35,120
day numeric
5774
03:46:36,000 --> 03:46:39,060
our numeric
5775
03:46:39,060 --> 03:46:42,239
minute numeric and we don't need seconds
5776
03:46:42,239 --> 03:46:44,399
here
5777
03:46:44,399 --> 03:46:46,080
so this is fine
5778
03:46:46,080 --> 03:46:48,239
now we can use this function in our node
5779
03:46:48,239 --> 03:46:50,100
component
5780
03:46:50,100 --> 03:46:51,960
but we want to display either the
5781
03:46:51,960 --> 03:46:53,819
created timestamp or the updated
5782
03:46:53,819 --> 03:46:55,979
timestamp depending on which one is
5783
03:46:55,979 --> 03:46:58,319
newer so let's put this logic here
5784
03:46:58,319 --> 03:47:00,660
inside the body of this component above
5785
03:47:00,660 --> 03:47:02,340
the return statement
5786
03:47:02,340 --> 03:47:04,140
we create a LED
5787
03:47:04,140 --> 03:47:06,960
created updated
5788
03:47:06,960 --> 03:47:09,720
text of type string
5789
03:47:09,720 --> 03:47:11,220
if there should not be a comma by the
5790
03:47:11,220 --> 03:47:15,239
semicolon below we check if
5791
03:47:15,239 --> 03:47:17,939
updated ad is created and created at
5792
03:47:17,939 --> 03:47:19,920
which means that the note has been
5793
03:47:19,920 --> 03:47:21,840
updated because when we create a new
5794
03:47:21,840 --> 03:47:24,000
node updated add and created it will
5795
03:47:24,000 --> 03:47:26,100
have the same value when we updated node
5796
03:47:26,100 --> 03:47:28,680
updated it will be a greater so if
5797
03:47:28,680 --> 03:47:30,540
updated ad is greater we want to show
5798
03:47:30,540 --> 03:47:32,880
the updated timestamp so with that
5799
03:47:32,880 --> 03:47:35,700
created updated text
5800
03:47:35,700 --> 03:47:37,560
tool
5801
03:47:37,560 --> 03:47:40,020
updated colon
5802
03:47:40,020 --> 03:47:42,899
and then the formatted date string so we
5803
03:47:42,899 --> 03:47:45,540
call our format date function
5804
03:47:45,540 --> 03:47:49,500
and paste the updated add timestamp
5805
03:47:49,500 --> 03:47:52,380
if updated add is not greater so in the
5806
03:47:52,380 --> 03:47:54,000
else block
5807
03:47:54,000 --> 03:47:57,660
we want to set created updated text tool
5808
03:47:57,660 --> 03:48:00,979
created colon
5809
03:48:01,080 --> 03:48:05,939
and the formatted created at timestamp
5810
03:48:05,939 --> 03:48:08,399
now remember that whatever we put inside
5811
03:48:08,399 --> 03:48:11,460
this component body without a use effect
5812
03:48:11,460 --> 03:48:14,279
will be executed on every render this is
5813
03:48:14,279 --> 03:48:16,380
okay because format date is a cheap
5814
03:48:16,380 --> 03:48:18,720
operation we can afford to execute this
5815
03:48:18,720 --> 03:48:20,880
on every render but if this was more
5816
03:48:20,880 --> 03:48:22,380
expensive you should use something
5817
03:48:22,380 --> 03:48:24,540
either a use effect or something else
5818
03:48:24,540 --> 03:48:26,819
called us memo which you can read up in
5819
03:48:26,819 --> 03:48:29,040
the documentation which is basically a
5820
03:48:29,040 --> 03:48:32,279
holder for this expensive operations but
5821
03:48:32,279 --> 03:48:34,560
cheap simple operations that are very
5822
03:48:34,560 --> 03:48:36,779
fast can be done directly in the body of
5823
03:48:36,779 --> 03:48:38,880
a component just be aware that this gets
5824
03:48:38,880 --> 03:48:41,520
executed on every single render okay and
5825
03:48:41,520 --> 03:48:43,560
then down here we don't want to display
5826
03:48:43,560 --> 03:48:45,920
created yet we want to display our
5827
03:48:45,920 --> 03:48:49,200
formatted created updated text
5828
03:48:49,200 --> 03:48:51,359
and when we save this it now looks much
5829
03:48:51,359 --> 03:48:53,340
better again the first one has an
5830
03:48:53,340 --> 03:48:56,040
invalid date but later we will not add
5831
03:48:56,040 --> 03:48:58,739
any new nodes with invalid dates so we
5832
03:48:58,739 --> 03:49:00,120
don't really have to take care of this
5833
03:49:00,120 --> 03:49:02,399
case because it's just in our
5834
03:49:02,399 --> 03:49:04,680
development setup here but let's try
5835
03:49:04,680 --> 03:49:07,560
updating a note to see if the timestamp
5836
03:49:07,560 --> 03:49:09,899
changes properly
5837
03:49:09,899 --> 03:49:13,680
I want to update node three
5838
03:49:13,680 --> 03:49:17,220
which has this ID here
5839
03:49:17,220 --> 03:49:19,380
let's do it over Postman so it's
5840
03:49:19,380 --> 03:49:22,859
actually recognized as an update
5841
03:49:22,859 --> 03:49:25,979
we do a patch request to the notes
5842
03:49:25,979 --> 03:49:30,000
endpoint to denote with this idea
5843
03:49:30,000 --> 03:49:34,859
we keep the title as node 3. and there
5844
03:49:34,859 --> 03:49:39,840
this is an updated note body
5845
03:49:39,840 --> 03:49:41,460
send this update
5846
03:49:41,460 --> 03:49:43,979
it went through
5847
03:49:43,979 --> 03:49:46,319
refresh this and now we see our updated
5848
03:49:46,319 --> 03:49:48,840
timestamp because now the updated
5849
03:49:48,840 --> 03:49:51,060
timestamp is greater than the created
5850
03:49:51,060 --> 03:49:53,040
timestamp really nice
5851
03:49:53,040 --> 03:49:55,920
our notes already look pretty cool the
5852
03:49:55,920 --> 03:49:58,020
layout is responsive
5853
03:49:58,020 --> 03:50:00,180
the next step is to add a way to add
5854
03:50:00,180 --> 03:50:02,340
nodes through our front end rather than
5855
03:50:02,340 --> 03:50:04,380
having to do this via Postman all the
5856
03:50:04,380 --> 03:50:07,939
time okay so let's do that next
5857
03:50:11,520 --> 03:50:14,040
okay so we want to add a way to add new
5858
03:50:14,040 --> 03:50:16,560
nodes through our front end and we
5859
03:50:16,560 --> 03:50:18,300
already have all the endpoints on our
5860
03:50:18,300 --> 03:50:20,340
server in place for that for creating
5861
03:50:20,340 --> 03:50:22,920
reading updating and deleting notes and
5862
03:50:22,920 --> 03:50:24,660
now we just need some kind of form in
5863
03:50:24,660 --> 03:50:26,939
our react app where we can enter a new
5864
03:50:26,939 --> 03:50:29,399
node's title and text and send them to
5865
03:50:29,399 --> 03:50:31,859
our server first of all let's organize
5866
03:50:31,859 --> 03:50:34,439
our fetch code a bit better so when our
5867
03:50:34,439 --> 03:50:37,800
ftsx file we have our fetch call at the
5868
03:50:37,800 --> 03:50:39,899
moment where we get our existing notes
5869
03:50:39,899 --> 03:50:42,120
from the server and I like to put this
5870
03:50:42,120 --> 03:50:44,279
low level code into a separate file for
5871
03:50:44,279 --> 03:50:46,260
better organization so that we don't
5872
03:50:46,260 --> 03:50:48,720
have these endpoint strings and this
5873
03:50:48,720 --> 03:50:51,359
method get in our react components but
5874
03:50:51,359 --> 03:50:53,460
rather a clean function that we can call
5875
03:50:53,460 --> 03:50:56,880
so what we do is we go into our source
5876
03:50:56,880 --> 03:50:59,279
folder in the front-end folder right
5877
03:50:59,279 --> 03:51:00,359
here
5878
03:51:00,359 --> 03:51:02,399
right click and create a new folder
5879
03:51:02,399 --> 03:51:05,960
which I'm gonna call Network
5880
03:51:06,540 --> 03:51:08,819
and in here we create a new file
5881
03:51:08,819 --> 03:51:12,960
which recall notes underscore API dot TS
5882
03:51:12,960 --> 03:51:15,720
not TSX it's not a react component it's
5883
03:51:15,720 --> 03:51:17,819
just a normal typescript file and you
5884
03:51:17,819 --> 03:51:19,439
can give this any name you want but I
5885
03:51:19,439 --> 03:51:21,479
think this won't make sense and in here
5886
03:51:21,479 --> 03:51:23,520
I want to move the fetch curl from our
5887
03:51:23,520 --> 03:51:25,979
get request here but there's also
5888
03:51:25,979 --> 03:51:27,899
another problem this code has right now
5889
03:51:27,899 --> 03:51:31,439
by default server responses like 400 and
5890
03:51:31,439 --> 03:51:34,080
500 which mean that something went wrong
5891
03:51:34,080 --> 03:51:36,960
are not handled as Errors By The fetch
5892
03:51:36,960 --> 03:51:39,120
call so they don't end up in the sketch
5893
03:51:39,120 --> 03:51:41,279
block here but they should because these
5894
03:51:41,279 --> 03:51:43,020
status codes mean that something went
5895
03:51:43,020 --> 03:51:44,640
wrong and in this case we want to show
5896
03:51:44,640 --> 03:51:46,380
an error in the UI because we don't have
5897
03:51:46,380 --> 03:51:48,720
any real data to work with so we have to
5898
03:51:48,720 --> 03:51:51,479
do this ourselves if we get these status
5899
03:51:51,479 --> 03:51:53,340
codes from the server we have to throw
5900
03:51:53,340 --> 03:51:55,680
an error ourselves but to see the
5901
03:51:55,680 --> 03:51:57,720
problem let's just throw an error from
5902
03:51:57,720 --> 03:51:59,880
our get nodes endpoint here
5903
03:51:59,880 --> 03:52:02,100
so in our backend code
5904
03:52:02,100 --> 03:52:05,880
let's just throw a create HTTP error
5905
03:52:05,880 --> 03:52:07,800
with 401 for example it doesn't really
5906
03:52:07,800 --> 03:52:09,479
matter I will remove this in a moment
5907
03:52:09,479 --> 03:52:12,239
this is just for presentation and now
5908
03:52:12,239 --> 03:52:14,939
when we try to access our front end
5909
03:52:14,939 --> 03:52:17,880
through localhost we don't see an alert
5910
03:52:17,880 --> 03:52:20,819
that we are normally do in our catch
5911
03:52:20,819 --> 03:52:22,200
block here
5912
03:52:22,200 --> 03:52:24,180
instead we just see an arrow in the
5913
03:52:24,180 --> 03:52:26,340
console notes map is not a function
5914
03:52:26,340 --> 03:52:28,560
which happens because we don't actually
5915
03:52:28,560 --> 03:52:30,960
get an array of notes back since we
5916
03:52:30,960 --> 03:52:33,540
throw this error in our server here I
5917
03:52:33,540 --> 03:52:35,279
don't know what we get back now
5918
03:52:35,279 --> 03:52:37,800
undefined I guess and our react app
5919
03:52:37,800 --> 03:52:39,540
can't work with that but the correct way
5920
03:52:39,540 --> 03:52:41,520
to handle this is to actually throw an
5921
03:52:41,520 --> 03:52:43,560
error when we get an error response like
5922
03:52:43,560 --> 03:52:45,779
401 but again we have to do this
5923
03:52:45,779 --> 03:52:48,600
ourselves first of all let's get rid of
5924
03:52:48,600 --> 03:52:50,399
this error code here
5925
03:52:50,399 --> 03:52:53,640
then we go into our notes API file again
5926
03:52:53,640 --> 03:52:55,920
and what we want to do is we want to
5927
03:52:55,920 --> 03:52:58,319
create a wrapper around fetch a wrapper
5928
03:52:58,319 --> 03:53:00,960
that uses fetch but throws an error if
5929
03:53:00,960 --> 03:53:04,260
the status code is 400 or 500 so we
5930
03:53:04,260 --> 03:53:06,779
create an async function we don't have
5931
03:53:06,779 --> 03:53:08,819
to export it because we will only use it
5932
03:53:08,819 --> 03:53:11,160
within this nodes API file
5933
03:53:11,160 --> 03:53:13,560
and I'm gonna call it fetch data which
5934
03:53:13,560 --> 03:53:16,020
is similar to fetch but it's our own
5935
03:53:16,020 --> 03:53:18,420
function you could also call it Fetch
5936
03:53:18,420 --> 03:53:21,420
with error I guess but I like this name
5937
03:53:21,420 --> 03:53:23,580
this will take the same arguments as the
5938
03:53:23,580 --> 03:53:25,260
fetch function itself
5939
03:53:25,260 --> 03:53:27,060
so when we look into fetch here we can
5940
03:53:27,060 --> 03:53:29,160
see that it takes an input request in
5941
03:53:29,160 --> 03:53:31,979
four and an optional init request in it
5942
03:53:31,979 --> 03:53:35,279
so let's add the same arguments here
5943
03:53:35,279 --> 03:53:40,380
input column of type request info
5944
03:53:40,380 --> 03:53:43,800
and an optional init
5945
03:53:43,800 --> 03:53:48,000
of type request in it
5946
03:53:48,000 --> 03:53:49,800
this way we can call this function like
5947
03:53:49,800 --> 03:53:51,660
the normal fetch function
5948
03:53:51,660 --> 03:53:54,359
so the next line is const response
5949
03:53:54,359 --> 03:53:56,819
equals a weight fetch so we execute the
5950
03:53:56,819 --> 03:53:58,439
fetch color itself
5951
03:53:58,439 --> 03:54:01,200
where we just pass the input and the
5952
03:54:01,200 --> 03:54:02,399
init
5953
03:54:02,399 --> 03:54:05,279
that repairs to the fetch data function
5954
03:54:05,279 --> 03:54:08,300
and then we do a check if
5955
03:54:08,300 --> 03:54:10,920
response dot okay
5956
03:54:10,920 --> 03:54:12,840
which is this property on the return
5957
03:54:12,840 --> 03:54:15,319
type of the normal fetch Carl
5958
03:54:15,319 --> 03:54:18,300
andresponse dot OK will return true if
5959
03:54:18,300 --> 03:54:21,899
the response is between 200 and 300. if
5960
03:54:21,899 --> 03:54:24,540
it's between 400 and 500 this will
5961
03:54:24,540 --> 03:54:27,660
return false so only if the response
5962
03:54:27,660 --> 03:54:30,779
HTTP code is okay we want to return the
5963
03:54:30,779 --> 03:54:32,340
response
5964
03:54:32,340 --> 03:54:34,620
else we want to throw an error so that
5965
03:54:34,620 --> 03:54:37,260
we end up in our catch block now our
5966
03:54:37,260 --> 03:54:39,600
errors also have a Json body right when
5967
03:54:39,600 --> 03:54:42,960
we look into the backend appts file we
5968
03:54:42,960 --> 03:54:44,640
set up our error Handler in a way that
5969
03:54:44,640 --> 03:54:46,800
the error gets sent back through the
5970
03:54:46,800 --> 03:54:48,060
Json body
5971
03:54:48,060 --> 03:54:50,160
so let's read this here so that we can
5972
03:54:50,160 --> 03:54:52,319
lock it and also maybe display it in the
5973
03:54:52,319 --> 03:54:54,960
UI depending on the error so we create a
5974
03:54:54,960 --> 03:54:58,580
const error body
5975
03:54:58,979 --> 03:55:01,279
which we get with a weight
5976
03:55:01,279 --> 03:55:03,840
response.json the normal way like we
5977
03:55:03,840 --> 03:55:07,939
also get a real body out of the response
5978
03:55:07,939 --> 03:55:10,580
const error message
5979
03:55:10,580 --> 03:55:15,239
equals error body Dot error
5980
03:55:15,239 --> 03:55:18,120
why error because this is the key we set
5981
03:55:18,120 --> 03:55:20,220
on this Json body here so those have to
5982
03:55:20,220 --> 03:55:22,560
be the same
5983
03:55:22,560 --> 03:55:24,899
and then we want to throw
5984
03:55:24,899 --> 03:55:26,880
an arrow
5985
03:55:26,880 --> 03:55:28,920
and for the message we will just pass
5986
03:55:28,920 --> 03:55:31,260
the error message here
5987
03:55:31,260 --> 03:55:33,060
now later we will get back to this
5988
03:55:33,060 --> 03:55:34,439
function and distinguish between
5989
03:55:34,439 --> 03:55:36,479
different kind of error codes and
5990
03:55:36,479 --> 03:55:37,979
through different errors which we can
5991
03:55:37,979 --> 03:55:39,960
then handle differently in our UI
5992
03:55:39,960 --> 03:55:42,180
depending on the type of error but for
5993
03:55:42,180 --> 03:55:43,739
now this is sufficient
5994
03:55:43,739 --> 03:55:46,800
now let's put our fetch note skull in
5995
03:55:46,800 --> 03:55:48,359
here
5996
03:55:48,359 --> 03:55:49,800
So Below
5997
03:55:49,800 --> 03:55:52,800
we write export async function because
5998
03:55:52,800 --> 03:55:55,380
we want to use this one from the outside
5999
03:55:55,380 --> 03:55:58,620
and let's call it fetch notes
6000
03:55:58,620 --> 03:56:01,859
it doesn't take any arguments
6001
03:56:01,859 --> 03:56:03,960
but it will return
6002
03:56:03,960 --> 03:56:07,979
a promise of type node array
6003
03:56:07,979 --> 03:56:09,720
now adding this return type here is
6004
03:56:09,720 --> 03:56:12,060
optional but I like to edit because this
6005
03:56:12,060 --> 03:56:13,920
makes sure that we are actually return
6006
03:56:13,920 --> 03:56:16,140
the correct value from here and not mess
6007
03:56:16,140 --> 03:56:17,760
anything up when we change the code
6008
03:56:17,760 --> 03:56:20,340
later and all async functions have to
6009
03:56:20,340 --> 03:56:22,739
return a promise because async is
6010
03:56:22,739 --> 03:56:24,600
basically just syntactic sugar around
6011
03:56:24,600 --> 03:56:26,880
promises so whenever you create a
6012
03:56:26,880 --> 03:56:28,739
function that is async and return
6013
03:56:28,739 --> 03:56:30,720
something the return type is
6014
03:56:30,720 --> 03:56:33,420
automatically wrapped into a promise and
6015
03:56:33,420 --> 03:56:35,100
then we want to take the code from over
6016
03:56:35,100 --> 03:56:36,899
here
6017
03:56:36,899 --> 03:56:39,899
these two lines cut them out and paste
6018
03:56:39,899 --> 03:56:42,180
them in here just with a little change
6019
03:56:42,180 --> 03:56:44,880
we don't want to store the Json body in
6020
03:56:44,880 --> 03:56:47,520
a variable we want to return it we want
6021
03:56:47,520 --> 03:56:49,680
to call fetch data instead of the normal
6022
03:56:49,680 --> 03:56:53,699
fetch so that 400 and 500 responses are
6023
03:56:53,699 --> 03:56:55,800
treated as arrows and we have to import
6024
03:56:55,800 --> 03:56:58,199
a note type here
6025
03:56:58,199 --> 03:57:01,319
so let's just use autocompletion
6026
03:57:01,319 --> 03:57:04,199
and it should add this import statement
6027
03:57:04,199 --> 03:57:07,859
let's save this go back into our ftsx
6028
03:57:07,859 --> 03:57:10,680
file and call our newer shiny function
6029
03:57:10,680 --> 03:57:12,239
here instead
6030
03:57:12,239 --> 03:57:14,760
so first of all I want to import our
6031
03:57:14,760 --> 03:57:17,399
notes API file and I import it manually
6032
03:57:17,399 --> 03:57:19,439
because I want to import it in a special
6033
03:57:19,439 --> 03:57:21,540
way I don't want to import each function
6034
03:57:21,540 --> 03:57:23,460
separately I want to add them to a
6035
03:57:23,460 --> 03:57:26,100
namespace so we do the star asterisk
6036
03:57:26,100 --> 03:57:30,300
symbol write s nodes API
6037
03:57:30,300 --> 03:57:32,819
from
6038
03:57:32,819 --> 03:57:37,020
and the folder is dot slash network and
6039
03:57:37,020 --> 03:57:39,060
the notes API file
6040
03:57:39,060 --> 03:57:41,300
and now here we can write
6041
03:57:41,300 --> 03:57:44,220
coins notes equals
6042
03:57:44,220 --> 03:57:47,100
await notes API
6043
03:57:47,100 --> 03:57:49,439
Dot fetchnotes
6044
03:57:49,439 --> 03:57:51,420
and now we have this clean function that
6045
03:57:51,420 --> 03:57:53,340
we can call here and we hide the low
6046
03:57:53,340 --> 03:57:55,620
level fetch code here inside this
6047
03:57:55,620 --> 03:57:58,319
separate file I just think this looks
6048
03:57:58,319 --> 03:58:00,359
better and it keeps these components
6049
03:58:00,359 --> 03:58:02,580
clean but let's see if our error now
6050
03:58:02,580 --> 03:58:05,160
works properly so I go into our endpoint
6051
03:58:05,160 --> 03:58:07,140
again and I add this
6052
03:58:07,140 --> 03:58:09,359
Arrow here once again which of course I
6053
03:58:09,359 --> 03:58:12,420
will remove in a moment
6054
03:58:12,420 --> 03:58:16,580
let's open our page here again
6055
03:58:17,399 --> 03:58:20,760
refresh it and now we get our alert
6056
03:58:20,760 --> 03:58:22,500
message here which we do in the catch
6057
03:58:22,500 --> 03:58:25,199
block because now 401 and also 500
6058
03:58:25,199 --> 03:58:26,640
responses
6059
03:58:26,640 --> 03:58:28,920
throw errors and end up at the correct
6060
03:58:28,920 --> 03:58:31,560
place now let's also add a function to a
6061
03:58:31,560 --> 03:58:33,960
create a new node which we put into our
6062
03:58:33,960 --> 03:58:36,239
notes API file as well
6063
03:58:36,239 --> 03:58:40,739
let's create an export async function
6064
03:58:40,739 --> 03:58:43,319
create note
6065
03:58:43,319 --> 03:58:45,660
and this of course has to take input the
6066
03:58:45,660 --> 03:58:48,120
node title and the text now we could put
6067
03:58:48,120 --> 03:58:49,920
this as separate string arguments in
6068
03:58:49,920 --> 03:58:51,420
here but I actually want to create an
6069
03:58:51,420 --> 03:58:53,340
interface for that because we use the
6070
03:58:53,340 --> 03:58:55,199
same types later in our form and this
6071
03:58:55,199 --> 03:58:57,120
way we don't have to repeat title and
6072
03:58:57,120 --> 03:58:59,040
text again all the time and when we make
6073
03:58:59,040 --> 03:59:00,840
changes we have one single place to
6074
03:59:00,840 --> 03:59:02,279
change it
6075
03:59:02,279 --> 03:59:04,560
so we put an export interface in here
6076
03:59:04,560 --> 03:59:06,540
export because we want to use this from
6077
03:59:06,540 --> 03:59:08,760
a different file later
6078
03:59:08,760 --> 03:59:12,300
let's call it node input
6079
03:59:12,300 --> 03:59:14,939
and every node needs us a title in form
6080
03:59:14,939 --> 03:59:16,260
of a string
6081
03:59:16,260 --> 03:59:19,560
and the optional body text which is a
6082
03:59:19,560 --> 03:59:21,000
string as well
6083
03:59:21,000 --> 03:59:22,859
and then we can use this type here as
6084
03:59:22,859 --> 03:59:26,399
the input parameter
6085
03:59:26,399 --> 03:59:29,120
like this
6086
03:59:29,819 --> 03:59:32,340
this will also return something I
6087
03:59:32,340 --> 03:59:34,800
promise because it's an async function
6088
03:59:34,800 --> 03:59:36,660
and it will return the note we created
6089
03:59:36,660 --> 03:59:39,479
when this was successful so a promise of
6090
03:59:39,479 --> 03:59:41,040
type node
6091
03:59:41,040 --> 03:59:46,260
so we create a const response equals
6092
03:59:46,260 --> 03:59:49,500
await fetch data not fetch but fetch
6093
03:59:49,500 --> 03:59:51,840
data the endpoint where we create new
6094
03:59:51,840 --> 03:59:53,880
nodes which is the same for getting
6095
03:59:53,880 --> 03:59:54,899
nodes
6096
03:59:54,899 --> 03:59:59,220
so slash API slash nodes
6097
03:59:59,220 --> 04:00:00,779
comma
6098
04:00:00,779 --> 04:00:02,640
and I put a configuration into a
6099
04:00:02,640 --> 04:00:04,439
separate file because this will be a bit
6100
04:00:04,439 --> 04:00:07,859
longer so we add a pair of curly braces
6101
04:00:07,859 --> 04:00:11,640
the method this time of course is post
6102
04:00:11,640 --> 04:00:13,739
and we have to add some headers to our
6103
04:00:13,739 --> 04:00:16,140
request to indicate what kind of data we
6104
04:00:16,140 --> 04:00:18,660
are sending so we write headers colon
6105
04:00:18,660 --> 04:00:21,300
add a pair of curly braces then we add a
6106
04:00:21,300 --> 04:00:22,920
string as the key
6107
04:00:22,920 --> 04:00:24,779
and the spelling has to be correct
6108
04:00:24,779 --> 04:00:26,640
otherwise it will not recognize this
6109
04:00:26,640 --> 04:00:29,340
header content type like this with the
6110
04:00:29,340 --> 04:00:30,779
same casing
6111
04:00:30,779 --> 04:00:33,840
colon and the content type will be
6112
04:00:33,840 --> 04:00:37,199
application slash Json this just helps
6113
04:00:37,199 --> 04:00:39,120
the back end what kind of format our
6114
04:00:39,120 --> 04:00:41,100
body is in of course we are sending a
6115
04:00:41,100 --> 04:00:43,080
Json going to add a trailing comma here
6116
04:00:43,080 --> 04:00:44,939
we don't have to but again I like
6117
04:00:44,939 --> 04:00:46,800
trailing commas
6118
04:00:46,800 --> 04:00:49,199
and then we want to pass the body itself
6119
04:00:49,199 --> 04:00:51,899
and since we can only send string back
6120
04:00:51,899 --> 04:00:53,939
and forth between our server and our
6121
04:00:53,939 --> 04:00:56,460
front end we are one a stringifier the
6122
04:00:56,460 --> 04:00:58,739
node that we pass here so we write Json
6123
04:00:58,739 --> 04:01:01,979
in our uppercase Dot stringify and pass
6124
04:01:01,979 --> 04:01:04,140
our node
6125
04:01:04,140 --> 04:01:06,180
then I'm going to put the semicolon here
6126
04:01:06,180 --> 04:01:08,340
because we are not Savages
6127
04:01:08,340 --> 04:01:11,279
okay and if this went successful in
6128
04:01:11,279 --> 04:01:13,680
which case it didn't throw then we can
6129
04:01:13,680 --> 04:01:15,260
just return
6130
04:01:15,260 --> 04:01:17,939
response.json which should contain the
6131
04:01:17,939 --> 04:01:20,460
newly created note which we can then put
6132
04:01:20,460 --> 04:01:22,500
into our UI
6133
04:01:22,500 --> 04:01:24,540
okay and now we want to add some kind of
6134
04:01:24,540 --> 04:01:27,720
form to input new nodes and send them to
6135
04:01:27,720 --> 04:01:30,000
our backend and for this we will use
6136
04:01:30,000 --> 04:01:31,920
bootstrap models
6137
04:01:31,920 --> 04:01:33,720
which are these dialogues here that we
6138
04:01:33,720 --> 04:01:34,920
can open
6139
04:01:34,920 --> 04:01:37,020
and we will create such a model with
6140
04:01:37,020 --> 04:01:39,359
some form input in it I think it's just
6141
04:01:39,359 --> 04:01:42,420
a quick way to insert new nodes
6142
04:01:42,420 --> 04:01:45,660
so let's go into our front-end folder
6143
04:01:45,660 --> 04:01:48,180
and create a new file in our components
6144
04:01:48,180 --> 04:01:49,800
folder
6145
04:01:49,800 --> 04:01:51,660
right in here
6146
04:01:51,660 --> 04:01:55,140
let's call it add note dialog and this
6147
04:01:55,140 --> 04:01:57,239
one is a TSX file again
6148
04:01:57,239 --> 04:01:59,580
and I said the second time we create a
6149
04:01:59,580 --> 04:02:01,920
react component we want to use an
6150
04:02:01,920 --> 04:02:04,020
extension for that so we click here on
6151
04:02:04,020 --> 04:02:06,600
this extensions tab
6152
04:02:06,600 --> 04:02:08,699
and I think we should find it when we
6153
04:02:08,699 --> 04:02:10,800
search for react or react Snippets
6154
04:02:10,800 --> 04:02:13,020
actually
6155
04:02:13,020 --> 04:02:15,000
should be this one here
6156
04:02:15,000 --> 04:02:18,000
this gives us these different shortcuts
6157
04:02:18,000 --> 04:02:19,680
we can use to create stuff like
6158
04:02:19,680 --> 04:02:22,080
components and your state and stuff like
6159
04:02:22,080 --> 04:02:23,939
that so let's install this but of course
6160
04:02:23,939 --> 04:02:25,739
you don't have to if you don't want you
6161
04:02:25,739 --> 04:02:27,960
can also just keep typing them out
6162
04:02:27,960 --> 04:02:30,359
manually like a peasant
6163
04:02:30,359 --> 04:02:33,359
okay and now we can just type sfc
6164
04:02:33,359 --> 04:02:35,699
which creates this functional react
6165
04:02:35,699 --> 04:02:36,840
component
6166
04:02:36,840 --> 04:02:39,899
going to paste the name here
6167
04:02:39,899 --> 04:02:41,640
and then we have the return statement
6168
04:02:41,640 --> 04:02:43,920
and the export and everything let's
6169
04:02:43,920 --> 04:02:46,199
start by creating a minimal version of
6170
04:02:46,199 --> 04:02:48,180
this dialog just to see how we can show
6171
04:02:48,180 --> 04:02:49,620
it in the UI
6172
04:02:49,620 --> 04:02:52,080
so between return
6173
04:02:52,080 --> 04:02:54,479
we add a modal tag from the react
6174
04:02:54,479 --> 04:02:56,279
bootstrap package
6175
04:02:56,279 --> 04:02:58,080
close this
6176
04:02:58,080 --> 04:03:01,560
and here in the opening text before the
6177
04:03:01,560 --> 04:03:04,620
closing angle bracket we press show
6178
04:03:04,620 --> 04:03:07,680
this has the same effect as passing
6179
04:03:07,680 --> 04:03:10,020
shower equals true like this whenever
6180
04:03:10,020 --> 04:03:11,580
you want to pass through you can also
6181
04:03:11,580 --> 04:03:14,100
just pass the name of the property
6182
04:03:14,100 --> 04:03:17,279
then in between these tags we add a
6183
04:03:17,279 --> 04:03:20,279
modal.header
6184
04:03:20,279 --> 04:03:23,220
where we add the close button to the tag
6185
04:03:23,220 --> 04:03:25,380
to add this little X in the top right
6186
04:03:25,380 --> 04:03:27,000
corner where we can close this model
6187
04:03:27,000 --> 04:03:28,199
later
6188
04:03:28,199 --> 04:03:29,760
and in between here we add the
6189
04:03:29,760 --> 04:03:31,500
model.title
6190
04:03:31,500 --> 04:03:33,540
when this tag is just responsible for
6191
04:03:33,540 --> 04:03:35,460
formatting the title properly with the
6192
04:03:35,460 --> 04:03:38,040
correct text size and everything
6193
04:03:38,040 --> 04:03:41,040
and the title will be a add note
6194
04:03:41,040 --> 04:03:43,140
let's keep it like that for now and
6195
04:03:43,140 --> 04:03:45,960
display this model from our app TSX file
6196
04:03:45,960 --> 04:03:48,840
and the way this works is that we need a
6197
04:03:48,840 --> 04:03:51,180
state that tells us if the model should
6198
04:03:51,180 --> 04:03:53,340
be shown or not and the little animation
6199
04:03:53,340 --> 04:03:55,380
for opening and closing this all happens
6200
04:03:55,380 --> 04:03:56,760
automatically
6201
04:03:56,760 --> 04:03:59,279
we just need to 10 react if it should be
6202
04:03:59,279 --> 04:04:01,920
shown or not so below our notes State
6203
04:04:01,920 --> 04:04:03,960
here we create another state the same
6204
04:04:03,960 --> 04:04:07,680
way we call the variable show at node
6205
04:04:07,680 --> 04:04:09,300
dialog
6206
04:04:09,300 --> 04:04:12,899
and the zeta function will be a set
6207
04:04:12,899 --> 04:04:16,859
show at node dialog in camera case
6208
04:04:16,859 --> 04:04:19,260
and we initialize this with us State
6209
04:04:19,260 --> 04:04:20,520
faults
6210
04:04:20,520 --> 04:04:23,040
and again typescript can infer the type
6211
04:04:23,040 --> 04:04:25,080
because we pass false which is a Boolean
6212
04:04:25,080 --> 04:04:27,420
so it knows that this variable here is a
6213
04:04:27,420 --> 04:04:30,000
Boolean and then we can just add this
6214
04:04:30,000 --> 04:04:32,880
dialog like every other piece of UI into
6215
04:04:32,880 --> 04:04:35,100
our return statement here where we draw
6216
04:04:35,100 --> 04:04:37,260
the UI on the screen we do this within
6217
04:04:37,260 --> 04:04:39,479
the container block because it needs
6218
04:04:39,479 --> 04:04:41,279
some text on the outside can be a
6219
04:04:41,279 --> 04:04:43,140
container can be a div doesn't matter
6220
04:04:43,140 --> 04:04:46,020
then we add a pair of curly braces
6221
04:04:46,020 --> 04:04:48,600
and now we can do a little yeah react
6222
04:04:48,600 --> 04:04:51,180
syntax that might look a bit weird at
6223
04:04:51,180 --> 04:04:53,580
first but this is how you can draw UI
6224
04:04:53,580 --> 04:04:55,439
components conditionally on the screen
6225
04:04:55,439 --> 04:04:58,439
we take the name of our state show add
6226
04:04:58,439 --> 04:05:02,040
notes dialog and add two embossance
6227
04:05:02,040 --> 04:05:04,080
which means that whatever we put after
6228
04:05:04,080 --> 04:05:06,300
that will only be a drawn on the screen
6229
04:05:06,300 --> 04:05:09,239
if show at no dialog is true if it falls
6230
04:05:09,239 --> 04:05:12,300
it will not be drawn and here we add our
6231
04:05:12,300 --> 04:05:14,699
add node dialog which right now it
6232
04:05:14,699 --> 04:05:17,100
doesn't take any arguments but let's try
6233
04:05:17,100 --> 04:05:20,460
it out first of all I still have the 401
6234
04:05:20,460 --> 04:05:22,199
array of course we have to remove this
6235
04:05:22,199 --> 04:05:24,239
then since we don't have a button to
6236
04:05:24,239 --> 04:05:26,819
show our dialog yet let's initialize
6237
04:05:26,819 --> 04:05:28,500
this withdrawal
6238
04:05:28,500 --> 04:05:30,540
just to see our dialogue here on the
6239
04:05:30,540 --> 04:05:32,160
screen and there it is right now we
6240
04:05:32,160 --> 04:05:34,439
can't close it because we don't have a
6241
04:05:34,439 --> 04:05:36,899
way to set show at node dialog to false
6242
04:05:36,899 --> 04:05:38,819
yet but you can already see that this is
6243
04:05:38,819 --> 04:05:40,739
displayed on the screen here
6244
04:05:40,739 --> 04:05:43,560
let's set this back to false we will
6245
04:05:43,560 --> 04:05:45,660
show this later when we click our add
6246
04:05:45,660 --> 04:05:47,819
new note button and I want to mention
6247
04:05:47,819 --> 04:05:50,040
something instead of writing it like
6248
04:05:50,040 --> 04:05:52,199
this with this variable down here
6249
04:05:52,199 --> 04:05:54,720
we could have also passed this variable
6250
04:05:54,720 --> 04:05:57,840
as a property and used it as the value
6251
04:05:57,840 --> 04:06:00,540
for this show property here so then we
6252
04:06:00,540 --> 04:06:02,819
could remove this part here with the two
6253
04:06:02,819 --> 04:06:05,220
ampersands and pass show as node dialog
6254
04:06:05,220 --> 04:06:07,319
as a property the reason I do it like
6255
04:06:07,319 --> 04:06:09,420
this is because when we do it the other
6256
04:06:09,420 --> 04:06:12,359
way then any input in this dialog will
6257
04:06:12,359 --> 04:06:14,520
be maintained after we close the dialog
6258
04:06:14,520 --> 04:06:16,560
so let's say we type in a title and a
6259
04:06:16,560 --> 04:06:18,359
text and we close the dialog and open it
6260
04:06:18,359 --> 04:06:19,979
again the title and the text will still
6261
04:06:19,979 --> 04:06:21,960
be there because this date is contained
6262
04:06:21,960 --> 04:06:24,300
in this dialog component depending on
6263
04:06:24,300 --> 04:06:26,040
your situation this might be what you
6264
04:06:26,040 --> 04:06:29,340
need but in our case I want to clear the
6265
04:06:29,340 --> 04:06:31,319
whole form whenever we close the dialog
6266
04:06:31,319 --> 04:06:33,239
because if you will try to enter a node
6267
04:06:33,239 --> 04:06:35,220
and close the dialog I assume that you
6268
04:06:35,220 --> 04:06:36,660
will change your mind and you don't want
6269
04:06:36,660 --> 04:06:39,000
to store the input that's right I write
6270
04:06:39,000 --> 04:06:41,520
the syntax like this since this will
6271
04:06:41,520 --> 04:06:43,920
completely remove this component from
6272
04:06:43,920 --> 04:06:46,080
the screen the state inside this
6273
04:06:46,080 --> 04:06:48,300
component is also lost just as a little
6274
04:06:48,300 --> 04:06:50,880
explanation why we write it this way and
6275
04:06:50,880 --> 04:06:52,620
don't use this show property here
6276
04:06:52,620 --> 04:06:55,439
instead we hardcore show to true
6277
04:06:55,439 --> 04:06:57,720
and handle showing or not showing from
6278
04:06:57,720 --> 04:06:59,939
the outside like this now let's actually
6279
04:06:59,939 --> 04:07:01,920
go ahead and add the button that can
6280
04:07:01,920 --> 04:07:04,199
open this dialog so we don't have to
6281
04:07:04,199 --> 04:07:06,600
hard code the state here to true so what
6282
04:07:06,600 --> 04:07:08,399
we do is inside this container here
6283
04:07:08,399 --> 04:07:11,279
above our node grid
6284
04:07:11,279 --> 04:07:13,439
we add a button
6285
04:07:13,439 --> 04:07:16,859
which was a add new note
6286
04:07:16,859 --> 04:07:21,319
the button gets an on click attribute
6287
04:07:21,660 --> 04:07:23,520
where we pass an error function like
6288
04:07:23,520 --> 04:07:24,239
this
6289
04:07:24,239 --> 04:07:27,960
and this will call set show at node
6290
04:07:27,960 --> 04:07:30,180
dialog and set this to true
6291
04:07:30,180 --> 04:07:33,120
this way we display our dialog
6292
04:07:33,120 --> 04:07:37,199
then we save this we go into our dialog
6293
04:07:37,199 --> 04:07:39,239
and we add an interface because we want
6294
04:07:39,239 --> 04:07:41,220
to pass some data here
6295
04:07:41,220 --> 04:07:44,760
interface add note dialog props
6296
04:07:44,760 --> 04:07:46,859
we want to add a way to a close the
6297
04:07:46,859 --> 04:07:48,960
dialog now when we click outside of the
6298
04:07:48,960 --> 04:07:50,880
dialog or we click the close button all
6299
04:07:50,880 --> 04:07:53,100
of this will be detected inside here we
6300
04:07:53,100 --> 04:07:55,020
just have to pass it to The Carling
6301
04:07:55,020 --> 04:07:57,180
component to tell okay change the state
6302
04:07:57,180 --> 04:08:00,239
back to a z show dialog false and we do
6303
04:08:00,239 --> 04:08:02,460
this via a callback this might be
6304
04:08:02,460 --> 04:08:04,260
confusing at first but this is just how
6305
04:08:04,260 --> 04:08:06,300
you do it in declarative UI if you've
6306
04:08:06,300 --> 04:08:07,979
been coding with jetpack compose on
6307
04:08:07,979 --> 04:08:09,420
Android for example then it works
6308
04:08:09,420 --> 04:08:11,279
similarly with these callbacks on
6309
04:08:11,279 --> 04:08:13,260
dismiss is a function
6310
04:08:13,260 --> 04:08:15,359
that doesn't take any arguments and it
6311
04:08:15,359 --> 04:08:17,699
doesn't return anything so write it like
6312
04:08:17,699 --> 04:08:19,680
this
6313
04:08:19,680 --> 04:08:22,319
added here as the actual argument on
6314
04:08:22,319 --> 04:08:24,680
dismiss
6315
04:08:25,020 --> 04:08:26,939
we destructured this as we have done
6316
04:08:26,939 --> 04:08:29,580
before and use our add no dialog props
6317
04:08:29,580 --> 04:08:33,660
type here and then on this model tag
6318
04:08:33,660 --> 04:08:36,720
we have this on height callback which
6319
04:08:36,720 --> 04:08:39,120
will be a cult whenever we do an action
6320
04:08:39,120 --> 04:08:41,160
that closes the dialog this includes
6321
04:08:41,160 --> 04:08:43,199
clicking outside of the dialog but also
6322
04:08:43,199 --> 04:08:44,760
clicking this close button for example
6323
04:08:44,760 --> 04:08:47,040
and here we just want to forward to our
6324
04:08:47,040 --> 04:08:49,260
on dismissed callback and since this
6325
04:08:49,260 --> 04:08:51,300
doesn't take any argument we can just
6326
04:08:51,300 --> 04:08:53,220
pass the function as a reference
6327
04:08:53,220 --> 04:08:55,140
directly by its name and we don't have
6328
04:08:55,140 --> 04:08:57,000
to pass an error function we could also
6329
04:08:57,000 --> 04:08:59,040
write it like this
6330
04:08:59,040 --> 04:09:00,899
this would have the same effect but
6331
04:09:00,899 --> 04:09:02,640
since this doesn't take any arguments
6332
04:09:02,640 --> 04:09:04,620
this is basically a shorthand for the
6333
04:09:04,620 --> 04:09:06,300
same thing
6334
04:09:06,300 --> 04:09:08,880
we save this and now of course we have
6335
04:09:08,880 --> 04:09:11,220
to pass this new property to our dialog
6336
04:09:11,220 --> 04:09:12,300
here
6337
04:09:12,300 --> 04:09:14,939
so we pass on dismiss
6338
04:09:14,939 --> 04:09:17,340
which this time again is an error
6339
04:09:17,340 --> 04:09:19,800
function because we want to call such
6340
04:09:19,800 --> 04:09:22,439
show at notes dialog
6341
04:09:22,439 --> 04:09:25,620
and set us back to files
6342
04:09:25,620 --> 04:09:27,660
so let's try this out we open our page
6343
04:09:27,660 --> 04:09:29,520
again and The Styling is not great at
6344
04:09:29,520 --> 04:09:31,560
the moment we will fix this later but
6345
04:09:31,560 --> 04:09:33,779
our button is still and we can use it to
6346
04:09:33,779 --> 04:09:36,779
open our dialog so this sets the shower
6347
04:09:36,779 --> 04:09:38,939
dialog state to true and when we click
6348
04:09:38,939 --> 04:09:41,340
the close button or we click outside it
6349
04:09:41,340 --> 04:09:43,739
sets the state back to false and this is
6350
04:09:43,739 --> 04:09:45,660
how you show a model dialogues in
6351
04:09:45,660 --> 04:09:47,160
declarative UI
6352
04:09:47,160 --> 04:09:50,460
now let's finish our add note dialog
6353
04:09:50,460 --> 04:09:53,819
and for forms we also have bootstrap
6354
04:09:53,819 --> 04:09:55,800
components that we can use they just
6355
04:09:55,800 --> 04:09:58,140
look a bit better than the usual HTML
6356
04:09:58,140 --> 04:10:00,180
forms you have they look like this of
6357
04:10:00,180 --> 04:10:01,680
course you can change the styling if you
6358
04:10:01,680 --> 04:10:02,580
want
6359
04:10:02,580 --> 04:10:05,160
but yeah you can do a lot with them
6360
04:10:05,160 --> 04:10:07,260
okay so let's add these form components
6361
04:10:07,260 --> 04:10:09,239
to our add node dialog
6362
04:10:09,239 --> 04:10:11,640
we go below the model header and I like
6363
04:10:11,640 --> 04:10:13,620
to put a space in here the space has
6364
04:10:13,620 --> 04:10:15,540
nothing to do with how the motor looks
6365
04:10:15,540 --> 04:10:18,359
later it's just in our code
6366
04:10:18,359 --> 04:10:20,460
and we add the modal body for the main
6367
04:10:20,460 --> 04:10:21,660
part
6368
04:10:21,660 --> 04:10:23,880
and in here we want to put a form and we
6369
04:10:23,880 --> 04:10:25,920
use the form from the react bootstrap
6370
04:10:25,920 --> 04:10:27,840
package
6371
04:10:27,840 --> 04:10:29,939
and then we create our form like this
6372
04:10:29,939 --> 04:10:32,040
and I've pulled it into a split screen
6373
04:10:32,040 --> 04:10:34,319
again so we can see how this form comes
6374
04:10:34,319 --> 04:10:35,939
into a reality
6375
04:10:35,939 --> 04:10:38,939
so we need a form dot group and again I
6376
04:10:38,939 --> 04:10:40,560
know all of this from the bootstrap
6377
04:10:40,560 --> 04:10:42,660
documentation the idea explain how you
6378
04:10:42,660 --> 04:10:45,479
have to structure this the form group
6379
04:10:45,479 --> 04:10:48,359
will take a class from the bootstrap
6380
04:10:48,359 --> 04:10:52,080
Library which is called mb-3 this just
6381
04:10:52,080 --> 04:10:54,239
adds some margin at the bottom of each
6382
04:10:54,239 --> 04:10:56,340
input field
6383
04:10:56,340 --> 04:10:59,160
in here inside the form group we add a
6384
04:10:59,160 --> 04:11:01,560
form label
6385
04:11:01,560 --> 04:11:04,319
which we call Title okay so that's the
6386
04:11:04,319 --> 04:11:05,760
first piece of form
6387
04:11:05,760 --> 04:11:08,640
on the screen below the form label we
6388
04:11:08,640 --> 04:11:11,100
add a form dot control
6389
04:11:11,100 --> 04:11:13,800
which is the actual input itself
6390
04:11:13,800 --> 04:11:16,560
this takes a self-closing tag because
6391
04:11:16,560 --> 04:11:18,479
everything that we pass to this
6392
04:11:18,479 --> 04:11:21,120
component is a property when we save
6393
04:11:21,120 --> 04:11:23,100
this we already see the input field but
6394
04:11:23,100 --> 04:11:25,199
we have to Define some configuration
6395
04:11:25,199 --> 04:11:27,540
here this is the same for react forms
6396
04:11:27,540 --> 04:11:30,120
but also normal HTML forms we have to
6397
04:11:30,120 --> 04:11:33,180
give it a type which we set to text
6398
04:11:33,180 --> 04:11:35,340
this just helps the browser know what
6399
04:11:35,340 --> 04:11:37,739
kind of input this input field expects
6400
04:11:37,739 --> 04:11:39,660
for password for example we will later
6401
04:11:39,660 --> 04:11:41,160
set the type to password which
6402
04:11:41,160 --> 04:11:43,620
automatically hides the characters we
6403
04:11:43,620 --> 04:11:45,899
can add a placeholder
6404
04:11:45,899 --> 04:11:47,880
set this to title
6405
04:11:47,880 --> 04:11:49,620
which adds this additional little text
6406
04:11:49,620 --> 04:11:52,080
Insider field that is only there until
6407
04:11:52,080 --> 04:11:54,600
we insert some text that's it for this
6408
04:11:54,600 --> 04:11:56,460
input field for now we will later add
6409
04:11:56,460 --> 04:11:58,319
some more stuff but not right now
6410
04:11:58,319 --> 04:12:00,840
instead We'll add another form group
6411
04:12:00,840 --> 04:12:03,180
below the first one
6412
04:12:03,180 --> 04:12:05,760
this will take the body of the node the
6413
04:12:05,760 --> 04:12:07,739
text itself
6414
04:12:07,739 --> 04:12:12,720
again it takes a class name mbr3
6415
04:12:13,380 --> 04:12:16,080
it gets a label
6416
04:12:16,080 --> 04:12:19,520
which was a text
6417
04:12:19,680 --> 04:12:23,479
and we add another form control
6418
04:12:23,939 --> 04:12:26,279
this time we don't set the type to text
6419
04:12:26,279 --> 04:12:30,239
instead we do this we write ask equals
6420
04:12:30,239 --> 04:12:33,840
text area but as a string like this
6421
04:12:33,840 --> 04:12:35,939
which creates this larger input field
6422
04:12:35,939 --> 04:12:38,819
where we can change the size of it this
6423
04:12:38,819 --> 04:12:42,000
is just another type of HTML input
6424
04:12:42,000 --> 04:12:45,540
we can also Define the rows number which
6425
04:12:45,540 --> 04:12:48,479
sets the default the size
6426
04:12:48,479 --> 04:12:51,180
of this input field so by default this
6427
04:12:51,180 --> 04:12:54,300
has a size of five lines basically
6428
04:12:54,300 --> 04:12:57,899
again we set the placeholder
6429
04:12:57,899 --> 04:13:00,620
to text
6430
04:13:00,720 --> 04:13:03,239
and that's it for this component for now
6431
04:13:03,239 --> 04:13:05,340
the last thing we need is a button to
6432
04:13:05,340 --> 04:13:07,620
submit this form right and I want to
6433
04:13:07,620 --> 04:13:09,540
have this button in the folder of the
6434
04:13:09,540 --> 04:13:11,460
model it's just a better look in my
6435
04:13:11,460 --> 04:13:12,439
opinion
6436
04:13:12,439 --> 04:13:15,540
Zorb below the modal body
6437
04:13:15,540 --> 04:13:19,340
we create a model footer
6438
04:13:19,739 --> 04:13:23,300
and in here we put a button
6439
04:13:24,600 --> 04:13:27,359
that was a safe or submit or whatever
6440
04:13:27,359 --> 04:13:28,620
you want
6441
04:13:28,620 --> 04:13:30,779
and this button takes some properties as
6442
04:13:30,779 --> 04:13:31,859
well
6443
04:13:31,859 --> 04:13:34,500
we want to set the type of the button
6444
04:13:34,500 --> 04:13:36,180
to submit
6445
04:13:36,180 --> 04:13:38,460
and this is not an arbitrary string this
6446
04:13:38,460 --> 04:13:40,859
has a special effect if we put this
6447
04:13:40,859 --> 04:13:43,439
button inside the form here inside this
6448
04:13:43,439 --> 04:13:46,859
form tags then submit tells the browser
6449
04:13:46,859 --> 04:13:48,840
that this button is supposed to send
6450
04:13:48,840 --> 04:13:51,060
this data so it has an effect when we
6451
04:13:51,060 --> 04:13:53,040
click it automatically without us having
6452
04:13:53,040 --> 04:13:54,960
to add an on click listener this is the
6453
04:13:54,960 --> 04:13:57,300
property that tells HTML components that
6454
04:13:57,300 --> 04:13:59,100
this button is responsible for sending
6455
04:13:59,100 --> 04:14:01,800
the form but since we put this button
6456
04:14:01,800 --> 04:14:03,960
into this footer here it's disconnected
6457
04:14:03,960 --> 04:14:05,399
from the form as you can see it's
6458
04:14:05,399 --> 04:14:07,260
outside of it this way the browser
6459
04:14:07,260 --> 04:14:09,120
doesn't know anymore that these two are
6460
04:14:09,120 --> 04:14:10,859
connected but we can fix this with
6461
04:14:10,859 --> 04:14:12,359
another property
6462
04:14:12,359 --> 04:14:15,540
with the form property we can add an ID
6463
04:14:15,540 --> 04:14:20,040
here let's say add node form and then we
6464
04:14:20,040 --> 04:14:22,319
can set the exact same ID on the form
6465
04:14:22,319 --> 04:14:24,479
itself this way the browser knows that
6466
04:14:24,479 --> 04:14:26,640
these two are connected so we set an ID
6467
04:14:26,640 --> 04:14:27,540
here
6468
04:14:27,540 --> 04:14:30,000
and it has to be spelled exactly like
6469
04:14:30,000 --> 04:14:33,620
down here at node form
6470
04:14:33,779 --> 04:14:35,640
now this button is connected to this
6471
04:14:35,640 --> 04:14:37,319
form right now this won't do anything
6472
04:14:37,319 --> 04:14:39,479
because we still have to Define what
6473
04:14:39,479 --> 04:14:41,460
happens when we submit a form but now
6474
04:14:41,460 --> 04:14:43,859
these two are connected I think when we
6475
04:14:43,859 --> 04:14:45,720
click it by default the page will
6476
04:14:45,720 --> 04:14:48,600
refresh this is how this behaves by
6477
04:14:48,600 --> 04:14:51,359
default but we will change this in a
6478
04:14:51,359 --> 04:14:53,520
moment now handling forms manually
6479
04:14:53,520 --> 04:14:55,680
especially in react can be a bit
6480
04:14:55,680 --> 04:14:57,420
trickier because you have to synchronize
6481
04:14:57,420 --> 04:14:59,100
it with the state you have to handle
6482
04:14:59,100 --> 04:15:01,260
different error States and so on and
6483
04:15:01,260 --> 04:15:03,300
also you don't want to retract a UI too
6484
04:15:03,300 --> 04:15:05,100
often when your insert some new text
6485
04:15:05,100 --> 04:15:07,500
this is why you usually want to use a
6486
04:15:07,500 --> 04:15:09,180
package for that which handles the form
6487
04:15:09,180 --> 04:15:11,460
stuff and the most popular one for react
6488
04:15:11,460 --> 04:15:13,680
is react hook form there's also another
6489
04:15:13,680 --> 04:15:16,080
one called formic which hasn't gotten
6490
04:15:16,080 --> 04:15:18,660
any new updates in almost two years I
6491
04:15:18,660 --> 04:15:20,580
think so this is the one you want to use
6492
04:15:20,580 --> 04:15:23,279
it's really good again you can read the
6493
04:15:23,279 --> 04:15:25,560
documentation if you want but I will
6494
04:15:25,560 --> 04:15:28,380
show you how this works so as usual we
6495
04:15:28,380 --> 04:15:31,080
open the command line and we go into our
6496
04:15:31,080 --> 04:15:33,540
front end command line here
6497
04:15:33,540 --> 04:15:36,660
stop the execution for a moment
6498
04:15:36,660 --> 04:15:39,300
and install react hook
6499
04:15:39,300 --> 04:15:41,840
form
6500
04:15:42,720 --> 04:15:45,540
then let's start our server again
6501
04:15:45,540 --> 04:15:47,460
close the terminal
6502
04:15:47,460 --> 04:15:49,380
and then let's finish our add note
6503
04:15:49,380 --> 04:15:52,560
dialog by using react hook form first of
6504
04:15:52,560 --> 04:15:55,319
all we add another property that we pass
6505
04:15:55,319 --> 04:15:57,479
to this form we also need a callback
6506
04:15:57,479 --> 04:15:59,699
when a note was saved successfully
6507
04:15:59,699 --> 04:16:01,680
because then we want to add it to the UI
6508
04:16:01,680 --> 04:16:03,779
we want to add it to our list here so we
6509
04:16:03,779 --> 04:16:06,060
add another callback called on node
6510
04:16:06,060 --> 04:16:07,800
saved
6511
04:16:07,800 --> 04:16:10,140
which is also a function but this time
6512
04:16:10,140 --> 04:16:12,120
the function takes an argument
6513
04:16:12,120 --> 04:16:14,399
so we write node colon which is the name
6514
04:16:14,399 --> 04:16:16,439
of the argument and the type of the
6515
04:16:16,439 --> 04:16:19,620
argument is node in uppercase
6516
04:16:19,620 --> 04:16:22,500
but it won't return anything
6517
04:16:22,500 --> 04:16:24,420
and then we add the second card back
6518
04:16:24,420 --> 04:16:27,960
here as the argument to our component
6519
04:16:27,960 --> 04:16:30,359
then to use react talk form and here we
6520
04:16:30,359 --> 04:16:32,460
go above the return statement and there
6521
04:16:32,460 --> 04:16:34,859
is a special hook we can use this hook
6522
04:16:34,859 --> 04:16:36,779
returns different kind of data and
6523
04:16:36,779 --> 04:16:38,640
functions that we then use in our form
6524
04:16:38,640 --> 04:16:41,279
so we have to destructure this single
6525
04:16:41,279 --> 04:16:44,340
barrier boots by creating a const adding
6526
04:16:44,340 --> 04:16:47,040
a pair of curly braces and then we list
6527
04:16:47,040 --> 04:16:49,500
them in here before we do that let's go
6528
04:16:49,500 --> 04:16:52,500
behind it and write use form which is
6529
04:16:52,500 --> 04:16:54,420
the name of the hook so it adds another
6530
04:16:54,420 --> 04:16:57,060
import statement
6531
04:16:57,060 --> 04:16:59,640
and we have to give this a type which is
6532
04:16:59,640 --> 04:17:02,340
the input type of our form here and this
6533
04:17:02,340 --> 04:17:04,500
is why I created this node input
6534
04:17:04,500 --> 04:17:07,859
interface in our nodes API file earlier
6535
04:17:07,859 --> 04:17:10,319
and exported it there because now we can
6536
04:17:10,319 --> 04:17:12,600
reuse it here and this is a functions
6537
04:17:12,600 --> 04:17:14,640
that we have to call it
6538
04:17:14,640 --> 04:17:16,500
and now we should have Auto completion
6539
04:17:16,500 --> 04:17:19,260
in here I think so yeah the first one is
6540
04:17:19,260 --> 04:17:21,600
called register you will see what we use
6541
04:17:21,600 --> 04:17:23,880
this for in a moment the second we need
6542
04:17:23,880 --> 04:17:25,800
is handle submit
6543
04:17:25,800 --> 04:17:28,620
and then we need the form state which we
6544
04:17:28,620 --> 04:17:31,800
want to further destructure which we do
6545
04:17:31,800 --> 04:17:33,840
like this colon another pair of curly
6546
04:17:33,840 --> 04:17:36,239
braces this just is the beauty of
6547
04:17:36,239 --> 04:17:38,040
JavaScript syntax
6548
04:17:38,040 --> 04:17:39,859
and in here we have an arrows field
6549
04:17:39,859 --> 04:17:43,800
comma and this is submitting callback
6550
04:17:43,800 --> 04:17:45,840
this one
6551
04:17:45,840 --> 04:17:48,239
okay that's a lot of stuff to add but
6552
04:17:48,239 --> 04:17:51,000
just type it out like I did here
6553
04:17:51,000 --> 04:17:53,279
then below we create a function that
6554
04:17:53,279 --> 04:17:55,500
actually handles submitting a node and
6555
04:17:55,500 --> 04:17:58,140
calling our API endpoint
6556
04:17:58,140 --> 04:18:00,479
so it's an async function because we do
6557
04:18:00,479 --> 04:18:02,880
our async request and we call it on
6558
04:18:02,880 --> 04:18:04,080
submit
6559
04:18:04,080 --> 04:18:05,580
which is a naming convention for
6560
04:18:05,580 --> 04:18:07,800
whenever you do something in response to
6561
04:18:07,800 --> 04:18:10,260
submitting a form you can also call this
6562
04:18:10,260 --> 04:18:12,479
whatever you want
6563
04:18:12,479 --> 04:18:15,720
it will take input in form of our node
6564
04:18:15,720 --> 04:18:18,000
input type
6565
04:18:18,000 --> 04:18:19,920
then we add a try catch block here
6566
04:18:19,920 --> 04:18:22,199
because we do our back end request which
6567
04:18:22,199 --> 04:18:23,880
can always go wrong
6568
04:18:23,880 --> 04:18:26,399
we lock the arrow
6569
04:18:26,399 --> 04:18:29,819
in case there is one and we use another
6570
04:18:29,819 --> 04:18:31,199
alert
6571
04:18:31,199 --> 04:18:33,359
now in a real app you can use a more
6572
04:18:33,359 --> 04:18:35,460
beautiful UI elements than an alert
6573
04:18:35,460 --> 04:18:37,979
dialog because it's a bit disruptive and
6574
04:18:37,979 --> 04:18:39,479
we will actually do this later in a
6575
04:18:39,479 --> 04:18:41,580
different place but for this here this
6576
04:18:41,580 --> 04:18:43,859
is sufficient okay when we create a note
6577
04:18:43,859 --> 04:18:46,500
we will get the created node back as a
6578
04:18:46,500 --> 04:18:49,920
response so we create a const
6579
04:18:49,920 --> 04:18:52,920
colored note response
6580
04:18:52,920 --> 04:18:56,340
a weight notes API which of course we
6581
04:18:56,340 --> 04:18:58,260
have to import here
6582
04:18:58,260 --> 04:19:04,020
import Star as nodes API from
6583
04:19:04,020 --> 04:19:07,140
dot dot Network this is just write an
6584
04:19:07,140 --> 04:19:09,840
error where where the location of the
6585
04:19:09,840 --> 04:19:12,180
package itself is we already have one up
6586
04:19:12,180 --> 04:19:14,100
here but with the syntax we can't
6587
04:19:14,100 --> 04:19:16,140
combine the two we can't cut this out
6588
04:19:16,140 --> 04:19:17,880
and put it in front of these curly
6589
04:19:17,880 --> 04:19:18,960
braces
6590
04:19:18,960 --> 04:19:22,620
okay so await notes API dot create node
6591
04:19:22,620 --> 04:19:25,260
where we forward the input
6592
04:19:25,260 --> 04:19:28,439
which is the title and the text
6593
04:19:28,439 --> 04:19:32,100
okay so on and if this succeeded we want
6594
04:19:32,100 --> 04:19:35,460
to cut our unknown saved callback
6595
04:19:35,460 --> 04:19:38,100
and paste the note response
6596
04:19:38,100 --> 04:19:41,040
toward the color of this component which
6597
04:19:41,040 --> 04:19:43,260
right now is the ftsx file this
6598
04:19:43,260 --> 04:19:45,540
represents the successfully created
6599
04:19:45,540 --> 04:19:47,640
nodes there and we can add it to the UI
6600
04:19:47,640 --> 04:19:50,220
the next step is to go into our form tag
6601
04:19:50,220 --> 04:19:52,319
here again and add a second property to
6602
04:19:52,319 --> 04:19:53,340
it
6603
04:19:53,340 --> 04:19:55,920
which will be on submit as the name
6604
04:19:55,920 --> 04:19:58,020
implies this is called when this form is
6605
04:19:58,020 --> 04:20:00,180
submitted which happens when we press
6606
04:20:00,180 --> 04:20:02,520
our submit button because it's connected
6607
04:20:02,520 --> 04:20:05,699
to the form right and in here we want to
6608
04:20:05,699 --> 04:20:07,620
use this handle submit function that we
6609
04:20:07,620 --> 04:20:10,260
get from the use form hook sounds a bit
6610
04:20:10,260 --> 04:20:12,060
complicated at first but those are just
6611
04:20:12,060 --> 04:20:13,920
steps you have to learn because it works
6612
04:20:13,920 --> 04:20:16,260
the same way every single time
6613
04:20:16,260 --> 04:20:19,560
so in here we want to call handle submit
6614
04:20:19,560 --> 04:20:21,840
and pass an argument to it which is our
6615
04:20:21,840 --> 04:20:24,479
own on submit function so this is a bit
6616
04:20:24,479 --> 04:20:27,239
weird syntax but this connects the form
6617
04:20:27,239 --> 04:20:30,479
to the react hook form package so on
6618
04:20:30,479 --> 04:20:32,880
submit is the Callback when this form is
6619
04:20:32,880 --> 04:20:35,939
submitted entry pass handle submit which
6620
04:20:35,939 --> 04:20:38,520
hence the inputs to a react hook form
6621
04:20:38,520 --> 04:20:41,340
first which then does some stuff behind
6622
04:20:41,340 --> 04:20:43,739
the scenes and cuts hour on submit
6623
04:20:43,739 --> 04:20:45,479
function which executes this part here
6624
04:20:45,479 --> 04:20:48,060
so pay attention to the syntax seal even
6625
04:20:48,060 --> 04:20:49,979
though we have parentheses and calders
6626
04:20:49,979 --> 04:20:52,319
this is not an error function so we do
6627
04:20:52,319 --> 04:20:54,840
not write it like this we write it like
6628
04:20:54,840 --> 04:20:57,239
this why because this gets called at
6629
04:20:57,239 --> 04:20:59,399
initialization not when we click submit
6630
04:20:59,399 --> 04:21:01,500
I guess it creates some return value
6631
04:21:01,500 --> 04:21:03,660
that it then uses in here I don't know
6632
04:21:03,660 --> 04:21:05,220
how this exactly Works under the hood
6633
04:21:05,220 --> 04:21:07,020
but this is the syntax you have to use
6634
04:21:07,020 --> 04:21:10,260
okay and the way we connect each form
6635
04:21:10,260 --> 04:21:13,020
input to a react hook form looks also a
6636
04:21:13,020 --> 04:21:15,120
bit weird but again this is how it's
6637
04:21:15,120 --> 04:21:17,100
supposed to be done we add a pair of
6638
04:21:17,100 --> 04:21:19,260
curly braces here inside this form
6639
04:21:19,260 --> 04:21:22,220
control component then we do a three
6640
04:21:22,220 --> 04:21:25,260
dots which means that whatever comes
6641
04:21:25,260 --> 04:21:27,239
after it gets destructured into its
6642
04:21:27,239 --> 04:21:28,560
single components
6643
04:21:28,560 --> 04:21:31,260
so we add one thing here which is this
6644
04:21:31,260 --> 04:21:33,840
register car but this basically gets
6645
04:21:33,840 --> 04:21:35,819
separated into a many different
6646
04:21:35,819 --> 04:21:38,460
properties that I get added to this
6647
04:21:38,460 --> 04:21:40,500
component to the phone control this is
6648
04:21:40,500 --> 04:21:42,359
what the syntax here means it takes
6649
04:21:42,359 --> 04:21:44,760
register and it separates them into its
6650
04:21:44,760 --> 04:21:46,620
single pieces basically
6651
04:21:46,620 --> 04:21:48,620
register is a function that takes
6652
04:21:48,620 --> 04:21:51,660
arguments the first argument is the name
6653
04:21:51,660 --> 04:21:54,300
of this input field which has to be one
6654
04:21:54,300 --> 04:21:56,819
of the properties that are contained in
6655
04:21:56,819 --> 04:21:59,399
our node input here that we used in use
6656
04:21:59,399 --> 04:22:02,040
form so node input contains a title and
6657
04:22:02,040 --> 04:22:04,020
a text and auto completion already tells
6658
04:22:04,020 --> 04:22:05,520
us that we have to use one of them down
6659
04:22:05,520 --> 04:22:07,680
here because this is what connects this
6660
04:22:07,680 --> 04:22:10,680
input field to this value and react hook
6661
04:22:10,680 --> 04:22:12,300
form later knows that when we submit
6662
04:22:12,300 --> 04:22:14,760
this this is the tidal value that it
6663
04:22:14,760 --> 04:22:17,939
will send to our onsubmit function here
6664
04:22:17,939 --> 04:22:19,920
again it's a bit complicated at first
6665
04:22:19,920 --> 04:22:21,899
but this is how HTML forms work and
6666
04:22:21,899 --> 04:22:24,180
react the second argument to register
6667
04:22:24,180 --> 04:22:26,220
after the string is some configuration
6668
04:22:26,220 --> 04:22:28,439
we can pass in form of a JavaScript
6669
04:22:28,439 --> 04:22:31,199
object so we add a pair of curly braces
6670
04:22:31,199 --> 04:22:33,540
in here you can for example set up form
6671
04:22:33,540 --> 04:22:35,640
validation for example you can use this
6672
04:22:35,640 --> 04:22:37,199
required key
6673
04:22:37,199 --> 04:22:39,600
and pass a string here for example
6674
04:22:39,600 --> 04:22:41,939
required the string is what will later
6675
04:22:41,939 --> 04:22:44,399
be shown in the UI if we try to submit
6676
04:22:44,399 --> 04:22:46,439
this without input by adding this
6677
04:22:46,439 --> 04:22:49,020
required property in our form control
6678
04:22:49,020 --> 04:22:51,660
for the text we won't use required
6679
04:22:51,660 --> 04:22:53,760
because this one is optional
6680
04:22:53,760 --> 04:22:56,939
so here again we are at register like
6681
04:22:56,939 --> 04:22:59,460
this and just pass the name which this
6682
04:22:59,460 --> 04:23:01,020
time is the text
6683
04:23:01,020 --> 04:23:03,660
okay but for our required field we also
6684
04:23:03,660 --> 04:23:05,460
want to have some feedback right we want
6685
04:23:05,460 --> 04:23:07,500
to have this little red text below the
6686
04:23:07,500 --> 04:23:09,479
input field that prints this required
6687
04:23:09,479 --> 04:23:11,760
text so inside the form group we add
6688
04:23:11,760 --> 04:23:14,540
another component
6689
04:23:14,699 --> 04:23:18,660
which is called form.control.feetback
6690
04:23:20,580 --> 04:23:23,760
and this gets a property as input
6691
04:23:23,760 --> 04:23:26,699
which we said to invalid
6692
04:23:26,699 --> 04:23:29,100
there are two types of feedback valid
6693
04:23:29,100 --> 04:23:30,899
one which is the screen text when
6694
04:23:30,899 --> 04:23:32,819
something when successful and invalid
6695
04:23:32,819 --> 04:23:34,979
which is this red text and in here we
6696
04:23:34,979 --> 04:23:36,720
want to get the error message where do
6697
04:23:36,720 --> 04:23:38,880
we get it from we get it from this
6698
04:23:38,880 --> 04:23:41,939
errors property of use form
6699
04:23:41,939 --> 04:23:43,739
so to use a variable in here we add
6700
04:23:43,739 --> 04:23:45,779
another pair of curly braces
6701
04:23:45,779 --> 04:23:48,840
and we pass arrows Dot and we get Auto
6702
04:23:48,840 --> 04:23:52,199
completion for our two input fields
6703
04:23:52,199 --> 04:23:54,300
title
6704
04:23:54,300 --> 04:23:57,840
and this error is undefined if there is
6705
04:23:57,840 --> 04:23:59,880
no error so if you didn't try to submit
6706
04:23:59,880 --> 04:24:02,399
empty input so we have to add the save
6707
04:24:02,399 --> 04:24:04,739
call Operator here which is this
6708
04:24:04,739 --> 04:24:06,300
question mark and then the dots behind
6709
04:24:06,300 --> 04:24:09,479
it which means that we will use this
6710
04:24:09,479 --> 04:24:11,460
message where you hear only if title is
6711
04:24:11,460 --> 04:24:13,620
not undefined or null and otherwise this
6712
04:24:13,620 --> 04:24:16,380
will return undefined and we use this
6713
04:24:16,380 --> 04:24:18,000
message property here which contains the
6714
04:24:18,000 --> 04:24:20,040
error message itself which is this piece
6715
04:24:20,040 --> 04:24:21,479
here
6716
04:24:21,479 --> 04:24:24,359
okay but we only want to show this input
6717
04:24:24,359 --> 04:24:26,819
if there's an actual error for the title
6718
04:24:26,819 --> 04:24:28,680
right we handle this with another
6719
04:24:28,680 --> 04:24:31,739
property on the form control
6720
04:24:31,739 --> 04:24:34,439
this one is cut is invalid and we have
6721
04:24:34,439 --> 04:24:36,300
to set this to a Boolean
6722
04:24:36,300 --> 04:24:38,460
we want to show the error down here only
6723
04:24:38,460 --> 04:24:40,859
if there is an error for the title right
6724
04:24:40,859 --> 04:24:43,859
remember this is possibly undefined only
6725
04:24:43,859 --> 04:24:45,479
if it's there we want to show this piece
6726
04:24:45,479 --> 04:24:48,120
down here so what we do is we write two
6727
04:24:48,120 --> 04:24:50,479
exclamation marks which takes a value
6728
04:24:50,479 --> 04:24:53,279
like our Title Here which is of type
6729
04:24:53,279 --> 04:24:55,920
field error or undefined and turns it
6730
04:24:55,920 --> 04:24:58,380
into a Boolean true or false so when we
6731
04:24:58,380 --> 04:25:00,540
write arrows.title
6732
04:25:00,540 --> 04:25:03,120
if the title arrow is undefined this
6733
04:25:03,120 --> 04:25:05,760
will resolve to false if the error title
6734
04:25:05,760 --> 04:25:07,620
contains a value this will be resolve to
6735
04:25:07,620 --> 04:25:09,420
true this is why we use this for this
6736
04:25:09,420 --> 04:25:11,939
invalid property here so invalid will be
6737
04:25:11,939 --> 04:25:13,620
true if there is an error for the title
6738
04:25:13,620 --> 04:25:16,199
and if not it will resolve to false and
6739
04:25:16,199 --> 04:25:18,960
it will hide this piece down here react
6740
04:25:18,960 --> 04:25:21,180
hook form can also do more sophisticated
6741
04:25:21,180 --> 04:25:23,279
validation than just this it can also
6742
04:25:23,279 --> 04:25:24,960
recognize certain patterns like email
6743
04:25:24,960 --> 04:25:27,000
addresses we will not use that here you
6744
04:25:27,000 --> 04:25:28,560
can read the documentation if you want
6745
04:25:28,560 --> 04:25:30,540
to get more into it but I just also want
6746
04:25:30,540 --> 04:25:32,760
to note that front-end validation does
6747
04:25:32,760 --> 04:25:34,800
not replace backend validation because
6748
04:25:34,800 --> 04:25:37,020
whatever we enforce on the front end can
6749
04:25:37,020 --> 04:25:39,239
always be circumvented for example by
6750
04:25:39,239 --> 04:25:41,819
sending a request with Postman to the
6751
04:25:41,819 --> 04:25:44,100
back end instead of using our neat forms
6752
04:25:44,100 --> 04:25:45,779
here this is why it's important to have
6753
04:25:45,779 --> 04:25:48,239
the validation on the back end as well
6754
04:25:48,239 --> 04:25:51,060
like we have in our notes controller and
6755
04:25:51,060 --> 04:25:52,620
all these different places
6756
04:25:52,620 --> 04:25:55,140
where we check if the required fields
6757
04:25:55,140 --> 04:25:56,819
are actually present
6758
04:25:56,819 --> 04:25:58,680
so this is just for better user
6759
04:25:58,680 --> 04:26:01,380
experience but whenever something is
6760
04:26:01,380 --> 04:26:03,479
important and has to be taken care of
6761
04:26:03,479 --> 04:26:04,920
then you have to do it in the back end
6762
04:26:04,920 --> 04:26:07,080
also this is pretty where both you will
6763
04:26:07,080 --> 04:26:09,720
later extract this stuff into reusable
6764
04:26:09,720 --> 04:26:11,279
foam components so we don't have to
6765
04:26:11,279 --> 04:26:13,199
repeat this over and over again but for
6766
04:26:13,199 --> 04:26:15,359
now this is fine okay and there's one
6767
04:26:15,359 --> 04:26:16,920
more property up here that we haven't
6768
04:26:16,920 --> 04:26:19,199
used yet and that's this is submitting
6769
04:26:19,199 --> 04:26:20,699
Boolean
6770
04:26:20,699 --> 04:26:22,560
I want to use this in the button down
6771
04:26:22,560 --> 04:26:24,840
here I want to disable the button as
6772
04:26:24,840 --> 04:26:26,760
long as the form is submitting so that
6773
04:26:26,760 --> 04:26:28,680
we can't click it tries right we don't
6774
04:26:28,680 --> 04:26:30,000
want to submit the phone twice
6775
04:26:30,000 --> 04:26:31,500
accidentally
6776
04:26:31,500 --> 04:26:33,899
so we can add a disabled attribute to
6777
04:26:33,899 --> 04:26:35,399
this button
6778
04:26:35,399 --> 04:26:38,399
and set it to this is submitting Boolean
6779
04:26:38,399 --> 04:26:40,680
so as long as the form is submitting
6780
04:26:40,680 --> 04:26:42,779
this button will be disabled okay let's
6781
04:26:42,779 --> 04:26:44,640
save everything
6782
04:26:44,640 --> 04:26:47,399
but now let's try it out and we still
6783
04:26:47,399 --> 04:26:50,040
have an arrow here because we haven't
6784
04:26:50,040 --> 04:26:52,380
passed the unknown saved callback here
6785
04:26:52,380 --> 04:26:54,779
yet let's do that now
6786
04:26:54,779 --> 04:26:57,120
but let's keep it empty for now I just
6787
04:26:57,120 --> 04:26:58,979
want to try it out quickly and then
6788
04:26:58,979 --> 04:27:01,140
we'll take care of this so we pass an
6789
04:27:01,140 --> 04:27:03,479
empty function here
6790
04:27:03,479 --> 04:27:05,880
save this
6791
04:27:05,880 --> 04:27:08,100
and now beer should be able to send a
6792
04:27:08,100 --> 04:27:11,840
new note to our back end so note from
6793
04:27:11,840 --> 04:27:15,479
react front end
6794
04:27:15,479 --> 04:27:18,060
this is amazing
6795
04:27:18,060 --> 04:27:20,580
let's try it out when we send this the
6796
04:27:20,580 --> 04:27:22,560
dialogue will stay opened because we
6797
04:27:22,560 --> 04:27:24,659
haven't taken care of closing it yet but
6798
04:27:24,659 --> 04:27:26,640
the save button should be a great out
6799
04:27:26,640 --> 04:27:28,739
and disabled for a short moment it will
6800
04:27:28,739 --> 04:27:30,659
be very fast because we are on localhost
6801
04:27:30,659 --> 04:27:32,340
and we don't have to send the data
6802
04:27:32,340 --> 04:27:33,779
through the internet but it might be
6803
04:27:33,779 --> 04:27:36,659
visible for a short moment and yeah it
6804
04:27:36,659 --> 04:27:39,540
was visible for a very short moment
6805
04:27:39,540 --> 04:27:41,399
so let's take a look into our database
6806
04:27:41,399 --> 04:27:45,439
and let's see if the new node is still
6807
04:27:48,600 --> 04:27:51,720
and there it is not from react front end
6808
04:27:51,720 --> 04:27:54,420
really nice so this is our first note we
6809
04:27:54,420 --> 04:27:57,120
created through our own website instead
6810
04:27:57,120 --> 04:27:59,580
of Postman now let's finish up the
6811
04:27:59,580 --> 04:28:02,040
dialog let's hide the dialog after we
6812
04:28:02,040 --> 04:28:03,659
have sent the note and let's also add a
6813
04:28:03,659 --> 04:28:05,939
note to the UI here because right now we
6814
04:28:05,939 --> 04:28:08,159
have to refresh the page before we
6815
04:28:08,159 --> 04:28:10,439
sealed and actually just realized that I
6816
04:28:10,439 --> 04:28:12,300
wouldn't have had to look into the
6817
04:28:12,300 --> 04:28:14,520
mongodb database because we can see it
6818
04:28:14,520 --> 04:28:16,439
right here but now we want to make it so
6819
04:28:16,439 --> 04:28:18,779
that the node appears immediately and we
6820
04:28:18,779 --> 04:28:21,420
don't have to refresh the page first so
6821
04:28:21,420 --> 04:28:23,699
the first thing we do here
6822
04:28:23,699 --> 04:28:26,220
inside the skull back
6823
04:28:26,220 --> 04:28:30,540
is we set show add notes dialog back to
6824
04:28:30,540 --> 04:28:33,300
false so the dialog disappears and we
6825
04:28:33,300 --> 04:28:35,340
also want to add this node to the UI
6826
04:28:35,340 --> 04:28:37,080
this is why we have this function block
6827
04:28:37,080 --> 04:28:38,880
instead of just one line we want to add
6828
04:28:38,880 --> 04:28:41,340
more stuff here we already know how we
6829
04:28:41,340 --> 04:28:43,260
update our state right we have to use
6830
04:28:43,260 --> 04:28:45,239
the set node scale here
6831
04:28:45,239 --> 04:28:47,760
and update it with the newer nodes array
6832
04:28:47,760 --> 04:28:49,620
but we don't get the whole array sent
6833
04:28:49,620 --> 04:28:51,479
back from our backend we just get our
6834
04:28:51,479 --> 04:28:53,880
new node so what we do is we call that
6835
04:28:53,880 --> 04:28:54,779
nodes
6836
04:28:54,779 --> 04:28:57,359
we pass an array here with scrap
6837
04:28:57,359 --> 04:28:59,819
brackets and then we write dot dot dot
6838
04:28:59,819 --> 04:29:01,080
nodes
6839
04:29:01,080 --> 04:29:03,479
what this does is it creates a new array
6840
04:29:03,479 --> 04:29:05,220
and it adds the nodes that are already
6841
04:29:05,220 --> 04:29:08,640
in our node State into this array so all
6842
04:29:08,640 --> 04:29:10,439
the single nodes that we already have on
6843
04:29:10,439 --> 04:29:12,479
the screen are added to this new array
6844
04:29:12,479 --> 04:29:14,520
and then we want to add our new node to
6845
04:29:14,520 --> 04:29:17,580
it the new node is the argument that we
6846
04:29:17,580 --> 04:29:19,680
get passed from this one node saved
6847
04:29:19,680 --> 04:29:22,319
callback remember we declared it up here
6848
04:29:22,319 --> 04:29:24,840
and here when we call on our saved we
6849
04:29:24,840 --> 04:29:26,699
pass our node response
6850
04:29:26,699 --> 04:29:28,620
so we can add the name for the variable
6851
04:29:28,620 --> 04:29:29,580
here
6852
04:29:29,580 --> 04:29:32,340
we can give it any name we want and this
6853
04:29:32,340 --> 04:29:35,399
is the node model that we are pass when
6854
04:29:35,399 --> 04:29:38,040
we are called this function so after the
6855
04:29:38,040 --> 04:29:42,000
comma down here we pass our newer node
6856
04:29:42,000 --> 04:29:45,300
so to recap this creates a new array
6857
04:29:45,300 --> 04:29:47,580
it adds all the existing notes to it and
6858
04:29:47,580 --> 04:29:49,680
it also adds the new node to it which
6859
04:29:49,680 --> 04:29:51,659
will then be displayed in the UI
6860
04:29:51,659 --> 04:29:55,080
Zoom let's make this bigger and let's
6861
04:29:55,080 --> 04:29:56,819
try adding another note
6862
04:29:56,819 --> 04:30:01,920
this is a newer react node blah blah
6863
04:30:01,920 --> 04:30:03,779
when we save this the dialog should
6864
04:30:03,779 --> 04:30:06,000
close and we should see our new node on
6865
04:30:06,000 --> 04:30:07,739
the screen but first of all let's try
6866
04:30:07,739 --> 04:30:10,140
the input validation so when we try to
6867
04:30:10,140 --> 04:30:12,420
save this we see our required text when
6868
04:30:12,420 --> 04:30:14,279
we type in something the required text
6869
04:30:14,279 --> 04:30:16,920
disappears this is really cool
6870
04:30:16,920 --> 04:30:18,659
it's actually try sending a note without
6871
04:30:18,659 --> 04:30:20,580
the text this should work
6872
04:30:20,580 --> 04:30:23,520
and we save it and it is added to the UI
6873
04:30:23,520 --> 04:30:25,800
nice or just one more thing I want to
6874
04:30:25,800 --> 04:30:28,020
show you what I mentioned earlier
6875
04:30:28,020 --> 04:30:29,760
when I type something in here and I
6876
04:30:29,760 --> 04:30:31,560
close the dialog
6877
04:30:31,560 --> 04:30:33,720
and open it again the next time the
6878
04:30:33,720 --> 04:30:35,699
input is gone this is by Design This is
6879
04:30:35,699 --> 04:30:38,399
how I wanted it this is why I handle the
6880
04:30:38,399 --> 04:30:40,380
dialog like this as I explained earlier
6881
04:30:40,380 --> 04:30:42,720
if you want to keep the input you can
6882
04:30:42,720 --> 04:30:44,640
use the show property here like I
6883
04:30:44,640 --> 04:30:47,279
explained earlier and now I wanna add
6884
04:30:47,279 --> 04:30:49,260
some margin to the bottom of this button
6885
04:30:49,260 --> 04:30:52,380
and Center it on the screen
6886
04:30:52,380 --> 04:30:55,439
so we add a class name to the button and
6887
04:30:55,439 --> 04:30:58,140
again we have this class that we can use
6888
04:30:58,140 --> 04:31:01,260
from bootstrap which is called mb4 which
6889
04:31:01,260 --> 04:31:03,000
adds a little bit of margin here at the
6890
04:31:03,000 --> 04:31:03,960
bottom
6891
04:31:03,960 --> 04:31:05,880
but we still want to send that is on the
6892
04:31:05,880 --> 04:31:08,040
screen so now we could add the class to
6893
04:31:08,040 --> 04:31:10,979
this button and put the CSS code in the
6894
04:31:10,979 --> 04:31:13,800
notes page module CSS file but for a
6895
04:31:13,800 --> 04:31:16,020
certain kind of CSS attributes that I
6896
04:31:16,020 --> 04:31:18,180
use over and over again I like to create
6897
04:31:18,180 --> 04:31:20,399
utility classes that I put into a
6898
04:31:20,399 --> 04:31:23,040
separate CSS file so let's do that now
6899
04:31:23,040 --> 04:31:25,500
let's open the sites bar and add another
6900
04:31:25,500 --> 04:31:28,800
file to the Styles folder
6901
04:31:28,800 --> 04:31:32,520
which we call utils.module.css
6902
04:31:32,520 --> 04:31:36,359
this contains a reusable utility classes
6903
04:31:36,359 --> 04:31:38,580
and I'm gonna cut this one block Center
6904
04:31:38,580 --> 04:31:40,260
I've actually not seen this name
6905
04:31:40,260 --> 04:31:42,479
anywhere else used but it makes sense to
6906
04:31:42,479 --> 04:31:44,220
me because here I want to use two
6907
04:31:44,220 --> 04:31:47,699
attributes display block and
6908
04:31:47,699 --> 04:31:51,720
March and left Auto
6909
04:31:51,720 --> 04:31:55,260
and margin right Auto
6910
04:31:55,260 --> 04:31:57,300
these attributes together will display
6911
04:31:57,300 --> 04:31:59,939
whatever we use this class on yeah in a
6912
04:31:59,939 --> 04:32:01,800
way that it is centered on the screen
6913
04:32:01,800 --> 04:32:04,800
because margin Auto does this it sets
6914
04:32:04,800 --> 04:32:06,600
the margins ordered as an equal amount
6915
04:32:06,600 --> 04:32:10,199
of either side so let's save this copy
6916
04:32:10,199 --> 04:32:12,420
the class name and use it in our app TSX
6917
04:32:12,420 --> 04:32:14,520
file
6918
04:32:14,520 --> 04:32:17,340
so let's duplicate this line and we can
6919
04:32:17,340 --> 04:32:19,500
do this with shift ielt and pressing the
6920
04:32:19,500 --> 04:32:22,800
down key or just type it out again
6921
04:32:22,800 --> 04:32:25,800
we call this style utils and we import
6922
04:32:25,800 --> 04:32:29,939
this due to its module.css
6923
04:32:30,659 --> 04:32:33,600
and then we can use it on our button
6924
04:32:33,600 --> 04:32:35,580
now we do the same trick as before since
6925
04:32:35,580 --> 04:32:37,920
we already use this bootstrap class we
6926
04:32:37,920 --> 04:32:40,199
replace this for a pair of curly braces
6927
04:32:40,199 --> 04:32:44,220
a string with back ticks use mb4 in here
6928
04:32:44,220 --> 04:32:49,319
and add our other class as a variable so
6929
04:32:49,319 --> 04:32:51,359
style your tits
6930
04:32:51,359 --> 04:32:54,359
block Center which now centers this
6931
04:32:54,359 --> 04:32:56,279
button on the screen and you can reuse
6932
04:32:56,279 --> 04:32:58,140
this block Center class in other places
6933
04:32:58,140 --> 04:33:00,659
if you want so the layout is still not
6934
04:33:00,659 --> 04:33:02,460
finished completely we still need some
6935
04:33:02,460 --> 04:33:04,080
room here at the top but we will do this
6936
04:33:04,080 --> 04:33:06,599
later when we add our navbar and also we
6937
04:33:06,599 --> 04:33:09,061
don't have any loading or error or empty
6938
04:33:09,061 --> 04:33:10,859
States yet so right now when we load
6939
04:33:10,859 --> 04:33:12,599
nodes from the back end it's very fast
6940
04:33:12,599 --> 04:33:14,820
because we are on localhost but when we
6941
04:33:14,820 --> 04:33:16,740
do it over the actual internet it may
6942
04:33:16,740 --> 04:33:18,900
take half a second or so that's why we
6943
04:33:18,900 --> 04:33:20,759
have to add some kind of progress
6944
04:33:20,759 --> 04:33:22,799
spinner while the nodes are loading we
6945
04:33:22,799 --> 04:33:24,480
also want to add an error state if the
6946
04:33:24,480 --> 04:33:26,458
nodes can't be loaded we don't just want
6947
04:33:26,458 --> 04:33:28,561
to show the alert dialog we want to show
6948
04:33:28,561 --> 04:33:30,480
something in the UI and it also makes
6949
04:33:30,480 --> 04:33:32,820
sense to have a special kind of text or
6950
04:33:32,820 --> 04:33:35,340
message that shows when there are no
6951
04:33:35,340 --> 04:33:37,561
nodes in our database so that the screen
6952
04:33:37,561 --> 04:33:39,599
isn't just empty it should say you
6953
04:33:39,599 --> 04:33:41,160
haven't added any notes yet or something
6954
04:33:41,160 --> 04:33:42,900
like that we will implement this in the
6955
04:33:42,900 --> 04:33:44,820
rest of the video but the next step is
6956
04:33:44,820 --> 04:33:46,980
to add a way to update existing nodes
6957
04:33:46,980 --> 04:33:48,958
and delete them so that's what we will
6958
04:33:48,958 --> 04:33:51,378
do next
6959
04:33:54,958 --> 04:33:57,480
okay let's add a way to delete nodes
6960
04:33:57,480 --> 04:33:59,641
next for this we need another function
6961
04:33:59,641 --> 04:34:02,699
in our notes API file
6962
04:34:02,699 --> 04:34:05,278
so let's put it here at the bottom
6963
04:34:05,278 --> 04:34:08,219
export async function
6964
04:34:08,219 --> 04:34:11,340
we colored delete node
6965
04:34:11,340 --> 04:34:13,438
and it will take an argument which is
6966
04:34:13,438 --> 04:34:16,919
the ID of the node we want to delete
6967
04:34:16,919 --> 04:34:22,919
and in here recall a weight fetch data
6968
04:34:22,919 --> 04:34:26,879
the delete endpoint is slash API slash
6969
04:34:26,879 --> 04:34:30,180
notes slash and then we want to append
6970
04:34:30,180 --> 04:34:32,160
the node idea at the end of it right
6971
04:34:32,160 --> 04:34:35,160
because when we look into our nodes
6972
04:34:35,160 --> 04:34:37,561
routes this is how we set up this delete
6973
04:34:37,561 --> 04:34:39,660
endpoint
6974
04:34:39,660 --> 04:34:42,359
so we can simply add a plus here
6975
04:34:42,359 --> 04:34:45,000
and append the node idea which is a
6976
04:34:45,000 --> 04:34:47,099
string anyway we don't have to work URL
6977
04:34:47,099 --> 04:34:49,141
encoded in a special way we can just
6978
04:34:49,141 --> 04:34:51,180
append it doesn't contain any special
6979
04:34:51,180 --> 04:34:53,039
characters
6980
04:34:53,039 --> 04:34:55,740
and then we add the configuration as
6981
04:34:55,740 --> 04:34:58,438
usual where we set the method
6982
04:34:58,438 --> 04:35:01,080
which this time is delete
6983
04:35:01,080 --> 04:35:03,599
this function won't return anything we
6984
04:35:03,599 --> 04:35:05,160
also don't get anything back from the
6985
04:35:05,160 --> 04:35:07,618
endpoint but we have to await this fetch
6986
04:35:07,618 --> 04:35:09,599
call otherwise arrows won't be
6987
04:35:09,599 --> 04:35:11,879
propagated to the outside so the weight
6988
04:35:11,879 --> 04:35:13,438
is important
6989
04:35:13,438 --> 04:35:15,480
to delete a node I want to add a little
6990
04:35:15,480 --> 04:35:18,958
a trash bin icon here to each node
6991
04:35:18,958 --> 04:35:21,359
and for that we can install this react
6992
04:35:21,359 --> 04:35:24,118
icons package
6993
04:35:24,118 --> 04:35:26,039
yeah which contains ideas different
6994
04:35:26,039 --> 04:35:27,900
icons here
6995
04:35:27,900 --> 04:35:30,419
from different other packages that you
6996
04:35:30,419 --> 04:35:32,219
can use
6997
04:35:32,219 --> 04:35:33,840
so let's add this
6998
04:35:33,840 --> 04:35:36,958
in our front-end terminal
6999
04:35:36,958 --> 04:35:39,778
we install react icons this dash dash
7000
04:35:39,778 --> 04:35:42,480
saved by the way is not necessary you
7001
04:35:42,480 --> 04:35:44,039
had to add this in the past but not
7002
04:35:44,039 --> 04:35:46,618
anymore so you can ignore this
7003
04:35:46,618 --> 04:35:49,799
okay we start this again
7004
04:35:49,799 --> 04:35:51,778
then we want to go into our node
7005
04:35:51,778 --> 04:35:54,299
component the nodes that we draw on the
7006
04:35:54,299 --> 04:35:56,879
screen so this one here the node TSX
7007
04:35:56,879 --> 04:35:57,958
file
7008
04:35:57,958 --> 04:36:00,061
and I want to add this icon here to the
7009
04:36:00,061 --> 04:36:02,219
title of the note so it's right next to
7010
04:36:02,219 --> 04:36:04,980
the title so what we can do is we can go
7011
04:36:04,980 --> 04:36:07,919
below the title value here but within
7012
04:36:07,919 --> 04:36:10,320
the card title tag and just add our icon
7013
04:36:10,320 --> 04:36:13,438
but we will have to import it manually
7014
04:36:13,438 --> 04:36:16,080
the IDE doesn't recognize it
7015
04:36:16,080 --> 04:36:18,118
so up here we write import
7016
04:36:18,118 --> 04:36:20,278
we restructure this
7017
04:36:20,278 --> 04:36:22,618
and write MD delete with the same
7018
04:36:22,618 --> 04:36:25,320
spelling the same casing
7019
04:36:25,320 --> 04:36:26,938
from
7020
04:36:26,938 --> 04:36:30,061
the react icons packet we just installed
7021
04:36:30,061 --> 04:36:33,299
slash MD MD is short for material design
7022
04:36:33,299 --> 04:36:35,160
by the way so this is the material
7023
04:36:35,160 --> 04:36:38,219
design delete icon by Google
7024
04:36:38,219 --> 04:36:40,740
and then we can use it down here open
7025
04:36:40,740 --> 04:36:43,320
the anchor bracket MD delete
7026
04:36:43,320 --> 04:36:46,740
let's close it see how it looks
7027
04:36:46,740 --> 04:36:48,958
might have to refresh this
7028
04:36:48,958 --> 04:36:51,599
okay we are not quite there yet
7029
04:36:51,599 --> 04:36:53,699
first of all we add the class to the
7030
04:36:53,699 --> 04:36:56,340
delete icon for which we use bootstrap
7031
04:36:56,340 --> 04:36:58,320
classes again the first one is text
7032
04:36:58,320 --> 04:37:00,840
muted we used this before this just
7033
04:37:00,840 --> 04:37:03,660
turns the color into this grayish color
7034
04:37:03,660 --> 04:37:06,359
which I think looks just a bit better
7035
04:37:06,359 --> 04:37:09,599
and then after a space we also add MS
7036
04:37:09,599 --> 04:37:12,240
Auto which adds the margin starts with
7037
04:37:12,240 --> 04:37:15,419
the value auto to the icons which should
7038
04:37:15,419 --> 04:37:17,400
move these icons to the right but only
7039
04:37:17,400 --> 04:37:19,561
if the container of the title actually
7040
04:37:19,561 --> 04:37:21,778
supports this so we have to add a class
7041
04:37:21,778 --> 04:37:24,419
to the card title here as well
7042
04:37:24,419 --> 04:37:26,458
class name
7043
04:37:26,458 --> 04:37:30,958
and we will use our utility styles again
7044
04:37:30,958 --> 04:37:33,719
which we have to import first
7045
04:37:33,719 --> 04:37:36,599
so Style utils
7046
04:37:36,599 --> 04:37:39,480
utils module.css
7047
04:37:39,480 --> 04:37:42,118
and we want to add a new one there
7048
04:37:42,118 --> 04:37:44,219
so let's go into the YouTube's module
7049
04:37:44,219 --> 04:37:46,320
CSS
7050
04:37:46,320 --> 04:37:49,320
and add another class which we call Flex
7051
04:37:49,320 --> 04:37:51,180
Center
7052
04:37:51,180 --> 04:37:53,219
which contains the following attributes
7053
04:37:53,219 --> 04:37:56,400
display Flex which is flexbox again you
7054
04:37:56,400 --> 04:37:57,778
can learn more about this through
7055
04:37:57,778 --> 04:37:59,400
Googling
7056
04:37:59,400 --> 04:38:02,539
a line item Center
7057
04:38:02,539 --> 04:38:05,820
justify content Center
7058
04:38:05,820 --> 04:38:09,719
and a gap of 4 pixels
7059
04:38:09,719 --> 04:38:12,920
type out all of this
7060
04:38:12,958 --> 04:38:15,480
let's copy the class name
7061
04:38:15,480 --> 04:38:17,641
and add it to our
7062
04:38:17,641 --> 04:38:22,219
node title here so the style
7063
04:38:22,219 --> 04:38:25,259
youtubes.flex Center which applies this
7064
04:38:25,259 --> 04:38:28,020
and this yeah send us the text and this
7065
04:38:28,020 --> 04:38:31,020
icon properly and because we added the
7066
04:38:31,020 --> 04:38:33,480
flexbox now this MS Auto here Works
7067
04:38:33,480 --> 04:38:35,699
which adds the spacing to the front of
7068
04:38:35,699 --> 04:38:37,320
the icon and moves it all the way to the
7069
04:38:37,320 --> 04:38:40,320
right looks much better doesn't it but
7070
04:38:40,320 --> 04:38:42,061
we actually want to do something when we
7071
04:38:42,061 --> 04:38:43,799
click this icon we want to delete our
7072
04:38:43,799 --> 04:38:46,080
node right so we can add an unclick
7073
04:38:46,080 --> 04:38:48,900
attribute toward the MD delete
7074
04:38:48,900 --> 04:38:51,599
component and here we pass a function
7075
04:38:51,599 --> 04:38:54,061
like this and we want to do a two things
7076
04:38:54,061 --> 04:38:56,699
we want to trigger a callback so that
7077
04:38:56,699 --> 04:38:59,458
whoever uses this node component notes
7078
04:38:59,458 --> 04:39:01,259
that a note was deleted with a certain
7079
04:39:01,259 --> 04:39:03,719
idea right so what we do is we go up
7080
04:39:03,719 --> 04:39:05,278
here to the interface
7081
04:39:05,278 --> 04:39:08,219
the note props and we add another color
7082
04:39:08,219 --> 04:39:11,520
back here let's call it on delete
7083
04:39:11,520 --> 04:39:14,520
note clicked
7084
04:39:14,520 --> 04:39:17,160
and here we will forward
7085
04:39:17,160 --> 04:39:19,680
the whole node that we clicked so that
7086
04:39:19,680 --> 04:39:21,480
the color can later decide what to do
7087
04:39:21,480 --> 04:39:23,340
with this information in our case we
7088
04:39:23,340 --> 04:39:25,259
will get the ID out of this node and
7089
04:39:25,259 --> 04:39:27,061
then the delete request to our backend
7090
04:39:27,061 --> 04:39:28,680
with this idea
7091
04:39:28,680 --> 04:39:31,859
and this function won't return anything
7092
04:39:31,859 --> 04:39:34,680
let's not forget to add it down here
7093
04:39:34,680 --> 04:39:37,020
to the parameters
7094
04:39:37,020 --> 04:39:39,240
and then we can color it in our on-click
7095
04:39:39,240 --> 04:39:40,980
card back here
7096
04:39:40,980 --> 04:39:43,980
on delete note clicked
7097
04:39:43,980 --> 04:39:46,740
where we pass the note what's the note
7098
04:39:46,740 --> 04:39:49,140
that's the note that we are passed
7099
04:39:49,140 --> 04:39:51,360
through the component itself so we pass
7100
04:39:51,360 --> 04:39:54,000
this back to the color of this node but
7101
04:39:54,000 --> 04:39:55,500
then I also want to add a second line
7102
04:39:55,500 --> 04:39:58,620
here later when we click a note we also
7103
04:39:58,620 --> 04:40:00,420
want to open it so that we can update it
7104
04:40:00,420 --> 04:40:02,940
by default our click on the delete icon
7105
04:40:02,940 --> 04:40:05,040
will go through so it will trigger both
7106
04:40:05,040 --> 04:40:07,260
the undelete node clicked but also our
7107
04:40:07,260 --> 04:40:09,360
other callback later to open the node
7108
04:40:09,360 --> 04:40:10,920
for this reason we want to add another
7109
04:40:10,920 --> 04:40:13,320
line that swallows this click so to
7110
04:40:13,320 --> 04:40:15,600
speak so on click actually can take an
7111
04:40:15,600 --> 04:40:16,500
argument
7112
04:40:16,500 --> 04:40:19,560
which is often just called ear it's this
7113
04:40:19,560 --> 04:40:21,718
mouse event from The Click we can Define
7114
04:40:21,718 --> 04:40:23,760
it in here or we can ignore it like we
7115
04:40:23,760 --> 04:40:25,500
have done before this time we want to
7116
04:40:25,500 --> 04:40:27,600
use it and we want to color something on
7117
04:40:27,600 --> 04:40:29,280
this e variable
7118
04:40:29,280 --> 04:40:31,320
stop propagation
7119
04:40:31,320 --> 04:40:33,240
which this allows this click to go
7120
04:40:33,240 --> 04:40:35,700
through so we use it upright here and
7121
04:40:35,700 --> 04:40:37,320
our other on click Handler below this
7122
04:40:37,320 --> 04:40:39,480
icon won't be called later again I found
7123
04:40:39,480 --> 04:40:41,160
this all through Googling I don't put
7124
04:40:41,160 --> 04:40:43,020
this information out of my behind I have
7125
04:40:43,020 --> 04:40:45,000
to figure this out myself so you just
7126
04:40:45,000 --> 04:40:47,160
Google something like don't let click
7127
04:40:47,160 --> 04:40:49,260
through react or something along those
7128
04:40:49,260 --> 04:40:51,180
lines and you will find these answers
7129
04:40:51,180 --> 04:40:54,120
okay let's save this
7130
04:40:54,120 --> 04:40:57,420
and go back to the ftsx file because now
7131
04:40:57,420 --> 04:40:59,400
we have to pass this new callback to our
7132
04:40:59,400 --> 04:41:01,920
node component here so let's put these
7133
04:41:01,920 --> 04:41:04,560
arguments here into a separate lines
7134
04:41:04,560 --> 04:41:08,218
and let's add this new card back below
7135
04:41:08,218 --> 04:41:11,400
on delete note clicked
7136
04:41:11,400 --> 04:41:13,320
and we could either handle it right here
7137
04:41:13,320 --> 04:41:15,120
but since we do an API calling
7138
04:41:15,120 --> 04:41:16,920
everything let's put it into a separate
7139
04:41:16,920 --> 04:41:18,120
function
7140
04:41:18,120 --> 04:41:21,180
which we will call delete node
7141
04:41:21,180 --> 04:41:23,160
we haven't created this function yet but
7142
04:41:23,160 --> 04:41:25,200
we will now do so above the return
7143
04:41:25,200 --> 04:41:26,638
statement
7144
04:41:26,638 --> 04:41:30,060
so we create an async function with the
7145
04:41:30,060 --> 04:41:31,740
same name
7146
04:41:31,740 --> 04:41:33,840
and it takes the note that we want to
7147
04:41:33,840 --> 04:41:36,620
delete as import
7148
04:41:37,440 --> 04:41:40,138
ant try catch Block in here
7149
04:41:40,138 --> 04:41:42,420
as usual if something goes wrong we
7150
04:41:42,420 --> 04:41:44,458
print the arrow to the console and we
7151
04:41:44,458 --> 04:41:45,360
are
7152
04:41:45,360 --> 04:41:47,700
open an alert dialog
7153
04:41:47,700 --> 04:41:49,680
and in the try block we want to call
7154
04:41:49,680 --> 04:41:51,480
await
7155
04:41:51,480 --> 04:41:53,760
notes API
7156
04:41:53,760 --> 04:41:55,980
our delete node function that we created
7157
04:41:55,980 --> 04:41:57,298
earlier
7158
04:41:57,298 --> 04:41:59,458
which expects the idea of the note we
7159
04:41:59,458 --> 04:42:01,980
want to delete so we pass note dot
7160
04:42:01,980 --> 04:42:04,320
underscore idea
7161
04:42:04,320 --> 04:42:06,480
if this doesn't throw an error we stay
7162
04:42:06,480 --> 04:42:08,638
in this try block so now we want to
7163
04:42:08,638 --> 04:42:10,260
remove the node that we just deleted
7164
04:42:10,260 --> 04:42:12,660
from the UI and this works similarly to
7165
04:42:12,660 --> 04:42:15,180
how we added a new node here with the
7166
04:42:15,180 --> 04:42:17,820
set nodes column just that we know when
7167
04:42:17,820 --> 04:42:19,980
to filter our nodes and remove the one
7168
04:42:19,980 --> 04:42:21,600
that we just deleted
7169
04:42:21,600 --> 04:42:24,000
so again we call Z nodes
7170
04:42:24,000 --> 04:42:27,540
this time we pass nodes dot filter
7171
04:42:27,540 --> 04:42:29,878
as the name implies this allows us to
7172
04:42:29,878 --> 04:42:31,860
take the nodes array and filter certain
7173
04:42:31,860 --> 04:42:34,680
nodes out of it filter takes a function
7174
04:42:34,680 --> 04:42:36,540
that takes an argument
7175
04:42:36,540 --> 04:42:38,280
we have to give this argument a name
7176
04:42:38,280 --> 04:42:40,500
let's call it existing node and then we
7177
04:42:40,500 --> 04:42:42,240
make such a right arrow this filter
7178
04:42:42,240 --> 04:42:44,160
function will go through each node of
7179
04:42:44,160 --> 04:42:46,378
this array and pass it to this callback
7180
04:42:46,378 --> 04:42:48,360
one by one and then we can decide what
7181
04:42:48,360 --> 04:42:49,980
we want to do with it and we have to
7182
04:42:49,980 --> 04:42:52,080
return a Boolean from this function true
7183
04:42:52,080 --> 04:42:54,480
if we want to keep this note element in
7184
04:42:54,480 --> 04:42:56,400
this array and false if we want to
7185
04:42:56,400 --> 04:42:58,638
remove it so what we do is we write
7186
04:42:58,638 --> 04:43:00,958
existing node
7187
04:43:00,958 --> 04:43:03,480
take the idea
7188
04:43:03,480 --> 04:43:06,360
and we compare to the node ID we just
7189
04:43:06,360 --> 04:43:08,340
deleted if we know that it's the same
7190
04:43:08,340 --> 04:43:10,440
idea then we want to remove it so we
7191
04:43:10,440 --> 04:43:12,780
write not equal to like this with an
7192
04:43:12,780 --> 04:43:15,840
exclamation mark and two equal signs
7193
04:43:15,840 --> 04:43:18,240
note which is the node that we just
7194
04:43:18,240 --> 04:43:19,320
deleted
7195
04:43:19,320 --> 04:43:22,160
dot idea
7196
04:43:22,440 --> 04:43:24,660
so to recap this deletes the node
7197
04:43:24,660 --> 04:43:27,000
through our delete endpoint and then if
7198
04:43:27,000 --> 04:43:28,980
it was successful it Fitters our
7199
04:43:28,980 --> 04:43:30,840
existing node and removes the one with
7200
04:43:30,840 --> 04:43:33,298
this ID we just deleted so that we
7201
04:43:33,298 --> 04:43:35,580
update our UI and reflect the newest
7202
04:43:35,580 --> 04:43:37,138
changes
7203
04:43:37,138 --> 04:43:39,600
so when we save this our preview here
7204
04:43:39,600 --> 04:43:41,940
compiles again and a short break now
7205
04:43:41,940 --> 04:43:44,280
right let's try it out let's delete a
7206
04:43:44,280 --> 04:43:46,620
node let's take not the last one but
7207
04:43:46,620 --> 04:43:49,860
this one here click delete and its gun
7208
04:43:49,860 --> 04:43:51,900
and the UI is updated
7209
04:43:51,900 --> 04:43:54,360
and it should also now be deleted from
7210
04:43:54,360 --> 04:43:56,040
our database
7211
04:43:56,040 --> 04:43:58,260
so let's look into the atlas backend
7212
04:43:58,260 --> 04:44:00,000
refresh this
7213
04:44:00,000 --> 04:44:01,980
and there should be five notes left
7214
04:44:01,980 --> 04:44:04,440
right node four and then this is a new
7215
04:44:04,440 --> 04:44:06,780
react node
7216
04:44:06,780 --> 04:44:10,580
and looks correct to me
7217
04:44:10,620 --> 04:44:13,378
so the leading works the next step is to
7218
04:44:13,378 --> 04:44:15,958
update notes but before we do that I
7219
04:44:15,958 --> 04:44:17,520
also want to add a little icon to the
7220
04:44:17,520 --> 04:44:19,378
add new note button here a little plus
7221
04:44:19,378 --> 04:44:21,660
icon now that we already have the react
7222
04:44:21,660 --> 04:44:23,760
icons package installed that will be
7223
04:44:23,760 --> 04:44:25,680
really quick let's go into our button
7224
04:44:25,680 --> 04:44:27,120
tag here
7225
04:44:27,120 --> 04:44:29,340
and add this icon to the content of the
7226
04:44:29,340 --> 04:44:30,240
button
7227
04:44:30,240 --> 04:44:33,120
but we will have to import this first so
7228
04:44:33,120 --> 04:44:34,980
let's go up here
7229
04:44:34,980 --> 04:44:38,878
import this one is called fa plus from
7230
04:44:38,878 --> 04:44:41,580
the font awesome package
7231
04:44:41,580 --> 04:44:45,060
from react icons
7232
04:44:45,060 --> 04:44:48,500
slash F A
7233
04:44:48,660 --> 04:44:51,060
and then we can use it here
7234
04:44:51,060 --> 04:44:52,798
if a
7235
04:44:52,798 --> 04:44:57,560
plus with a self-closing tag
7236
04:44:58,020 --> 04:45:00,060
and there it is now it's not aligned
7237
04:45:00,060 --> 04:45:02,040
properly yet so we have to add another
7238
04:45:02,040 --> 04:45:04,920
class to our button here and we can use
7239
04:45:04,920 --> 04:45:06,958
our Flex sender utility class that we
7240
04:45:06,958 --> 04:45:10,440
already created so let's add
7241
04:45:10,440 --> 04:45:14,100
another class like this style utils Flex
7242
04:45:14,100 --> 04:45:16,260
Center
7243
04:45:16,260 --> 04:45:18,540
which aligns everything properly because
7244
04:45:18,540 --> 04:45:20,820
it turns it into a flex box it applies
7245
04:45:20,820 --> 04:45:22,560
the line item sender attributes and
7246
04:45:22,560 --> 04:45:24,240
everything and the little space here in
7247
04:45:24,240 --> 04:45:26,638
between the icon and the text is caused
7248
04:45:26,638 --> 04:45:29,160
by this gap of 4 pixels here this is why
7249
04:45:29,160 --> 04:45:31,200
I added this earlier okay looks pretty
7250
04:45:31,200 --> 04:45:33,780
cool if you ask me okay now let's add
7251
04:45:33,780 --> 04:45:36,660
the way to a update existing notes for
7252
04:45:36,660 --> 04:45:38,580
which we need another entry in our notes
7253
04:45:38,580 --> 04:45:41,520
API a file here let's put it between
7254
04:45:41,520 --> 04:45:44,718
create node and delete node
7255
04:45:44,718 --> 04:45:48,600
export async function
7256
04:45:48,600 --> 04:45:51,180
update node
7257
04:45:51,180 --> 04:45:54,120
which will take two arguments it will
7258
04:45:54,120 --> 04:45:56,040
take the note ideal
7259
04:45:56,040 --> 04:45:58,980
of the node that we want to update
7260
04:45:58,980 --> 04:46:02,700
and denote input let's call it node
7261
04:46:02,700 --> 04:46:04,680
where input doesn't really matter but
7262
04:46:04,680 --> 04:46:06,840
it's of type node input so it contains
7263
04:46:06,840 --> 04:46:08,580
the title and the text that we want to
7264
04:46:08,580 --> 04:46:10,378
update our node with
7265
04:46:10,378 --> 04:46:14,160
this function will return a promise
7266
04:46:14,160 --> 04:46:17,040
of type node if everything goes
7267
04:46:17,040 --> 04:46:19,080
successful it will return the updated
7268
04:46:19,080 --> 04:46:21,240
nodes which we then display in our UI
7269
04:46:21,240 --> 04:46:25,920
okay so we create a const response
7270
04:46:25,920 --> 04:46:28,440
it creates await fetch data the usual
7271
04:46:28,440 --> 04:46:29,458
way
7272
04:46:29,458 --> 04:46:33,180
the update endpoint is slash API
7273
04:46:33,180 --> 04:46:35,340
slash nodes
7274
04:46:35,340 --> 04:46:37,080
slash
7275
04:46:37,080 --> 04:46:39,000
and denote ideal
7276
04:46:39,000 --> 04:46:41,218
similar to how we did it for the delete
7277
04:46:41,218 --> 04:46:42,480
endpoint
7278
04:46:42,480 --> 04:46:45,060
comma and configuration
7279
04:46:45,060 --> 04:46:48,420
which we put into a new line
7280
04:46:48,420 --> 04:46:52,560
the method will be patched this time
7281
04:46:52,560 --> 04:46:55,920
we need the same HTTP header up here
7282
04:46:55,920 --> 04:46:58,980
because we send Json data
7283
04:46:58,980 --> 04:47:01,860
and the body is the same as well
7284
04:47:01,860 --> 04:47:04,980
we then the stringified version of our
7285
04:47:04,980 --> 04:47:07,760
node input
7286
04:47:08,580 --> 04:47:11,580
and the line here below is the same as
7287
04:47:11,580 --> 04:47:13,620
for create node as well we want to
7288
04:47:13,620 --> 04:47:15,600
return the Json body of the successful
7289
04:47:15,600 --> 04:47:17,878
response which should contain the
7290
04:47:17,878 --> 04:47:19,560
updated node
7291
04:47:19,560 --> 04:47:21,718
to update an existing node I actually
7292
04:47:21,718 --> 04:47:24,060
want to reuse the add node dialog that
7293
04:47:24,060 --> 04:47:26,040
we already created earlier we want to
7294
04:47:26,040 --> 04:47:28,200
change the name from add note dialog to
7295
04:47:28,200 --> 04:47:30,900
add edit nodes dialog because adding new
7296
04:47:30,900 --> 04:47:33,240
nodes and updating existing ones looks
7297
04:47:33,240 --> 04:47:35,218
very similar so it makes sense to reuse
7298
04:47:35,218 --> 04:47:37,500
the existing model we already have and
7299
04:47:37,500 --> 04:47:39,240
by the way this is why I kept the name
7300
04:47:39,240 --> 04:47:41,760
of this onnode saved callback generic I
7301
04:47:41,760 --> 04:47:43,798
didn't call this onnode edit because I
7302
04:47:43,798 --> 04:47:45,360
already knew that I want to reuse this
7303
04:47:45,360 --> 04:47:47,700
for editing notes as well so I called it
7304
04:47:47,700 --> 04:47:50,458
safe so what we do is first of all we
7305
04:47:50,458 --> 04:47:53,100
rename the props here I do this with the
7306
04:47:53,100 --> 04:47:55,560
F2 shortcut or right click rename from
7307
04:47:55,560 --> 04:47:58,680
add nodes dialog to add edit node dialog
7308
04:47:58,680 --> 04:48:00,360
props
7309
04:48:00,360 --> 04:48:05,360
let's also rename the component itself
7310
04:48:06,120 --> 04:48:08,040
but this doesn't automatically change
7311
04:48:08,040 --> 04:48:09,718
the file name so we have to do the
7312
04:48:09,718 --> 04:48:11,700
separate layer
7313
04:48:11,700 --> 04:48:15,180
to add edit notes dialog.tfx
7314
04:48:15,180 --> 04:48:17,520
update Imports always automatically
7315
04:48:17,520 --> 04:48:20,520
update Imports make sense
7316
04:48:20,520 --> 04:48:23,340
entry might have to use this save all
7317
04:48:23,340 --> 04:48:25,980
filets command here to save the files
7318
04:48:25,980 --> 04:48:28,920
where this import has changed okay then
7319
04:48:28,920 --> 04:48:31,560
we want to pass an optional argument to
7320
04:48:31,560 --> 04:48:33,958
this add edit nodes dialog
7321
04:48:33,958 --> 04:48:37,138
we want to pass the note to add it to it
7322
04:48:37,138 --> 04:48:40,200
which is an optional argument of type
7323
04:48:40,200 --> 04:48:41,218
node
7324
04:48:41,218 --> 04:48:43,620
which is our node model the way this
7325
04:48:43,620 --> 04:48:45,660
works is that we can pass this node to
7326
04:48:45,660 --> 04:48:48,420
edit to it or not if we pass one then we
7327
04:48:48,420 --> 04:48:50,218
know that we are not adding a new node
7328
04:48:50,218 --> 04:48:52,500
we are updating an existing one if we
7329
04:48:52,500 --> 04:48:54,480
don't pass this node to edit then we
7330
04:48:54,480 --> 04:48:56,878
know that we are creating a new node
7331
04:48:56,878 --> 04:48:59,100
so let's not forget to add it down here
7332
04:48:59,100 --> 04:49:01,080
then we want to make some changes
7333
04:49:01,080 --> 04:49:03,060
throughout this dialog here
7334
04:49:03,060 --> 04:49:04,860
first of all we want to pass a
7335
04:49:04,860 --> 04:49:07,378
configuration to use form which we do by
7336
04:49:07,378 --> 04:49:09,718
adding a pair of curly braces between
7337
04:49:09,718 --> 04:49:11,940
the parentheses of the series form color
7338
04:49:11,940 --> 04:49:14,458
let's put this into a new line
7339
04:49:14,458 --> 04:49:16,560
because one thing we can do in here we
7340
04:49:16,560 --> 04:49:19,980
can Define default values like this so
7341
04:49:19,980 --> 04:49:21,718
what we do is we set the default value
7342
04:49:21,718 --> 04:49:23,520
of title
7343
04:49:23,520 --> 04:49:26,160
to a node to edit
7344
04:49:26,160 --> 04:49:28,378
save color title
7345
04:49:28,378 --> 04:49:30,120
so this will use the title of the note
7346
04:49:30,120 --> 04:49:32,218
we are updating if we pass this argument
7347
04:49:32,218 --> 04:49:34,920
if not this will resolve to undefined
7348
04:49:34,920 --> 04:49:36,600
and we can handle this case with these
7349
04:49:36,600 --> 04:49:38,638
two vertical pipes which is a logical or
7350
04:49:38,638 --> 04:49:40,260
this exists in most programming
7351
04:49:40,260 --> 04:49:42,660
languages if the node to edit repairs is
7352
04:49:42,660 --> 04:49:44,700
undefined then we want to use an empty
7353
04:49:44,700 --> 04:49:46,740
string as the default value because then
7354
04:49:46,740 --> 04:49:48,600
we just have our usual add node
7355
04:49:48,600 --> 04:49:50,700
situation where we don't want to have
7356
04:49:50,700 --> 04:49:53,340
any default values already in the input
7357
04:49:53,340 --> 04:49:55,980
fields comma entry do the same for the
7358
04:49:55,980 --> 04:49:57,240
text
7359
04:49:57,240 --> 04:49:59,340
note to edit
7360
04:49:59,340 --> 04:50:03,120
text or an empty string then in on
7361
04:50:03,120 --> 04:50:05,040
submit we have to distinguish between
7362
04:50:05,040 --> 04:50:06,900
the situation where we want to create a
7363
04:50:06,900 --> 04:50:09,180
new node or update an existing one so
7364
04:50:09,180 --> 04:50:11,400
what we do is we create
7365
04:50:11,400 --> 04:50:13,680
a letter variable for this node response
7366
04:50:13,680 --> 04:50:14,340
here
7367
04:50:14,340 --> 04:50:17,820
so we write let note response
7368
04:50:17,820 --> 04:50:20,040
which will be of type node but we don't
7369
04:50:20,040 --> 04:50:21,660
initialize this yet
7370
04:50:21,660 --> 04:50:24,840
because now below we want to check
7371
04:50:24,840 --> 04:50:27,180
if not to edit
7372
04:50:27,180 --> 04:50:29,400
which means that it's not undefined we
7373
04:50:29,400 --> 04:50:31,560
forwarded it then we want to set node
7374
04:50:31,560 --> 04:50:33,718
response to
7375
04:50:33,718 --> 04:50:36,540
await notes API
7376
04:50:36,540 --> 04:50:39,480
and make our update node color
7377
04:50:39,480 --> 04:50:43,980
where we pass the node to edits idea
7378
04:50:43,980 --> 04:50:46,260
and the node input right the title and
7379
04:50:46,260 --> 04:50:48,560
the text
7380
04:50:49,200 --> 04:50:51,600
if we don't have this node to edit idea
7381
04:50:51,600 --> 04:50:53,820
we are in our usual create node
7382
04:50:53,820 --> 04:50:55,920
saturation then we want to color create
7383
04:50:55,920 --> 04:50:58,680
node and store the return value in this
7384
04:50:58,680 --> 04:51:01,440
variable and then we just do our unknown
7385
04:51:01,440 --> 04:51:03,900
saved callback in either case
7386
04:51:03,900 --> 04:51:06,000
when we created a new node and when we
7387
04:51:06,000 --> 04:51:08,218
updated an existing one
7388
04:51:08,218 --> 04:51:10,500
just a few more changes we have to do
7389
04:51:10,500 --> 04:51:14,280
here in our add edit notes dialog
7390
04:51:14,280 --> 04:51:16,680
we want to change the title and make it
7391
04:51:16,680 --> 04:51:18,480
dependent if it's an update or an ad
7392
04:51:18,480 --> 04:51:20,638
situation so what we do is we add a pair
7393
04:51:20,638 --> 04:51:22,560
of curly braces
7394
04:51:22,560 --> 04:51:25,740
we check if not to edit ID has a value
7395
04:51:25,740 --> 04:51:28,020
which we can do with this ternary
7396
04:51:28,020 --> 04:51:32,100
operator so node to edit question mark
7397
04:51:32,100 --> 04:51:34,860
if this value exists then the title will
7398
04:51:34,860 --> 04:51:36,240
be edit node
7399
04:51:36,240 --> 04:51:39,958
colon if this value does not exist
7400
04:51:39,958 --> 04:51:42,958
we use the title add node and let's also
7401
04:51:42,958 --> 04:51:44,760
adjust for completion
7402
04:51:44,760 --> 04:51:47,400
update the idea of this form we could
7403
04:51:47,400 --> 04:51:50,280
keep it at node but I'm perfectionist so
7404
04:51:50,280 --> 04:51:53,340
let's change this to alt edit node form
7405
04:51:53,340 --> 04:51:56,878
and not forget update here in the button
7406
04:51:56,878 --> 04:51:58,440
component as well
7407
04:51:58,440 --> 04:52:01,320
save this but we don't have a way to
7408
04:52:01,320 --> 04:52:04,138
open this edit dialog yet right so next
7409
04:52:04,138 --> 04:52:06,240
we need to catch this click on our nodes
7410
04:52:06,240 --> 04:52:08,340
here on the Node component and then open
7411
04:52:08,340 --> 04:52:10,680
this dialog in response to it
7412
04:52:10,680 --> 04:52:13,620
so let's go into the notes component
7413
04:52:13,620 --> 04:52:16,020
once again which I have to find First
7414
04:52:16,020 --> 04:52:18,480
this one here and we want to add another
7415
04:52:18,480 --> 04:52:20,040
card back here
7416
04:52:20,040 --> 04:52:22,200
on node
7417
04:52:22,200 --> 04:52:23,700
clicked
7418
04:52:23,700 --> 04:52:25,860
let's keep the name generic let's not
7419
04:52:25,860 --> 04:52:27,900
call it update node or something like
7420
04:52:27,900 --> 04:52:30,000
that because this way the color can
7421
04:52:30,000 --> 04:52:31,798
decide what they do in response to a
7422
04:52:31,798 --> 04:52:33,600
note being clicked maybe they don't
7423
04:52:33,600 --> 04:52:36,000
always want to open the updates dialog
7424
04:52:36,000 --> 04:52:37,920
maybe they just want to show the text so
7425
04:52:37,920 --> 04:52:40,080
we keep this decision to the color okay
7426
04:52:40,080 --> 04:52:43,080
this is a function that looks exactly
7427
04:52:43,080 --> 04:52:46,400
like the delete function here
7428
04:52:46,500 --> 04:52:49,200
it takes the note that we clicked and
7429
04:52:49,200 --> 04:52:51,060
forwards it to the color
7430
04:52:51,060 --> 04:52:53,760
we could also put this edit dialog
7431
04:52:53,760 --> 04:52:56,100
directly into this component this would
7432
04:52:56,100 --> 04:52:58,260
work theoretically but we want to pass
7433
04:52:58,260 --> 04:53:00,298
this card back to the outside instead
7434
04:53:00,298 --> 04:53:02,100
because this way we keep our code more
7435
04:53:02,100 --> 04:53:04,320
flexible and how we use these nodes this
7436
04:53:04,320 --> 04:53:06,420
is called hoisting by the way hoisting
7437
04:53:06,420 --> 04:53:08,520
means you're taking state or callbacks
7438
04:53:08,520 --> 04:53:11,040
and moving them one level higher to the
7439
04:53:11,040 --> 04:53:13,378
component that uses this component where
7440
04:53:13,378 --> 04:53:15,000
you put State and where you handle
7441
04:53:15,000 --> 04:53:16,980
clicks always depends on what exactly
7442
04:53:16,980 --> 04:53:18,780
you want to do and how flexible you want
7443
04:53:18,780 --> 04:53:20,700
your code to be okay and we want to
7444
04:53:20,700 --> 04:53:22,740
trigger this callback when we click our
7445
04:53:22,740 --> 04:53:24,660
node right
7446
04:53:24,660 --> 04:53:28,200
so what we do is we are going to discard
7447
04:53:28,200 --> 04:53:29,820
tag here
7448
04:53:29,820 --> 04:53:33,298
and add an on-click attribute to it
7449
04:53:33,298 --> 04:53:35,878
which takes an error function
7450
04:53:35,878 --> 04:53:40,080
that just calls on node clicked I
7451
04:53:40,080 --> 04:53:43,080
haven't added it here to our
7452
04:53:43,080 --> 04:53:46,620
parameters yet so let's do this
7453
04:53:46,620 --> 04:53:49,378
at this position
7454
04:53:49,378 --> 04:53:52,740
we want to cut this down here
7455
04:53:52,740 --> 04:53:54,780
and we want to forward the node that we
7456
04:53:54,780 --> 04:53:55,740
clicked
7457
04:53:55,740 --> 04:53:57,540
the same as we did here in the delete
7458
04:53:57,540 --> 04:53:59,280
callback
7459
04:53:59,280 --> 04:54:01,620
now let's wire everything together in
7460
04:54:01,620 --> 04:54:04,560
our ftsx file this now expects the
7461
04:54:04,560 --> 04:54:06,600
onnode click callback but first of all
7462
04:54:06,600 --> 04:54:09,360
we need a state where we store this node
7463
04:54:09,360 --> 04:54:11,520
ID that we clicked so let's add another
7464
04:54:11,520 --> 04:54:13,500
state here at the top
7465
04:54:13,500 --> 04:54:16,798
let's put it below show add node dialog
7466
04:54:16,798 --> 04:54:19,020
because this decides if we show the edit
7467
04:54:19,020 --> 04:54:21,240
version of our dialog so we create
7468
04:54:21,240 --> 04:54:23,638
another state
7469
04:54:23,638 --> 04:54:27,900
const let's call it node to edit
7470
04:54:27,900 --> 04:54:30,660
and the setup function set in order to
7471
04:54:30,660 --> 04:54:33,060
edit and we always have to change this
7472
04:54:33,060 --> 04:54:35,780
to camera case
7473
04:54:36,000 --> 04:54:38,760
which is a use State and we have to set
7474
04:54:38,760 --> 04:54:40,860
the type of this explicitly because we
7475
04:54:40,860 --> 04:54:43,020
initialize this with Nile
7476
04:54:43,020 --> 04:54:45,780
so the type of this is node model or
7477
04:54:45,780 --> 04:54:46,980
null
7478
04:54:46,980 --> 04:54:49,740
because we use null when we don't have a
7479
04:54:49,740 --> 04:54:51,180
node that we want to update right now
7480
04:54:51,180 --> 04:54:53,400
this is what we also start with
7481
04:54:53,400 --> 04:54:55,560
then we go back down to our node
7482
04:54:55,560 --> 04:54:57,600
component which is already complaining
7483
04:54:57,600 --> 04:54:58,560
here
7484
04:54:58,560 --> 04:55:01,138
and adds dismissing on Note clicked
7485
04:55:01,138 --> 04:55:04,080
callback and here we should be able to
7486
04:55:04,080 --> 04:55:06,420
just pass the name of the setup function
7487
04:55:06,420 --> 04:55:08,520
of our new state
7488
04:55:08,520 --> 04:55:12,240
so Z note to edit
7489
04:55:12,240 --> 04:55:14,820
because this is a function that gets
7490
04:55:14,820 --> 04:55:17,878
passed a node model and Z node to edit
7491
04:55:17,878 --> 04:55:20,100
is a function that takes a node model so
7492
04:55:20,100 --> 04:55:22,080
if I'm not mistaken right now the syntax
7493
04:55:22,080 --> 04:55:23,160
should work
7494
04:55:23,160 --> 04:55:24,900
otherwise we could write it like this
7495
04:55:24,900 --> 04:55:29,160
note right arrow and pass it here but
7496
04:55:29,160 --> 04:55:30,718
since these functions have the same
7497
04:55:30,718 --> 04:55:34,080
signature this should work as well
7498
04:55:34,080 --> 04:55:36,060
but first we have to set up the second
7499
04:55:36,060 --> 04:55:39,000
dialog so here we did show add node
7500
04:55:39,000 --> 04:55:41,638
dialog to Amazon science below we want
7501
04:55:41,638 --> 04:55:44,458
to do the same but this time with node
7502
04:55:44,458 --> 04:55:46,200
to edit
7503
04:55:46,200 --> 04:55:48,600
if this value is not null we also want
7504
04:55:48,600 --> 04:55:51,900
to show our add edit nodes dialog just
7505
04:55:51,900 --> 04:55:54,120
with a slightly different configuration
7506
04:55:54,120 --> 04:55:57,180
first of all we want to pass the node to
7507
04:55:57,180 --> 04:55:59,940
edit this time which is this one here
7508
04:55:59,940 --> 04:56:02,040
and since we have this check up here
7509
04:56:02,040 --> 04:56:03,900
with the embossing sign we know that
7510
04:56:03,900 --> 04:56:06,540
oops it's not null down here because
7511
04:56:06,540 --> 04:56:08,340
otherwise we would not get inside this
7512
04:56:08,340 --> 04:56:11,040
block and typescript is smart enough to
7513
04:56:11,040 --> 04:56:13,260
smartcasters it's smart enough to know
7514
04:56:13,260 --> 04:56:15,180
that this can't be null down here so we
7515
04:56:15,180 --> 04:56:17,280
can pass this value then we want to
7516
04:56:17,280 --> 04:56:19,620
define the on dismiss callback like we
7517
04:56:19,620 --> 04:56:21,120
did up here
7518
04:56:21,120 --> 04:56:23,878
just that we don't want to sit show at
7519
04:56:23,878 --> 04:56:25,980
no dialogue to false
7520
04:56:25,980 --> 04:56:29,700
instead we want to set notes to edit
7521
04:56:29,700 --> 04:56:31,440
back to another
7522
04:56:31,440 --> 04:56:33,600
which will also hide this dialog because
7523
04:56:33,600 --> 04:56:35,520
of this checkup here
7524
04:56:35,520 --> 04:56:38,280
and the last thing we need is our onnote
7525
04:56:38,280 --> 04:56:40,620
saved callback
7526
04:56:40,620 --> 04:56:43,620
which the Sim is up here forwards as the
7527
04:56:43,620 --> 04:56:45,718
node that reserved just that this time
7528
04:56:45,718 --> 04:56:47,280
it's not a new node it's the updated
7529
04:56:47,280 --> 04:56:50,100
note it's the same variable basically
7530
04:56:50,100 --> 04:56:53,160
but we can give it a different name
7531
04:56:53,160 --> 04:56:56,340
we can call it updated note
7532
04:56:56,340 --> 04:56:58,200
and then Define what we want to do with
7533
04:56:58,200 --> 04:57:01,080
it first of all we want to set note to
7534
04:57:01,080 --> 04:57:03,900
add it back to Nile here as well to
7535
04:57:03,900 --> 04:57:05,760
close the dialog after we save the
7536
04:57:05,760 --> 04:57:07,020
changes
7537
04:57:07,020 --> 04:57:09,420
and then we want to update the UI and
7538
04:57:09,420 --> 04:57:10,980
what we want to do is we want to take
7539
04:57:10,980 --> 04:57:13,138
the node that we updated but put the new
7540
04:57:13,138 --> 04:57:15,480
data in there without having to refresh
7541
04:57:15,480 --> 04:57:17,400
the page first we want to do this with
7542
04:57:17,400 --> 04:57:19,378
the value we get returned from the API
7543
04:57:19,378 --> 04:57:22,680
so again we call Z nodes
7544
04:57:22,680 --> 04:57:25,680
which is our state setup function
7545
04:57:25,680 --> 04:57:29,160
and then recall notes dot map earlier we
7546
04:57:29,160 --> 04:57:31,740
used notes.filter to remove a node from
7547
04:57:31,740 --> 04:57:34,138
the nodes array with nodes.map we can
7548
04:57:34,138 --> 04:57:36,240
take each single node and transform it
7549
04:57:36,240 --> 04:57:38,400
in some way now we want to leave most of
7550
04:57:38,400 --> 04:57:40,138
the nodes untouched we only want to
7551
04:57:40,138 --> 04:57:42,240
transform the one that has the ID of the
7552
04:57:42,240 --> 04:57:44,520
node we updated to put our new data in
7553
04:57:44,520 --> 04:57:47,760
there so nodes.map which again takes an
7554
04:57:47,760 --> 04:57:51,240
argument which we call existing node
7555
04:57:51,240 --> 04:57:53,760
as we did earlier when we deleted a node
7556
04:57:53,760 --> 04:57:57,600
then we want to check if the idea of the
7557
04:57:57,600 --> 04:57:59,700
existing node
7558
04:57:59,700 --> 04:58:03,180
is the same as the idea of the updated
7559
04:58:03,180 --> 04:58:05,520
note then we know that this is the one
7560
04:58:05,520 --> 04:58:08,100
we just updated and we wanna display not
7561
04:58:08,100 --> 04:58:10,560
the old one but the new one in the UI I
7562
04:58:10,560 --> 04:58:12,360
have to make this a bit bigger again
7563
04:58:12,360 --> 04:58:15,180
so what we can do is if existing node ID
7564
04:58:15,180 --> 04:58:17,218
is the same as updated node ideal
7565
04:58:17,218 --> 04:58:19,980
question mark in this case we want to
7566
04:58:19,980 --> 04:58:23,340
return the updated note colon
7567
04:58:23,340 --> 04:58:25,860
otherwise we want to return the existing
7568
04:58:25,860 --> 04:58:27,480
node so this might look a bit
7569
04:58:27,480 --> 04:58:29,458
complicated at first
7570
04:58:29,458 --> 04:58:31,860
but it completely makes sense we call
7571
04:58:31,860 --> 04:58:34,320
our Z nodes state map our existing nodes
7572
04:58:34,320 --> 04:58:36,360
by checking for the idea for each of
7573
04:58:36,360 --> 04:58:38,160
them and if the ID is the one we just
7574
04:58:38,160 --> 04:58:40,080
updated then we want to display the
7575
04:58:40,080 --> 04:58:42,540
updated note and put it into this array
7576
04:58:42,540 --> 04:58:44,280
otherwise we want to put the same old
7577
04:58:44,280 --> 04:58:46,980
node as we had before into this array
7578
04:58:46,980 --> 04:58:49,620
okay let's try it out let's make this a
7579
04:58:49,620 --> 04:58:52,500
bit smaller again and let's try updating
7580
04:58:52,500 --> 04:58:55,920
a note so when we click this node it
7581
04:58:55,920 --> 04:58:58,080
should set the node to edit states to
7582
04:58:58,080 --> 04:59:00,120
the node we just clicked which opens
7583
04:59:00,120 --> 04:59:02,760
this dialog with the edit node State and
7584
04:59:02,760 --> 04:59:04,680
the default values already inserted
7585
04:59:04,680 --> 04:59:07,378
let's change the title and the text
7586
04:59:07,378 --> 04:59:11,100
updated note 3. we updated this note
7587
04:59:11,100 --> 04:59:14,400
through our react app
7588
04:59:14,400 --> 04:59:16,798
when we click save the save button
7589
04:59:16,798 --> 04:59:18,298
should be disabled again for a very
7590
04:59:18,298 --> 04:59:20,160
short moment while the update is running
7591
04:59:20,160 --> 04:59:22,560
the changes should be saved in our
7592
04:59:22,560 --> 04:59:25,020
database through our server and because
7593
04:59:25,020 --> 04:59:26,700
of our map call we should see the
7594
04:59:26,700 --> 04:59:30,060
updated state in the UI so save and
7595
04:59:30,060 --> 04:59:31,740
after a very short moment everything
7596
04:59:31,740 --> 04:59:34,200
works properly here's our updated note
7597
04:59:34,200 --> 04:59:36,360
and when we refresh the screen we still
7598
04:59:36,360 --> 04:59:38,580
see the Zim because this data is stored
7599
04:59:38,580 --> 04:59:41,280
in our database properly
7600
04:59:41,280 --> 04:59:44,040
somewhere here is our updated node 3.
7601
04:59:44,040 --> 04:59:47,100
with the latest updated timestamp really
7602
04:59:47,100 --> 04:59:47,878
cool
7603
04:59:47,878 --> 04:59:50,700
also I don't know why the ftsx file has
7604
04:59:50,700 --> 04:59:52,920
two spaces instead of four by default
7605
04:59:52,920 --> 04:59:55,440
but it looks a bit cramped in my opinion
7606
04:59:55,440 --> 04:59:58,260
so we can change the stone here
7607
04:59:58,260 --> 05:00:00,298
indent using tabs you don't have to do
7608
05:00:00,298 --> 05:00:02,940
this but I want to use the default tab
7609
05:00:02,940 --> 05:00:05,760
size of four and when I format this
7610
05:00:05,760 --> 05:00:08,580
again looks better in my opinion okay
7611
05:00:08,580 --> 05:00:10,500
this works really nicely and the next
7612
05:00:10,500 --> 05:00:12,240
thing we want to do is we want to put
7613
05:00:12,240 --> 05:00:15,060
this loading and arrow State into our UI
7614
05:00:15,060 --> 05:00:16,740
to take care of situations where
7615
05:00:16,740 --> 05:00:18,660
something goes wrong and we also want to
7616
05:00:18,660 --> 05:00:20,638
organize our code a bit better so that
7617
05:00:20,638 --> 05:00:23,100
we don't put everything into our ftsx
7618
05:00:23,100 --> 05:00:25,400
file
7619
05:00:28,920 --> 05:00:31,080
okay so right now since we are on
7620
05:00:31,080 --> 05:00:33,298
localhost loading notes is really fast
7621
05:00:33,298 --> 05:00:35,700
so there's almost no delay and we see
7622
05:00:35,700 --> 05:00:37,920
our nodes on the screen instantly when
7623
05:00:37,920 --> 05:00:39,900
we actually deploy this to a server this
7624
05:00:39,900 --> 05:00:41,700
will take a moment so it makes sense to
7625
05:00:41,700 --> 05:00:43,440
add a progress bar while the nodes are
7626
05:00:43,440 --> 05:00:45,718
loading and it also makes sense to add
7627
05:00:45,718 --> 05:00:48,180
some text in case loading went wrong so
7628
05:00:48,180 --> 05:00:50,160
we show something like a code and load
7629
05:00:50,160 --> 05:00:52,020
your data right now we are just showing
7630
05:00:52,020 --> 05:00:54,240
the alert dialog and of course that's
7631
05:00:54,240 --> 05:00:56,340
also an option but it's a bit annoying
7632
05:00:56,340 --> 05:00:58,320
to the user so how you display these
7633
05:00:58,320 --> 05:01:00,298
errors in the UI is up to you if you
7634
05:01:00,298 --> 05:01:02,100
show a dialogue or an error message
7635
05:01:02,100 --> 05:01:04,500
embedded on the screen you can decide
7636
05:01:04,500 --> 05:01:06,180
this yourself I just want to show you
7637
05:01:06,180 --> 05:01:08,700
different options so what we do is we go
7638
05:01:08,700 --> 05:01:12,060
into our ftsx file and at another state
7639
05:01:12,060 --> 05:01:14,580
for when the notes are loading let's put
7640
05:01:14,580 --> 05:01:18,120
it down here below the nodes state
7641
05:01:18,120 --> 05:01:20,400
we create another state that we call
7642
05:01:20,400 --> 05:01:22,680
nodes loading
7643
05:01:22,680 --> 05:01:27,600
and set nodes loading for the setter
7644
05:01:27,600 --> 05:01:30,540
and we initialize this state riff true
7645
05:01:30,540 --> 05:01:32,760
this time because as soon as we open the
7646
05:01:32,760 --> 05:01:35,520
page the nodes try to load right so we
7647
05:01:35,520 --> 05:01:37,860
can set this to True white array then we
7648
05:01:37,860 --> 05:01:40,860
add another state below
7649
05:01:40,860 --> 05:01:44,400
which we call show notes
7650
05:01:44,400 --> 05:01:47,040
loading error
7651
05:01:47,040 --> 05:01:49,138
and then the setup which I'm going to
7652
05:01:49,138 --> 05:01:51,240
copy this and change this here to camera
7653
05:01:51,240 --> 05:01:53,660
case
7654
05:01:54,660 --> 05:01:57,480
which we initialize with a state of
7655
05:01:57,480 --> 05:01:58,798
false
7656
05:01:58,798 --> 05:02:00,660
we don't want to show this right array
7657
05:02:00,660 --> 05:02:03,000
only when something went wrong
7658
05:02:03,000 --> 05:02:04,920
now as you can see I give this error a
7659
05:02:04,920 --> 05:02:07,020
very specific name I prefer long
7660
05:02:07,020 --> 05:02:09,600
variable names to be more descriptive I
7661
05:02:09,600 --> 05:02:11,400
don't just want to call this show error
7662
05:02:11,400 --> 05:02:13,200
because this could be any error we want
7663
05:02:13,200 --> 05:02:15,180
to use this error specifically for when
7664
05:02:15,180 --> 05:02:17,580
loading the nodes failed so I give it
7665
05:02:17,580 --> 05:02:20,100
this long name and we handle all the
7666
05:02:20,100 --> 05:02:21,958
states down here when we try to load
7667
05:02:21,958 --> 05:02:24,480
nodes so what we do is as soon as we
7668
05:02:24,480 --> 05:02:26,458
load nodes
7669
05:02:26,458 --> 05:02:28,798
we set the error
7670
05:02:28,798 --> 05:02:30,718
we are showing the error to false
7671
05:02:30,718 --> 05:02:32,760
because we might want to add a retry
7672
05:02:32,760 --> 05:02:34,378
button or something like that and when
7673
05:02:34,378 --> 05:02:36,060
we retry loading the notes we want to
7674
05:02:36,060 --> 05:02:39,420
hide the arrow if there was one right
7675
05:02:39,420 --> 05:02:43,080
and we want to set loads loading no
7676
05:02:43,080 --> 05:02:46,080
notes loading to true
7677
05:02:46,080 --> 05:02:48,780
because as soon as we call this function
7678
05:02:48,780 --> 05:02:51,298
here we start loading our nodes and only
7679
05:02:51,298 --> 05:02:54,540
after this run we are finished
7680
05:02:54,540 --> 05:02:58,200
we want to replace the alert dialog here
7681
05:02:58,200 --> 05:03:01,020
in case something goes wrong
7682
05:03:01,020 --> 05:03:04,920
and set short notes loading error to
7683
05:03:04,920 --> 05:03:05,820
true
7684
05:03:05,820 --> 05:03:07,920
which will contain a generic error
7685
05:03:07,920 --> 05:03:09,840
message by the way you could also
7686
05:03:09,840 --> 05:03:11,638
display the error you get from the back
7687
05:03:11,638 --> 05:03:13,920
end by extracting it from this error
7688
05:03:13,920 --> 05:03:16,080
variable I just want to show a generic
7689
05:03:16,080 --> 05:03:18,120
error message instead it always depends
7690
05:03:18,120 --> 05:03:20,100
on how much information you want to give
7691
05:03:20,100 --> 05:03:21,780
to the user
7692
05:03:21,780 --> 05:03:24,180
and then we also have to set nodes
7693
05:03:24,180 --> 05:03:26,520
loading back to false now we want to do
7694
05:03:26,520 --> 05:03:28,860
this in both cases when everything went
7695
05:03:28,860 --> 05:03:31,500
well and when there was an error so we
7696
05:03:31,500 --> 05:03:33,900
put this into the finally block try
7697
05:03:33,900 --> 05:03:35,760
catch finally
7698
05:03:35,760 --> 05:03:38,700
set notes loading
7699
05:03:38,700 --> 05:03:40,680
back to false
7700
05:03:40,680 --> 05:03:43,200
okay so now we have the state and now we
7701
05:03:43,200 --> 05:03:45,420
use this data to display different UI
7702
05:03:45,420 --> 05:03:48,000
elements on our page for this we will
7703
05:03:48,000 --> 05:03:50,100
have to put some conditional Stone here
7704
05:03:50,100 --> 05:03:52,980
into our UI that decides if we show our
7705
05:03:52,980 --> 05:03:55,798
usual notes grid here or if we show
7706
05:03:55,798 --> 05:03:57,660
something else now to keep this a bit
7707
05:03:57,660 --> 05:03:59,580
organized let's put this into a variable
7708
05:03:59,580 --> 05:04:01,980
that's something we can actually do we
7709
05:04:01,980 --> 05:04:04,560
can cut this out go above our return
7710
05:04:04,560 --> 05:04:06,600
statement here
7711
05:04:06,600 --> 05:04:09,060
create a variable that's colored
7712
05:04:09,060 --> 05:04:12,240
nodes grid
7713
05:04:12,240 --> 05:04:14,638
and we can set it to the piece we just
7714
05:04:14,638 --> 05:04:16,440
cut out
7715
05:04:16,440 --> 05:04:18,480
and then we can use the stone here in
7716
05:04:18,480 --> 05:04:20,160
the place where we had the chord before
7717
05:04:20,160 --> 05:04:22,500
but now I want to check
7718
05:04:22,500 --> 05:04:24,780
in between curly braces
7719
05:04:24,780 --> 05:04:28,260
if notes loading is true
7720
05:04:28,260 --> 05:04:30,780
then so we use these two Embers enzymes
7721
05:04:30,780 --> 05:04:33,298
as we have done before we want to show a
7722
05:04:33,298 --> 05:04:34,980
loading spinner which is another
7723
05:04:34,980 --> 05:04:38,040
component from the bootstrap Library
7724
05:04:38,040 --> 05:04:40,680
we had some configuration here we set
7725
05:04:40,680 --> 05:04:44,458
the animation to border
7726
05:04:44,458 --> 05:04:47,280
and the variant to primary which just
7727
05:04:47,280 --> 05:04:49,440
sets the color and we close this with a
7728
05:04:49,440 --> 05:04:52,200
slash and a closing angle bracket
7729
05:04:52,200 --> 05:04:54,000
this will be shown why the notes are
7730
05:04:54,000 --> 05:04:55,560
loading you could see this for a very
7731
05:04:55,560 --> 05:04:57,840
short moment
7732
05:04:57,840 --> 05:05:01,320
but we are not quite there yet
7733
05:05:01,320 --> 05:05:02,820
the second line we add another
7734
05:05:02,820 --> 05:05:04,440
conditional
7735
05:05:04,440 --> 05:05:08,218
if show notes loading error is true
7736
05:05:08,218 --> 05:05:10,500
then we want to show some little text in
7737
05:05:10,500 --> 05:05:12,180
the UI for which we can use this
7738
05:05:12,180 --> 05:05:14,940
paragraph HTML tag which basically adds
7739
05:05:14,940 --> 05:05:16,980
some text with a little bit of margin
7740
05:05:16,980 --> 05:05:19,138
below
7741
05:05:19,138 --> 05:05:22,378
something Rand wrong
7742
05:05:22,378 --> 05:05:24,840
no there's no apostrophe here
7743
05:05:24,840 --> 05:05:27,120
something went wrong
7744
05:05:27,120 --> 05:05:30,900
please refresh the page
7745
05:05:30,900 --> 05:05:33,420
okay this will show in case there is an
7746
05:05:33,420 --> 05:05:36,180
error you will see this in a moment
7747
05:05:36,180 --> 05:05:39,180
and finally below we want to show the
7748
05:05:39,180 --> 05:05:41,218
notes themselves if there was no error
7749
05:05:41,218 --> 05:05:43,200
if the notes are not loading and if they
7750
05:05:43,200 --> 05:05:45,718
are not empty so another pair of curly
7751
05:05:45,718 --> 05:05:48,240
braces here we write exclamation mark
7752
05:05:48,240 --> 05:05:50,820
notes loading if the notes are not
7753
05:05:50,820 --> 05:05:54,360
loading anymore and exclamation mark
7754
05:05:54,360 --> 05:05:57,298
if show notes loading error is false as
7755
05:05:57,298 --> 05:06:00,480
well and again to emboss enzymes then we
7756
05:06:00,480 --> 05:06:02,760
show the component itself and I actually
7757
05:06:02,760 --> 05:06:04,560
want to add another conditional down
7758
05:06:04,560 --> 05:06:06,180
here because I want to show a little
7759
05:06:06,180 --> 05:06:08,878
text if the notes array is empty so what
7760
05:06:08,878 --> 05:06:12,180
we do is we add this pair of emptier
7761
05:06:12,180 --> 05:06:14,218
angle brackets which is a so-called
7762
05:06:14,218 --> 05:06:16,920
fragment in react a fragment basically
7763
05:06:16,920 --> 05:06:18,900
allows us to put more than one
7764
05:06:18,900 --> 05:06:21,540
components or HTML tags in places where
7765
05:06:21,540 --> 05:06:23,520
we could only put one and we need it
7766
05:06:23,520 --> 05:06:25,320
right here because we want to add
7767
05:06:25,320 --> 05:06:27,298
another pair of curly braces for a
7768
05:06:27,298 --> 05:06:28,680
conditional
7769
05:06:28,680 --> 05:06:31,200
and if we remove this fragment
7770
05:06:31,200 --> 05:06:34,020
this will complain because the syntax is
7771
05:06:34,020 --> 05:06:36,060
not valid you can't put curly braces
7772
05:06:36,060 --> 05:06:38,878
into another pair of curly braces you
7773
05:06:38,878 --> 05:06:41,340
have to use this fragment in between and
7774
05:06:41,340 --> 05:06:43,740
here we want to check if notes dot
7775
05:06:43,740 --> 05:06:45,000
length
7776
05:06:45,000 --> 05:06:47,638
is greater than zero so if the nodes are
7777
05:06:47,638 --> 05:06:48,840
not emptier
7778
05:06:48,840 --> 05:06:50,760
then I put the next part into a separate
7779
05:06:50,760 --> 05:06:52,138
line
7780
05:06:52,138 --> 05:06:53,940
question mark which is a ternary
7781
05:06:53,940 --> 05:06:55,080
operator
7782
05:06:55,080 --> 05:06:57,718
then we want to render the notes grid
7783
05:06:57,718 --> 05:07:01,378
and Below colon if notes that length is
7784
05:07:01,378 --> 05:07:03,480
not greater than zero which means that
7785
05:07:03,480 --> 05:07:06,120
our nodes array is emptier then we want
7786
05:07:06,120 --> 05:07:08,820
to show another paragraph
7787
05:07:08,820 --> 05:07:11,218
you don't
7788
05:07:11,218 --> 05:07:13,200
have any
7789
05:07:13,200 --> 05:07:17,280
notes yet and I'm really bad at spelling
7790
05:07:17,280 --> 05:07:20,458
so to recap why not loading is true we
7791
05:07:20,458 --> 05:07:22,560
show a spinner if there's an error we
7792
05:07:22,560 --> 05:07:24,360
show the error message if both the
7793
05:07:24,360 --> 05:07:26,340
loading State and the error are false
7794
05:07:26,340 --> 05:07:29,100
then we either show the notes grid if
7795
05:07:29,100 --> 05:07:30,660
there are nodes in the array or our
7796
05:07:30,660 --> 05:07:32,820
empty State here now let's make some
7797
05:07:32,820 --> 05:07:34,920
changes in our nodes controller in the
7798
05:07:34,920 --> 05:07:36,480
back end just to see these different
7799
05:07:36,480 --> 05:07:37,680
states
7800
05:07:37,680 --> 05:07:39,660
you don't have to do this you can just
7801
05:07:39,660 --> 05:07:42,060
watch the video here go into the back
7802
05:07:42,060 --> 05:07:43,680
end
7803
05:07:43,680 --> 05:07:45,780
and the place where we fetch our nodes
7804
05:07:45,780 --> 05:07:47,878
first of all let's see the loading State
7805
05:07:47,878 --> 05:07:50,180
again
7806
05:07:50,280 --> 05:07:52,080
we still have to Zender this progress
7807
05:07:52,080 --> 05:07:53,700
bar on the screen we will do this in a
7808
05:07:53,700 --> 05:07:54,540
moment
7809
05:07:54,540 --> 05:07:58,260
but let's try throwing an error here
7810
05:07:58,260 --> 05:08:00,360
create HTTP error doesn't really matter
7811
05:08:00,360 --> 05:08:02,100
which one we throw
7812
05:08:02,100 --> 05:08:04,200
refresh this something went wrong
7813
05:08:04,200 --> 05:08:06,660
refresh the pitch and let's also try
7814
05:08:06,660 --> 05:08:10,138
returning an empty array as if we didn't
7815
05:08:10,138 --> 05:08:12,840
have any nodes in our data set
7816
05:08:12,840 --> 05:08:16,040
refresh this again
7817
05:08:16,260 --> 05:08:18,540
you don't have any notes yet
7818
05:08:18,540 --> 05:08:20,638
okay let's not forget to rewrote this
7819
05:08:20,638 --> 05:08:23,160
and now I want to Center everything on
7820
05:08:23,160 --> 05:08:25,200
the screen horizontally
7821
05:08:25,200 --> 05:08:28,080
and our app TSX file now we could use
7822
05:08:28,080 --> 05:08:30,600
our block Center class that we used here
7823
05:08:30,600 --> 05:08:32,520
on our button but then we have to add
7824
05:08:32,520 --> 05:08:34,680
this to each element separately so let's
7825
05:08:34,680 --> 05:08:36,420
actually handle this in one place
7826
05:08:36,420 --> 05:08:38,760
instead by adding a class to the whole
7827
05:08:38,760 --> 05:08:41,520
container of our notes page
7828
05:08:41,520 --> 05:08:45,200
so let's add a class name here
7829
05:08:45,560 --> 05:08:47,878
styles.nodes page we haven't created
7830
05:08:47,878 --> 05:08:49,500
this class yet we will do this in a
7831
05:08:49,500 --> 05:08:53,760
moment and also one on our grid here we
7832
05:08:53,760 --> 05:08:55,560
will need this in a moment so let's
7833
05:08:55,560 --> 05:08:58,798
remember this g-4
7834
05:08:58,798 --> 05:09:02,040
change this again to a g-4 but also add
7835
05:09:02,040 --> 05:09:03,840
the second one
7836
05:09:03,840 --> 05:09:07,680
Styles dot node grid
7837
05:09:07,680 --> 05:09:09,780
or in the closing
7838
05:09:09,780 --> 05:09:12,060
back tick here of the string has to go
7839
05:09:12,060 --> 05:09:13,560
here
7840
05:09:13,560 --> 05:09:16,920
okay let's go into the notes module CSS
7841
05:09:16,920 --> 05:09:19,500
file once again and set up these new
7842
05:09:19,500 --> 05:09:21,900
classes here let's put it here at the
7843
05:09:21,900 --> 05:09:22,980
top
7844
05:09:22,980 --> 05:09:25,798
the first one is the notes pitch
7845
05:09:25,798 --> 05:09:29,580
we want to set display to flex
7846
05:09:29,580 --> 05:09:33,060
and flex direction to column
7847
05:09:33,060 --> 05:09:36,180
because with flex we can use align items
7848
05:09:36,180 --> 05:09:38,160
again and send everything
7849
05:09:38,160 --> 05:09:39,840
yeah which puts everything below each
7850
05:09:39,840 --> 05:09:41,218
other
7851
05:09:41,218 --> 05:09:42,780
which should now also be the case for
7852
05:09:42,780 --> 05:09:44,100
our spinner
7853
05:09:44,100 --> 05:09:45,840
yeah and our error messages and
7854
05:09:45,840 --> 05:09:48,000
everything it's all send out now the
7855
05:09:48,000 --> 05:09:49,798
reason why I also added the second class
7856
05:09:49,798 --> 05:09:52,440
for the node script
7857
05:09:52,440 --> 05:09:55,200
is because if there are now only one or
7858
05:09:55,200 --> 05:09:57,298
two nodes in our array it doesn't fill
7859
05:09:57,298 --> 05:09:59,100
the whole width we can't see this right
7860
05:09:59,100 --> 05:10:00,840
now because we have so many nodes but
7861
05:10:00,840 --> 05:10:02,340
this looks weird when we have fewer
7862
05:10:02,340 --> 05:10:04,740
nodes and we can fix this by setting the
7863
05:10:04,740 --> 05:10:08,218
width of the node squid to 100 so this
7864
05:10:08,218 --> 05:10:09,900
will always take up the full width it
7865
05:10:09,900 --> 05:10:12,240
has available you can check this out by
7866
05:10:12,240 --> 05:10:14,760
just deleting all nodes but one or two
7867
05:10:14,760 --> 05:10:17,638
and try it without this width 100 and
7868
05:10:17,638 --> 05:10:20,520
with it we see a difference okay now we
7869
05:10:20,520 --> 05:10:22,560
already have a nice front end and
7870
05:10:22,560 --> 05:10:24,480
loading States and error States and
7871
05:10:24,480 --> 05:10:26,400
everything we are not quite done yet but
7872
05:10:26,400 --> 05:10:28,138
the next step is to add user
7873
05:10:28,138 --> 05:10:29,760
authentication to the app this will be
7874
05:10:29,760 --> 05:10:31,378
very exciting because in the future
7875
05:10:31,378 --> 05:10:33,480
users have to sign up here in our app
7876
05:10:33,480 --> 05:10:35,760
and then every user will just see the
7877
05:10:35,760 --> 05:10:37,680
nodes that belong to them and also only
7878
05:10:37,680 --> 05:10:39,540
delete and update their own nodes rather
7879
05:10:39,540 --> 05:10:42,240
than everyone being able to change every
7880
05:10:42,240 --> 05:10:45,320
note in our database
7881
05:10:48,780 --> 05:10:51,600
okay when a user signs up on our website
7882
05:10:51,600 --> 05:10:53,400
of course we want to save them in our
7883
05:10:53,400 --> 05:10:55,080
database this means we need another
7884
05:10:55,080 --> 05:10:57,958
Mongoose schema so let's create one but
7885
05:10:57,958 --> 05:10:59,580
first of all I'm gonna close all these
7886
05:10:59,580 --> 05:11:01,020
tabs here
7887
05:11:01,020 --> 05:11:04,020
because I'm losing all overview
7888
05:11:04,020 --> 05:11:07,320
then we go into our back end folder
7889
05:11:07,320 --> 05:11:09,360
into the models folder and we want to
7890
05:11:09,360 --> 05:11:11,820
create another one in here which we call
7891
05:11:11,820 --> 05:11:13,680
user.ts
7892
05:11:13,680 --> 05:11:15,660
this will look similar to what we
7893
05:11:15,660 --> 05:11:19,320
already did here in our note schema
7894
05:11:19,320 --> 05:11:23,820
so rewrite const user schema
7895
05:11:23,820 --> 05:11:26,540
equals new schema
7896
05:11:26,540 --> 05:11:30,420
parentheses curly braces
7897
05:11:30,420 --> 05:11:32,700
put the semicolon here and configure
7898
05:11:32,700 --> 05:11:33,840
this
7899
05:11:33,840 --> 05:11:36,780
every user needs a username
7900
05:11:36,780 --> 05:11:39,240
at least in our app of course you could
7901
05:11:39,240 --> 05:11:41,280
also just use an email address if you
7902
05:11:41,280 --> 05:11:42,958
want
7903
05:11:42,958 --> 05:11:45,360
of type string remember we write this
7904
05:11:45,360 --> 05:11:48,378
type here in uppercase
7905
05:11:48,780 --> 05:11:52,500
and with that required to true
7906
05:11:52,500 --> 05:11:55,620
because the username is not optional and
7907
05:11:55,620 --> 05:11:57,780
we can actually duplicate this line on
7908
05:11:57,780 --> 05:11:59,520
Windows we can do this by holding shift
7909
05:11:59,520 --> 05:12:01,560
and ielt and then pressing the down
7910
05:12:01,560 --> 05:12:03,480
arrow key like this
7911
05:12:03,480 --> 05:12:06,120
because the other two fields are similar
7912
05:12:06,120 --> 05:12:09,000
the second property is an email
7913
05:12:09,000 --> 05:12:11,638
also type string required Journal
7914
05:12:11,638 --> 05:12:13,320
and a password
7915
05:12:13,320 --> 05:12:15,600
but we want to add another setting to
7916
05:12:15,600 --> 05:12:17,400
the email and password configuration
7917
05:12:17,400 --> 05:12:20,878
here after required rule we want to set
7918
05:12:20,878 --> 05:12:24,000
a select column faults for both of them
7919
05:12:24,000 --> 05:12:25,620
so I copy this and add this to the
7920
05:12:25,620 --> 05:12:27,240
password as well
7921
05:12:27,240 --> 05:12:29,700
this means that when we retrieve a user
7922
05:12:29,700 --> 05:12:31,740
from the database by default the email
7923
05:12:31,740 --> 05:12:33,600
and the password will not be returned to
7924
05:12:33,600 --> 05:12:35,700
us we have to request them explicitly
7925
05:12:35,700 --> 05:12:37,740
this makes sense because if you retrieve
7926
05:12:37,740 --> 05:12:40,020
a user for example on a public profile
7927
05:12:40,020 --> 05:12:41,760
then you don't want to return the email
7928
05:12:41,760 --> 05:12:43,680
address and password even though the
7929
05:12:43,680 --> 05:12:45,120
password will be hashed but you don't
7930
05:12:45,120 --> 05:12:46,680
want the stuff to be visible to the
7931
05:12:46,680 --> 05:12:48,718
outside right and even if you don't show
7932
05:12:48,718 --> 05:12:50,700
the user data in the front end it might
7933
05:12:50,700 --> 05:12:52,620
be in the Json response you get from the
7934
05:12:52,620 --> 05:12:54,600
server so with select faults we make
7935
05:12:54,600 --> 05:12:56,280
sure that this is not returned by
7936
05:12:56,280 --> 05:12:58,320
default if we just request a user from
7937
05:12:58,320 --> 05:13:00,240
the database it will only contain the
7938
05:13:00,240 --> 05:13:02,458
username unless we request the email and
7939
05:13:02,458 --> 05:13:04,320
password specifically you will see how
7940
05:13:04,320 --> 05:13:07,020
this works later so let's finish setting
7941
05:13:07,020 --> 05:13:10,320
up our schema we create our type user
7942
05:13:10,320 --> 05:13:11,760
for typescript
7943
05:13:11,760 --> 05:13:14,638
equals in first schema type we have
7944
05:13:14,638 --> 05:13:16,920
already done this before
7945
05:13:16,920 --> 05:13:19,320
as the type parameter we passed type of
7946
05:13:19,320 --> 05:13:23,940
in our lowercase user schema
7947
05:13:23,940 --> 05:13:28,378
and then we add our export default
7948
05:13:28,378 --> 05:13:30,120
model
7949
05:13:30,120 --> 05:13:33,060
add the user as the type parameter here
7950
05:13:33,060 --> 05:13:36,360
parentheses pass user as a string the
7951
05:13:36,360 --> 05:13:38,878
name of the collection later
7952
05:13:38,878 --> 05:13:41,040
comma and the user schema this is
7953
05:13:41,040 --> 05:13:42,840
nothing new for us we've already done
7954
05:13:42,840 --> 05:13:45,480
this in our node schema and then we save
7955
05:13:45,480 --> 05:13:46,980
this
7956
05:13:46,980 --> 05:13:48,900
and next we need endpoints where we
7957
05:13:48,900 --> 05:13:51,120
interact with our user data where we can
7958
05:13:51,120 --> 05:13:53,100
create users by designing up where we
7959
05:13:53,100 --> 05:13:55,798
can log in Fetch user data and so on so
7960
05:13:55,798 --> 05:13:57,718
let's create another controller for this
7961
05:13:57,718 --> 05:13:59,100
stuff
7962
05:13:59,100 --> 05:14:01,260
so now our controllers folder here we
7963
05:14:01,260 --> 05:14:02,638
right click
7964
05:14:02,638 --> 05:14:06,360
and create a users.ts file let's start
7965
05:14:06,360 --> 05:14:08,458
with the sign up endpoint
7966
05:14:08,458 --> 05:14:11,340
so we create an export const which we
7967
05:14:11,340 --> 05:14:13,260
call Design up
7968
05:14:13,260 --> 05:14:16,320
which is of course of type request
7969
05:14:16,320 --> 05:14:18,958
Handler but when we sign up the user we
7970
05:14:18,958 --> 05:14:20,638
have to send some data we have to send
7971
05:14:20,638 --> 05:14:22,560
the username email and password that we
7972
05:14:22,560 --> 05:14:25,440
just required in our user schema
7973
05:14:25,440 --> 05:14:27,360
so let's create an interface for the
7974
05:14:27,360 --> 05:14:29,400
sign up audio
7975
05:14:29,400 --> 05:14:32,100
interface sign up
7976
05:14:32,100 --> 05:14:34,520
body
7977
05:14:35,340 --> 05:14:38,400
we need a username again all of these
7978
05:14:38,400 --> 05:14:40,920
values here are optional because we
7979
05:14:40,920 --> 05:14:42,780
don't know if the use of our endpoint
7980
05:14:42,780 --> 05:14:44,820
actually sends this data so we have to
7981
05:14:44,820 --> 05:14:46,680
be safe
7982
05:14:46,680 --> 05:14:49,560
email of type string
7983
05:14:49,560 --> 05:14:52,860
and a password of type string
7984
05:14:52,860 --> 05:14:54,958
and then we add this type argument down
7985
05:14:54,958 --> 05:14:56,580
here
7986
05:14:56,580 --> 05:14:59,878
unknown comma unknown comma sign up body
7987
05:14:59,878 --> 05:15:02,400
comma unknown
7988
05:15:02,400 --> 05:15:05,760
is in typescript a beautiful language
7989
05:15:05,760 --> 05:15:09,120
and this is of course in async function
7990
05:15:09,120 --> 05:15:12,120
that takes a request object a response
7991
05:15:12,120 --> 05:15:13,138
object
7992
05:15:13,138 --> 05:15:16,020
and the next function
7993
05:15:16,020 --> 05:15:18,600
and then we add the body as usual
7994
05:15:18,600 --> 05:15:20,878
at the start of this callback we want to
7995
05:15:20,878 --> 05:15:23,820
get our data out of design our body so
7996
05:15:23,820 --> 05:15:26,100
we create a const username equals
7997
05:15:26,100 --> 05:15:30,660
request dot body dot username
7998
05:15:30,660 --> 05:15:33,980
the same for the email
7999
05:15:36,540 --> 05:15:38,820
and for the password but I'm gonna call
8000
05:15:38,820 --> 05:15:41,638
this password Raw
8001
05:15:41,638 --> 05:15:44,958
instead of just password
8002
05:15:45,240 --> 05:15:47,638
because for security reasons you should
8003
05:15:47,638 --> 05:15:49,980
never store passwords in plain text in
8004
05:15:49,980 --> 05:15:52,500
your database you need to Hash them we
8005
05:15:52,500 --> 05:15:54,420
will see how this works in a moment and
8006
05:15:54,420 --> 05:15:56,340
to make sure that we don't accidentally
8007
05:15:56,340 --> 05:15:59,160
save this raw password in our database I
8008
05:15:59,160 --> 05:16:01,320
give it this name here so this makes it
8009
05:16:01,320 --> 05:16:03,660
very explicit and makes it less likely
8010
05:16:03,660 --> 05:16:05,400
that we accidentally use it like the
8011
05:16:05,400 --> 05:16:06,840
hashed password
8012
05:16:06,840 --> 05:16:08,878
okay and the rest of this card back here
8013
05:16:08,878 --> 05:16:11,218
happens in the try catch block
8014
05:16:11,218 --> 05:16:13,138
let's call
8015
05:16:13,138 --> 05:16:15,180
our error Handler down here through the
8016
05:16:15,180 --> 05:16:16,378
next function
8017
05:16:16,378 --> 05:16:18,780
and then take care of the try block
8018
05:16:18,780 --> 05:16:20,878
as usual we want to do some input
8019
05:16:20,878 --> 05:16:22,980
validation here so that we get proper
8020
05:16:22,980 --> 05:16:25,320
error messages and not just rely on the
8021
05:16:25,320 --> 05:16:27,240
cryptic error messages of the database
8022
05:16:27,240 --> 05:16:29,520
so first we want to check if any of
8023
05:16:29,520 --> 05:16:32,280
these variables here are missing so we
8024
05:16:32,280 --> 05:16:35,958
type exclamation mark username
8025
05:16:36,180 --> 05:16:39,240
two vertical bars for a logical or
8026
05:16:39,240 --> 05:16:41,700
so if there's no username or if there is
8027
05:16:41,700 --> 05:16:42,900
no email
8028
05:16:42,900 --> 05:16:45,718
or if there is no password wrong in the
8029
05:16:45,718 --> 05:16:47,160
request
8030
05:16:47,160 --> 05:16:49,138
then we want to throw
8031
05:16:49,138 --> 05:16:51,840
a create HTTP error
8032
05:16:51,840 --> 05:16:55,080
with a status code of 400 which means
8033
05:16:55,080 --> 05:16:58,200
bad request which is appropriate here
8034
05:16:58,200 --> 05:17:01,020
and error message was there parameters
8035
05:17:01,020 --> 05:17:03,600
missing
8036
05:17:03,600 --> 05:17:06,060
and we also want to keep the usernames
8037
05:17:06,060 --> 05:17:08,580
and emails unique so that no two users
8038
05:17:08,580 --> 05:17:10,320
with the same name or email address can
8039
05:17:10,320 --> 05:17:12,060
sign up and there's actually another
8040
05:17:12,060 --> 05:17:14,458
schema attribute for that which I forgot
8041
05:17:14,458 --> 05:17:17,218
to add earlier so let's do that now
8042
05:17:17,218 --> 05:17:19,200
for username and email
8043
05:17:19,200 --> 05:17:21,540
we go after the required
8044
05:17:21,540 --> 05:17:24,298
true and by the way you can hold IELTS
8045
05:17:24,298 --> 05:17:26,820
down and click somewhere to create a
8046
05:17:26,820 --> 05:17:29,218
second cursor like this and now we can
8047
05:17:29,218 --> 05:17:31,080
add another attribute to both lines at
8048
05:17:31,080 --> 05:17:32,700
the same time
8049
05:17:32,700 --> 05:17:36,180
let's set unique to true
8050
05:17:36,180 --> 05:17:38,520
which as the name implies make sure that
8051
05:17:38,520 --> 05:17:40,440
we can only ever insert a single user
8052
05:17:40,440 --> 05:17:42,000
with the same username or email address
8053
05:17:42,000 --> 05:17:43,680
into the database
8054
05:17:43,680 --> 05:17:45,600
let's save this and go back to our
8055
05:17:45,600 --> 05:17:48,180
controller as usual we don't want to
8056
05:17:48,180 --> 05:17:49,560
rely on the error message of the
8057
05:17:49,560 --> 05:17:51,600
database we want to insert our own check
8058
05:17:51,600 --> 05:17:53,400
here as well so we have more control
8059
05:17:53,400 --> 05:17:55,500
over the status code and the error
8060
05:17:55,500 --> 05:17:57,718
message that we show to the user so
8061
05:17:57,718 --> 05:17:59,700
let's try to retrieve the user with this
8062
05:17:59,700 --> 05:18:02,040
username out of the database and then if
8063
05:18:02,040 --> 05:18:03,240
there is someone we know that the
8064
05:18:03,240 --> 05:18:05,638
username already exists so we create a
8065
05:18:05,638 --> 05:18:09,718
const existing username
8066
05:18:09,718 --> 05:18:12,660
and cut a weight we have to import the
8067
05:18:12,660 --> 05:18:15,540
user model here
8068
05:18:15,540 --> 05:18:17,540
import
8069
05:18:17,540 --> 05:18:23,100
user model from dot dot slash models
8070
05:18:23,100 --> 05:18:25,378
slash user
8071
05:18:25,378 --> 05:18:28,500
then we continue down here
8072
05:18:28,500 --> 05:18:32,940
user model dot find one
8073
05:18:32,940 --> 05:18:35,400
which as the name implies returns us one
8074
05:18:35,400 --> 05:18:38,040
single document if the filter fits in
8075
05:18:38,040 --> 05:18:39,900
between parentheses we add a pair of
8076
05:18:39,900 --> 05:18:41,400
curly braces
8077
05:18:41,400 --> 05:18:44,520
write username colon this is the field
8078
05:18:44,520 --> 05:18:46,320
in the user schema we are searching for
8079
05:18:46,320 --> 05:18:48,060
and then we want to search for the
8080
05:18:48,060 --> 05:18:49,798
username we passed here so again we
8081
05:18:49,798 --> 05:18:52,980
write username as the value
8082
05:18:52,980 --> 05:18:56,958
and don't forget the exec here
8083
05:18:57,060 --> 05:18:58,860
if there is a username found we know
8084
05:18:58,860 --> 05:19:01,500
that this username already exists So
8085
05:19:01,500 --> 05:19:04,020
Below we do a check if
8086
05:19:04,020 --> 05:19:06,958
existing username
8087
05:19:06,958 --> 05:19:08,820
then we want to throw another create
8088
05:19:08,820 --> 05:19:12,900
HTTP error with status code 409 don't
8089
05:19:12,900 --> 05:19:14,700
remember what is meant right now I think
8090
05:19:14,700 --> 05:19:16,680
something like a clash I looked this up
8091
05:19:16,680 --> 05:19:18,900
before preparing the tutorial so 409
8092
05:19:18,900 --> 05:19:21,060
should be appropriate
8093
05:19:21,060 --> 05:19:23,280
and an error message
8094
05:19:23,280 --> 05:19:25,920
username already taken
8095
05:19:25,920 --> 05:19:30,900
please choose a different one or log in
8096
05:19:30,900 --> 05:19:33,420
instead of course you can write any
8097
05:19:33,420 --> 05:19:36,000
error message here that you want
8098
05:19:36,000 --> 05:19:37,500
then we want to do the same for the
8099
05:19:37,500 --> 05:19:39,540
email address and again we could skip
8100
05:19:39,540 --> 05:19:41,700
this because our user schema already
8101
05:19:41,700 --> 05:19:43,920
enforces the uniqueness of the email
8102
05:19:43,920 --> 05:19:46,260
address and username but this way we
8103
05:19:46,260 --> 05:19:48,240
have better error messages and better
8104
05:19:48,240 --> 05:19:51,600
status codes below let's do the same
8105
05:19:51,600 --> 05:19:53,820
again for the email we create a const
8106
05:19:53,820 --> 05:19:56,540
existing email
8107
05:19:56,540 --> 05:20:01,280
awaits user model dot find one
8108
05:20:02,820 --> 05:20:05,700
email colon email
8109
05:20:05,700 --> 05:20:08,700
exec
8110
05:20:09,540 --> 05:20:13,798
then we check if existing email
8111
05:20:13,798 --> 05:20:17,638
we want to throw another error
8112
05:20:17,638 --> 05:20:20,900
again 409
8113
05:20:21,360 --> 05:20:25,080
the user with this email address already
8114
05:20:25,080 --> 05:20:26,940
exists
8115
05:20:26,940 --> 05:20:31,440
please log in instead
8116
05:20:31,440 --> 05:20:33,240
nice
8117
05:20:33,240 --> 05:20:35,458
so if we get to the point here below
8118
05:20:35,458 --> 05:20:37,860
then this user doesn't exist yet and we
8119
05:20:37,860 --> 05:20:39,780
can go ahead and save it in the database
8120
05:20:39,780 --> 05:20:42,000
now as I already said we don't want to
8121
05:20:42,000 --> 05:20:43,980
save the raw password in the database
8122
05:20:43,980 --> 05:20:46,440
because even though your database is
8123
05:20:46,440 --> 05:20:48,900
locked by a password usually there can
8124
05:20:48,900 --> 05:20:51,240
always be something bad happening and
8125
05:20:51,240 --> 05:20:53,218
your database leaks and then you don't
8126
05:20:53,218 --> 05:20:55,560
want to leak the raw passwords of audio
8127
05:20:55,560 --> 05:20:57,780
users so instead as a process called
8128
05:20:57,780 --> 05:20:59,940
hashing which basically turns this
8129
05:20:59,940 --> 05:21:02,580
password into an unreadable string of
8130
05:21:02,580 --> 05:21:04,378
characters you can Google the exact
8131
05:21:04,378 --> 05:21:07,080
mechanism behind it but it makes it so
8132
05:21:07,080 --> 05:21:09,298
that even if the database leaks no one
8133
05:21:09,298 --> 05:21:10,680
really can do anything with the
8134
05:21:10,680 --> 05:21:13,080
passwords of course there's a package we
8135
05:21:13,080 --> 05:21:15,420
can use for that which is called B Crypt
8136
05:21:15,420 --> 05:21:17,820
it's a very popular one so let's see D
8137
05:21:17,820 --> 05:21:19,440
into our
8138
05:21:19,440 --> 05:21:21,660
backend folder
8139
05:21:21,660 --> 05:21:24,298
and run npm install
8140
05:21:24,298 --> 05:21:27,958
bcrypt which is spelled like this
8141
05:21:27,958 --> 05:21:30,360
and we also need a type package for it
8142
05:21:30,360 --> 05:21:32,580
so we type npm IR
8143
05:21:32,580 --> 05:21:35,820
-d for Dev dependencies
8144
05:21:35,820 --> 05:21:40,138
add types slash bcrypt like this
8145
05:21:40,138 --> 05:21:42,900
and install it as well
8146
05:21:42,900 --> 05:21:45,680
okay and then we can go ahead close this
8147
05:21:45,680 --> 05:21:49,378
and save our user here so we create a
8148
05:21:49,378 --> 05:21:51,780
const for the hashed password I'm going
8149
05:21:51,780 --> 05:21:55,320
to call it password hashed equals
8150
05:21:55,320 --> 05:21:57,600
then we use bcrip to Hash our raw
8151
05:21:57,600 --> 05:21:59,760
password and this is an asynchronous
8152
05:21:59,760 --> 05:22:02,700
operations area write a rate
8153
05:22:02,700 --> 05:22:05,878
bcrypt dot hash
8154
05:22:05,878 --> 05:22:07,500
which is a function
8155
05:22:07,500 --> 05:22:10,260
but we have to import bcrypt first so up
8156
05:22:10,260 --> 05:22:11,280
here
8157
05:22:11,280 --> 05:22:17,240
import B Crypt from the bcrypt package
8158
05:22:17,520 --> 05:22:19,580
the first argument is the raw password
8159
05:22:19,580 --> 05:22:22,138
and the second argument is the resulting
8160
05:22:22,138 --> 05:22:23,160
rounds
8161
05:22:23,160 --> 05:22:26,160
salting is another security mechanism to
8162
05:22:26,160 --> 05:22:28,440
make it harder to a reverse engineer
8163
05:22:28,440 --> 05:22:30,480
these hash passwords because there is
8164
05:22:30,480 --> 05:22:32,400
actually a way to figure out a real
8165
05:22:32,400 --> 05:22:34,560
password from a hashed one if it's a
8166
05:22:34,560 --> 05:22:36,000
commonly used one through something
8167
05:22:36,000 --> 05:22:38,280
called rainbow tables and sorting is
8168
05:22:38,280 --> 05:22:40,138
another security mechanism that makes
8169
05:22:40,138 --> 05:22:42,660
this yeah impossible basically so if you
8170
05:22:42,660 --> 05:22:44,218
write it like this then your passwords
8171
05:22:44,218 --> 05:22:46,680
will be saved and then we can save this
8172
05:22:46,680 --> 05:22:49,378
data as a new user in the database
8173
05:22:49,378 --> 05:22:52,440
so let's create a cons new user because
8174
05:22:52,440 --> 05:22:54,780
after signing up successfully we want to
8175
05:22:54,780 --> 05:22:56,218
return the user to the front end
8176
05:22:56,218 --> 05:22:57,600
immediately
8177
05:22:57,600 --> 05:23:00,420
equates await user model
8178
05:23:00,420 --> 05:23:02,940
dot create
8179
05:23:02,940 --> 05:23:06,540
parentheses calibrases
8180
05:23:06,540 --> 05:23:09,480
we store the username that we are sent
8181
05:23:09,480 --> 05:23:11,280
to the endpoint
8182
05:23:11,280 --> 05:23:14,580
we store the email as it is
8183
05:23:14,580 --> 05:23:18,958
and we store our password hashed not a
8184
05:23:18,958 --> 05:23:21,378
raw one
8185
05:23:22,740 --> 05:23:26,580
and then Ria sent a status back with the
8186
05:23:26,580 --> 05:23:28,440
code 201
8187
05:23:28,440 --> 05:23:31,200
and the Json body will contain this
8188
05:23:31,200 --> 05:23:34,320
newer user we just created
8189
05:23:34,320 --> 05:23:36,480
next we have to set up the route to this
8190
05:23:36,480 --> 05:23:38,340
controller right so we create another
8191
05:23:38,340 --> 05:23:41,940
file in our roads folder
8192
05:23:41,940 --> 05:23:45,718
which we also call users.ts
8193
05:23:45,718 --> 05:23:47,820
so we need a router here
8194
05:23:47,820 --> 05:23:51,298
so first we import Express
8195
05:23:51,298 --> 05:23:54,420
from the Express package
8196
05:23:54,420 --> 05:23:56,700
we also want to import our user
8197
05:23:56,700 --> 05:24:01,320
controller import Star as user
8198
05:24:01,320 --> 05:24:03,360
controller
8199
05:24:03,360 --> 05:24:04,740
from
8200
05:24:04,740 --> 05:24:10,860
dot dot slash controllers slash users
8201
05:24:11,340 --> 05:24:14,160
and then we create a router constrada
8202
05:24:14,160 --> 05:24:18,740
equals Express dot router
8203
05:24:19,440 --> 05:24:22,620
and then below we create our first
8204
05:24:22,620 --> 05:24:25,378
endpoint for our users controller which
8205
05:24:25,378 --> 05:24:27,718
will be a post request because present
8206
05:24:27,718 --> 05:24:31,798
data to our sign up endpoint
8207
05:24:31,798 --> 05:24:35,520
the URL is slash design up
8208
05:24:35,520 --> 05:24:37,680
and we want to forward this to user
8209
05:24:37,680 --> 05:24:41,820
controller DOT sign up
8210
05:24:41,820 --> 05:24:44,638
don't forget to add a default export
8211
05:24:44,638 --> 05:24:46,020
below
8212
05:24:46,020 --> 05:24:49,440
export this router save this
8213
05:24:49,440 --> 05:24:51,298
and then we have to register this new
8214
05:24:51,298 --> 05:24:53,878
route in our app TS file so let's go
8215
05:24:53,878 --> 05:24:56,160
over there the same as we did for our
8216
05:24:56,160 --> 05:24:59,820
notes routes we need our user roads so
8217
05:24:59,820 --> 05:25:00,840
import
8218
05:25:00,840 --> 05:25:03,958
user routes from
8219
05:25:03,958 --> 05:25:07,260
routes slash users
8220
05:25:07,260 --> 05:25:10,620
and then we added down here either above
8221
05:25:10,620 --> 05:25:12,660
or below it doesn't really matter put it
8222
05:25:12,660 --> 05:25:14,160
above
8223
05:25:14,160 --> 05:25:19,260
app.use slash API slash users
8224
05:25:19,260 --> 05:25:21,000
comma
8225
05:25:21,000 --> 05:25:24,060
and our user routes
8226
05:25:24,060 --> 05:25:26,040
okay and now we should be able to reach
8227
05:25:26,040 --> 05:25:27,900
our sign up endpoint
8228
05:25:27,900 --> 05:25:30,060
since we don't have a sign up form yet
8229
05:25:30,060 --> 05:25:34,020
let's try it out in Postman first so we
8230
05:25:34,020 --> 05:25:37,020
want to do a request to our server slash
8231
05:25:37,020 --> 05:25:40,080
API slash users
8232
05:25:40,080 --> 05:25:44,100
slash sign up and we have to pass a body
8233
05:25:44,100 --> 05:25:46,378
here because if we try to send it like
8234
05:25:46,378 --> 05:25:48,480
this it saves parameters missing which
8235
05:25:48,480 --> 05:25:51,958
is our own error message we set up
8236
05:25:51,958 --> 05:25:55,138
so we need a username
8237
05:25:55,138 --> 05:25:58,320
gonna give it my name
8238
05:25:58,320 --> 05:26:00,718
needs an email address
8239
05:26:00,718 --> 05:26:03,900
let's set this to a
8240
05:26:03,900 --> 05:26:08,060
Florian at test.com
8241
05:26:08,458 --> 05:26:11,458
and we need a password
8242
05:26:11,458 --> 05:26:14,878
I use my favorite password which is one
8243
05:26:14,878 --> 05:26:16,680
two three four five
8244
05:26:16,680 --> 05:26:18,560
let's send this and see what happens
8245
05:26:18,560 --> 05:26:21,180
okay so it looks like we got the user
8246
05:26:21,180 --> 05:26:23,400
back this is our hashed password Here by
8247
05:26:23,400 --> 05:26:25,260
the way let's also take a look into the
8248
05:26:25,260 --> 05:26:27,660
database
8249
05:26:27,660 --> 05:26:29,700
so we have our new users collection
8250
05:26:29,700 --> 05:26:32,638
again this takes the name of the schema
8251
05:26:32,638 --> 05:26:35,040
which is user and pluralizes it
8252
05:26:35,040 --> 05:26:37,020
and our data is in here this is our
8253
05:26:37,020 --> 05:26:39,298
hashed and soldered password now let's
8254
05:26:39,298 --> 05:26:42,000
try signing up with the same email or
8255
05:26:42,000 --> 05:26:43,798
username again
8256
05:26:43,798 --> 05:26:45,840
which shows us our error message here
8257
05:26:45,840 --> 05:26:48,480
let's also see if the unique key
8258
05:26:48,480 --> 05:26:52,020
actually works by removing our own error
8259
05:26:52,020 --> 05:26:54,298
for a moment so let's remove these
8260
05:26:54,298 --> 05:26:56,760
checks for existing email and existing
8261
05:26:56,760 --> 05:26:58,740
username just for a moment I'm going to
8262
05:26:58,740 --> 05:27:01,200
comment this out save this
8263
05:27:01,200 --> 05:27:03,240
try this again this should still not
8264
05:27:03,240 --> 05:27:04,860
work
8265
05:27:04,860 --> 05:27:07,080
an unknown error occurred and in our
8266
05:27:07,080 --> 05:27:09,540
logs we should see our mongodb error
8267
05:27:09,540 --> 05:27:11,638
here duplicate key error collection this
8268
05:27:11,638 --> 05:27:13,138
is what we want to see
8269
05:27:13,138 --> 05:27:15,600
so this is basically a fallback security
8270
05:27:15,600 --> 05:27:18,360
mechanism that happens because we are
8271
05:27:18,360 --> 05:27:20,940
that is unique key is here in our user
8272
05:27:20,940 --> 05:27:22,200
schema
8273
05:27:22,200 --> 05:27:24,120
so let's just go to have these two lines
8274
05:27:24,120 --> 05:27:26,218
of defenses because of course we can
8275
05:27:26,218 --> 05:27:28,560
always mess up our own server code here
8276
05:27:28,560 --> 05:27:30,958
so it's good if the database itself also
8277
05:27:30,958 --> 05:27:33,480
enforces this constraint and of course
8278
05:27:33,480 --> 05:27:35,638
if we use a different username by the
8279
05:27:35,638 --> 05:27:37,500
same email address we should still see
8280
05:27:37,500 --> 05:27:39,420
an error here a user with this email
8281
05:27:39,420 --> 05:27:41,458
address already exists please log in
8282
05:27:41,458 --> 05:27:43,560
instead nice and we will later show this
8283
05:27:43,560 --> 05:27:45,718
error message in our front end in our
8284
05:27:45,718 --> 05:27:48,120
react app but of course inserting the
8285
05:27:48,120 --> 05:27:50,280
user in the database is not enough we
8286
05:27:50,280 --> 05:27:51,900
also have to keep the user locked in
8287
05:27:51,900 --> 05:27:54,000
somehow right and we want to do this
8288
05:27:54,000 --> 05:27:56,218
right after a user signs up because why
8289
05:27:56,218 --> 05:27:57,540
should they insert their login
8290
05:27:57,540 --> 05:27:59,520
credentials again if they just enter
8291
05:27:59,520 --> 05:28:01,020
their own password
8292
05:28:01,020 --> 05:28:03,360
for user authentication we will use
8293
05:28:03,360 --> 05:28:05,700
sessions through this Express session
8294
05:28:05,700 --> 05:28:07,620
package here there are basically two
8295
05:28:07,620 --> 05:28:09,660
popular ways of keeping a user logged in
8296
05:28:09,660 --> 05:28:13,798
either sessions or JWT tokens JWT tokens
8297
05:28:13,798 --> 05:28:15,900
are used in a lot of tutorials but they
8298
05:28:15,900 --> 05:28:17,780
are actually quite hard to use because
8299
05:28:17,780 --> 05:28:21,240
JWT tokens are self-contained which
8300
05:28:21,240 --> 05:28:23,520
means that once a user has it they can
8301
05:28:23,520 --> 05:28:25,680
always log in with it there is no way to
8302
05:28:25,680 --> 05:28:29,218
invalidate an existing JWT token but in
8303
05:28:29,218 --> 05:28:31,500
validating a session after user signed
8304
05:28:31,500 --> 05:28:33,120
in is important for example when they
8305
05:28:33,120 --> 05:28:34,798
want to change their password then they
8306
05:28:34,798 --> 05:28:36,420
should be locked out in all the other
8307
05:28:36,420 --> 05:28:38,160
places right because maybe their
8308
05:28:38,160 --> 05:28:40,440
password has been compromised this is a
8309
05:28:40,440 --> 05:28:42,840
problem that this self-contained token
8310
05:28:42,840 --> 05:28:44,940
spring and the usual way to handle this
8311
05:28:44,940 --> 05:28:46,440
is to keep these tokens will be
8312
05:28:46,440 --> 05:28:48,718
short-lived usually one hour and then
8313
05:28:48,718 --> 05:28:50,580
they have to be refreshed before they
8314
05:28:50,580 --> 05:28:52,320
can be used again this way when you
8315
05:28:52,320 --> 05:28:54,540
change the password then the user Can
8316
05:28:54,540 --> 05:28:56,280
Only log in for the rest of that hour
8317
05:28:56,280 --> 05:28:58,560
until the token expires but then you
8318
05:28:58,560 --> 05:29:00,240
also have to implement a refresher
8319
05:29:00,240 --> 05:29:02,280
mechanism which can be quite complicated
8320
05:29:02,280 --> 05:29:04,500
or another way to handle this is to keep
8321
05:29:04,500 --> 05:29:07,260
the tokens long-lived but Implement some
8322
05:29:07,260 --> 05:29:09,060
information in your database some kind
8323
05:29:09,060 --> 05:29:11,218
of Blacklist where you enter tokens that
8324
05:29:11,218 --> 05:29:12,840
have been expired but then you're
8325
05:29:12,840 --> 05:29:14,760
basically just rebuilding sessions in a
8326
05:29:14,760 --> 05:29:17,340
sub-optimal way what you must not do is
8327
05:29:17,340 --> 05:29:19,920
giving a user a token that lives for
8328
05:29:19,920 --> 05:29:22,200
like a week or a month and then you have
8329
05:29:22,200 --> 05:29:24,000
no way to invalidate it that's a
8330
05:29:24,000 --> 05:29:26,100
security issue but if you have your own
8331
05:29:26,100 --> 05:29:28,320
server that can store data then I
8332
05:29:28,320 --> 05:29:30,060
actually prefer just using sessions
8333
05:29:30,060 --> 05:29:31,798
because they are much easier to use
8334
05:29:31,798 --> 05:29:34,980
possession works so that a user has some
8335
05:29:34,980 --> 05:29:37,378
kind of key stored in a cookie and there
8336
05:29:37,378 --> 05:29:39,120
is a corresponding entry in your own
8337
05:29:39,120 --> 05:29:41,218
database on your server for each session
8338
05:29:41,218 --> 05:29:43,378
and then when you need to invalidate the
8339
05:29:43,378 --> 05:29:45,420
sessions for a particular user then it's
8340
05:29:45,420 --> 05:29:47,520
just a matter of deleting this database
8341
05:29:47,520 --> 05:29:49,378
entry and that's it these session
8342
05:29:49,378 --> 05:29:51,060
cookies are not self-contained as
8343
05:29:51,060 --> 05:29:53,700
opposed to what JWT tokens
8344
05:29:53,700 --> 05:29:55,560
so yeah this is in my opinion much
8345
05:29:55,560 --> 05:29:57,360
easier to implement you will see how
8346
05:29:57,360 --> 05:29:59,160
easy this is in a moment let's install
8347
05:29:59,160 --> 05:30:01,500
this package now so we need this install
8348
05:30:01,500 --> 05:30:05,958
command here go into our terminal
8349
05:30:06,600 --> 05:30:08,520
cancel the execution for the back end
8350
05:30:08,520 --> 05:30:10,798
for a moment and install Express session
8351
05:30:10,798 --> 05:30:12,600
here
8352
05:30:12,600 --> 05:30:14,580
we also need the types for Express
8353
05:30:14,580 --> 05:30:19,440
session again so npm IO Dash Dio
8354
05:30:19,440 --> 05:30:24,000
add types slash Express session
8355
05:30:24,000 --> 05:30:26,218
install this as well
8356
05:30:26,218 --> 05:30:29,100
and we need one more thing our session
8357
05:30:29,100 --> 05:30:31,138
information has to be stored somewhere
8358
05:30:31,138 --> 05:30:33,298
and there are different adapters that
8359
05:30:33,298 --> 05:30:34,798
you can use for that they are listed
8360
05:30:34,798 --> 05:30:36,900
somewhere down here there are different
8361
05:30:36,900 --> 05:30:40,080
ones we are going to use the mongodb one
8362
05:30:40,080 --> 05:30:42,240
so we store the session in our existing
8363
05:30:42,240 --> 05:30:45,000
Atlas mongodb should be this one here
8364
05:30:45,000 --> 05:30:47,700
connect Bongo in a real app I actually
8365
05:30:47,700 --> 05:30:49,378
recommend that you use something like
8366
05:30:49,378 --> 05:30:52,560
Reddit because our mongodb database is a
8367
05:30:52,560 --> 05:30:54,660
remote database it's not on our server
8368
05:30:54,660 --> 05:30:56,700
directly and it's on atlas's server
8369
05:30:56,700 --> 05:30:58,798
right this means that it takes a moment
8370
05:30:58,798 --> 05:31:00,540
to store this information in the
8371
05:31:00,540 --> 05:31:02,160
database if you want something that's
8372
05:31:02,160 --> 05:31:04,200
really fast you can just install a
8373
05:31:04,200 --> 05:31:06,540
radius database on your own server and
8374
05:31:06,540 --> 05:31:08,340
then this is just a bit faster but
8375
05:31:08,340 --> 05:31:10,680
setting up Reddit for development is a
8376
05:31:10,680 --> 05:31:12,840
bit complicated so for the ease of this
8377
05:31:12,840 --> 05:31:14,760
tutorial we will just use the mongodb
8378
05:31:14,760 --> 05:31:16,378
one because we already have this
8379
05:31:16,378 --> 05:31:18,480
database set up and you can also use
8380
05:31:18,480 --> 05:31:21,000
connect in your production app you
8381
05:31:21,000 --> 05:31:22,860
don't have to use the Reddit one it's
8382
05:31:22,860 --> 05:31:24,840
just a little bit faster
8383
05:31:24,840 --> 05:31:27,718
so this is also an npm package
8384
05:31:27,718 --> 05:31:29,760
and we want to install this here as well
8385
05:31:29,760 --> 05:31:33,180
npm install connect Mongol
8386
05:31:33,180 --> 05:31:35,580
let's do that
8387
05:31:35,580 --> 05:31:37,320
and then let's start the server again
8388
05:31:37,320 --> 05:31:39,000
because we have installed all
8389
05:31:39,000 --> 05:31:41,160
dependencies we need for now and then
8390
05:31:41,160 --> 05:31:43,378
when a user has signed up successfully
8391
05:31:43,378 --> 05:31:46,020
before we return the response we want to
8392
05:31:46,020 --> 05:31:47,878
establish the session and this is really
8393
05:31:47,878 --> 05:31:50,700
easy with this package we just take our
8394
05:31:50,700 --> 05:31:52,440
request object
8395
05:31:52,440 --> 05:31:55,440
on which we now have the session keyer
8396
05:31:55,440 --> 05:31:57,840
and here we can store some data and what
8397
05:31:57,840 --> 05:31:59,580
do we want to store we want to store the
8398
05:31:59,580 --> 05:32:01,680
ID of the user that just signed up
8399
05:32:01,680 --> 05:32:04,560
because this way we can identify a user
8400
05:32:04,560 --> 05:32:08,100
so we store this user ID property here
8401
05:32:08,100 --> 05:32:10,860
and we set it to the ID of the new user
8402
05:32:10,860 --> 05:32:14,940
we just created right so underscore idea
8403
05:32:14,940 --> 05:32:16,620
in normal JavaScript this would be
8404
05:32:16,620 --> 05:32:18,120
enough but as usual typescript
8405
05:32:18,120 --> 05:32:19,980
complaints because it doesn't know about
8406
05:32:19,980 --> 05:32:22,260
this user ID field here on the session
8407
05:32:22,260 --> 05:32:24,900
object because we decide ourselves how
8408
05:32:24,900 --> 05:32:27,060
this is called so we have to do a little
8409
05:32:27,060 --> 05:32:29,638
bit of extra configuration what we need
8410
05:32:29,638 --> 05:32:31,320
to do is we need to create another
8411
05:32:31,320 --> 05:32:34,740
folder here in our backend folder not in
8412
05:32:34,740 --> 05:32:36,600
that Source but right at the root folder
8413
05:32:36,600 --> 05:32:38,458
because this is not our normal source
8414
05:32:38,458 --> 05:32:39,540
code
8415
05:32:39,540 --> 05:32:41,878
which we call it types and in here we
8416
05:32:41,878 --> 05:32:43,620
can put stuff like our own type
8417
05:32:43,620 --> 05:32:45,780
definitions that we sometimes need and
8418
05:32:45,780 --> 05:32:47,520
again I figure this out through a Google
8419
05:32:47,520 --> 05:32:49,500
and documentation
8420
05:32:49,500 --> 05:32:51,480
you always find these Solutions in
8421
05:32:51,480 --> 05:32:52,860
Google somewhere
8422
05:32:52,860 --> 05:32:55,020
and here we put a new file
8423
05:32:55,020 --> 05:32:58,980
which we call session dot deal dot TS
8424
05:32:58,980 --> 05:33:01,378
and these DTS files are these type
8425
05:33:01,378 --> 05:33:03,780
definition files which help typescript
8426
05:33:03,780 --> 05:33:06,660
recognize variables and stuff like that
8427
05:33:06,660 --> 05:33:09,420
and whenever we install these add types
8428
05:33:09,420 --> 05:33:11,700
slash packages then we are installing
8429
05:33:11,700 --> 05:33:14,280
DTS files basically just that they are
8430
05:33:14,280 --> 05:33:16,620
residing inside the node modules folder
8431
05:33:16,620 --> 05:33:19,020
okay and here we will need Mongoose
8432
05:33:19,020 --> 05:33:21,298
because we want to store the object idea
8433
05:33:21,298 --> 05:33:23,638
which is a mongoose type so we import
8434
05:33:23,638 --> 05:33:26,100
Mongoose from mongoose
8435
05:33:26,100 --> 05:33:30,958
and then below we write declare module
8436
05:33:30,958 --> 05:33:34,080
the same name that this package has
8437
05:33:34,080 --> 05:33:35,878
where we want to add this type tool so
8438
05:33:35,878 --> 05:33:38,040
this is Express session
8439
05:33:38,040 --> 05:33:39,840
curly braces and now we can basically
8440
05:33:39,840 --> 05:33:42,840
add our user ID variable to the existing
8441
05:33:42,840 --> 05:33:45,000
Express session type
8442
05:33:45,000 --> 05:33:48,740
by writing interface in here
8443
05:33:50,100 --> 05:33:52,440
we call it session data and you have to
8444
05:33:52,440 --> 05:33:54,360
use this exact name because this is how
8445
05:33:54,360 --> 05:33:56,520
the type on Express session is called
8446
05:33:56,520 --> 05:33:59,100
then we add another pair of curly braces
8447
05:33:59,100 --> 05:34:02,100
and now we can add our user ID field
8448
05:34:02,100 --> 05:34:05,580
by declaring its type so Mongoose dot
8449
05:34:05,580 --> 05:34:09,298
types dot object idea
8450
05:34:09,298 --> 05:34:11,878
so that's it for this file
8451
05:34:11,878 --> 05:34:13,740
we have to make a little change to our
8452
05:34:13,740 --> 05:34:16,378
TS config file as well so that this is
8453
05:34:16,378 --> 05:34:18,360
actually used here because otherwise
8454
05:34:18,360 --> 05:34:20,638
this will not work properly so we go
8455
05:34:20,638 --> 05:34:23,580
into the TS config file
8456
05:34:23,580 --> 05:34:26,638
and we search for the key called type
8457
05:34:26,638 --> 05:34:29,100
Roots which is down here
8458
05:34:29,100 --> 05:34:30,900
we uncomment this
8459
05:34:30,900 --> 05:34:33,060
here we tell typescript where the type
8460
05:34:33,060 --> 05:34:35,958
definitions are
8461
05:34:36,060 --> 05:34:38,638
by default this should point to node
8462
05:34:38,638 --> 05:34:40,980
modules types but now that we set this
8463
05:34:40,980 --> 05:34:42,420
explicitly we have to enter this
8464
05:34:42,420 --> 05:34:43,740
ourselves
8465
05:34:43,740 --> 05:34:46,200
so in here we put a string
8466
05:34:46,200 --> 05:34:48,600
node underscore modules
8467
05:34:48,600 --> 05:34:51,480
slash add types
8468
05:34:51,480 --> 05:34:53,100
comma and then we end the second one
8469
05:34:53,100 --> 05:34:56,040
which is our own types folder now so we
8470
05:34:56,040 --> 05:34:58,440
just type add types which points to this
8471
05:34:58,440 --> 05:35:00,120
one here we just created
8472
05:35:00,120 --> 05:35:02,580
and this now tells typescripts that it
8473
05:35:02,580 --> 05:35:04,740
can find type definitions in both places
8474
05:35:04,740 --> 05:35:06,900
the node modules folder which are the
8475
05:35:06,900 --> 05:35:09,540
ones that we installed and our own one
8476
05:35:09,540 --> 05:35:11,940
and one more thing we have to add all
8477
05:35:11,940 --> 05:35:13,798
the way at the bottom because this
8478
05:35:13,798 --> 05:35:16,080
happens outside of this closing curly
8479
05:35:16,080 --> 05:35:17,580
brace here so there should be two
8480
05:35:17,580 --> 05:35:19,620
closing curly braces down here
8481
05:35:19,620 --> 05:35:22,980
we go before the last one at the comma
8482
05:35:22,980 --> 05:35:26,100
and adds this TS node key which is
8483
05:35:26,100 --> 05:35:28,620
necessary for TS node to recognize these
8484
05:35:28,620 --> 05:35:31,080
types remember TS node is what we use to
8485
05:35:31,080 --> 05:35:33,180
automatically restart our server in
8486
05:35:33,180 --> 05:35:36,180
development whenever we save the changes
8487
05:35:36,180 --> 05:35:38,760
and here we add the silence key
8488
05:35:38,760 --> 05:35:41,520
and that is the true and again I found
8489
05:35:41,520 --> 05:35:43,560
this through a stack Overflow because
8490
05:35:43,560 --> 05:35:45,240
the session key didn't work then I
8491
05:35:45,240 --> 05:35:46,680
looked this up and then you find all
8492
05:35:46,680 --> 05:35:48,780
these necessary steps there
8493
05:35:48,780 --> 05:35:50,760
okay we save this
8494
05:35:50,760 --> 05:35:53,280
let's not forget to save the changes to
8495
05:35:53,280 --> 05:35:55,740
our users controller and then we have to
8496
05:35:55,740 --> 05:35:58,080
go into the FTS file once again because
8497
05:35:58,080 --> 05:36:00,120
we still have to configure our Express
8498
05:36:00,120 --> 05:36:02,340
session package here but this is just a
8499
05:36:02,340 --> 05:36:03,900
little bit of code and then we can use
8500
05:36:03,900 --> 05:36:06,958
our sessions throughout our whole server
8501
05:36:06,958 --> 05:36:08,760
the Press session is another middleware
8502
05:36:08,760 --> 05:36:11,400
that we have to register here but the
8503
05:36:11,400 --> 05:36:13,080
place is important we want to register
8504
05:36:13,080 --> 05:36:16,020
them before our routes but after we read
8505
05:36:16,020 --> 05:36:18,600
the Json body so right here we write
8506
05:36:18,600 --> 05:36:21,180
app.use
8507
05:36:21,180 --> 05:36:23,878
and here we want to call session
8508
05:36:23,878 --> 05:36:26,040
okay Auto Import doesn't work so we have
8509
05:36:26,040 --> 05:36:28,440
to import it manually
8510
05:36:28,440 --> 05:36:34,200
import session from Express session
8511
05:36:34,200 --> 05:36:37,260
which is a function that we can call
8512
05:36:37,260 --> 05:36:39,718
and add a pair of curly braces to do a
8513
05:36:39,718 --> 05:36:42,980
configuration in here
8514
05:36:43,378 --> 05:36:45,600
the first thing we have to set is a
8515
05:36:45,600 --> 05:36:48,540
secret this liquid is used to assign the
8516
05:36:48,540 --> 05:36:50,820
cookie that the user receives so this is
8517
05:36:50,820 --> 05:36:53,340
basically their key to identify their
8518
05:36:53,340 --> 05:36:55,378
own user session so each session has an
8519
05:36:55,378 --> 05:36:57,540
entry in the database but also a cookie
8520
05:36:57,540 --> 05:36:59,638
with a secret key is stored on the side
8521
05:36:59,638 --> 05:37:01,378
of the user in their browser there
8522
05:37:01,378 --> 05:37:03,420
should be a random string now as usual
8523
05:37:03,420 --> 05:37:05,638
we don't want to hard code sensitive
8524
05:37:05,638 --> 05:37:07,798
data right into our code we want to put
8525
05:37:07,798 --> 05:37:09,900
it into the nth file
8526
05:37:09,900 --> 05:37:13,080
so we open our backend and file
8527
05:37:13,080 --> 05:37:15,360
and add another entry in here which
8528
05:37:15,360 --> 05:37:18,240
requires session secret the name is up
8529
05:37:18,240 --> 05:37:19,138
to you
8530
05:37:19,138 --> 05:37:22,440
and then we just set this to a random
8531
05:37:22,440 --> 05:37:24,980
string
8532
05:37:25,020 --> 05:37:27,360
you can type in whatever you want
8533
05:37:27,360 --> 05:37:29,540
just don't use any reserved characters
8534
05:37:29,540 --> 05:37:32,160
this should suffice but this has to be
8535
05:37:32,160 --> 05:37:35,160
an equal sign not a column I always mix
8536
05:37:35,160 --> 05:37:37,200
this up
8537
05:37:37,200 --> 05:37:40,200
okay and we also need to add this to our
8538
05:37:40,200 --> 05:37:43,080
sanitized and file right remember we
8539
05:37:43,080 --> 05:37:46,740
have this validate and f file here
8540
05:37:46,740 --> 05:37:48,900
where we want to registered this new key
8541
05:37:48,900 --> 05:37:51,060
as well so that we make sure that we
8542
05:37:51,060 --> 05:37:53,280
actually have this available at runtime
8543
05:37:53,280 --> 05:37:56,160
this is a string
8544
05:37:56,160 --> 05:37:59,580
let's go back into the app TS file and
8545
05:37:59,580 --> 05:38:02,520
import our validate and Theorem so
8546
05:38:02,520 --> 05:38:04,798
import enf
8547
05:38:04,798 --> 05:38:06,200
R from
8548
05:38:06,200 --> 05:38:11,458
dot slash uter slash validate INF
8549
05:38:11,458 --> 05:38:14,160
o and then down here for the secret
8550
05:38:14,160 --> 05:38:18,860
we pass nth dot session Secret
8551
05:38:19,560 --> 05:38:22,980
then we set Reserve to false
8552
05:38:22,980 --> 05:38:26,400
entry that save uninitialized to false
8553
05:38:26,400 --> 05:38:28,980
as well this is some configuration that
8554
05:38:28,980 --> 05:38:30,540
is necessary that I don't want to
8555
05:38:30,540 --> 05:38:32,340
explain here you can hover over this or
8556
05:38:32,340 --> 05:38:34,378
look into the documentation and it tells
8557
05:38:34,378 --> 05:38:36,180
you what this means it's just not
8558
05:38:36,180 --> 05:38:37,440
important here
8559
05:38:37,440 --> 05:38:39,360
then we need to configure the cookie
8560
05:38:39,360 --> 05:38:41,458
that will be stored on the user's
8561
05:38:41,458 --> 05:38:42,540
browser
8562
05:38:42,540 --> 05:38:45,840
so cookie colon curly braces
8563
05:38:45,840 --> 05:38:48,240
entry set the max Edge how long this
8564
05:38:48,240 --> 05:38:51,120
cookie lives this is up to you again you
8565
05:38:51,120 --> 05:38:52,980
can make this long lift because we can
8566
05:38:52,980 --> 05:38:55,320
invalidate sessions from our side even
8567
05:38:55,320 --> 05:38:57,780
if the cookie hasn't expired yet I'm
8568
05:38:57,780 --> 05:39:00,120
gonna set this to one hour here you can
8569
05:39:00,120 --> 05:39:02,700
also make this longer and I later also
8570
05:39:02,700 --> 05:39:04,560
want to try out what happens when this
8571
05:39:04,560 --> 05:39:07,620
cookie expires but that's something we
8572
05:39:07,620 --> 05:39:09,600
want to do later but the cool thing we
8573
05:39:09,600 --> 05:39:11,520
can do is we can set rolling to true
8574
05:39:11,520 --> 05:39:14,218
this means as long as the user is using
8575
05:39:14,218 --> 05:39:16,320
our website this cookie will be
8576
05:39:16,320 --> 05:39:18,420
refreshed automatically so if the user
8577
05:39:18,420 --> 05:39:20,458
enters the website within this one hour
8578
05:39:20,458 --> 05:39:22,440
they will stay signed in and of course
8579
05:39:22,440 --> 05:39:24,000
if you say this to a week for example
8580
05:39:24,000 --> 05:39:25,980
then they will stay signed in if they
8581
05:39:25,980 --> 05:39:28,138
use this website within a week which I
8582
05:39:28,138 --> 05:39:29,820
think is really cool because if a user
8583
05:39:29,820 --> 05:39:31,680
doesn't use your website I think it's a
8584
05:39:31,680 --> 05:39:33,298
good idea to lock them out eventually
8585
05:39:33,298 --> 05:39:36,718
and lastly we need to set a store
8586
05:39:36,718 --> 05:39:39,060
store is where the session data will be
8587
05:39:39,060 --> 05:39:41,100
stored if you don't pass anything here
8588
05:39:41,100 --> 05:39:42,718
the session data will be stored in
8589
05:39:42,718 --> 05:39:44,700
memory so it will work until you restart
8590
05:39:44,700 --> 05:39:46,560
your server then your sessions will be
8591
05:39:46,560 --> 05:39:48,180
gone this can be enough for development
8592
05:39:48,180 --> 05:39:50,160
but definitely not for production but
8593
05:39:50,160 --> 05:39:51,600
also for development it's good to
8594
05:39:51,600 --> 05:39:53,040
actually store the session data
8595
05:39:53,040 --> 05:39:54,540
somewhere and for this we want to use
8596
05:39:54,540 --> 05:39:57,298
our store that we installed
8597
05:39:57,298 --> 05:39:59,400
so we have to import this here import
8598
05:39:59,400 --> 05:40:01,500
store or the completion takes care
8599
05:40:01,500 --> 05:40:03,360
of the rest
8600
05:40:03,360 --> 05:40:05,940
and then we want to use it down here
8601
05:40:05,940 --> 05:40:08,940
bongostore.create
8602
05:40:09,320 --> 05:40:12,718
parentheses curly braces
8603
05:40:12,718 --> 05:40:14,940
and in here we can pass a URL
8604
05:40:14,940 --> 05:40:17,400
which is just the same URL that we also
8605
05:40:17,400 --> 05:40:18,900
use for the rest of our database
8606
05:40:18,900 --> 05:40:20,940
connection which we stored in the
8607
05:40:20,940 --> 05:40:23,280
environment variables in this
8608
05:40:23,280 --> 05:40:25,740
connection string
8609
05:40:25,740 --> 05:40:27,540
let's save this
8610
05:40:27,540 --> 05:40:29,820
and now we could create a new user and
8611
05:40:29,820 --> 05:40:31,798
see how this cookie is stored we can
8612
05:40:31,798 --> 05:40:33,660
actually see this in Postman but I don't
8613
05:40:33,660 --> 05:40:35,520
want to create a yet another user so
8614
05:40:35,520 --> 05:40:37,440
let's implement the lock and Route first
8615
05:40:37,440 --> 05:40:40,320
and use that one that so we go into the
8616
05:40:40,320 --> 05:40:42,718
user's controller
8617
05:40:42,718 --> 05:40:45,240
and Below sign up we export another
8618
05:40:45,240 --> 05:40:47,340
function here
8619
05:40:47,340 --> 05:40:49,860
export cons login
8620
05:40:49,860 --> 05:40:52,560
of type request Handler
8621
05:40:52,560 --> 05:40:54,420
it needs a body so we create an
8622
05:40:54,420 --> 05:40:55,620
interface
8623
05:40:55,620 --> 05:40:58,200
this is second nature for us by now
8624
05:40:58,200 --> 05:41:00,180
lock and body
8625
05:41:00,180 --> 05:41:03,060
we send a username
8626
05:41:03,060 --> 05:41:05,458
and a password
8627
05:41:05,458 --> 05:41:07,620
it's hard to type and talk at the same
8628
05:41:07,620 --> 05:41:09,000
time
8629
05:41:09,000 --> 05:41:11,218
we don't need to send an email this time
8630
05:41:11,218 --> 05:41:13,080
because we are not creating a newer user
8631
05:41:13,080 --> 05:41:14,760
account we are just signing into an
8632
05:41:14,760 --> 05:41:17,100
existing one
8633
05:41:17,100 --> 05:41:21,240
down here unknown comma unknown comma
8634
05:41:21,240 --> 05:41:24,780
login body comma unknown
8635
05:41:24,780 --> 05:41:26,700
and if I'm too fast for you then you
8636
05:41:26,700 --> 05:41:28,440
just have to step up your game come on
8637
05:41:28,440 --> 05:41:33,560
man type faster async Reckless next
8638
05:41:34,798 --> 05:41:39,020
and we create our error function
8639
05:41:39,420 --> 05:41:41,700
okay I'm slowing down a bit
8640
05:41:41,700 --> 05:41:43,798
we want to extract the input from the
8641
05:41:43,798 --> 05:41:45,120
body
8642
05:41:45,120 --> 05:41:48,958
so a const username equals rack dot body
8643
05:41:48,958 --> 05:41:52,280
dot username
8644
05:41:52,500 --> 05:41:56,000
and design for the password
8645
05:41:58,260 --> 05:42:01,020
and then we add our good old friend the
8646
05:42:01,020 --> 05:42:03,360
try catch block
8647
05:42:03,360 --> 05:42:05,580
and call our next function down here
8648
05:42:05,580 --> 05:42:09,240
which we have to do a every single time
8649
05:42:09,240 --> 05:42:11,100
in the try block we want to do some
8650
05:42:11,100 --> 05:42:13,560
input validation
8651
05:42:13,560 --> 05:42:16,740
if the username is missing or if the
8652
05:42:16,740 --> 05:42:19,760
password is missing
8653
05:42:20,160 --> 05:42:23,520
we throw a create HTTP error
8654
05:42:23,520 --> 05:42:26,580
status quo 400
8655
05:42:26,580 --> 05:42:30,920
error message parameters missing
8656
05:42:32,638 --> 05:42:34,798
now before we can check if the passwords
8657
05:42:34,798 --> 05:42:36,540
match we first have to figure out if
8658
05:42:36,540 --> 05:42:38,520
there's actually a user with these
8659
05:42:38,520 --> 05:42:40,560
credentials with this username in our
8660
05:42:40,560 --> 05:42:41,940
database
8661
05:42:41,940 --> 05:42:44,700
so we create a const user
8662
05:42:44,700 --> 05:42:49,320
to an await user model find one
8663
05:42:49,320 --> 05:42:52,878
for the username we entered
8664
05:42:54,660 --> 05:42:56,700
and since the user is signing in we want
8665
05:42:56,700 --> 05:42:58,138
to send them back all their data
8666
05:42:58,138 --> 05:43:00,420
including their own email address and
8667
05:43:00,420 --> 05:43:02,940
remember we set select to false by
8668
05:43:02,940 --> 05:43:04,680
default so they will not be included
8669
05:43:04,680 --> 05:43:07,080
here when we call find one but we can
8670
05:43:07,080 --> 05:43:10,260
include them explicitly by adding dot
8671
05:43:10,260 --> 05:43:11,660
select
8672
05:43:11,660 --> 05:43:14,040
parentheses and then we pass a string
8673
05:43:14,040 --> 05:43:16,138
here and in the string we write a plus
8674
05:43:16,138 --> 05:43:17,820
and then the name of the field we want
8675
05:43:17,820 --> 05:43:20,638
to include so plus password then is
8676
05:43:20,638 --> 05:43:22,798
space plus email
8677
05:43:22,798 --> 05:43:26,400
and then outside we append our exec
8678
05:43:26,400 --> 05:43:29,040
this adds the password and email to this
8679
05:43:29,040 --> 05:43:31,138
find one request which is necessary
8680
05:43:31,138 --> 05:43:34,020
because by default we exclude them
8681
05:43:34,020 --> 05:43:36,298
okay so if the user doesn't exist we
8682
05:43:36,298 --> 05:43:39,420
want to throw another arrow so if
8683
05:43:39,420 --> 05:43:42,000
exclamation mark user
8684
05:43:42,000 --> 05:43:45,900
we want to throw a create HTTP error
8685
05:43:45,900 --> 05:43:48,780
the response will be 401 which means
8686
05:43:48,780 --> 05:43:51,600
unauthorized
8687
05:43:51,600 --> 05:43:54,180
and the error message was a invalid
8688
05:43:54,180 --> 05:43:55,920
credentials
8689
05:43:55,920 --> 05:43:58,620
now I keep this error here generic on
8690
05:43:58,620 --> 05:44:01,138
purpose I don't say this user doesn't
8691
05:44:01,138 --> 05:44:03,660
exist you can do it if you want but for
8692
05:44:03,660 --> 05:44:06,058
security it's a good idea tool don't
8693
05:44:06,058 --> 05:44:08,100
tell whoever is trying to log in that
8694
05:44:08,100 --> 05:44:09,958
this user doesn't exist because it makes
8695
05:44:09,958 --> 05:44:11,940
Brute Force attempts more easy right
8696
05:44:11,940 --> 05:44:14,458
this way when a user types in a username
8697
05:44:14,458 --> 05:44:16,500
that doesn't exist they don't actually
8698
05:44:16,500 --> 05:44:18,718
know if this user doesn't exist or if
8699
05:44:18,718 --> 05:44:20,580
the password is just wrong but this is
8700
05:44:20,580 --> 05:44:22,200
up to you you can implement it this way
8701
05:44:22,200 --> 05:44:24,420
you can also tell an error message that
8702
05:44:24,420 --> 05:44:27,120
this username doesn't exist but if we
8703
05:44:27,120 --> 05:44:28,798
found the user we can go ahead and
8704
05:44:28,798 --> 05:44:31,620
compare the passwords so we create a
8705
05:44:31,620 --> 05:44:34,080
const password match
8706
05:44:34,080 --> 05:44:36,958
equals a weight and then we have another
8707
05:44:36,958 --> 05:44:40,500
b-crypt function that we can use compare
8708
05:44:40,500 --> 05:44:42,840
which can take the raw password which we
8709
05:44:42,840 --> 05:44:45,180
are sending over the login body and it
8710
05:44:45,180 --> 05:44:48,180
can compare it to the hashed password of
8711
05:44:48,180 --> 05:44:50,280
the user from the database so even
8712
05:44:50,280 --> 05:44:52,080
though this password Here is hashed and
8713
05:44:52,080 --> 05:44:54,000
this one is raw decrypt knows how to
8714
05:44:54,000 --> 05:44:55,558
compare them and tell us if they
8715
05:44:55,558 --> 05:44:57,120
actually match this is the cool thing
8716
05:44:57,120 --> 05:44:59,040
about this Library it hides all this
8717
05:44:59,040 --> 05:45:02,218
complexity from us okay so below we
8718
05:45:02,218 --> 05:45:05,400
check if exclamation mark password match
8719
05:45:05,400 --> 05:45:07,378
so this is a Boolean that we are getting
8720
05:45:07,378 --> 05:45:08,340
back
8721
05:45:08,340 --> 05:45:11,100
if this is false the passwords don't
8722
05:45:11,100 --> 05:45:12,298
match
8723
05:45:12,298 --> 05:45:14,878
we want to throw another error we
8724
05:45:14,878 --> 05:45:16,920
actually reuse this one here in valid
8725
05:45:16,920 --> 05:45:18,240
credentials
8726
05:45:18,240 --> 05:45:20,580
if we get below this if block then
8727
05:45:20,580 --> 05:45:23,100
everything went well the user exists the
8728
05:45:23,100 --> 05:45:25,320
passwords match then we can establish a
8729
05:45:25,320 --> 05:45:26,160
session
8730
05:45:26,160 --> 05:45:28,860
which again we do a with request
8731
05:45:28,860 --> 05:45:30,900
dot session
8732
05:45:30,900 --> 05:45:33,420
we set the user idea
8733
05:45:33,420 --> 05:45:35,760
to the ID of the user but with the
8734
05:45:35,760 --> 05:45:37,378
underscore
8735
05:45:37,378 --> 05:45:39,840
that we just compare the passwords with
8736
05:45:39,840 --> 05:45:42,540
and lastly we want to return this user
8737
05:45:42,540 --> 05:45:45,058
to the front end with a status code of
8738
05:45:45,058 --> 05:45:46,680
201
8739
05:45:46,680 --> 05:45:50,100
and we send the user as the Json body
8740
05:45:50,100 --> 05:45:52,558
so we can use it in our front end right
8741
05:45:52,558 --> 05:45:53,400
away
8742
05:45:53,400 --> 05:45:55,740
let's save this and register this new
8743
05:45:55,740 --> 05:45:58,200
route in our user routes file
8744
05:45:58,200 --> 05:46:00,360
So Below sign up we create another
8745
05:46:00,360 --> 05:46:03,180
user.post
8746
05:46:03,180 --> 05:46:06,058
slash login and we forward this to
8747
05:46:06,058 --> 05:46:08,878
usercontroller.login
8748
05:46:08,878 --> 05:46:11,820
and then let's try this out in Postman
8749
05:46:11,820 --> 05:46:15,298
so I opened Postman change the endpoint
8750
05:46:15,298 --> 05:46:17,340
here to slash login
8751
05:46:17,340 --> 05:46:20,760
we don't need the email address anymore
8752
05:46:20,760 --> 05:46:23,340
let's remove the password for now to see
8753
05:46:23,340 --> 05:46:25,020
what happens
8754
05:46:25,020 --> 05:46:26,340
um yeah we also have to remove this
8755
05:46:26,340 --> 05:46:28,138
comma here
8756
05:46:28,138 --> 05:46:30,298
parameters missing let's add the
8757
05:46:30,298 --> 05:46:34,340
password back in but let's make it wrong
8758
05:46:36,360 --> 05:46:38,940
invalid credentials
8759
05:46:38,940 --> 05:46:42,120
let's make the username wrong
8760
05:46:42,120 --> 05:46:44,280
invalid credentials
8761
05:46:44,280 --> 05:46:46,020
and now let's use the correct
8762
05:46:46,020 --> 05:46:47,280
credentials
8763
05:46:47,280 --> 05:46:49,920
okay and we are getting a user back not
8764
05:46:49,920 --> 05:46:51,660
only that we are also getting a cookie
8765
05:46:51,660 --> 05:46:52,378
back
8766
05:46:52,378 --> 05:46:55,558
this connect.s idea is the session from
8767
05:46:55,558 --> 05:46:57,540
the express session package we just set
8768
05:46:57,540 --> 05:47:00,000
up and Postman actually stores these
8769
05:47:00,000 --> 05:47:01,860
cookies just like a real browser would
8770
05:47:01,860 --> 05:47:05,100
so you can see it's stored here
8771
05:47:05,100 --> 05:47:07,680
as the key is stored in there and when
8772
05:47:07,680 --> 05:47:10,980
we look into our mongodb database
8773
05:47:10,980 --> 05:47:13,200
we will actually find another collection
8774
05:47:13,200 --> 05:47:15,780
now for the sessions this is what the
8775
05:47:15,780 --> 05:47:18,058
Bongo store did for us so this is the
8776
05:47:18,058 --> 05:47:19,740
session for our user that's stored in
8777
05:47:19,740 --> 05:47:22,138
our database with this x bios keyer and
8778
05:47:22,138 --> 05:47:24,240
now we can force the user to log out by
8779
05:47:24,240 --> 05:47:26,280
simply removing the session here but
8780
05:47:26,280 --> 05:47:28,378
before we see how this works we'll have
8781
05:47:28,378 --> 05:47:30,240
to add another endpoint where we can
8782
05:47:30,240 --> 05:47:32,340
retrieve the user data of the currently
8783
05:47:32,340 --> 05:47:34,798
locked in user so that we see that when
8784
05:47:34,798 --> 05:47:36,900
we delete the session this will actually
8785
05:47:36,900 --> 05:47:39,058
not work anymore so let's go back into
8786
05:47:39,058 --> 05:47:41,280
our code and into our users controller
8787
05:47:41,280 --> 05:47:42,420
once again
8788
05:47:42,420 --> 05:47:44,400
and all the way at the top here before
8789
05:47:44,400 --> 05:47:46,920
designer body I want to add another end
8790
05:47:46,920 --> 05:47:49,860
point which I'm gonna call
8791
05:47:49,860 --> 05:47:53,638
get authenticated user
8792
05:47:53,638 --> 05:47:56,100
so here we get our user data from if we
8793
05:47:56,100 --> 05:47:57,900
are logged in
8794
05:47:57,900 --> 05:47:59,700
it's a request Handler it doesn't take
8795
05:47:59,700 --> 05:48:02,340
any body or anything so we don't have to
8796
05:48:02,340 --> 05:48:05,100
declare any types
8797
05:48:05,100 --> 05:48:10,100
and as usual it's an async request next
8798
05:48:12,298 --> 05:48:14,520
and here we want to get the currently
8799
05:48:14,520 --> 05:48:16,740
locked end user out of the session which
8800
05:48:16,740 --> 05:48:20,180
is very simple we can write const
8801
05:48:20,180 --> 05:48:23,218
authenticated user and set it to
8802
05:48:23,218 --> 05:48:25,378
rec.session
8803
05:48:25,378 --> 05:48:28,760
dot user idea
8804
05:48:29,040 --> 05:48:30,840
if there is a user currently logged in
8805
05:48:30,840 --> 05:48:33,480
then this will have the ID of this user
8806
05:48:33,480 --> 05:48:35,160
and if there is no user logged in it
8807
05:48:35,160 --> 05:48:37,860
will be undefined and we know that there
8808
05:48:37,860 --> 05:48:40,260
is no login session right now
8809
05:48:40,260 --> 05:48:42,718
so in the try catch block here let's add
8810
05:48:42,718 --> 05:48:46,020
the next call First
8811
05:48:46,020 --> 05:48:48,540
we can check if there is no
8812
05:48:48,540 --> 05:48:51,660
authenticated user
8813
05:48:51,660 --> 05:48:53,340
then we could either just not return
8814
05:48:53,340 --> 05:48:55,378
anything from the database but let's
8815
05:48:55,378 --> 05:48:59,458
actually throw an error here so throw
8816
05:48:59,458 --> 05:49:03,120
create HTTP error 401 for not
8817
05:49:03,120 --> 05:49:05,280
authenticated
8818
05:49:05,280 --> 05:49:07,260
viewers are not
8819
05:49:07,260 --> 05:49:10,160
authenticated
8820
05:49:11,700 --> 05:49:13,798
if there is an authenticated user we can
8821
05:49:13,798 --> 05:49:15,600
return it here and we can call this
8822
05:49:15,600 --> 05:49:17,400
endpoint for example later when we open
8823
05:49:17,400 --> 05:49:19,558
our website so we open the website and
8824
05:49:19,558 --> 05:49:21,000
of course we don't want to log in every
8825
05:49:21,000 --> 05:49:22,920
time instead we want to see if there is
8826
05:49:22,920 --> 05:49:24,900
a session that exists and the key that's
8827
05:49:24,900 --> 05:49:26,638
stored in the session cookie and then we
8828
05:49:26,638 --> 05:49:28,920
can just return the user right array So
8829
05:49:28,920 --> 05:49:31,260
Below we want to retrieve the locked end
8830
05:49:31,260 --> 05:49:33,480
user from the database
8831
05:49:33,480 --> 05:49:36,740
so const user equals await
8832
05:49:36,740 --> 05:49:39,480
usermodel dot find this time we can use
8833
05:49:39,480 --> 05:49:41,700
find by ID because we have the idea
8834
05:49:41,700 --> 05:49:44,340
stored in the session so between
8835
05:49:44,340 --> 05:49:48,058
parentheses we pass authenticated user I
8836
05:49:48,058 --> 05:49:49,080
actually want to rename this to
8837
05:49:49,080 --> 05:49:51,120
authenticated user idea
8838
05:49:51,120 --> 05:49:54,920
so rename this one up here as well
8839
05:49:55,798 --> 05:49:57,480
we don't need to return the password
8840
05:49:57,480 --> 05:49:59,458
here because the user doesn't need their
8841
05:49:59,458 --> 05:50:01,320
own password on the front end but they
8842
05:50:01,320 --> 05:50:02,638
probably need their own email address
8843
05:50:02,638 --> 05:50:05,040
for example to show it in the user
8844
05:50:05,040 --> 05:50:06,958
profile
8845
05:50:06,958 --> 05:50:09,840
so we add a select
8846
05:50:09,840 --> 05:50:12,480
plus email
8847
05:50:12,480 --> 05:50:14,820
and add exec
8848
05:50:14,820 --> 05:50:17,400
and then we want to return the user with
8849
05:50:17,400 --> 05:50:20,580
a status code of 200
8850
05:50:20,580 --> 05:50:24,660
and we send the user and the Json body
8851
05:50:24,660 --> 05:50:29,540
save this then set up a route for it
8852
05:50:30,298 --> 05:50:33,600
router dot get
8853
05:50:33,600 --> 05:50:35,760
slash we do it right at the user
8854
05:50:35,760 --> 05:50:37,740
endpoint
8855
05:50:37,740 --> 05:50:39,840
he was a controller dot get
8856
05:50:39,840 --> 05:50:41,700
authenticated user
8857
05:50:41,700 --> 05:50:43,798
save this as well and then let's try it
8858
05:50:43,798 --> 05:50:45,540
out in Postman
8859
05:50:45,540 --> 05:50:48,360
Postman
8860
05:50:48,360 --> 05:50:52,200
get request two API users now we should
8861
05:50:52,200 --> 05:50:53,878
be getting a user because we have the
8862
05:50:53,878 --> 05:50:56,400
session cookies stored
8863
05:50:56,400 --> 05:50:58,980
let's look in the audio there it is our
8864
05:50:58,980 --> 05:51:00,900
user with our email address
8865
05:51:00,900 --> 05:51:03,180
let's delete our cookie that we have
8866
05:51:03,180 --> 05:51:05,100
stored here then the session should not
8867
05:51:05,100 --> 05:51:08,218
work anymore use are not authenticated
8868
05:51:08,218 --> 05:51:12,000
let's log in again
8869
05:51:12,000 --> 05:51:15,558
of course that's a post
8870
05:51:16,558 --> 05:51:19,320
change this back to get but before I
8871
05:51:19,320 --> 05:51:21,058
execute this
8872
05:51:21,058 --> 05:51:23,580
I wanna delete the session from the
8873
05:51:23,580 --> 05:51:25,940
database
8874
05:51:26,280 --> 05:51:28,558
so we have two now because one for the
8875
05:51:28,558 --> 05:51:30,000
designer one for the login but the
8876
05:51:30,000 --> 05:51:32,458
expire after a while so they don't keep
8877
05:51:32,458 --> 05:51:34,440
stacking up in the database after they
8878
05:51:34,440 --> 05:51:35,760
expire they will be deleted
8879
05:51:35,760 --> 05:51:37,860
automatically
8880
05:51:37,860 --> 05:51:40,200
but now we delete them manually
8881
05:51:40,200 --> 05:51:43,500
and no decision should also be invalid
8882
05:51:43,500 --> 05:51:46,500
even though the cookie is still stored
8883
05:51:46,500 --> 05:51:48,540
yours are not authenticated so this is
8884
05:51:48,540 --> 05:51:50,280
how sessions work there are two pieces
8885
05:51:50,280 --> 05:51:51,780
there are the cookies which are
8886
05:51:51,780 --> 05:51:53,940
basically the keys to your session and
8887
05:51:53,940 --> 05:51:55,920
the session entry in the database and by
8888
05:51:55,920 --> 05:51:57,900
deleting a session from the database you
8889
05:51:57,900 --> 05:52:00,058
can force the user to log out but of
8890
05:52:00,058 --> 05:52:01,860
course the user should also have a way
8891
05:52:01,860 --> 05:52:04,138
to log out themselves without us having
8892
05:52:04,138 --> 05:52:06,120
to delete them from the database so
8893
05:52:06,120 --> 05:52:08,218
let's add another route for that
8894
05:52:08,218 --> 05:52:11,400
let's add the route itself right array
8895
05:52:11,400 --> 05:52:14,160
and then set it up afterwards so
8896
05:52:14,160 --> 05:52:16,378
router.post
8897
05:52:16,378 --> 05:52:18,958
slash logout
8898
05:52:18,958 --> 05:52:20,760
entry will forward this to user
8899
05:52:20,760 --> 05:52:23,340
controller dot logout which we haven't
8900
05:52:23,340 --> 05:52:26,700
created yet but we will do this next
8901
05:52:26,700 --> 05:52:29,160
now you could also make logout a get
8902
05:52:29,160 --> 05:52:31,620
endpoint because we are not sending any
8903
05:52:31,620 --> 05:52:33,660
body to the server but I think it's more
8904
05:52:33,660 --> 05:52:35,878
correct to make this a post because we
8905
05:52:35,878 --> 05:52:38,580
are changing something on our side on
8906
05:52:38,580 --> 05:52:39,840
the back end side there are different
8907
05:52:39,840 --> 05:52:42,180
opinions about it but I think post is
8908
05:52:42,180 --> 05:52:44,760
appropriate so let's go into the user's
8909
05:52:44,760 --> 05:52:48,860
controller let's put it at the bottom
8910
05:52:49,520 --> 05:52:53,520
export const log out
8911
05:52:53,520 --> 05:52:56,520
request Handler
8912
05:52:56,520 --> 05:53:00,320
red rest next
8913
05:53:02,040 --> 05:53:04,378
and then we have this rack dot session
8914
05:53:04,378 --> 05:53:06,600
dot destroy function we can call to
8915
05:53:06,600 --> 05:53:08,700
destroy a session now this doesn't
8916
05:53:08,700 --> 05:53:11,638
return a promise so we can't use a
8917
05:53:11,638 --> 05:53:13,620
weight and try catch in here instead we
8918
05:53:13,620 --> 05:53:15,420
have to use a callback here but that's
8919
05:53:15,420 --> 05:53:18,000
fine it's not a lot of code anyway so
8920
05:53:18,000 --> 05:53:20,218
destroy attempts to destroy the session
8921
05:53:20,218 --> 05:53:22,440
if there's anything that goes wrong it
8922
05:53:22,440 --> 05:53:24,360
will pass us an error in this callback
8923
05:53:24,360 --> 05:53:26,580
so we add error
8924
05:53:26,580 --> 05:53:29,458
a right arrow and the function body here
8925
05:53:29,458 --> 05:53:31,080
on curly braces
8926
05:53:31,080 --> 05:53:35,040
we check if the error exists
8927
05:53:35,040 --> 05:53:37,138
then we want to forward it to our error
8928
05:53:37,138 --> 05:53:39,180
Handler as usual
8929
05:53:39,180 --> 05:53:42,240
and if the error does not exist we just
8930
05:53:42,240 --> 05:53:43,620
send
8931
05:53:43,620 --> 05:53:46,558
a status code of 200 to indicate that
8932
05:53:46,558 --> 05:53:49,320
the logout when successful and again
8933
05:53:49,320 --> 05:53:51,600
since we don't send a Json body you have
8934
05:53:51,600 --> 05:53:55,440
to use Zen status instead of just status
8935
05:53:55,440 --> 05:53:57,540
okay and that's our last authentication
8936
05:53:57,540 --> 05:53:59,458
route we need let's try it out in
8937
05:53:59,458 --> 05:54:01,440
Postman once again
8938
05:54:01,440 --> 05:54:04,500
let's log in first
8939
05:54:04,500 --> 05:54:07,500
slash user slash login
8940
05:54:07,500 --> 05:54:11,280
there's our session let's try retrieving
8941
05:54:11,280 --> 05:54:13,558
the locked in user
8942
05:54:13,558 --> 05:54:15,718
works as well
8943
05:54:15,718 --> 05:54:18,480
let's log out
8944
05:54:18,480 --> 05:54:21,240
we got our status of 200
8945
05:54:21,240 --> 05:54:24,240
and let's try to retrieve the user again
8946
05:54:24,240 --> 05:54:25,920
oops
8947
05:54:25,920 --> 05:54:27,780
and doesn't work because we are not
8948
05:54:27,780 --> 05:54:30,000
authenticated anymore
8949
05:54:30,000 --> 05:54:32,340
and there should be no session in the
8950
05:54:32,340 --> 05:54:34,620
database yeah the session is gone as
8951
05:54:34,620 --> 05:54:35,400
well
8952
05:54:35,400 --> 05:54:37,320
okay so we just implemented
8953
05:54:37,320 --> 05:54:39,360
authentication into our app using
8954
05:54:39,360 --> 05:54:41,040
Express session if you found this
8955
05:54:41,040 --> 05:54:42,600
helpful please leave a like on this
8956
05:54:42,600 --> 05:54:45,000
video and the next step is to use these
8957
05:54:45,000 --> 05:54:47,160
new endpoints on our front end so that
8958
05:54:47,160 --> 05:54:49,920
we can actually sign in and log in and
8959
05:54:49,920 --> 05:54:52,320
retrieve our user data in our react app
8960
05:54:52,320 --> 05:54:55,458
rather than in Postman
8961
05:54:59,100 --> 05:55:01,320
alright we will need a model for our
8962
05:55:01,320 --> 05:55:04,378
user on our front end as well right so
8963
05:55:04,378 --> 05:55:08,160
let's close all these tabs here for now
8964
05:55:08,160 --> 05:55:11,940
and go into our front-end code
8965
05:55:11,940 --> 05:55:15,840
here front end Source models and here we
8966
05:55:15,840 --> 05:55:17,520
want to add a new file
8967
05:55:17,520 --> 05:55:20,400
which we call user.ts
8968
05:55:20,400 --> 05:55:22,980
that's just a simple interface
8969
05:55:22,980 --> 05:55:25,378
that we want to export
8970
05:55:25,378 --> 05:55:28,080
which we call user so that we have a
8971
05:55:28,080 --> 05:55:30,240
type that we can work with
8972
05:55:30,240 --> 05:55:33,900
it will contain the username
8973
05:55:33,900 --> 05:55:36,240
and the email of the user
8974
05:55:36,240 --> 05:55:38,160
we don't need a password here because
8975
05:55:38,160 --> 05:55:40,740
there's no reason why we need this in
8976
05:55:40,740 --> 05:55:42,718
any way in our front end we probably
8977
05:55:42,718 --> 05:55:45,180
wanted this by the username somewhere
8978
05:55:45,180 --> 05:55:47,340
and maybe the email address so the user
8979
05:55:47,340 --> 05:55:48,900
can see it and change it but we don't
8980
05:55:48,900 --> 05:55:51,240
want to do anything with the password on
8981
05:55:51,240 --> 05:55:53,700
the front end now let's add all the
8982
05:55:53,700 --> 05:55:56,280
endpoints that we need to our notes API
8983
05:55:56,280 --> 05:55:58,798
file the endpoints for retrieving user
8984
05:55:58,798 --> 05:56:00,900
data are logging in signing up and so on
8985
05:56:00,900 --> 05:56:03,420
I'm going to put this all the way at the
8986
05:56:03,420 --> 05:56:07,080
top here but below fetch data right here
8987
05:56:07,080 --> 05:56:09,958
and you can also create different files
8988
05:56:09,958 --> 05:56:12,180
for the API endpoints for the nodes and
8989
05:56:12,180 --> 05:56:14,520
for users and other stuff but since they
8990
05:56:14,520 --> 05:56:16,138
aren't too many right now I'm gonna put
8991
05:56:16,138 --> 05:56:18,900
them into the same file Zone the first
8992
05:56:18,900 --> 05:56:22,218
one is export
8993
05:56:22,340 --> 05:56:24,840
async functional
8994
05:56:24,840 --> 05:56:28,500
get locked in user
8995
05:56:28,500 --> 05:56:31,320
I think the name is self-explaining
8996
05:56:31,320 --> 05:56:33,958
it will return a promise
8997
05:56:33,958 --> 05:56:35,820
with our user type here which we have to
8998
05:56:35,820 --> 05:56:38,100
import properly
8999
05:56:38,100 --> 05:56:41,240
in here we write const response equals
9000
05:56:41,240 --> 05:56:44,878
await fetch data
9001
05:56:44,878 --> 05:56:51,798
the end point is a slash API slash users
9002
05:56:51,900 --> 05:56:57,138
and this will be a a get request
9003
05:56:57,240 --> 05:56:59,940
now since both the front end and the
9004
05:56:59,940 --> 05:57:02,160
back end are on the same URL this will
9005
05:57:02,160 --> 05:57:03,780
actually send the cookies to the back
9006
05:57:03,780 --> 05:57:05,878
end automatically so we don't have to do
9007
05:57:05,878 --> 05:57:07,980
anything special in here we will either
9008
05:57:07,980 --> 05:57:10,558
get the user if we are locked in or we
9009
05:57:10,558 --> 05:57:13,620
will get a 401 response if we aren't
9010
05:57:13,620 --> 05:57:17,040
so here we can simply return the Json
9011
05:57:17,040 --> 05:57:19,798
body of the response which will contain
9012
05:57:19,798 --> 05:57:22,378
the user data if we are logged in this
9013
05:57:22,378 --> 05:57:24,180
works since the front end and the back
9014
05:57:24,180 --> 05:57:26,700
end are on the same URL if they are on
9015
05:57:26,700 --> 05:57:29,280
different domains or subdomains then you
9016
05:57:29,280 --> 05:57:30,958
have to include the credentials
9017
05:57:30,958 --> 05:57:33,360
explicitly which you do in the fetch
9018
05:57:33,360 --> 05:57:35,160
configuration here so there's a
9019
05:57:35,160 --> 05:57:36,900
credentials include property for that
9020
05:57:36,900 --> 05:57:38,520
but you have to Google it because I
9021
05:57:38,520 --> 05:57:40,500
don't remember the exact Syntax for that
9022
05:57:40,500 --> 05:57:42,240
right now but you have to add this to
9023
05:57:42,240 --> 05:57:44,520
the fetch call but our front end and
9024
05:57:44,520 --> 05:57:46,620
back end are on the same URL so the
9025
05:57:46,620 --> 05:57:49,138
cookies represent automatically but we
9026
05:57:49,138 --> 05:57:51,420
also need endpoints for signing up
9027
05:57:51,420 --> 05:57:54,420
logging in and logging out so for sign
9028
05:57:54,420 --> 05:57:56,940
up we will need some input so let's
9029
05:57:56,940 --> 05:57:59,580
create another interface for that
9030
05:57:59,580 --> 05:58:02,058
which requires design up credentials
9031
05:58:02,058 --> 05:58:04,500
similar to how we have created this node
9032
05:58:04,500 --> 05:58:07,020
input interface earlier we will also use
9033
05:58:07,020 --> 05:58:10,138
this in our sign up form later
9034
05:58:10,138 --> 05:58:12,958
design up credentials consist of a
9035
05:58:12,958 --> 05:58:15,120
username
9036
05:58:15,120 --> 05:58:17,700
the email
9037
05:58:17,700 --> 05:58:20,480
and the password
9038
05:58:21,000 --> 05:58:24,660
and then we export another async
9039
05:58:24,660 --> 05:58:26,280
function
9040
05:58:26,280 --> 05:58:29,718
that we call Design up
9041
05:58:29,760 --> 05:58:31,980
and this will take these credentials
9042
05:58:31,980 --> 05:58:36,000
design up credentials as input
9043
05:58:36,000 --> 05:58:38,638
and it will also return a promise of
9044
05:58:38,638 --> 05:58:40,980
type user because after signing up rear
9045
05:58:40,980 --> 05:58:44,760
return the sign up user right away okay
9046
05:58:44,760 --> 05:58:48,058
const response equals
9047
05:58:48,058 --> 05:58:51,000
await fetch data
9048
05:58:51,000 --> 05:58:56,400
to a slash API slash users slash sign up
9049
05:58:56,400 --> 05:59:00,440
comma and some configuration
9050
05:59:01,080 --> 05:59:05,040
the method will be posed
9051
05:59:05,040 --> 05:59:07,500
we send the Json body so we need our
9052
05:59:07,500 --> 05:59:10,280
Json header here so let's copy this
9053
05:59:10,280 --> 05:59:13,680
insert it here
9054
05:59:13,680 --> 05:59:15,840
and the body itself
9055
05:59:15,840 --> 05:59:19,320
for which we stringify
9056
05:59:19,320 --> 05:59:24,298
our not create nodes but credentials
9057
05:59:24,298 --> 05:59:26,700
which we pass to this function
9058
05:59:26,700 --> 05:59:28,798
and then if everything went successful
9059
05:59:28,798 --> 05:59:32,820
we want to return response.json
9060
05:59:32,878 --> 05:59:34,860
which is the data of the newly designed
9061
05:59:34,860 --> 05:59:36,240
app user
9062
05:59:36,240 --> 05:59:38,480
we need another function for logging in
9063
05:59:38,480 --> 05:59:42,000
again we need the credentials for which
9064
05:59:42,000 --> 05:59:44,040
we create another interface
9065
05:59:44,040 --> 05:59:46,260
lock in credentials
9066
05:59:46,260 --> 05:59:48,420
because those only consist of the
9067
05:59:48,420 --> 05:59:50,160
username and the password
9068
05:59:50,160 --> 05:59:53,218
we don't need to send the email again
9069
05:59:53,218 --> 05:59:56,520
so we create this interface
9070
05:59:56,520 --> 05:59:59,580
and another function
9071
05:59:59,580 --> 06:00:02,660
which we call login
9072
06:00:04,440 --> 06:00:06,660
it takes the log in
9073
06:00:06,660 --> 06:00:08,638
credentials
9074
06:00:08,638 --> 06:00:11,340
and it also returns a promise
9075
06:00:11,340 --> 06:00:13,320
of type user
9076
06:00:13,320 --> 06:00:16,200
let's actually copy this from design up
9077
06:00:16,200 --> 06:00:20,040
function because it's very similar
9078
06:00:20,040 --> 06:00:22,320
just that we make the request to the
9079
06:00:22,320 --> 06:00:25,200
login endpoint and the rest is the same
9080
06:00:25,200 --> 06:00:27,298
we still send the credentials we still
9081
06:00:27,298 --> 06:00:30,120
get the user back
9082
06:00:30,120 --> 06:00:32,040
and lastly we need a function for
9083
06:00:32,040 --> 06:00:35,100
logging out through our front end
9084
06:00:35,100 --> 06:00:40,080
so another export async function log out
9085
06:00:40,080 --> 06:00:41,940
doesn't take any arguments doesn't
9086
06:00:41,940 --> 06:00:44,160
return anything we know that it went
9087
06:00:44,160 --> 06:00:47,878
successful if fetch data didn't throw
9088
06:00:47,878 --> 06:00:52,080
so we simply call a weight of fetch data
9089
06:00:52,080 --> 06:00:58,940
two slash API slash users slash logout
9090
06:00:59,820 --> 06:01:04,340
and the method is post
9091
06:01:04,500 --> 06:01:06,900
and that's it
9092
06:01:06,900 --> 06:01:08,520
save everything
9093
06:01:08,520 --> 06:01:10,860
okay and now we want to create models
9094
06:01:10,860 --> 06:01:13,320
for logging in and signing up which of
9095
06:01:13,320 --> 06:01:15,840
course contain form input similar to
9096
06:01:15,840 --> 06:01:19,080
what we already have in our add edit
9097
06:01:19,080 --> 06:01:21,120
node dialog here
9098
06:01:21,120 --> 06:01:23,240
but I said that we want to create
9099
06:01:23,240 --> 06:01:25,920
reusable components for text input
9100
06:01:25,920 --> 06:01:28,378
Fields otherwise we have to repeat the
9101
06:01:28,378 --> 06:01:30,180
syntax here with form group and form
9102
06:01:30,180 --> 06:01:32,340
label over and over again and it's quite
9103
06:01:32,340 --> 06:01:34,620
verbose so what we do is we create
9104
06:01:34,620 --> 06:01:36,660
another folder here in the components
9105
06:01:36,660 --> 06:01:38,940
folder
9106
06:01:38,940 --> 06:01:41,700
which we call form
9107
06:01:41,700 --> 06:01:44,040
I just want to put form Fields into a
9108
06:01:44,040 --> 06:01:47,218
separate folder for better organization
9109
06:01:47,218 --> 06:01:50,360
and then here we created text input
9110
06:01:50,360 --> 06:01:52,500
field.tsx file
9111
06:01:52,500 --> 06:01:54,540
and we can split this and move this to
9112
06:01:54,540 --> 06:01:57,958
the right so we can view both our files
9113
06:01:57,958 --> 06:02:00,600
here at the same time
9114
06:02:00,600 --> 06:02:02,700
we want to set this up in a way that we
9115
06:02:02,700 --> 06:02:04,798
get rid of a lot of duplication here
9116
06:02:04,798 --> 06:02:07,860
when adding these form input Fields so
9117
06:02:07,860 --> 06:02:10,080
the first step is to create our
9118
06:02:10,080 --> 06:02:12,660
component function here with the same
9119
06:02:12,660 --> 06:02:13,680
name
9120
06:02:13,680 --> 06:02:16,620
text input field
9121
06:02:16,620 --> 06:02:19,200
and above it we want to Define an
9122
06:02:19,200 --> 06:02:21,120
interface for the arguments that we pass
9123
06:02:21,120 --> 06:02:22,980
to this component
9124
06:02:22,980 --> 06:02:26,340
so text input field props
9125
06:02:26,340 --> 06:02:28,860
so every text input field will need a
9126
06:02:28,860 --> 06:02:31,200
name which is this part here that we use
9127
06:02:31,200 --> 06:02:33,480
to register the field with the react
9128
06:02:33,480 --> 06:02:36,298
formwork Library so we add a name of
9129
06:02:36,298 --> 06:02:38,040
type string
9130
06:02:38,040 --> 06:02:40,860
also all of our forms you have a label
9131
06:02:40,860 --> 06:02:43,260
above it right so let's add this as
9132
06:02:43,260 --> 06:02:45,240
another property you can make this
9133
06:02:45,240 --> 06:02:47,100
optional if you want and only show the
9134
06:02:47,100 --> 06:02:49,440
label for some but we show labels for
9135
06:02:49,440 --> 06:02:51,000
all input fields
9136
06:02:51,000 --> 06:02:53,700
then we need to pass this register call
9137
06:02:53,700 --> 06:02:56,638
here to the text input field
9138
06:02:56,638 --> 06:02:58,620
so we add a property that we call
9139
06:02:58,620 --> 06:03:00,120
register
9140
06:03:00,120 --> 06:03:04,558
and the type is used for register
9141
06:03:04,558 --> 06:03:07,680
of type any like this we have to set
9142
06:03:07,680 --> 06:03:09,540
this to any because we don't know yet
9143
06:03:09,540 --> 06:03:11,878
what Fields this register will contain
9144
06:03:11,878 --> 06:03:14,160
this way we keep it reusable we can use
9145
06:03:14,160 --> 06:03:16,500
it in our zainab form in our added node
9146
06:03:16,500 --> 06:03:18,660
model and so on and where do I know this
9147
06:03:18,660 --> 06:03:20,820
return type from that we have to use use
9148
06:03:20,820 --> 06:03:23,218
from register again I just Google this
9149
06:03:23,218 --> 06:03:25,138
you just have to figure out stuff like
9150
06:03:25,138 --> 06:03:27,240
this through Googling and this way we
9151
06:03:27,240 --> 06:03:30,058
can pass this register function here to
9152
06:03:30,058 --> 06:03:32,458
our text input field to register it with
9153
06:03:32,458 --> 06:03:34,020
react talk form
9154
06:03:34,020 --> 06:03:36,120
we will pass this configuration here
9155
06:03:36,120 --> 06:03:38,218
separately
9156
06:03:38,218 --> 06:03:42,480
so we add a register options
9157
06:03:42,480 --> 06:03:45,120
and this one is optional because yeah we
9158
06:03:45,120 --> 06:03:47,760
can pass register options like we do up
9159
06:03:47,760 --> 06:03:50,580
here or we can omit them so we make this
9160
06:03:50,580 --> 06:03:52,440
property optional
9161
06:03:52,440 --> 06:03:56,100
and the type here is register options
9162
06:03:56,100 --> 06:03:58,138
I just think it's more readable to
9163
06:03:58,138 --> 06:04:00,360
separate the register function and the
9164
06:04:00,360 --> 06:04:02,760
options rather than passing them as one
9165
06:04:02,760 --> 06:04:05,218
thing and we also want to have this
9166
06:04:05,218 --> 06:04:07,620
error message here and the type of these
9167
06:04:07,620 --> 06:04:10,500
errors is field error or undefined so
9168
06:04:10,500 --> 06:04:12,360
what we do is we add another property
9169
06:04:12,360 --> 06:04:15,058
that we call error but this one is
9170
06:04:15,058 --> 06:04:18,360
optional and it's of type field error
9171
06:04:18,360 --> 06:04:20,638
so we can pass a field error or we can
9172
06:04:20,638 --> 06:04:22,620
pass undefined in which case we want to
9173
06:04:22,620 --> 06:04:24,600
ignore it just like we did here
9174
06:04:24,600 --> 06:04:26,878
and then we add one more property here
9175
06:04:26,878 --> 06:04:28,558
which we write like this
9176
06:04:28,558 --> 06:04:31,798
scrap records X colon
9177
06:04:31,798 --> 06:04:33,958
of type string
9178
06:04:33,958 --> 06:04:35,340
like this
9179
06:04:35,340 --> 06:04:37,740
and then call on any
9180
06:04:37,740 --> 06:04:39,780
what does this mean this basically
9181
06:04:39,780 --> 06:04:43,020
allows us to pass any other probes that
9182
06:04:43,020 --> 06:04:44,878
we want to our text input field
9183
06:04:44,878 --> 06:04:46,860
component even if they are not defined
9184
06:04:46,860 --> 06:04:48,718
in here and we do this because these
9185
06:04:48,718 --> 06:04:50,340
form input Fields have a lot of
9186
06:04:50,340 --> 06:04:52,680
different properties available that we
9187
06:04:52,680 --> 06:04:55,080
can add or omit and instead of defining
9188
06:04:55,080 --> 06:04:57,718
each of them here separately we just add
9189
06:04:57,718 --> 06:05:00,420
this syntax which allows us to pass any
9190
06:05:00,420 --> 06:05:03,298
array of remaining properties then we
9191
06:05:03,298 --> 06:05:05,458
have to list all these properties again
9192
06:05:05,458 --> 06:05:09,480
down here so name comma label comma
9193
06:05:09,480 --> 06:05:10,620
register
9194
06:05:10,620 --> 06:05:13,500
comma register options
9195
06:05:13,500 --> 06:05:15,240
comma error
9196
06:05:15,240 --> 06:05:18,240
and for these
9197
06:05:18,240 --> 06:05:20,520
optional props here at the bottom we
9198
06:05:20,520 --> 06:05:23,638
write it like this dot dot dot props
9199
06:05:23,638 --> 06:05:26,340
which is the syntax again where it takes
9200
06:05:26,340 --> 06:05:28,378
something and it destructures it into
9201
06:05:28,378 --> 06:05:31,020
its single pieces basically this just
9202
06:05:31,020 --> 06:05:32,878
means that we get past each of these
9203
06:05:32,878 --> 06:05:35,340
props that we add additionally one by
9204
06:05:35,340 --> 06:05:37,620
one could be a bit confusing but if you
9205
06:05:37,620 --> 06:05:39,240
just follow along you will see how this
9206
06:05:39,240 --> 06:05:42,420
works and we must not forget to set the
9207
06:05:42,420 --> 06:05:43,500
type here
9208
06:05:43,500 --> 06:05:47,240
text input field props
9209
06:05:48,420 --> 06:05:50,280
let's make this big for a short moment
9210
06:05:50,280 --> 06:05:52,260
so we can see the whole thing this is
9211
06:05:52,260 --> 06:05:54,660
how it looks
9212
06:05:54,660 --> 06:05:56,878
and then we want to set up the same way
9213
06:05:56,878 --> 06:05:58,740
as we did here on the left
9214
06:05:58,740 --> 06:06:04,580
zor the outer tag is a form group
9215
06:06:05,280 --> 06:06:06,840
with this
9216
06:06:06,840 --> 06:06:09,780
mb3 class name
9217
06:06:09,780 --> 06:06:11,878
and we add another property called
9218
06:06:11,878 --> 06:06:15,360
control ID on this one
9219
06:06:15,360 --> 06:06:18,920
and reset it with curly braces to name
9220
06:06:18,920 --> 06:06:23,400
plus and then we append a string input
9221
06:06:23,400 --> 06:06:25,378
and then we close this tag
9222
06:06:25,378 --> 06:06:28,320
control ID is a property that does some
9223
06:06:28,320 --> 06:06:30,420
accessibility stuff for example it
9224
06:06:30,420 --> 06:06:32,160
connects the label that we put into this
9225
06:06:32,160 --> 06:06:34,558
form group with the input field itself
9226
06:06:34,558 --> 06:06:36,780
so that we can click the label and it
9227
06:06:36,780 --> 06:06:39,000
activates the form input field and it
9228
06:06:39,000 --> 06:06:41,040
also helps screen readers for example
9229
06:06:41,040 --> 06:06:43,020
this is what this control ID is for we
9230
06:06:43,020 --> 06:06:44,878
just have to set it here and we use the
9231
06:06:44,878 --> 06:06:46,920
name of this input field which is
9232
06:06:46,920 --> 06:06:48,718
different for each of them and we just
9233
06:06:48,718 --> 06:06:50,280
append input to make it more
9234
06:06:50,280 --> 06:06:51,540
recognizable
9235
06:06:51,540 --> 06:06:54,120
then let's finish setting up this input
9236
06:06:54,120 --> 06:06:58,620
field so we add the form label here
9237
06:06:58,620 --> 06:07:00,900
which will contain the label that we
9238
06:07:00,900 --> 06:07:03,120
pass to this component
9239
06:07:03,120 --> 06:07:04,680
you can make this optional if you want
9240
06:07:04,680 --> 06:07:06,480
by wrapping this with these two Amazon
9241
06:07:06,480 --> 06:07:09,058
signs as we did earlier but I don't want
9242
06:07:09,058 --> 06:07:11,638
to make this optional
9243
06:07:11,638 --> 06:07:14,160
below we add form control which is the
9244
06:07:14,160 --> 06:07:17,040
input field itself
9245
06:07:17,040 --> 06:07:20,100
then first we want to pass these props
9246
06:07:20,100 --> 06:07:23,160
here with this three dots which we
9247
06:07:23,160 --> 06:07:24,840
simply do like this a pair of curly
9248
06:07:24,840 --> 06:07:26,760
braces and then we write dot dot dots
9249
06:07:26,760 --> 06:07:29,100
propsy as well and this simply takes
9250
06:07:29,100 --> 06:07:31,200
these optional props and adds them to
9251
06:07:31,200 --> 06:07:32,940
the form control and then we can just
9252
06:07:32,940 --> 06:07:35,040
add them like we add any other props to
9253
06:07:35,040 --> 06:07:36,958
our component just that we don't have to
9254
06:07:36,958 --> 06:07:39,480
Define them up here on our interface
9255
06:07:39,480 --> 06:07:41,218
then we have to do the same with the
9256
06:07:41,218 --> 06:07:44,100
register like we did earlier another
9257
06:07:44,100 --> 06:07:46,320
pair of curly braces dot dot dot
9258
06:07:46,320 --> 06:07:48,600
register
9259
06:07:48,600 --> 06:07:50,700
the first argument will be the name that
9260
06:07:50,700 --> 06:07:53,760
we pass that's this property here
9261
06:07:53,760 --> 06:07:55,620
comma and then we pass the register
9262
06:07:55,620 --> 06:07:58,680
options which repair separately
9263
06:07:58,680 --> 06:08:02,180
and then the same as over here
9264
06:08:02,180 --> 06:08:06,298
is invalid property
9265
06:08:06,298 --> 06:08:10,020
which we set to a to our exclamation
9266
06:08:10,020 --> 06:08:12,420
marks to turn it into a Boolean and then
9267
06:08:12,420 --> 06:08:14,218
the arrow we pass here which is the
9268
06:08:14,218 --> 06:08:16,200
field Arrow or undefined
9269
06:08:16,200 --> 06:08:18,180
this stuff is a bit complicated and a
9270
06:08:18,180 --> 06:08:20,100
bit annoying but you only have to set up
9271
06:08:20,100 --> 06:08:22,260
this input field once and then you can
9272
06:08:22,260 --> 06:08:24,298
reuse them in your app that's the cool
9273
06:08:24,298 --> 06:08:26,100
thing about it and it will be much less
9274
06:08:26,100 --> 06:08:27,900
for both than what we have currently
9275
06:08:27,900 --> 06:08:31,558
here and then we need this invalid input
9276
06:08:31,558 --> 06:08:33,718
feedback let's copy this
9277
06:08:33,718 --> 06:08:36,058
opacity inside the form group
9278
06:08:36,058 --> 06:08:39,180
but change this to error.message because
9279
06:08:39,180 --> 06:08:41,520
this is already the field error but it
9280
06:08:41,520 --> 06:08:43,320
can be undefined so we add the save call
9281
06:08:43,320 --> 06:08:45,420
Operator but it has to be error not
9282
06:08:45,420 --> 06:08:47,340
arrows because this is the name of the
9283
06:08:47,340 --> 06:08:48,360
property
9284
06:08:48,360 --> 06:08:50,700
and now let's replace these input Fields
9285
06:08:50,700 --> 06:08:52,798
over here in our add edit nodes dialog
9286
06:08:52,798 --> 06:08:54,900
for these newly created text input
9287
06:08:54,900 --> 06:08:57,360
fields so I'm going to do this with the
9288
06:08:57,360 --> 06:09:00,000
browser open so we can see if the newer
9289
06:09:00,000 --> 06:09:01,620
input field looks the same as the old
9290
06:09:01,620 --> 06:09:02,760
one
9291
06:09:02,760 --> 06:09:05,160
so let's not delete the other ones right
9292
06:09:05,160 --> 06:09:07,500
away let's first add the new ones so
9293
06:09:07,500 --> 06:09:11,940
opening angle bracket text input field
9294
06:09:11,940 --> 06:09:14,100
and we can add the self-closing tag
9295
06:09:14,100 --> 06:09:15,780
right array
9296
06:09:15,780 --> 06:09:18,180
they all need a name the same one as we
9297
06:09:18,180 --> 06:09:20,400
used here to register this
9298
06:09:20,400 --> 06:09:23,360
so title
9299
06:09:24,298 --> 06:09:27,298
they all need a label
9300
06:09:27,298 --> 06:09:30,558
which will be title
9301
06:09:32,700 --> 06:09:35,400
we set the type to text and this is the
9302
06:09:35,400 --> 06:09:38,040
first example of our rest props here
9303
06:09:38,040 --> 06:09:40,920
because we didn't Define the text as
9304
06:09:40,920 --> 06:09:43,620
input props because we don't want to
9305
06:09:43,620 --> 06:09:45,480
pass one down here for the text area
9306
06:09:45,480 --> 06:09:48,120
this one is optional but instead of
9307
06:09:48,120 --> 06:09:50,820
adding an optional property here we use
9308
06:09:50,820 --> 06:09:52,558
these rest props this is how this is
9309
06:09:52,558 --> 06:09:53,580
called
9310
06:09:53,580 --> 06:09:55,860
so we can basically pass any property
9311
06:09:55,860 --> 06:09:57,840
here and they will be put into our text
9312
06:09:57,840 --> 06:09:59,940
input field so this is really just a
9313
06:09:59,940 --> 06:10:01,680
matter of how explicit you want to be
9314
06:10:01,680 --> 06:10:03,480
you can add them to the properties
9315
06:10:03,480 --> 06:10:04,980
interface if you want to make this
9316
06:10:04,980 --> 06:10:07,378
decision explicit or you can use these
9317
06:10:07,378 --> 06:10:09,058
rest properties where you can basically
9318
06:10:09,058 --> 06:10:11,520
add anything you want it makes sense for
9319
06:10:11,520 --> 06:10:12,958
something like a form input field
9320
06:10:12,958 --> 06:10:14,580
because there are so many different
9321
06:10:14,580 --> 06:10:17,160
properties that we can pass or not and
9322
06:10:17,160 --> 06:10:19,200
we don't want to specify each of them in
9323
06:10:19,200 --> 06:10:22,320
our interface okay we want to set a
9324
06:10:22,320 --> 06:10:24,120
placeholder as well which is another
9325
06:10:24,120 --> 06:10:26,580
example of a rest property it's not in
9326
06:10:26,580 --> 06:10:28,138
our interface
9327
06:10:28,138 --> 06:10:31,620
3.0 set this to title 2.
9328
06:10:31,620 --> 06:10:35,160
we need register that's required
9329
06:10:35,160 --> 06:10:38,280
here we can simply pass the register
9330
06:10:38,280 --> 06:10:42,740
that we get returned from react formhog
9331
06:10:43,680 --> 06:10:47,638
register options are optional
9332
06:10:47,638 --> 06:10:49,440
and here we have to add two pair of
9333
06:10:49,440 --> 06:10:52,020
curly braces because what we pass is a
9334
06:10:52,020 --> 06:10:54,480
JavaScript object and that's why we need
9335
06:10:54,480 --> 06:10:57,480
another pair of curly braces
9336
06:10:57,480 --> 06:10:59,820
we want to set required
9337
06:10:59,820 --> 06:11:01,860
and use the required string the same as
9338
06:11:01,860 --> 06:11:04,020
we did before and now we can already
9339
06:11:04,020 --> 06:11:06,718
save this and we see our second input
9340
06:11:06,718 --> 06:11:08,820
field here the one at the top is the new
9341
06:11:08,820 --> 06:11:10,558
one through our text input field
9342
06:11:10,558 --> 06:11:13,260
component one more thing we also want to
9343
06:11:13,260 --> 06:11:16,340
test the error here
9344
06:11:17,040 --> 06:11:20,280
which we set two errors from react from
9345
06:11:20,280 --> 06:11:23,340
Hook dot title
9346
06:11:23,340 --> 06:11:26,700
so now when we don't add the title the
9347
06:11:26,700 --> 06:11:28,558
error is recognized properly
9348
06:11:28,558 --> 06:11:30,958
okay so we can delete the first input
9349
06:11:30,958 --> 06:11:33,058
field here of the title
9350
06:11:33,058 --> 06:11:35,040
and then we want to create another text
9351
06:11:35,040 --> 06:11:39,980
input field for the node body area
9352
06:11:41,638 --> 06:11:46,020
the name of this one is text the same as
9353
06:11:46,020 --> 06:11:50,298
we are used down here to register it
9354
06:11:50,820 --> 06:11:54,780
the label will say text
9355
06:11:54,780 --> 06:11:59,100
we use S text area
9356
06:11:59,100 --> 06:12:01,020
reset the rows
9357
06:12:01,020 --> 06:12:03,298
and here you can really see all these
9358
06:12:03,298 --> 06:12:04,920
different combinations of properties
9359
06:12:04,920 --> 06:12:06,780
that we can pass
9360
06:12:06,780 --> 06:12:08,700
that we all want to handle through this
9361
06:12:08,700 --> 06:12:10,260
rest props here
9362
06:12:10,260 --> 06:12:12,540
because it would be a lot of work to add
9363
06:12:12,540 --> 06:12:14,340
each of them to the interface and it
9364
06:12:14,340 --> 06:12:17,280
would be very verbose
9365
06:12:17,280 --> 06:12:20,660
we add the placeholder
9366
06:12:23,100 --> 06:12:26,280
which is a text
9367
06:12:26,280 --> 06:12:28,798
and re-register this
9368
06:12:28,798 --> 06:12:30,540
but this time we don't need register
9369
06:12:30,540 --> 06:12:33,240
options add no errors because this is an
9370
06:12:33,240 --> 06:12:34,440
optional field
9371
06:12:34,440 --> 06:12:36,958
we don't have to pass it
9372
06:12:36,958 --> 06:12:39,660
and there's our new text field
9373
06:12:39,660 --> 06:12:43,440
and now let's delete this one down here
9374
06:12:43,440 --> 06:12:45,840
now the new text input field code is not
9375
06:12:45,840 --> 06:12:47,820
really that much shorter but the
9376
06:12:47,820 --> 06:12:49,320
important part is that we don't have to
9377
06:12:49,320 --> 06:12:51,780
repeat stuff like adding the correct
9378
06:12:51,780 --> 06:12:53,878
margin all the time or all these
9379
06:12:53,878 --> 06:12:55,500
different components is just a bit
9380
06:12:55,500 --> 06:12:57,718
easier to use and everything that's
9381
06:12:57,718 --> 06:12:59,458
required you can just add to the
9382
06:12:59,458 --> 06:13:00,420
interface
9383
06:13:00,420 --> 06:13:03,480
let's save this and see if adding a node
9384
06:13:03,480 --> 06:13:06,058
still works
9385
06:13:06,058 --> 06:13:09,200
another note
9386
06:13:09,240 --> 06:13:12,320
blah blah
9387
06:13:12,360 --> 06:13:15,180
and it's here at the bottom so it still
9388
06:13:15,180 --> 06:13:16,860
seems to work
9389
06:13:16,860 --> 06:13:18,840
now let's create a design up model
9390
06:13:18,840 --> 06:13:21,360
through which we can create a new user
9391
06:13:21,360 --> 06:13:23,340
account through our front end let's
9392
06:13:23,340 --> 06:13:25,680
close the text input field code and the
9393
06:13:25,680 --> 06:13:28,020
add edit node dialog code
9394
06:13:28,020 --> 06:13:30,780
the API we can actually close everything
9395
06:13:30,780 --> 06:13:32,660
and then let's create another component
9396
06:13:32,660 --> 06:13:38,040
front end Source components new file
9397
06:13:38,040 --> 06:13:40,580
we call it
9398
06:13:40,580 --> 06:13:43,680
zineappmodel.tsx copy this name
9399
06:13:43,680 --> 06:13:46,920
and create a functional component in
9400
06:13:46,920 --> 06:13:48,058
here
9401
06:13:48,058 --> 06:13:50,400
first we need to define the input props
9402
06:13:50,400 --> 06:13:52,500
interface
9403
06:13:52,500 --> 06:13:56,218
sign up model props
9404
06:13:56,218 --> 06:13:58,740
we need an on dismiss callback
9405
06:13:58,740 --> 06:14:01,500
so that we can close the model
9406
06:14:01,500 --> 06:14:04,920
a function that doesn't return anything
9407
06:14:04,920 --> 06:14:06,660
and we need another callback for when
9408
06:14:06,660 --> 06:14:08,638
the sign up was successful so that we
9409
06:14:08,638 --> 06:14:11,040
can set the use of our app to the newly
9410
06:14:11,040 --> 06:14:13,020
signed up user
9411
06:14:13,020 --> 06:14:15,840
so on design up
9412
06:14:15,840 --> 06:14:18,840
successful
9413
06:14:19,558 --> 06:14:22,080
this is a function that gets a user
9414
06:14:22,080 --> 06:14:23,878
passed to it
9415
06:14:23,878 --> 06:14:26,520
and doesn't return anything
9416
06:14:26,520 --> 06:14:28,920
then we add these properties down here
9417
06:14:28,920 --> 06:14:30,780
the usual way
9418
06:14:30,780 --> 06:14:34,260
on this Miss on sign up successful
9419
06:14:34,260 --> 06:14:37,500
of type sign up model props
9420
06:14:37,500 --> 06:14:39,540
all right and now we have to write a
9421
06:14:39,540 --> 06:14:41,218
little bit of code
9422
06:14:41,218 --> 06:14:43,378
so first of all we need our register
9423
06:14:43,378 --> 06:14:46,320
form hook and this stuff so we create a
9424
06:14:46,320 --> 06:14:49,980
const curly braces the same as in the
9425
06:14:49,980 --> 06:14:52,138
add edit note dialog
9426
06:14:52,138 --> 06:14:54,180
we need to register
9427
06:14:54,180 --> 06:14:57,958
let's actually add equals use form here
9428
06:14:57,958 --> 06:14:59,760
at the end right array so that we have
9429
06:14:59,760 --> 06:15:01,558
proper Auto completion for these
9430
06:15:01,558 --> 06:15:04,558
different fields so we call use form add
9431
06:15:04,558 --> 06:15:06,000
this import statement if it didn't
9432
06:15:06,000 --> 06:15:08,700
automatically and the type for the input
9433
06:15:08,700 --> 06:15:12,058
is our sign up credentials that we
9434
06:15:12,058 --> 06:15:15,180
created in the notes API file earlier
9435
06:15:15,180 --> 06:15:17,218
so we add this type argument and call
9436
06:15:17,218 --> 06:15:19,378
this and now we have better Auto
9437
06:15:19,378 --> 06:15:20,878
completion here
9438
06:15:20,878 --> 06:15:23,760
we need handle submit
9439
06:15:23,760 --> 06:15:26,638
we need form state
9440
06:15:26,638 --> 06:15:30,420
which we destructure tour errors and is
9441
06:15:30,420 --> 06:15:32,760
submitting this is the exact same stuff
9442
06:15:32,760 --> 06:15:35,280
we have done in the add edit node model
9443
06:15:35,280 --> 06:15:36,958
before
9444
06:15:36,958 --> 06:15:38,820
then we need a function that handles
9445
06:15:38,820 --> 06:15:41,218
submitting this data
9446
06:15:41,218 --> 06:15:44,940
soar an async function
9447
06:15:44,940 --> 06:15:48,020
on submit
9448
06:15:49,620 --> 06:15:53,400
which will take design up credentials as
9449
06:15:53,400 --> 06:15:55,820
input
9450
06:15:56,700 --> 06:15:58,860
we need to try catch Block in case
9451
06:15:58,860 --> 06:16:00,540
something goes wrong
9452
06:16:00,540 --> 06:16:04,320
as usual we do alert with the error
9453
06:16:04,320 --> 06:16:09,320
and we lock the arrow in the console
9454
06:16:09,540 --> 06:16:12,420
we need a handle to our notes API file
9455
06:16:12,420 --> 06:16:14,940
so let's duplicate this line
9456
06:16:14,940 --> 06:16:19,520
and change this to a star as nodes API
9457
06:16:19,520 --> 06:16:22,980
from the network nodes API file
9458
06:16:22,980 --> 06:16:25,620
because we want to use this here
9459
06:16:25,620 --> 06:16:28,138
we create a const for the newly signed
9460
06:16:28,138 --> 06:16:29,340
abuser
9461
06:16:29,340 --> 06:16:33,240
equals the weight notes API
9462
06:16:33,240 --> 06:16:36,058
and we call our sign up function
9463
06:16:36,058 --> 06:16:38,820
which expects design up credentials
9464
06:16:38,820 --> 06:16:40,200
passed to it
9465
06:16:40,200 --> 06:16:42,540
and if this weren't successful
9466
06:16:42,540 --> 06:16:44,700
we want to call our on sign up
9467
06:16:44,700 --> 06:16:46,558
successful callback
9468
06:16:46,558 --> 06:16:49,260
and paste this newly created user to
9469
06:16:49,260 --> 06:16:51,718
whoever is showing this sign up model
9470
06:16:51,718 --> 06:16:54,240
now even though we don't have a sign up
9471
06:16:54,240 --> 06:16:56,940
button yet we can still go into our ftsx
9472
06:16:56,940 --> 06:16:58,920
file and show the signup model already
9473
06:16:58,920 --> 06:17:00,900
so that we can see what we are building
9474
06:17:00,900 --> 06:17:03,120
while we are adding the form Fields so
9475
06:17:03,120 --> 06:17:05,280
let's go into app TSX and down here
9476
06:17:05,280 --> 06:17:07,260
where we already have some models set up
9477
06:17:07,260 --> 06:17:10,260
let's put it here below another pair of
9478
06:17:10,260 --> 06:17:11,820
curly braces
9479
06:17:11,820 --> 06:17:13,920
and since we don't have a state yet for
9480
06:17:13,920 --> 06:17:15,958
now we just write true and to Amber's
9481
06:17:15,958 --> 06:17:20,340
enzymes to show this model all the time
9482
06:17:20,340 --> 06:17:22,320
and here we want to use our sign up
9483
06:17:22,320 --> 06:17:23,520
model
9484
06:17:23,520 --> 06:17:25,798
which expects the on dismissed callback
9485
06:17:25,798 --> 06:17:30,480
on enter on sign up successful callback
9486
06:17:30,480 --> 06:17:33,360
and for now we just pass empty functions
9487
06:17:33,360 --> 06:17:36,360
here to ignore these callbacks
9488
06:17:36,360 --> 06:17:38,340
but we have to finish our sign up model
9489
06:17:38,340 --> 06:17:40,680
return statement here first before we
9490
06:17:40,680 --> 06:17:43,260
can see it on the screen so let's finish
9491
06:17:43,260 --> 06:17:45,900
setting this up this should return a
9492
06:17:45,900 --> 06:17:49,280
model from the react bootstrap package
9493
06:17:49,280 --> 06:17:52,138
research Hotel true as before
9494
06:17:52,138 --> 06:17:54,600
and on height will trigger our on
9495
06:17:54,600 --> 06:17:57,558
dismissed callback
9496
06:18:00,900 --> 06:18:05,218
in here we put the model dot header
9497
06:18:05,218 --> 06:18:08,660
with a close button
9498
06:18:09,480 --> 06:18:13,458
and the title will say sign up
9499
06:18:15,058 --> 06:18:18,360
below we put a model body
9500
06:18:18,360 --> 06:18:21,000
which will contain our form
9501
06:18:21,000 --> 06:18:24,000
so we import this react bootstrap form
9502
06:18:24,000 --> 06:18:26,420
tag
9503
06:18:26,458 --> 06:18:29,520
we forward on submit
9504
06:18:29,520 --> 06:18:32,580
to a handle submit from the react talk
9505
06:18:32,580 --> 06:18:34,620
form package
9506
06:18:34,620 --> 06:18:37,200
and call on submit here which is our own
9507
06:18:37,200 --> 06:18:39,600
submit function
9508
06:18:39,600 --> 06:18:41,638
I don't explain this again because this
9509
06:18:41,638 --> 06:18:43,260
is the same thing we have done in the
9510
06:18:43,260 --> 06:18:46,080
add edit node dialog before now when we
9511
06:18:46,080 --> 06:18:47,638
save this we should actually see
9512
06:18:47,638 --> 06:18:49,680
something on the screen nice it's our
9513
06:18:49,680 --> 06:18:52,320
emptier model right now and the
9514
06:18:52,320 --> 06:18:54,180
formatting of the title is not correct
9515
06:18:54,180 --> 06:18:56,100
yet because we also have to wrap this
9516
06:18:56,100 --> 06:18:58,798
into a model title
9517
06:18:58,798 --> 06:19:01,260
which takes care of using the correct
9518
06:19:01,260 --> 06:19:03,180
font size and everything
9519
06:19:03,180 --> 06:19:05,638
then let's finish setting up our form
9520
06:19:05,638 --> 06:19:07,260
down here
9521
06:19:07,260 --> 06:19:10,440
so we use our shiny newer text input
9522
06:19:10,440 --> 06:19:12,920
fields
9523
06:19:14,638 --> 06:19:16,860
the first one has the name
9524
06:19:16,860 --> 06:19:19,638
username
9525
06:19:20,340 --> 06:19:22,860
the label username as well just that we
9526
06:19:22,860 --> 06:19:24,600
start with an uppercase because this
9527
06:19:24,600 --> 06:19:27,740
will be visible to the outside
9528
06:19:27,780 --> 06:19:31,400
we set the type to text
9529
06:19:31,860 --> 06:19:34,558
we set the placeholder
9530
06:19:34,558 --> 06:19:37,580
to username
9531
06:19:37,980 --> 06:19:40,020
we Press Register
9532
06:19:40,020 --> 06:19:42,958
from react formhook
9533
06:19:42,958 --> 06:19:45,480
we Define register options with two pair
9534
06:19:45,480 --> 06:19:48,000
of curly braces again
9535
06:19:48,000 --> 06:19:51,620
because this will be required
9536
06:19:52,740 --> 06:19:54,900
and you want to show an arrow here in
9537
06:19:54,900 --> 06:19:57,298
case we didn't enter anything into this
9538
06:19:57,298 --> 06:19:59,160
field
9539
06:19:59,160 --> 06:20:02,580
which we get from errors Dot the
9540
06:20:02,580 --> 06:20:05,940
username also from react formhog
9541
06:20:05,940 --> 06:20:08,458
and when we save this we see our first
9542
06:20:08,458 --> 06:20:10,620
input field
9543
06:20:10,620 --> 06:20:12,718
then we want to add a second one below
9544
06:20:12,718 --> 06:20:14,940
for the email address and then one for
9545
06:20:14,940 --> 06:20:17,458
the password email and password are very
9546
06:20:17,458 --> 06:20:20,940
similar so let's copy this
9547
06:20:20,940 --> 06:20:23,458
it takes the same attributes but the
9548
06:20:23,458 --> 06:20:25,620
name is email
9549
06:20:25,620 --> 06:20:28,320
the label is email
9550
06:20:28,320 --> 06:20:31,138
the type will be email as well and this
9551
06:20:31,138 --> 06:20:32,878
changes how the browser handles this
9552
06:20:32,878 --> 06:20:34,860
input field when we set the type to
9553
06:20:34,860 --> 06:20:37,080
email we actually get some simple input
9554
06:20:37,080 --> 06:20:39,360
validation here which means that Chrome
9555
06:20:39,360 --> 06:20:41,458
or any other browser that handles this
9556
06:20:41,458 --> 06:20:43,798
will not accept an email that's not
9557
06:20:43,798 --> 06:20:45,480
shaped like an email with an ad and
9558
06:20:45,480 --> 06:20:47,520
everything that's just a bit more useful
9559
06:20:47,520 --> 06:20:49,740
than a normal text field but it does not
9560
06:20:49,740 --> 06:20:51,480
replace backend validation remember
9561
06:20:51,480 --> 06:20:53,580
because front-end validation can always
9562
06:20:53,580 --> 06:20:56,760
be circumvented in one way or another on
9563
06:20:56,760 --> 06:20:58,260
the back end we only checked that an
9564
06:20:58,260 --> 06:21:01,020
email exists we don't actually check its
9565
06:21:01,020 --> 06:21:03,000
shape there are packages that you can
9566
06:21:03,000 --> 06:21:04,798
use for that but that's beyond the scope
9567
06:21:04,798 --> 06:21:06,780
of this tutorial you can look that up in
9568
06:21:06,780 --> 06:21:09,780
Google the placeholder will say email as
9569
06:21:09,780 --> 06:21:12,780
well register is the same it's also a
9570
06:21:12,780 --> 06:21:15,480
required field but for the error we want
9571
06:21:15,480 --> 06:21:17,520
to use arrows.email
9572
06:21:17,520 --> 06:21:19,680
and then let's copy it once again for
9573
06:21:19,680 --> 06:21:20,820
the password and we can actually
9574
06:21:20,820 --> 06:21:22,920
duplicate The Code by highlighting it
9575
06:21:22,920 --> 06:21:26,580
and then we can press shift out and the
9576
06:21:26,580 --> 06:21:28,260
down arrow like this
9577
06:21:28,260 --> 06:21:30,480
but you can also just copy paste it the
9578
06:21:30,480 --> 06:21:33,180
old way like a peasant okay so the name
9579
06:21:33,180 --> 06:21:36,500
of this field is password
9580
06:21:36,600 --> 06:21:39,058
the label is password
9581
06:21:39,058 --> 06:21:41,100
the type is password 2 and this is
9582
06:21:41,100 --> 06:21:43,378
pretty cool because it automatically
9583
06:21:43,378 --> 06:21:45,660
hides the characters here so these types
9584
06:21:45,660 --> 06:21:48,240
are handled by the browsers
9585
06:21:48,240 --> 06:21:51,180
the placeholder will be password
9586
06:21:51,180 --> 06:21:53,580
register and register options stay the
9587
06:21:53,580 --> 06:21:56,820
same and we use errors password down
9588
06:21:56,820 --> 06:21:57,840
here
9589
06:21:57,840 --> 06:22:00,180
then of course we need a submit button
9590
06:22:00,180 --> 06:22:04,620
so below here we add another button
9591
06:22:04,620 --> 06:22:07,378
which is a sign up
9592
06:22:07,378 --> 06:22:09,900
and it needs some properties
9593
06:22:09,900 --> 06:22:12,480
we set the type to submit again so the
9594
06:22:12,480 --> 06:22:14,700
browser recognizes this as a form submit
9595
06:22:14,700 --> 06:22:16,500
button but this time we don't have to
9596
06:22:16,500 --> 06:22:19,080
specify the form idea because now we
9597
06:22:19,080 --> 06:22:21,298
have put the button inside the form tag
9598
06:22:21,298 --> 06:22:23,458
itself remember earlier we put such a
9599
06:22:23,458 --> 06:22:25,080
submit button outside of the form then
9600
06:22:25,080 --> 06:22:27,000
we had to connect it manually but since
9601
06:22:27,000 --> 06:22:28,860
this is inside the form the browser
9602
06:22:28,860 --> 06:22:30,660
knows that this subnet button belongs to
9603
06:22:30,660 --> 06:22:33,058
this form and when we press it it will
9604
06:22:33,058 --> 06:22:34,920
automatically trigger this unsubmit
9605
06:22:34,920 --> 06:22:37,680
callback here okay so we want to disable
9606
06:22:37,680 --> 06:22:39,840
this button while the form is submitting
9607
06:22:39,840 --> 06:22:43,378
so that we can't submit it twice
9608
06:22:43,378 --> 06:22:46,458
at the same time
9609
06:22:46,920 --> 06:22:49,320
this is our button but I also want to
9610
06:22:49,320 --> 06:22:52,200
give it a width of 100 so it fills up
9611
06:22:52,200 --> 06:22:54,480
the full horizontal space
9612
06:22:54,480 --> 06:22:56,520
for this we can create another utility
9613
06:22:56,520 --> 06:22:59,760
CSS class let's do that let's go over
9614
06:22:59,760 --> 06:23:01,100
into a
9615
06:23:01,100 --> 06:23:03,900
utils.module.css add another class down
9616
06:23:03,900 --> 06:23:06,180
here which I'm gonna call width for 100
9617
06:23:06,180 --> 06:23:09,480
I think the name makes sense
9618
06:23:09,480 --> 06:23:13,878
and here we set the width to 100 percent
9619
06:23:13,920 --> 06:23:15,420
save this
9620
06:23:15,420 --> 06:23:17,760
go back into our sign up model and
9621
06:23:17,760 --> 06:23:20,638
import our utility styles
9622
06:23:20,638 --> 06:23:23,878
so import Style utils
9623
06:23:23,878 --> 06:23:25,680
from
9624
06:23:25,680 --> 06:23:28,138
where is it located
9625
06:23:28,138 --> 06:23:30,660
dot dot styles
9626
06:23:30,660 --> 06:23:32,040
slash
9627
06:23:32,040 --> 06:23:35,040
utils.module.css
9628
06:23:37,680 --> 06:23:41,298
and now we can use it on here
9629
06:23:41,340 --> 06:23:44,480
class name
9630
06:23:45,000 --> 06:23:49,260
style utils dot with 100. and looks much
9631
06:23:49,260 --> 06:23:51,298
better in my opinion looks like a real
9632
06:23:51,298 --> 06:23:52,680
sign up form
9633
06:23:52,680 --> 06:23:55,020
let's format this properly
9634
06:23:55,020 --> 06:23:56,340
like this
9635
06:23:56,340 --> 06:23:58,500
and then try it out
9636
06:23:58,500 --> 06:24:01,440
so let's enter a new username
9637
06:24:01,440 --> 06:24:03,900
Florent tool
9638
06:24:03,900 --> 06:24:07,200
for around 2 at test.com
9639
06:24:07,200 --> 06:24:10,558
and the password ASD ASD
9640
06:24:10,558 --> 06:24:12,240
sign up
9641
06:24:12,240 --> 06:24:14,700
now we don't have any feedback yet and
9642
06:24:14,700 --> 06:24:16,740
the model doesn't close yet but if
9643
06:24:16,740 --> 06:24:18,660
everything went successful we should see
9644
06:24:18,660 --> 06:24:20,638
our new user in the database
9645
06:24:20,638 --> 06:24:24,200
and also a session for it
9646
06:24:24,900 --> 06:24:27,958
so here's Florian tour
9647
06:24:27,958 --> 06:24:30,360
and there should be a cookie in our
9648
06:24:30,360 --> 06:24:32,878
browser yet security is not secure is
9649
06:24:32,878 --> 06:24:34,680
because we are on localhost it will be
9650
06:24:34,680 --> 06:24:38,160
secure when we deploy this with https
9651
06:24:38,160 --> 06:24:40,920
and there is our connect Sid cookie and
9652
06:24:40,920 --> 06:24:42,780
now we should be able to retrieve the
9653
06:24:42,780 --> 06:24:44,940
user data without having to send our
9654
06:24:44,940 --> 06:24:47,040
login credentials again unless we log
9655
06:24:47,040 --> 06:24:49,680
out let's also try this email validation
9656
06:24:49,680 --> 06:24:51,360
that I was talking about
9657
06:24:51,360 --> 06:24:54,298
so the browser yeah actually recognizes
9658
06:24:54,298 --> 06:24:57,240
this and shows this error message here
9659
06:24:57,240 --> 06:24:59,878
if the email is misshaped and also
9660
06:24:59,878 --> 06:25:01,980
password managers recognize these input
9661
06:25:01,980 --> 06:25:03,718
Fields because we set these different
9662
06:25:03,718 --> 06:25:05,458
types here password and email and
9663
06:25:05,458 --> 06:25:07,798
everything so a password manager like
9664
06:25:07,798 --> 06:25:10,138
one password or LastPass will recognize
9665
06:25:10,138 --> 06:25:11,940
that this is a password field and
9666
06:25:11,940 --> 06:25:13,798
suggest you a credentials to put in
9667
06:25:13,798 --> 06:25:15,780
there now let's also create the login
9668
06:25:15,780 --> 06:25:16,920
model
9669
06:25:16,920 --> 06:25:19,620
so we create another file here in our
9670
06:25:19,620 --> 06:25:21,780
components folder
9671
06:25:21,780 --> 06:25:24,058
we colored lock and model and of course
9672
06:25:24,058 --> 06:25:25,558
you can organize this into another
9673
06:25:25,558 --> 06:25:27,600
folder if you want but we don't have
9674
06:25:27,600 --> 06:25:29,520
that many components in this tutorial so
9675
06:25:29,520 --> 06:25:32,480
I keep it like this
9676
06:25:32,520 --> 06:25:35,340
and create another functional component
9677
06:25:35,340 --> 06:25:37,500
in here let's start with the props
9678
06:25:37,500 --> 06:25:39,980
interface
9679
06:25:40,558 --> 06:25:43,920
login model props
9680
06:25:43,920 --> 06:25:46,798
as usual it's a dialog so it needs an on
9681
06:25:46,798 --> 06:25:49,700
dismissed callback
9682
06:25:50,340 --> 06:25:52,340
and then on login
9683
06:25:52,340 --> 06:25:55,740
successful callback
9684
06:25:55,740 --> 06:25:58,680
which again gets the user passed to it
9685
06:25:58,680 --> 06:26:03,080
so that we can use it in our front end
9686
06:26:03,958 --> 06:26:08,120
then we add this properties down here
9687
06:26:09,240 --> 06:26:12,958
on this Miss unload in successful
9688
06:26:12,958 --> 06:26:16,558
login model props
9689
06:26:16,558 --> 06:26:18,600
and then the setup is very similar to
9690
06:26:18,600 --> 06:26:20,100
design up model
9691
06:26:20,100 --> 06:26:22,798
we need to set up our form so let's
9692
06:26:22,798 --> 06:26:24,360
write const
9693
06:26:24,360 --> 06:26:28,860
curly braces equals use form
9694
06:26:28,860 --> 06:26:32,160
of type log in credentials
9695
06:26:32,160 --> 06:26:37,100
that we put into our notes API file
9696
06:26:37,378 --> 06:26:39,900
and then we need
9697
06:26:39,900 --> 06:26:42,120
register
9698
06:26:42,120 --> 06:26:45,240
we need handle submit
9699
06:26:45,240 --> 06:26:48,180
and we need form state
9700
06:26:48,180 --> 06:26:52,520
arrows and is submitting
9701
06:26:53,638 --> 06:26:57,480
we need our async submit function
9702
06:26:57,480 --> 06:27:00,780
so async function on the submit
9703
06:27:00,780 --> 06:27:04,260
which takes the look in credentials
9704
06:27:04,260 --> 06:27:06,600
log in
9705
06:27:06,600 --> 06:27:08,940
credentials
9706
06:27:08,940 --> 06:27:11,520
try catch
9707
06:27:11,520 --> 06:27:14,820
alert error
9708
06:27:14,820 --> 06:27:19,020
console Arrow error
9709
06:27:19,020 --> 06:27:21,718
in try we want to try logging in
9710
06:27:21,718 --> 06:27:24,958
a const user equals a weight
9711
06:27:24,958 --> 06:27:27,958
we need a notes API let's copy it from
9712
06:27:27,958 --> 06:27:30,540
design up model here because we are lazy
9713
06:27:30,540 --> 06:27:33,958
this one here notes API
9714
06:27:33,958 --> 06:27:36,420
the same location
9715
06:27:36,420 --> 06:27:38,580
Dot Login
9716
06:27:38,580 --> 06:27:41,760
we pass the user login credentials
9717
06:27:41,760 --> 06:27:44,458
and if this weren't successful recall
9718
06:27:44,458 --> 06:27:47,340
unlock and successful and pass back the
9719
06:27:47,340 --> 06:27:48,420
user
9720
06:27:48,420 --> 06:27:52,080
okay and then we need more forms so in
9721
06:27:52,080 --> 06:27:53,400
here
9722
06:27:53,400 --> 06:27:55,558
we create a model
9723
06:27:55,558 --> 06:27:56,700
show
9724
06:27:56,700 --> 06:27:58,440
on height
9725
06:27:58,440 --> 06:28:00,180
I don't expand this again because we
9726
06:28:00,180 --> 06:28:01,860
have already done this a few times by
9727
06:28:01,860 --> 06:28:04,100
now
9728
06:28:04,620 --> 06:28:07,500
oops what the hell is this
9729
06:28:07,500 --> 06:28:10,020
model dot header
9730
06:28:10,020 --> 06:28:12,958
with a close button
9731
06:28:12,958 --> 06:28:16,020
model dot title
9732
06:28:16,020 --> 06:28:19,520
that says login
9733
06:28:20,878 --> 06:28:23,458
below the header we put a modal dot
9734
06:28:23,458 --> 06:28:26,180
audio
9735
06:28:26,820 --> 06:28:30,320
and inside here we put a form
9736
06:28:31,620 --> 06:28:34,638
on submit
9737
06:28:34,798 --> 06:28:38,240
handle submit
9738
06:28:38,400 --> 06:28:40,558
on submit
9739
06:28:40,558 --> 06:28:43,200
I heard that you like submits so we put
9740
06:28:43,200 --> 06:28:45,540
an unsubmit into a handle submit into an
9741
06:28:45,540 --> 06:28:48,120
unsubmit this was just a joke by the way
9742
06:28:48,120 --> 06:28:49,798
don't get confused
9743
06:28:49,798 --> 06:28:54,620
and we need tour text input fields
9744
06:28:56,160 --> 06:29:00,080
first one has name username
9745
06:29:00,540 --> 06:29:03,058
labor
9746
06:29:03,058 --> 06:29:05,760
username
9747
06:29:05,760 --> 06:29:09,080
type would be text
9748
06:29:09,860 --> 06:29:14,420
placeholder will be a username
9749
06:29:14,520 --> 06:29:17,900
we set up register
9750
06:29:18,420 --> 06:29:22,260
we set up the register options
9751
06:29:22,260 --> 06:29:24,360
required
9752
06:29:24,360 --> 06:29:27,840
with the warning method required
9753
06:29:27,840 --> 06:29:31,138
and for error repairs errors dot
9754
06:29:31,138 --> 06:29:33,120
username
9755
06:29:33,120 --> 06:29:35,940
that's a lot of work
9756
06:29:35,940 --> 06:29:39,240
okay let's duplicate this
9757
06:29:39,240 --> 06:29:41,298
for the password
9758
06:29:41,298 --> 06:29:45,020
name password
9759
06:29:45,600 --> 06:29:47,100
label
9760
06:29:47,100 --> 06:29:49,500
password
9761
06:29:49,500 --> 06:29:51,718
type password
9762
06:29:51,718 --> 06:29:56,298
placeholder you guessed it password
9763
06:29:56,638 --> 06:29:59,820
and error set password
9764
06:29:59,820 --> 06:30:01,980
and then we need our submit button here
9765
06:30:01,980 --> 06:30:04,080
as well let's copy this again first of
9766
06:30:04,080 --> 06:30:06,660
all we need a style
9767
06:30:06,660 --> 06:30:09,958
the style utils
9768
06:30:09,958 --> 06:30:12,958
and the submit dot button down here
9769
06:30:12,958 --> 06:30:16,680
copy this as well go over here paste it
9770
06:30:16,680 --> 06:30:19,400
inside the form
9771
06:30:19,558 --> 06:30:23,878
just change the text to a log in I never
9772
06:30:23,878 --> 06:30:25,740
know if it's one word or two word it's
9773
06:30:25,740 --> 06:30:28,320
very confusing and we have to import the
9774
06:30:28,320 --> 06:30:31,520
react bootstrap button
9775
06:30:31,740 --> 06:30:33,718
let's take a quick look at our login
9776
06:30:33,718 --> 06:30:34,740
model
9777
06:30:34,740 --> 06:30:38,520
over here we set this to false
9778
06:30:38,520 --> 06:30:41,400
and we add another drawer
9779
06:30:41,400 --> 06:30:43,860
to Amazon science
9780
06:30:43,860 --> 06:30:46,440
log and model will be later set this up
9781
06:30:46,440 --> 06:30:48,780
properly
9782
06:30:48,780 --> 06:30:53,340
on dismiss and on login successfully
9783
06:30:53,340 --> 06:30:55,500
for which repairs empty functions for
9784
06:30:55,500 --> 06:30:57,000
now
9785
06:30:57,000 --> 06:30:58,558
just to see this
9786
06:30:58,558 --> 06:31:00,480
and as you can see the browser already
9787
06:31:00,480 --> 06:31:03,120
suggests to save the password because it
9788
06:31:03,120 --> 06:31:05,340
recognizes this as sign up and the login
9789
06:31:05,340 --> 06:31:07,020
form
9790
06:31:07,020 --> 06:31:09,120
okay here's our login form now it
9791
06:31:09,120 --> 06:31:10,500
doesn't make sense to try this out right
9792
06:31:10,500 --> 06:31:12,840
now the cookie is already saved first of
9793
06:31:12,840 --> 06:31:14,760
all we have to set up enough bar and
9794
06:31:14,760 --> 06:31:16,798
this navbar will then indicate if a user
9795
06:31:16,798 --> 06:31:18,780
is logged in or not because we will see
9796
06:31:18,780 --> 06:31:20,940
the username in the navbar
9797
06:31:20,940 --> 06:31:24,000
let's set this to false for now so that
9798
06:31:24,000 --> 06:31:26,280
we can set up our navbar and for the
9799
06:31:26,280 --> 06:31:28,440
navbar as usual we will use a react
9800
06:31:28,440 --> 06:31:30,900
bootstrap component this is how this nav
9801
06:31:30,900 --> 06:31:33,000
bars here look you can add different
9802
06:31:33,000 --> 06:31:34,378
stuff to them
9803
06:31:34,378 --> 06:31:36,840
different colors
9804
06:31:36,840 --> 06:31:40,920
different buttons and so on
9805
06:31:40,920 --> 06:31:44,400
so let's add it to our app so we add
9806
06:31:44,400 --> 06:31:46,920
another component to the components
9807
06:31:46,920 --> 06:31:49,400
folder
9808
06:31:50,900 --> 06:31:54,000
and I write the bar with an uppercase B
9809
06:31:54,000 --> 06:31:56,580
like this because the component from
9810
06:31:56,580 --> 06:31:58,680
react bootstrap is also called navbar
9811
06:31:58,680 --> 06:32:01,138
but with a lowercase b and this way the
9812
06:32:01,138 --> 06:32:02,820
names don't Clash
9813
06:32:02,820 --> 06:32:05,458
soar we set up a functional component
9814
06:32:05,458 --> 06:32:07,440
with the name navbar
9815
06:32:07,440 --> 06:32:10,200
and as usual we have to sign up the
9816
06:32:10,200 --> 06:32:13,138
props interface
9817
06:32:13,138 --> 06:32:15,360
with the arguments the snuffbar
9818
06:32:15,360 --> 06:32:17,280
component will take
9819
06:32:17,280 --> 06:32:19,558
the Snapback will take the locked in
9820
06:32:19,558 --> 06:32:22,020
user as an argument so that we can
9821
06:32:22,020 --> 06:32:24,120
display the name of the logged in user
9822
06:32:24,120 --> 06:32:26,100
but also the correct buttons either log
9823
06:32:26,100 --> 06:32:28,558
in and sign up or log out for this we
9824
06:32:28,558 --> 06:32:30,840
set the type of this to user
9825
06:32:30,840 --> 06:32:33,298
from our models folder
9826
06:32:33,298 --> 06:32:35,340
vertical barn now
9827
06:32:35,340 --> 06:32:37,440
so this can be either the locked in user
9828
06:32:37,440 --> 06:32:39,600
or null if there is no user locked in
9829
06:32:39,600 --> 06:32:40,620
currently
9830
06:32:40,620 --> 06:32:42,900
now why don't I make this an optional
9831
06:32:42,900 --> 06:32:45,420
property like this because I want to
9832
06:32:45,420 --> 06:32:47,760
force the color of the snuffbar
9833
06:32:47,760 --> 06:32:50,218
component to pass something here if we
9834
06:32:50,218 --> 06:32:51,900
used an optional parameter like this
9835
06:32:51,900 --> 06:32:54,000
then we could just omit this property
9836
06:32:54,000 --> 06:32:56,520
completely but our enough bar doesn't
9837
06:32:56,520 --> 06:32:58,378
work without the user even if it's
9838
06:32:58,378 --> 06:33:00,298
locked out we need this information so
9839
06:33:00,298 --> 06:33:02,040
this is why we declare this type like
9840
06:33:02,040 --> 06:33:04,500
this now we have to pass in a user even
9841
06:33:04,500 --> 06:33:06,840
if it's null and then we need a few
9842
06:33:06,840 --> 06:33:09,120
callbacks here for our different buttons
9843
06:33:09,120 --> 06:33:11,940
log in and out and everything
9844
06:33:11,940 --> 06:33:15,000
so the first one is called on sign up
9845
06:33:15,000 --> 06:33:17,480
clicked
9846
06:33:17,760 --> 06:33:19,680
when we click design up button we want
9847
06:33:19,680 --> 06:33:22,620
to open our sign up model in the ftsx
9848
06:33:22,620 --> 06:33:25,260
file now the question is why don't we
9849
06:33:25,260 --> 06:33:27,660
put the authentication models directly
9850
06:33:27,660 --> 06:33:29,940
into the navbar component then we
9851
06:33:29,940 --> 06:33:32,040
wouldn't need a skull back to the app
9852
06:33:32,040 --> 06:33:34,620
TSX file this is definitely an option
9853
06:33:34,620 --> 06:33:36,298
but then you have to keep in mind that
9854
06:33:36,298 --> 06:33:38,820
you can only open the models from within
9855
06:33:38,820 --> 06:33:40,860
the navbar code but you might have other
9856
06:33:40,860 --> 06:33:43,080
triggers in your app later that also
9857
06:33:43,080 --> 06:33:45,360
open the sign up or login model for
9858
06:33:45,360 --> 06:33:46,980
example if the user tries to do an
9859
06:33:46,980 --> 06:33:49,558
action that requires a user account this
9860
06:33:49,558 --> 06:33:51,360
is why we want to hoist this to the
9861
06:33:51,360 --> 06:33:54,240
color of this navbar okay and two more
9862
06:33:54,240 --> 06:33:56,400
let's duplicate this
9863
06:33:56,400 --> 06:33:58,620
the second one is
9864
06:33:58,620 --> 06:34:01,138
on login clicked
9865
06:34:01,138 --> 06:34:03,240
and then we have
9866
06:34:03,240 --> 06:34:06,600
on log out success file
9867
06:34:06,600 --> 06:34:08,458
because we will handle the log out
9868
06:34:08,458 --> 06:34:10,798
directly in here and then just notify
9869
06:34:10,798 --> 06:34:13,200
the ftsx files so that it can remove the
9870
06:34:13,200 --> 06:34:15,660
user data that's currently stored in the
9871
06:34:15,660 --> 06:34:18,660
memory okay then we add all these
9872
06:34:18,660 --> 06:34:21,540
properties here
9873
06:34:21,540 --> 06:34:25,860
locked in user on sign up clicked
9874
06:34:25,860 --> 06:34:28,860
unlock and clicked and unlock out
9875
06:34:28,860 --> 06:34:31,080
successful
9876
06:34:31,080 --> 06:34:34,320
and the type is our navbar props
9877
06:34:34,320 --> 06:34:36,900
okay and then let's start building the
9878
06:34:36,900 --> 06:34:40,320
navbar here and the return block
9879
06:34:40,320 --> 06:34:42,660
we use this navbar tag from react
9880
06:34:42,660 --> 06:34:44,760
bootstrap again this one has a lowercase
9881
06:34:44,760 --> 06:34:47,218
b that's not our own component it's
9882
06:34:47,218 --> 06:34:49,860
enough buff from react bootstrap
9883
06:34:49,860 --> 06:34:52,020
we can do some configuration here we can
9884
06:34:52,020 --> 06:34:53,760
set the background color
9885
06:34:53,760 --> 06:34:56,520
to primary which is this default Bluer
9886
06:34:56,520 --> 06:34:58,680
that react bootstrap components have you
9887
06:34:58,680 --> 06:35:01,378
can customize this with CSS if you want
9888
06:35:01,378 --> 06:35:03,660
but we won't do that here
9889
06:35:03,660 --> 06:35:06,180
the variant defines the text color we
9890
06:35:06,180 --> 06:35:07,860
said is too dark so that we have light
9891
06:35:07,860 --> 06:35:10,680
text on the blue background
9892
06:35:10,680 --> 06:35:13,798
expand defines Edward screen size the
9893
06:35:13,798 --> 06:35:16,440
navbar turns into this mobile mode where
9894
06:35:16,440 --> 06:35:18,420
we have this drop down menu that you
9895
06:35:18,420 --> 06:35:20,040
will see in a moment
9896
06:35:20,040 --> 06:35:22,138
so the Snapper is completely responsive
9897
06:35:22,138 --> 06:35:24,900
and we set this to LG to what size you
9898
06:35:24,900 --> 06:35:26,760
say this depends on how many buttons you
9899
06:35:26,760 --> 06:35:28,500
have in there if there are a lot of
9900
06:35:28,500 --> 06:35:30,120
buttons of course you have to collapse
9901
06:35:30,120 --> 06:35:31,680
earlier otherwise there will not be
9902
06:35:31,680 --> 06:35:33,120
enough room
9903
06:35:33,120 --> 06:35:35,400
and we set a sticky attribute to Top
9904
06:35:35,400 --> 06:35:37,260
This Way the nuff Bob will always be
9905
06:35:37,260 --> 06:35:39,780
visible even if we scroll down
9906
06:35:39,780 --> 06:35:42,600
but that's up to you and your preference
9907
06:35:42,600 --> 06:35:45,120
okay inside the snuff bar we add the
9908
06:35:45,120 --> 06:35:47,580
container which adds some padding so it
9909
06:35:47,580 --> 06:35:49,558
looks better and then we have different
9910
06:35:49,558 --> 06:35:51,900
components available which again I know
9911
06:35:51,900 --> 06:35:54,420
from the documentation
9912
06:35:54,420 --> 06:35:57,480
navbar brand adds a text or an image to
9913
06:35:57,480 --> 06:35:59,458
the left side which many websites have
9914
06:35:59,458 --> 06:36:01,558
when you click it you usually get back
9915
06:36:01,558 --> 06:36:03,120
to the home page
9916
06:36:03,120 --> 06:36:05,760
so let's enter a text in here Queen
9917
06:36:05,760 --> 06:36:08,760
Notes app but you can also use an image
9918
06:36:08,760 --> 06:36:09,958
if you want
9919
06:36:09,958 --> 06:36:12,180
and let's already display the softball
9920
06:36:12,180 --> 06:36:14,760
on our page where we are building it so
9921
06:36:14,760 --> 06:36:17,400
we go into the ftsx file
9922
06:36:17,400 --> 06:36:19,620
scroll up here to the notes page
9923
06:36:19,620 --> 06:36:21,360
container and we want to wrap this into
9924
06:36:21,360 --> 06:36:23,820
another div because we don't want to put
9925
06:36:23,820 --> 06:36:26,458
the snuff bar inside this container we
9926
06:36:26,458 --> 06:36:28,740
want to put it above it
9927
06:36:28,740 --> 06:36:31,260
select this and then here we can
9928
06:36:31,260 --> 06:36:33,900
use our navbar but this time with an
9929
06:36:33,900 --> 06:36:35,760
uppercase B because that's our own one
9930
06:36:35,760 --> 06:36:38,520
that's not the bootstrap one
9931
06:36:38,520 --> 06:36:40,458
foreign
9932
06:36:40,458 --> 06:36:44,218
user we just pass null right now and for
9933
06:36:44,218 --> 06:36:45,718
the callbacks
9934
06:36:45,718 --> 06:36:49,280
we pass empty carbex
9935
06:36:51,480 --> 06:36:54,240
we will hit this properly later
9936
06:36:54,240 --> 06:36:57,298
when we have this data
9937
06:36:57,298 --> 06:37:00,900
so here we pass an empty function
9938
06:37:00,900 --> 06:37:03,180
and there's our navbar
9939
06:37:03,180 --> 06:37:04,980
of course it's not finished yet but we
9940
06:37:04,980 --> 06:37:07,680
can already see our brand text let's
9941
06:37:07,680 --> 06:37:11,420
actually make this a bit bigger here
9942
06:37:11,760 --> 06:37:13,980
as I already mentioned we want to show
9943
06:37:13,980 --> 06:37:16,020
different content in our navbar
9944
06:37:16,020 --> 06:37:17,940
depending if we are logged in or logged
9945
06:37:17,940 --> 06:37:19,798
out because if we are logged out we want
9946
06:37:19,798 --> 06:37:21,480
to show a sign up and the login button
9947
06:37:21,480 --> 06:37:23,100
and if we are logged in we want to show
9948
06:37:23,100 --> 06:37:25,260
the username and the log out button so
9949
06:37:25,260 --> 06:37:26,940
now we could cram all of this in here
9950
06:37:26,940 --> 06:37:29,100
but it makes sense to extract this into
9951
06:37:29,100 --> 06:37:32,638
separate files for better organization
9952
06:37:32,638 --> 06:37:34,860
so we create another component in our
9953
06:37:34,860 --> 06:37:36,958
components folder
9954
06:37:36,958 --> 06:37:40,620
and recall this nuff bar locked in
9955
06:37:40,620 --> 06:37:43,620
viewer.tsx
9956
06:37:44,400 --> 06:37:47,700
I think the name is self-explaining
9957
06:37:47,700 --> 06:37:49,558
and we create a functional component
9958
06:37:49,558 --> 06:37:51,600
with the same name
9959
06:37:51,600 --> 06:37:55,320
this takes some properties
9960
06:37:55,320 --> 06:37:57,360
so interface
9961
06:37:57,360 --> 06:38:01,680
Enough by locked in view props
9962
06:38:01,680 --> 06:38:04,320
the first one will be the user
9963
06:38:04,320 --> 06:38:06,900
from our user models and this time it
9964
06:38:06,900 --> 06:38:08,638
doesn't have to be nullable because we
9965
06:38:08,638 --> 06:38:10,740
only show this if there is a user only
9966
06:38:10,740 --> 06:38:12,420
if we are logged in
9967
06:38:12,420 --> 06:38:15,840
but we need a callback for logging out
9968
06:38:15,840 --> 06:38:19,680
on lockout successful
9969
06:38:19,680 --> 06:38:21,600
which is a callback that Returns the
9970
06:38:21,600 --> 06:38:23,900
void
9971
06:38:23,940 --> 06:38:27,480
we add these properties here user unlock
9972
06:38:27,480 --> 06:38:31,320
out successful
9973
06:38:31,620 --> 06:38:36,798
of type navbar locked in viewer props
9974
06:38:37,200 --> 06:38:39,120
then we need a function for logging out
9975
06:38:39,120 --> 06:38:41,280
which we can put directly in this
9976
06:38:41,280 --> 06:38:43,260
component there's no real reason to
9977
06:38:43,260 --> 06:38:45,660
hoist it to the outside in my opinion
9978
06:38:45,660 --> 06:38:47,458
because the navbar is the only place
9979
06:38:47,458 --> 06:38:49,440
where we have a logout button
9980
06:38:49,440 --> 06:38:53,218
so we create an async function that we
9981
06:38:53,218 --> 06:38:55,260
call logout
9982
06:38:55,260 --> 06:38:58,320
doesn't take any arguments
9983
06:38:58,320 --> 06:39:00,600
try catch as usual because even for
9984
06:39:00,600 --> 06:39:02,100
logging out something can go wrong
9985
06:39:02,100 --> 06:39:03,958
because it's a post request to our
9986
06:39:03,958 --> 06:39:06,240
server which can always fail
9987
06:39:06,240 --> 06:39:09,840
in which case we want to lock the error
9988
06:39:09,840 --> 06:39:12,360
and alert it
9989
06:39:12,360 --> 06:39:15,540
then we need our notes API so I'm going
9990
06:39:15,540 --> 06:39:19,700
to copy this import statement here again
9991
06:39:21,840 --> 06:39:24,420
they wait it doesn't need to return
9992
06:39:24,420 --> 06:39:26,040
anything
9993
06:39:26,040 --> 06:39:30,680
notes API dot logout
9994
06:39:31,378 --> 06:39:34,200
and if this was successful we'll call
9995
06:39:34,200 --> 06:39:36,298
our callback
9996
06:39:36,298 --> 06:39:39,240
so that the ftsx file that contains the
9997
06:39:39,240 --> 06:39:41,340
navbar can then remove the user from
9998
06:39:41,340 --> 06:39:42,240
memory
9999
06:39:42,240 --> 06:39:44,700
and in here we put the content of the
10000
06:39:44,700 --> 06:39:47,040
navbar this will be two elements the
10001
06:39:47,040 --> 06:39:49,558
text we've designed and username and the
10002
06:39:49,558 --> 06:39:51,298
logout button and we want to display
10003
06:39:51,298 --> 06:39:53,040
these two elements here in our enough
10004
06:39:53,040 --> 06:39:54,360
bar later
10005
06:39:54,360 --> 06:39:56,940
as I already explained we can't put two
10006
06:39:56,940 --> 06:39:59,458
elements into a return statement they
10007
06:39:59,458 --> 06:40:01,558
need one parent element we could put
10008
06:40:01,558 --> 06:40:04,020
this into a diff but we basically just
10009
06:40:04,020 --> 06:40:06,298
want to render them as they are inside
10010
06:40:06,298 --> 06:40:09,120
this navbar here so we use a fragment as
10011
06:40:09,120 --> 06:40:11,280
we did earlier and the fragment allows
10012
06:40:11,280 --> 06:40:14,160
us to use two or more attacks in a place
10013
06:40:14,160 --> 06:40:17,840
where we could usually just use one
10014
06:40:18,920 --> 06:40:21,420
with a lowercase b and that's the
10015
06:40:21,420 --> 06:40:22,798
bootstrap one
10016
06:40:22,798 --> 06:40:25,558
dot text
10017
06:40:25,558 --> 06:40:27,718
we add the class name
10018
06:40:27,718 --> 06:40:30,780
m e minus 2 which adds some margin to
10019
06:40:30,780 --> 06:40:32,700
the end of this text
10020
06:40:32,700 --> 06:40:36,780
and this will say signed in as colon
10021
06:40:36,780 --> 06:40:39,120
and then we use a variable
10022
06:40:39,120 --> 06:40:42,058
we want to print user.username
10023
06:40:42,058 --> 06:40:43,860
this will be shown in the nuff bar and
10024
06:40:43,860 --> 06:40:47,420
then we add our log out button
10025
06:40:47,940 --> 06:40:51,798
which was a log out
10026
06:40:51,840 --> 06:40:54,840
and it needs an unclick attribute
10027
06:40:54,840 --> 06:40:56,400
which will
10028
06:40:56,400 --> 06:41:00,660
simply call our log out function up here
10029
06:41:00,660 --> 06:41:03,120
which will then handle the rest
10030
06:41:03,120 --> 06:41:05,760
now we also need a locked out navbar
10031
06:41:05,760 --> 06:41:09,480
viewer so we create another component
10032
06:41:09,480 --> 06:41:15,180
nav bar locked out viewer.tsx
10033
06:41:16,320 --> 06:41:18,840
sfc
10034
06:41:18,840 --> 06:41:20,940
this doesn't need a user because there
10035
06:41:20,940 --> 06:41:22,980
is no user logged in but it needs
10036
06:41:22,980 --> 06:41:25,080
callbacks for when we click the login or
10037
06:41:25,080 --> 06:41:26,580
sign up button
10038
06:41:26,580 --> 06:41:29,480
the interface
10039
06:41:29,480 --> 06:41:34,620
navbar locked out viewer props
10040
06:41:34,620 --> 06:41:37,320
and we need two callbacks
10041
06:41:37,320 --> 06:41:41,120
on design up clicked
10042
06:41:42,298 --> 06:41:44,700
and
10043
06:41:44,700 --> 06:41:46,680
can you guess it
10044
06:41:46,680 --> 06:41:48,900
unlock and clicked
10045
06:41:48,900 --> 06:41:51,420
we add these here
10046
06:41:51,420 --> 06:41:54,120
on sign up clicked
10047
06:41:54,120 --> 06:41:56,878
on login clicked
10048
06:41:56,878 --> 06:42:01,980
the type is navbar locked out view props
10049
06:42:01,980 --> 06:42:04,020
and then in here we have set up this
10050
06:42:04,020 --> 06:42:04,860
View
10051
06:42:04,860 --> 06:42:07,200
again we create a fragment that will
10052
06:42:07,200 --> 06:42:10,400
contain two buttons
10053
06:42:12,420 --> 06:42:16,200
the first one triggers our on sign up
10054
06:42:16,200 --> 06:42:18,420
clicked callback
10055
06:42:18,420 --> 06:42:21,980
and it says sign up
10056
06:42:22,260 --> 06:42:24,240
the second button
10057
06:42:24,240 --> 06:42:29,040
triggers our on login clicked callback
10058
06:42:29,040 --> 06:42:31,260
and the text says
10059
06:42:31,260 --> 06:42:33,860
login
10060
06:42:34,680 --> 06:42:38,458
that's it for the navbar locked out View
10061
06:42:38,458 --> 06:42:41,280
now let's go back to the navbar TSX file
10062
06:42:41,280 --> 06:42:44,120
and finish this
10063
06:42:44,160 --> 06:42:47,760
So Below enough bar brand we add navbar
10064
06:42:47,760 --> 06:42:50,340
dot talker
10065
06:42:50,340 --> 06:42:52,200
this is this button that will later
10066
06:42:52,200 --> 06:42:54,480
appear when the screen is small where we
10067
06:42:54,480 --> 06:42:56,878
can expand and collapse the manual in
10068
06:42:56,878 --> 06:42:59,160
Mobile viewer this needs a property
10069
06:42:59,160 --> 06:43:02,638
called area controls
10070
06:43:02,638 --> 06:43:05,340
which defines what menu this talker is
10071
06:43:05,340 --> 06:43:07,740
responsible for it will be responsible
10072
06:43:07,740 --> 06:43:11,638
for the main navbar
10073
06:43:11,638 --> 06:43:14,400
so we type it in like this
10074
06:43:14,400 --> 06:43:16,860
and then below we add enough bar
10075
06:43:16,860 --> 06:43:19,740
dot collapse which we give the same ID
10076
06:43:19,740 --> 06:43:21,900
here so this connects this toggle with
10077
06:43:21,900 --> 06:43:23,458
this navbar collapse which is the
10078
06:43:23,458 --> 06:43:24,900
collapsible manual
10079
06:43:24,900 --> 06:43:26,940
so we said the idea
10080
06:43:26,940 --> 06:43:29,878
to main navbar here as well you can set
10081
06:43:29,878 --> 06:43:32,100
any name you want they just have to be
10082
06:43:32,100 --> 06:43:33,480
identical
10083
06:43:33,480 --> 06:43:35,160
close this
10084
06:43:35,160 --> 06:43:39,420
and in here we put this enough Tech
10085
06:43:39,420 --> 06:43:42,540
which just aligns the elements probably
10086
06:43:42,540 --> 06:43:46,200
give it a class name of MS Auto
10087
06:43:46,200 --> 06:43:48,540
which adds a margin start of order to it
10088
06:43:48,540 --> 06:43:50,280
which means that these buttons and these
10089
06:43:50,280 --> 06:43:51,900
elements will be moved all the way to
10090
06:43:51,900 --> 06:43:53,940
the right so they will be at the end of
10091
06:43:53,940 --> 06:43:56,340
our navbar
10092
06:43:56,340 --> 06:43:58,558
so we close this as well
10093
06:43:58,558 --> 06:44:00,540
and then what we show in here is either
10094
06:44:00,540 --> 06:44:02,700
our navbar locked in or our locked audio
10095
06:44:02,700 --> 06:44:05,638
depending if we are locked in or not so
10096
06:44:05,638 --> 06:44:07,500
we add a pair of curly braces and check
10097
06:44:07,500 --> 06:44:09,780
our locked in user
10098
06:44:09,780 --> 06:44:13,440
which we passed with the snuff bar
10099
06:44:13,440 --> 06:44:16,138
in the next line we add a question mark
10100
06:44:16,138 --> 06:44:18,480
if there is a locked in user we want to
10101
06:44:18,480 --> 06:44:23,520
show our navbar locked in viewer
10102
06:44:23,520 --> 06:44:26,760
which expects some arguments it expects
10103
06:44:26,760 --> 06:44:27,900
the user
10104
06:44:27,900 --> 06:44:29,700
which is the locked in user which now
10105
06:44:29,700 --> 06:44:31,558
can't be null anymore because we have
10106
06:44:31,558 --> 06:44:34,260
this checkup here
10107
06:44:34,260 --> 06:44:38,638
and our on logout successful callback
10108
06:44:38,638 --> 06:44:41,458
which we forward to unlock out
10109
06:44:41,458 --> 06:44:43,080
successful
10110
06:44:43,080 --> 06:44:45,240
which is the property that repairs to
10111
06:44:45,240 --> 06:44:47,520
the navbar component
10112
06:44:47,520 --> 06:44:49,080
now as you can see when you have
10113
06:44:49,080 --> 06:44:51,000
components and other components you
10114
06:44:51,000 --> 06:44:53,160
often have to forward properties like
10115
06:44:53,160 --> 06:44:55,740
this so we have unlock out successful in
10116
06:44:55,740 --> 06:44:57,840
the nav bar but then we also have it in
10117
06:44:57,840 --> 06:44:59,760
the navbar locked in viewer to a certain
10118
06:44:59,760 --> 06:45:01,920
point this is okay this is called prop
10119
06:45:01,920 --> 06:45:04,020
drilling because you trade these props
10120
06:45:04,020 --> 06:45:06,360
into different layers if this gets too
10121
06:45:06,360 --> 06:45:08,458
much there are ways around this for
10122
06:45:08,458 --> 06:45:10,680
example your react context which you can
10123
06:45:10,680 --> 06:45:12,420
Google there's also something called
10124
06:45:12,420 --> 06:45:15,000
react query which gives some convenience
10125
06:45:15,000 --> 06:45:17,340
functions and hooks for when you will
10126
06:45:17,340 --> 06:45:19,320
fetch data from an API but this is
10127
06:45:19,320 --> 06:45:21,180
beyond the scope of this tutorial here
10128
06:45:21,180 --> 06:45:23,218
we use good old prop trailing because
10129
06:45:23,218 --> 06:45:24,900
you need to get a feel for how this
10130
06:45:24,900 --> 06:45:26,760
works when you are a beginner
10131
06:45:26,760 --> 06:45:29,638
and below this line we write colon for
10132
06:45:29,638 --> 06:45:32,100
when the user is null
10133
06:45:32,100 --> 06:45:34,920
then we want to show the navbar locked
10134
06:45:34,920 --> 06:45:37,500
out viewer
10135
06:45:37,500 --> 06:45:39,900
which needs to unlock and clicked
10136
06:45:39,900 --> 06:45:43,138
callback which we forward to unlock and
10137
06:45:43,138 --> 06:45:45,240
clicked
10138
06:45:45,240 --> 06:45:49,820
and the same for on sign up clicked
10139
06:45:50,458 --> 06:45:52,558
okay and after saving this we can see
10140
06:45:52,558 --> 06:45:55,980
that our token menu here appears
10141
06:45:55,980 --> 06:45:57,958
let's make this bigger because at a
10142
06:45:57,958 --> 06:45:59,580
certain point
10143
06:45:59,580 --> 06:46:02,520
this will not collapse anymore again the
10144
06:46:02,520 --> 06:46:04,500
size depends on what you want to put in
10145
06:46:04,500 --> 06:46:06,718
here we can actually make this expand
10146
06:46:06,718 --> 06:46:08,820
Point smaller because we only have these
10147
06:46:08,820 --> 06:46:11,520
two buttons here so let's set this to SM
10148
06:46:11,520 --> 06:46:14,040
which should be small so now it should
10149
06:46:14,040 --> 06:46:16,080
collapse a little bit later
10150
06:46:16,080 --> 06:46:19,020
so you can still it's still expanded
10151
06:46:19,020 --> 06:46:21,120
and here then collapses
10152
06:46:21,120 --> 06:46:23,218
I think that's more appropriate you can
10153
06:46:23,218 --> 06:46:25,200
set this back to LG if you add more
10154
06:46:25,200 --> 06:46:27,298
stuff to the navbar that just gets too
10155
06:46:27,298 --> 06:46:29,458
long so now we actually want to check if
10156
06:46:29,458 --> 06:46:31,378
the user is locked in and only show the
10157
06:46:31,378 --> 06:46:33,240
nodes in this case again we have to
10158
06:46:33,240 --> 06:46:36,058
handle this in the app TSX file but I
10159
06:46:36,058 --> 06:46:38,100
actually want to organize the code that
10160
06:46:38,100 --> 06:46:40,080
shows our nodes into a separate file
10161
06:46:40,080 --> 06:46:41,580
because it doesn't really belong into
10162
06:46:41,580 --> 06:46:44,580
app TSX it's not called organization and
10163
06:46:44,580 --> 06:46:46,378
also it will make the code messy when we
10164
06:46:46,378 --> 06:46:48,718
now have to add another conditioner that
10165
06:46:48,718 --> 06:46:51,840
checks if the user is null or not so
10166
06:46:51,840 --> 06:46:53,400
what we do is we create another
10167
06:46:53,400 --> 06:46:55,740
component
10168
06:46:55,740 --> 06:46:59,540
in the components folder
10169
06:46:59,760 --> 06:47:04,680
which we call nodes page locked in
10170
06:47:04,680 --> 06:47:07,680
viewer.tsx
10171
06:47:09,600 --> 06:47:11,878
create a functional component with the
10172
06:47:11,878 --> 06:47:13,138
same name
10173
06:47:13,138 --> 06:47:15,000
and then we want to cut out some code
10174
06:47:15,000 --> 06:47:17,280
from the ftsx file and move it over
10175
06:47:17,280 --> 06:47:18,058
there
10176
06:47:18,058 --> 06:47:20,760
the authentication models will stay in
10177
06:47:20,760 --> 06:47:24,780
the ftsx file but all these added and
10178
06:47:24,780 --> 06:47:26,760
add nodes dialogues belonging to our
10179
06:47:26,760 --> 06:47:28,920
newer logged in viewer because there
10180
06:47:28,920 --> 06:47:31,080
should only be available if a user is
10181
06:47:31,080 --> 06:47:35,520
logged in so we cut out node to edit
10182
06:47:35,520 --> 06:47:38,280
show add node dialog
10183
06:47:38,280 --> 06:47:41,218
the loading Spinners and everything
10184
06:47:41,218 --> 06:47:44,218
all the way up here to this button
10185
06:47:44,218 --> 06:47:46,558
cut this out
10186
06:47:46,558 --> 06:47:48,540
paste it over here
10187
06:47:48,540 --> 06:47:51,420
but since those are multiple elements we
10188
06:47:51,420 --> 06:47:54,660
have to put into a fragment like this
10189
06:47:54,660 --> 06:47:56,040
now we have to fix some import
10190
06:47:56,040 --> 06:47:58,378
statements and you can do this by just
10191
06:47:58,378 --> 06:48:00,240
removing the last letter
10192
06:48:00,240 --> 06:48:02,218
and then Auto completion will help you
10193
06:48:02,218 --> 06:48:04,920
on importing this again
10194
06:48:04,920 --> 06:48:07,798
at least for the components
10195
06:48:07,798 --> 06:48:10,260
add edit note dialog
10196
06:48:10,260 --> 06:48:12,958
the other stuff here is mostly still
10197
06:48:12,958 --> 06:48:14,458
over here
10198
06:48:14,458 --> 06:48:17,100
we need to copy over some states
10199
06:48:17,100 --> 06:48:20,420
basically all of those
10200
06:48:21,058 --> 06:48:24,138
move them in here
10201
06:48:26,340 --> 06:48:28,920
import use state
10202
06:48:28,920 --> 06:48:32,100
Auto completion doesn't work
10203
06:48:32,100 --> 06:48:35,840
let's copy it from over here
10204
06:48:39,420 --> 06:48:42,180
we need our node model
10205
06:48:42,180 --> 06:48:44,340
here we have to import this manually as
10206
06:48:44,340 --> 06:48:47,900
well because of the name
10207
06:48:47,940 --> 06:48:50,340
so we copy this line
10208
06:48:50,340 --> 06:48:52,320
put it in here
10209
06:48:52,320 --> 06:48:54,240
we have to fix the folder where it's
10210
06:48:54,240 --> 06:48:55,920
coming from I think we only have to add
10211
06:48:55,920 --> 06:48:58,080
the dot here yeah that works
10212
06:48:58,080 --> 06:49:01,620
and we need our style utils let's copy
10213
06:49:01,620 --> 06:49:03,240
this as well
10214
06:49:03,240 --> 06:49:06,360
paste it here fix the import as well
10215
06:49:06,360 --> 06:49:09,920
should look like this I think
10216
06:49:10,020 --> 06:49:13,740
import a spinner from react bootstrap
10217
06:49:13,740 --> 06:49:16,260
and our notes grid is still over there
10218
06:49:16,260 --> 06:49:18,900
of course we need that as well and we
10219
06:49:18,900 --> 06:49:21,120
also need our delete node and load nodes
10220
06:49:21,120 --> 06:49:23,340
functions actually the whole user effect
10221
06:49:23,340 --> 06:49:25,978
so we cut our use effect with load nodes
10222
06:49:25,978 --> 06:49:28,620
we cut our delete node and our whole
10223
06:49:28,620 --> 06:49:30,540
node squared
10224
06:49:30,540 --> 06:49:32,520
and even though it's a bit messy right
10225
06:49:32,520 --> 06:49:35,100
now it will be much better organized
10226
06:49:35,100 --> 06:49:37,440
after we have done this
10227
06:49:37,440 --> 06:49:39,660
so let's paste this here
10228
06:49:39,660 --> 06:49:42,240
let's do some imports missing
10229
06:49:42,240 --> 06:49:45,500
notes API
10230
06:49:51,298 --> 06:49:53,940
diodes is missing
10231
06:49:53,940 --> 06:49:56,940
this one here notes page
10232
06:49:56,940 --> 06:49:59,638
this is by the way why I call this notes
10233
06:49:59,638 --> 06:50:02,940
page module zss and not app module CSS
10234
06:50:02,940 --> 06:50:05,160
because I knew that we have to refactor
10235
06:50:05,160 --> 06:50:06,780
this later
10236
06:50:06,780 --> 06:50:08,820
okay and we need to import the row from
10237
06:50:08,820 --> 06:50:11,100
react bootstrap
10238
06:50:11,100 --> 06:50:14,058
and coil
10239
06:50:14,160 --> 06:50:17,958
and our node component
10240
06:50:18,420 --> 06:50:20,218
and this looks good
10241
06:50:20,218 --> 06:50:22,558
I think we got everything so let's
10242
06:50:22,558 --> 06:50:24,600
organize the import statements properly
10243
06:50:24,600 --> 06:50:27,718
for which there is a shortcut shift alt
10244
06:50:27,718 --> 06:50:29,280
or
10245
06:50:29,280 --> 06:50:31,500
which organizes them properly and gets
10246
06:50:31,500 --> 06:50:34,320
rid of the one we don't use
10247
06:50:34,320 --> 06:50:36,240
format and save the file and let's do
10248
06:50:36,240 --> 06:50:39,420
the same over here shift alt over
10249
06:50:39,420 --> 06:50:42,660
shift IDF to align everything properly
10250
06:50:42,660 --> 06:50:44,940
and let's save this
10251
06:50:44,940 --> 06:50:47,218
then we want to create a little notes
10252
06:50:47,218 --> 06:50:49,440
page locked out viewer but don't worry
10253
06:50:49,440 --> 06:50:51,000
this will be a
10254
06:50:51,000 --> 06:50:53,340
fast because you just print some text
10255
06:50:53,340 --> 06:50:56,580
there so another component
10256
06:50:56,580 --> 06:51:03,680
notes page locked out viewer dot TSX
10257
06:51:04,200 --> 06:51:07,080
we create a functional component
10258
06:51:07,080 --> 06:51:08,700
this one doesn't need to take any
10259
06:51:08,700 --> 06:51:10,558
arguments
10260
06:51:10,558 --> 06:51:13,798
and it will return a paragraph tag which
10261
06:51:13,798 --> 06:51:16,020
says please log in
10262
06:51:16,020 --> 06:51:18,718
to zero nodes of course you can always
10263
06:51:18,718 --> 06:51:20,760
make this more sophisticated
10264
06:51:20,760 --> 06:51:23,100
in your real app but for this tutorial
10265
06:51:23,100 --> 06:51:24,660
this will do files
10266
06:51:24,660 --> 06:51:26,340
okay and now we have to connect
10267
06:51:26,340 --> 06:51:28,860
everything in our app TSX file set up
10268
06:51:28,860 --> 06:51:30,780
the models and fetch the user and
10269
06:51:30,780 --> 06:51:33,120
everything and then we will be able to
10270
06:51:33,120 --> 06:51:35,340
log in and out from our front end so
10271
06:51:35,340 --> 06:51:37,020
this was a lot of work but we are almost
10272
06:51:37,020 --> 06:51:39,900
done with this part okay first of all we
10273
06:51:39,900 --> 06:51:43,378
need a state for the locked in user
10274
06:51:43,378 --> 06:51:45,840
when we open our page which Ibex
10275
06:51:45,840 --> 06:51:48,298
executes this app component we will try
10276
06:51:48,298 --> 06:51:50,160
to fetch the locked in user from the
10277
06:51:50,160 --> 06:51:52,558
back end by making a request to the get
10278
06:51:52,558 --> 06:51:54,900
authenticated user endpoint and remember
10279
06:51:54,900 --> 06:51:56,700
we are automatically sending our
10280
06:51:56,700 --> 06:51:58,798
credential cookies so we either get the
10281
06:51:58,798 --> 06:52:00,900
user back if we are logged in or we get
10282
06:52:00,900 --> 06:52:03,120
the 401 response if we are not logged in
10283
06:52:03,120 --> 06:52:05,340
in which case we will just ignore this
10284
06:52:05,340 --> 06:52:07,860
and then we can use our website as a not
10285
06:52:07,860 --> 06:52:12,420
locked in user so we call this locked in
10286
06:52:12,420 --> 06:52:19,878
user and The Zeta Zed locked in user
10287
06:52:20,218 --> 06:52:22,500
which is a used State we have to import
10288
06:52:22,500 --> 06:52:24,958
this again from react
10289
06:52:24,958 --> 06:52:28,920
let's copy it from over here
10290
06:52:28,920 --> 06:52:31,740
we will need to use effect as well
10291
06:52:31,740 --> 06:52:33,958
use state
10292
06:52:33,958 --> 06:52:37,320
of type user or Nile
10293
06:52:37,320 --> 06:52:40,080
if there is no user logged in and we
10294
06:52:40,080 --> 06:52:42,240
initialize this with no
10295
06:52:42,240 --> 06:52:46,638
and we have to import our user type here
10296
06:52:47,820 --> 06:52:50,638
we will also two states that indicate if
10297
06:52:50,638 --> 06:52:52,860
the sign up or the login model are
10298
06:52:52,860 --> 06:52:54,058
showing
10299
06:52:54,058 --> 06:52:57,478
So Below const
10300
06:52:57,478 --> 06:52:58,680
show
10301
06:52:58,680 --> 06:53:01,700
sign up model
10302
06:53:02,218 --> 06:53:07,458
and set a shower sign up model
10303
06:53:09,298 --> 06:53:11,218
which is the use state that we
10304
06:53:11,218 --> 06:53:13,500
initialize with faults
10305
06:53:13,500 --> 06:53:16,860
entry duplicators for login
10306
06:53:16,860 --> 06:53:21,240
so show login model and set show login
10307
06:53:21,240 --> 06:53:22,558
model
10308
06:53:22,558 --> 06:53:24,780
like this
10309
06:53:24,780 --> 06:53:27,420
then we need a user effect because as I
10310
06:53:27,420 --> 06:53:29,340
said as soon as we open this page we
10311
06:53:29,340 --> 06:53:31,860
want to try loading the user
10312
06:53:31,860 --> 06:53:33,718
which is a side effect
10313
06:53:33,718 --> 06:53:36,478
so we add a user effect like this
10314
06:53:36,478 --> 06:53:38,878
and before we forget it we add an empty
10315
06:53:38,878 --> 06:53:41,400
array down here so that we only execute
10316
06:53:41,400 --> 06:53:44,340
this one time when we open the page in
10317
06:53:44,340 --> 06:53:47,820
here we put an async function
10318
06:53:47,820 --> 06:53:52,138
that we call fetch locked in user
10319
06:53:52,138 --> 06:53:55,138
doesn't need any arguments
10320
06:53:55,138 --> 06:53:56,878
and below
10321
06:53:56,878 --> 06:53:59,520
we call this function and I explained
10322
06:53:59,520 --> 06:54:01,138
earlier where we have to do this because
10323
06:54:01,138 --> 06:54:03,360
we can't make this function that we pass
10324
06:54:03,360 --> 06:54:05,520
to use effect async we have to do it
10325
06:54:05,520 --> 06:54:08,580
over this intermediate function
10326
06:54:08,580 --> 06:54:10,860
and this gets a try catch block
10327
06:54:10,860 --> 06:54:13,860
if there is an error including our 401
10328
06:54:13,860 --> 06:54:16,200
response we actually don't want to show
10329
06:54:16,200 --> 06:54:18,420
a dialogue to the user we just want to
10330
06:54:18,420 --> 06:54:20,400
lock the arrow to the console
10331
06:54:20,400 --> 06:54:23,160
because otherwise every time the page is
10332
06:54:23,160 --> 06:54:25,138
opened without cookies it would show an
10333
06:54:25,138 --> 06:54:27,240
error message which we don't want now
10334
06:54:27,240 --> 06:54:29,100
later we will Implement a way to
10335
06:54:29,100 --> 06:54:31,200
distinguish between different errors so
10336
06:54:31,200 --> 06:54:33,120
that we could still show an error
10337
06:54:33,120 --> 06:54:35,580
message if it is not for one response
10338
06:54:35,580 --> 06:54:38,458
but for now this is sufficient
10339
06:54:38,458 --> 06:54:41,280
and here we will try to fetch the user
10340
06:54:41,280 --> 06:54:44,638
and save the return value in a variable
10341
06:54:44,638 --> 06:54:48,240
so Constitution equates a weight user
10342
06:54:48,240 --> 06:54:52,400
notes API we have to import this
10343
06:54:53,600 --> 06:54:56,638
dot get locked in user which we had set
10344
06:54:56,638 --> 06:54:58,320
up earlier or the import statement is
10345
06:54:58,320 --> 06:55:00,540
not correct
10346
06:55:00,540 --> 06:55:03,058
like this
10347
06:55:03,058 --> 06:55:05,218
doesn't need any input because again it
10348
06:55:05,218 --> 06:55:07,620
will automatically send the cookie that
10349
06:55:07,620 --> 06:55:10,020
we have stored
10350
06:55:10,020 --> 06:55:12,298
and if this went without an error then
10351
06:55:12,298 --> 06:55:15,240
we want to set our locked in user state
10352
06:55:15,240 --> 06:55:17,458
to what we get back
10353
06:55:17,458 --> 06:55:19,620
and we are almost done
10354
06:55:19,620 --> 06:55:22,138
to our nav bar down here
10355
06:55:22,138 --> 06:55:25,200
we want to pass the locked in user and
10356
06:55:25,200 --> 06:55:26,458
enough bar will then use this
10357
06:55:26,458 --> 06:55:28,740
information to decide what content to
10358
06:55:28,740 --> 06:55:31,260
show but we will see this in a bit
10359
06:55:31,260 --> 06:55:34,440
on login click it will set a show login
10360
06:55:34,440 --> 06:55:36,780
model to true
10361
06:55:36,780 --> 06:55:39,600
and on sign up click it will set show
10362
06:55:39,600 --> 06:55:43,138
sign up model tutorial
10363
06:55:43,138 --> 06:55:46,500
and in on logout successful we know that
10364
06:55:46,500 --> 06:55:49,378
we successfully destroyed the session
10365
06:55:49,378 --> 06:55:52,020
then we want to set locked in user back
10366
06:55:52,020 --> 06:55:53,400
to null
10367
06:55:53,400 --> 06:55:55,920
and the beauty of declarative UI is that
10368
06:55:55,920 --> 06:55:57,420
all of this will stay in sync
10369
06:55:57,420 --> 06:55:59,280
automatically when we set the locked in
10370
06:55:59,280 --> 06:56:00,840
user to null our navbar will
10371
06:56:00,840 --> 06:56:02,820
automatically be re-rendered with the
10372
06:56:02,820 --> 06:56:04,740
Nile logged in user and the same after
10373
06:56:04,740 --> 06:56:06,540
logging in but you will see there's an
10374
06:56:06,540 --> 06:56:08,580
action in a short moment
10375
06:56:08,580 --> 06:56:11,400
here for a sign up model we use our show
10376
06:56:11,400 --> 06:56:14,160
sign up model state
10377
06:56:14,160 --> 06:56:18,000
and here show login model
10378
06:56:18,000 --> 06:56:21,540
and up here we want to show the content
10379
06:56:21,540 --> 06:56:23,638
of this page depending if we are logged
10380
06:56:23,638 --> 06:56:25,920
in or not so we have to add a fragment
10381
06:56:25,920 --> 06:56:28,020
because otherwise we can't add this
10382
06:56:28,020 --> 06:56:29,878
conditional here
10383
06:56:29,878 --> 06:56:33,660
here we check for the locked in user
10384
06:56:33,660 --> 06:56:36,240
if there is a locked in user we want to
10385
06:56:36,240 --> 06:56:40,760
display the notes page locked in viewer
10386
06:56:41,340 --> 06:56:43,558
and otherwise
10387
06:56:43,558 --> 06:56:47,400
the notes page locked out View
10388
06:56:47,400 --> 06:56:49,740
again this automatically re-runnels when
10389
06:56:49,740 --> 06:56:52,860
this logged in user State changes and
10390
06:56:52,860 --> 06:56:54,420
these two components don't take any
10391
06:56:54,420 --> 06:56:55,978
props because they are completely
10392
06:56:55,978 --> 06:56:57,840
self-contained they handle their own
10393
06:56:57,840 --> 06:56:58,680
state
10394
06:56:58,680 --> 06:57:01,020
let's actually take these models and
10395
06:57:01,020 --> 06:57:03,180
move them out of this container it
10396
06:57:03,180 --> 06:57:05,100
doesn't matter because the models are
10397
06:57:05,100 --> 06:57:07,440
not affected by the container
10398
06:57:07,440 --> 06:57:10,440
but for better overview I want to have
10399
06:57:10,440 --> 06:57:13,320
them in this outer diff here
10400
06:57:13,320 --> 06:57:15,478
and now that I save this file you can
10401
06:57:15,478 --> 06:57:17,638
see that our navbar already displays the
10402
06:57:17,638 --> 06:57:20,218
locked in view really cool but we still
10403
06:57:20,218 --> 06:57:22,978
have to fill this callbacks down here so
10404
06:57:22,978 --> 06:57:25,020
on dismiss in the sign up model we'll
10405
06:57:25,020 --> 06:57:29,840
set our sign up model back to false
10406
06:57:29,940 --> 06:57:32,458
on this Mist down here we'll set your
10407
06:57:32,458 --> 06:57:35,218
login model to false
10408
06:57:35,218 --> 06:57:38,458
and remember when we designed in or log
10409
06:57:38,458 --> 06:57:41,280
in then we get the user back from the
10410
06:57:41,280 --> 06:57:44,160
API so that we don't have to run our
10411
06:57:44,160 --> 06:57:46,200
fetch locked in user again
10412
06:57:46,200 --> 06:57:48,360
instead we can set our user State
10413
06:57:48,360 --> 06:57:50,638
directly from the return value
10414
06:57:50,638 --> 06:57:52,680
so both of them
10415
06:57:52,680 --> 06:57:55,558
get a user test back because this is
10416
06:57:55,558 --> 06:57:59,100
what we defined here in this callback
10417
06:57:59,100 --> 06:58:02,458
in both of them we want to set the
10418
06:58:02,458 --> 06:58:05,280
logged in user to the return value so I
10419
06:58:05,280 --> 06:58:07,558
duplicate the cursor like this and I
10420
06:58:07,558 --> 06:58:10,440
type Z locked in user
10421
06:58:10,440 --> 06:58:13,978
and I pass the return value
10422
06:58:13,978 --> 06:58:16,020
like this
10423
06:58:16,020 --> 06:58:18,058
entry also want to close the models
10424
06:58:18,058 --> 06:58:19,020
again
10425
06:58:19,020 --> 06:58:24,180
so set show sign up model faults
10426
06:58:24,180 --> 06:58:29,040
and that show login model faults
10427
06:58:29,040 --> 06:58:31,558
and then we save everything
10428
06:58:31,558 --> 06:58:33,478
okay and now let's try the different
10429
06:58:33,478 --> 06:58:36,180
states so when we log out the user
10430
06:58:36,180 --> 06:58:38,100
should be set to null if it went
10431
06:58:38,100 --> 06:58:40,080
successful and our nodes should
10432
06:58:40,080 --> 06:58:42,180
disappear and we should see the locked
10433
06:58:42,180 --> 06:58:43,920
out view on there it is we will fix the
10434
06:58:43,920 --> 06:58:47,780
padding later but for now this is cool
10435
06:58:47,940 --> 06:58:51,900
let's try to log in Florian tour with a
10436
06:58:51,900 --> 06:58:53,520
wrong password first
10437
06:58:53,520 --> 06:58:55,978
invalid credentials we will put this
10438
06:58:55,978 --> 06:58:57,780
error message into the log and dialog
10439
06:58:57,780 --> 06:58:59,700
later instead of this alert dialog
10440
06:58:59,700 --> 06:59:02,700
because this is a bit annoying but for
10441
06:59:02,700 --> 06:59:04,558
now this is fine
10442
06:59:04,558 --> 06:59:07,798
log in it works the browser suggests to
10443
06:59:07,798 --> 06:59:09,660
save the password don't do this right
10444
06:59:09,660 --> 06:59:11,878
now and now we can see our notes again
10445
06:59:11,878 --> 06:59:14,458
and enough bar updates and again this is
10446
06:59:14,458 --> 06:59:16,620
the beauty of declarative UI everything
10447
06:59:16,620 --> 06:59:18,840
is synchronized through our state
10448
06:59:18,840 --> 06:59:21,180
automatically basically as long as we
10449
06:59:21,180 --> 06:59:23,638
declare it correctly of course right now
10450
06:59:23,638 --> 06:59:26,218
nodes are not tied to a specific user
10451
06:59:26,218 --> 06:59:29,638
yet so if I log out to a Florian tour
10452
06:59:29,638 --> 06:59:32,760
and log in with Florian one or just
10453
06:59:32,760 --> 06:59:36,120
Florian then we see the same notes oh I
10454
06:59:36,120 --> 06:59:38,280
forgot my credentials okay one two three
10455
06:59:38,280 --> 06:59:41,400
four five six right here
10456
06:59:41,400 --> 06:59:43,378
then we see the same nodes as in the
10457
06:59:43,378 --> 06:59:45,240
other account we will make it so that
10458
06:59:45,240 --> 06:59:47,820
each user has their own nodes later in
10459
06:59:47,820 --> 06:59:50,218
this tutorial but at least signing in
10460
06:59:50,218 --> 06:59:51,900
and out Works already
10461
06:59:51,900 --> 06:59:53,878
let's also try out if the cookie
10462
06:59:53,878 --> 06:59:55,978
expiration works so I go into our
10463
06:59:55,978 --> 06:59:58,020
backend code once again
10464
06:59:58,020 --> 07:00:00,718
and I'm gonna set this to a I don't know
10465
07:00:00,718 --> 07:00:03,840
20 seconds but I will we wrote this in a
10466
07:00:03,840 --> 07:00:06,360
moment just to see if this expires
10467
07:00:06,360 --> 07:00:10,080
correctly so I saved this I log out here
10468
07:00:10,080 --> 07:00:12,120
then I log in again to create a new
10469
07:00:12,120 --> 07:00:14,360
session
10470
07:00:16,320 --> 07:00:18,660
and then I wait 20 seconds and refresh
10471
07:00:18,660 --> 07:00:20,638
the page and see if we are still logged
10472
07:00:20,638 --> 07:00:22,798
in we shouldn't because I set the cookie
10473
07:00:22,798 --> 07:00:25,500
max age to a 20 seconds but this will
10474
07:00:25,500 --> 07:00:27,840
automatically refresh the session if we
10475
07:00:27,840 --> 07:00:29,700
do something on the page where the
10476
07:00:29,700 --> 07:00:31,440
session is still valid but if we don't
10477
07:00:31,440 --> 07:00:34,378
do anything this will expire okay 20
10478
07:00:34,378 --> 07:00:36,600
seconds have passed and when I refresh
10479
07:00:36,600 --> 07:00:38,400
this we are not locked in anymore
10480
07:00:38,400 --> 07:00:41,040
because the session expired but I want
10481
07:00:41,040 --> 07:00:43,920
to keep this at one hour or however long
10482
07:00:43,920 --> 07:00:45,360
you want you can also increase this
10483
07:00:45,360 --> 07:00:47,340
value and yeah the next step is to
10484
07:00:47,340 --> 07:00:49,860
actually tie nodes to a specific user
10485
07:00:49,860 --> 07:00:54,080
accounts so that's what we will do now
10486
07:00:57,660 --> 07:01:00,298
in order to tie a node to a specific
10487
07:01:00,298 --> 07:01:02,280
user we have to make a little change to
10488
07:01:02,280 --> 07:01:04,920
the node schema in our backend code so
10489
07:01:04,920 --> 07:01:08,400
let's search for the notes model
10490
07:01:08,400 --> 07:01:10,320
in our backend here
10491
07:01:10,320 --> 07:01:12,298
here we want to add one additional field
10492
07:01:12,298 --> 07:01:14,280
to the schema I'm going to put it at the
10493
07:01:14,280 --> 07:01:15,958
top you can also put it somewhere else
10494
07:01:15,958 --> 07:01:17,638
doesn't matter
10495
07:01:17,638 --> 07:01:20,340
we want to store the ID of the user that
10496
07:01:20,340 --> 07:01:22,320
created this nodes so later when we
10497
07:01:22,320 --> 07:01:24,540
retrieve nodes we can only retrieve the
10498
07:01:24,540 --> 07:01:26,878
notes for a particular user
10499
07:01:26,878 --> 07:01:30,180
the type of this field will be
10500
07:01:30,180 --> 07:01:32,400
mongoose's object ID type that we
10501
07:01:32,400 --> 07:01:34,200
already used earlier that's the one we
10502
07:01:34,200 --> 07:01:37,020
store in our session so we already have
10503
07:01:37,020 --> 07:01:39,240
schema here imported so we can just type
10504
07:01:39,240 --> 07:01:40,920
schema
10505
07:01:40,920 --> 07:01:42,718
dot types
10506
07:01:42,718 --> 07:01:44,760
dot object idea
10507
07:01:44,760 --> 07:01:47,700
and with that required to draw so that
10508
07:01:47,700 --> 07:01:50,100
every node needs a user ID attached to
10509
07:01:50,100 --> 07:01:51,240
it
10510
07:01:51,240 --> 07:01:54,298
comma and then we save these changes
10511
07:01:54,298 --> 07:01:57,298
and then in our notes routes we want to
10512
07:01:57,298 --> 07:01:59,700
do the same as we did in our get
10513
07:01:59,700 --> 07:02:02,760
authenticated user function inside the
10514
07:02:02,760 --> 07:02:05,280
user's controller
10515
07:02:05,280 --> 07:02:09,000
we want to check the idea that's stored
10516
07:02:09,000 --> 07:02:11,160
in the session and if there is no ID
10517
07:02:11,160 --> 07:02:12,840
stored in the session we want to return
10518
07:02:12,840 --> 07:02:15,900
a 401 response because the user is not
10519
07:02:15,900 --> 07:02:17,940
authenticated and they shouldn't be able
10520
07:02:17,940 --> 07:02:19,978
to retrieve or update or delete nodes
10521
07:02:19,978 --> 07:02:22,200
but this means that we have to repeat
10522
07:02:22,200 --> 07:02:24,298
this piece of code with the same error
10523
07:02:24,298 --> 07:02:26,700
message in many different endpoints
10524
07:02:26,700 --> 07:02:28,440
instead of repeating this for every
10525
07:02:28,440 --> 07:02:30,718
endpoint it makes sense to extract this
10526
07:02:30,718 --> 07:02:33,058
logic into our own middleware and then
10527
07:02:33,058 --> 07:02:34,740
we can use this middleware like we used
10528
07:02:34,740 --> 07:02:37,920
any other middleware by attaching it to
10529
07:02:37,920 --> 07:02:40,138
the routes that we want to protect
10530
07:02:40,138 --> 07:02:42,000
so let's do that now
10531
07:02:42,000 --> 07:02:44,940
in the source folder of our backend we
10532
07:02:44,940 --> 07:02:46,978
create a new folder
10533
07:02:46,978 --> 07:02:49,080
which we call middleware
10534
07:02:49,080 --> 07:02:50,878
here you can store all kinds of
10535
07:02:50,878 --> 07:02:54,000
middleware that you create yourself
10536
07:02:54,000 --> 07:02:57,298
and let's call it off.ts
10537
07:02:57,298 --> 07:02:59,280
in middleware is basically just a
10538
07:02:59,280 --> 07:03:01,378
request Handler just like our endpoint
10539
07:03:01,378 --> 07:03:03,000
controllers
10540
07:03:03,000 --> 07:03:05,520
so we create an export const
10541
07:03:05,520 --> 07:03:09,298
let's call it requires of
10542
07:03:09,298 --> 07:03:13,200
of type request Handler
10543
07:03:13,200 --> 07:03:16,700
wreck rest next
10544
07:03:16,740 --> 07:03:20,058
we already know that
10545
07:03:21,600 --> 07:03:24,958
and in here we simply check if the SS
10546
07:03:24,958 --> 07:03:28,558
session in the request object
10547
07:03:28,558 --> 07:03:31,020
that contains the user ID in other words
10548
07:03:31,020 --> 07:03:32,940
if a user is logged in
10549
07:03:32,940 --> 07:03:35,280
that's what we did in get authenticated
10550
07:03:35,280 --> 07:03:38,580
user earlier up here
10551
07:03:38,580 --> 07:03:41,218
then we want to call next and next
10552
07:03:41,218 --> 07:03:43,860
without an error simply occult the next
10553
07:03:43,860 --> 07:03:45,840
middleware so it forwards the request
10554
07:03:45,840 --> 07:03:47,520
and the response object to the next
10555
07:03:47,520 --> 07:03:49,558
middleware in the row which will then be
10556
07:03:49,558 --> 07:03:52,638
the endpoints itself
10557
07:03:53,100 --> 07:03:55,798
else if there is no authenticated user
10558
07:03:55,798 --> 07:03:58,680
we want to throw an HTTP error now we
10559
07:03:58,680 --> 07:04:00,298
could wrap this into a try catch block
10560
07:04:00,298 --> 07:04:02,458
and throw an error here but we can also
10561
07:04:02,458 --> 07:04:03,360
just
10562
07:04:03,360 --> 07:04:07,320
call next and forward the error directly
10563
07:04:07,320 --> 07:04:09,058
and since we don't have any other code
10564
07:04:09,058 --> 07:04:11,100
in here that can throw this is more
10565
07:04:11,100 --> 07:04:12,298
concise
10566
07:04:12,298 --> 07:04:14,878
remember we trigger our error middleware
10567
07:04:14,878 --> 07:04:17,520
at the very end simply by passing the
10568
07:04:17,520 --> 07:04:19,740
arrow to the next function this is also
10569
07:04:19,740 --> 07:04:21,900
what we did in all our endpoints just
10570
07:04:21,900 --> 07:04:23,820
that those were the arrows that we
10571
07:04:23,820 --> 07:04:26,340
caught in the try catch block but we can
10572
07:04:26,340 --> 07:04:28,260
also forward an error to the next
10573
07:04:28,260 --> 07:04:30,900
function directly and here we want to
10574
07:04:30,900 --> 07:04:32,940
use our 401
10575
07:04:32,940 --> 07:04:35,580
around that says
10576
07:04:35,580 --> 07:04:38,638
use are not authenticated
10577
07:04:38,638 --> 07:04:42,320
with the correct spelling of course
10578
07:04:43,260 --> 07:04:45,478
and now we can use this middleware on
10579
07:04:45,478 --> 07:04:47,100
all endpoints that we want to protect
10580
07:04:47,100 --> 07:04:49,680
and we don't have to repeat this chord
10581
07:04:49,680 --> 07:04:52,320
and now let's use this for our endpoints
10582
07:04:52,320 --> 07:04:53,700
and let's start with our get
10583
07:04:53,700 --> 07:04:56,820
authenticated user endpoint here so what
10584
07:04:56,820 --> 07:04:59,280
we do is we remove this check here this
10585
07:04:59,280 --> 07:05:03,000
will be replaced by our new middleware
10586
07:05:03,000 --> 07:05:04,740
and then we can also remove this
10587
07:05:04,740 --> 07:05:07,378
intermediate variable we can keep it but
10588
07:05:07,378 --> 07:05:09,000
we can also just
10589
07:05:09,000 --> 07:05:11,760
press rec.session.usid here directly
10590
07:05:11,760 --> 07:05:15,298
because we don't use it in another place
10591
07:05:15,298 --> 07:05:18,000
we save this then we go to the user's
10592
07:05:18,000 --> 07:05:19,440
routes
10593
07:05:19,440 --> 07:05:22,200
and use this middleware here we want to
10594
07:05:22,200 --> 07:05:24,420
use it for the get authenticated user
10595
07:05:24,420 --> 07:05:25,558
endpoint
10596
07:05:25,558 --> 07:05:28,760
let's see if Auto Import works it does
10597
07:05:28,760 --> 07:05:30,900
requires off
10598
07:05:30,900 --> 07:05:33,478
it adds this import here at the top and
10599
07:05:33,478 --> 07:05:35,040
the Order of these middlewares of course
10600
07:05:35,040 --> 07:05:37,440
matter we first want to check if the
10601
07:05:37,440 --> 07:05:39,660
user is authenticated before we call our
10602
07:05:39,660 --> 07:05:42,298
endpoint Handler otherwise this one here
10603
07:05:42,298 --> 07:05:43,978
would be executed first and then we
10604
07:05:43,978 --> 07:05:45,718
would check for off which doesn't make
10605
07:05:45,718 --> 07:05:46,740
much sense
10606
07:05:46,740 --> 07:05:49,440
so to see if this still works let's save
10607
07:05:49,440 --> 07:05:50,520
our code
10608
07:05:50,520 --> 07:05:53,160
and then open our website again
10609
07:05:53,160 --> 07:05:57,240
so the session has timed out meanwhile
10610
07:05:57,240 --> 07:06:00,240
let's refresh the page and it says user
10611
07:06:00,240 --> 07:06:01,978
not authenticated so we are still
10612
07:06:01,978 --> 07:06:05,160
getting our 401 response
10613
07:06:05,160 --> 07:06:07,020
but we shouldn't get it anymore after
10614
07:06:07,020 --> 07:06:09,620
logging in
10615
07:06:11,160 --> 07:06:14,340
so let's refresh it once again
10616
07:06:14,340 --> 07:06:17,340
and we get our authenticated user with a
10617
07:06:17,340 --> 07:06:20,160
200 response so it seems like our
10618
07:06:20,160 --> 07:06:22,620
endpoint still works properly just that
10619
07:06:22,620 --> 07:06:24,900
we extracted the authentication logic
10620
07:06:24,900 --> 07:06:27,058
into this middleware and now we can
10621
07:06:27,058 --> 07:06:29,280
reuse it on other endpoints as well and
10622
07:06:29,280 --> 07:06:31,378
we basically want to use it on all nodes
10623
07:06:31,378 --> 07:06:32,940
and points right because you can't
10624
07:06:32,940 --> 07:06:34,920
really interact with notes if you are
10625
07:06:34,920 --> 07:06:37,020
not authenticated so let's do that now
10626
07:06:37,020 --> 07:06:40,260
and now we can decide if we want to add
10627
07:06:40,260 --> 07:06:42,540
our requires of metalware only to
10628
07:06:42,540 --> 07:06:44,638
specific node endpoints in which case we
10629
07:06:44,638 --> 07:06:46,500
could add them here but we actually want
10630
07:06:46,500 --> 07:06:48,420
to add it to all of them so we can make
10631
07:06:48,420 --> 07:06:50,760
it easier and edit one level higher we
10632
07:06:50,760 --> 07:06:53,340
can add the middleware right here before
10633
07:06:53,340 --> 07:06:56,400
we forward to any of our notes routes
10634
07:06:56,400 --> 07:06:59,458
so here before notes routes
10635
07:06:59,458 --> 07:07:03,958
we also color requires of
10636
07:07:03,958 --> 07:07:06,958
and now just like that all our nodes and
10637
07:07:06,958 --> 07:07:08,878
points are protected by our
10638
07:07:08,878 --> 07:07:11,458
authentication middleware let's try it
10639
07:07:11,458 --> 07:07:13,080
out in Postman
10640
07:07:13,080 --> 07:07:14,878
first of all let's remove this once
10641
07:07:14,878 --> 07:07:16,740
again for a short moment just to see
10642
07:07:16,740 --> 07:07:19,620
that we can still retrieve all notes if
10643
07:07:19,620 --> 07:07:21,478
we don't have this middleware
10644
07:07:21,478 --> 07:07:23,878
so we want to make a get request to a
10645
07:07:23,878 --> 07:07:26,700
slash API slash nodes
10646
07:07:26,700 --> 07:07:28,860
here which returns us all notes right
10647
07:07:28,860 --> 07:07:29,820
now
10648
07:07:29,820 --> 07:07:33,740
now let's put the middleware back in
10649
07:07:33,780 --> 07:07:36,058
and there are no cookies set right now
10650
07:07:36,058 --> 07:07:38,400
so we shouldn't be authenticated
10651
07:07:38,400 --> 07:07:40,440
and now it says user not authenticated
10652
07:07:40,440 --> 07:07:42,840
with a 401 response which is triggered
10653
07:07:42,840 --> 07:07:45,298
by our middleware and this should also
10654
07:07:45,298 --> 07:07:47,878
work for creating new nodes updating or
10655
07:07:47,878 --> 07:07:49,978
deleting notes and everything else we
10656
07:07:49,978 --> 07:07:52,440
add to the notes routes so let's try
10657
07:07:52,440 --> 07:07:55,200
deleting a node from our database I
10658
07:07:55,200 --> 07:07:57,360
already refresh this just take a random
10659
07:07:57,360 --> 07:08:00,000
note here this idea
10660
07:08:00,000 --> 07:08:03,840
slash notes slash this idea a delete
10661
07:08:03,840 --> 07:08:07,440
request we are not authenticated user
10662
07:08:07,440 --> 07:08:09,600
not authenticated but right now this
10663
07:08:09,600 --> 07:08:12,000
only means that a user has to be logged
10664
07:08:12,000 --> 07:08:14,160
in to interact with the nodes but once
10665
07:08:14,160 --> 07:08:16,138
they are locked in every user can still
10666
07:08:16,138 --> 07:08:18,298
interact with all nodes because we
10667
07:08:18,298 --> 07:08:20,700
haven't added any logic that allows the
10668
07:08:20,700 --> 07:08:22,920
user to only retrieve their own nodes or
10669
07:08:22,920 --> 07:08:24,958
modify their own nodes so let's do that
10670
07:08:24,958 --> 07:08:27,420
now in our notes controller so the
10671
07:08:27,420 --> 07:08:29,458
authentication check is already in place
10672
07:08:29,458 --> 07:08:31,680
we only Reach This endpoint if we are
10673
07:08:31,680 --> 07:08:34,020
authenticated so now what we want to do
10674
07:08:34,020 --> 07:08:36,180
is we want to get the authenticated user
10675
07:08:36,180 --> 07:08:41,638
idea so const authenticated user idea
10676
07:08:41,638 --> 07:08:45,120
from the session so rec.session dot user
10677
07:08:45,120 --> 07:08:46,440
idea
10678
07:08:46,440 --> 07:08:48,420
but now we still have a little bit of a
10679
07:08:48,420 --> 07:08:51,120
type problem because authenticated user
10680
07:08:51,120 --> 07:08:53,520
ID could be possibly undefined there is
10681
07:08:53,520 --> 07:08:55,680
no guarantee that we set user ID on the
10682
07:08:55,680 --> 07:08:57,780
session now we kind of know that there
10683
07:08:57,780 --> 07:08:59,458
is a user ID because we have this
10684
07:08:59,458 --> 07:09:01,260
middleware in between that checks for
10685
07:09:01,260 --> 07:09:03,840
the user idea and throws the 401 error
10686
07:09:03,840 --> 07:09:06,120
if there is none but for one typescript
10687
07:09:06,120 --> 07:09:08,638
doesn't notice it's not smart enough to
10688
07:09:08,638 --> 07:09:10,798
recognize the middleware and say okay
10689
07:09:10,798 --> 07:09:12,780
the user ID will definitely be defined
10690
07:09:12,780 --> 07:09:15,180
but secondly even we can't really know
10691
07:09:15,180 --> 07:09:17,340
it because we can accidentally remove
10692
07:09:17,340 --> 07:09:19,440
this middleware and then this user ID
10693
07:09:19,440 --> 07:09:22,020
could be undefined and as I explained
10694
07:09:22,020 --> 07:09:24,540
earlier using undefined values in our
10695
07:09:24,540 --> 07:09:26,940
code is a bit dangerous because it could
10696
07:09:26,940 --> 07:09:29,100
make our app misbehave the one way to
10697
07:09:29,100 --> 07:09:31,740
handle this is just to be confident that
10698
07:09:31,740 --> 07:09:33,718
we added our middleware everywhere and
10699
07:09:33,718 --> 07:09:35,520
whenever we can't use an undefined value
10700
07:09:35,520 --> 07:09:38,400
we can use the normal assertion operator
10701
07:09:38,400 --> 07:09:40,200
on the exclamation mark but it's
10702
07:09:40,200 --> 07:09:42,000
actually better to have another check in
10703
07:09:42,000 --> 07:09:44,218
place that makes sure that the user ID
10704
07:09:44,218 --> 07:09:47,160
actually has a value so for this I want
10705
07:09:47,160 --> 07:09:48,840
to create another function that does
10706
07:09:48,840 --> 07:09:51,660
this check we put this into the YouTube
10707
07:09:51,660 --> 07:09:53,940
folder of our backend code
10708
07:09:53,940 --> 07:09:59,000
new file let's call it assert is
10709
07:09:59,000 --> 07:10:01,020
defined.ts and let's copy the name
10710
07:10:01,020 --> 07:10:02,760
because the function that we put in
10711
07:10:02,760 --> 07:10:04,558
there will have the same name
10712
07:10:04,558 --> 07:10:07,040
from here we want to export one function
10713
07:10:07,040 --> 07:10:09,240
with this name
10714
07:10:09,240 --> 07:10:11,280
and this function will take a generic
10715
07:10:11,280 --> 07:10:13,500
type argument which we do like this with
10716
07:10:13,500 --> 07:10:15,780
a pair of angle brackets and a big T in
10717
07:10:15,780 --> 07:10:18,120
between and then in between the
10718
07:10:18,120 --> 07:10:21,540
parentheses we write via colon tier
10719
07:10:21,540 --> 07:10:23,760
while it's just a variable name you can
10720
07:10:23,760 --> 07:10:25,740
give this any name you want and if you
10721
07:10:25,740 --> 07:10:27,718
haven't worked with generic types before
10722
07:10:27,718 --> 07:10:29,638
which is a feature that many languages
10723
07:10:29,638 --> 07:10:32,160
have this T basically allows us to pass
10724
07:10:32,160 --> 07:10:34,440
any type to this function so why don't
10725
07:10:34,440 --> 07:10:37,138
we use any instead of tier because we
10726
07:10:37,138 --> 07:10:39,478
actually want to get a non-nullable type
10727
07:10:39,478 --> 07:10:41,520
back there's a feature for that in
10728
07:10:41,520 --> 07:10:44,160
typescript which we do like this we add
10729
07:10:44,160 --> 07:10:46,020
the colon for the return type but
10730
07:10:46,020 --> 07:10:48,780
instead of defining a simple type
10731
07:10:48,780 --> 07:10:51,240
we write asserts which is a special
10732
07:10:51,240 --> 07:10:54,320
typescript keyword
10733
07:10:56,420 --> 07:10:59,700
and then again of this type tier and
10734
07:10:59,700 --> 07:11:01,260
then we add curly braces for the
10735
07:11:01,260 --> 07:11:03,780
function body now if this is confusing
10736
07:11:03,780 --> 07:11:05,458
to you don't worry you don't need to
10737
07:11:05,458 --> 07:11:06,840
understand this in detail and this
10738
07:11:06,840 --> 07:11:08,878
function will be very small it's just a
10739
07:11:08,878 --> 07:11:10,860
nice feature to use but as a simple
10740
07:11:10,860 --> 07:11:13,440
explanation this function allows us to
10741
07:11:13,440 --> 07:11:15,958
pass any type to it and then it will
10742
07:11:15,958 --> 07:11:17,820
check that this type is not null and
10743
07:11:17,820 --> 07:11:19,440
then on the outside where we use this
10744
07:11:19,440 --> 07:11:21,780
function we get the non-nullable type
10745
07:11:21,780 --> 07:11:23,940
bag that's basically it and this also
10746
07:11:23,940 --> 07:11:25,798
works for undefined so if we use this
10747
07:11:25,798 --> 07:11:28,020
for our possibly undefined user ID here
10748
07:11:28,020 --> 07:11:30,000
we can use this function and then know
10749
07:11:30,000 --> 07:11:32,520
that it is indeed an object idea
10750
07:11:32,520 --> 07:11:34,558
otherwise dysfunction will throw an
10751
07:11:34,558 --> 07:11:36,120
error which we do in the function body
10752
07:11:36,120 --> 07:11:38,478
so here we check if
10753
07:11:38,478 --> 07:11:41,340
exclamation marker value so if value is
10754
07:11:41,340 --> 07:11:43,320
undefined or null
10755
07:11:43,320 --> 07:11:46,020
then we want to throw an error
10756
07:11:46,020 --> 07:11:48,360
because again it's better to throw an
10757
07:11:48,360 --> 07:11:49,978
error if there's something wrong in our
10758
07:11:49,978 --> 07:11:52,080
code rather than using the wrong value
10759
07:11:52,080 --> 07:11:54,540
and having the server misbehave and in
10760
07:11:54,540 --> 07:11:56,700
here we pass an error message something
10761
07:11:56,700 --> 07:11:58,920
that allows us to later identify the
10762
07:11:58,920 --> 07:12:02,160
error so let's write expected and then
10763
07:12:02,160 --> 07:12:05,718
in single quotes buyer
10764
07:12:05,820 --> 07:12:07,798
to be defined
10765
07:12:07,798 --> 07:12:10,798
but received and then we can
10766
07:12:10,798 --> 07:12:13,798
add value itself which will then say
10767
07:12:13,798 --> 07:12:16,020
another undefined depending on what the
10768
07:12:16,020 --> 07:12:18,180
value is
10769
07:12:18,180 --> 07:12:20,580
and that's all for this function
10770
07:12:20,580 --> 07:12:22,200
if you want to learn more about the
10771
07:12:22,200 --> 07:12:23,878
syntax you can read the documentation
10772
07:12:23,878 --> 07:12:26,160
but for our example here this is all we
10773
07:12:26,160 --> 07:12:28,080
need and in here we want to use this new
10774
07:12:28,080 --> 07:12:30,660
function let's put it into the try block
10775
07:12:30,660 --> 07:12:33,600
so that if this function throws our
10776
07:12:33,600 --> 07:12:35,638
whole Server doesn't crash only this
10777
07:12:35,638 --> 07:12:38,100
endpoint here will return a 500 response
10778
07:12:38,100 --> 07:12:40,020
which is appropriate because we messed
10779
07:12:40,020 --> 07:12:41,520
something up in our code when this
10780
07:12:41,520 --> 07:12:42,360
happens
10781
07:12:42,360 --> 07:12:45,240
so we call assert is defined
10782
07:12:45,240 --> 07:12:48,120
and pass the authenticated user idea
10783
07:12:48,120 --> 07:12:50,100
and this is actually all we have to do
10784
07:12:50,100 --> 07:12:52,500
when we use this authenticated user ID
10785
07:12:52,500 --> 07:12:55,020
afterwards it will have the appropriate
10786
07:12:55,020 --> 07:12:57,360
type because typescript is smart enough
10787
07:12:57,360 --> 07:13:00,180
to know infer this type because of this
10788
07:13:00,180 --> 07:13:02,700
special syntax here
10789
07:13:02,700 --> 07:13:05,520
this function asserts that authenticated
10790
07:13:05,520 --> 07:13:09,360
user idea is defined so it has a value
10791
07:13:09,360 --> 07:13:11,580
again you don't have to do this but it's
10792
07:13:11,580 --> 07:13:13,558
a nice security measure and I think it's
10793
07:13:13,558 --> 07:13:15,478
a cool typescript feature to learn about
10794
07:13:15,478 --> 07:13:18,540
and now in our find call down here we
10795
07:13:18,540 --> 07:13:21,120
only want to return the nodes for that
10796
07:13:21,120 --> 07:13:23,100
specific user so we add a pair of
10797
07:13:23,100 --> 07:13:25,740
calibrators to add the filter here
10798
07:13:25,740 --> 07:13:28,620
we want to check the user idea of our
10799
07:13:28,620 --> 07:13:31,080
node model that we just added a moment
10800
07:13:31,080 --> 07:13:32,700
ago
10801
07:13:32,700 --> 07:13:34,500
and we want to compare it with the
10802
07:13:34,500 --> 07:13:36,540
authenticated user idea which Noah
10803
07:13:36,540 --> 07:13:38,760
definitely has a value because we are
10804
07:13:38,760 --> 07:13:42,180
past this assert as defined check
10805
07:13:42,180 --> 07:13:43,860
before we add this to the remaining
10806
07:13:43,860 --> 07:13:46,978
endpoints let's quickly try it out
10807
07:13:46,978 --> 07:13:49,378
let's remove the requires of middleware
10808
07:13:49,378 --> 07:13:53,160
from the notes routes once again
10809
07:13:53,160 --> 07:13:55,200
and let's try to make a get request
10810
07:13:55,200 --> 07:13:57,900
without being authenticated
10811
07:13:57,900 --> 07:14:00,660
an unknown error occurred it's a 500
10812
07:14:00,660 --> 07:14:02,760
response with this generic error message
10813
07:14:02,760 --> 07:14:06,000
because it's not a HTTP error we threw
10814
07:14:06,000 --> 07:14:08,400
here and in our FTS file we only handle
10815
07:14:08,400 --> 07:14:10,978
HTTP errors and otherwise we file back
10816
07:14:10,978 --> 07:14:12,718
to this an unknown error occurred
10817
07:14:12,718 --> 07:14:15,000
message but I think this is fine because
10818
07:14:15,000 --> 07:14:17,160
when an error like this happens the user
10819
07:14:17,160 --> 07:14:18,478
doesn't really need to know what
10820
07:14:18,478 --> 07:14:20,820
happened in our code instead we just
10821
07:14:20,820 --> 07:14:23,340
lock this error which will print the
10822
07:14:23,340 --> 07:14:25,138
whole message to the console so it
10823
07:14:25,138 --> 07:14:27,180
should be visible in our backend
10824
07:14:27,180 --> 07:14:29,580
terminal
10825
07:14:29,580 --> 07:14:31,680
yeah expected value to be defined but
10826
07:14:31,680 --> 07:14:34,260
received undefined and as long as we
10827
07:14:34,260 --> 07:14:36,000
know it everything is fine the user
10828
07:14:36,000 --> 07:14:37,320
doesn't have to see this error message
10829
07:14:37,320 --> 07:14:40,280
now let's add this to our remaining
10830
07:14:40,280 --> 07:14:42,420
endpoints here
10831
07:14:42,420 --> 07:14:44,520
get node is the next one
10832
07:14:44,520 --> 07:14:45,900
here we also want to get the
10833
07:14:45,900 --> 07:14:47,940
authenticated user ID so let's copy
10834
07:14:47,940 --> 07:14:49,558
paste this line
10835
07:14:49,558 --> 07:14:51,660
then at the start of the try block we
10836
07:14:51,660 --> 07:14:54,180
want to check assert as defined to make
10837
07:14:54,180 --> 07:14:55,680
sure this has a value
10838
07:14:55,680 --> 07:14:58,378
so we have to reuse this everywhere and
10839
07:14:58,378 --> 07:15:00,298
then we have to add some code that only
10840
07:15:00,298 --> 07:15:02,820
allows the user to access this node if
10841
07:15:02,820 --> 07:15:05,820
the user ID is fit right so after the
10842
07:15:05,820 --> 07:15:08,700
note was found we can check if this node
10843
07:15:08,700 --> 07:15:10,378
belongs to this user
10844
07:15:10,378 --> 07:15:12,200
so we do if
10845
07:15:12,200 --> 07:15:14,100
exclamation mark
10846
07:15:14,100 --> 07:15:18,058
note dot user idea which is of type
10847
07:15:18,058 --> 07:15:20,940
object idea and to compare objects IDs
10848
07:15:20,940 --> 07:15:23,520
we have this equals function here that
10849
07:15:23,520 --> 07:15:26,218
belongs to this type
10850
07:15:26,218 --> 07:15:27,600
we want to compare it with the
10851
07:15:27,600 --> 07:15:29,638
authenticated user idea
10852
07:15:29,638 --> 07:15:31,558
so exclamation marks so if they don't
10853
07:15:31,558 --> 07:15:33,298
match then we want to throw another
10854
07:15:33,298 --> 07:15:35,400
error
10855
07:15:35,400 --> 07:15:39,240
throw create HTTP error for one response
10856
07:15:39,240 --> 07:15:41,760
for not authorized
10857
07:15:41,760 --> 07:15:47,298
you cannot access this node
10858
07:15:47,400 --> 07:15:50,160
maybe without the exclamation mark
10859
07:15:50,160 --> 07:15:52,500
and now this node endpoint is protected
10860
07:15:52,500 --> 07:15:53,520
properly
10861
07:15:53,520 --> 07:15:56,340
the next one is create note again we
10862
07:15:56,340 --> 07:15:58,860
need to extract the authenticated use ID
10863
07:15:58,860 --> 07:16:01,260
from this session
10864
07:16:01,260 --> 07:16:02,700
let's do it here
10865
07:16:02,700 --> 07:16:05,580
we need to check that it's defined so we
10866
07:16:05,580 --> 07:16:08,040
copy this as well the start of our try
10867
07:16:08,040 --> 07:16:10,280
block
10868
07:16:10,320 --> 07:16:12,718
and now when we create this node we also
10869
07:16:12,718 --> 07:16:14,638
have to pass the user idea it's a
10870
07:16:14,638 --> 07:16:17,160
required field now so this wouldn't work
10871
07:16:17,160 --> 07:16:19,558
without the user ID and since we have
10872
07:16:19,558 --> 07:16:21,360
our off middleware we shouldn't be able
10873
07:16:21,360 --> 07:16:23,580
to get to this create nodes function
10874
07:16:23,580 --> 07:16:26,520
unless we are authenticated
10875
07:16:26,520 --> 07:16:28,620
so we want to store the authenticated
10876
07:16:28,620 --> 07:16:31,978
user idea in the node as well
10877
07:16:31,978 --> 07:16:34,740
then we copy this authenticated user ID
10878
07:16:34,740 --> 07:16:36,420
once again
10879
07:16:36,420 --> 07:16:38,400
for update node
10880
07:16:38,400 --> 07:16:41,040
we copy assert as defined
10881
07:16:41,040 --> 07:16:42,780
put it here
10882
07:16:42,780 --> 07:16:45,240
and then we want to do the same thing as
10883
07:16:45,240 --> 07:16:48,000
we did for get node we want to copy this
10884
07:16:48,000 --> 07:16:51,840
block here you cannot access this node
10885
07:16:51,840 --> 07:16:54,420
and do this check after we checked if
10886
07:16:54,420 --> 07:16:57,558
this node actually exists
10887
07:16:57,718 --> 07:16:59,878
now of course you can always extract
10888
07:16:59,878 --> 07:17:02,280
some of this validation logic into a
10889
07:17:02,280 --> 07:17:04,080
separate function that you can put for
10890
07:17:04,080 --> 07:17:05,760
example directly into this controller
10891
07:17:05,760 --> 07:17:08,100
file for Simplicity I will just copy
10892
07:17:08,100 --> 07:17:09,600
paste the code that we need on different
10893
07:17:09,600 --> 07:17:12,000
endpoints because maybe you also want to
10894
07:17:12,000 --> 07:17:13,620
use different error messages for the
10895
07:17:13,620 --> 07:17:15,240
different endpoints in which case you
10896
07:17:15,240 --> 07:17:16,680
have to write the stuff over and over
10897
07:17:16,680 --> 07:17:17,580
again
10898
07:17:17,580 --> 07:17:20,638
and one more time for delete note so
10899
07:17:20,638 --> 07:17:23,280
again we copy this line
10900
07:17:23,280 --> 07:17:27,958
paste it here we copy assert as defined
10901
07:17:27,958 --> 07:17:30,958
paste it here
10902
07:17:30,958 --> 07:17:33,540
and we need our
10903
07:17:33,540 --> 07:17:37,020
check for the user idea as well
10904
07:17:37,020 --> 07:17:40,260
after we check that the node exists
10905
07:17:40,260 --> 07:17:42,840
and save everything
10906
07:17:42,840 --> 07:17:45,958
this will now after saving everything
10907
07:17:45,958 --> 07:17:48,540
when we refresh our front end these
10908
07:17:48,540 --> 07:17:50,280
nodes here should disappear because they
10909
07:17:50,280 --> 07:17:52,138
are not tied to a user they don't have a
10910
07:17:52,138 --> 07:17:54,478
user ID attached to them but now we only
10911
07:17:54,478 --> 07:17:57,000
get notes with our authenticated user ID
10912
07:17:57,000 --> 07:17:58,860
back which is stored in the session
10913
07:17:58,860 --> 07:18:01,320
cookie
10914
07:18:01,320 --> 07:18:04,920
this one so let's refresh this page
10915
07:18:04,920 --> 07:18:07,440
you don't have any notes yet
10916
07:18:07,440 --> 07:18:10,458
let's create a node for this user
10917
07:18:10,458 --> 07:18:12,058
authenticated
10918
07:18:12,058 --> 07:18:13,740
note
10919
07:18:13,740 --> 07:18:18,240
this is my private note
10920
07:18:18,240 --> 07:18:19,920
okay there's still something off with
10921
07:18:19,920 --> 07:18:21,958
the formatting I know why we will fix
10922
07:18:21,958 --> 07:18:24,298
this in a moment for now let's log out
10923
07:18:24,298 --> 07:18:27,120
with this user with Florian
10924
07:18:27,120 --> 07:18:30,680
log in with florian2
10925
07:18:31,320 --> 07:18:33,298
who doesn't see this note
10926
07:18:33,298 --> 07:18:35,760
because it's not tied to a specific user
10927
07:18:35,760 --> 07:18:36,840
account
10928
07:18:36,840 --> 07:18:40,080
Florian 2 snort
10929
07:18:40,080 --> 07:18:42,120
this note is
10930
07:18:42,120 --> 07:18:44,040
a secret
10931
07:18:44,040 --> 07:18:46,200
so again this is attached to this user
10932
07:18:46,200 --> 07:18:47,040
account
10933
07:18:47,040 --> 07:18:50,340
log out into the other one
10934
07:18:50,340 --> 07:18:52,798
and we won't see this note
10935
07:18:52,798 --> 07:18:55,260
only our own one
10936
07:18:55,260 --> 07:18:56,580
nice
10937
07:18:56,580 --> 07:18:58,740
now let's fix this formatting here real
10938
07:18:58,740 --> 07:18:59,820
quick
10939
07:18:59,820 --> 07:19:04,798
earlier in our notes page modules yes S5
10940
07:19:04,798 --> 07:19:07,138
we created this node squid class with
10941
07:19:07,138 --> 07:19:10,620
width 100 but we never used it anywhere
10942
07:19:10,620 --> 07:19:14,940
so we go into our notes page logged in
10943
07:19:14,940 --> 07:19:18,298
View and when we scroll down we have a
10944
07:19:18,298 --> 07:19:21,478
typo here we call it node grid but the
10945
07:19:21,478 --> 07:19:23,878
class is called notes grid now it should
10946
07:19:23,878 --> 07:19:25,620
be fixed
10947
07:19:25,620 --> 07:19:28,020
yeah it looks better now
10948
07:19:28,020 --> 07:19:30,540
let's add the second note
10949
07:19:30,540 --> 07:19:32,520
authenticated
10950
07:19:32,520 --> 07:19:35,820
note number two
10951
07:19:35,820 --> 07:19:38,760
and the layout looks good now
10952
07:19:38,760 --> 07:19:41,400
and when we look into our database
10953
07:19:41,400 --> 07:19:43,680
we will see that our new nodes now have
10954
07:19:43,680 --> 07:19:46,740
a user idea attached to them our old
10955
07:19:46,740 --> 07:19:49,080
nodes don't because we created them
10956
07:19:49,080 --> 07:19:51,600
before we require the user idea but here
10957
07:19:51,600 --> 07:19:53,638
our new nodes have this user idea so
10958
07:19:53,638 --> 07:19:55,320
these other notes are basically off and
10959
07:19:55,320 --> 07:19:57,540
now no one will be able to receive them
10960
07:19:57,540 --> 07:19:59,040
you can delete them from the database
10961
07:19:59,040 --> 07:20:01,620
you can keep them as a memory I don't
10962
07:20:01,620 --> 07:20:02,520
care
10963
07:20:02,520 --> 07:20:05,580
and our backend code is finished for now
10964
07:20:05,580 --> 07:20:08,040
we now created the fully blown server
10965
07:20:08,040 --> 07:20:10,020
with user authentication and different
10966
07:20:10,020 --> 07:20:12,120
endpoints of course there are more
10967
07:20:12,120 --> 07:20:13,920
features and little things you can add
10968
07:20:13,920 --> 07:20:15,600
but you can always figure this out
10969
07:20:15,600 --> 07:20:17,760
yourself or keep an eye open for my
10970
07:20:17,760 --> 07:20:19,978
future courses that I'm building where I
10971
07:20:19,978 --> 07:20:21,780
also want to cover more advanced topics
10972
07:20:21,780 --> 07:20:24,120
but this is already pretty cool the
10973
07:20:24,120 --> 07:20:26,280
course is not quite over yet because we
10974
07:20:26,280 --> 07:20:27,958
still have to make some improvements to
10975
07:20:27,958 --> 07:20:30,120
our front end first of all the styling
10976
07:20:30,120 --> 07:20:32,400
isn't great yet there are still some
10977
07:20:32,400 --> 07:20:33,900
problems here we like the missing
10978
07:20:33,900 --> 07:20:36,120
padding but even more important there
10979
07:20:36,120 --> 07:20:37,920
are some improvements we want to make to
10980
07:20:37,920 --> 07:20:39,900
the way we handle errors so that we
10981
07:20:39,900 --> 07:20:42,058
don't only always show the alert dialog
10982
07:20:42,058 --> 07:20:43,978
which is really annoying and I also want
10983
07:20:43,978 --> 07:20:45,718
to show you how you can navigate between
10984
07:20:45,718 --> 07:20:48,000
different pages in react because we
10985
07:20:48,000 --> 07:20:49,740
haven't done this yet we only have this
10986
07:20:49,740 --> 07:20:51,420
one page right now we will add
10987
07:20:51,420 --> 07:20:53,100
additional pages and then navigate
10988
07:20:53,100 --> 07:20:55,378
between them so that they are even
10989
07:20:55,378 --> 07:20:57,780
represented in relative ul's on our
10990
07:20:57,780 --> 07:21:00,478
front end like localhost 3000 slash
10991
07:21:00,478 --> 07:21:02,878
privacy for example and remember but
10992
07:21:02,878 --> 07:21:04,260
after that there is another video
10993
07:21:04,260 --> 07:21:06,360
waiting for you only notes Channel where
10994
07:21:06,360 --> 07:21:08,040
we deploy this whole thing to a real
10995
07:21:08,040 --> 07:21:09,780
server and if you want to put this
10996
07:21:09,780 --> 07:21:11,820
project into your portfolio for example
10997
07:21:11,820 --> 07:21:14,100
to get hired later it's a good idea to
10998
07:21:14,100 --> 07:21:15,840
actually deploy something to production
10999
07:21:15,840 --> 07:21:18,240
rather than only putting the project on
11000
07:21:18,240 --> 07:21:20,280
your GitHub profile because this way you
11001
07:21:20,280 --> 07:21:21,718
can prove that the thing you build
11002
07:21:21,718 --> 07:21:23,700
actually works and it's easier for
11003
07:21:23,700 --> 07:21:26,280
recruiters and employers to access your
11004
07:21:26,280 --> 07:21:28,500
project and try it out without access to
11005
07:21:28,500 --> 07:21:30,360
your GitHub account so you don't want to
11006
07:21:30,360 --> 07:21:32,040
miss out on the part where we deployed
11007
07:21:32,040 --> 07:21:34,638
this whole thing
11008
07:21:37,860 --> 07:21:40,558
to navigate between different pages in
11009
07:21:40,558 --> 07:21:42,420
our react app we can use this very
11010
07:21:42,420 --> 07:21:44,580
popular package called react router
11011
07:21:44,580 --> 07:21:46,378
because remember in a react app
11012
07:21:46,378 --> 07:21:48,660
everything is one single page basically
11013
07:21:48,660 --> 07:21:50,580
and the only thing we can really do is
11014
07:21:50,580 --> 07:21:52,558
replacing components that are rendered
11015
07:21:52,558 --> 07:21:54,958
on the screen but react router basically
11016
07:21:54,958 --> 07:21:57,180
gives this process more structure by
11017
07:21:57,180 --> 07:21:59,760
mapping different components that we use
11018
07:21:59,760 --> 07:22:02,820
as pages to different relative URLs so
11019
07:22:02,820 --> 07:22:04,798
that we can have something like a slash
11020
07:22:04,798 --> 07:22:07,558
privacy page where we render the page
11021
07:22:07,558 --> 07:22:11,100
component for the privacy policy slash
11022
07:22:11,100 --> 07:22:13,978
nodes and so on
11023
07:22:13,978 --> 07:22:16,500
as usual the documentation for this
11024
07:22:16,500 --> 07:22:19,080
package is humongous but we don't need
11025
07:22:19,080 --> 07:22:20,820
to know all of this you can just follow
11026
07:22:20,820 --> 07:22:23,520
along but we need to install it so let's
11027
07:22:23,520 --> 07:22:26,400
open the terminal for the front end
11028
07:22:26,400 --> 07:22:29,340
so I'm going to ZD into the front end
11029
07:22:29,340 --> 07:22:32,580
and we run npmi
11030
07:22:32,580 --> 07:22:34,218
react
11031
07:22:34,218 --> 07:22:37,020
router Dom that's the name of the whole
11032
07:22:37,020 --> 07:22:39,978
package that we need
11033
07:22:40,138 --> 07:22:43,638
and start a sucker
11034
07:22:43,978 --> 07:22:47,700
and right now here in our ftsx file our
11035
07:22:47,700 --> 07:22:50,400
notes page is hard coded yeah into the
11036
07:22:50,400 --> 07:22:52,440
page what we want to do is we want to
11037
07:22:52,440 --> 07:22:54,660
extract this into a separate file so
11038
07:22:54,660 --> 07:22:56,638
that we can replace this part here when
11039
07:22:56,638 --> 07:22:58,740
we navigate to different pages
11040
07:22:58,740 --> 07:23:01,020
so let's create a new folder in our
11041
07:23:01,020 --> 07:23:03,840
front end Source folder here
11042
07:23:03,840 --> 07:23:06,780
which we call pages
11043
07:23:06,780 --> 07:23:09,540
and inside this Pages folder we create a
11044
07:23:09,540 --> 07:23:10,440
file
11045
07:23:10,440 --> 07:23:15,058
code notes page.tsx
11046
07:23:15,240 --> 07:23:18,240
where we want to extract this piece of
11047
07:23:18,240 --> 07:23:19,620
code here into
11048
07:23:19,620 --> 07:23:20,820
so let's
11049
07:23:20,820 --> 07:23:23,160
split the editor
11050
07:23:23,160 --> 07:23:25,440
create a functional component with the
11051
07:23:25,440 --> 07:23:27,478
same name
11052
07:23:27,478 --> 07:23:31,200
and then we cut out this container here
11053
07:23:31,200 --> 07:23:33,120
put it over here
11054
07:23:33,120 --> 07:23:36,180
fix the import statements container from
11055
07:23:36,180 --> 07:23:38,218
react bootstrap
11056
07:23:38,218 --> 07:23:41,340
notes page locked in viewer from our own
11057
07:23:41,340 --> 07:23:42,900
components
11058
07:23:42,900 --> 07:23:45,120
and the locked out viewer
11059
07:23:45,120 --> 07:23:48,540
we also need a styles
11060
07:23:48,540 --> 07:23:50,820
which we can cut out here
11061
07:23:50,820 --> 07:23:52,260
paste here
11062
07:23:52,260 --> 07:23:54,600
but this page needs the currently logged
11063
07:23:54,600 --> 07:23:56,638
end user to know which of the views here
11064
07:23:56,638 --> 07:23:58,798
to render so we have to pass this as a
11065
07:23:58,798 --> 07:24:01,218
property
11066
07:24:01,558 --> 07:24:04,860
so we create an interface notes page
11067
07:24:04,860 --> 07:24:07,638
props
11068
07:24:07,978 --> 07:24:11,638
which receives the locked in user
11069
07:24:11,638 --> 07:24:13,798
which can be logged in or not so it's
11070
07:24:13,798 --> 07:24:15,180
either user
11071
07:24:15,180 --> 07:24:17,458
or no
11072
07:24:17,458 --> 07:24:19,978
if the user isn't logged in
11073
07:24:19,978 --> 07:24:22,860
and then we add a prop down here
11074
07:24:22,860 --> 07:24:25,440
locked in user and the type of these
11075
07:24:25,440 --> 07:24:29,940
props is notes page Pros
11076
07:24:29,940 --> 07:24:32,040
and now the same as before we check this
11077
07:24:32,040 --> 07:24:34,558
user to decide if we render the locked
11078
07:24:34,558 --> 07:24:36,420
in view or the locked audio and the rest
11079
07:24:36,420 --> 07:24:38,340
works the same as before
11080
07:24:38,340 --> 07:24:40,740
we can also remove this unused import
11081
07:24:40,740 --> 07:24:42,478
statements over here
11082
07:24:42,478 --> 07:24:44,580
shift alt or
11083
07:24:44,580 --> 07:24:46,860
and they are gone
11084
07:24:46,860 --> 07:24:48,840
before we set this up let's create
11085
07:24:48,840 --> 07:24:51,000
another page that we can navigate to
11086
07:24:51,000 --> 07:24:53,700
gonna close this
11087
07:24:53,700 --> 07:24:55,620
and create another file in the pages
11088
07:24:55,620 --> 07:24:57,180
folder
11089
07:24:57,180 --> 07:24:59,820
which I call a privacy page
11090
07:24:59,820 --> 07:25:02,760
dot TSX
11091
07:25:02,760 --> 07:25:04,558
recreate the functional component with
11092
07:25:04,558 --> 07:25:06,298
the same name
11093
07:25:06,298 --> 07:25:08,760
and I actually hired a lawyer just to
11094
07:25:08,760 --> 07:25:11,000
make this course that came up with a
11095
07:25:11,000 --> 07:25:14,340
elaborate privacy policy
11096
07:25:14,340 --> 07:25:16,500
he told me to write this
11097
07:25:16,500 --> 07:25:20,340
we care about your privacy
11098
07:25:20,340 --> 07:25:22,138
promise
11099
07:25:22,138 --> 07:25:24,180
this way you are safe and no one can
11100
07:25:24,180 --> 07:25:26,820
ever sue you of course that's a joke I
11101
07:25:26,820 --> 07:25:28,740
can't take any liability for your
11102
07:25:28,740 --> 07:25:31,260
privacy policy or any other legal topics
11103
07:25:31,260 --> 07:25:33,780
this is not legal advice
11104
07:25:33,780 --> 07:25:35,700
let's save this and let's actually
11105
07:25:35,700 --> 07:25:38,458
create a third page I want to have a
11106
07:25:38,458 --> 07:25:40,378
fallback page for when we try to access
11107
07:25:40,378 --> 07:25:43,200
a URL that doesn't exist so we want a
11108
07:25:43,200 --> 07:25:45,660
page not found page so we create another
11109
07:25:45,660 --> 07:25:48,120
file in the pages folder
11110
07:25:48,120 --> 07:25:51,780
which require not a found page
11111
07:25:51,780 --> 07:25:54,920
dot TSX
11112
07:25:56,760 --> 07:25:59,580
again this contains a div
11113
07:25:59,580 --> 07:26:01,920
with a paragraph
11114
07:26:01,920 --> 07:26:04,260
that says page not found
11115
07:26:04,260 --> 07:26:06,420
of course you can make this always more
11116
07:26:06,420 --> 07:26:08,458
elaborate
11117
07:26:08,458 --> 07:26:12,298
okay back into our app TSX file in order
11118
07:26:12,298 --> 07:26:15,180
to implement react router Dom we have to
11119
07:26:15,180 --> 07:26:17,820
wrap this whole diff here
11120
07:26:17,820 --> 07:26:20,878
into this browser router tag
11121
07:26:20,878 --> 07:26:23,458
this basically activates this routing
11122
07:26:23,458 --> 07:26:24,840
functionality
11123
07:26:24,840 --> 07:26:27,120
and this is the place where we want to
11124
07:26:27,120 --> 07:26:29,520
navigate between our different pages the
11125
07:26:29,520 --> 07:26:31,680
navbar is unaffected by this because the
11126
07:26:31,680 --> 07:26:33,660
navbar stays in place no matter what
11127
07:26:33,660 --> 07:26:36,000
page we are on right and our models can
11128
07:26:36,000 --> 07:26:38,520
also show on any page because they are
11129
07:26:38,520 --> 07:26:40,558
triggered by the buttons in our navbar
11130
07:26:40,558 --> 07:26:43,978
so in between these two parts where our
11131
07:26:43,978 --> 07:26:46,320
nodes page code was before
11132
07:26:46,320 --> 07:26:48,718
we add a container from react bootstrap
11133
07:26:48,718 --> 07:26:51,360
again container centers the content and
11134
07:26:51,360 --> 07:26:53,400
gives it some padding and also I want to
11135
07:26:53,400 --> 07:26:55,798
add some styling to it later and in here
11136
07:26:55,798 --> 07:26:58,378
we add this routes tag this one here
11137
07:26:58,378 --> 07:27:00,540
routes from react router
11138
07:27:00,540 --> 07:27:02,760
as the name implies this allows us to
11139
07:27:02,760 --> 07:27:05,040
set up routes in here between which we
11140
07:27:05,040 --> 07:27:07,978
can navigate and now registering these
11141
07:27:07,978 --> 07:27:10,260
different routes is actually quite easy
11142
07:27:10,260 --> 07:27:12,718
for each route we create a route tag
11143
07:27:12,718 --> 07:27:16,760
this time it's not routes it's route
11144
07:27:16,798 --> 07:27:19,200
close this tag
11145
07:27:19,200 --> 07:27:21,420
and then we give it two attributes the
11146
07:27:21,420 --> 07:27:23,520
first one is the path so the relative
11147
07:27:23,520 --> 07:27:26,040
URL to this page
11148
07:27:26,040 --> 07:27:28,138
for the first one we just use a slash
11149
07:27:28,138 --> 07:27:30,240
because we want to show our notes page
11150
07:27:30,240 --> 07:27:33,478
on the base URL you can also put this on
11151
07:27:33,478 --> 07:27:35,700
slash nodes and add a different home
11152
07:27:35,700 --> 07:27:37,260
page but that's something you can do
11153
07:27:37,260 --> 07:27:38,520
yourself
11154
07:27:38,520 --> 07:27:40,680
then we have to define the element which
11155
07:27:40,680 --> 07:27:42,540
is the component that we want to render
11156
07:27:42,540 --> 07:27:43,860
for this page
11157
07:27:43,860 --> 07:27:46,020
so between curly braces
11158
07:27:46,020 --> 07:27:48,718
we pass our notes page component that we
11159
07:27:48,718 --> 07:27:50,580
created earlier
11160
07:27:50,580 --> 07:27:53,580
which takes the locked end user as a
11161
07:27:53,580 --> 07:27:55,700
prop
11162
07:27:59,218 --> 07:28:01,138
ER and then let's add the other Pages as
11163
07:28:01,138 --> 07:28:01,978
well
11164
07:28:01,978 --> 07:28:04,920
so we create another route
11165
07:28:04,920 --> 07:28:08,340
the second one will go to a slash
11166
07:28:08,340 --> 07:28:11,120
privacy
11167
07:28:11,218 --> 07:28:13,978
and for the element we want to render
11168
07:28:13,978 --> 07:28:17,240
the Privacy page
11169
07:28:17,280 --> 07:28:20,160
and for any URL that we didn't specify
11170
07:28:20,160 --> 07:28:21,958
we want to file back to our not found
11171
07:28:21,958 --> 07:28:22,920
page
11172
07:28:22,920 --> 07:28:24,718
which you can do like this we add
11173
07:28:24,718 --> 07:28:26,580
another route
11174
07:28:26,580 --> 07:28:28,320
but for the path
11175
07:28:28,320 --> 07:28:32,040
repairs slash and the star
11176
07:28:32,040 --> 07:28:35,400
this fits any URL but before it checks
11177
07:28:35,400 --> 07:28:37,680
this one it checks the ones above it so
11178
07:28:37,680 --> 07:28:40,138
if none of these match it will fall back
11179
07:28:40,138 --> 07:28:42,478
to this one here
11180
07:28:42,478 --> 07:28:44,400
and for the element
11181
07:28:44,400 --> 07:28:49,458
we pass our not found page
11182
07:28:52,080 --> 07:28:54,780
and as simple as that our routes are set
11183
07:28:54,780 --> 07:28:57,478
up let's save everything
11184
07:28:57,478 --> 07:28:59,580
open our cool Notes app
11185
07:28:59,580 --> 07:29:01,260
and there seems to be something wrong
11186
07:29:01,260 --> 07:29:02,940
with an import here
11187
07:29:02,940 --> 07:29:05,638
in the notes page TSX file and the
11188
07:29:05,638 --> 07:29:07,138
import to the
11189
07:29:07,138 --> 07:29:09,958
CSS module is wrong
11190
07:29:09,958 --> 07:29:15,058
so notes page dot TSX
11191
07:29:15,058 --> 07:29:17,878
I guess that's two dots now let's try it
11192
07:29:17,878 --> 07:29:19,020
out
11193
07:29:19,020 --> 07:29:22,378
and now it works again so let's try it
11194
07:29:22,378 --> 07:29:25,680
out manually let's type in slash privacy
11195
07:29:25,680 --> 07:29:28,740
in the URL and here's our nice privacy
11196
07:29:28,740 --> 07:29:31,620
policy of course the styling is not good
11197
07:29:31,620 --> 07:29:33,958
you can improve this yourself if you
11198
07:29:33,958 --> 07:29:34,920
want
11199
07:29:34,920 --> 07:29:37,200
and then let's type in a URL that
11200
07:29:37,200 --> 07:29:39,718
doesn't exist and we fall back to our
11201
07:29:39,718 --> 07:29:41,940
page not phone page
11202
07:29:41,940 --> 07:29:44,280
and now before we go to the next step
11203
07:29:44,280 --> 07:29:47,040
let's fix the padding here so what we
11204
07:29:47,040 --> 07:29:49,680
can do is we can take the container of
11205
07:29:49,680 --> 07:29:52,138
this page and just add some padding to
11206
07:29:52,138 --> 07:29:54,180
the top and it will be applied to all
11207
07:29:54,180 --> 07:29:56,580
our Pages here
11208
07:29:56,580 --> 07:29:59,280
so back into ftsx
11209
07:29:59,280 --> 07:30:02,458
we need a new CSS module for our app TSX
11210
07:30:02,458 --> 07:30:05,040
file because all the other CSS move to
11211
07:30:05,040 --> 07:30:07,260
these different pages and components we
11212
07:30:07,260 --> 07:30:09,900
created so we create another file in
11213
07:30:09,900 --> 07:30:11,878
styles
11214
07:30:11,878 --> 07:30:15,900
which we call app.module.css
11215
07:30:16,320 --> 07:30:18,660
and again this module will be scoped to
11216
07:30:18,660 --> 07:30:21,540
the ftsx file and not clash with any
11217
07:30:21,540 --> 07:30:24,000
other class names and here I want to
11218
07:30:24,000 --> 07:30:28,340
create a page container class
11219
07:30:28,440 --> 07:30:30,958
on which we set a padding
11220
07:30:30,958 --> 07:30:34,798
gonna use 32 pixels for the first value
11221
07:30:34,798 --> 07:30:36,958
which are the vertical values so top and
11222
07:30:36,958 --> 07:30:38,940
bottom and for the left and right side
11223
07:30:38,940 --> 07:30:40,978
I'm going to use 0 because everything is
11224
07:30:40,978 --> 07:30:43,638
centered anyway
11225
07:30:43,740 --> 07:30:47,400
and then we import this in ftsx
11226
07:30:47,400 --> 07:30:51,478
Imports diodes from
11227
07:30:51,478 --> 07:30:55,138
dot slash Styles slash app Dot
11228
07:30:55,138 --> 07:30:57,120
module.css
11229
07:30:57,120 --> 07:31:00,000
and then we scroll down to our container
11230
07:31:00,000 --> 07:31:01,138
here
11231
07:31:01,138 --> 07:31:03,360
and add this class
11232
07:31:03,360 --> 07:31:05,638
Styles Dot Page
11233
07:31:05,638 --> 07:31:07,798
container
11234
07:31:07,798 --> 07:31:10,320
and now when we save this and open our
11235
07:31:10,320 --> 07:31:12,120
quill Notes app again we have this
11236
07:31:12,120 --> 07:31:14,100
padding here applied
11237
07:31:14,100 --> 07:31:17,040
which will work on any page now
11238
07:31:17,040 --> 07:31:18,780
looks much better
11239
07:31:18,780 --> 07:31:21,900
now let's add a menu point to our navbar
11240
07:31:21,900 --> 07:31:24,058
with which we can navigate to our
11241
07:31:24,058 --> 07:31:26,580
privacy page and let's make this brand
11242
07:31:26,580 --> 07:31:28,798
text here behave like a link tool so we
11243
07:31:28,798 --> 07:31:30,660
can click it to get back to our notes
11244
07:31:30,660 --> 07:31:31,978
page
11245
07:31:31,978 --> 07:31:34,798
so back into our code we want to go into
11246
07:31:34,798 --> 07:31:37,500
our navbar component
11247
07:31:37,500 --> 07:31:41,100
so Above This enough with MSR tour we
11248
07:31:41,100 --> 07:31:43,440
create another nav tag
11249
07:31:43,440 --> 07:31:45,298
we put it outside of this part here
11250
07:31:45,298 --> 07:31:47,218
because these buttons will be on the
11251
07:31:47,218 --> 07:31:48,900
right side and I want to have the links
11252
07:31:48,900 --> 07:31:50,878
on the left side this is why it goes
11253
07:31:50,878 --> 07:31:53,040
outside of this margin start Auto attack
11254
07:31:53,040 --> 07:31:56,040
here and also from bootstrap we have
11255
07:31:56,040 --> 07:31:58,798
this nuff link components
11256
07:31:58,798 --> 07:32:01,620
which just at a properly stated link to
11257
07:32:01,620 --> 07:32:04,260
the navbar that looks good
11258
07:32:04,260 --> 07:32:06,718
this one with a privacy
11259
07:32:06,718 --> 07:32:10,138
and it needs an href attribute which is
11260
07:32:10,138 --> 07:32:12,660
the link where we want to link this tool
11261
07:32:12,660 --> 07:32:16,320
we want to go to slash privacy
11262
07:32:16,320 --> 07:32:19,080
so let's save this and try it out
11263
07:32:19,080 --> 07:32:21,718
we now have this link up here and when
11264
07:32:21,718 --> 07:32:25,138
we click it we go to the Privacy page
11265
07:32:25,138 --> 07:32:27,478
but if you pay close attention the way
11266
07:32:27,478 --> 07:32:29,280
this works is not quite optimal yet
11267
07:32:29,280 --> 07:32:32,400
since this is a normal link it will
11268
07:32:32,400 --> 07:32:34,680
actually refresh the whole page which
11269
07:32:34,680 --> 07:32:36,900
means that the user that we stored in
11270
07:32:36,900 --> 07:32:39,240
memory disappears for a moment and we
11271
07:32:39,240 --> 07:32:41,280
also see the navbar flashing up because
11272
07:32:41,280 --> 07:32:43,558
the whole page is replaced let's take a
11273
07:32:43,558 --> 07:32:45,900
look at it again it looks weird you can
11274
07:32:45,900 --> 07:32:47,700
see that we are locked out for a moment
11275
07:32:47,700 --> 07:32:49,798
so to speak why because it's fetching
11276
07:32:49,798 --> 07:32:51,180
the user again
11277
07:32:51,180 --> 07:32:53,218
we don't want it to behave like this
11278
07:32:53,218 --> 07:32:55,200
instead we want this to behave like a
11279
07:32:55,200 --> 07:32:57,000
real app we had enough ball stays in
11280
07:32:57,000 --> 07:32:59,040
place and only these parts down here are
11281
07:32:59,040 --> 07:33:00,660
replaced right
11282
07:33:00,660 --> 07:33:03,240
this is where we can't use a normal href
11283
07:33:03,240 --> 07:33:05,458
link for internal app navigation because
11284
07:33:05,458 --> 07:33:07,680
it will always refresh the page to
11285
07:33:07,680 --> 07:33:10,020
handle this internal app navigation we
11286
07:33:10,020 --> 07:33:12,180
want to use a special link component
11287
07:33:12,180 --> 07:33:14,820
from the react router package
11288
07:33:14,820 --> 07:33:16,558
we have to import this manually I
11289
07:33:16,558 --> 07:33:18,120
checked it a moment ago the auto
11290
07:33:18,120 --> 07:33:20,520
completion didn't work so we import it
11291
07:33:20,520 --> 07:33:23,840
like this link between curly braces
11292
07:33:23,840 --> 07:33:29,040
from react router Dom down here
11293
07:33:29,040 --> 07:33:33,360
and then we go inside the nuff link
11294
07:33:33,360 --> 07:33:36,958
and we wrap this privacy text
11295
07:33:36,958 --> 07:33:40,200
into this reactor rather Dom link
11296
07:33:40,200 --> 07:33:43,200
and then we cut out the slash privacy
11297
07:33:43,200 --> 07:33:45,900
here remove the href attribute because
11298
07:33:45,900 --> 07:33:48,058
we want to let the react router domlink
11299
07:33:48,058 --> 07:33:49,138
navigate
11300
07:33:49,138 --> 07:33:52,620
which we do we add this tool prop
11301
07:33:52,620 --> 07:33:54,360
now the syntax is where we were both
11302
07:33:54,360 --> 07:33:56,100
with the snuff link and this link inside
11303
07:33:56,100 --> 07:33:57,900
it we will actually improve this in a
11304
07:33:57,900 --> 07:34:00,000
moment but for Simplicity I want to show
11305
07:34:00,000 --> 07:34:02,760
it like this first so we go to the cool
11306
07:34:02,760 --> 07:34:05,218
Notes app again and now the styling is
11307
07:34:05,218 --> 07:34:06,958
completely off as you can see the color
11308
07:34:06,958 --> 07:34:09,120
doesn't work properly anymore which is
11309
07:34:09,120 --> 07:34:10,798
the case because we invested these
11310
07:34:10,798 --> 07:34:12,420
different components but we will fix
11311
07:34:12,420 --> 07:34:13,680
this in a moment
11312
07:34:13,680 --> 07:34:15,718
the important thing is when we now click
11313
07:34:15,718 --> 07:34:17,580
this we will navigate to the Privacy
11314
07:34:17,580 --> 07:34:20,820
page but the navbar will not refresh and
11315
07:34:20,820 --> 07:34:23,160
the user will stay logged in click it
11316
07:34:23,160 --> 07:34:24,958
and as you can see it's a much smoother
11317
07:34:24,958 --> 07:34:27,378
transition
11318
07:34:27,540 --> 07:34:29,520
and even when we press the back button
11319
07:34:29,520 --> 07:34:32,280
the behavior is the same
11320
07:34:32,280 --> 07:34:34,558
it doesn't refresh the whole page
11321
07:34:34,558 --> 07:34:37,260
but now let's fix the styling here
11322
07:34:37,260 --> 07:34:40,378
what we can do is we can keep using the
11323
07:34:40,378 --> 07:34:43,440
nuff link but we can render it as a link
11324
07:34:43,440 --> 07:34:45,780
from the react router Dom package and
11325
07:34:45,780 --> 07:34:47,458
again I figure this out through Google
11326
07:34:47,458 --> 07:34:49,978
this is a special syntax that we can use
11327
07:34:49,978 --> 07:34:52,860
on this bootstrap components and this
11328
07:34:52,860 --> 07:34:54,900
feature is called render props it's a
11329
07:34:54,900 --> 07:34:57,180
certain design pattern and reacts that
11330
07:34:57,180 --> 07:34:59,400
you can also build yourself if you need
11331
07:34:59,400 --> 07:35:01,260
it and some packages like react
11332
07:35:01,260 --> 07:35:03,180
bootstrap use that for certain
11333
07:35:03,180 --> 07:35:05,458
components so what we can do is on the
11334
07:35:05,458 --> 07:35:08,520
nav link we can pass this as prop
11335
07:35:08,520 --> 07:35:11,280
with one else not two and here we can
11336
07:35:11,280 --> 07:35:13,798
tell react bootstrap to use the styling
11337
07:35:13,798 --> 07:35:15,540
and everything from the nuff link but
11338
07:35:15,540 --> 07:35:17,280
actually render a different component
11339
07:35:17,280 --> 07:35:19,500
when this is displayed on the screen and
11340
07:35:19,500 --> 07:35:21,600
in here we can pass the link from react
11341
07:35:21,600 --> 07:35:22,860
router Dom
11342
07:35:22,860 --> 07:35:25,138
and the cool thing is now that we render
11343
07:35:25,138 --> 07:35:28,260
this as a react router Dom link it also
11344
07:35:28,260 --> 07:35:30,298
behaves like a react router dumpling
11345
07:35:30,298 --> 07:35:32,580
this is why we now see this error that
11346
07:35:32,580 --> 07:35:35,040
tells us that we need this two attribute
11347
07:35:35,040 --> 07:35:37,740
that's the two attribute down here now
11348
07:35:37,740 --> 07:35:40,378
that we render this link up here we have
11349
07:35:40,378 --> 07:35:42,780
to pass this to attribute here as well
11350
07:35:42,780 --> 07:35:44,878
and now we can remove this intermediate
11351
07:35:44,878 --> 07:35:46,860
link here in between
11352
07:35:46,860 --> 07:35:50,160
and now basically we are using a react
11353
07:35:50,160 --> 07:35:52,260
router Dom link but we are using the
11354
07:35:52,260 --> 07:35:54,420
styling of a normal bootstrap nuff link
11355
07:35:54,420 --> 07:35:57,240
so when we save this and go back to our
11356
07:35:57,240 --> 07:36:00,298
app now The Styling is correct again and
11357
07:36:00,298 --> 07:36:01,860
it behaves
11358
07:36:01,860 --> 07:36:04,978
like a react router link
11359
07:36:04,978 --> 07:36:07,200
okay now let's also make the cool Notes
11360
07:36:07,200 --> 07:36:10,138
app brand text here clickable
11361
07:36:10,138 --> 07:36:12,600
so we go into navbar brand
11362
07:36:12,600 --> 07:36:15,058
and here we use this as attribute as
11363
07:36:15,058 --> 07:36:17,878
well we want to render a react router
11364
07:36:17,878 --> 07:36:19,020
link
11365
07:36:19,020 --> 07:36:22,920
and we want to navigate tour slash as a
11366
07:36:22,920 --> 07:36:26,280
string so this is just the home page
11367
07:36:26,280 --> 07:36:28,080
and it already is
11368
07:36:28,080 --> 07:36:30,900
you can zip this trade out and now this
11369
07:36:30,900 --> 07:36:32,940
one here is clickable as well you can go
11370
07:36:32,940 --> 07:36:36,798
to privacy and back to the home page
11371
07:36:36,958 --> 07:36:38,458
cool stuff
11372
07:36:38,458 --> 07:36:41,638
okay our page itself is almost complete
11373
07:36:41,638 --> 07:36:43,920
but there's one more important topic I
11374
07:36:43,920 --> 07:36:46,080
want to teach you and that's how we can
11375
07:36:46,080 --> 07:36:48,120
distinguish between different errors on
11376
07:36:48,120 --> 07:36:50,520
our front end because right now for
11377
07:36:50,520 --> 07:36:52,320
every arrow we do the exact same thing
11378
07:36:52,320 --> 07:36:54,000
we lock the error message and show an
11379
07:36:54,000 --> 07:36:55,798
alert dialog but you might want to
11380
07:36:55,798 --> 07:36:57,900
handle different errors differently so
11381
07:36:57,900 --> 07:37:01,458
that's what we will learn next
11382
07:37:05,058 --> 07:37:07,440
distinguishing between different arrows
11383
07:37:07,440 --> 07:37:09,420
on the front end allows us to handle
11384
07:37:09,420 --> 07:37:11,700
them differently for example when we try
11385
07:37:11,700 --> 07:37:14,100
to log in with invalid credentials and
11386
07:37:14,100 --> 07:37:16,680
we get a 401 error back then we probably
11387
07:37:16,680 --> 07:37:18,660
want to display this differently than
11388
07:37:18,660 --> 07:37:21,240
internal Zara arrows for example so if
11389
07:37:21,240 --> 07:37:23,040
the credentials are incorrect we want to
11390
07:37:23,040 --> 07:37:25,320
display a little error text here above
11391
07:37:25,320 --> 07:37:27,900
the input Fields so what we do is we go
11392
07:37:27,900 --> 07:37:30,420
into our front end code
11393
07:37:30,420 --> 07:37:32,878
and create a new file here
11394
07:37:32,878 --> 07:37:37,320
we put it into a front-end Source here
11395
07:37:37,320 --> 07:37:39,540
we create a new folder first
11396
07:37:39,540 --> 07:37:42,478
which we call errors
11397
07:37:42,478 --> 07:37:46,440
and in here we create a file called http
11398
07:37:46,440 --> 07:37:49,440
arrows.ts
11399
07:37:50,340 --> 07:37:52,558
and in here we Define some classes that
11400
07:37:52,558 --> 07:37:54,120
allow us to distinguish between
11401
07:37:54,120 --> 07:37:56,940
different errors that we receive now I
11402
07:37:56,940 --> 07:37:59,520
guess we could also use the HTTP errors
11403
07:37:59,520 --> 07:38:01,620
package that we used on the back end but
11404
07:38:01,620 --> 07:38:03,718
I think this is a bit Overkill because
11405
07:38:03,718 --> 07:38:05,458
it has all these functions to create
11406
07:38:05,458 --> 07:38:07,740
errors but on the front end we don't
11407
07:38:07,740 --> 07:38:09,718
want to create errors we only receive
11408
07:38:09,718 --> 07:38:11,160
them and want to distinguish between
11409
07:38:11,160 --> 07:38:13,558
them so I think we don't need to add
11410
07:38:13,558 --> 07:38:15,660
this package instead we will just create
11411
07:38:15,660 --> 07:38:17,940
some small classes that allow us to
11412
07:38:17,940 --> 07:38:19,378
distinguish between these different
11413
07:38:19,378 --> 07:38:22,200
errors so what we do is we create a
11414
07:38:22,200 --> 07:38:26,240
class called HTTP error
11415
07:38:26,240 --> 07:38:29,600
extends error
11416
07:38:29,878 --> 07:38:32,218
we use a class instead of an interface
11417
07:38:32,218 --> 07:38:34,440
this time because error is also a class
11418
07:38:34,440 --> 07:38:36,660
and we want to extend the normal error
11419
07:38:36,660 --> 07:38:38,940
to add our own fields to it
11420
07:38:38,940 --> 07:38:40,920
and here we add a Constructor if you
11421
07:38:40,920 --> 07:38:42,540
have any programming experience then you
11422
07:38:42,540 --> 07:38:45,000
know what the Constructor is the same as
11423
07:38:45,000 --> 07:38:47,820
the normal error class our class will
11424
07:38:47,820 --> 07:38:50,100
take an optional message that we can
11425
07:38:50,100 --> 07:38:52,440
store in this error and show later the
11426
07:38:52,440 --> 07:38:54,058
question mark colon
11427
07:38:54,058 --> 07:38:56,638
trippy of type string
11428
07:38:56,638 --> 07:38:58,920
and then we want to pass this message to
11429
07:38:58,920 --> 07:39:00,900
the superclass to Stuart in the normal
11430
07:39:00,900 --> 07:39:04,138
error message field so we call Super
11431
07:39:04,138 --> 07:39:07,320
and pass the message here
11432
07:39:07,320 --> 07:39:09,120
the reason why we overwrite The
11433
07:39:09,120 --> 07:39:11,340
Constructor is because we also want to
11434
07:39:11,340 --> 07:39:15,180
set the name attribute of the error
11435
07:39:15,180 --> 07:39:19,320
and set it to this dot Constructor
11436
07:39:19,320 --> 07:39:20,878
dot name
11437
07:39:20,878 --> 07:39:22,920
and this means that when we create a
11438
07:39:22,920 --> 07:39:25,798
subclass of HTTP error it will use the
11439
07:39:25,798 --> 07:39:28,080
name of the class itself and put it into
11440
07:39:28,080 --> 07:39:30,600
this name field and now below we want to
11441
07:39:30,600 --> 07:39:32,458
do exactly that we want to create a
11442
07:39:32,458 --> 07:39:35,280
fewer subclasses of HTTP error that then
11443
07:39:35,280 --> 07:39:37,020
allow us to distinguish between
11444
07:39:37,020 --> 07:39:39,718
different error codes and we export
11445
07:39:39,718 --> 07:39:41,520
these because we want to use them on the
11446
07:39:41,520 --> 07:39:44,040
outside we don't want to use the generic
11447
07:39:44,040 --> 07:39:46,500
HTTP error on the outside only the
11448
07:39:46,500 --> 07:39:48,000
subclasses are meant to be used
11449
07:39:48,000 --> 07:39:50,760
throughout our code so we write export
11450
07:39:50,760 --> 07:39:54,120
class and the first one is for 401
11451
07:39:54,120 --> 07:39:57,360
responses when we are not authorized so
11452
07:39:57,360 --> 07:39:59,298
we colored
11453
07:39:59,298 --> 07:40:02,280
unauthorized error
11454
07:40:02,280 --> 07:40:04,320
and it extends
11455
07:40:04,320 --> 07:40:07,740
our HTTP error class
11456
07:40:07,740 --> 07:40:10,920
and again our HTTP hour class does is
11457
07:40:10,920 --> 07:40:12,660
basically putting the name of
11458
07:40:12,660 --> 07:40:14,878
unauthorized error into the name field
11459
07:40:14,878 --> 07:40:17,700
of the error class
11460
07:40:17,700 --> 07:40:20,040
and every class needs a body but we can
11461
07:40:20,040 --> 07:40:21,718
keep this body empty because we don't
11462
07:40:21,718 --> 07:40:23,878
want to do anything special in here we
11463
07:40:23,878 --> 07:40:25,620
just need this name to distinguish
11464
07:40:25,620 --> 07:40:27,718
between the different error codes
11465
07:40:27,718 --> 07:40:29,820
and we will also add a documentation
11466
07:40:29,820 --> 07:40:31,260
comment here
11467
07:40:31,260 --> 07:40:35,040
with a slash and tour style symbols
11468
07:40:35,040 --> 07:40:36,958
and here we can write a message that we
11469
07:40:36,958 --> 07:40:38,940
will then see when we hover over this
11470
07:40:38,940 --> 07:40:42,180
class I'm just gonna set this to a 401
11471
07:40:42,180 --> 07:40:44,700
so now when we hover over on authorized
11472
07:40:44,700 --> 07:40:47,340
around we can see for one actually let's
11473
07:40:47,340 --> 07:40:48,660
write
11474
07:40:48,660 --> 07:40:51,478
status code 401
11475
07:40:51,478 --> 07:40:53,940
this just makes it a bit easier later to
11476
07:40:53,940 --> 07:40:56,520
remember what the status code each error
11477
07:40:56,520 --> 07:40:58,740
was and then we create the second one
11478
07:40:58,740 --> 07:41:00,660
below
11479
07:41:00,660 --> 07:41:03,058
export class
11480
07:41:03,058 --> 07:41:06,360
we call this one conflict error
11481
07:41:06,360 --> 07:41:09,180
which we use for four or nine responses
11482
07:41:09,180 --> 07:41:11,280
for example when we try to sign up with
11483
07:41:11,280 --> 07:41:13,860
an email that already exists
11484
07:41:13,860 --> 07:41:17,580
it also extends HTTP error with an empty
11485
07:41:17,580 --> 07:41:18,660
body
11486
07:41:18,660 --> 07:41:20,940
and create a documentation command as
11487
07:41:20,940 --> 07:41:21,718
well
11488
07:41:21,718 --> 07:41:23,940
status code 409
11489
07:41:23,940 --> 07:41:25,378
and I'm also going to add another
11490
07:41:25,378 --> 07:41:28,138
comment here at the bottom
11491
07:41:28,138 --> 07:41:32,520
which was a it's more error classes if
11492
07:41:32,520 --> 07:41:33,740
you need
11493
07:41:33,740 --> 07:41:36,360
distinction this is just a reminder for
11494
07:41:36,360 --> 07:41:38,340
you that you can always add more error
11495
07:41:38,340 --> 07:41:39,420
classes here
11496
07:41:39,420 --> 07:41:42,420
then we go into our notes API file here
11497
07:41:42,420 --> 07:41:44,760
in our Network folder
11498
07:41:44,760 --> 07:41:47,040
and all the way up into fetch data where
11499
07:41:47,040 --> 07:41:48,718
we now want to distinguish between these
11500
07:41:48,718 --> 07:41:50,700
different arrows because right now we
11501
07:41:50,700 --> 07:41:53,940
always throw this generic error class
11502
07:41:53,940 --> 07:41:56,878
so we go above this line
11503
07:41:56,878 --> 07:41:59,520
and we simply check for the status codes
11504
07:41:59,520 --> 07:42:01,798
so that we know whichever we have to
11505
07:42:01,798 --> 07:42:04,878
throw so if
11506
07:42:04,878 --> 07:42:07,260
response dot status
11507
07:42:07,260 --> 07:42:11,458
is equal to 401
11508
07:42:11,580 --> 07:42:14,160
then we want to throw
11509
07:42:14,160 --> 07:42:17,040
an unauthorized error
11510
07:42:17,040 --> 07:42:18,840
which will also contain the error
11511
07:42:18,840 --> 07:42:19,978
message
11512
07:42:19,978 --> 07:42:21,900
and we actually have to add the new
11513
07:42:21,900 --> 07:42:24,058
keyword here in front of it for normal
11514
07:42:24,058 --> 07:42:25,920
errors you don't have to do this you can
11515
07:42:25,920 --> 07:42:28,378
if you want but since this is our own
11516
07:42:28,378 --> 07:42:30,780
custom class we have to add a new
11517
07:42:30,780 --> 07:42:33,718
keyword in order to instantiate it
11518
07:42:33,718 --> 07:42:37,020
then we do an else if because now we
11519
07:42:37,020 --> 07:42:39,240
want to check if response dot status is
11520
07:42:39,240 --> 07:42:42,600
equal to 409
11521
07:42:42,600 --> 07:42:45,718
in which case we want to throw
11522
07:42:45,718 --> 07:42:47,940
a conflict error
11523
07:42:47,940 --> 07:42:51,000
which will also take the error message
11524
07:42:51,000 --> 07:42:53,760
and only if both of those are not the
11525
07:42:53,760 --> 07:42:55,798
case we want to go to the else block and
11526
07:42:55,798 --> 07:42:59,120
throw our normal error
11527
07:43:00,298 --> 07:43:02,340
and we can also make the error message a
11528
07:43:02,340 --> 07:43:04,680
bit more elaborate so we see which
11529
07:43:04,680 --> 07:43:06,900
status code we got from the error
11530
07:43:06,900 --> 07:43:08,218
message itself
11531
07:43:08,218 --> 07:43:10,798
so I'm going to pass a string that says
11532
07:43:10,798 --> 07:43:15,298
request failed with status colon
11533
07:43:15,298 --> 07:43:19,080
then I append response dot status
11534
07:43:19,080 --> 07:43:22,920
and add something more to the string
11535
07:43:22,920 --> 07:43:26,040
message colon
11536
07:43:26,040 --> 07:43:28,558
and error message
11537
07:43:28,558 --> 07:43:30,240
you don't have to do this but I think
11538
07:43:30,240 --> 07:43:31,798
it's a good idea
11539
07:43:31,798 --> 07:43:33,900
now let's save this and update a few
11540
07:43:33,900 --> 07:43:36,600
places in our app first of all let's go
11541
07:43:36,600 --> 07:43:39,298
into our login model
11542
07:43:39,298 --> 07:43:41,520
and instead of RS showing this alert
11543
07:43:41,520 --> 07:43:44,458
message when we get a 401 response back
11544
07:43:44,458 --> 07:43:46,860
because the password or the username was
11545
07:43:46,860 --> 07:43:48,600
wrong we want to show an error message
11546
07:43:48,600 --> 07:43:51,120
that is embedded in the dialog directly
11547
07:43:51,120 --> 07:43:53,458
instead the first of all we need a state
11548
07:43:53,458 --> 07:43:54,780
for this
11549
07:43:54,780 --> 07:43:57,298
that decides if this error message is
11550
07:43:57,298 --> 07:43:58,860
shown or not
11551
07:43:58,860 --> 07:44:03,540
so const let's call it error text
11552
07:44:03,540 --> 07:44:06,958
and set error text
11553
07:44:06,958 --> 07:44:10,798
equals use State Auto Import often
11554
07:44:10,798 --> 07:44:12,958
doesn't work for you state I don't know
11555
07:44:12,958 --> 07:44:13,860
why
11556
07:44:13,860 --> 07:44:17,100
let's take it from somewhere else
11557
07:44:17,100 --> 07:44:20,540
here you state
11558
07:44:21,058 --> 07:44:24,798
we actually don't need use effect
11559
07:44:26,160 --> 07:44:28,200
we call you state
11560
07:44:28,200 --> 07:44:30,420
we set the type to a string or another
11561
07:44:30,420 --> 07:44:32,580
because if there is no error we want to
11562
07:44:32,580 --> 07:44:35,360
set this to Nile
11563
07:44:35,458 --> 07:44:37,558
then here in the catch block we want to
11564
07:44:37,558 --> 07:44:38,520
check
11565
07:44:38,520 --> 07:44:40,138
if
11566
07:44:40,138 --> 07:44:41,580
error
11567
07:44:41,580 --> 07:44:43,620
instance of
11568
07:44:43,620 --> 07:44:45,660
which is a keyword that we can use to
11569
07:44:45,660 --> 07:44:48,718
compare the type of this error if this
11570
07:44:48,718 --> 07:44:52,200
is an instance of an authorized error
11571
07:44:52,200 --> 07:44:55,558
which has the status code 401 we can
11572
07:44:55,558 --> 07:44:59,160
remember this by hovering over it
11573
07:44:59,160 --> 07:45:02,520
then we want to set the error text
11574
07:45:02,520 --> 07:45:06,718
to error Dot message
11575
07:45:06,718 --> 07:45:09,478
and if this is any other type of error
11576
07:45:09,478 --> 07:45:12,360
we want to do the same as before we want
11577
07:45:12,360 --> 07:45:14,700
to show the alert dialog or just lock it
11578
07:45:14,700 --> 07:45:16,320
or whatever you want
11579
07:45:16,320 --> 07:45:18,840
and we will lock the error in both cases
11580
07:45:18,840 --> 07:45:21,780
so we put it outside of the if block
11581
07:45:21,780 --> 07:45:23,940
and now we also need an element in the
11582
07:45:23,940 --> 07:45:26,340
model body here that actually displays
11583
07:45:26,340 --> 07:45:28,680
this error let's put it here inside the
11584
07:45:28,680 --> 07:45:31,320
model body but above the form
11585
07:45:31,320 --> 07:45:34,740
we check if error text is defined so
11586
07:45:34,740 --> 07:45:37,260
error text and to Embers enzymes
11587
07:45:37,260 --> 07:45:39,478
then we want to show an alert which is
11588
07:45:39,478 --> 07:45:42,718
another bootstrap component
11589
07:45:42,718 --> 07:45:45,840
and this alert will show the error text
11590
07:45:45,840 --> 07:45:48,298
from the state
11591
07:45:48,298 --> 07:45:50,940
and we set the variant to Danger which
11592
07:45:50,940 --> 07:45:53,820
gives this some nice styling it makes it
11593
07:45:53,820 --> 07:45:54,780
red
11594
07:45:54,780 --> 07:45:57,180
let's save it and try it out
11595
07:45:57,180 --> 07:46:00,000
let's go to our quill Notes app
11596
07:46:00,000 --> 07:46:01,978
and try to log in with invalid
11597
07:46:01,978 --> 07:46:03,780
credentials and now we see this message
11598
07:46:03,780 --> 07:46:06,600
here instead but any error that's not a
11599
07:46:06,600 --> 07:46:09,298
401 error will just show the usual alert
11600
07:46:09,298 --> 07:46:11,760
dialog as we had before let's do the
11601
07:46:11,760 --> 07:46:14,700
same for our sign up model
11602
07:46:14,700 --> 07:46:17,458
so we open the design about the TSX file
11603
07:46:17,458 --> 07:46:20,940
again we need a state
11604
07:46:20,940 --> 07:46:22,920
const
11605
07:46:22,920 --> 07:46:28,100
error text and set error text
11606
07:46:28,138 --> 07:46:30,780
use State again we have to import this
11607
07:46:30,780 --> 07:46:33,138
manually
11608
07:46:36,120 --> 07:46:40,080
use state of type string or null
11609
07:46:40,080 --> 07:46:43,080
and we initialize it with null
11610
07:46:43,080 --> 07:46:45,058
and then down here
11611
07:46:45,058 --> 07:46:48,240
we want to check if error instance off
11612
07:46:48,240 --> 07:46:50,700
and this time we use conflict error
11613
07:46:50,700 --> 07:46:52,860
which is the status code that we return
11614
07:46:52,860 --> 07:46:55,440
When the username or the email is
11615
07:46:55,440 --> 07:46:59,600
already in use right 409
11616
07:47:00,660 --> 07:47:04,558
then we want to set the error text
11617
07:47:04,558 --> 07:47:08,100
to error.message
11618
07:47:10,020 --> 07:47:13,500
else we want to use the same alert as we
11619
07:47:13,500 --> 07:47:16,020
had before and we want to lock the error
11620
07:47:16,020 --> 07:47:18,180
in both cases
11621
07:47:18,180 --> 07:47:20,218
again we need our
11622
07:47:20,218 --> 07:47:23,638
Alert warning here in the model body
11623
07:47:23,638 --> 07:47:26,520
so error text
11624
07:47:26,520 --> 07:47:29,780
to Ampersand science
11625
07:47:29,878 --> 07:47:32,660
alert
11626
07:47:32,940 --> 07:47:36,240
variant Danger
11627
07:47:36,240 --> 07:47:38,400
and for the text we display the error
11628
07:47:38,400 --> 07:47:40,200
text
11629
07:47:40,200 --> 07:47:43,200
okay let's try this out as well
11630
07:47:43,200 --> 07:47:45,958
so we open the sign up model and I use a
11631
07:47:45,958 --> 07:47:49,320
username that already exists test at
11632
07:47:49,320 --> 07:47:51,840
test.com
11633
07:47:51,840 --> 07:47:54,958
we try to sign up and with the username
11634
07:47:54,958 --> 07:47:56,638
already taken please choose a different
11635
07:47:56,638 --> 07:47:59,040
one or log in instead and this is the
11636
07:47:59,040 --> 07:48:00,540
error message that's coming from our
11637
07:48:00,540 --> 07:48:02,340
server
11638
07:48:02,340 --> 07:48:03,900
from our
11639
07:48:03,900 --> 07:48:06,298
user controller
11640
07:48:06,298 --> 07:48:09,000
username already taken and the other one
11641
07:48:09,000 --> 07:48:11,878
which you can try out as well
11642
07:48:11,878 --> 07:48:14,940
so we use a different username but in
11643
07:48:14,940 --> 07:48:17,280
email that already exists I forgot which
11644
07:48:17,280 --> 07:48:21,240
email I used earlier florian2a test.com
11645
07:48:21,240 --> 07:48:23,160
a user with this email address already
11646
07:48:23,160 --> 07:48:24,600
exists
11647
07:48:24,600 --> 07:48:26,700
and we get the error message directly
11648
07:48:26,700 --> 07:48:29,218
from the server because we read it from
11649
07:48:29,218 --> 07:48:32,360
the response body
11650
07:48:32,400 --> 07:48:35,160
and then pass it to our error here
11651
07:48:35,160 --> 07:48:37,860
and then we display this in the UI
11652
07:48:37,860 --> 07:48:40,378
through this error.message field
11653
07:48:40,378 --> 07:48:41,940
and of course you can use the same
11654
07:48:41,940 --> 07:48:43,558
approach to distinguish between
11655
07:48:43,558 --> 07:48:45,660
different errors in different places in
11656
07:48:45,660 --> 07:48:47,398
your app and this way decide what you
11657
07:48:47,398 --> 07:48:49,320
want to do with them
11658
07:48:49,320 --> 07:48:51,478
all right we have successfully created
11659
07:48:51,478 --> 07:48:54,180
our first mon app this is a special
11660
07:48:54,180 --> 07:48:56,520
moment that you will always remember and
11661
07:48:56,520 --> 07:48:58,320
of course you will like this video and
11662
07:48:58,320 --> 07:49:00,298
share it with someone that could need it
11663
07:49:00,298 --> 07:49:03,240
because this helps me and you are a very
11664
07:49:03,240 --> 07:49:05,660
thankful person that wants to give back
11665
07:49:05,660 --> 07:49:08,638
although I heard remember that there is
11666
07:49:08,638 --> 07:49:10,378
another video where we deploy this whole
11667
07:49:10,378 --> 07:49:12,898
thing only notes Channel you can find
11668
07:49:12,898 --> 07:49:14,638
the link in the video description below
11669
07:49:14,638 --> 07:49:16,558
you definitely have to watch this as
11670
07:49:16,558 --> 07:49:18,240
well because building this app and then
11671
07:49:18,240 --> 07:49:20,760
not deploying it would be a waste also
11672
07:49:20,760 --> 07:49:22,440
of course there are a lot more features
11673
07:49:22,440 --> 07:49:25,020
we could add to our Merin app and keep
11674
07:49:25,020 --> 07:49:27,180
an eye open on this channel because I
11675
07:49:27,180 --> 07:49:28,680
want to make more web development
11676
07:49:28,680 --> 07:49:30,840
tutorials so subscribe if you haven't
11677
07:49:30,840 --> 07:49:34,080
yet and if you build a cool project on
11678
07:49:34,080 --> 07:49:36,420
top of our moon app rebuild here if you
11679
07:49:36,420 --> 07:49:38,040
have some cool features that you added
11680
07:49:38,040 --> 07:49:41,218
or a project that you deployed or added
11681
07:49:41,218 --> 07:49:43,080
to your portfolio that I would be very
11682
07:49:43,080 --> 07:49:45,000
happy if you tag me on Twitter and just
11683
07:49:45,000 --> 07:49:47,280
show it to me tag me ads either at
11684
07:49:47,280 --> 07:49:50,280
coding in floor or flow and score Volta
11685
07:49:50,280 --> 07:49:51,958
which is my personal account doesn't
11686
07:49:51,958 --> 07:49:53,878
matter which one and then I wish you
11687
07:49:53,878 --> 07:49:58,340
best of luck and happy coding take care
817034
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.