Skip to content

Commit

Permalink
Add graphql query for analytical framework details
Browse files Browse the repository at this point in the history
  • Loading branch information
shreeyash07 authored and AdityaKhatri committed May 2, 2024
1 parent 6c477bb commit 3d67775
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 27 deletions.
77 changes: 57 additions & 20 deletions app/components/framework/FrameworkImageButton/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import React, { memo } from 'react';
import { _cs } from '@togglecorp/fujs';
import React, { memo, useMemo } from 'react';
import { gql, useQuery } from '@apollo/client';
import {
_cs,
isNotDefined,
isDefined,
} from '@togglecorp/fujs';
import {
Button,
ButtonProps,
Expand All @@ -10,22 +15,36 @@ import {
Kraken,
} from '@the-deep/deep-ui';

import { useRequest } from '#base/utils/restRequest';
import {
AnalysisFrameworkDetailsQuery,
AnalysisFrameworkDetailsQueryVariables,
} from '#generated/types';

import { useModalState } from '#hooks/stateManagement';
import { AnalyticalFramework } from '#types';
import _ts from '#ts';

import styles from './styles.css';

const query = {
fields: ['title', 'preview_image', 'id'],
};
const ANALYSIS_FRAMEWORK_DETAILS = gql`
query AnalysisFrameworkDetails(
$frameworkId: ID!,
) {
analysisFramework(id: $frameworkId) {
title
id
previewImage {
name
url
}
}
}
`;

export type Props = {
label?: string;
className?: string;
variant?: ButtonProps<string>['variant'];
frameworkId?: number | string;
frameworkId?: string;
image?: string;
}

Expand All @@ -44,15 +63,33 @@ function FrameworkImageButton(props: Props) {
hideModal,
] = useModalState(false);

const analysisFrameworkVariables = useMemo(
(): AnalysisFrameworkDetailsQueryVariables | undefined => (
frameworkId ? { frameworkId } : undefined
),
[frameworkId],
);

const {
pending,
response: frameworkDetails,
} = useRequest<AnalyticalFramework>({
skip: !isModalVisible || !frameworkId,
url: `server://analysis-frameworks/${frameworkId}/`,
query,
method: 'GET',
});
data: analysisFrameworkData,
loading: analysisFrameworkLoading,
} = useQuery<AnalysisFrameworkDetailsQuery, AnalysisFrameworkDetailsQueryVariables>(
ANALYSIS_FRAMEWORK_DETAILS,
{
skip: isNotDefined(analysisFrameworkVariables),
variables: analysisFrameworkVariables,
},
);

const imageSource = useMemo(
() => {
if (isDefined(image)) {
return image;
}
return analysisFrameworkData?.analysisFramework?.previewImage?.url;
},
[analysisFrameworkData, image],
);

return (
<>
Expand All @@ -79,11 +116,11 @@ function FrameworkImageButton(props: Props) {
onCloseButtonClick={hideModal}
bodyClassName={styles.content}
>
{pending && <PendingMessage />}
{(frameworkDetails?.previewImage || image) ? (
{analysisFrameworkLoading && <PendingMessage />}
{imageSource ? (
<ImagePreview
alt={frameworkDetails?.title ?? _ts('projectEdit', 'frameworkReferenceImageAlt')}
src={image ?? frameworkDetails?.previewImage}
alt={analysisFrameworkData?.analysisFramework?.title ?? _ts('projectEdit', 'frameworkReferenceImageAlt')}
src={imageSource}
/>
) : (
<Message
Expand Down
2 changes: 1 addition & 1 deletion app/views/AnalyticalFramework/PrimaryTagging/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import styles from './styles.css';

interface PrimaryTaggingInput<K extends string> {
className?: string;
frameworkId: number | undefined;
frameworkId: string | undefined;

allWidgets: WidgetsType | undefined;

Expand Down
2 changes: 1 addition & 1 deletion app/views/AnalyticalFramework/SecondaryTagging/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import styles from './styles.css';

interface Props<K extends string> {
className?: string;
frameworkId: number | undefined;
frameworkId: string | undefined;

allWidgets: WidgetsType | undefined;

Expand Down
9 changes: 4 additions & 5 deletions app/views/AnalyticalFramework/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,9 @@ function AnalyticalFramework(props: Props) {

const location = useLocation();

const { frameworkId: frameworkIdFromParams } = useParams<{ frameworkId: string }>();
const { frameworkId } = useParams<{ frameworkId: string }>();
const accessPrivateProject = !!user?.accessibleFeatures?.some((f) => f.key === 'PRIVATE_PROJECT');
const frameworkId = !frameworkIdFromParams ? undefined : +frameworkIdFromParams;
const createMode = !frameworkIdFromParams;
const createMode = !frameworkId;

// NOTE: userId should be defined in this page
const userId = user?.id ?? 'NULL';
Expand Down Expand Up @@ -252,9 +251,9 @@ function AnalyticalFramework(props: Props) {

const frameworkQueryVariables = useMemo(
(): CurrentFrameworkQueryVariables => ({
id: frameworkIdFromParams,
id: frameworkId,
}),
[frameworkIdFromParams],
[frameworkId],
);
const {
loading: frameworkQueryLoading,
Expand Down

0 comments on commit 3d67775

Please sign in to comment.