Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as React from 'react'
  2. import {Layout, Fluid, Loading} from '../../components'
  3. import styled from 'styled-components'
  4. import TextField from '@material-ui/core/TextField'
  5. import Button from '@material-ui/core/Button'
  6. import SendIcon from '@material-ui/icons/Send'
  7. import CircularProgress from '@material-ui/core/CircularProgress'
  8. import fetch from 'isomorphic-unfetch'
  9. import md5 from 'md5-hash'
  10.  
  11. const LoginPanel = styled.main`
  12.     ${props => props.active ? null : `display: none;`}
  13. `
  14.  
  15. const Index = () => {
  16.     const [values, setValues] = React.useState({
  17.         login: '',
  18.         password: ''
  19.     })
  20.     const [state, setState] = React.useState(false)
  21.     const [result, setResult] = React.useState({
  22.         result: undefined
  23.     })
  24.    
  25.     const checkData = async (profile) => {
  26.         console.log(profile)
  27.         if (profile === {}) {
  28.             setResult({result: false})
  29.         } else {
  30.             setResult({result: true})
  31.         }
  32.     }
  33.  
  34.     const fetchData = async () => {
  35.         const pageRequest = `https://school-vol.mdcholewka.now.sh/api/checkAdmin?login=${values.login}?password=${md5(values.password)}`
  36.         const res = await fetch(pageRequest)
  37.         const json = res.json()
  38.         json.then(checkData(json))
  39.     }
  40.  
  41.     const handleChange = name => event => {
  42.         setValues({...values, [name]: event.target.value})
  43.     }
  44.  
  45.     return (
  46.         <Layout title='Panel administratora'>
  47.             <Loading isRouteChanging={state} loadingKey={123} />
  48.             <Fluid padding={true} paddingTop={true}>
  49.                 <LoginPanel active={(result.result === undefined || result.result === false) ? true : false}>
  50.                     <h1>Zaloguj się, aby potwierdzić, że jesteś administratorem</h1>
  51.                     <TextField onChange={handleChange('login')} label='Nazwa użytkownika' value={values.login} />
  52.                     <TextField onChange={handleChange('password')} name='password' label='Hasło' value={values.password} type='password' /><br /><br />
  53.                     <Button type="submit" variant="contained" size="medium" disabled={state} startIcon={<SendIcon />} color="primary" onClick={() => { setState(!state); fetchData(); }}>{state ? <><CircularProgress size={18} /><span style={{marginLeft: 10}}>Wysyłanie...</span></> : 'Wyślij'}</Button>
  54.                     {result === false ? `Błędne dane. Spróbuj jeszcze raz.` : null}
  55.                 </LoginPanel>
  56.             </Fluid>
  57.         </Layout>
  58.     )
  59. }
  60.  
  61. export default Index
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement