Skip to content
Snippets Groups Projects
Main.jsx 5.97 KiB
Newer Older
Bo-Chun Chen's avatar
Bo-Chun Chen committed
import Button from 'react-bootstrap/Button';
import Container from 'react-bootstrap/Container';
import Form from 'react-bootstrap/Form';
Krish Moodbidri's avatar
Krish Moodbidri committed
import io from 'socket.io-client'
Bo-Chun Chen's avatar
Bo-Chun Chen committed
import Modal from 'react-bootstrap/Modal';
import Nav from 'react-bootstrap/Nav';
import Navbar from 'react-bootstrap/Navbar';
import React, { useRef, useState, useEffect } from 'react';
import Spinner from 'react-bootstrap/Spinner';
import { faInfoCircle } from '@fortawesome/free-solid-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
const DEBUG = true;
Bo-Chun Chen's avatar
Bo-Chun Chen committed
// use REMOTE_USER for production
const username = 'louistw';
const params = new URLSearchParams(window.location.search);
const redir_url = params.get('redir') || '/pun/sys/dashboard';
Bo-Chun Chen's avatar
Bo-Chun Chen committed
function Main(props) {
  // Setup states
  const [isLoading, setIsLoading] = useState(false);
  const [validated, setValidated] = useState(false);
  const [modalTitle, setModalTitle] = useState("Your account request has been submitted.");
  const [modalBody, setModalBody] = useState(<Spinner animation="border" variant="success" />);
  const [aup, setAup] = useState(false);
Bo-Chun Chen's avatar
Bo-Chun Chen committed
  // Setup refs for easy access
Bo-Chun Chen's avatar
Bo-Chun Chen committed
  const socketRef = useRef(null);
Bo-Chun Chen's avatar
Bo-Chun Chen committed
  const firstName = useRef(null);
  const lastName = useRef(null);
  const reason = useRef(null);
  const blazerid = useRef(username);
Bo-Chun Chen's avatar
Bo-Chun Chen committed
  // Define functions
  const hideModal = () => setIsLoading(false);
  const showModal = () => setIsLoading(true);
  const handleAup = (e) => {
    setAup(!aup)
  }
Bo-Chun Chen's avatar
Bo-Chun Chen committed
  const handleSubmit = (e) => {
    e.preventDefault();
Bo-Chun Chen's avatar
Bo-Chun Chen committed
    const form = e.currentTarget;
    setValidated(true);
    if (form.checkValidity() === false) {
      e.stopPropagation();
    } else {
      showModal();
Bo-Chun Chen's avatar
Bo-Chun Chen committed
      let message = {
        'username': blazerid.current.value,
Bo-Chun Chen's avatar
Bo-Chun Chen committed
        'firstName': firstName.current.value,
        'lastName': lastName.current.value,
        'reason': reason.current.value,
        'aup': aup
Bo-Chun Chen's avatar
Bo-Chun Chen committed
      };
      if (DEBUG) console.log('Modal showed')
      if (DEBUG) console.log('Message:')
      if (DEBUG) console.log(message)
      socketRef.current.emit('request', message);
Bo-Chun Chen's avatar
Bo-Chun Chen committed
  useEffect(() => {
Bo-Chun Chen's avatar
Bo-Chun Chen committed
    const socket = socketRef.current = socketRef.current || io();

    if (DEBUG) console.log(socket)
Bo-Chun Chen's avatar
Bo-Chun Chen committed
    // Define socketio events
    socket.on('message', data => {
      if (DEBUG) console.log(data + ' ' + socket.id);
Bo-Chun Chen's avatar
Bo-Chun Chen committed
    });
    socket.on('connect', data => {
      if (DEBUG) console.log('on connect: ' + socket.id);
Bo-Chun Chen's avatar
Bo-Chun Chen committed
      socket.emit('join_room', {username});
Bo-Chun Chen's avatar
Bo-Chun Chen committed
    });
    socket.on('requested', data => {
      if (DEBUG) console.log('Account requested');
Bo-Chun Chen's avatar
Bo-Chun Chen committed
      showModal();
    });
    socket.on('created', data => {
      if (DEBUG) console.log('account has been created.')
Bo-Chun Chen's avatar
Bo-Chun Chen committed
      let sec = 3;
      let myTimer;
Bo-Chun Chen's avatar
Bo-Chun Chen committed
      (function countdown () {
        setModalTitle("Your account is ready!");
        setModalBody(`Will redirect in ${sec--} seconds.`);
        myTimer = setTimeout(countdown, 1000);
        if (sec < 0)
          clearTimeout(myTimer);
Bo-Chun Chen's avatar
Bo-Chun Chen committed
      })()
      setTimeout(() => {
        if (DEBUG) console.log('Redirecting to ' + redir_url)
        window.location = redir_url;
Bo-Chun Chen's avatar
Bo-Chun Chen committed
      }, sec * 1000);
    });
Bo-Chun Chen's avatar
Bo-Chun Chen committed
    return () => socket.disconnect();
  }, []);
Bo-Chun Chen's avatar
Bo-Chun Chen committed
  return (
    <>
      <Navbar className="mb-4" bg="dark" variant="dark">
        <Container fluid>
          <Navbar.Brand href="#home">Self Registration</Navbar.Brand>
          <Navbar.Toggle aria-controls="basic-navbar-nav" />
          <Navbar.Collapse id="basic-navbar-nav">
            <Nav className="ml-auto">
              <Nav.Link href="#docs">
                <FontAwesomeIcon icon={faInfoCircle} /> Online Documentation
              </Nav.Link>
            </Nav>
          </Navbar.Collapse>
        </Container>
      </Navbar>
Bo-Chun Chen's avatar
Bo-Chun Chen committed
      <Container className="col-4">
        <Form noValidate validated={validated} onSubmit={handleSubmit}>
          <Form.Group controlId="formBlazerID">
            <Form.Label>BlazerID</Form.Label>
            <Form.Control required defaultValue={username} ref={blazerid} type="text" placeholder="BlazerID" />
            <Form.Control.Feedback type="invalid">
              Required
            </Form.Control.Feedback>
          </Form.Group>
Bo-Chun Chen's avatar
Bo-Chun Chen committed
          <Form.Group controlId="formFirstName">
            <Form.Label>First Name</Form.Label>
            <Form.Control required ref={firstName} type="text" placeholder="First Name" />
            <Form.Control.Feedback type="invalid">
              Required
            </Form.Control.Feedback>
          </Form.Group>
          <Form.Group controlId="formLastName">
            <Form.Label>Last Name</Form.Label>
            <Form.Control required ref={lastName} type="text" placeholder="Last Name" />
            <Form.Control.Feedback type="invalid">
              Required
            </Form.Control.Feedback>
          </Form.Group>
          <Form.Group controlId="formReason">
            <Form.Label>Reason</Form.Label>
            <Form.Control ref={reason} type="text" placeholder="Reason" />
          </Form.Group>
          <Form.Group controlId="formAUP">
            <Form.Check onClick={handleAup} type="checkbox" label="Agree to proceed" />
            <Form.Control.Feedback type="invalid">
              Required
            </Form.Control.Feedback>
          </Form.Group>
Bo-Chun Chen's avatar
Bo-Chun Chen committed
          <Form.Group controlId="formReason">
            <Button disabled={!aup} variant="success" type="submit">
Bo-Chun Chen's avatar
Bo-Chun Chen committed
              Submit
            </Button>
          </Form.Group>
        </Form>
      </Container>
      <Modal
        show={isLoading}
        onHide={hideModal}
        backdrop="static"
        animation={false}
        keyboard={false}
        centered
      >
        <Modal.Header>
          <Modal.Title>{modalTitle}</Modal.Title>
        </Modal.Header>
        <Modal.Body className="mx-auto">
          {modalBody}
        </Modal.Body>
        <Modal.Footer>
          <Button variant="secondary" onClick={hideModal}>
            Close
          </Button>
          <Button variant="success">Understood</Button>
        </Modal.Footer>
      </Modal>
    </>
  )
Krish Moodbidri's avatar
Krish Moodbidri committed
}

export default Main;