Blame view

components/user/MyProfile.js 32.5 KB
1 2 3 4 5 6
import { Field, Formik } from 'formik';
import Image from 'next/image';
import React, { Fragment, useEffect, useRef, useState } from 'react';
import { Button, Form, Table } from 'react-bootstrap';
import * as Yup from "yup";
import { renderImage } from '../../services/imageHandling';
Ravindra Kanojiya committed
7
import { useSelector } from 'react-redux';
8 9 10 11

const MyProfile = () => {
    let formikUploadImage;
    const [readOnly, setReadOnly] = useState(true);
Ravindra Kanojiya committed
12 13
    const { endUser } = useSelector(state => state.endUser);
    console.log("users info", endUser);
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
    const profileValidationSchema = Yup.object().shape({
        first_name: Yup.string()
            .required("First Name is Required"),
        last_name: Yup.string()
            .required("Last Name is Required"),
        email: Yup.string()
            .required("Email Id is Required")
            .email("Please Enter An Valid Email Id"),
        country_code: Yup.string()
            .required("Country Code is Required"),
        mobile: Yup.string()
            .required("Mobile Number is Required")
            .matches(/^[0-9\s]+$/, "Please Enter Correct Mobile No."),
        password: Yup.string()
    });

    const addressesValidationSchema = Yup.object().shape({
        addresses: Yup.array().of(
            Yup.object().shape({
                pincode: Yup.string().required('Pincode is required'),
                country: Yup.string().required('Country is required'),
                state: Yup.string().required('State is required'),
                city: Yup.string().required('City is required'),
Jyotsna committed
37 38
                addressLine1: Yup.string().required('Address Line1 is required'),
                addressLine2: Yup.string().required('Adrress Line2 is required')
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
            })
        )
    });

    const profileInitialValue = {
        first_name: "John",
        last_name: "Doe",
        email: "john@gmail.com",
        country_code: "+1 (406)",
        mobile: "785-9909",
        password: "*********"
    }

    // Ref for the first input field
    const firstFieldRef = useRef(null);

    // Effect to focus on the first field when entering edit mode
    useEffect(() => {
        console.log("firstFieldRef", readOnly, firstFieldRef.current)
        if (!readOnly && firstFieldRef.current) {
            firstFieldRef.current.focus();
        }
    }, [readOnly]);

    const handleProfileUploadChange = (event) => {
        console.log(event)
        console.log(event?.currentTarget?.files[0])
        formikUploadImage.setFieldValue("profile_image", event.currentTarget.files[0]);
        formikUploadImage.submitForm()
    }

    return (
        <Fragment>
            <div className="container">
                <div className="row">
                    <div className="col-12 col-lg-12">
                        <div className="form-container content-wraaper">
                            <div className='px-5'>
                                <h2>My Profile</h2>


Ravindra Kanojiya committed
80
                                {/* <Table className='abc'>
Jyotsna committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
                                    <thead>
                                        <tr>
                                            <th>1</th>
                                            <th>2</th>
                                            <th>3</th>
                                            <th>4</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr className='main'>
                                            <td rowSpan={3}>1</td>
                                        </tr>
                                        <tr className='sub1'>
                                            <td>11</td>
                                            <td>22</td>
                                            <td>33</td>
                                        </tr>
                                        <tr className='sub2'>
                                            <td>11</td>
                                            <td>22</td>
                                            <td>33</td>
                                        </tr>
103

Jyotsna committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117
                                        <tr className='main'>
                                            <td rowSpan={3}>1</td>
                                        </tr>
                                        <tr className='sub'>
                                            <td>11</td>
                                            <td>22</td>
                                            <td>33</td>
                                        </tr>
                                        <tr className='sub'>
                                            <td>11</td>
                                            <td>22</td>
                                            <td>33</td>
                                        </tr>
                                    </tbody>
Ravindra Kanojiya committed
118
                                </Table> */}
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
                                <div className="bg-white border-div">
                                    <Formik
                                        initialValues={{ profile_image: null }}
                                        validationSchema={Yup.object().shape({
                                            profile_image: Yup.mixed().required("Profile Image is Required")
                                        })}
                                        enableReinitialize={true}
                                        onSubmit={values => {
                                            console.log("profile image", values)

                                            // onSubmit: async (values, { setSubmitting }) => {
                                            //     setSubmitting(true);
                                            //     try {
                                            //       const formData = new FormData();
                                            //       formData.append("file", values.file);
Jyotsna committed
134

135 136 137 138 139 140
                                            //       // Use Axios to send the file data to the server
                                            //       const response = await axios.post("/upload", formData, {
                                            //         headers: {
                                            //           "Content-Type": "multipart/form-data"
                                            //         }
                                            //       });
Jyotsna committed
141

142 143 144 145 146 147 148 149 150 151
                                            //       console.log("File uploaded successfully:", response.data);
                                            //     } catch (error) {
                                            //       console.error("Error uploading file:", error);
                                            //     }
                                            //     setSubmitting(false);
                                            //   }
                                        }}
                                        innerRef={formik => {
                                            // Store Formik instance in a variable
                                            formikUploadImage = formik;
Jyotsna committed
152
                                        }}
153 154 155 156 157 158 159 160 161
                                    >
                                        {({ values, errors, touched, handleChange, handleBlur, handleSubmit }) => (
                                            <Form
                                                onSubmit={e => {
                                                    e.preventDefault();
                                                    handleSubmit();
                                                    console.log(errors)
                                                }}
                                            >
Ravindra Kanojiya committed
162 163
                                                <div className="row align-items-center justify-content-between">
                                                    <div className="col-lg-12 d-flex align-items-center">
Ravindra Kanojiya committed
164
                                                        <span className="image-container me-4 profile-img-s">
Ravindra Kanojiya committed
165
                                                            <Image layout="fill" alt="" src="/images/user/user-big.jpg" className="image" />
Jyotsna committed
166
                                                        </span>
Ravindra Kanojiya committed
167 168 169
                                                        <p className="username">
                                                            {endUser?.attributes?.name}
                                                        </p>
170
                                                    </div>
Ravindra Kanojiya committed
171
                                                    {/* <div className="col-lg-4">
Jyotsna committed
172 173 174
                                                        <label className="uploadProfileImage" htmlFor="file-upload">
                                                            <p className="mb-0">Upload Profile Photo</p>
                                                            <Image alt="" src="/images/user/icon-upload.svg" width="24" height="24" className="pb-1" />
175 176 177 178 179 180 181 182
                                                        </label>
                                                        <input
                                                            type="file"
                                                            id="file-upload"
                                                            name="profile_image"
                                                            onChange={handleProfileUploadChange}
                                                            // onBlur={handleBlur}
                                                            style={{ display: "none" }}
Jyotsna committed
183 184 185 186
                                                            onClick={event => {
                                                                const { target = {} } = event || {};
                                                                target.value = "";
                                                            }}
187 188
                                                        />
                                                        {errors.profile_image && touched.profile_image && (<span className="form-error">{errors.profile_image}</span>)}
Ravindra Kanojiya committed
189
                                                    </div> */}
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
                                                </div>
                                            </Form>
                                        )}
                                    </Formik>
                                </div>
                                <div className="bg-white border-div">
                                    <Formik
                                        initialValues={profileInitialValue}
                                        validationSchema={profileValidationSchema}
                                        enableReinitialize={true}
                                        onSubmit={values => {
                                            console.log("profile personal details", values)
                                        }}
                                    >
                                        {({ values, errors, touched, handleChange, handleBlur, handleSubmit }) => (
                                            <Form
                                                onSubmit={e => {
                                                    e.preventDefault();
                                                    handleSubmit();
                                                }}
                                            >
                                                <>
                                                    <div className="d-flex justify-content-between mb-4">
                                                        <h2 className="mb-0">Personal Information</h2>
                                                        {/* onClick={() => setReadOnly(prevState => !prevState)} */}
Ravindra Kanojiya committed
215
                                                        {/* {readOnly && (
216 217 218 219
                                                            <div className="btn-edit">
                                                                <span className="me-3">Edit</span>
                                                                <i className="fa fa-edit"></i>
                                                            </div>
Ravindra Kanojiya committed
220
                                                        )} */}
221 222 223 224
                                                    </div>
                                                    <div className="row">
                                                        <div className="col-12 col-lg-5">
                                                            <div className="input-group">
Ravindra Kanojiya committed
225
                                                                <label>Your Full Name</label>
226 227 228 229 230
                                                                <Field
                                                                    type="text"
                                                                    name="first_name"
                                                                    onChange={handleChange}
                                                                    onBlur={handleBlur}
Ravindra Kanojiya committed
231
                                                                    value={endUser?.attributes?.name}
232 233 234
                                                                    readOnly={readOnly}
                                                                    innerRef={firstFieldRef}
                                                                />
Ravindra Kanojiya committed
235
                                                                {/* {errors.first_name && touched.first_name && (<span className="form-error">{errors.first_name}</span>)} */}
236 237
                                                            </div>
                                                        </div>
Ravindra Kanojiya committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
                                                        <div className="col-12 col-lg-5">
                                                            <div className="input-group">
                                                                <label>Email Id</label>
                                                                <Field
                                                                    type="text"
                                                                    name="email"
                                                                    onChange={handleChange}
                                                                    onBlur={handleBlur}
                                                                    value={endUser?.attributes?.email}
                                                                    readOnly={readOnly}
                                                                />
                                                                {/* {errors.email && touched.email && (<span className="form-error">{errors.email}</span>)} */}
                                                            </div>
                                                        </div>
                                                        {/* <div className="col-12 offset-lg-1 col-lg-5">
253 254 255 256 257 258 259 260 261 262 263 264
                                                            <div className="input-group">
                                                                <label>Your Last Name</label>
                                                                <Field
                                                                    type="text"
                                                                    name="last_name"
                                                                    onChange={handleChange}
                                                                    onBlur={handleBlur}
                                                                    value={values.last_name}
                                                                    readOnly={readOnly}
                                                                />
                                                                {errors.last_name && touched.last_name && (<span className="form-error">{errors.last_name}</span>)}
                                                            </div>
Ravindra Kanojiya committed
265
                                                        </div> */}
266 267
                                                    </div>
                                                    <div className="row">
Ravindra Kanojiya committed
268
                                                        {/* <div className="col-12 col-lg-5">
269 270 271 272 273 274 275 276 277 278 279 280
                                                            <div className="input-group">
                                                                <label>Email Id</label>
                                                                <Field
                                                                    type="text"
                                                                    name="email"
                                                                    onChange={handleChange}
                                                                    onBlur={handleBlur}
                                                                    value={values.email}
                                                                    readOnly={readOnly}
                                                                />
                                                                {errors.email && touched.email && (<span className="form-error">{errors.email}</span>)}
                                                            </div>
Ravindra Kanojiya committed
281 282
                                                        </div> */}
                                                        <div className="col-12 col-lg-5">
283 284 285
                                                            <div className="input-group">
                                                                <label>Mobile Number</label>
                                                                <div className="contact-number">
Ravindra Kanojiya committed
286
                                                                    {/* {readOnly && (
287 288 289 290 291 292 293 294 295
                                                                        <Field
                                                                            type="text"
                                                                            name="country_code"
                                                                            onChange={handleChange}
                                                                            onBlur={handleBlur}
                                                                            value={values.country_code}
                                                                            readOnly={readOnly}
                                                                            style={{ width: "80px" }}
                                                                        />
Ravindra Kanojiya committed
296
                                                                    )} */}
297 298 299 300 301 302 303 304 305 306 307 308 309
                                                                    {!readOnly && (
                                                                        <select
                                                                            id="country_code"
                                                                            name="country_code"
                                                                            onChange={handleChange}
                                                                            onBlur={handleBlur}
                                                                            style={{ width: "80px" }}
                                                                        >
                                                                            <option value="+91">+91</option>
                                                                            <option value="+44">+44</option>
                                                                        </select>
                                                                    )}
                                                                    <Field
Ravindra Kanojiya committed
310
                                                                    className='m-0'
311 312 313 314
                                                                        type="text"
                                                                        name="mobile"
                                                                        onChange={handleChange}
                                                                        onBlur={handleBlur}
Ravindra Kanojiya committed
315
                                                                        value={endUser?.attributes?.phone}
316 317 318
                                                                        readOnly={readOnly}
                                                                    />
                                                                </div>
Ravindra Kanojiya committed
319
                                                                {/* {errors.mobile && touched.mobile && (<span className="form-error">{errors.mobile}</span>)} */}
320 321 322
                                                            </div>
                                                        </div>
                                                    </div>
Ravindra Kanojiya committed
323
                                                    {/* <div className="row">
324 325 326 327 328 329 330 331 332 333 334 335 336 337
                                                        <div className="col-12 col-lg-5">
                                                            <div className="input-group">
                                                                <label>Current Password</label>
                                                                <input
                                                                    type="text"
                                                                    name="password"
                                                                    onChange={handleChange}
                                                                    onBlur={handleBlur}
                                                                    value={values.password}
                                                                    readOnly={readOnly}
                                                                    disabled={true}
                                                                />
                                                            </div>
                                                        </div>
Ravindra Kanojiya committed
338
                                                    </div> */}
339 340 341 342 343 344 345
                                                </>
                                            </Form>
                                        )}
                                    </Formik>
                                </div>


Ravindra Kanojiya committed
346
                                {/* <div className="bg-white border-div">
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
                                    <Formik
                                        initialValues={{
                                            addresses: [
                                                {
                                                    pincode: "",

                                                }
                                            ]
                                        }}
                                        validationSchema={addressesValidationSchema}
                                        // enableReinitialize={true}
                                        onSubmit={values => {
                                            console.log("profile address details", values)
                                        }}
                                    >
                                        {({ values, errors, touched, handleChange, handleBlur, handleSubmit }) => (
                                            <Form
                                                onSubmit={e => {
                                                    e.preventDefault();
                                                    handleSubmit();
                                                }}
                                            >
                                                <>
                                                    <div className="d-flex justify-content-between mb-4">
                                                        <h2 className='mb-0'>Address</h2>
                                                        <div className="btn-edit">
                                                            <span className="me-3">Add</span>
                                                            <i className="fa fa-plus"></i>
                                                        </div>
                                                    </div>
                                                    <div className="row">
                                                        <div className="col-12 col-lg-5">
                                                            <div className="input-group">
                                                                <label>Pincode</label>
                                                                <input
                                                                    type="text"
                                                                    name="pincode"
                                                                    onChange={handleChange}
                                                                    onBlur={handleBlur}
                                                                    value={values.pincode}
                                                                />
                                                                {errors.pincode && touched.pincode && (<span className="form-error">{errors.pincode}</span>)}
                                                            </div>
                                                        </div>
                                                        <div className="col-12 offset-lg-1 col-lg-5">
                                                            <div className="input-group">
                                                                <label>Country</label>
                                                                <select
                                                                    id="country"
                                                                    name="country"
                                                                    onChange={handleChange}
                                                                    onBlur={handleBlur}
                                                                >
                                                                    <option value="India">India</option>
                                                                    <option value="America">America</option>
                                                                </select>
                                                                {errors.country && touched.country && (<span className="form-error">{errors.country}</span>)}
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div className="row">
                                                        <div className="col-12 col-lg-5">
                                                            <div className="input-group">
                                                                <label>State</label>
                                                                <select
                                                                    id="state"
                                                                    name="state"
                                                                    onChange={handleChange}
                                                                    onBlur={handleBlur}
                                                                >
                                                                    <option value="India">India</option>
                                                                    <option value="America">America</option>
                                                                </select>
                                                                {errors.state && touched.state && (<span className="form-error">{errors.state}</span>)}
                                                            </div>
                                                        </div>
                                                        <div className="col-12 offset-lg-1 col-lg-5">
                                                            <div className="input-group">
                                                                <label>City</label>
                                                                <select
                                                                    id="city"
                                                                    name="city"
                                                                    onChange={handleChange}
                                                                    onBlur={handleBlur}
                                                                >
                                                                    <option value="India">India</option>
                                                                    <option value="America">America</option>
                                                                </select>
                                                                {errors.city && touched.city && (<span className="form-error">{errors.city}</span>)}
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div className="row">
                                                        <div className="col-12 col-lg-5">
                                                            <div className="input-group">
                                                                <label>Address Line 1</label>
                                                                <input
                                                                    type="text"
Jyotsna committed
445
                                                                    name="addressLine1"
446 447
                                                                    onChange={handleChange}
                                                                    onBlur={handleBlur}
Jyotsna committed
448
                                                                    value={values.addressLine1}
449
                                                                />
Jyotsna committed
450
                                                                {errors.addressLine1 && touched.addressLine1 && (<span className="form-error">{errors.addressLine1}</span>)}
451 452 453 454 455 456 457
                                                            </div>
                                                        </div>
                                                        <div className="col-12 offset-lg-1 col-lg-5">
                                                            <div className="input-group">
                                                                <label>Address Line 2</label>
                                                                <input
                                                                    type="text"
Jyotsna committed
458
                                                                    name="addressLine2"
459 460
                                                                    onChange={handleChange}
                                                                    onBlur={handleBlur}
Jyotsna committed
461
                                                                    value={values.addressLine2}
462
                                                                />
Jyotsna committed
463
                                                                {errors.addressLine2 && touched.addressLine2 && (<span className="form-error">{errors.addressLine2}</span>)}
464 465 466 467 468 469 470
                                                            </div>
                                                        </div>
                                                    </div>
                                                </>
                                            </Form>
                                        )}
                                    </Formik>
Ravindra Kanojiya committed
471
                                </div> */}
472 473 474 475 476 477 478 479 480 481
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </Fragment>
    )
}

export default MyProfile;