Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce card data fetching complexity #115

Merged
merged 3 commits into from
Nov 18, 2024
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
45 changes: 17 additions & 28 deletions app/components/PaginatedTableCard.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect } from "react";
import { useEffect, useState } from "react";
import TableCard from "~/components/TableCard";

import { Card } from "./ui/card";
Expand All @@ -8,7 +8,7 @@
interface PaginatedTableCardProps {
siteId: string;
interval: string;
dataFetcher: any;

Check warning on line 11 in app/components/PaginatedTableCard.tsx

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
columnHeaders: string[];
filters?: SearchFilters;
loaderUrl: string;
Expand All @@ -27,38 +27,27 @@
timezone,
}: PaginatedTableCardProps) => {
const countsByProperty = dataFetcher.data?.countsByProperty || [];
const page = dataFetcher.data?.page || 1;

const loadData = (page: string | undefined = undefined) => {
// turn filters into query string
const filterString = filters
? Object.entries(filters)
.map(([key, value]) => `&${key}=${value}`)
.join("")
: "";

let url = `${loaderUrl}?site=${siteId}&interval=${interval}&timezone=${timezone}${filterString}`;
if (page) {
url += `&page=${page}`;
}

dataFetcher.load(url);
};
const [page, setPage] = useState(1);

useEffect(() => {
if (dataFetcher.state === "idle") {
loadData();
}
}, []);
const params = {
site: siteId,
interval,
timezone,
...filters,
page,
};

useEffect(() => {
if (dataFetcher.state === "idle") {
loadData();
}
}, [siteId, interval, filters]);
dataFetcher.submit(params, {
method: "get",
action: loaderUrl,
});
// NOTE: dataFetcher is intentionally omitted from the useEffect dependency array
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [loaderUrl, siteId, interval, filters, timezone, page]); //

function handlePagination(page: number) {
loadData(page.toString());
setPage(page);

Check warning on line 50 in app/components/PaginatedTableCard.tsx

View check run for this annotation

Codecov / codecov/patch

app/components/PaginatedTableCard.tsx#L50

Added line #L50 was not covered by tests
}

const hasMore = countsByProperty.length === 10;
Expand Down
34 changes: 14 additions & 20 deletions app/routes/resources.stats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,25 @@ export const StatsCard = ({
timezone: string;
}) => {
const dataFetcher = useFetcher<typeof loader>();

const { views, visits, visitors } = dataFetcher.data || {};
const countFormatter = Intl.NumberFormat("en", { notation: "compact" });

const loadData = () => {
const filterString = filters
? Object.entries(filters)
.map(([key, value]) => `&${key}=${value}`)
.join("")
: "";

const url = `/resources/stats?site=${siteId}&interval=${interval}&timezone=${timezone}${filterString}`;
dataFetcher.load(url);
};

useEffect(() => {
if (dataFetcher.state === "idle") {
loadData();
}
}, []);
const params = {
site: siteId,
interval,
timezone,
...filters,
};

useEffect(() => {
if (dataFetcher.state === "idle") {
loadData();
}
}, [siteId, interval, filters]);
dataFetcher.submit(params, {
method: "get",
action: `/resources/stats`,
});
// NOTE: dataFetcher is intentionally omitted from the useEffect dependency array
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [siteId, interval, filters, timezone]);

return (
<Card>
Expand Down
33 changes: 13 additions & 20 deletions app/routes/resources.timeseries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,28 +60,21 @@ export const TimeSeriesCard = ({
const dataFetcher = useFetcher<typeof loader>();
const { chartData, intervalType } = dataFetcher.data || {};

const loadData = () => {
const filterString = filters
? Object.entries(filters)
.map(([key, value]) => `&${key}=${value}`)
.join("")
: "";

const url = `/resources/timeseries?site=${siteId}&interval=${interval}&timezone=${timezone}${filterString}`;
dataFetcher.load(url);
};

useEffect(() => {
if (dataFetcher.state === "idle") {
loadData();
}
}, []);
const params = {
site: siteId,
interval,
timezone,
...filters,
};

useEffect(() => {
if (dataFetcher.state === "idle") {
loadData();
}
}, [siteId, interval, filters]);
dataFetcher.submit(params, {
method: "get",
action: `/resources/timeseries`,
});
// NOTE: dataFetcher is intentionally omitted from the useEffect dependency array
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [siteId, interval, filters, timezone]);

return (
<Card>
Expand Down
Loading