use page id for css route, cleanup

This commit is contained in:
Oleg Isonen committed 2022-11-15 14:11:38 +01:00
1 parent dcc4de4beb
commit ff0dde06d8
14 files changed
+74 -32

No files matched your search

@@ -10,7 +10,6 @@ import {
} from "@webstudio-is/react-sdk";
import { useCssRules } from "@webstudio-is/css-engine";
import { useTextEditingInstanceId } from "~/shared/nano-states";
import "./wrapper-component-css";
import { useSelectedElement } from "~/canvas/shared/nano-states";
const TextEditor = lazy(() => import("../text-editor"));
+1 -1
View File
@@ -284,7 +284,7 @@ export const Designer = ({
);
const page = useMemo(() => {
const page = projectUtils.pages.findById(pages, pageId);
const page = projectUtils.pages.findByIdOrPath(pages, pageId);
if (page === undefined) {
throw new Error(`Page with id ${pageId} not found`);
}
@@ -260,7 +260,7 @@ export const PageSettings = ({
const fetcher = useFetcher<EditPageData>();
const [pages] = usePages();
const page = pages && projectUtils.pages.findById(pages, pageId);
const page = pages && projectUtils.pages.findByIdOrPath(pages, pageId);
const [unsavedValues, setUnsavedValues] = useState<Partial<FormPage>>({});
const [submittedValues, setSubmittedValues] = useState<Partial<FormPage>>({});
@@ -26,7 +26,7 @@ export const loader: LoaderFunction = async ({
const page =
params.pageId === undefined
? prodBuild.pages.homePage
: utils.pages.findById(prodBuild.pages, params.pageId);
: utils.pages.findByIdOrPath(prodBuild.pages, params.pageId);
if (page === undefined) {
throw new Error(`Page ${params.pageId} not found`);
@@ -26,7 +26,7 @@ export const loader: LoaderFunction = async ({
const page =
params.pageId === undefined
? prodBuild.pages.homePage
: utils.pages.findById(prodBuild.pages, params.pageId);
: utils.pages.findByIdOrPath(prodBuild.pages, params.pageId);
if (page === undefined) {
throw new Error(`Page ${params.pageId} not found`);
+1 -1
View File
@@ -24,7 +24,7 @@ export const loader: ActionFunction = async ({ request, params }) => {
const canvasData = await loadCanvasData(
project,
buildParams.mode === "published" ? "prod" : "dev",
params.pagePath // @todo use page id
buildParams.pageId
);
if (canvasData === undefined) {
+3 -3
View File
@@ -8,7 +8,7 @@ export type CanvasData = Data & { buildId: Build["id"]; page: Page };
export const loadCanvasData = async (
project: Project,
env: "dev" | "prod",
pagePath = ""
pageIdOrPath = ""
): Promise<CanvasData | undefined> => {
const build =
env === "dev"
@@ -19,10 +19,10 @@ export const loadCanvasData = async (
throw new Error("The project is not published");
}
const page = utils.pages.findByPath(build.pages, pagePath);
const page = utils.pages.findByIdOrPath(build.pages, pageIdOrPath);
if (page === undefined) {
return;
throw new Error(`Page ${pageIdOrPath} not found`);
}
const [tree, props, breakpoints] = await Promise.all([
@@ -62,17 +62,20 @@ export const getBuildParams = (
projectId: string;
mode: BuildMode;
pathname: string;
pageId?: string;
}
| {
projectDomain: string;
mode: BuildMode;
pathname: string;
pageId?: string;
}
| undefined => {
const url = new URL(request.url);
const requestHost = getRequestHost(request);
const buildHost = new URL(getBuildOrigin(request, env)).host;
const pageId = url.searchParams.get("pageId");
if (env.BUILD_REQUIRE_SUBDOMAIN !== "true") {
const projectId = url.searchParams.get("projectId");
@@ -81,6 +84,7 @@ export const getBuildParams = (
projectId,
mode: getMode(url),
pathname: url.pathname,
pageId,
};
}
}
@@ -92,6 +96,7 @@ export const getBuildParams = (
projectDomain,
mode: getMode(url),
pathname: url.pathname,
pageId,
};
}
};
+2 -2
View File
@@ -109,7 +109,7 @@ export const editPage = async (
data: Partial<Omit<Page, "id" | "treeId">>
) => {
return updatePages(buildId, async (currentPages) => {
const currentPage = pagesUtils.findById(currentPages, pageId);
const currentPage = pagesUtils.findByIdOrPath(currentPages, pageId);
if (currentPage === undefined) {
throw new Error(`Page with id "${pageId}" not found`);
}
@@ -141,7 +141,7 @@ export const deletePage = async (buildId: Build["id"], pageId: Page["id"]) => {
throw new Error("Cannot delete home page");
}
const page = pagesUtils.findById(currentPages, pageId);
const page = pagesUtils.findByIdOrPath(currentPages, pageId);
if (page === undefined) {
throw new Error(`Page with id "${pageId}" not found`);
}
@@ -0,0 +1,45 @@
import { findByIdOrPath } from "./find-by-id-or-path";
const pages = {
homePage: {
id: "home",
path: "/",
name: "Home",
title: "Home",
treeId: "tree-1",
meta: {},
},
pages: [
{
id: "page1",
path: "/page1",
name: "Page",
title: "Page",
treeId: "tree-1",
meta: {},
},
],
};
describe("Find by id or path", () => {
test("home page by id", () => {
const page = findByIdOrPath(pages, "home");
expect(page).toEqual(pages.homePage);
});
test("home page by path /", () => {
const page = findByIdOrPath(pages, "/");
expect(page).toEqual(pages.homePage);
});
test("home page by empty path", () => {
const page = findByIdOrPath(pages, "");
expect(page).toEqual(pages.homePage);
});
test("find page by id", () => {
const page = findByIdOrPath(pages, "page1");
expect(page).toEqual(pages.pages[0]);
});
test("find page by path", () => {
const page = findByIdOrPath(pages, "/path1");
expect(page).toEqual(pages.pages[0]);
});
});
@@ -0,0 +1,13 @@
import { type Pages, type Page } from "../../db/types";
export const findByIdOrPath = (
pages: Pages,
idOrPath: string
): Page | undefined => {
if (idOrPath === "" || idOrPath === "/" || idOrPath === pages.homePage.id) {
return pages.homePage;
}
return pages.pages.find(
(page) => page.path === idOrPath || page.id === idOrPath
);
};
@@ -1,8 +0,0 @@
import { type Pages, type Page } from "../../db/types";
export const findById = (pages: Pages, pageId: string): Page | undefined => {
if (pages.homePage.id === pageId) {
return pages.homePage;
}
return pages.pages.find((page) => page.id === pageId);
};
@@ -1,11 +0,0 @@
import { type Pages, type Page } from "../../db/types";
export const findByPath = (
pages: Pages,
pagePath: string
): Page | undefined => {
if (pagePath === "" || pagePath === "/") {
return pages.homePage;
}
return pages.pages.find((page) => page.path === pagePath);
};
+1 -2
View File
@@ -1,2 +1 @@
export * from "./find-by-id";
export * from "./find-by-path";
export * from "./find-by-id-or-path";