-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add classroom tab in statistics dashboard
- Loading branch information
Showing
5 changed files
with
153 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React from 'react'; | ||
import type { QueryFunction } from 'react-query'; | ||
import { useQueryClient, useQuery } from 'react-query'; | ||
|
||
import { axiosRequest } from 'src/utils/axiosRequest'; | ||
import type { Classroom, ClassroomFilter } from 'types/classroom.type'; | ||
|
||
export const useClassrooms = (options?: ClassroomFilter): { classrooms: Classroom[]; setClassrooms(newClassrooms: Classroom[]): void } => { | ||
const queryClient = useQueryClient(); | ||
const buildUrl = () => { | ||
if (!options) return '/classrooms'; | ||
const { villageId } = options; | ||
const queryParams = []; | ||
|
||
if (villageId) queryParams.push(`villageId=${villageId}`); | ||
|
||
return queryParams.length ? `/classrooms?${queryParams.join('&')}` : '/classrooms'; | ||
}; | ||
|
||
const url = buildUrl(); | ||
// The query function for fetching villages, memoized with useCallback | ||
const getClassrooms: QueryFunction<Classroom[]> = React.useCallback(async () => { | ||
const response = await axiosRequest({ | ||
method: 'GET', | ||
url, | ||
}); | ||
if (response.error) { | ||
return []; | ||
} | ||
return response.data; | ||
}, [url]); | ||
|
||
// Notice that we include `countryIsoCode` in the query key so that the query is refetched | ||
const { data, isLoading, error } = useQuery<Classroom[], unknown>( | ||
['classrooms', options], // Include countryIsoCode in the query key to trigger refetching | ||
getClassrooms, | ||
); | ||
|
||
const setClassrooms = React.useCallback( | ||
(newClassrooms: Classroom[]) => { | ||
queryClient.setQueryData(['classrooms', options], newClassrooms); // Ensure that the query key includes countryIsoCode | ||
}, | ||
[queryClient, options], | ||
); | ||
|
||
return { | ||
classrooms: isLoading || error ? [] : data || [], | ||
setClassrooms, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters