-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Migrate deprecated Table to DataTable for EnrolledLearnersTable
- Loading branch information
1 parent
ec24029
commit b75845e
Showing
3 changed files
with
406 additions
and
73 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
51 changes: 51 additions & 0 deletions
51
src/components/EnrolledLearnersTable/data/hooks/useEnrolledLearners.js
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,51 @@ | ||
import { useCallback, useMemo, useState } from 'react'; | ||
import { camelCaseObject } from '@edx/frontend-platform/utils'; | ||
import debounce from 'lodash.debounce'; | ||
import { logError } from '@edx/frontend-platform/logging'; | ||
import EnterpriseDataApiService from '../../../../data/services/EnterpriseDataApiService'; | ||
|
||
const useEnrolledLearners = (enterpriseId) => { | ||
const [isLoading, setIsLoading] = useState(true); | ||
const [enrolledLearners, setEnrolledLearners] = useState( | ||
{ | ||
itemCount: 0, | ||
pageCount: 0, | ||
results: [], | ||
}, | ||
); | ||
|
||
const fetchEnrolledLearners = useCallback(async (args) => { | ||
try { | ||
setIsLoading(true); | ||
const options = { | ||
page: args.pageIndex + 1, | ||
pageSize: args.pageSize, | ||
}; | ||
const response = await EnterpriseDataApiService.fetchEnrolledLearners(enterpriseId, options); | ||
const data = camelCaseObject(response.data); | ||
|
||
setEnrolledLearners({ | ||
itemCount: data.count, | ||
pageCount: data.numPages ?? Math.floor(data.count / options.pageSize), | ||
results: data.results, | ||
}); | ||
} catch (error) { | ||
logError(error); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}, [enterpriseId]); | ||
|
||
const debouncedFetchEnrolledLearners = useMemo( | ||
() => debounce(fetchEnrolledLearners, 300), | ||
[fetchEnrolledLearners], | ||
); | ||
|
||
return { | ||
isLoading, | ||
enrolledLearners, | ||
fetchEnrolledLearners: debouncedFetchEnrolledLearners, | ||
}; | ||
}; | ||
|
||
export default useEnrolledLearners; |
Oops, something went wrong.