Skip to content

Commit

Permalink
Sumarized code inside useEffects in OfferForm.
Browse files Browse the repository at this point in the history
  • Loading branch information
Process-ing committed Mar 7, 2023
1 parent 1cd75e8 commit b44a324
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions src/components/Offers/Form/form-components/OfferForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ export const PAID_OPTIONS = [
{ value: false, label: "Unpaid" },
];

const scrollToError = (errorArray) => {
if (Object.keys(errorArray).length !== 0) {
const element = document.getElementById(Object.keys(errorArray)[0]);
if (element?.scrollIntoView) {
element.scrollIntoView({ behavior: "smooth" });
}
if (element?.focus) {
element.focus();
}
}
};

const OfferForm = ({ context, title }) => {
const {
submit,
Expand Down Expand Up @@ -103,27 +115,11 @@ const OfferForm = ({ context, title }) => {
};

useEffect(() => {
if (Object.keys(errors).length !== 0) {
const element = document.getElementById(Object.keys(errors)[0]);
if (element?.scrollIntoView) {
element.scrollIntoView({ behavior: "smooth" });
}
if (element?.focus) {
element.focus();
}
}
scrollToError(errors);
}, [errors]);

useEffect(() => {
if (Object.keys(requestErrors).length !== 0) {
const element = document.getElementById(Object.keys(requestErrors)[0]);
if (element?.scrollIntoView) {
element.scrollIntoView({ behavior: "smooth" });
}
if (element?.focus) {
element.focus();
}
}
scrollToError(requestErrors);
}, [requestErrors]);

return (
Expand Down

0 comments on commit b44a324

Please sign in to comment.