Skip to content

Commit

Permalink
Update job counts on filter changes
Browse files Browse the repository at this point in the history
  • Loading branch information
keiranjprice101 committed Feb 21, 2025
1 parent 74bef92 commit 298eb45
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/components/jobs/JobTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,21 @@ const JobTable: React.FC<{
const offset = currentPage * rowsPerPage;
const [filters, setFilters] = useState<JobQueryFilters>({});
const query = `limit=${rowsPerPage}&offset=${offset}&order_by=${orderBy}&order_direction=${orderDirection}&include_run=true&filters=${JSON.stringify(filters)}&as_user=${asUser}`;
const countQuery = `filters=${JSON.stringify(filters)}`;
const queryPath = selectedInstrument === 'ALL' ? '/jobs' : `/instrument/${selectedInstrument}/jobs`;
const countQueryPath = selectedInstrument === 'ALL' ? '/jobs/count' : `/instrument/${selectedInstrument}/jobs/count`;
const fetchJobs = useFetchJobs(queryPath, query, setJobs);
const fetchTotalCount = useFetchTotalCount(countQueryPath, setTotalRows);
const fetchTotalCount = useFetchTotalCount(countQueryPath, countQuery, setTotalRows);

useEffect(() => {
fetchJobs().then(() => setLoading(false));
void fetchTotalCount();
}, [fetchTotalCount, fetchJobs]);

const refreshJobs = (): Promise<void> => Promise.resolve(fetchJobs());
const refreshJobs = (): void => {
void Promise.resolve(fetchJobs());
void Promise.resolve(fetchTotalCount);
};
const handleRerun = async (job: Job): Promise<void> => {
await fiaApi.post('/job/rerun', { job_id: job.id, runner_image: job.runner_image, script: job.script.value });
};
Expand Down
2 changes: 1 addition & 1 deletion src/components/jobs/Row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ const Row: React.FC<{
showInstrumentColumn: boolean;
index: number;
submitRerun: (job: Job) => Promise<void>;
refreshJobs: () => Promise<void>;
refreshJobs: () => void;
}> = ({ job, showInstrumentColumn, index, submitRerun, refreshJobs }) => {
const [open, setOpen] = useState(false);
const theme = useTheme();
Expand Down
5 changes: 3 additions & 2 deletions src/lib/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ export const useFetchJobs = (

export const useFetchTotalCount = (
apiPath: string,
query: string,
setTotalRows: React.Dispatch<React.SetStateAction<number>>
): (() => Promise<void>) => {
return useCallback(async () => {
fiaApi
.get(apiPath)
.get(`${apiPath}?${query}`)
.then((res) => setTotalRows(res.data.count))
.catch((err) => console.error('Error fetching row count', err.message));
}, [apiPath, setTotalRows]);
}, [apiPath, setTotalRows, query]);
};

0 comments on commit 298eb45

Please sign in to comment.