Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable Click-to-Search for Non-Searchable Fields in Overview #168

Merged
merged 3 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions src/js/components/BentoAppRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useAppDispatch } from '@/hooks';

import { makeGetConfigRequest, makeGetServiceInfoRequest } from '@/features/config/config.store';
import { makeGetAboutRequest } from '@/features/content/content.store';
import { makeGetDataRequestThunk } from '@/features/data/data.store';
import { makeGetDataRequestThunk, populateClickable } from '@/features/data/data.store';
import { makeGetKatsuPublic, makeGetSearchFields } from '@/features/search/query.store';
import { makeGetProvenanceRequest } from '@/features/provenance/provenance.store';
import { getBeaconConfig } from '@/features/beacon/beaconConfig.store';
Expand All @@ -26,19 +26,31 @@ const BentoAppRouter = () => {
const isAuthenticated = useIsAuthenticated();

useEffect(() => {
dispatch(makeGetConfigRequest()).then(() => dispatch(getBeaconConfig()));
dispatch(makeGetAboutRequest());
dispatch(makeGetDataRequestThunk());
dispatch(makeGetSearchFields());
dispatch(makeGetProvenanceRequest());
dispatch(makeGetKatsuPublic());
dispatch(fetchKatsuData());
dispatch(fetchGohanData());
dispatch(makeGetServiceInfoRequest());
//TODO: Dispatch makeGetDataTypes to get the data types from service-registry
if (isAuthenticated) {
dispatch(makeGetDataTypes());
}
const fetchInitialData = async () => {
try {
await dispatch(makeGetConfigRequest());
await dispatch(getBeaconConfig());
await dispatch(makeGetAboutRequest());

await Promise.all([dispatch(makeGetDataRequestThunk()), dispatch(makeGetSearchFields())]);

dispatch(populateClickable());

await dispatch(makeGetProvenanceRequest());
await dispatch(makeGetKatsuPublic());
await dispatch(fetchKatsuData());
await dispatch(fetchGohanData());
await dispatch(makeGetServiceInfoRequest());
SanjeevLakhwani marked this conversation as resolved.
Show resolved Hide resolved

if (isAuthenticated) {
await dispatch(makeGetDataTypes());
}
} catch (error) {
console.error('Error fetching initial data', error);
}
};

fetchInitialData();
}, [isAuthenticated]);

if (isAutoAuthenticating) {
Expand Down
14 changes: 8 additions & 6 deletions src/js/components/Overview/Chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ import {
ChartConfig,
} from '@/types/chartConfig';

const Chart = memo(({ chartConfig, data, units, id }: ChartProps) => {
const Chart = memo(({ chartConfig, data, units, id, isClickable }: ChartProps) => {
const { t, i18n } = useTranslation();
const navigate = useNavigate();
const translateMap = ({ x, y }: { x: string; y: number }) => ({ x: t(x), y });
const removeMissing = ({ x }: { x: string }) => x !== 'missing';
const barChartOnClickHandler = (d: { payload: { x: string } }) => {
navigate(`/${i18n.language}/search?${id}=${d.payload.x}`);
};
const pieChartOnClickHandler = (d: { name: string }) => {
navigate(`/${i18n.language}/search?${id}=${d.name}`);
};

const { chart_type: type } = chartConfig;

Expand All @@ -34,7 +37,7 @@ const Chart = memo(({ chartConfig, data, units, id }: ChartProps) => {
units={units}
preFilter={removeMissing}
dataMap={translateMap}
onClick={barChartOnClickHandler}
{...(isClickable ? { onClick: barChartOnClickHandler } : {})}
/>
);
case CHART_TYPE_HISTOGRAM:
Expand All @@ -45,7 +48,7 @@ const Chart = memo(({ chartConfig, data, units, id }: ChartProps) => {
data={data}
preFilter={removeMissing}
dataMap={translateMap}
onClick={barChartOnClickHandler}
{...(isClickable ? { onClick: barChartOnClickHandler } : {})}
/>
);
case CHART_TYPE_PIE:
Expand All @@ -55,9 +58,7 @@ const Chart = memo(({ chartConfig, data, units, id }: ChartProps) => {
height={PIE_CHART_HEIGHT}
preFilter={removeMissing}
dataMap={translateMap}
onClick={(d) => {
navigate(`/${i18n.language}/search?${id}=${d.name}`);
}}
{...(isClickable ? { onClick: pieChartOnClickHandler } : {})}
/>
);
case CHART_TYPE_CHOROPLETH: {
Expand Down Expand Up @@ -98,6 +99,7 @@ export interface ChartProps {
data: ChartData[];
units: string;
id: string;
isClickable: boolean;
}

export default Chart;
10 changes: 9 additions & 1 deletion src/js/components/Overview/ChartCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const ChartCard: React.FC<ChartCardProps> = memo(({ section, chart, onRemoveChar
data,
field: { id, description, title, config },
chartConfig,
isSearchable,
} = chart;

const extraOptionsData = [
Expand Down Expand Up @@ -63,7 +64,14 @@ const ChartCard: React.FC<ChartCardProps> = memo(({ section, chart, onRemoveChar
}
>
{data.filter((e) => !(e.x === 'missing')).length !== 0 ? (
<Chart chartConfig={chartConfig} data={data} units={config?.units || ''} id={id} key={id} />
<Chart
chartConfig={chartConfig}
data={data}
units={config?.units || ''}
id={id}
key={id}
isClickable={isSearchable}
/>
) : (
<Row style={ROW_EMPTY_STYLE} justify="center" align="middle">
<CustomEmpty text="No Data" />
Expand Down
8 changes: 6 additions & 2 deletions src/js/components/Overview/PublicOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ const PublicOverview = () => {
const [drawerVisible, setDrawerVisible] = useState(false);
const [aboutContent, setAboutContent] = useState('');

const { isFetchingData: isFetchingOverviewData, sections } = useAppSelector((state) => state.data);
const {
isFetchingData: isFetchingOverviewData,
isContentPopulated,
sections,
} = useAppSelector((state) => state.data);
const { isFetchingAbout, about } = useAppSelector((state) => state.content);

useEffect(() => {
Expand All @@ -49,7 +53,7 @@ const PublicOverview = () => {
saveToLocalStorage(sections);
}, [sections]);

return isFetchingOverviewData ? (
return !isContentPopulated ? (
<Loader />
) : (
<>
Expand Down
22 changes: 21 additions & 1 deletion src/js/features/data/data.store.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { createAsyncThunk, createSlice, PayloadAction } from '@reduxjs/toolkit';

import { makeGetDataRequestThunk } from './makeGetDataRequest.thunk';
import { Sections } from '@/types/data';
import { Counts } from '@/types/overviewResponse';
import { QueryState } from '@/features/search/query.store';

export const populateClickable = createAsyncThunk<string[], void, { state: { query: QueryState } }>(
'data/populateClickable',
async (_, { getState }) => {
return getState()
.query.querySections.flatMap((section) => section.fields)
.map((field) => field.id);
}
);

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

const initialState: DataState = {
isFetchingData: true,
isContentPopulated: false,
defaultLayout: [],
sections: [],
counts: {
Expand Down Expand Up @@ -88,6 +100,14 @@ const data = createSlice({
})
.addCase(makeGetDataRequestThunk.rejected, (state) => {
state.isFetchingData = false;
})
.addCase(populateClickable.fulfilled, (state, { payload }) => {
state.sections.forEach((section) => {
section.charts.forEach((chart) => {
chart.isSearchable = payload.includes(chart.id);
});
});
state.isContentPopulated = true;
});
},
});
Expand Down
1 change: 1 addition & 0 deletions src/js/features/data/makeGetDataRequest.thunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const makeGetDataRequestThunk = createAsyncThunk<
// Initial display state
isDisplayed: i < MAX_CHARTS,
width: chart.width ?? DEFAULT_CHART_WIDTH, // initial configured width; users can change it from here
isSearchable: false,
};
};

Expand Down
2 changes: 1 addition & 1 deletion src/js/features/search/query.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { serializeChartData } from '@/utils/chart';
import { KatsuSearchResponse, SearchFieldResponse } from '@/types/search';
import { ChartData } from '@/types/data';

type QueryState = {
export type QueryState = {
isFetchingFields: boolean;
isFetchingData: boolean;
attemptedFetch: boolean;
Expand Down
1 change: 1 addition & 0 deletions src/js/types/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface ChartDataField {
// display options:
isDisplayed: boolean; // whether the chart is currently displayed (state data)
width: number; // current width (state data); initial data taken from chart config
isSearchable: boolean; // whether the field is searchable
}

export interface ChartData {
Expand Down