Skip to content

Commit

Permalink
[C] use questions instead of datasets
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgoff authored and blnkt committed Apr 10, 2024
1 parent ebc64a3 commit 1b6edfe
Show file tree
Hide file tree
Showing 9 changed files with 370 additions and 260 deletions.
22 changes: 7 additions & 15 deletions components/content-blocks/SupernovaDistanceDistribution/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FunctionComponent } from "react";
import isNull from "lodash/isNull";
import { BaseContentBlockProps } from "@/components/shapes";
import { FragmentType, graphql, useFragment } from "@/gql/public-schema";
import SupernovaDistanceDistribution from "@/components/dynamic/SupernovaDistanceDistribution";
Expand All @@ -22,30 +23,20 @@ const Fragment = graphql(`
}
}
step: binSize
userData: dataset {
... on datasets_supernovaGalaxyObservations_Entry {
id: title
distance
lat: galacticLatitude
long: galacticLongitude
}
}
...LightCurveQuestionData
}
`);

const SupernovaDistanceDistributionBlock: FunctionComponent<
BaseContentBlockProps<FragmentType<typeof Fragment>>
> = async ({ data }) => {
const { imageAlbum, json, step, userData, instructions } = useFragment(
Fragment,
data
);
const { imageAlbum, json, step, instructions } = useFragment(Fragment, data);

if (!json || !json[0]) return null;
if (isNull(json) || isNull(json[0])) return null;

const { url } = json[0];

if (!url) return null;
if (isNull(url)) return null;

const response = await fetch(url, {
cache: "force-cache",
Expand All @@ -64,7 +55,8 @@ const SupernovaDistanceDistributionBlock: FunctionComponent<
<SupernovaDistanceDistribution
album={imageAlbum}
instructions={instructions || undefined}
{...{ supernovaData, step, userData }}
questionData={data}
{...{ supernovaData, step }}
/>
</>
);
Expand Down
129 changes: 104 additions & 25 deletions components/dynamic/SupernovaDistanceDistribution/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
"use client";
import { FunctionComponent } from "react";
import { FunctionComponent, useContext } from "react";
import isNull from "lodash/isNull";
import isUndefined from "lodash/isUndefined";
import dynamic from "next/dynamic";
import { FragmentType, graphql, useFragment } from "@/gql/public-schema";
import { useTranslation } from "react-i18next";
import withModal from "@/components/hoc/withModal";
import ModalProps from "@/components/shapes/modal";
import WidgetContainer from "@/components/layout/WidgetContainer";
import Loader from "@/components/page/Loader";
import { WidgetInput } from "@/types/answers";
import { exists, isDefined } from "@/lib/utils";
import StoredAnswersContext from "@/contexts/StoredAnswersContext";
import WidgetContainerWithModal from "@/components/layout/WidgetContainerWithModal";
import { isEntryValid } from "@/helpers/gql";

const SupernovaDistanceDistribution = dynamic(
() =>
Expand All @@ -18,41 +23,118 @@ const SupernovaDistanceDistribution = dynamic(
}
);

interface SupernovaDistanceDistributionContainerProps extends ModalProps {
const Fragment = graphql(`
fragment LightCurveQuestionData on contentBlocks_supernovaDistanceDistribution_BlockType {
questionEntries {
... on questions_default_Entry {
id
questionWidgetsBlock {
... on questionWidgetsBlock_lightCurveBlock_BlockType {
lightCurveTool {
... on widgets_lightCurveTool_Entry {
title
displayName
dataset {
... on datasets_supernovaGalaxyObservations_Entry {
id
title
distance
lat: galacticLatitude
long: galacticLongitude
}
}
}
}
}
}
}
}
}
`);

interface SupernovaDistanceDistributionContainerProps {
step: number;
album: Array<any>;
supernovaData: Array<number>;
userData: Array<any>;
questionData: FragmentType<typeof Fragment>;
instructions?: string;
}

const SupernovaDistanceDistributionContainer: FunctionComponent<
SupernovaDistanceDistributionContainerProps
> = ({
step,
album = [],
supernovaData,
userData = [],
instructions,
openModal,
closeModal,
isOpen,
}) => {
> = ({ step, album = [], supernovaData, questionData, instructions }) => {
const { questionEntries } = useFragment(Fragment, questionData);
const { answers } = useContext(StoredAnswersContext);

const { t } = useTranslation();

const histogramData = supernovaData.map((value, i) => {
return {
bin: i * step,
value,
};
});

const userData = [...questionEntries]
.map((entry) => {
if (!isEntryValid(entry, "questions_default_Entry")) return undefined;

const { questionWidgetsBlock, id } = entry;

if (
isNull(id) ||
isNull(questionWidgetsBlock) ||
!isEntryValid(
questionWidgetsBlock[0],
"questionWidgetsBlock_lightCurveBlock_BlockType"
)
)
return undefined;

const { data } = answers[id] || {};

// if there is no answer, exit
if (isUndefined(data)) return undefined;

const { lightCurveTool } = questionWidgetsBlock[0];

if (
isNull(lightCurveTool) ||
!isEntryValid(lightCurveTool[0], "widgets_lightCurveTool_Entry")
)
return undefined;

const { dataset, title, displayName } = lightCurveTool[0];

const source = dataset.find((source) => {
if (!isEntryValid(source, "datasets_supernovaGalaxyObservations_Entry"))
return false;

return (data as WidgetInput).datasetId === source.id;
});

if (!isEntryValid(source, "datasets_supernovaGalaxyObservations_Entry"))
return undefined;

const { title: datasetName, distance, lat, long } = source;

return {
id: `${displayName || title}: ${datasetName}`,
distance,
lat,
long,
magnitude: 0,
};
})
.filter(exists);

const binnedImages = histogramData.map(({ bin }) => {
const matchedImage = album.find(({ name }: { name: string }) => {
const regex = new RegExp(`${bin}_`);
return name.search(regex) === 0;
});

if (typeof matchedImage === "undefined") {
if (isUndefined(matchedImage)) {
return undefined;
}

Expand All @@ -66,26 +148,23 @@ const SupernovaDistanceDistributionContainer: FunctionComponent<
});

const imageCount = binnedImages.length;
const filteredBinnedImages = binnedImages.filter(
(img): img is { width: number; height: number; url: string } =>
typeof img !== "undefined"
);
const filteredBinnedImages = binnedImages.filter(isDefined);
const imageMismatch = filteredBinnedImages.length !== imageCount;

return (
<WidgetContainer
<WidgetContainerWithModal
title={t("widgets.supernova_three_vector") || undefined}
variant="light"
fillScreen
{...{ isOpen, openModal, closeModal, instructions }}
{...{ instructions }}
>
<SupernovaDistanceDistribution
step={step || 100}
binnedImages={imageMismatch ? [] : filteredBinnedImages}
{...{ histogramData, userData }}
/>
</WidgetContainer>
</WidgetContainerWithModal>
);
};

export default withModal(SupernovaDistanceDistributionContainer);
export default SupernovaDistanceDistributionContainer;
134 changes: 67 additions & 67 deletions gql/educator-schema/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5649,7 +5649,6 @@ export type ContentBlocks_SupernovaDistanceDistribution_BlockType = ElementInter
/** Whether the element is archived. */
archived?: Maybe<Scalars['Boolean']['output']>;
binSize?: Maybe<Scalars['Number']['output']>;
dataset: Array<Maybe<EntryInterface>>;
/** The date the element was created. */
dateCreated?: Maybe<Scalars['DateTime']['output']>;
/** The date the element was last updated. */
Expand All @@ -5669,6 +5668,7 @@ export type ContentBlocks_SupernovaDistanceDistribution_BlockType = ElementInter
level?: Maybe<Scalars['Int']['output']>;
/** The ID of the primary owner of the Neo block. */
primaryOwnerId?: Maybe<Scalars['Int']['output']>;
questionEntries: Array<Maybe<EntryInterface>>;
/** The element’s search score, if the `search` parameter was used when querying for the element. */
searchScore?: Maybe<Scalars['Int']['output']>;
/** The handle of the site the element is associated with. */
Expand Down Expand Up @@ -5704,7 +5704,72 @@ export type ContentBlocks_SupernovaDistanceDistribution_BlockType_CountArgs = {
};


export type ContentBlocks_SupernovaDistanceDistribution_BlockTypeDatasetArgs = {
export type ContentBlocks_SupernovaDistanceDistribution_BlockTypeImageAlbumArgs = {
except?: InputMaybe<Array<InputMaybe<Scalars['Int']['input']>>>;
first?: InputMaybe<Scalars['Boolean']['input']>;
forPage?: InputMaybe<ForPageInput>;
last?: InputMaybe<Scalars['Boolean']['input']>;
nth?: InputMaybe<Scalars['Int']['input']>;
random?: InputMaybe<Scalars['Int']['input']>;
reverse?: InputMaybe<Scalars['Boolean']['input']>;
shuffle?: InputMaybe<Scalars['Int']['input']>;
skip?: InputMaybe<Scalars['Int']['input']>;
sortBy?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
sortByDesc?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
where?: InputMaybe<WhereFiltersInput>;
whereBetween?: InputMaybe<WhereBetweenFiltersInput>;
whereIn?: InputMaybe<WhereInFiltersInput>;
whereNotBetween?: InputMaybe<WhereNotBetweenFiltersInput>;
whereNotIn?: InputMaybe<WhereNotInFiltersInput>;
whereNotNull?: InputMaybe<Scalars['String']['input']>;
whereNull?: InputMaybe<Scalars['String']['input']>;
};


export type ContentBlocks_SupernovaDistanceDistribution_BlockTypeJsonArgs = {
dateCreated?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
dateModified?: InputMaybe<Scalars['String']['input']>;
dateUpdated?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
filename?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
fixedOrder?: InputMaybe<Scalars['Boolean']['input']>;
folderId?: InputMaybe<Array<InputMaybe<Scalars['QueryArgument']['input']>>>;
hasAlt?: InputMaybe<Scalars['Boolean']['input']>;
height?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
id?: InputMaybe<Array<InputMaybe<Scalars['QueryArgument']['input']>>>;
inReverse?: InputMaybe<Scalars['Boolean']['input']>;
includeSubfolders?: InputMaybe<Scalars['Boolean']['input']>;
kind?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
limit?: InputMaybe<Scalars['Int']['input']>;
offset?: InputMaybe<Scalars['Int']['input']>;
orderBy?: InputMaybe<Scalars['String']['input']>;
preferSites?: InputMaybe<Array<InputMaybe<Scalars['QueryArgument']['input']>>>;
ref?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
relatedTo?: InputMaybe<Array<InputMaybe<Scalars['QueryArgument']['input']>>>;
relatedToAll?: InputMaybe<Array<InputMaybe<Scalars['QueryArgument']['input']>>>;
relatedToAssets?: InputMaybe<Array<InputMaybe<AssetCriteriaInput>>>;
relatedToCategories?: InputMaybe<Array<InputMaybe<CategoryCriteriaInput>>>;
relatedToEntries?: InputMaybe<Array<InputMaybe<EntryCriteriaInput>>>;
relatedToTags?: InputMaybe<Array<InputMaybe<TagCriteriaInput>>>;
relatedToUsers?: InputMaybe<Array<InputMaybe<UserCriteriaInput>>>;
search?: InputMaybe<Scalars['String']['input']>;
site?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
siteId?: InputMaybe<Array<InputMaybe<Scalars['QueryArgument']['input']>>>;
siteSettingsId?: InputMaybe<Array<InputMaybe<Scalars['QueryArgument']['input']>>>;
size?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
slug?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
title?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
uid?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
unique?: InputMaybe<Scalars['Boolean']['input']>;
uploader?: InputMaybe<Scalars['QueryArgument']['input']>;
uri?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
volume?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
volumeId?: InputMaybe<Array<InputMaybe<Scalars['QueryArgument']['input']>>>;
width?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
withTransforms?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
};


export type ContentBlocks_SupernovaDistanceDistribution_BlockTypeQuestionEntriesArgs = {
after?: InputMaybe<Scalars['String']['input']>;
ancestorDist?: InputMaybe<Scalars['Int']['input']>;
ancestorOf?: InputMaybe<Scalars['Int']['input']>;
Expand Down Expand Up @@ -5785,71 +5850,6 @@ export type ContentBlocks_SupernovaDistanceDistribution_BlockTypeDatasetArgs = {
yAxisMin?: InputMaybe<Array<InputMaybe<Scalars['QueryArgument']['input']>>>;
};


export type ContentBlocks_SupernovaDistanceDistribution_BlockTypeImageAlbumArgs = {
except?: InputMaybe<Array<InputMaybe<Scalars['Int']['input']>>>;
first?: InputMaybe<Scalars['Boolean']['input']>;
forPage?: InputMaybe<ForPageInput>;
last?: InputMaybe<Scalars['Boolean']['input']>;
nth?: InputMaybe<Scalars['Int']['input']>;
random?: InputMaybe<Scalars['Int']['input']>;
reverse?: InputMaybe<Scalars['Boolean']['input']>;
shuffle?: InputMaybe<Scalars['Int']['input']>;
skip?: InputMaybe<Scalars['Int']['input']>;
sortBy?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
sortByDesc?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
where?: InputMaybe<WhereFiltersInput>;
whereBetween?: InputMaybe<WhereBetweenFiltersInput>;
whereIn?: InputMaybe<WhereInFiltersInput>;
whereNotBetween?: InputMaybe<WhereNotBetweenFiltersInput>;
whereNotIn?: InputMaybe<WhereNotInFiltersInput>;
whereNotNull?: InputMaybe<Scalars['String']['input']>;
whereNull?: InputMaybe<Scalars['String']['input']>;
};


export type ContentBlocks_SupernovaDistanceDistribution_BlockTypeJsonArgs = {
dateCreated?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
dateModified?: InputMaybe<Scalars['String']['input']>;
dateUpdated?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
filename?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
fixedOrder?: InputMaybe<Scalars['Boolean']['input']>;
folderId?: InputMaybe<Array<InputMaybe<Scalars['QueryArgument']['input']>>>;
hasAlt?: InputMaybe<Scalars['Boolean']['input']>;
height?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
id?: InputMaybe<Array<InputMaybe<Scalars['QueryArgument']['input']>>>;
inReverse?: InputMaybe<Scalars['Boolean']['input']>;
includeSubfolders?: InputMaybe<Scalars['Boolean']['input']>;
kind?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
limit?: InputMaybe<Scalars['Int']['input']>;
offset?: InputMaybe<Scalars['Int']['input']>;
orderBy?: InputMaybe<Scalars['String']['input']>;
preferSites?: InputMaybe<Array<InputMaybe<Scalars['QueryArgument']['input']>>>;
ref?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
relatedTo?: InputMaybe<Array<InputMaybe<Scalars['QueryArgument']['input']>>>;
relatedToAll?: InputMaybe<Array<InputMaybe<Scalars['QueryArgument']['input']>>>;
relatedToAssets?: InputMaybe<Array<InputMaybe<AssetCriteriaInput>>>;
relatedToCategories?: InputMaybe<Array<InputMaybe<CategoryCriteriaInput>>>;
relatedToEntries?: InputMaybe<Array<InputMaybe<EntryCriteriaInput>>>;
relatedToTags?: InputMaybe<Array<InputMaybe<TagCriteriaInput>>>;
relatedToUsers?: InputMaybe<Array<InputMaybe<UserCriteriaInput>>>;
search?: InputMaybe<Scalars['String']['input']>;
site?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
siteId?: InputMaybe<Array<InputMaybe<Scalars['QueryArgument']['input']>>>;
siteSettingsId?: InputMaybe<Array<InputMaybe<Scalars['QueryArgument']['input']>>>;
size?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
slug?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
title?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
uid?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
unique?: InputMaybe<Scalars['Boolean']['input']>;
uploader?: InputMaybe<Scalars['QueryArgument']['input']>;
uri?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
volume?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
volumeId?: InputMaybe<Array<InputMaybe<Scalars['QueryArgument']['input']>>>;
width?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
withTransforms?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
};

export type ContentBlocks_Table_BlockType = ElementInterface & NeoBlockInterface & {
__typename?: 'contentBlocks_table_BlockType';
/** Return a number of related elements for a field. */
Expand Down
Loading

0 comments on commit 1b6edfe

Please sign in to comment.