﻿1
00:00:01,170 --> 00:00:03,210
‫And now, to finish this part,

2
00:00:03,210 --> 00:00:06,363
‫let's incorporate a geolocation feature.

3
00:00:07,590 --> 00:00:11,310
‫And to do that, you will actually bring back the custom hook

4
00:00:11,310 --> 00:00:15,180
‫that we wrote earlier in one of the challenges.

5
00:00:15,180 --> 00:00:17,430
‫Now, if you didn't complete that challenge,

6
00:00:17,430 --> 00:00:20,310
‫then you can just grab this custom hook here

7
00:00:20,310 --> 00:00:24,270
‫from this URL that I will provide in this lecture.

8
00:00:24,270 --> 00:00:27,630
‫And so this is now really the power of custom hooks.

9
00:00:27,630 --> 00:00:30,870
‫So we just write them once, and then we can drop them

10
00:00:30,870 --> 00:00:33,630
‫into any project that we want,

11
00:00:33,630 --> 00:00:36,930
‫at least if we made them reusable enough.

12
00:00:36,930 --> 00:00:39,120
‫So let's just copy...

13
00:00:39,120 --> 00:00:41,700
‫Well, actually we're missing this part,

14
00:00:41,700 --> 00:00:45,870
‫so let's copy all of this.

15
00:00:45,870 --> 00:00:50,870
‫And then let's first create a new folder here for our hooks.

16
00:00:52,410 --> 00:00:56,340
‫So then we get a new nice icon there.

17
00:00:56,340 --> 00:00:59,673
‫And then let's say useGeolocation.js.

18
00:01:04,740 --> 00:01:07,560
‫So this one doesn't have to be a JSX file

19
00:01:07,560 --> 00:01:10,710
‫because we are not exporting any JSX.

20
00:01:10,710 --> 00:01:13,113
‫So it's just a JavaScript function.

21
00:01:14,340 --> 00:01:18,300
‫Now, here we are missing the useState hook,

22
00:01:18,300 --> 00:01:20,970
‫so let's include it like this,

23
00:01:20,970 --> 00:01:25,970
‫and then let's actually export this function from here.

24
00:01:27,540 --> 00:01:30,540
‫And here I'm not doing a default export,

25
00:01:30,540 --> 00:01:32,820
‫but really a named export.

26
00:01:32,820 --> 00:01:35,460
‫So that's how I like to do it with hooks,

27
00:01:35,460 --> 00:01:38,820
‫because sometimes we can have multiple custom hooks

28
00:01:38,820 --> 00:01:40,050
‫in the same file.

29
00:01:40,050 --> 00:01:42,720
‫And so then it's easier to have this convention,

30
00:01:42,720 --> 00:01:46,560
‫where we always export them with named exports.

31
00:01:46,560 --> 00:01:48,870
‫Now, there's just one thing that I want to change

32
00:01:48,870 --> 00:01:51,570
‫in this custom hook, which is to have it

33
00:01:51,570 --> 00:01:53,973
‫allow a default position.

34
00:01:56,190 --> 00:02:00,060
‫So I want this custom hook to receive a default position,

35
00:02:00,060 --> 00:02:04,740
‫but if not, then let's set the position to null.

36
00:02:04,740 --> 00:02:07,950
‫So to null instead of an empty object

37
00:02:07,950 --> 00:02:09,510
‫that we have right now.

38
00:02:09,510 --> 00:02:11,340
‫So I think that the empty object

39
00:02:11,340 --> 00:02:12,930
‫doesn't make a lot of sense,

40
00:02:12,930 --> 00:02:14,970
‫and instead, this should be null.

41
00:02:14,970 --> 00:02:19,110
‫And so here, let's now use that default position

42
00:02:19,110 --> 00:02:23,343
‫which will default to null if it's not set.

43
00:02:24,390 --> 00:02:28,890
‫Okay, and so with this, we can then use

44
00:02:28,890 --> 00:02:31,410
‫the get position function on any button

45
00:02:31,410 --> 00:02:35,250
‫to retrieve the current position of our user.

46
00:02:35,250 --> 00:02:39,180
‫And so let's use that here now.

47
00:02:39,180 --> 00:02:43,410
‫And like to always have all my hooks at the very top

