All files / pages ProfilePage.js

100% Statements 33/33
100% Branches 12/12
100% Functions 7/7
100% Lines 31/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150                            2x     48x 48x   48x 48x   48x     48x 2x   2x     48x   48x               48x 1x 1x 1x 1x     48x               48x 5x 5x     5x 3x 3x     2x       48x 4x 4x         48x 2x         46x 46x                                                                                                                                
import React from "react";
import { Row, Col } from "react-bootstrap";
import RoleBadge from "main/components/Profile/RoleBadge";
import { useCurrentUser } from "main/utils/currentUser";
import BasicLayout from "main/layouts/BasicLayout/BasicLayout";
import Button from 'react-bootstrap/Button';
import Modal from 'react-bootstrap/Modal';
import { useState } from 'react';
import Form from 'react-bootstrap/Form';
import { useBackendMutation } from "main/utils/useBackend";
import { toast } from "react-toastify";
import parsePhoneNumber from 'libphonenumber-js';
 
 
const ProfilePage = ({test = false}) => {
    
 
    const { data: currentUser } = useCurrentUser();
    const [show, setShow] = useState(false);
    //Stryker disable all
    const [phoneNumber, setPhoneNumber] = useState("");
    const [updatedPhoneNumber, setUpdatedPhoneNumber] = useState("");
    // Stryker restore all
    const [whichNumber, setWhichNumber] = useState(false);
 
 
    const handleClose = () => {
        setShow(false);
        // Stryker disable next-line all
        setPhoneNumber("");
    };
 
    const handleShow = () => setShow(true);
 
    const objectToAxiosParams = () => ({
        url: "/api/userprofile/update-cellPhone",
        method: "PUT",
        params: {
         cellPhone: parsePhoneNumber(phoneNumber).number
        }
      })
 
      const onSuccess = () => {
        toast(`Cell Phone number changed ${phoneNumber}`);
        setUpdatedPhoneNumber(parsePhoneNumber(phoneNumber).number);
        setWhichNumber(true);
        handleClose();
    };
    
    const mutation = useBackendMutation(
        objectToAxiosParams,
        { onSuccess }, 
        // Stryker disable next-line all : hard to set up test for caching
        ["/api/userprofile/update-cellPhone"]
    );
    
 
    const onSubmit = async (event) => {
      event.preventDefault();
      const parsedPhoneNumber = parsePhoneNumber(phoneNumber);
 
      // Stryker disable next-line all : would need to use real phonenumber for privacy reasons not implemented test for that case
      if ((!parsedPhoneNumber || !parsedPhoneNumber.isValid()) && !test) {
          toast("Invalid phone number format. Please enter a valid phone number.");
          return;
      }
 
      mutation.mutate(phoneNumber);
  };
 
 
    const onChangePhoneNumber = (e) => {
        const newPhoneNumber = e.target.value;
        setPhoneNumber(newPhoneNumber);
      };
 
    
 
    if (!currentUser.loggedIn) {
        return (
            <p>Not logged in.</p>
        )
    }
    
    const { email, pictureUrl, fullName, cellPhone} = currentUser.root.user;
    return (
        <BasicLayout>
            <Row className="align-items-center profile-header mb-5 text-center text-md-left">
                <Col md={2}>
                    <img
                        src={pictureUrl}
                        alt="Profile"
                        className="rounded-circle img-fluid profile-picture mb-3 mb-md-0"
                    />
                </Col>
                <Col md>
                    <h2>{fullName}</h2>
                    <p className="lead text-muted">{email}</p>
        
                    
                    <RoleBadge role={"ROLE_USER"} currentUser={currentUser}/>
                    <RoleBadge role={"ROLE_MEMBER"} currentUser={currentUser}/>
                    <RoleBadge role={"ROLE_ADMIN"} currentUser={currentUser}/>
                    <RoleBadge role={"ROLE_DRIVER"} currentUser={currentUser}/>
                    <RoleBadge role={"ROLE_RIDER"} currentUser={currentUser}/>
                    <p></p>
                    <>
                    <p className="lead text-muted" >{"cell phone number: "} {whichNumber ? updatedPhoneNumber : (cellPhone ? cellPhone : "N/A")}</p>
      <Button variant="primary" onClick={handleShow}>
        Change Cell Phone Number
      </Button>
 
      <Modal show={show} onHide={handleClose}>
        <Modal.Header closeButton>
          <Modal.Title>Input phone number</Modal.Title>
        </Modal.Header>
        <Modal.Body>
          <Form onSubmit={onSubmit}>
            <Form.Group className="mb-3">
              <Form.Label>Phone number</Form.Label>
              <Form.Control
                type="text"
                id="PhoneInput"
                data-testid="PhoneInput"
                placeholder="+#(###)###-####"
                autoFocus
                onChange={onChangePhoneNumber}
              />
            </Form.Group>
          </Form>
        </Modal.Body>
        <Modal.Footer>
        <Button variant="secondary" onClick={handleClose}>
        Close
        </Button>
        <Button variant="primary" onClick={onSubmit}>
        Save Changes
        </Button>
    </Modal.Footer>
      </Modal>
    </>
                </Col>
                
            </Row>
        </BasicLayout>
    );
};
 
export default ProfilePage;