Skip to content

Commit

Permalink
Separate loading for list and map view.
Browse files Browse the repository at this point in the history
  • Loading branch information
brysonjbest committed Dec 16, 2024
1 parent a297c5e commit 3bf5f99
Showing 1 changed file with 49 additions and 25 deletions.
74 changes: 49 additions & 25 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ interface TableParams {
export default function Home() {
const [rules, setRules] = useState<RuleInfo[]>([]);
const [loading, setLoading] = useState(true);
const [mapLoading, setMapLoading] = useState(true);
const [listOrMap, setListOrMap] = useState("list");
const [categories, setCategories] = useState<ColumnFilterItem[]>([]);

Expand All @@ -56,6 +57,7 @@ export default function Home() {

const fetchData = async () => {
setLoading(true);
setMapLoading(true);
try {
const response = await getAllRuleData({
page: tableParams.pagination?.current || 1,
Expand All @@ -75,14 +77,16 @@ export default function Home() {
total: response.total,
},
});
setLoading(false);

const { rules: graphRules, categories: graphCategories } = await fetchGraphRuleData();
setKlammRules(graphRules);
setCategoriesMap(graphCategories);
setMapLoading(false);
} catch (error) {
logError(`Error loading rules: ${error}`);
} finally {
setLoading(false);
setMapLoading(false);
}
};

Expand Down Expand Up @@ -247,6 +251,49 @@ export default function Home() {
});
};

const pageContent = () => {
if (loading) {
return (
<Spin tip="Loading rules..." className="spinner">
<div className="content" />
</Spin>
);
}

if (listOrMap === "list") {
return (
<Table
columns={columns}
dataSource={rules}
tableLayout="fixed"
pagination={tableParams.pagination}
loading={loading}
onChange={handleTableChange}
rowKey="_id"
/>
);
}

if (mapLoading) {
return (
<Spin tip="Loading map view..." className="spinner">
<div className="content" />
</Spin>
);
}

return (
<RuleRelationsGraph
rules={klammRules}
categories={categoriesMap}
location={location}
filter={categoryValue}
searchTerm={searchTerm}
setSearchTerm={setSearchTerm}
/>
);
};

return (
<>
<Flex justify="space-between" align="center" className={styles.headerWrapper}>
Expand Down Expand Up @@ -293,30 +340,7 @@ export default function Home() {
value={listOrMap}
/>
</div>
{loading ? (
<Spin tip="Loading rules..." className="spinner">
<div className="content" />
</Spin>
) : listOrMap === "list" ? (
<Table
columns={columns}
dataSource={rules}
tableLayout="fixed"
pagination={tableParams.pagination}
loading={loading}
onChange={handleTableChange}
rowKey="_id"
/>
) : (
<RuleRelationsGraph
rules={klammRules}
categories={categoriesMap}
location={location}
filter={categoryValue}
searchTerm={searchTerm}
setSearchTerm={setSearchTerm}
/>
)}
{pageContent()}
</>
);
}

0 comments on commit 3bf5f99

Please sign in to comment.