You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 4edc2b0
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: docs/source/tutorial/data-source.md
+22-4Lines changed: 22 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -49,7 +49,7 @@ Now that you have an understanding of how data sources work, let's hook it up fo
49
49
50
50
Create a new `datasources` folder inside the `src` directory. This folder will contain our data source files. Now, create `launch.js` within the `datasources` directory.
51
51
52
-
The REST API endpoint we'll use for our app is `https://api.spacexdata.com/v2/`. Add the endpoint as the base URL as shown in the code below:
52
+
The REST API endpoint we'll use for our app is `https://api.spacexdata.com/v2/`. Go ahead and add the endpoint as the base URL as shown in the code below:
53
53
54
54
_src/datasources/launch.js_
55
55
@@ -132,7 +132,7 @@ async getAllLaunches() {
132
132
133
133
With the above changes, we can easily make changes to the `launchReducer` method while the `getAllLaunches` method stays lean and concise. The `launchReducer` method also makes testing the `LaunchAPI` data source class easier. The later part of this tutorial covers testing!
134
134
135
-
Next, let's take care of getting a specific launch. Add the following methods, `getLaunchById`, and `getLaunchesByIds` to the `LaunchAPI` class.
135
+
Next, let's take care of getting a specific launch. Add the method, `getLaunchById`, and `getLaunchesByIds` to the `LaunchAPI` class.
136
136
137
137
```js
138
138
...
@@ -259,7 +259,7 @@ The `cancelTrip` method requires a `userId` and `launchId` to delete a trip from
259
259
260
260
<h4id="get-launches">Get All Launches By User</h4>
261
261
262
-
We need to get all the launches reserved by a user. This calls for a method, `getLaunchIdsByUser`. Copy the code below and add it to the file.
262
+
We need to get all the launches reserved by a user. This calls for a method, `getLaunchIdsByUser`. Copy the method below and add it to the file.
263
263
264
264
_src/datasources/user.js_
265
265
@@ -289,8 +289,26 @@ Let's analyze the code above.
289
289
290
290
In the `getLaunchIdsByUser` method, a `userId` is accepted via the `context` object. All the trips booked by a user with a particular `userId` are fetched and stored in the `found` variable. If there are trips found, then an array of launch ids are returned else an empty array is returned.
291
291
292
+
<h4id="get-launches">Get Booked Status on a Launch</h4>
292
293
293
-
In the various methods that we created and copied to the `UserAPI` class, you must have noticed `this.store.users` and `this.store.trips`. These are two tables from our SQLite data store.
294
+
We need to add a method that can return the booked status of a launch for a particular user. Copy the method, `isBookedOnLaunch` below and add it to the `UserAPI` class.
295
+
296
+
_src/datasources/user.js_
297
+
298
+
```js
299
+
...
300
+
asyncisBookedOnLaunch({ launchId }) {
301
+
constuserId=this.context.user.id;
302
+
constfound=awaitthis.store.trips.findAll({
303
+
where: { userId, launchId },
304
+
});
305
+
return found &&found.length>0;
306
+
}
307
+
```
308
+
309
+
In the `isBookedOnLaunch` method, we invoke the `findAll` method of the `trips` table to find out if the particular launch passed to the method via `launchId` has been booked by the logged-in user.
310
+
311
+
In the various methods that we created and copied to the `UserAPI` class, you must have noticed `this.store.users` and `this.store.trips`. These are two tables from our potential SQLite data store. Let's create the store!
0 commit comments