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

first pass at adding scoping to beacon UI #241

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 15 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 } = 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['datasets'] = { datasetIds: [scope.dataset] };
}
return payload;
},
[scope]
);

const launchEmptyQuery = useCallback(
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;
};
2 changes: 2 additions & 0 deletions 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 @@ -74,6 +75,7 @@ export interface BeaconQueryPayload {
meta: { apiVersion: string };
query: { requestParameters: { g_variant: PayloadVariantsQuery }; filters: PayloadFilter[] };
bento?: { showSummaryStatistics: boolean };
datasets?: { datasetIds: Dataset['identifier'][] };
}

export type BeaconQueryAction = ActionCreator<
Expand Down
Loading