Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only scroll to error blurb on field errors mount #2512

Merged
merged 2 commits into from
Feb 2, 2024
Merged
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
3 changes: 2 additions & 1 deletion src/apps/content-editor/src/app/components/Editor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export default memo(function Editor({
saveClicked,
onUpdateFieldErrors,
fieldErrors,
hasErrors,
}) {
const dispatch = useDispatch();
const isNewItem = itemZUID.slice(0, 3) === "new";
Expand Down Expand Up @@ -203,7 +204,7 @@ export default memo(function Editor({
return (
<ThemeProvider theme={theme}>
<div className={styles.Fields}>
{saveClicked && (
{saveClicked && hasErrors && (
<FieldError errors={fieldErrors} fields={activeFields} />
)}
{activeFields.length ? (
Expand Down
27 changes: 13 additions & 14 deletions src/apps/content-editor/src/app/components/Editor/FieldError.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMemo } from "react";
import { useMemo, useRef, useEffect } from "react";
import { Stack, Typography, Box, ThemeProvider } from "@mui/material";
import DangerousRoundedIcon from "@mui/icons-material/DangerousRounded";
import { theme } from "@zesty-io/material";
Expand All @@ -11,6 +11,17 @@ type FieldErrorProps = {
};

export const FieldError = ({ errors, fields }: FieldErrorProps) => {
const errorContainerEl = useRef(null);

// Scroll to the errors on mount
useEffect(() => {
errorContainerEl?.current?.scrollIntoView({
behavior: "smooth",
block: "center",
inline: "center",
});
}, []);

const fieldErrors = useMemo(() => {
const errorMap = Object.entries(errors)?.map(([name, errors]) => {
let errorMessage = "";
Expand All @@ -36,29 +47,17 @@ export const FieldError = ({ errors, fields }: FieldErrorProps) => {
return errorMap.sort((a, b) => a.sort - b.sort);
}, [errors, fields]);

const hasErrors = fieldErrors?.some((error) => error.errorMessage);
const fieldsWithErrors = fieldErrors?.filter((error) => error.errorMessage);

const handleErrorClick = (fieldZUID: string) => {
const fieldElement = document.getElementById(fieldZUID);
fieldElement?.scrollIntoView({ behavior: "smooth" });
};

if (!hasErrors) {
return <></>;
}

return (
<ThemeProvider theme={theme}>
<Stack
// when errors are present, scroll to the top of the error message
ref={(el) =>
el?.scrollIntoView({
behavior: "smooth",
block: "center",
inline: "center",
})
}
ref={errorContainerEl}
p={2}
gap={1}
borderRadius={1}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from "react";
import { useEffect, useMemo, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import useIsMounted from "ismounted";
import { useHistory, useParams } from "react-router-dom";
Expand Down Expand Up @@ -107,7 +107,7 @@ export const ItemCreate = () => {
}
}, [isPublishing, isPublished, publishedItem, willRedirect]);

useEffect(() => {
const hasErrors = useMemo(() => {
const hasErrors = Object.values(fieldErrors)
?.map((error) => {
return Object.values(error) ?? [];
Expand All @@ -118,6 +118,8 @@ export const ItemCreate = () => {
if (!hasErrors) {
setSaveClicked(false);
}

return hasErrors;
}, [fieldErrors]);

const loadItemFields = async (modelZUID: string) => {
Expand Down Expand Up @@ -282,8 +284,7 @@ export const ItemCreate = () => {
<div className={styles.Editor}>
<Editor
// @ts-ignore no types
active={active}
scrolled={setActive}
hasErrors={hasErrors}
itemZUID={itemZUID}
item={item}
items={content}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export default function Content(props) {
saveClicked={props.saveClicked}
fieldErrors={props.fieldErrors}
onUpdateFieldErrors={props.onUpdateFieldErrors}
hasErrors={props.hasErrors}
/>
</Box>
</Box>
Expand Down
7 changes: 5 additions & 2 deletions src/apps/content-editor/src/app/views/ItemEdit/ItemEdit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Fragment, useEffect, useState } from "react";
import { Fragment, useEffect, useState, useMemo } from "react";
import {
Switch,
Route,
Expand Down Expand Up @@ -125,7 +125,7 @@ export default function ItemEdit() {
};
}, [modelZUID, itemZUID]);

useEffect(() => {
const hasErrors = useMemo(() => {
const hasErrors = Object.values(fieldErrors)
?.map((error) => {
return Object.values(error) ?? [];
Expand All @@ -136,6 +136,8 @@ export default function ItemEdit() {
if (!hasErrors) {
setSaveClicked(false);
}

return hasErrors;
}, [fieldErrors]);

async function lockItem() {
Expand Down Expand Up @@ -464,6 +466,7 @@ export default function ItemEdit() {
setFieldErrors(errors);
}}
fieldErrors={fieldErrors}
hasErrors={hasErrors}
/>
)}
/>
Expand Down
Loading