-
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 RegisteredLearnersTable
- Loading branch information
1 parent
ec24029
commit 40ba31d
Showing
3 changed files
with
330 additions
and
51 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
49 changes: 49 additions & 0 deletions
49
src/components/RegisteredLearnersTable/data/hooks/useRegisteredLearners.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,49 @@ | ||
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 useRegisteredLearners = (enterpriseId) => { | ||
const [isLoading, setIsLoading] = useState(true); | ||
const [registeredLearners, setRegisteredLearners] = useState({ | ||
itemCount: 0, | ||
pageCount: 0, | ||
results: [], | ||
}); | ||
|
||
const fetchRegisteredLearners = useCallback(async (args) => { | ||
try { | ||
setIsLoading(true); | ||
const options = { | ||
page: args.pageIndex + 1, // `DataTable` uses zero-indexed array | ||
pageSize: args.pageSize, | ||
}; | ||
const response = await EnterpriseDataApiService.fetchUnenrolledRegisteredLearners(enterpriseId, options); | ||
const data = camelCaseObject(response.data); | ||
|
||
setRegisteredLearners({ | ||
itemCount: data.count, | ||
pageCount: data.numPages ?? Math.floor(data.count / options.pageSize), | ||
results: data.results, | ||
}); | ||
} catch (error) { | ||
logError(error); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}, [enterpriseId]); | ||
|
||
const debouncedFetchRegisteredLearners = useMemo( | ||
() => debounce(fetchRegisteredLearners, 300), | ||
[fetchRegisteredLearners], | ||
); | ||
|
||
return { | ||
isLoading, | ||
registeredLearners, | ||
fetchRegisteredLearners: debouncedFetchRegisteredLearners, | ||
}; | ||
}; | ||
|
||
export default useRegisteredLearners; |
Oops, something went wrong.