Skip to content

Commit f799c95

Browse files
committed
Added companies being able to edit their info
1 parent 2e4ebe9 commit f799c95

File tree

5 files changed

+129
-48
lines changed

5 files changed

+129
-48
lines changed

src/components/Company/Edit/EditCompanyProfileForm.js

Lines changed: 91 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,73 @@ import { Redirect, useLocation, useParams } from "react-router-dom/cjs/react-rou
2424
import { yupResolver } from "@hookform/resolvers/yup";
2525
import EditCompanySchema from "./EditCompanySchema";
2626
import MultiOptionTextField from "../../utils/form/MultiOptionTextField";
27-
import LogoUploadForm from "../Registration/Finish/LogoUploadForm";
27+
import LogoUploadForm, { turnImgIntoFile } from "../Registration/Finish/LogoUploadForm";
2828
import { useContacts } from "../Registration/Finish/ContactsForm";
29-
30-
export const EditCompanyControllerContext = React.createContext();
31-
32-
const EditImage = ({ style, image, imageAlt }) => {
33-
return (<Avatar
34-
alt={imageAlt}
35-
src={image}
36-
style={style}
37-
/>);
38-
};
29+
import { FinishCompanyRegistrationController, FinishCompanyRegistrationControllerContext } from "../Registration/Finish/FinishCompanyRegistrationWidget";
30+
import { useLogoUpload } from "../Registration/Finish/LogoUploadForm";
31+
import { CloudUpload, Edit } from "@material-ui/icons";
32+
import getCroppedImg from "../../utils/image/cropImage";
3933

4034
const useStyles = makeStyles((theme) => ({
4135
submitBtn: {
4236
marginTop: theme.spacing(2),
4337
},
38+
avatar: {
39+
marginBottom: theme.spacing(1),
40+
height: "5em",
41+
width: "5em",
42+
},
4443
}));
4544

45+
export const EditCompanyControllerContext = React.createContext();
46+
47+
const EditImageText = () => <>
48+
<Box
49+
marginY={2}
50+
>
51+
<Typography align="center" variant="h6">
52+
{"Upload your Company's logo."}
53+
</Typography>
54+
</Box>
55+
</>;
56+
57+
const EditImage = ({ classname, image, imageAlt }) => {
58+
const {
59+
logoUploadOptions,
60+
register,
61+
errors,
62+
} = useContext(EditCompanyControllerContext);
63+
64+
const [editingImage, setEditingImage] = useState(false);
65+
66+
return editingImage ?
67+
<FinishCompanyRegistrationControllerContext.Provider value={{ logoUploadOptions, register, errors }}>
68+
<LogoUploadForm InfoText={EditImageText} />
69+
</FinishCompanyRegistrationControllerContext.Provider>
70+
:
71+
(<>
72+
<Box
73+
align="center"
74+
>
75+
<Avatar
76+
alt={imageAlt}
77+
src={image}
78+
className={classname}
79+
/>
80+
<Button
81+
onClick={() => setEditingImage(true)}
82+
variant="contained"
83+
component="span"
84+
color="primary"
85+
startIcon={<Edit />}
86+
>
87+
Edit
88+
</Button>
89+
</Box>
90+
</>
91+
);
92+
};
93+
4694
const getParsedCompanyContacts = (contacts) => {
4795
const newContacts = [];
4896
for (const contact of contacts) {
@@ -58,20 +106,25 @@ const parseCompany = ({
58106
}) => ({
59107
contacts: contacts.map((value) => ({ value })),
60108
...company,
61-
logo: "https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftse1.mm.bing.net%2Fth%3Fid%3DOIP.1SzV5xZOzNbaUOENWpw9vwHaHa%26pid%3DApi&f=1&ipt=36ec7cc75953554a84897bc0aaa6402e9267a031b8618c412927094349aca538&ipo=images"
62109
});
63110

111+
const getCorrectEditedImage = (logoUploadOptions, logo) => {
112+
if (!logoUploadOptions.logoPreview) {
113+
114+
}
115+
};
116+
64117
export const EditCompanyController = () => {
65118
const { id } = useParams();
66119
const { company, error: companyError, loading: loadingCompany } = useCompany(id);
67120
const { data: user, isValidating } = useSession();
68121
let canEditRaceControl = false;
69122

70-
const { handleSubmit, control, reset, getValues } = useForm({
123+
const { handleSubmit, control, reset, getValues, register, watch, formState: { errors } } = useForm({
71124
mode: "all",
72125
resolver: yupResolver(EditCompanySchema),
73126
defaultValues: {
74-
logo: "",
127+
logo: undefined,
75128
name: "",
76129
contacts: [],
77130
bio: "",
@@ -109,15 +162,26 @@ export const EditCompanyController = () => {
109162
},
110163
};
111164

112-
const submit = (data) => {
165+
const submit = async (data) => {
113166
const contacts = getParsedCompanyContacts(data.contacts);
114-
editCompany({ ...data, contacts: contacts }).then(() => {
167+
168+
const croppedImage = logoUploadOptions.logoPreview ? await
169+
getCroppedImg(
170+
logoUploadOptions.logoPreview,
171+
logoUploadOptions.croppedAreaPixels,
172+
0
173+
) : undefined;
174+
175+
editCompany({ ...data, contacts: contacts, logo: croppedImage }).then(() => {
115176

116177
}).catch((err) => {
117178

118179
});
180+
119181
};
120182

183+
const logoUploadOptions = useLogoUpload({ control, watch });
184+
121185
return {
122186
controllerOptions: {
123187
initialValue: {
@@ -131,6 +195,9 @@ export const EditCompanyController = () => {
131195
control,
132196
fields,
133197
getValues,
198+
logoUploadOptions,
199+
register,
200+
errors,
134201
submit: handleSubmit(submit),
135202
},
136203
},
@@ -152,6 +219,8 @@ const EditCompanyProfileForm = ({ title }) => {
152219
control,
153220
getValues,
154221
submit,
222+
register,
223+
errors,
155224
} = useContext(EditCompanyControllerContext);
156225

157226
const classes = useStyles();
@@ -175,16 +244,11 @@ const EditCompanyProfileForm = ({ title }) => {
175244
>
176245
<Grid container spacing={4}>
177246
<Grid item xs={12}>
178-
<Box
179-
display="flex"
180-
justifyContent="center"
181-
>
182-
<EditImage
183-
image={company?.logo}
184-
style={{ height: "5em", width: "5em" }}
185-
imageAlt={`${company?.name}'s logo`}
186-
/>
187-
</Box>
247+
<EditImage
248+
image={company?.logo}
249+
imageAlt={`${company?.name}'s logo`}
250+
classname={classes.avatar}
251+
/>
188252
</Grid>
189253
<Grid item xs={12}>
190254
<Controller
@@ -254,7 +318,7 @@ const EditCompanyProfileForm = ({ title }) => {
254318
</Grid>
255319
</Grid>
256320
<Button
257-
// disabled={loading || formDisabled}
321+
disabled={loadingCompany}
258322
variant="contained"
259323
color="primary"
260324
type="submit"

src/components/Company/Registration/Finish/FinishCompanyRegistrationWidget.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
StepLabel,
1111
Stepper,
1212
Typography,
13+
Box,
1314
} from "@material-ui/core";
1415
import LogoUploadForm, { useLogoUpload } from "./LogoUploadForm";
1516
import ContactsForm, { useContacts } from "./ContactsForm";
@@ -29,11 +30,24 @@ import useSession from "../../../../hooks/useSession";
2930
import useFinishCompanyRegistrationStyles from "./finishCompanyRegistrationStyles";
3031
import Constants from "../../../../utils/Constants";
3132

33+
const ImageUploadText = () => <>
34+
<Typography variant="h6">
35+
{"Upload your Company's logo."}
36+
</Typography>
37+
<Typography variant="caption" gutterBottom paragraph>
38+
{"A picture is worth a thousand words. Is there any better way to represent your brand than your Company's logo?"}
39+
</Typography>
40+
<Box marginY={1} fontStyle="italic">
41+
<Typography variant="caption" gutterBottom paragraph>
42+
{"It should be a PNG or JPG file, with no more than 10MB."}
43+
</Typography>
44+
</Box>
45+
</>;
3246

3347
function getStepContent(step) {
3448
switch (step) {
3549
case 0:
36-
return <LogoUploadForm />;
50+
return <LogoUploadForm InfoText={ImageUploadText} />;
3751
case 1:
3852
return <BioForm />;
3953
case 2:

src/components/Company/Registration/Finish/LogoUploadForm.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ const useStyles = makeStyles((theme) => ({
1515
},
1616
}));
1717

18+
const turnImgIntoFile = (imageLink) => {
19+
fetch(imageLink).then(async (res) => {
20+
const imageBlob = res.blob();
21+
22+
return new File([imageBlob]);
23+
});
24+
};
25+
1826
export const useLogoUpload = ({ watch }) => {
1927
const [logoPreview, setLogoPreview] = useState(null);
2028
const [croppedAreaPixels, setCroppedAreaPixels] = useState(null);
@@ -31,19 +39,18 @@ export const useLogoUpload = ({ watch }) => {
3139
}, [logoInput]);
3240

3341
useEffect(() => {
34-
if (!logoInput) {
42+
if (!logoInput || typeof watch("logo") !== "object") {
3543
setLogoPreview(undefined);
3644
return () => { };
3745
} else {
38-
3946
const objectUrl = URL.createObjectURL(logoInput);
4047
setLogoPreview(objectUrl);
4148

4249
// free memory when ever this component is unmounted
4350
return () => URL.revokeObjectURL(objectUrl);
4451
}
4552

46-
}, [logoInput, setLogoPreview]);
53+
}, [logoInput, setLogoPreview, watch]);
4754

4855
const onCropComplete = (_, croppedAreaPixels) => {
4956
setCroppedAreaPixels(croppedAreaPixels);
@@ -116,7 +123,7 @@ LogoPreview.propTypes = {
116123
img: PropTypes.string,
117124
};
118125

119-
const LogoUploadForm = () => {
126+
const LogoUploadForm = ({ InfoText }) => {
120127

121128
const {
122129
logoUploadOptions,
@@ -139,17 +146,7 @@ const LogoUploadForm = () => {
139146
alignItems="center"
140147
>
141148
<Grid item xs={12}>
142-
<Typography variant="h6">
143-
{"Upload your Company's logo."}
144-
</Typography>
145-
<Typography variant="caption" gutterBottom paragraph>
146-
{"A picture is worth a thousand words. Is there any better way to represent your brand than your Company's logo?"}
147-
</Typography>
148-
<Box marginY={1} fontStyle="italic">
149-
<Typography variant="caption" gutterBottom paragraph>
150-
{"It should be a PNG or JPG file, with no more than 10MB."}
151-
</Typography>
152-
</Box>
149+
<InfoText />
153150
</Grid>
154151
<Grid item xs="auto">
155152
<input

src/components/utils/form/MultiOptionTextField.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const MultiOptionTextField = ({
7777
{...textFieldProps}
7878
/>)}
7979
control={control}
80-
defaultValue={getValues(`${controllerName}.${i}.value` || "")}
80+
defaultValue={getValues(`${controllerName}.${i}.value`) || ""}
8181
/>
8282
))}
8383
<Button

src/services/companyEditService.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,20 @@ export const editCompany = measureTime(TIMED_ACTIONS.OFFER_EDIT, async ({
2222
logo,
2323
};
2424

25+
const formData = new FormData();
26+
27+
if (logo) formData.append("logo", logo);
28+
formData.append("name", name);
29+
contacts.forEach((contact) => {
30+
formData.append("contacts", contact);
31+
});
32+
formData.append("bio", bio);
33+
2534
try {
2635
const res = await fetch(`${API_HOSTNAME}/company/${id}/edit`, {
2736
method: "PUT",
28-
headers: {
29-
"Content-Type": "application/json",
30-
},
37+
body: formData,
3138
credentials: "include",
32-
body: JSON.stringify(updatedCompany),
3339
});
3440
const json = await res.json();
3541

0 commit comments

Comments
 (0)