Files
bodyshop/client/src/components/form-fields-changed-alert/form-fields-changed-alert.component.jsx
2022-07-18 16:25:33 -07:00

74 lines
2.3 KiB
JavaScript

import React from "react";
import { Form, Space } from "antd";
import { useTranslation } from "react-i18next";
import AlertComponent from "../alert/alert.component";
import { Prompt, useLocation } from "react-router-dom";
import "./form-fields-changed.styles.scss";
export default function FormsFieldChanged({ form, skipPrompt }) {
const { t } = useTranslation();
const handleReset = () => {
form.resetFields();
};
const loc = useLocation();
//if (!form.isFieldsTouched()) return <></>;
return (
<Form.Item
className="form-fields-changed"
shouldUpdate
style={{ margin: 0, padding: 0, minHeight: "unset" }}
>
{() => {
const errors = form.getFieldsError().filter((e) => e.errors.length > 0);
if (form.isFieldsTouched())
return (
<Space direction="vertical" style={{ width: "100%" }}>
<Prompt
when={skipPrompt ? false : true}
message={(location) => {
if (loc.pathname === location.pathname) return false;
return t("general.messages.unsavedchangespopup");
}}
/>
<AlertComponent
type="warning"
message={
<div>
<span>{t("general.messages.unsavedchanges")} </span>
<span
onClick={handleReset}
style={{
cursor: "pointer",
textDecoration: "underline",
}}
>
{t("general.actions.reset")}
</span>
</div>
}
/>
{errors.length > 0 && (
<AlertComponent
type="error"
message={
<div>
<ul>
{errors.map((e, idx) =>
e.errors.map((e2, idx2) => (
<li key={`${idx}${idx2}`}>{e2}</li>
))
)}
</ul>
</div>
}
/>
)}
</Space>
);
return <div style={{ display: "none" }}></div>;
}}
</Form.Item>
);
}