diff --git a/src/AppRouter.js b/src/AppRouter.js
index 2b83f2e5..b066e5b7 100644
--- a/src/AppRouter.js
+++ b/src/AppRouter.js
@@ -32,7 +32,7 @@ import {
import { CookieConsent } from "./cookieConsent";
import {
EditOfferController,
- EditOfferControllerContext
+ EditOfferControllerContext,
} from "./components/Offers/Edit/EditOfferForm";
import EditOfferPage from "./pages/EditOfferPage";
import PrivacyPolicyPage from "./pages/PrivacyPolicyPage";
diff --git a/src/components/Company/Edit/EditCompanyProfileForm.js b/src/components/Company/Edit/EditCompanyProfileForm.js
index 42d617e1..29665882 100644
--- a/src/components/Company/Edit/EditCompanyProfileForm.js
+++ b/src/components/Company/Edit/EditCompanyProfileForm.js
@@ -17,7 +17,7 @@ import { useMobile } from "../../../utils/media-queries";
import useOfferFormStyles from "../../../components/Offers/Form/form-components/offerStyles";
import TextEditorComponent from "../../Offers/Form/form-components/TextEditorComponent";
import useOffer from "../../../hooks/useOffer";
-import { Controller, useForm } from "react-hook-form";
+import { Controller, useForm, useWatch } from "react-hook-form";
import useCompany from "../../../hooks/useCompany";
import useSession from "../../../hooks/useSession";
import { Redirect, useLocation, useParams } from "react-router-dom/cjs/react-router-dom.min";
@@ -31,8 +31,8 @@ export const EditCompanyControllerContext = React.createContext();
const useStyles = makeStyles((theme) => ({
submitBtn: {
- marginTop: theme.spacing(2)
- }
+ marginTop: theme.spacing(2),
+ },
}));
export const EditCompanyController = () => {
@@ -41,9 +41,21 @@ export const EditCompanyController = () => {
const { data: user, isValidating } = useSession();
let canEditRaceControl = false;
- const shouldRevalidateEditingPermissions = useCallback(() => {
- return user?.isAdmin || user?.company?._id === id;
- }, [company, user]);
+ const { handleSubmit, control, reset, formState } = useForm({
+ mode: "all",
+ resolver: yupResolver(EditCompanySchema),
+ defaultValues: {
+ logo: "",
+ name: "",
+ contacts: [],
+ bio: "",
+ },
+ reValidateMode: "onChange",
+ });
+
+ const fields = useWatch({ control });
+
+ const shouldRevalidateEditingPermissions = useCallback(() => user?.isAdmin || user?.company?._id === id, [id, user]);
const [canEdit, setCanEdit] = useState(shouldRevalidateEditingPermissions());
@@ -54,6 +66,12 @@ export const EditCompanyController = () => {
}
}, [shouldRevalidateEditingPermissions, loadingCompany, company, user]);
+ useEffect(() => {
+ if (company && !isValidating && canEdit) {
+ reset(company);
+ }
+ }, [canEdit, isValidating, company, reset]);
+
const location = useLocation();
const redirectProps = {
to: {
@@ -65,6 +83,11 @@ export const EditCompanyController = () => {
},
};
+ const submit = useCallback((data) => {
+ alert("Foda-se meu");
+ },
+ );
+
return {
controllerOptions: {
initialValue: {
@@ -75,7 +98,10 @@ export const EditCompanyController = () => {
companyError,
isValidating,
canEditRaceControl,
- }
+ control,
+ fields,
+ submit: handleSubmit(submit),
+ },
},
};
};
@@ -92,17 +118,13 @@ const EditCompanyProfileForm = ({ title }) => {
redirectProps,
isValidating,
canEditRaceControl,
+ control,
+ submit,
} = useContext(EditCompanyControllerContext);
const classes = useStyles();
- const { control } = useForm({
- mode: "all",
- resolver: yupResolver(EditCompanySchema),
- reValidateMode: "onChange",
- });
-
- const { fields, append, remove } = useContacts({ control });
+ const { fields: contacts, append, remove } = useContacts({ control });
const getContacts = useCallback(() => {
if (!company) return [{}];
@@ -126,82 +148,108 @@ const EditCompanyProfileForm = ({ title }) => {
return