Skip to content

Commit

Permalink
Better state management/loader usage
Browse files Browse the repository at this point in the history
  • Loading branch information
benvinegar committed Nov 18, 2024
1 parent bd619ed commit 5b1d71a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 51 deletions.
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 Down Expand Up @@ -27,38 +27,27 @@ const PaginatedTableCard = ({
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 useEffectdependency array
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [loaderUrl, siteId, interval, filters, timezone, page]); //

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

const hasMore = countsByProperty.length === 10;
Expand Down
6 changes: 3 additions & 3 deletions app/routes/resources.stats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ export const StatsCard = ({
const countFormatter = Intl.NumberFormat("en", { notation: "compact" });

useEffect(() => {
if (dataFetcher.state !== "idle") return;

const params = {
site: siteId,
interval,
Expand All @@ -52,7 +50,9 @@ export const StatsCard = ({
method: "get",
action: `/resources/stats`,
});
}, [dataFetcher, siteId, interval, filters, timezone]);
// NOTE: dataFetcher is intentionally omitted from the useEffectdependency 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 useEffectdependency array
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [siteId, interval, filters, timezone]);

return (
<Card>
Expand Down

0 comments on commit 5b1d71a

Please sign in to comment.