Skip to content

Commit

Permalink
feat: add a hooks folder
Browse files Browse the repository at this point in the history
  • Loading branch information
aldbr committed Sep 29, 2023
1 parent bdbcf53 commit 1646971
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
20 changes: 7 additions & 13 deletions src/components/JobDataGrid.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use client";
import { DataGrid } from "@mui/x-data-grid";
import { useOidcAccessToken } from "@axa-fr/react-oidc";
import useSWR from "swr";
import { useJobs } from "@/hooks/jobs";

const columns = [
{ field: "JobID", headerName: "Job ID", width: 70 },
Expand All @@ -16,22 +15,17 @@ const columns = [
* @returns a DataGrid displaying details about jobs
*/
export function JobDataGrid() {
const { accessToken } = useOidcAccessToken();
const { data, error, isLoading } = useJobs();

// TODO: move fetcher to make it usable from other places
const fetcher = (params: string[]) => {
const [path] = params;
return fetch(process.env.NEXT_PUBLIC_DIRACX_URL + path, {
method: "POST",
headers: { Authorization: "Bearer " + accessToken },
}).then((res) => res.json());
};
const { data, error } = useSWR(["/jobs/search?page=0&per_page=100"], fetcher);
if (isLoading) {
return <div>Loading...</div>;
}

if (error) {
return <div>An error occurred while fetching jobs.</div>;
}
if (!data || data.length == 0) {

if (!data || data.length === 0) {
return <div>No job submitted.</div>;
}

Expand Down
26 changes: 26 additions & 0 deletions src/hooks/jobs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useOidcAccessToken } from "@axa-fr/react-oidc";
import useSWR from "swr";

const fetcher = (args: any[]) => {
const [url, accessToken] = args;

return fetch(url, {
method: "POST",
headers: { Authorization: "Bearer " + accessToken },
}).then((res) => {
if (!res.ok) throw new Error("Failed to fetch jobs");
return res.json();
});
};

export function useJobs() {
const { accessToken } = useOidcAccessToken();
const url = `${process.env.NEXT_PUBLIC_DIRACX_URL}/jobs/search?page=0&per_page=100`;
const { data, error } = useSWR([url, accessToken], fetcher);

return {
data,
error,
isLoading: !data && !error,
};
}

0 comments on commit 1646971

Please sign in to comment.