@@ -24,25 +24,73 @@ import { Redirect, useLocation, useParams } from "react-router-dom/cjs/react-rou
2424import { yupResolver } from "@hookform/resolvers/yup" ;
2525import EditCompanySchema from "./EditCompanySchema" ;
2626import MultiOptionTextField from "../../utils/form/MultiOptionTextField" ;
27- import LogoUploadForm from "../Registration/Finish/LogoUploadForm" ;
27+ import LogoUploadForm , { turnImgIntoFile } from "../Registration/Finish/LogoUploadForm" ;
2828import { 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
4034const 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+
4694const 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+
64117export 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"
0 commit comments