Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
} from "@ndla/primitives";
import { styled } from "@ndla/styled-system/jsx";
import { MultiSearchSummaryDTO } from "@ndla/types-backend/search-api";
import { ResourceType } from "@ndla/types-backend/taxonomy-api";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { TFunction } from "i18next";
import { useMemo, useState } from "react";
Expand All @@ -45,6 +44,7 @@ import { nodeQueryKeys } from "../../../modules/nodes/nodeQueries";
import { postSearch } from "../../../modules/search/searchApi";
import { useSearch } from "../../../modules/search/searchQueries";
import { createResourceResourceType } from "../../../modules/taxonomy";
import { useAllResourceTypes } from "../../../modules/taxonomy/resourcetypes/resourceTypesQueries";
import { resolveUrls } from "../../../modules/taxonomy/taxonomyApi";
import handleError from "../../../util/handleError";
import { isValidContextId } from "../../../util/urlHelpers";
Expand Down Expand Up @@ -88,7 +88,6 @@ const StyledFieldRoot = styled(FieldRoot, {

interface Props {
onClose: () => void;
resourceTypes?: ResourceType[];
nodeId: string;
existingResourceIds: string[];
type: Exclude<ResourceGroup, "link">;
Expand Down Expand Up @@ -215,7 +214,7 @@ const doPastedSearch = async ({ input, type, t, taxonomyVersion, language }: Pas
return res.results[0];
};

const AddExistingResource = ({ onClose, resourceTypes, existingResourceIds, nodeId, type }: Props) => {
const AddExistingResource = ({ onClose, existingResourceIds, nodeId, type }: Props) => {
const { t, i18n } = useTranslation();
const { query, delayedQuery, setQuery, page, setPage } = usePaginatedQuery();
const [error, setError] = useState("");
Expand All @@ -227,6 +226,8 @@ const AddExistingResource = ({ onClose, resourceTypes, existingResourceIds, node
const typeTocheckFor = type === "learningpath" ? "learningpath" : "article";
const compKey = nodeQueryKeys.childNodes({ id: nodeId, language: i18n.language });

const { data: resourceTypes } = useAllResourceTypes({ language: i18n.language, taxonomyVersion });

const alreadyExists = useMemo(() => {
if (!preview) return false;
return existingResourceIds.includes(preview.context?.publicId ?? "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
TabsRoot,
TabsTrigger,
} from "@ndla/primitives";
import { Node, ResourceType } from "@ndla/types-backend/taxonomy-api";
import { Node } from "@ndla/types-backend/taxonomy-api";
import { useTranslation } from "react-i18next";
import { DialogCloseButton } from "../../../components/DialogCloseButton";
import AddExistingResource from "../plannedResource/AddExistingResource";
Expand All @@ -26,13 +26,12 @@ import { ResourceGroup } from "../utils";

interface Props {
currentNode: Node;
resourceTypes: ResourceType[];
existingResourceIds: string[];
supplementary?: boolean;
type: Exclude<ResourceGroup, "link">;
}

export const PlannedResourceDialogContent = ({ currentNode, resourceTypes, existingResourceIds, type }: Props) => {
export const PlannedResourceDialogContent = ({ currentNode, existingResourceIds, type }: Props) => {
const { t } = useTranslation();
const { setOpen } = useDialogContext();
return (
Expand All @@ -59,7 +58,6 @@ export const PlannedResourceDialogContent = ({ currentNode, resourceTypes, exist
<TabsContent value="get-existing-resource">
<AddExistingResource
type={type}
resourceTypes={resourceTypes}
nodeId={currentNode.id}
onClose={() => setOpen(false)}
existingResourceIds={existingResourceIds}
Expand Down
6 changes: 3 additions & 3 deletions src/containers/StructurePage/resourceComponents/Resource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ interface Props {
invalidate: () => void;
type: ResourceGroup;
index: number;
isUngrouped?: boolean;
numbered?: boolean;
}

const Resource = ({
Expand All @@ -173,7 +173,7 @@ const Resource = ({
contentMeta,
type,
index,
isUngrouped,
numbered,
}: Props) => {
const { t, i18n } = useTranslation();
const { taxonomyVersion } = useTaxonomyVersion();
Expand Down Expand Up @@ -213,7 +213,7 @@ const Resource = ({

return (
<StyledListItemRoot ref={ref}>
{!!isUngrouped && (
{!!numbered && (
<IndicatorWrapper>
<Indicator>{index + 1}</Indicator>
<IndicatorTrack />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { AddLine, Draggable } from "@ndla/icons";
import { Button, DialogContent, DialogRoot, DialogTrigger, Heading, Text } from "@ndla/primitives";
import { styled } from "@ndla/styled-system/jsx";
import { MultiSearchSummaryDTO } from "@ndla/types-backend/search-api";
import { Node, NodeChild, ResourceType } from "@ndla/types-backend/taxonomy-api";
import { Node, NodeChild } from "@ndla/types-backend/taxonomy-api";
import { sortBy } from "@ndla/util";
import { useQueryClient } from "@tanstack/react-query";
import { useTranslation } from "react-i18next";
Expand Down Expand Up @@ -70,12 +70,11 @@ interface Props {
description?: string;
resources: NodeChild[];
currentNode: Node;
resourceTypes?: ResourceType[];
contentMetas: Dictionary<MultiSearchSummaryDTO>;
nodeResourcesIsPending: boolean;
users?: Dictionary<Auth0UserData>;
existingResourceIds: string[];
isUngrouped?: boolean;
numbered?: boolean;
hideAddButton?: boolean;
}

Expand All @@ -90,9 +89,8 @@ const ResourceItems = ({
type,
title,
description,
resourceTypes,
existingResourceIds,
isUngrouped,
numbered,
hideAddButton,
}: Props) => {
const { setCurrentNode } = useCurrentNode();
Expand Down Expand Up @@ -196,7 +194,6 @@ const ResourceItems = ({
) : (
<PlannedResourceDialogContent
currentNode={currentNode}
resourceTypes={resourceTypes ?? []}
existingResourceIds={existingResourceIds}
type={type}
/>
Expand Down Expand Up @@ -229,7 +226,7 @@ const ResourceItems = ({
nodeResourcesIsPending={nodeResourcesIsPending}
invalidate={onDeleted}
index={idx}
isUngrouped={isUngrouped}
numbered={numbered}
/>
)}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { Spinner } from "@ndla/primitives";
import { styled } from "@ndla/styled-system/jsx";
import { MultiSearchSummaryDTO } from "@ndla/types-backend/search-api";
import { NodeChild, ResourceType } from "@ndla/types-backend/taxonomy-api";
import { NodeChild } from "@ndla/types-backend/taxonomy-api";
import { useMemo } from "react";
import { useTranslation } from "react-i18next";
import { Auth0UserData, Dictionary } from "../../../interfaces";
Expand All @@ -36,48 +36,32 @@ const ResourceWrapper = styled("div", {

interface Props {
nodeResources: NodeChild[];
resourceTypes: ResourceType[];
currentNode: NodeChild;
contentMetas: Dictionary<MultiSearchSummaryDTO>;
isUngrouped: boolean;
numbered: boolean;
hasSubTopics: boolean;
nodeResourcesIsPending: boolean;
users: Dictionary<Auth0UserData> | undefined;
}

const ResourcesContainer = ({
resourceTypes,
nodeResources,
currentNode,
contentMetas,
isUngrouped,
numbered,
hasSubTopics,
nodeResourcesIsPending,
users,
}: Props) => {
const { t } = useTranslation();
const resourceTypesWithoutMissing = useMemo(
() =>
resourceTypes
.filter((rt) => rt.id !== "missing")
.map((rt) => ({
...rt,
subtypes: undefined,
})),
[resourceTypes],
);
const { taxonomyVersion } = useTaxonomyVersion();

const { data } = useNodes(
{ contentURI: currentNode.contentUri, taxonomyVersion, includeContexts: true, filterProgrammes: true },
{ enabled: !!currentNode.contentUri },
);

const { coreArticles, supplementaryArticles, learningpaths } = partitionResources(
nodeResources ?? [],
resourceTypes ?? [],
isUngrouped,
);
const { coreArticles, supplementaryArticles, learningpaths } = partitionResources(nodeResources ?? []);

const paths = useMemo(() => data?.map((d) => d.path ?? "").filter((d) => !!d) ?? [], [data]);
const currentMeta = currentNode.contentUri ? contentMetas[currentNode.contentUri] : undefined;
Expand All @@ -102,20 +86,18 @@ const ResourcesContainer = ({
type="core"
title={t("taxonomy.core.title")}
resources={coreArticles}
resourceTypes={resourceTypesWithoutMissing}
currentNode={currentNode}
contentMetas={contentMetas}
nodeResourcesIsPending={nodeResourcesIsPending}
existingResourceIds={nodeResources.map((r) => r.id)}
users={users}
isUngrouped={isUngrouped}
numbered={numbered}
hideAddButton={hasSubTopics}
/>
<ResourceItems
type="learningpath"
title={t("taxonomy.learningpath.title")}
resources={learningpaths}
resourceTypes={resourceTypesWithoutMissing}
currentNode={currentNode}
contentMetas={contentMetas}
nodeResourcesIsPending={nodeResourcesIsPending}
Expand All @@ -128,7 +110,6 @@ const ResourcesContainer = ({
title={t("taxonomy.supplementary.title")}
description={t("taxonomy.supplementary.description")}
resources={supplementaryArticles}
resourceTypes={resourceTypesWithoutMissing}
currentNode={currentNode}
contentMetas={contentMetas}
nodeResourcesIsPending={nodeResourcesIsPending}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
*
*/

import { NodeChild, ResourceType } from "@ndla/types-backend/taxonomy-api";
import { NodeChild } from "@ndla/types-backend/taxonomy-api";
import { keyBy, partition } from "@ndla/util";
import { TFunction } from "i18next";
import { memo, useMemo } from "react";
import { useTranslation } from "react-i18next";
import { Auth0UserData, Dictionary } from "../../../interfaces";
import { useChildNodes, useNodeResourceMetas } from "../../../modules/nodes/nodeQueries";
import { useAllResourceTypes } from "../../../modules/taxonomy/resourcetypes/resourceTypesQueries";
import { getContentUriFromSearchSummary } from "../../../util/searchHelpers";
import { useTaxonomyVersion } from "../../StructureVersion/TaxonomyVersionProvider";
import ResourcesContainer from "./ResourcesContainer";
Expand All @@ -23,15 +22,6 @@ interface Props {
users: Dictionary<Auth0UserData> | undefined;
}

const getMissingResourceType = (t: TFunction): ResourceType & { disabled?: boolean } => ({
id: "missing",
name: t("taxonomy.missingResourceType"),
disabled: true,
supportedLanguages: [],
translations: [],
subtypes: [],
});

const withMissing = (r: NodeChild, t: TFunction): NodeChild => ({
...r,
resourceTypes: [
Expand All @@ -49,7 +39,7 @@ const withMissing = (r: NodeChild, t: TFunction): NodeChild => ({
const StructureResources = ({ currentChildNode, users }: Props) => {
const { t, i18n } = useTranslation();
const { taxonomyVersion } = useTaxonomyVersion();
const isUngrouped = currentChildNode?.metadata?.customFields["topic-resources"] === "ungrouped";
const numbered = currentChildNode?.metadata?.customFields["numbered"] === "true";

const { data: nodeChildren, isPending: nodeResourcesIsPending } = useChildNodes(
{
Expand Down Expand Up @@ -86,22 +76,14 @@ const StructureResources = ({ currentChildNode, users }: Props) => {
[nodeResourceMetas],
);

const { data: resourceTypes } = useAllResourceTypes(
{ language: i18n.language, taxonomyVersion },
{
select: (resourceTypes) => resourceTypes.concat(getMissingResourceType(t)),
},
);

const hasSubTopics = nodeTopics?.length > 0 || false;

return (
<ResourcesContainer
nodeResources={nodeResources ?? []}
resourceTypes={resourceTypes ?? []}
currentNode={currentChildNode}
contentMetas={keyedMetas}
isUngrouped={isUngrouped}
numbered={numbered}
hasSubTopics={hasSubTopics}
nodeResourcesIsPending={contentMetaIsPending || nodeResourcesIsPending}
users={users}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/taxonomy/resourcetypes/resourceTypesQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const useResourceType = (params: UseResourceTypeParams, options?: Partial
interface UseAllResourceTypesParams extends WithTaxonomyVersion {
language: string;
}
export const useAllResourceTypes = <ReturnType>(
export const useAllResourceTypes = <ReturnType = ResourceType[]>(
params: UseAllResourceTypesParams,
options?: Partial<UseQueryOptions<ResourceType[], unknown, ReturnType>>,
) =>
Expand Down
26 changes: 5 additions & 21 deletions src/util/taxonomyHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
*/

import { NodeChild, ResourceType } from "@ndla/types-backend/taxonomy-api";
import { NodeChild } from "@ndla/types-backend/taxonomy-api";
import { partition, sortBy, uniqBy } from "@ndla/util";
import { RESOURCE_FILTER_SUPPLEMENTARY, RESOURCE_TYPE_LEARNING_PATH } from "../constants";
import { ContentUriInfo } from "../interfaces";
Expand All @@ -21,21 +21,9 @@ export const getContentUriInfo = (urn?: string): ContentUriInfo | undefined => {
};

type ResourceLike = Pick<NodeChild, "id" | "resourceTypes" | "rank" | "relevanceId">;
export const sortResources = <T extends ResourceLike>(
resources: T[],
resourceTypes: ResourceType[],
grouped?: boolean,
) => {
const sortResources = <T extends ResourceLike>(resources: T[]) => {
const uniq = uniqBy(resources, (res) => res.id);
const sortedByRank = sortBy(uniq, (res) => res.rank ?? res.id);
if (!grouped) {
return sortedByRank;
}
const resourceTypeOrder = resourceTypes.reduce<Record<string, number>>((order, rt, index) => {
order[rt.id] = index;
return order;
}, {});
return sortBy(uniq, (res) => resourceTypeOrder[res.resourceTypes?.[0]?.id ?? ""] ?? Number.MAX_SAFE_INTEGER);
return sortBy(uniq, (res) => res.rank ?? res.id);
};

interface PartitionedResources<T> {
Expand All @@ -44,12 +32,8 @@ interface PartitionedResources<T> {
coreArticles: T[];
}

export const partitionResources = (
resources: NodeChild[],
resourceTypes: ResourceType[],
ungrouped: boolean,
): PartitionedResources<NodeChild> => {
const sortedResources = sortResources(resources, resourceTypes ?? [], !ungrouped);
export const partitionResources = (resources: NodeChild[]): PartitionedResources<NodeChild> => {
const sortedResources = sortResources(resources);

const [learningpaths, articles] = partition(
sortedResources,
Expand Down
Loading