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 ; } + console.log("Submit is: ", submit); + return <>
- + - - - - - - - - - - - - ( - )} - control={control} - /> + + + + + + + + ( + + )} + control={control} + /> + + + + + + ( + )} + control={control} + /> + + + + -
* Required fields
@@ -212,7 +260,7 @@ const EditCompanyProfileForm = ({ title }) => { }; EditCompanyProfileForm.propTypes = { - title: PropTypes.string -} + title: PropTypes.string, +}; export default EditCompanyProfileForm; diff --git a/src/components/Company/Edit/EditCompanySchema.js b/src/components/Company/Edit/EditCompanySchema.js new file mode 100644 index 00000000..22f89236 --- /dev/null +++ b/src/components/Company/Edit/EditCompanySchema.js @@ -0,0 +1,11 @@ +import * as yup from "yup"; +import { HumanValidationReasons } from "../../../utils"; +import FinishCompanyRegistrationSchema from "../Registration/Finish/FinishCompanyRegistrationSchema"; +import { generateValidationRule } from "./EditCompanyUtils"; + +export default FinishCompanyRegistrationSchema.concat(yup.object().shape({ + name: yup.string() + .required(HumanValidationReasons.REQUIRED) + .min(...generateValidationRule("name", "minLength", HumanValidationReasons.TOO_SHORT)) + .max(...generateValidationRule("name", "maxLength", HumanValidationReasons.TOO_LONG)), +})); diff --git a/src/components/Company/Edit/EditCompanyUtils.js b/src/components/Company/Edit/EditCompanyUtils.js new file mode 100644 index 00000000..ff64786e --- /dev/null +++ b/src/components/Company/Edit/EditCompanyUtils.js @@ -0,0 +1,8 @@ +import { validationRulesGenerator, generalHumanError } from "../../../utils"; +import { CompanyApplicationConstants } from "../../Apply/Company/CompanyApplicationUtils"; + +export const EditCompanyConstants = { + name: { ...CompanyApplicationConstants.companyName }, +}; + +export const generateValidationRule = validationRulesGenerator(EditCompanyConstants); diff --git a/src/components/Company/Registration/Finish/LogoUploadForm.js b/src/components/Company/Registration/Finish/LogoUploadForm.js index 04744e95..d80b7a2e 100644 --- a/src/components/Company/Registration/Finish/LogoUploadForm.js +++ b/src/components/Company/Registration/Finish/LogoUploadForm.js @@ -33,7 +33,7 @@ export const useLogoUpload = ({ watch }) => { useEffect(() => { if (!logoInput) { setLogoPreview(undefined); - return () => {}; + return () => { }; } else { const objectUrl = URL.createObjectURL(logoInput); @@ -180,10 +180,10 @@ const LogoUploadForm = () => { > {logoPreview && - } + }
{ showHiddenField, } = useContext(context); + console.log("Fields are: ", fields); + const isMobile = useMobile(); const formDisabled = !isLoggedIn || companyUnfinishedRegistration; @@ -149,14 +151,14 @@ const OfferForm = ({ context, title }) => { /> {showOwnerComponent && - - - } + + + } onRemove(i)} /> }} + onRemove(i)} /> + }} margin="normal" helperText={errors?.[i]?.value?.message || ""} {...textFieldProps} diff --git a/src/hooks/useOfferForm.js b/src/hooks/useOfferForm.js index 03d1cb06..fcd65a64 100644 --- a/src/hooks/useOfferForm.js +++ b/src/hooks/useOfferForm.js @@ -72,7 +72,9 @@ export default (schema) => { clearErrors(); } }, - [clearErrors, isLoggedIn]); + [clearErrors, isLoggedIn]); + + console.log("So much abstraction makes the code harder to read: ", fields); return { success, diff --git a/src/services/companyEditService.js b/src/services/companyEditService.js new file mode 100644 index 00000000..8de118cf --- /dev/null +++ b/src/services/companyEditService.js @@ -0,0 +1,44 @@ +import Offer from "../components/HomePage/SearchResultsArea/Offer/Offer"; +import config from "../config"; +import { parseFiltersToURL, buildCancelableRequest } from "../utils"; +import { createEvent, measureTime, sendSearchReport, createErrorEvent } from "../utils/analytics"; +import { EVENT_TYPES, TIMED_ACTIONS } from "../utils/analytics/constants"; +import Constants from "../utils/Constants"; +import ErrorTypes from "../utils/ErrorTypes"; +const { API_HOSTNAME } = config; + +export const editCompany = measureTime(TIMED_ACTIONS.OFFER_EDIT, async ({ + id, + name, + contacts, + bio, + logo +}) => { + const updatedCompany = { + id, + name, + contacts, + bio, + logo, + }; + + try { + const res = await fetch(`${API_HOSTNAME}/company/edit/${id}`, { + method: "PUT", + headers: { + "Content-Type": "application/json", + }, + credentials: "include", + body: JSON.stringify(updatedCompany), + }); + const json = await res.json(); + + if (!res.ok) { + throw json.errors; + } + + return json; + } catch (error) { + + } +});