Skip to content

Commit

Permalink
Main: Add support for console API pagination on /list endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron5670 committed Jun 19, 2024
1 parent b1ff189 commit 1c836dc
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 26 deletions.
50 changes: 36 additions & 14 deletions src/hooks/useDynamicConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,50 @@ import type {DynamicConfig} from "~types/statsig";

import {useLocalStorage} from "@uidotdev/usehooks";
import {api} from "~helpers/fetcher";
import {useEffect, useState} from "react";
import useSWR from "swr";

const limit = 100;

export const useDynamicConfigs = (): {
error: null | string,
dynamicConfigs: DynamicConfig[],
isLoading: boolean,
dynamicConfigs: DynamicConfig[],
error: null | string,
isLoading: boolean,
} => {
const [apiKey] = useLocalStorage("statsig-console-api-key");
const {data, isLoading} = useSWR(apiKey ? '/dynamic_configs' : null, () => api.get('/dynamic_configs', {
headers: {
"STATSIG-API-KEY": apiKey as string,
const [page, setPage] = useState(1);
const [dynamicConfigs, setDynamicConfigs] = useState<DynamicConfig[]>([]);
const [apiKey] = useLocalStorage("statsig-console-api-key");

const { data, error, isLoading } = useSWR(apiKey ? ['/dynamic_configs', page] : null, () =>
api.get('/dynamic_configs', {
headers: {
"STATSIG-API-KEY": apiKey as string,
},
params: {
limit,
page,
}
}).then(res => res.data)
.catch(err => err)
);

useEffect(() => {
if (data?.data && data?.pagination?.totalItems > dynamicConfigs.length) {
setDynamicConfigs(prev => [...prev, ...data.data]);
}
}, [data]);

useEffect(() => {
if (data?.pagination?.totalItems && dynamicConfigs.length < data.pagination.totalItems) {
setPage(prev => prev + 1);
}
}).then(res => res.data)
.catch(err => err)
);
}, [dynamicConfigs]);

const error = data?.status || !data?.data ? 'An error occurred while fetching dynamic configs data.' : null;
const dynamicConfigs = data?.data || [];
const fetchError = error ? 'An error occurred while fetching experiment data.' : null;

return {
error,
dynamicConfigs,
error: fetchError,
isLoading,
};
};
};
46 changes: 34 additions & 12 deletions src/hooks/useExperiments.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,50 @@
import type {Experiment} from "~types/statsig";
import type { Experiment } from "~types/statsig";

import {useLocalStorage} from "@uidotdev/usehooks";
import {api} from "~helpers/fetcher";
import { useLocalStorage } from "@uidotdev/usehooks";
import { api } from "~helpers/fetcher";
import { useEffect, useState } from "react";
import useSWR from "swr";

const limit = 100;

export const useExperiments = (): {
error: null | string,
experiments: Experiment[],
isLoading: boolean,
} => {
const [page, setPage] = useState(1);
const [experiments, setExperiments] = useState<Experiment[]>([]);
const [apiKey] = useLocalStorage("statsig-console-api-key");
const {data, isLoading} = useSWR(apiKey ? '/experiments' : null, () => api.get('/experiments', {
headers: {
"STATSIG-API-KEY": apiKey as string,
}
}).then(res => res.data)
.catch(err => err)

const { data, error, isLoading } = useSWR(apiKey ? ['/experiments', page] : null, ([url, page]) =>
api.get(url, {
headers: {
"STATSIG-API-KEY": apiKey as string,
},
params: {
limit,
page,
}
}).then(res => res.data)
.catch(err => err)
);

const error = data?.status || !data?.data ? 'An error occurred while fetching experiment data.' : null;
const experiments = data?.data || [];
useEffect(() => {
if (data?.data && data?.pagination?.totalItems > experiments.length) {
setExperiments(prev => [...prev, ...data.data]);
}
}, [data]);

useEffect(() => {
if (data?.pagination?.totalItems && experiments.length < data.pagination.totalItems) {
setPage(prev => prev + 1);
}
}, [experiments]);

const fetchError = error ? 'An error occurred while fetching experiment data.' : null;

return {
error,
error: fetchError,
experiments,
isLoading,
};
Expand Down

0 comments on commit 1c836dc

Please sign in to comment.