Skip to content

Commit

Permalink
Merge pull request #241 from bento-platform/features/beacon-ui-scoping
Browse files Browse the repository at this point in the history
add scoping to beacon UI
  • Loading branch information
gsfk authored Jan 24, 2025
2 parents 5d1f12e + 7b65cc5 commit 283d1c8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
24 changes: 18 additions & 6 deletions src/js/components/Beacon/BeaconCommon/BeaconQueryFormUi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useCallback, useEffect, useMemo, useState } from 'react';
import { Button, Card, Col, Form, Row } from 'antd';
import { useIsAuthenticated } from 'bento-auth-js';
import { useAppDispatch, useQueryWithAuthIfAllowed, useTranslationFn } from '@/hooks';
import { useSelectedScope } from '@/features/metadata/hooks';
import VariantsForm from './VariantsForm';
import Filters from './Filters';
import SearchToolTip from './ToolTips/SearchToolTip';
Expand Down Expand Up @@ -55,6 +56,8 @@ const BeaconQueryFormUi = ({
const dispatch = useAppDispatch();
const isAuthenticated = useIsAuthenticated();

const { scope, scopeSet } = useSelectedScope();

const formInitialValues = useMemo(
() => ({
'Assembly ID': beaconAssemblyIds.length === 1 && beaconAssemblyIds[0],
Expand All @@ -67,12 +70,18 @@ const BeaconQueryFormUi = ({
const showError = hasError && !errorAlertClosed;

const requestPayload = useCallback(
(query: PayloadVariantsQuery, payloadFilters: PayloadFilter[]): BeaconQueryPayload => ({
meta: { apiVersion: '2.0.0' },
query: { requestParameters: { g_variant: query }, filters: payloadFilters },
bento: { showSummaryStatistics: true },
}),
[]
(query: PayloadVariantsQuery, payloadFilters: PayloadFilter[]): BeaconQueryPayload => {
const payload: BeaconQueryPayload = {
meta: { apiVersion: '2.0.0' },
query: { requestParameters: { g_variant: query }, filters: payloadFilters },
bento: { showSummaryStatistics: true },
};
if (scope.dataset) {
payload['query']['datasets'] = { datasetIds: [scope.dataset] };
}
return payload;
},
[scope]
);

const launchEmptyQuery = useCallback(
Expand All @@ -90,6 +99,9 @@ const BeaconQueryFormUi = ({
};

useEffect(() => {
if (!scopeSet) {
return;
}
launchEmptyQuery();
setHasVariants(beaconAssemblyIds.length > 0 || isNetworkQuery);

Expand Down
8 changes: 5 additions & 3 deletions src/js/features/beacon/beacon.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import axios from 'axios';
import { makeAuthorizationHeader } from 'bento-auth-js';

import { EMPTY_DISCOVERY_RESULTS } from '@/constants/searchConstants';
import { BEACON_INDIVIDUALS_ENDPOINT, BEACON_INFO_ENDPOINT } from '@/features/beacon/constants';
import { BEACON_INFO_ENDPOINT } from '@/features/beacon/constants';
import type { RootState } from '@/store';
import type { BeaconConfigResponse, BeaconAssemblyIds, BeaconQueryResponse, BeaconQueryPayload } from '@/types/beacon';
import type { DiscoveryResults } from '@/types/data';
import { beaconApiError, errorMsgOrDefault } from '@/utils/beaconApiError';
import { printAPIError } from '@/utils/error.util';

import { extractBeaconDiscoveryOverview } from './utils';
import { extractBeaconDiscoveryOverview, scopedBeaconIndividualsUrl } from './utils';

// config response is not scoped
export const getBeaconConfig = createAsyncThunk<BeaconConfigResponse, void, { state: RootState; rejectValue: string }>(
'beacon/getBeaconConfig',
(_, { rejectWithValue }) => {
Expand All @@ -33,9 +34,10 @@ export const makeBeaconQuery = createAsyncThunk<
{ state: RootState; rejectValue: string }
>('beacon/makeBeaconQuery', async (payload, { getState, rejectWithValue }) => {
const token = getState().auth.accessToken;
const projectId = getState().metadata.selectedScope.scope.project;
const headers = makeAuthorizationHeader(token);
return axios
.post(BEACON_INDIVIDUALS_ENDPOINT, payload, { headers: headers as Record<string, string> })
.post(scopedBeaconIndividualsUrl(projectId), payload, { headers: headers as Record<string, string> })
.then((res) => res.data)
.catch(beaconApiError(rejectWithValue));
});
Expand Down
9 changes: 8 additions & 1 deletion src/js/features/beacon/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { BEACON_NETWORK_URL } from '@/config';
import { BEACON_URL, BEACON_NETWORK_URL } from '@/config';
import { BEACON_INDIVIDUALS_PATH } from './constants';
import type { BeaconAssemblyIds, BeaconNetworkResponses, BeaconQueryResponse } from '@/types/beacon';
import type { ChartData, DiscoveryResults, OptionalDiscoveryResults } from '@/types/data';
import type { NetworkBeacon } from '@/types/beaconNetwork';
import type { Project } from '@/types/metadata';
import { serializeChartData } from '@/utils/chart';

type TempChartObject = Record<string, number>;
Expand Down Expand Up @@ -84,3 +86,8 @@ export const extractBeaconDiscoveryOverview = (response: BeaconQueryResponse): O

export const atLeastOneNetworkResponseIsPending = (responses: BeaconNetworkResponses) =>
Object.values(responses).some((r) => r.isFetchingQueryResponse);

export const scopedBeaconIndividualsUrl = (projectId: Project['identifier'] | undefined): string => {
const projectPath: string = projectId ? '/' + projectId : '';
return BEACON_URL + projectPath + BEACON_INDIVIDUALS_PATH;
};
7 changes: 6 additions & 1 deletion src/js/types/beacon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Datum } from '@/types/overviewResponse';
import type { makeBeaconQuery } from '@/features/beacon/beacon.store';
import type { beaconNetworkQuery } from '@/features/beacon/network.store';
import type { OptionalDiscoveryResults } from '@/types/data';
import type { Dataset } from '@/types/metadata';

// ----------------------------
// form handling
Expand Down Expand Up @@ -72,7 +73,11 @@ export type PayloadVariantsQuery =

export interface BeaconQueryPayload {
meta: { apiVersion: string };
query: { requestParameters: { g_variant: PayloadVariantsQuery }; filters: PayloadFilter[] };
query: {
requestParameters: { g_variant: PayloadVariantsQuery };
filters: PayloadFilter[];
datasets?: { datasetIds: [Dataset['identifier']] };
};
bento?: { showSummaryStatistics: boolean };
}

Expand Down

0 comments on commit 283d1c8

Please sign in to comment.