Advertisement
Kevin_Zhang

Untitled

Mar 16th, 2023
559
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { useState, useEffect } from "react";
  2. import styled from "@emotion/styled";
  3. import FormControl from '@mui/material/FormControl';
  4. import OutlinedInput from '@mui/material/OutlinedInput';
  5.  
  6. import TextField from '@mui/material/TextField';
  7. import FormHelperText from '@mui/material/FormHelperText';
  8.  
  9. import InputAdornment from '@mui/material/InputAdornment';
  10. import IconButton from '@mui/material/IconButton';
  11. import VisibilityOff from '@mui/icons-material/VisibilityOff';
  12. import Visibility from '@mui/icons-material/Visibility';
  13.  
  14. import FullScreen from '../components/FullScreen';
  15. import Content from '../components/Content';
  16. import Button from '../components/Button';
  17. import { useLoggerContext } from "../contexts/Logger"
  18. import { useAPIContext } from "../contexts/API"
  19.  
  20. const Container = styled(Content)`
  21.   background-color: #494949;
  22.   width: 85%;
  23.   max-width: 600px;
  24.   padding: 30px calc(min(100px, 10%));
  25. `;
  26.  
  27. const formStyle = {
  28.   width: '100%',
  29.   "& .MuiInputBase-input": {
  30.     backgroundColor: '#fff'
  31.   },
  32.   '& fieldset': { borderRadius: 0 },
  33.   "& .MuiFormHelperText-root": { //<--- here
  34.     width: '50%',
  35.     //fontSize: 12,
  36.     color: '#fff',
  37.     height: 0
  38.   }
  39. };
  40.  
  41. export default function Register(){
  42.   const [username, setUsername] = useState("");
  43.   const [password, setPassword] = useState("");
  44.   const [fetching, setFetching] = useState(false);
  45.   const { setLogState } = useLoggerContext();
  46.   const { setCookie, request } = useAPIContext();
  47.   const [showPassword, setShowPassword] = useState(false);
  48.  
  49.   const [formatCorrect, setFormatCorrect] = useState(false);
  50.  
  51.   function submit() {
  52.     if (check_email_format(username)) {
  53.       register();
  54.     }
  55.     else
  56.       unsuccessful("信箱格式錯誤");
  57.   }
  58.  
  59.   function keypress({ key }){
  60.     if (key === 'Enter'){
  61.       submit();
  62.     }
  63.   }
  64.  
  65.   function unsuccessful(reason){
  66.     setLogState({
  67.       severity: 'error',
  68.       content: reason
  69.     });
  70.   }
  71.  
  72.   function check_email_format(s){
  73.     const email_regex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/gi;
  74.    return email_regex.test(s);
  75.  }
  76.  
  77.  
  78.  const handleOnInput = (e) => {
  79.    setUsername(e.target.value);
  80.    setFormatCorrect(check_email_format(e.target.value));
  81.  };
  82.  
  83.  // when trigered by clicking or enter,
  84.  // this function set fetching to true, and rerender the page
  85.  // and when every time we call this as a routine to check whether a request job exist.
  86.  
  87.  async function register(){
  88.    setFetching(true);
  89.    try{
  90.      // TODO connect to correct endpoint
  91.      // TODO check if this email exists in DB already
  92.      let v = 1;
  93.      while (v < 1000000000) {
  94.        ++v;
  95.        if (v % 10000000 == 0)
  96.          console.log(v);
  97.      }
  98.      let res = await request('/authentication', {
  99.        method: 'POST',
  100.        body: JSON.stringify({ username, password })
  101.      });
  102.  
  103.      if(res.ok){
  104.        res = await res.json();
  105.        console.log(res);
  106.        if(res.status !== 'Success') return unsuccessful("Error2");
  107.        setCookie('user', res.token);
  108.      }else unsuccessful('Error1');
  109.  
  110.    }finally{
  111.      setFetching(false);
  112.    }
  113.  }
  114.  
  115.  return (
  116.    <FullScreen>
  117.      <Container>
  118.  
  119.        <h2>信箱</h2>
  120.  
  121.        <FormControl sx={formStyle} variant='outlined'>
  122.          <TextField
  123.            error={!formatCorrect && username !== ''}
  124.            placeholder='example@domain.com'
  125.            autoComplete='email'
  126.            autoFocus
  127.            onKeyDown={keypress}
  128.            type='email'
  129.            value={username}
  130.            size="small"
  131.            inputProps={{
  132.              min: "0",
  133.              onInput: handleOnInput
  134.            }}
  135.            variant='outlined'
  136.          />
  137.        </FormControl>
  138.  
  139.        <Button
  140.          style={{ color: '#494949', marginTop: '25px' }}
  141.          variant="contained"
  142.          color="secondary"
  143.          onClick={submit}
  144.          disabled={fetching}
  145.          disabledColor="#ffd800"
  146.          loading={fetching}
  147.          // disabled={username.length * password.length === 0}
  148.        >註冊</Button>
  149.  
  150.      </Container>
  151.    </FullScreen>
  152.  );
  153. }
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement