Skip to content

Commit

Permalink
refact: move to RequestStatus for overview data
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlougheed committed Nov 28, 2024
1 parent 5d0d598 commit 3b0c087
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/js/components/BentoAppRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { getGenomes } from '@/features/reference/reference.store';
import Loader from '@/components/Loader';
import DefaultLayout from '@/components/Util/DefaultLayout';
import { BEACON_NETWORK_ENABLED } from '@/config';
import { WAITING_STATES } from '@/constants/requests';
import { RequestStatus } from '@/types/requests';
import { BentoRoute } from '@/types/routes';
import { scopeEqual, validProjectDataset } from '@/utils/router';
Expand All @@ -35,7 +36,7 @@ const ScopedRoute = () => {
const { selectedScope, projects, projectsStatus } = useMetadata();

useEffect(() => {
if ([RequestStatus.Idle, RequestStatus.Pending].includes(projectsStatus)) return; // Wait for projects to load first
if (WAITING_STATES.includes(projectsStatus)) return; // Wait for projects to load first

// Update selectedScope based on URL parameters
const valid = validProjectDataset(projects, { project: projectId, dataset: datasetId });
Expand Down
9 changes: 6 additions & 3 deletions src/js/components/Overview/Counts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { BiDna } from 'react-icons/bi';

import CountsTitleWithHelp from '@/components/Util/CountsTitleWithHelp';
import { BOX_SHADOW, COUNTS_FILL } from '@/constants/overviewConstants';
import { WAITING_STATES } from '@/constants/requests';
import { NO_RESULTS_DASHES } from '@/constants/searchConstants';
import { useAppSelector, useTranslationFn } from '@/hooks';
import { useCanSeeUncensoredCounts } from '@/hooks/censorship';
Expand All @@ -23,7 +24,7 @@ type CountEntry = { entity: BentoEntity; icon: ReactNode; count: number };
const Counts = () => {
const t = useTranslationFn();

const { counts, isFetchingData } = useAppSelector((state) => state.data);
const { counts, status } = useAppSelector((state) => state.data);

const uncensoredCounts = useCanSeeUncensoredCounts();

Expand All @@ -46,18 +47,20 @@ const Counts = () => {
},
];

const waitingForData = WAITING_STATES.includes(status);

return (
<>
<Typography.Title level={3}>{t('Counts')}</Typography.Title>
<Space wrap>
{data.map(({ entity, icon, count }, i) => (
<Card key={i} style={{ ...styles.countCard, height: isFetchingData ? 138 : 114 }}>
<Card key={i} style={{ ...styles.countCard, height: waitingForData ? 138 : 114 }}>
<Statistic
title={<CountsTitleWithHelp entity={entity} />}
value={count || (uncensoredCounts ? count : NO_RESULTS_DASHES)}
valueStyle={{ color: COUNTS_FILL }}
prefix={icon}
loading={isFetchingData}
loading={waitingForData}
/>
</Card>
))}
Expand Down
2 changes: 1 addition & 1 deletion src/js/components/Overview/Drawer/ManageChartsDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const ManageChartsDrawer = ({ onManageDrawerClose, manageDrawerVisible }: Manage

const dispatch = useAppDispatch();

const sections = useAppSelector((state) => state.data.sections);
const { sections } = useAppSelector((state) => state.data);

return (
<Drawer
Expand Down
9 changes: 5 additions & 4 deletions src/js/components/Overview/PublicOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { convertSequenceAndDisplayData, saveValue } from '@/utils/localStorage';
import type { Sections } from '@/types/data';

import { BOX_SHADOW, LOCALSTORAGE_CHARTS_KEY } from '@/constants/overviewConstants';
import { WAITING_STATES } from '@/constants/requests';

import OverviewSection from './OverviewSection';
import ManageChartsDrawer from './Drawer/ManageChartsDrawer';
Expand All @@ -29,17 +30,17 @@ const PublicOverview = () => {
const [drawerVisible, setDrawerVisible] = useState(false);
const [aboutContent, setAboutContent] = useState('');

const { isFetchingData: isFetchingOverviewData, sections } = useAppSelector((state) => state.data);
const { status: overviewDataStatus, sections } = useAppSelector((state) => state.data);
const { status: aboutStatus, about } = useAppSelector((state) => state.content);

const selectedProject = useSelectedProject();
const { scope } = useSelectedScope();

useEffect(() => {
// Save sections to localStorage when they change
if (isFetchingOverviewData) return;
if (overviewDataStatus != RequestStatus.Fulfilled) return;
saveToLocalStorage(sections);
}, [isFetchingOverviewData, sections]);
}, [overviewDataStatus, sections]);

useEffect(() => {
const activeLanguage = i18n.language;
Expand All @@ -58,7 +59,7 @@ const PublicOverview = () => {

const searchableFields = useSearchableFields();

return isFetchingOverviewData ? (
return WAITING_STATES.includes(overviewDataStatus) ? (
<Loader />
) : (
<>
Expand Down
3 changes: 3 additions & 0 deletions src/js/constants/requests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { RequestStatus } from '@/types/requests';

export const WAITING_STATES = [RequestStatus.Idle, RequestStatus.Pending];
11 changes: 6 additions & 5 deletions src/js/features/data/data.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import { createSlice } from '@reduxjs/toolkit';
import { makeGetDataRequestThunk } from './makeGetDataRequest.thunk';
import type { Sections } from '@/types/data';
import type { Counts } from '@/types/overviewResponse';
import { RequestStatus } from '@/types/requests';

interface DataState {
isFetchingData: boolean;
status: RequestStatus;
defaultLayout: Sections;
sections: Sections;
counts: Counts;
}

const initialState: DataState = {
isFetchingData: true,
status: RequestStatus.Idle,
defaultLayout: [],
sections: [],
counts: {
Expand Down Expand Up @@ -79,16 +80,16 @@ const data = createSlice({
extraReducers: (builder) => {
builder
.addCase(makeGetDataRequestThunk.pending, (state) => {
state.isFetchingData = true;
state.status = RequestStatus.Pending;
})
.addCase(makeGetDataRequestThunk.fulfilled, (state, { payload }) => {
state.sections = payload.sectionData;
state.defaultLayout = payload.defaultData;
state.counts = payload.counts;
state.isFetchingData = false;
state.status = RequestStatus.Fulfilled;
})
.addCase(makeGetDataRequestThunk.rejected, (state) => {
state.isFetchingData = false;
state.status = RequestStatus.Rejected;
});
},
});
Expand Down

0 comments on commit 3b0c087

Please sign in to comment.