Skip to content

Commit

Permalink
refactor: Add swr for better fetch performance and loading state control
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielborgesdm committed Aug 11, 2024
1 parent 4551001 commit df2065c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 34 deletions.
29 changes: 29 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"react-hook-form": "^7.51.5",
"react-router-dom": "^6.23.1",
"react-scripts": "^5.0.1",
"swr": "^2.2.5",
"typescript": "^4.9.5",
"web-vitals": "^2.1.4",
"yup": "^1.4.0"
Expand Down
64 changes: 30 additions & 34 deletions frontend/src/pages/Authors/Authors.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
import React, { useEffect, useState } from "react";
import React from "react";
import ManagementService from "services/managementService";
import { useFilterHook } from "components/Hooks/UseFilterHook";
import { Author } from "types/author";
import useSWR from "swr";

const managementService = new ManagementService();

const Authors: React.FC = () => {
const [authors, setAuthors] = useState<Author[]>([]);
const { handleChangeFilter, shouldFilterInWith } = useFilterHook();

useEffect(() => {
loadAuthors();
}, []);

const loadAuthors = async () => {
const authors = await managementService.getAuthors();

setAuthors(authors);
};
const { data: authorsData, error, isLoading } = useSWR("authors", () => managementService.getAuthors());

return (
<>
Expand Down Expand Up @@ -60,26 +51,26 @@ const Authors: React.FC = () => {
</div>
<hr />
<div className="block max-h-[78dvh] overflow-y-auto">
<table className="w-full text-sm text-left rtl:text-right text-gray-500">
<thead className="text-xs uppercase ">
<tr>
<th scope="col" className="px-1 py-3">
Author
</th>
<th scope="col" className="px-1 py-3">
Nationality
</th>
<th scope="col" className="px-1 py-3">
Birth date
</th>
<th scope="col" className="px-1 py-3">
E-mail
</th>
</tr>
</thead>
<tbody>
{authors?.length > 0 &&
authors.map(
{authorsData && authorsData.length > 0 ? (
<table className="w-full text-sm text-left rtl:text-right text-gray-500">
<thead className="text-xs uppercase ">
<tr>
<th scope="col" className="px-1 py-3">
Author
</th>
<th scope="col" className="px-1 py-3">
Nationality
</th>
<th scope="col" className="px-1 py-3">
Birth date
</th>
<th scope="col" className="px-1 py-3">
E-mail
</th>
</tr>
</thead>
<tbody>
{authorsData.map(
({ name, nationality, birthDate, email, id }: Author) =>
shouldFilterInWith(name, nationality, birthDate, email, id) && (
<tr key={id} className="border-b hover:bg-gray-50">
Expand All @@ -92,8 +83,13 @@ const Authors: React.FC = () => {
</tr>
),
)}
</tbody>
</table>
</tbody>
</table>
) : isLoading ? (
<span>Loading...</span>
) : (
<span className="pt-5">No authors registered</span>
)}
</div>
</>
);
Expand Down

0 comments on commit df2065c

Please sign in to comment.