Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package/src/libraries/react/hooks/useCustomerBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ export interface UseCustomerParams<
errorOnNotFound?: boolean;
expand?: T;
swrConfig?: SWRConfiguration;
/** Extra key(s) appended to the SWR query key that trigger a refetch when any value changes. */
extraQueryKeys?: (string | null | undefined)[];
}

export const useCustomerBase = <
Expand Down Expand Up @@ -158,7 +160,7 @@ export const useCustomerBase = <
}

const baseUrl = client?.backendUrl || "";
const queryKey = ["customer", baseUrl, params?.expand];
const queryKey = ["customer", baseUrl, params?.expand, ...(params?.extraQueryKeys ?? [])];

const fetchCustomer = async () => {
const { data, error } = await client!.createCustomer<T>({
Expand Down
8 changes: 6 additions & 2 deletions package/src/libraries/react/hooks/useEntity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { useEntityBase } from "./useEntityBase";

export const useEntity = (
entityId: string | null,
params?: GetEntityParams
params?: GetEntityParams & {
/** Extra key(s) appended to the SWR query key that trigger a refetch when any value changes. */
extraQueryKeys?: (string | null | undefined)[];
}
) => {
return useEntityBase({ AutumnContext, entityId, params });
const { extraQueryKeys, ...restParams } = params ?? {};
return useEntityBase({ AutumnContext, entityId, params: restParams, extraQueryKeys });
};
5 changes: 4 additions & 1 deletion package/src/libraries/react/hooks/useEntityBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ import { AutumnError, CheckResult, Entity } from "@sdk";
export const useEntityBase = ({
entityId,
params,
extraQueryKeys,
AutumnContext,
}: {
entityId: string | null;
params?: GetEntityParams;
/** Extra key(s) appended to the SWR query key that trigger a refetch when any value changes. */
extraQueryKeys?: (string | null | undefined)[];
AutumnContext: React.Context<AutumnContextParams>;
}): {
entity: Entity | null;
Expand All @@ -31,7 +34,7 @@ export const useEntityBase = ({
track: (params: TrackParams) => void;
} => {
const { client } = useContext(AutumnContext);
const queryKey = ["entity", entityId, params?.expand];
const queryKey = ["entity", entityId, params?.expand, ...(extraQueryKeys ?? [])];

const context = useAutumnContext({
AutumnContext,
Expand Down
11 changes: 9 additions & 2 deletions package/src/next/client/hooks/useEntity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import { GetEntityParams } from "../../../sdk/customers/entities/entTypes";
import { useEntityBase } from "../../../libraries/react/hooks/useEntityBase";
import { AutumnContext } from "../../../libraries/react/AutumnContext";

export const useEntity = (entityId: string | null, params?: GetEntityParams) => {
return useEntityBase({ AutumnContext, entityId, params });
export const useEntity = (
entityId: string | null,
params?: GetEntityParams & {
/** Extra key(s) appended to the SWR query key that trigger a refetch when any value changes. */
extraQueryKeys?: (string | null | undefined)[];
}
) => {
const { extraQueryKeys, ...restParams } = params ?? {};
return useEntityBase({ AutumnContext, entityId, params: restParams, extraQueryKeys });
};