Skip to content

Commit

Permalink
Fix the solution code for “Using Async Storage”
Browse files Browse the repository at this point in the history
Also, other minor improvements.
  • Loading branch information
chasenlehara committed May 24, 2024
1 parent 2f2de42 commit bb79a4f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import AsyncStorage from "@react-native-async-storage/async-storage"

// Exercise: Implement the four `AsyncStorage` helpers in `storage.ts`.
// Exercise: Implement the four `AsyncStorage` helpers.
export const getData = async <T>(key: string): Promise<T | undefined> => {}

export const getAllKeys = (): Promise<readonly string[]> => {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export async function apiRequest<
if (method === "GET" && response.ok) {
await storeData<CachedResponse<Data>>(keyPrefix + requestUrl, {
data: data,
dateTime: new Date().toDateString(),
dateTime: new Date().toJSON(),
})
}

Expand Down
18 changes: 14 additions & 4 deletions src/react-native/15-using-asyncstorage/using-asyncstorage.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,24 @@ Here are some pointers on implementing this strategy:
npm install @react-native-async-storage/async-storage@1
```

✏️ Update **jest-setup.ts** to be:
✏️ Terminate the existing dev server and start it again:

@diff ../../../exercises/react-native/14-user-input/01-solution/jest-setup.ts ../../../exercises/react-native/15-async-storage/01-problem/jest-setup.ts only
```bash
npm run start
```

✏️ Create **src/services/storage/index.ts** and update it to be:
✏️ Update **jest-setup.ts** to be:

@sourceref ../../../exercises/react-native/15-async-storage/01-problem/src/services/storage/index.ts
@diff ../../../exercises/react-native/14-user-input/01-solution/jest-setup.ts ../../../exercises/react-native/15-async-storage/01-problem/jest-setup.ts only

✏️ Create **src/services/storage/storage.ts** and update it to be:

@sourceref ../../../exercises/react-native/15-async-storage/01-problem/src/services/storage/storage.ts
@highlight 3

✏️ Create **src/services/storage/index.ts** and update it to be:

@sourceref ../../../exercises/react-native/15-async-storage/01-problem/src/services/storage/index.ts

✏️ Update **src/services/pmo/api/api.ts** to be:

Expand Down Expand Up @@ -166,6 +173,8 @@ You can verify that this is working correctly by using DevTools to:
- View the requests (or lack thereof) being made.
- Inspect the stored data.

**Hint:** Use [`new Date().toJSON()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toJSON) to get the current datetime as a string.

### Solution 1

If you’ve implemented the solution correctly, the tests will pass when you run `npm run test`!
Expand Down Expand Up @@ -249,6 +258,7 @@ We’ll assume that the data is version `1` if we haven’t stored the version n
✏️ Create **src/services/DataMigration/DataMigration.tsx** and update it to be:

@sourceref ../../../exercises/react-native/15-async-storage/02-problem/src/services/DataMigration/DataMigration.tsx
@highlight 13, 21-23, only

✏️ Create **src/services/DataMigration/index.ts** and update it to be:

Expand Down

0 comments on commit bb79a4f

Please sign in to comment.