Skip to content

Commit

Permalink
Route different content types to correct edit pages (#2169)
Browse files Browse the repository at this point in the history
* Reroute content

* Reverting changes to Root.tsx and urls.py

* Revert change to ResourceLinkMarkdownSyntax

* Update rerouting

* Undo changes to App.tsx

* Making routing work

* Adding URL test

* Adding test for GenericContentReroutePage
  • Loading branch information
pt2302 authored Apr 30, 2024
1 parent e8a0d1d commit 1fefd9a
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 3 deletions.
54 changes: 54 additions & 0 deletions static/js/components/GenericContentReroutePage.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { makeWebsiteDetail } from "../util/factories/websites"
import { useWebsiteContent } from "../hooks/websites"
import { IntegrationTestHelper } from "../testing_utils"
import GenericContentReroutePage from "./GenericContentReroutePage"
import { waitFor } from "@testing-library/react"
import React from "react"

jest.mock("../hooks/websites")

describe("GenericContentReroutePage", () => {
const mockWebsite = makeWebsiteDetail({
name: "test-site",
uuid: "website-uuid",
title: "Test Site",
})

const helper = new IntegrationTestHelper()
helper.mockGetWebsiteDetail(mockWebsite)

const testCases = [
{
type: "page",
expectedPath: "/sites/test-site/type/page/edit/test-uuid/",
},
{
type: "video_gallery",
expectedPath: "/sites/test-site/type/video_gallery/edit/test-uuid/",
},
{
type: "external_resource",
expectedPath: "/sites/test-site/type/external_resource/edit/test-uuid/",
},
]

testCases.forEach(({ type, expectedPath }) => {
it(`reroutes to the correct page based on content type: ${type}`, async () => {
const mockResource = {
type,
text_id: "test-uuid",
}

;(useWebsiteContent as jest.Mock).mockReturnValue([mockResource])

const [_, { history }] = helper.renderWithWebsite(
<GenericContentReroutePage />,
mockWebsite,
)

await waitFor(() => {
expect(history.location.pathname).toBe(expectedPath)
})
})
})
})
33 changes: 33 additions & 0 deletions static/js/components/GenericContentReroutePage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useParams, useHistory } from "react-router"
import { useWebsiteContent } from "../hooks/websites"
import { useEffect } from "react"
import { siteContentDetailUrl } from "../lib/urls"
import { useWebsite } from "../context/Website"
/**
* A page to be served at /content/:uuid that fetches the resource and reroutes
* the user to the appropriate page, i.e.,
* /type/pages/:uuid
* /type/resources/:uuid
* /type/video-galleries/:uuid
* etc.
**/
const GenericContentReroutePage = () => {
const { uuid } = useParams<{ uuid: string }>()
const history = useHistory()
const website = useWebsite()
const [resource] = useWebsiteContent(uuid)
useEffect(() => {
if (!resource?.type) return
history.replace({
pathname: siteContentDetailUrl.param({
contentType: resource.type,
name: website.name,
uuid: resource.text_id,
}).pathname,
})
}, [uuid, history, resource?.type, resource?.text_id, website.name])

return null
}

export default GenericContentReroutePage
5 changes: 2 additions & 3 deletions static/js/components/widgets/MarkdownEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
import ResourcePickerDialog from "./ResourcePickerDialog"
import useThrowSynchronously from "../../hooks/useAsyncError"
import { useWebsite } from "../../context/Website"
import { siteContentDetailUrl } from "../../lib/urls"
import { siteContentRerouteUrl } from "../../lib/urls"

export interface Props {
value?: string
Expand Down Expand Up @@ -128,9 +128,8 @@ export default function MarkdownEditor(props: Props): JSX.Element {
const resourceLink = {
[RESOURCE_LINK_CONFIG_KEY]: {
hrefTemplate: `${location.origin}${
siteContentDetailUrl.param({
siteContentRerouteUrl.param({
name: website.name,
contentType: "resource",
}).pathname
}`,
},
Expand Down
9 changes: 9 additions & 0 deletions static/js/lib/urls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
siteApiContentListingUrl,
siteContentNewUrl,
siteContentDetailUrl,
siteContentRerouteUrl,
} from "./urls"

describe("urls", () => {
Expand Down Expand Up @@ -154,6 +155,14 @@ describe("urls", () => {
.toString(),
).toBe("/api/websites/the-best-course/content/?limit=10&offset=40")
})

it("renders a content reroute URL", () => {
expect(
siteContentRerouteUrl
.param({ name: "site-name", uuid: "test-uuid" })
.toString(),
).toBe("/sites/site-name/content/test-uuid/")
})
})
})
})
1 change: 1 addition & 0 deletions static/js/lib/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const sitesBaseUrl = UrlAssembler.prefix("/sites/")
export const newSiteUrl = UrlAssembler.prefix("/new-site/")

export const siteDetailUrl = sitesBaseUrl.segment(":name/")
export const siteContentRerouteUrl = siteDetailUrl.segment("content/:uuid/")
export const siteContentListingUrl = siteDetailUrl.segment("type/:contentType/")
export const siteContentNewUrl = siteContentListingUrl.segment("new/")
export const siteContentDetailUrl = siteContentListingUrl.segment("edit/:uuid/")
Expand Down
8 changes: 8 additions & 0 deletions static/js/pages/SitePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import Spinner from "../components/util/Spinner"
import {
siteCollaboratorsUrl,
siteContentListingUrl,
siteContentRerouteUrl,
siteDetailUrl,
} from "../lib/urls"

import { useWebsite } from "../context/Website"
import DocumentTitle, { formatTitle } from "../components/DocumentTitle"
import GenericContentReroutePage from "../components/GenericContentReroutePage"

interface SitePageProps {
isLoading: boolean
Expand All @@ -33,6 +35,12 @@ export default function SitePage(props: SitePageProps): JSX.Element | null {
<SiteSidebar website={website} />
</Card>
<div className="content pl-3">
<Route
path={
siteContentRerouteUrl.param({ name: website.name }).pathname
}
component={GenericContentReroutePage}
/>
<Route
path={siteCollaboratorsUrl.param("name", website.name).toString()}
>
Expand Down

0 comments on commit 1fefd9a

Please sign in to comment.