chore: Remove params requirement on fetchPage

This commit is contained in:
Tom Moor
2024-03-17 13:13:23 -04:00
parent 8bd0aa43bc
commit 4e47f4c1e2
4 changed files with 12 additions and 4 deletions

View File

@@ -16,10 +16,12 @@ type RequestResponse<T> = {
* A hook to make an API request and track its state within a component. * A hook to make an API request and track its state within a component.
* *
* @param requestFn The function to call to make the request, it should return a promise. * @param requestFn The function to call to make the request, it should return a promise.
* @param makeRequestOnMount Whether to make the request when the component mounts.
* @returns An object containing the request state and a function to start the request. * @returns An object containing the request state and a function to start the request.
*/ */
export default function useRequest<T = unknown>( export default function useRequest<T = unknown>(
requestFn: () => Promise<T> requestFn: () => Promise<T>,
makeRequestOnMount = false
): RequestResponse<T> { ): RequestResponse<T> {
const isMounted = useIsMounted(); const isMounted = useIsMounted();
const [data, setData] = React.useState<T>(); const [data, setData] = React.useState<T>();
@@ -48,5 +50,11 @@ export default function useRequest<T = unknown>(
return undefined; return undefined;
}, [requestFn, isMounted]); }, [requestFn, isMounted]);
React.useEffect(() => {
if (makeRequestOnMount) {
void request();
}
}, [request, makeRequestOnMount]);
return { data, loading, error, request }; return { data, loading, error, request };
} }

View File

@@ -41,7 +41,7 @@ function Security() {
data: providers, data: providers,
loading, loading,
request, request,
} = useRequest(() => authenticationProviders.fetchPage({})); } = useRequest(authenticationProviders.fetchPage);
React.useEffect(() => { React.useEffect(() => {
if (!providers && !loading) { if (!providers && !loading) {

View File

@@ -262,7 +262,7 @@ export default abstract class Store<T extends Model> {
} }
@action @action
fetchPage = async (params: FetchPageParams | undefined): Promise<T[]> => { fetchPage = async (params?: FetchPageParams | undefined): Promise<T[]> => {
if (!this.actions.includes(RPCAction.List)) { if (!this.actions.includes(RPCAction.List)) {
throw new Error(`Cannot list ${this.modelName}`); throw new Error(`Cannot list ${this.modelName}`);
} }

View File

@@ -74,7 +74,7 @@ class AuthenticationProvider extends Model<
@Column(DataType.UUID) @Column(DataType.UUID)
teamId: string; teamId: string;
@HasMany(() => UserAuthentication, "providerId") @HasMany(() => UserAuthentication, "authenticationProviderId")
userAuthentications: UserAuthentication[]; userAuthentications: UserAuthentication[];
// instance methods // instance methods