diff --git a/app/src/contexts/telemetryContext.tsx b/app/src/contexts/telemetryContext.tsx deleted file mode 100644 index f4dc592c40..0000000000 --- a/app/src/contexts/telemetryContext.tsx +++ /dev/null @@ -1,48 +0,0 @@ -import { SurveyContext } from 'contexts/surveyContext'; -import { useBiohubApi } from 'hooks/useBioHubApi'; -import useDataLoader, { DataLoader } from 'hooks/useDataLoader'; -import { GetSurveyTelemetryResponse } from 'interfaces/useTelemetryApi.interface'; -import { GetSurveyDeploymentsResponse } from 'interfaces/useTelemetryDeploymentApi.interface'; - -import { createContext, PropsWithChildren, useContext } from 'react'; -import { ApiPaginationRequestOptions } from 'types/misc'; - -/** - * Context object that stores information about survey telemetry - * - * @export - * @interface ITelemetryContext - */ -export type ITelemetryContext = { - /** - * Data Loader used for retrieving survey deployments records. - */ - deploymentDataLoader: DataLoader<[pagination?: ApiPaginationRequestOptions], GetSurveyDeploymentsResponse, unknown>; - /** - * Data Loader used for retrieving survey telemetry records. - */ - telemetryDataLoader: DataLoader<[pagination?: ApiPaginationRequestOptions], GetSurveyTelemetryResponse, unknown>; -}; - -export const TelemetryContext = createContext(undefined); - -export const TelemetryContextProvider = (props: PropsWithChildren) => { - const { projectId, surveyId } = useContext(SurveyContext); - - const biohubApi = useBiohubApi(); - - const deploymentDataLoader = useDataLoader((pagination?: ApiPaginationRequestOptions) => - biohubApi.telemetryDeployment.getDeploymentsInSurvey(projectId, surveyId, pagination) - ); - - const telemetryDataLoader = useDataLoader((pagination?: ApiPaginationRequestOptions) => - biohubApi.telemetry.getTelemetryForSurvey(projectId, surveyId, pagination) - ); - - const telemetryContext: ITelemetryContext = { - deploymentDataLoader, - telemetryDataLoader - }; - - return {props.children}; -}; diff --git a/app/src/contexts/telemetryTableContext.tsx b/app/src/contexts/telemetryTableContext.tsx index b521263776..4ad8ec81ce 100644 --- a/app/src/contexts/telemetryTableContext.tsx +++ b/app/src/contexts/telemetryTableContext.tsx @@ -17,10 +17,12 @@ import { SIMS_TELEMETRY_HIDDEN_COLUMNS } from 'constants/session-storage'; import { default as dayjs } from 'dayjs'; import { APIError } from 'hooks/api/useAxios'; import { useBiohubApi } from 'hooks/useBioHubApi'; -import { useDialogContext, useSurveyContext, useTelemetryContext } from 'hooks/useContext'; +import { useDialogContext, useSurveyContext } from 'hooks/useContext'; +import useDataLoader from 'hooks/useDataLoader'; import { usePersistentState } from 'hooks/usePersistentState'; import { GetSurveyTelemetryResponse } from 'interfaces/useTelemetryApi.interface'; import { createContext, PropsWithChildren, useCallback, useEffect, useMemo, useRef, useState } from 'react'; +import { ApiPaginationRequestOptions } from 'types/misc'; import { firstOrNull } from 'utils/Utils'; import { v4 as uuidv4 } from 'uuid'; import { RowValidationError, TableValidationModel } from '../components/data-grid/DataGridValidationAlert'; @@ -178,9 +180,15 @@ export const TelemetryTableContextProvider = (props: IAllTelemetryTableContextPr const surveyContext = useSurveyContext(); const dialogContext = useDialogContext(); - const { - telemetryDataLoader: { data: telemetryData, isLoading: isLoadingTelemetryData, refresh: refreshTelemetryData } - } = useTelemetryContext(); + const telemetryDataLoader = useDataLoader((pagination?: ApiPaginationRequestOptions) => + biohubApi.telemetry.getTelemetryForSurvey(surveyContext.projectId, surveyContext.surveyId, pagination) + ); + + useEffect(() => { + telemetryDataLoader.load(); + }, [telemetryDataLoader]); + + const { data: telemetryData, isLoading: isLoadingTelemetryData, refresh: refreshTelemetryData } = telemetryDataLoader; // The data grid rows const [rows, setRows] = useState([]); diff --git a/app/src/features/surveys/SurveyRouter.tsx b/app/src/features/surveys/SurveyRouter.tsx index 99cb7b9a94..ef2e9716e3 100644 --- a/app/src/features/surveys/SurveyRouter.tsx +++ b/app/src/features/surveys/SurveyRouter.tsx @@ -3,7 +3,6 @@ import { PROJECT_PERMISSION, SYSTEM_ROLE } from 'constants/roles'; import { AnimalPageContextProvider } from 'contexts/animalPageContext'; import { DialogContextProvider } from 'contexts/dialogContext'; import { ObservationsContextProvider } from 'contexts/observationsContext'; -import { TelemetryContextProvider } from 'contexts/telemetryContext'; import { AnimalRouter } from 'features/surveys/animals/AnimalRouter'; import EditSurveyPage from 'features/surveys/edit/EditSurveyPage'; import { SurveyObservationPage } from 'features/surveys/observations/SurveyObservationPage'; @@ -70,9 +69,7 @@ const SurveyRouter: React.FC = () => { PROJECT_PERMISSION.OBSERVER ]} validSystemRoles={[SYSTEM_ROLE.SYSTEM_ADMIN, SYSTEM_ROLE.DATA_ADMINISTRATOR]}> - - - + diff --git a/app/src/features/surveys/telemetry/list/SurveyDeploymentList.tsx b/app/src/features/surveys/telemetry/list/SurveyDeploymentList.tsx index fb12ae3f97..c19b95e09c 100644 --- a/app/src/features/surveys/telemetry/list/SurveyDeploymentList.tsx +++ b/app/src/features/surveys/telemetry/list/SurveyDeploymentList.tsx @@ -19,9 +19,11 @@ import { LoadingGuard } from 'components/loading/LoadingGuard'; import { SkeletonList } from 'components/loading/SkeletonLoaders'; import { SurveyDeploymentListItem } from 'features/surveys/telemetry/list/SurveyDeploymentListItem'; import { useBiohubApi } from 'hooks/useBioHubApi'; -import { useCodesContext, useDialogContext, useSurveyContext, useTelemetryContext } from 'hooks/useContext'; +import { useCodesContext, useDialogContext, useSurveyContext } from 'hooks/useContext'; +import useDataLoader from 'hooks/useDataLoader'; import { useEffect, useState } from 'react'; import { Link as RouterLink } from 'react-router-dom'; +import { ApiPaginationRequestOptions } from 'types/misc'; /** * Renders a list of all deployments in the survey @@ -32,18 +34,19 @@ export const SurveyDeploymentList = () => { const dialogContext = useDialogContext(); const codesContext = useCodesContext(); const surveyContext = useSurveyContext(); - const telemetryContext = useTelemetryContext(); const biohubApi = useBiohubApi(); - const deploymentDataLoader = telemetryContext.deploymentDataLoader; - const [bulkDeploymentAnchorEl, setBulkDeploymentAnchorEl] = useState(null); const [deploymentAnchorEl, setDeploymentAnchorEl] = useState(null); const [checkboxSelectedIds, setCheckboxSelectedIds] = useState([]); const [selectedDeploymentId, setSelectedDeploymentId] = useState(); + const deploymentDataLoader = useDataLoader((pagination?: ApiPaginationRequestOptions) => + biohubApi.telemetryDeployment.getDeploymentsInSurvey(surveyContext.projectId, surveyContext.surveyId, pagination) + ); + const deployments = deploymentDataLoader.data?.deployments ?? []; const deploymentsCount = deploymentDataLoader.data?.count ?? 0; diff --git a/app/src/hooks/useContext.tsx b/app/src/hooks/useContext.tsx index 64b90c8054..d371a965b9 100644 --- a/app/src/hooks/useContext.tsx +++ b/app/src/hooks/useContext.tsx @@ -8,7 +8,6 @@ import { IObservationsTableContext, ObservationsTableContext } from 'contexts/ob import { IProjectContext, ProjectContext } from 'contexts/projectContext'; import { ISurveyContext, SurveyContext } from 'contexts/surveyContext'; import { ITaxonomyContext, TaxonomyContext } from 'contexts/taxonomyContext'; -import { ITelemetryContext, TelemetryContext } from 'contexts/telemetryContext'; import { IAllTelemetryTableContext, TelemetryTableContext } from 'contexts/telemetryTableContext'; import { useContext } from 'react'; @@ -198,20 +197,3 @@ export const useAnimalPageContext = (): IAnimalPageContext => { return context; }; - -/** - * Returns an instance of `ITelemetryContext` from `TelemetryContext`. - * - * @return {*} {ITelemetryContext} - */ -export const useTelemetryContext = (): ITelemetryContext => { - const context = useContext(TelemetryContext); - - if (!context) { - throw Error( - 'ObservationsContext is undefined, please verify you are calling useObservationsContext() as child of an component.' - ); - } - - return context; -};