Lessons: 34Length: 3.4 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

6.12 Adding the Edit Application Code

In this lesson, you'll code the Edit Application component.

1.Introduction
2 lessons, 07:21

1.1
Introduction
01:02

1.2
Prerequisites
06:19

2.Getting Started
3 lessons, 30:48

2.1
Creating the App Structure
11:46

2.2
Creating the Server-Side Entry Point
10:14

2.3
Starting the Angular and Express Apps
08:48

3.Setting Up the Mongo Database
4 lessons, 27:53

3.1
Getting MongoDB Up and Running
06:08

3.2
Connecting to MongoDB
06:47

3.3
Creating the Database Schema
07:49

3.4
Creating a Simple Data Access Layer
07:09

4.Creating an API With Express
6 lessons, 29:16

4.1
Handling Requests in Express
09:57

4.2
Taking Advantage of the Express Router
05:52

4.3
Adding the `GET` Handler to the API
05:34

4.4
Adding the `POST` Handler to the API
03:18

4.5
Adding the `PUT` Handler to the API
02:17

4.6
Adding the `DELETE` Handler to the API
02:18

5.Building the Front-End Angular App
6 lessons, 45:52

5.1
Creating the Front-End Models
06:57

5.2
Creating an Angular Service
07:31

5.3
Making HTTP Requests From the Service
08:33

5.4
Setting Up the User Interface
09:05

5.5
Creating All the Components
05:28

5.6
Adding Routing to the App
08:18

6.Creating the App Components
12 lessons, 1:00:02

6.1
Adding the View Lawn Markup
05:55

6.2
Adding the View Lawn Code
06:51

6.3
Adding the Add Lawn Markup
04:34

6.4
Adding the Add Lawn Code
07:41

6.5
Adding the Edit Lawn Markup
03:06

6.6
Adding the Edit Lawn Code
04:11

6.7
Adding the View Application Markup
02:54

6.8
Adding the View Application Code
07:46

6.9
Adding the Add Application Markup
02:16

6.10
Adding the Add Application Code
04:49

6.11
Adding the Edit Application Markup
04:20

6.12
Adding the Edit Application Code
05:39

7.Conclusion
1 lesson, 03:18

7.1
Conclusion
03:18


6.12 Adding the Edit Application Code

And then the final piece of the puzzle is to be able to edit the application that is associated with our lawn. And so we're gonna do a little bit of modifications in here in our edit-application.component.ts file. But just a couple little things, and it's gonna be relatively short. So we now have these two Inputs which we had to define in the previous lesson so we could get things to kinda show up. And then once again, we are going to have an Output. Because we wanna make sure that we let the outside world know that we have updated a lawn again. So we're gonna have updateLawn as our Output. EventEmitter, would you look at that? It actually found the right one this time. So we're gonna use an EventEmitter. And that's gonna be of type Lawn, and we'll go ahead and set that equal to a new instance, Like we have done before. We'll go ahead and bring in the Output. So now we have the event that we can use. Then on the ngOnInit, which we don't really need this time, but we do have to make sure that we bring in our lawnService again, lawnService. And now we have two functions that we need to write. The first one is going to onSubmit. So when we try to submit this form, we need to do a little bit of work. So we'll say, this.lawn.applications. And now this time, we want to make sure we get the one that we are modifying. We don't wanna remove it necessarily, but we need to update it. So let's go ahead and find it in a different way this time. We're gonna use a filter function this time. And we're gonna say for each application in there, we wanna look for the one whose application ID is equal to this.application._id. So we wanna find the application within the lawn that has the same ID as the one that has been passed into this component. And this is gonna return a collection, or an array, so we'll just get the first one. And we'll say, that's gonna be equal to this.application. So we're going to take what we have updated, and just jam that back into our applications array. Because it's been updated and it's gonna work just fine. You could come with a more elegant way to do it, but this will get the job done. And then we'll say this.lawnService. And once again, we are going to update our lawn, or this.lawn. And then we will subscribe to that, we'll get back a response or a result. And then we'll say this.updateLawn, and we will emit that event with that result, so we can pass that out to the rest of the world. And that'll take care of that situation. But the one thing that we have to do right now is we need to do a little bit of work on the date updating. So if we do change the date, remember, we are now subscribing to the ng model change on that model. So we're gonna have this updateDate function, which is gonna have an event on it, which is gonna be of type string. So once it comes into us, it's gonna be a string. So we're gonna have to pick it apart, and then format it properly as a date. So we're gonna say const dateParts. And we're gonna set that equal to event., and we're gonna split this. And we're gonna split it on a hyphen, because that's gonna be the demarcation between the years, the months, and the days. So we're gonna break it up into three parts. And then we're gonna say this.application.date is gonna be equal to a new Date. And then we need to pass in the different date parts that are associated with the year, the month, and the date. But the thing is, right now, these date parts are actually strings, and we need to convert them to integers or to numbers. And the way we do that, or kind of a shorthand notation way that we can do that in JavaScript, or in TypeScript as well, is simply by using the plus operator. So now I can say dateParts, so I can get the first one which is going to be the year. Then I can come in and I can also, now I need to do the month. But the interesting thing about the month is that, the months are actually zero-based, so January is 0 and December is 11. So you're gonna have to make sure that you convert this to an integer, just like this, like we've done for the year. And then, you're gonna have to subtract one to make sure that it's actually properly formatted. Then once we have the month, we can go ahead and do the day. So you do, dateParts 2 is gonna be the day. And then you can put in hours, minutes, seconds, milliseconds if you want to just to give more precision. So we have to do a little bit of work here, I know it seems a little odd. But if you want things to show up properly in the UI, you have to kind of do a little bit of tweaking here and there. But that is pretty much what it's gonna take to update an application. So let's go ahead and save that, then we'll come back to our browser. Let's go ahead and give it a try. So let's go ahead and edit this one. So we're gonna see that the details for when we put down fertilizer. So let's say we want to change this date to be, say the 23rd, and this was a new fertilizer. So we'll say New Fertilizer, and we put down 15 pounds, something like that. So now we can save that. And now we see that we have persisted all of that information, which is good. And everything has been put in there the way that we wanted it to. So now you can go back, and you can begin to track all the lawns that you take care of, all the applications of products that you put down in the lawn. And make sure that you're applying things properly, and doing things at the right dates and the right times, to get your optimum performance all throughout the year.

Back to the top