48
00:02:43,410 --> 00:02:45,633
‫to keep the code a bit more organized.

49
00:02:46,770 --> 00:02:48,153
‫So let's do that here.

50
00:02:50,670 --> 00:02:52,443
‫And for now, just leaving it empty.

51
00:02:53,550 --> 00:02:55,173
‫So useGeolocation.

52
00:02:58,650 --> 00:03:02,343
‫And then we have have to manually import that here.

53
00:03:03,900 --> 00:03:06,607
‫Import useGeolocation from...

54
00:03:12,450 --> 00:03:15,900
‫We need to move up, then into the hooks folder,

55
00:03:15,900 --> 00:03:17,573
‫and useGeolocation.

56
00:03:19,341 --> 00:03:22,860
‫Now let's just check again what this actually exports.

57
00:03:22,860 --> 00:03:27,860
‫So it exports isLoading, position, error, and getPosition.

58
00:03:28,050 --> 00:03:31,383
‫Now, let's not use the error, but only these other ones.

59
00:03:33,570 --> 00:03:38,570
‫So isLoading, but since we are already using a variable

60
00:03:39,480 --> 00:03:42,630
‫with this name, let's actually rename it.

61
00:03:42,630 --> 00:03:44,613
‫So isLoadingPosition.

62
00:03:46,830 --> 00:03:49,053
‫So just to avoid any confusion.

63
00:03:52,140 --> 00:03:55,170
‫Then we have to position, and let's again rename it

64
00:03:55,170 --> 00:03:59,667
‫to geolocationPosition so that we are really sure

65
00:04:01,740 --> 00:04:04,680
‫what each of the variable names mean.

66
00:04:04,680 --> 00:04:09,513
‫And then let's also get the getPosition function.

67
00:04:10,350 --> 00:04:14,482
‫Okay, and now we need to trigger this

68
00:04:14,482 --> 00:04:16,830
‫with the button right here.

69
00:04:16,830 --> 00:04:20,700
‫So let's place that button also right here in this div,

70
00:04:20,700 --> 00:04:22,743
‫so beside this map container.

71
00:04:24,030 --> 00:04:27,330
‫So for this, we will bring back our button component

72
00:04:27,330 --> 00:04:28,653
‫that we built earlier.

73
00:04:30,570 --> 00:04:34,053
‫And then here, this one is of the type of position.

74
00:04:35,340 --> 00:04:40,280
‫So I know that because here in the CSS module,

75
00:04:41,250 --> 00:04:44,100
‫that's one of the types of classes that we have.

76
00:04:44,100 --> 00:04:46,650
‫So we have the primary button, the back button,

77
00:04:46,650 --> 00:04:49,950
‫and the position button, and that's it.

78
00:04:49,950 --> 00:04:51,243
‫So only those three.

79
00:04:52,740 --> 00:04:54,990
‫And here we also need the onClick handler,

80
00:04:54,990 --> 00:04:57,960
‫which is simply getPosition, like this.

81
00:05:02,400 --> 00:05:04,890
‫And then here, let's do some conditional rendering

82
00:05:04,890 --> 00:05:09,890
‫and ask isLoadingPosition, then here display

83
00:05:11,130 --> 00:05:16,130
‫loading and otherwise use your position.

84
00:05:19,110 --> 00:05:21,060
‫Okay, nice.

85
00:05:21,060 --> 00:05:24,243
‫So we should already see that button here in our UI.

86
00:05:25,854 --> 00:05:26,687
‫And there it is.

87
00:05:27,690 --> 00:05:30,390
‫So let's just reload to make sure.

88
00:05:30,390 --> 00:05:31,773
‫And now let's see.

89
00:05:32,790 --> 00:05:36,750
‫So we click here and then we get the question

90
00:05:36,750 --> 00:05:39,510
‫if we want to allow the geolocation.

91
00:05:39,510 --> 00:05:43,740
‫So let's say so, and then at some point it will finish.

92
00:05:43,740 --> 00:05:46,650
‫It will however, not do anything with the map yet,

93
00:05:46,650 --> 00:05:48,930
‫because we haven't set that up.

94
00:05:48,930 --> 00:05:53,930
‫But if we come here to our map component,

95
00:05:55,050 --> 00:05:59,450
‫somewhere down here in this huge tree, then...

