aboutsummaryrefslogtreecommitdiff
path: root/vnext/src/ui/Login.js
blob: ed0e990c780b7d9351a3422d8016548e0b443485 (plain) (blame)
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
import { useEffect } from 'react'
import { useLocation, useNavigate } from 'react-router-dom'

import Icon from './Icon'
import Button from './Button'
import { useForm } from 'react-hook-form'

import { me, facebookLink, vkLink, appleLink } from '../api'

import { useVisitor } from './VisitorContext'

/**
 * @typedef {object} LoginProps
 * @property {Function} onAuth
 */

/**
 * Login page
 * @param {LoginProps} props
 */
function Login({ onAuth }) {
    const location = useLocation()
    const navigate = useNavigate()
    const [visitor] = useVisitor()
    useEffect(() => {
        if (visitor && visitor.hash) {
            const retpath = location.state?.retpath || '/'
            console.log(retpath)
            navigate(retpath)
        }
    }, [navigate, location.state, visitor])

    const { register, handleSubmit } = useForm()

    /** @type { import('react-hook-form').SubmitHandler<import('react-hook-form').FieldValues> } */
    let onSubmit = (values) => {
        me(values.username, values.password)
            .then(response => {
                onAuth(response)
            }
            ).catch(ex => {
                console.log(ex)
            })
    }
    return (
        <div className="msg-cont">
            <div className="dialoglogin">
                <p>Please, introduce yourself:</p>
                <div style={socialButtonsStyle}>
                    <a href={facebookLink()} style={facebookButtonStyle}>
                        <Icon name="ei-sc-facebook" size="s" noFill={true} />Log in
              </a>
                    <a href={vkLink()} style={vkButtonStyle}>
                        <Icon name="ei-sc-vk" size="s" noFill={true} />
                        Log in
             </a>
             <a href={appleLink()}><img src="https://appleid.cdn-apple.com/appleid/button" /></a>
                </div>
                <p>Already registered?</p>
                <form onSubmit={handleSubmit(onSubmit)}>
                    <input placeholder="Username..." {...register('username')} /><br />
                    <input placeholder="Password..." type="password" {...register('password')} /><br />
                    <Button onClick={handleSubmit(onSubmit)}>OK</Button>
                </form>
            </div>
        </div>
    )
}

export default Login

const socialButtonsStyle = {
    display: 'flex',
    justifyContent: 'space-evenly',
    padding: '4px'
}

const facebookButtonStyle = {
    color: '#fff',
    padding: '2px 14px',
    background: '#3b5998'
}

const vkButtonStyle = {
    color: '#fff',
    padding: '2px 14px',
    background: '#4c75a3'
}