Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

status:'ok' & move makeFieldError to shared

Roman Pominov6a8764357f6e
diff --git a/apps/designer/app/designer/features/sidebar-left/panels/pages/settings.tsx b/apps/designer/app/designer/features/sidebar-left/panels/pages/settings.tsx
index 349a38ff7c..1250bd1c53 100644
--- a/apps/designer/app/designer/features/sidebar-left/panels/pages/settings.tsx
+++ b/apps/designer/app/designer/features/sidebar-left/panels/pages/settings.tsx
@@ -108,7 +108,7 @@ export const NewPageSettings = ({
const fetcher = useFetcher<CreatePageData>();
useOnFetchEnd(fetcher, (data) => {
- if ("ok" in data) {
+ if (data.status === "ok") {
onSuccess?.(data.page);
}
});
@@ -268,7 +268,7 @@ export const PageSettings = ({
toFormData({ id: pageId, ...submittedValues, ...unsavedValues }),
{ method: "post", action: `/rest/pages/${projectId}` },
(data) => {
- if ("errors" in data) {
+ if (data.status === "error") {
toastUnknownFieldErrors(normalizeErrors(data.errors), []);
}
}
@@ -276,7 +276,7 @@ export const PageSettings = ({
});
useOnFetchEnd(fetcher, (data) => {
- if ("errors" in data) {
+ if (data.status === "error") {
setUnsavedValues({ ...submittedValues, ...unsavedValues });
}
setSubmittedValues({});
@@ -289,7 +289,7 @@ export const PageSettings = ({
{ id: pageId },
{ method: "delete", action: `/rest/pages/${projectId}` },
(data) => {
- if ("errors" in data) {
+ if (data.status === "error") {
toastUnknownFieldErrors(normalizeErrors(data.errors), []);
}
}
diff --git a/apps/designer/app/routes/rest/pages.$projectId.ts b/apps/designer/app/routes/rest/pages.$projectId.ts
index 6fc79900c9..628762a2cb 100644
--- a/apps/designer/app/routes/rest/pages.$projectId.ts
+++ b/apps/designer/app/routes/rest/pages.$projectId.ts
@@ -9,6 +9,7 @@ import type {
DeletePageData,
EditPageData,
} from "~/shared/pages";
+import { type FetcherData, makeFieldError } from "~/shared/form-utils";
const nonEmptyString = z
.string({
@@ -42,7 +43,7 @@ const handlePut = async (
): Promise<CreatePageData> => {
const result = CreatePageInput.safeParse(await request.formData());
if (result.success === false) {
- return { errors: result.error.formErrors };
+ return { status: "error", errors: result.error.formErrors };
}
const data = result.data;
@@ -50,7 +51,7 @@ const handlePut = async (
const existingPage = utils.pages.findByPath(devBuild.pages, data.path);
if (existingPage !== undefined) {
- return fieldError("path", `Already used for "${existingPage.name}"`);
+ return makeFieldError("path", `Already used for "${existingPage.name}"`);
}
const updatedBuild = await db.build.addPage(devBuild.id, data);
@@ -61,7 +62,7 @@ const handlePut = async (
throw new Error("New page not found");
}
- return { ok: true, page: newPage };
+ return { status: "ok", page: newPage };
};
const EditPageInput = zfd.formData({
@@ -76,7 +77,7 @@ const handlePost = async (
): Promise<EditPageData> => {
const result = EditPageInput.safeParse(await request.formData());
if (result.success === false) {
- return { errors: result.error.formErrors };
+ return { status: "error", errors: result.error.formErrors };
}
const { id, ...data } = result.data;
@@ -85,13 +86,13 @@ const handlePost = async (
if (data.path !== undefined) {
const existingPage = utils.pages.findByPath(devBuild.pages, data.path);
if (existingPage !== undefined && existingPage.id !== id) {
- return fieldError("path", `Already used for "${existingPage.name}"`);
+ return makeFieldError("path", `Already used for "${existingPage.name}"`);
}
}
await db.build.editPage(devBuild.id, id, data);
- return { ok: true };
+ return { status: "ok" };
};
const DeletePageInput = zfd.formData({ id: z.string() });
@@ -102,22 +103,25 @@ const handleDelete = async (
): Promise<DeletePageData> => {
const result = DeletePageInput.safeParse(await request.formData());
if (result.success === false) {
- return { errors: result.error.formErrors };
+ return { status: "error", errors: result.error.formErrors };
}
const { id } = result.data;
const devBuild = await db.build.loadByProjectId(projectId, "dev");
if (devBuild.pages.homePage.id === id) {
- return fieldError("id", `Can't delete the home page`);
+ return makeFieldError("id", `Can't delete the home page`);
}
await db.build.deletePage(devBuild.id, id);
- return { ok: true };
+ return { status: "ok" };
};
-export const action: ActionFunction = async ({ request, params }) => {
+export const action: ActionFunction = async ({
+ request,
+ params,
+}): Promise<FetcherData<unknown>> => {
try {
if (params.projectId === undefined) {
throw new Error(`Project ID required`);
@@ -139,6 +143,7 @@ export const action: ActionFunction = async ({ request, params }) => {
} catch (error) {
sentryException({ error });
return {
+ status: "error",
errors: error instanceof Error ? error.message : String(error),
};
}
@@ -172,12 +177,3 @@ export const loader: LoaderFunction = async ({
};
}
};
-
-const fieldError = (fieldName: string, error: string) => {
- return {
- errors: {
- formErrors: [],
- fieldErrors: { [fieldName]: [error] },
- },
- };
-};
diff --git a/apps/designer/app/shared/form-utils/validation.ts b/apps/designer/app/shared/form-utils/validation.ts
index d771421447..f8907accb9 100644
--- a/apps/designer/app/shared/form-utils/validation.ts
+++ b/apps/designer/app/shared/form-utils/validation.ts
@@ -5,8 +5,8 @@ import type { ZodError } from "zod";
import { useOnFetchEnd } from "~/shared/fetcher";
export type FetcherData<Payload> =
- | ({ ok: true } & Payload)
- | { errors: string | ZodError["formErrors"] };
+ | ({ status: "ok" } & Payload)
+ | { status: "error"; errors: string | ZodError["formErrors"] };
export const normalizeErrors = (
errors: string | ZodError["formErrors"]
@@ -30,7 +30,7 @@ export const useFetcherErrors = <FieldName extends string>({
>({});
useOnFetchEnd(fetcher, (data) => {
- if ("errors" in data) {
+ if (data.status === "error") {
const errors = normalizeErrors(data.errors);
toastUnknownFieldErrors(errors, fieldNames);
setFieldErrors(errors.fieldErrors);
@@ -64,3 +64,16 @@ export const toastUnknownFieldErrors = (
}
}
};
+
+export const makeFieldError = <Payload>(
+ fieldName: string,
+ error: string
+): FetcherData<Payload> => {
+ return {
+ status: "error",
+ errors: {
+ formErrors: [],
+ fieldErrors: { [fieldName]: [error] },
+ },
+ };
+};