96
00:06:03,450 --> 00:06:06,213
‫Now it went back to app, so that's not helpful.

97
00:06:08,040 --> 00:06:09,153
‫Right here.

98
00:06:10,920 --> 00:06:12,660
‫Yeah, we have our geolocation,

99
00:06:12,660 --> 00:06:15,600
‫and then there we have our position.

100
00:06:15,600 --> 00:06:18,360
‫And so what we want to do then with this position

101
00:06:18,360 --> 00:06:20,913
‫is of course to move the map there.

102
00:06:22,380 --> 00:06:25,533
‫All right, and so let's implement that.

103
00:06:26,490 --> 00:06:29,730
‫So basically we need to do the same thing as before,

104
00:06:29,730 --> 00:06:33,510
‫which is to synchronize this map position now

105
00:06:33,510 --> 00:06:35,700
‫with the geolocation position.

106
00:06:35,700 --> 00:06:38,613
‫So we will need another effect to do that.

107
00:06:39,510 --> 00:06:42,480
‫Now, if we were not using the custom hook

108
00:06:42,480 --> 00:06:44,850
‫for the geolocation, then we could of course

109
00:06:44,850 --> 00:06:49,710
‫directly store the received position into map position.

110
00:06:49,710 --> 00:06:53,580
‫So right here we would then...

111
00:06:53,580 --> 00:06:56,850
‫Yeah, set map position right here.

112
00:06:56,850 --> 00:06:59,580
‫However, since we are using this custom hook,

113
00:06:59,580 --> 00:07:02,040
‫we cannot easily do that.

114
00:07:02,040 --> 00:07:05,250
‫So we could of course pass in a setter function as well,

115
00:07:05,250 --> 00:07:07,680
‫but that's not the point here.

116
00:07:07,680 --> 00:07:09,900
‫So instead, this geolocation hook

117
00:07:09,900 --> 00:07:12,663
‫should actually have its own position state.

118
00:07:14,010 --> 00:07:17,490
‫We just happen to now want to synchronize

119
00:07:17,490 --> 00:07:19,950
‫that state here with our map position

120
00:07:19,950 --> 00:07:22,893
‫so that we can then display exactly that.

121
00:07:25,380 --> 00:07:28,383
‫So let's do that here.

122
00:07:31,128 --> 00:07:32,880
‫And so I was telling you about all that

123
00:07:32,880 --> 00:07:36,090
‫because in React there's right now a push

124
00:07:36,090 --> 00:07:39,120
‫to write as little effects as possible.

125
00:07:39,120 --> 00:07:41,970
‫And so if we did what I mentioned earlier,

126
00:07:41,970 --> 00:07:45,030
‫then we could just avoid this one.

127
00:07:45,030 --> 00:07:46,860
‫So then we wouldn't need this effect.

128
00:07:46,860 --> 00:07:48,690
‫But it's also not a big deal.

129
00:07:48,690 --> 00:07:51,480
‫So all we're going to do is to just synchronize

130
00:07:51,480 --> 00:07:54,483
‫two variables here, very similar to what we have here.

131
00:07:55,920 --> 00:07:58,560
‫So here we will run this effect each time

132
00:07:58,560 --> 00:08:01,320
‫that the geolocation position changes,

133
00:08:01,320 --> 00:08:03,540
‫and then what we want to do here

134
00:08:03,540 --> 00:08:08,160
‫is to check if the position actually exists.

135
00:08:08,160 --> 00:08:12,483
‫So only if it exists, then we want to set the map position.

136
00:08:14,058 --> 00:08:15,990
‫And then let's create an array here.

137
00:08:15,990 --> 00:08:20,990
‫So geolocationPosition.lng, and geolocationposition.,

138
00:08:22,800 --> 00:08:27,093
‫actually here it's the LNG and here the latitude.

139
00:08:29,190 --> 00:08:31,260
‫Okay, so at the beginning,

140
00:08:31,260 --> 00:08:35,610
‫the geolocation position will by default still be null,

141
00:08:35,610 --> 00:08:38,250
‫and so then this code here doesn't run.

142
00:08:38,250 --> 00:08:40,020
‫But then, as we click this button,

143
00:08:40,020 --> 00:08:42,960
‫the geolocation will get retrieved,

