Skip to content

Commit

Permalink
feat: rename the hook to onAppSessionStart and use callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanenadi committed Sep 18, 2023
1 parent a955158 commit bc364c0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 30 deletions.
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,39 @@ export default function App() {
}
```

### onAppSessionStart

`onAppSessionStart` is a function that is invoked when a new logged-in app
session start (user sign-in/sign-up, app refresh, etc.). It can be used for
analytics purposes, checking subscription status, etc.

````typescript
```typescript
import React, { FC } from 'react';
import { authConfig } from './authConfig';
import { RootProviders, RootStack } from '@lifeomic/react-native-sdk';
export default function App() {
const onAppSessionStart = async (resumeAppSession: () => void) => {
// track app session start metric
await trackAppSessionStart();
resumeAppSession();
};
return (
<DeveloperConfigProvider
developerConfig={{
onAppSessionStart,
}}
>
<RootProviders authConfig={authConfig}>
<RootStack />
</RootProviders>
</DeveloperConfigProvider>
);
}
````
### Customizing the Default Login Screen with a Native View
The default login screen adds a long button to a native view with the name
Expand Down Expand Up @@ -497,4 +530,3 @@ better solution.
For specific guidance on upgrading between versions of this SDK, see
[the upgrade guide](./UPGRADING.md).

17 changes: 2 additions & 15 deletions src/common/DeveloperConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ import { EducationContent } from '../components/TrackTile/services/TrackTileServ
* showing a custom thumbnail image in AdvancedTrackerDetails. The key provided
* must be the `coding.code` value, e.g. '41950-7' for steps.
*
* @param onUserSignIn Allows for providing a callback to be called when a user
* signs in. This is useful for analytics tracking, subscription status checks, etc.
* @param onAppSessionStart Allows for providing a callback to be called when a logged in session starts. This is useful for analytics tracking, subscription status checks, etc.
*/

export interface RouteColor {
Expand Down Expand Up @@ -108,19 +107,7 @@ export type DeveloperConfig = {
ontology?: {
educationContentOverrides?: Record<string, EducationContent>;
};
onUserSignIn?: ({
patientId,
userId,
email,
account,
project,
}: {
patientId: string;
userId: string;
email: string;
account: string;
project: string;
}) => Promise<void>;
onAppSessionStart?: (resumeAppSession: () => void) => Promise<void>;
};

export type LogoHeaderConfig = { [key in Route]?: LogoHeaderOptions };
Expand Down
6 changes: 3 additions & 3 deletions src/components/DeveloperConfigProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ describe('with developerConfig injected into provider', () => {
});

test('allows for onUserSignIn to be configured', async () => {
const onUserSignIn = jest.fn();
const onAppSessionStart = jest.fn();
const { result } = await renderHookInContext({
onUserSignIn,
onAppSessionStart,
});
expect(result.current.onUserSignIn).toEqual(onUserSignIn);
expect(result.current.onAppSessionStart).toEqual(onAppSessionStart);
});
});
20 changes: 9 additions & 11 deletions src/navigators/LoggedInStack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ export function LoggedInStack() {
isFetched: onboardingCourseIsFetched,
} = useOnboardingCourse();
const { isInitialLoading: loadingJoinCircles } = useJoinCircles();
const { onUserSignIn } = useDeveloperConfig();
const { onAppSessionStart } = useDeveloperConfig();
const [shouldWaitForOnUserSignIn, setShouldWaitForOnUserSignIn] = useState(
!!onUserSignIn,
!!onAppSessionStart,
);

const loadingProject = !isFetchedProject || isLoadingProject;
Expand All @@ -58,6 +58,10 @@ export function LoggedInStack() {
const hasAccountAndProject = !!(activeProject?.id && account?.id);
const hasUserAndPatient = !!(activeSubjectId && userData);

const resumeAppSession = useCallback(async () => {
setShouldWaitForOnUserSignIn(false);
}, [setShouldWaitForOnUserSignIn]);

const initialRoute = !(hasAccountAndProject || hasUserAndPatient)
? 'InviteRequired'
: shouldRenderConsentScreen
Expand Down Expand Up @@ -105,26 +109,20 @@ export function LoggedInStack() {
hasAccountAndProject &&
hasUserAndPatient
) {
await onUserSignIn?.({
userId: userData.id,
account: account.id,
email: userData.profile.email,
patientId: activeSubjectId,
project: activeProject.id,
});
setShouldWaitForOnUserSignIn(false);
await onAppSessionStart?.(resumeAppSession);
}
};

executeOnUserSignInIfNeeded();
}, [
onUserSignIn,
userData,
account,
activeProject,
activeSubjectId,
hasAccountAndProject,
hasUserAndPatient,
onAppSessionStart,
resumeAppSession,
shouldWaitForOnUserSignIn,
]);

Expand Down

0 comments on commit bc364c0

Please sign in to comment.