Skip to content

Commit

Permalink
feat: add 'mergeUpdate' override to merge nested props
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor-anderson committed May 13, 2023
1 parent 2baa88f commit 0e87df1
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/app/apolloCache/reactiveVars/authenticatedUserStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,36 @@ import { isConnectOnboardingNeededStore } from "./isConnectOnboardingNeededStore
import type { EncodedAuthToken, AuthTokenPayload } from "@types";

class AuthenticatedUserStore extends ReactiveStore<AuthTokenPayload> {
/**
* An override of `ReactiveStore.mergeUpdate` that shallow-merges all
* `AuthTokenPayload` properties, including nested objects.
*/
override mergeUpdate(partialNewValue: Partial<AuthTokenPayload>) {
const currentValue = this.get();

const typeSafeCurrentValue =
currentValue && typeof currentValue === "object"
? currentValue
: ({} as Partial<AuthTokenPayload>);

return this.set({
...typeSafeCurrentValue,
...partialNewValue,
profile: {
...(typeSafeCurrentValue?.profile ?? {}),
...(partialNewValue?.profile ?? {}),
},
subscription: {
...(typeSafeCurrentValue?.subscription ?? {}),
...(partialNewValue?.subscription ?? {}),
},
stripeConnectAccount: {
...(typeSafeCurrentValue?.stripeConnectAccount ?? {}),
...(partialNewValue?.stripeConnectAccount ?? {}),
},
} as AuthTokenPayload);
}

/**
* Process an auth token to authenticate the user.
*/
Expand Down

0 comments on commit 0e87df1

Please sign in to comment.