144
00:08:42,960 --> 00:08:46,470
‫and then of course this state here will update.

145
00:08:46,470 --> 00:08:48,360
‫And so then this effect will run,

146
00:08:48,360 --> 00:08:51,780
‫which will then in turn set the map position,

147
00:08:51,780 --> 00:08:55,230
‫which will re-render the whole component once more,

148
00:08:55,230 --> 00:08:59,790
‫and then finally the map can move to that new position.

149
00:08:59,790 --> 00:09:03,120
‫So basically this effect here introduces another render,

150
00:09:03,120 --> 00:09:05,610
‫which is one of the reasons why we should avoid

151
00:09:05,610 --> 00:09:07,440
‫having too many effects.

152
00:09:07,440 --> 00:09:10,383
‫But we will learn more about that in the next section.

153
00:09:12,030 --> 00:09:15,570
‫But anyway, I think with this we are done.

154
00:09:15,570 --> 00:09:18,270
‫So let's check, and it should move

155
00:09:18,270 --> 00:09:20,313
‫to somewhere close to Lisbon.

156
00:09:21,840 --> 00:09:24,810
‫So yeah, there it is.

157
00:09:24,810 --> 00:09:27,330
‫So this is not because there's a city there,

158
00:09:27,330 --> 00:09:30,810
‫but really because that's where my GPS location

159
00:09:30,810 --> 00:09:32,130
‫is pointed to.

160
00:09:32,130 --> 00:09:35,370
‫But we can actually change that somewhere.

161
00:09:35,370 --> 00:09:38,130
‫I haven't done that in a long time.

162
00:09:38,130 --> 00:09:43,130
‫So maybe it's right here somewhere.

163
00:09:45,810 --> 00:09:48,060
‫Yeah, I think it's in sensors.

164
00:09:48,060 --> 00:09:50,160
‫Yeah, here we can set the location,

165
00:09:50,160 --> 00:09:52,023
‫for example, to San Francisco.

166
00:09:53,100 --> 00:09:58,100
‫And so if I do this again, then let's see what happens.

167
00:10:01,740 --> 00:10:04,293
‫Yeah, we actually moved to San Francisco.

168
00:10:05,670 --> 00:10:09,930
‫So that's where this is in the US west coast.

169
00:10:09,930 --> 00:10:14,930
‫And so this did work, but now of course, let's go back.

170
00:10:14,970 --> 00:10:18,540
‫And actually this button should really disappear

171
00:10:18,540 --> 00:10:21,513
‫once the geolocation position has been received.

172
00:10:22,890 --> 00:10:26,460
‫So the user will not suddenly move somewhere else,

173
00:10:26,460 --> 00:10:28,860
‫and so it doesn't make a lot of sense

174
00:10:28,860 --> 00:10:32,790
‫that this button stays here after the position

175
00:10:32,790 --> 00:10:35,040
‫has already been received.

176
00:10:35,040 --> 00:10:40,040
‫So yeah, let's load this button here conditionally.

177
00:10:41,670 --> 00:10:46,653
‫So only if there is no geolocation position yet.

178
00:10:47,880 --> 00:10:50,250
‫So in that case, display the button.

179
00:10:50,250 --> 00:10:52,293
‫And so now it is gone.

180
00:10:53,370 --> 00:10:56,670
‫Okay, and with this, our map here

181
00:10:56,670 --> 00:10:58,830
‫is actually feature complete.

182
00:10:58,830 --> 00:11:03,830
‫So it can move between cities like this,

183
00:11:04,230 --> 00:11:08,430
‫and we can use our own position to also move the map

184
00:11:08,430 --> 00:11:11,580
‫to that geo-located position.

185
00:11:11,580 --> 00:11:14,670
‫And finally, as we click somewhere on the map,

186
00:11:14,670 --> 00:11:18,150
‫that will then move there as well, open the form,

187
00:11:18,150 --> 00:11:22,590
‫and place the current clicked position in the URL.

188
00:11:22,590 --> 00:11:25,110
‫Great, and in the next lecture,

189
00:11:25,110 --> 00:11:28,770
‫it's actually time to start working on this form.

190
00:11:28,770 --> 00:11:30,813
‫So let's move there right away.

