diff --git a/src/containers/StructurePage/plannedResource/AddExistingResource.tsx b/src/containers/StructurePage/plannedResource/AddExistingResource.tsx index a1f6ad337b..152345c900 100644 --- a/src/containers/StructurePage/plannedResource/AddExistingResource.tsx +++ b/src/containers/StructurePage/plannedResource/AddExistingResource.tsx @@ -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"; @@ -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"; @@ -88,7 +88,6 @@ const StyledFieldRoot = styled(FieldRoot, { interface Props { onClose: () => void; - resourceTypes?: ResourceType[]; nodeId: string; existingResourceIds: string[]; type: Exclude; @@ -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(""); @@ -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 ?? ""); diff --git a/src/containers/StructurePage/plannedResource/PlannedResourceDialog.tsx b/src/containers/StructurePage/plannedResource/PlannedResourceDialog.tsx index af91e6f71a..5c5316e392 100644 --- a/src/containers/StructurePage/plannedResource/PlannedResourceDialog.tsx +++ b/src/containers/StructurePage/plannedResource/PlannedResourceDialog.tsx @@ -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"; @@ -26,13 +26,12 @@ import { ResourceGroup } from "../utils"; interface Props { currentNode: Node; - resourceTypes: ResourceType[]; existingResourceIds: string[]; supplementary?: boolean; type: Exclude; } -export const PlannedResourceDialogContent = ({ currentNode, resourceTypes, existingResourceIds, type }: Props) => { +export const PlannedResourceDialogContent = ({ currentNode, existingResourceIds, type }: Props) => { const { t } = useTranslation(); const { setOpen } = useDialogContext(); return ( @@ -59,7 +58,6 @@ export const PlannedResourceDialogContent = ({ currentNode, resourceTypes, exist setOpen(false)} existingResourceIds={existingResourceIds} diff --git a/src/containers/StructurePage/resourceComponents/Resource.tsx b/src/containers/StructurePage/resourceComponents/Resource.tsx index 0e45c646ea..2d6e6d40a3 100644 --- a/src/containers/StructurePage/resourceComponents/Resource.tsx +++ b/src/containers/StructurePage/resourceComponents/Resource.tsx @@ -161,7 +161,7 @@ interface Props { invalidate: () => void; type: ResourceGroup; index: number; - isUngrouped?: boolean; + numbered?: boolean; } const Resource = ({ @@ -173,7 +173,7 @@ const Resource = ({ contentMeta, type, index, - isUngrouped, + numbered, }: Props) => { const { t, i18n } = useTranslation(); const { taxonomyVersion } = useTaxonomyVersion(); @@ -213,7 +213,7 @@ const Resource = ({ return ( - {!!isUngrouped && ( + {!!numbered && ( {index + 1} diff --git a/src/containers/StructurePage/resourceComponents/ResourceItems.tsx b/src/containers/StructurePage/resourceComponents/ResourceItems.tsx index a79ef3645e..3f91a16490 100644 --- a/src/containers/StructurePage/resourceComponents/ResourceItems.tsx +++ b/src/containers/StructurePage/resourceComponents/ResourceItems.tsx @@ -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"; @@ -70,12 +70,11 @@ interface Props { description?: string; resources: NodeChild[]; currentNode: Node; - resourceTypes?: ResourceType[]; contentMetas: Dictionary; nodeResourcesIsPending: boolean; users?: Dictionary; existingResourceIds: string[]; - isUngrouped?: boolean; + numbered?: boolean; hideAddButton?: boolean; } @@ -90,9 +89,8 @@ const ResourceItems = ({ type, title, description, - resourceTypes, existingResourceIds, - isUngrouped, + numbered, hideAddButton, }: Props) => { const { setCurrentNode } = useCurrentNode(); @@ -196,7 +194,6 @@ const ResourceItems = ({ ) : ( @@ -229,7 +226,7 @@ const ResourceItems = ({ nodeResourcesIsPending={nodeResourcesIsPending} invalidate={onDeleted} index={idx} - isUngrouped={isUngrouped} + numbered={numbered} /> )} /> diff --git a/src/containers/StructurePage/resourceComponents/ResourcesContainer.tsx b/src/containers/StructurePage/resourceComponents/ResourcesContainer.tsx index f9004bf8d3..3139eed920 100644 --- a/src/containers/StructurePage/resourceComponents/ResourcesContainer.tsx +++ b/src/containers/StructurePage/resourceComponents/ResourcesContainer.tsx @@ -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"; @@ -36,36 +36,24 @@ const ResourceWrapper = styled("div", { interface Props { nodeResources: NodeChild[]; - resourceTypes: ResourceType[]; currentNode: NodeChild; contentMetas: Dictionary; - isUngrouped: boolean; + numbered: boolean; hasSubTopics: boolean; nodeResourcesIsPending: boolean; users: Dictionary | 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( @@ -73,11 +61,7 @@ const ResourcesContainer = ({ { 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; @@ -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} /> | 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: [ @@ -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( { @@ -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 ( ( +export const useAllResourceTypes = ( params: UseAllResourceTypesParams, options?: Partial>, ) => diff --git a/src/util/taxonomyHelpers.ts b/src/util/taxonomyHelpers.ts index 90a7465036..c589ddfcfa 100644 --- a/src/util/taxonomyHelpers.ts +++ b/src/util/taxonomyHelpers.ts @@ -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"; @@ -21,21 +21,9 @@ export const getContentUriInfo = (urn?: string): ContentUriInfo | undefined => { }; type ResourceLike = Pick; -export const sortResources = ( - resources: T[], - resourceTypes: ResourceType[], - grouped?: boolean, -) => { +const sortResources = (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>((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 { @@ -44,12 +32,8 @@ interface PartitionedResources { coreArticles: T[]; } -export const partitionResources = ( - resources: NodeChild[], - resourceTypes: ResourceType[], - ungrouped: boolean, -): PartitionedResources => { - const sortedResources = sortResources(resources, resourceTypes ?? [], !ungrouped); +export const partitionResources = (resources: NodeChild[]): PartitionedResources => { + const sortedResources = sortResources(resources); const [learningpaths, articles] = partition( sortedResources,