Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(expo): add ExpoClientStorage for persisted store in RN #3307

Merged
merged 25 commits into from
Nov 17, 2023

Conversation

ThatOneBro
Copy link
Member

@ThatOneBro ThatOneBro commented Nov 11, 2023

Needs some cleanup but mostly done. Working well on Android device. Still need to add a few tests too.

Fixes #3301

TODO:

  • Add tests
  • Change error to OperationOutcomeError
  • Clean up .catchs
  • Fix repeated call to removeKey invoking several calls to setKeys in clear
  • Test MedplumProvider setting and unsetting loading
  • Add docs for how to await initialized and what it means
  • Add docs for how to use the loading state from useMedplumContext

Copy link

vercel bot commented Nov 11, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Ignored Deployments
Name Status Preview Comments Updated (UTC)
medplum-storybook ⬜️ Ignored (Inspect) Visit Preview Nov 17, 2023 1:09am
medplum-www ⬜️ Ignored (Inspect) Visit Preview Nov 17, 2023 1:09am

Copy link
Contributor

sweep-ai bot commented Nov 11, 2023

Apply Sweep Rules to your PR?

  • Apply: All new business logic should have corresponding unit tests.
  • Apply: Refactor large functions to be more modular.

Copy link

github-actions bot commented Nov 11, 2023

prettier errors have been resolved. Thank you.

#cd5357dd2c700cb62837819e74f5054b675db19f

@ThatOneBro ThatOneBro changed the title feat(expo): add ExpoClientStorage for persisted store on RN feat(expo): add ExpoClientStorage for persisted store in RN Nov 11, 2023
@ThatOneBro ThatOneBro force-pushed the derrick-fix-react-native-login-persistence branch 2 times, most recently from 2615998 to d81ad52 Compare November 13, 2023 23:01
@ThatOneBro ThatOneBro marked this pull request as ready for review November 13, 2023 23:24
@ThatOneBro ThatOneBro requested a review from a team as a code owner November 13, 2023 23:24
Copy link

github-actions bot commented Nov 13, 2023

Messages
📖 @medplum/core: 141.9 kB
📖 @medplum/react: 191.4 kB

Generated by 🚫 dangerJS against 8c5ef7d

Copy link
Member

@codyebberson codyebberson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever solution to a hard problem. Looks good from structural perspective. A bunch of nitpicky style comments.

packages/core/src/client.ts Outdated Show resolved Hide resolved
packages/core/src/client.ts Outdated Show resolved Hide resolved
packages/core/src/client.ts Outdated Show resolved Hide resolved
packages/expo-polyfills/src/index.test.ts Outdated Show resolved Hide resolved
class SyncSecureStorage implements Storage {
private readonly storage: Map<string, string>;
private readonly initPromise: Promise<void>;
private isInitialized = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably pedantic, but I think the recommended model for boolean naming is:

  1. noun for the variable name (i.e., initialized)
  2. is prefix for a getter (i.e., isInitialized())

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True,

In this case was trying to avoid collision with initialized Promise on the object... but yeah you're 100% right

examples/medplum-react-native-example/src/Home.tsx Outdated Show resolved Hide resolved
@@ -42,6 +43,7 @@
"esbuild": "0.19.5",
"esbuild-node-externals": "1.9.0",
"jest": "29.7.0",
"jest-expo": "^49.0.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why jest-expo? I don't see any calls to it. And it's big :(

Copy link
Member Author

@ThatOneBro ThatOneBro Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the test environment for Expo, it actually fixes a lot of problems with react-native preset, and better represents the various environments for Android, iOS, and Web as separate environments. See the jest.config.json:

"projects": [{ "preset": "jest-expo/ios" }, { "preset": "jest-expo/android" }],

packages/core/src/client.ts Outdated Show resolved Hide resolved
@ThatOneBro
Copy link
Member Author

After the introduction of the ExpoClientStorage, this should be the minimal example of using MedplumClient in React Native.

If you want to wait to load components until after the MedplumClient has initialized, you can conditionally render based on the loading property from the useMedplumContext hook, like so:

import { MedplumClient } from '@medplum/core';
import { MedplumProvider, useMedplumContext } from '@medplum/react-hooks';
import { polyfillMedplumWebAPIs, ExpoClientStorage } from '@medplum/expo-polyfills';

polyfillMedplumWebAPIs();

const medplum = new MedplumClient({ storage: new ExpoClientStorage() });

function Home(): JSX.Element {
  const { loading } = useMedplumContext();
  return loading ? <div>Loading...</div> : <div>Loaded!</div>;
}

function App(): JSX.Element {
  return (
    <MedplumProvider medplum={medplum}>
      <Home />
    </MedplumProvider>
  );
}

Any thoughts @codyebberson @rhys-jmc ?

@ThatOneBro ThatOneBro force-pushed the derrick-fix-react-native-login-persistence branch from 12cfbb5 to 6aacaed Compare November 17, 2023 00:57
Copy link

sonarcloud bot commented Nov 17, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

94.0% 94.0% Coverage
0.0% 0.0% Duplication

@ThatOneBro ThatOneBro added this pull request to the merge queue Nov 17, 2023
@ThatOneBro ThatOneBro removed this pull request from the merge queue due to a manual request Nov 17, 2023
private secureStorage?: SyncSecureStorage;
constructor() {
// Metro should automatically prune these branches out at compile time
if (Platform.OS === 'web') {
Copy link
Member Author

@ThatOneBro ThatOneBro Nov 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this seem like a sane way to handle the React Native Web case? These checks should compile out when Metro compiles the final app. @codyebberson

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable - it's exported by Expo, so feels official.

@reshmakh reshmakh added this to the November 30, 2023 milestone Nov 17, 2023
Copy link
Member

@codyebberson codyebberson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks @ThatOneBro

@ThatOneBro ThatOneBro added this pull request to the merge queue Nov 17, 2023
Merged via the queue into main with commit 077654f Nov 17, 2023
14 checks passed
@ThatOneBro ThatOneBro deleted the derrick-fix-react-native-login-persistence branch November 17, 2023 17:00
codyebberson added a commit that referenced this pull request Nov 19, 2023
Refinements and additions fot Health Gorilla documentation as we proceed through testing (#3333)
Tighter input validation on security endpoints (#3334)
Use react-jsx transform (#3338)
Create documentation for chained search (#3312)
Document performing insurance eligibility checks (#3280)
feat(expo): add `ExpoClientStorage` for persisted store in RN (#3307)
test(server): add `test.config.json` with port `8104` (#3329)
Make Postgres version configurable in CDK (#3327)
Configure agent service name and automatic start (#3321)
Link authentication methods to relevant Medplum Client functions (#3323)
server centralize request validation for co-location and reduced redundancy (#3228)
Fixed FHIR link (#3319)
github-merge-queue bot pushed a commit that referenced this pull request Nov 19, 2023
Refinements and additions fot Health Gorilla documentation as we proceed through testing (#3333)
Tighter input validation on security endpoints (#3334)
Use react-jsx transform (#3338)
Create documentation for chained search (#3312)
Document performing insurance eligibility checks (#3280)
feat(expo): add `ExpoClientStorage` for persisted store in RN (#3307)
test(server): add `test.config.json` with port `8104` (#3329)
Make Postgres version configurable in CDK (#3327)
Configure agent service name and automatic start (#3321)
Link authentication methods to relevant Medplum Client functions (#3323)
server centralize request validation for co-location and reduced redundancy (#3228)
Fixed FHIR link (#3319)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Fix auth not persisting in React Native
4 participants