bigbuffet-rw/app/soapbox/features/public_layout/components/header.js

148 lines
5.2 KiB
JavaScript
Raw Normal View History

2020-04-25 15:26:47 -07:00
import React from 'react';
2022-03-21 11:09:01 -07:00
import { defineMessages, useIntl } from 'react-intl';
import { useDispatch, useSelector } from 'react-redux';
import { Link, Redirect } from 'react-router-dom';
2021-03-23 22:05:06 -07:00
import { logIn, verifyCredentials } from 'soapbox/actions/auth';
import { fetchInstance } from 'soapbox/actions/instance';
2022-03-21 11:09:01 -07:00
import { getSoapboxConfig } from 'soapbox/actions/soapbox';
2022-03-21 11:09:01 -07:00
import { openModal } from '../../../actions/modals';
import { Button, Form, HStack, IconButton, Input, Tooltip } from '../../../components/ui';
import Pulse from './pulse';
const messages = defineMessages({
home: { id: 'header.home.label', defaultMessage: 'Home' },
login: { id: 'header.login.label', defaultMessage: 'Log in' },
2022-03-21 11:09:01 -07:00
register: { id: 'header.register.label', defaultMessage: 'Register' },
emailAddress: { id: 'header.login.email.placeholder', defaultMessage: 'Email address' },
password: { id: 'header.login.password.label', defaultMessage: 'Password' },
});
2020-04-25 15:26:47 -07:00
2022-03-21 11:09:01 -07:00
const Header = () => {
const dispatch = useDispatch();
const intl = useIntl();
const logo = useSelector((state) => getSoapboxConfig(state).get('logo'));
const instance = useSelector((state) => state.get('instance'));
const isOpen = instance.get('registrations', false) === true;
2022-03-21 11:09:01 -07:00
const [isLoading, setLoading] = React.useState(false);
const [email, setEmail] = React.useState('');
const [password, setPassword] = React.useState('');
const [shouldRedirect, setShouldRedirect] = React.useState(false);
const [mfaToken, setMfaToken] = React.useState(false);
const open = () => dispatch(openModal('LANDING_PAGE'));
const handleSubmit = (event) => {
event.preventDefault();
2022-03-21 11:09:01 -07:00
setLoading(true);
dispatch(logIn(intl, email, password))
.then(({ access_token }) => {
return (
dispatch(verifyCredentials(access_token))
// Refetch the instance for authenticated fetch
.then(() => dispatch(fetchInstance()))
.then(() => setShouldRedirect(true))
);
})
.catch((error) => {
setLoading(false);
const data = error.response && error.response.data;
if (data && data.error === 'mfa_required') {
setMfaToken(data.mfa_token);
2020-08-07 13:17:13 -07:00
}
2022-03-21 11:09:01 -07:00
});
};
if (shouldRedirect) return <Redirect to='/' />;
if (mfaToken) return <Redirect to={`/login?token=${encodeURIComponent(mfaToken)}`} />;
return (
<header>
<nav className='max-w-7xl mx-auto px-4 sm:px-6 lg:px-8' aria-label='Header'>
<div className='w-full py-6 flex items-center justify-between border-b border-indigo-500 lg:border-none'>
<div className='flex items-center relative'>
<div className='hidden sm:block absolute z-0 left-0 top-0 -ml-[330px] -mt-[400px]'>
<Pulse />
2022-03-21 11:09:01 -07:00
</div>
<Link to='/' className='z-10'>
2022-04-01 19:30:04 -07:00
<img alt='Logo' src={logo} className='h-6 w-auto cursor-pointer' />
2022-03-21 11:09:01 -07:00
<span className='hidden'>{intl.formatMessage(messages.home)}</span>
2020-04-25 15:26:47 -07:00
</Link>
</div>
2022-03-21 11:09:01 -07:00
<div className='ml-10 flex space-x-6 items-center relative z-10'>
<IconButton
title='Open Menu'
src={require('@tabler/icons/icons/menu-2.svg')}
onClick={open}
className='md:hidden bg-transparent text-gray-400 hover:text-gray-600'
/>
<div className='hidden md:flex items-center space-x-6'>
<HStack space={2} className='xl:hidden'>
<Button to='/login' theme='secondary'>
{intl.formatMessage(messages.login)}
</Button>
{isOpen && (
<Button
to='/auth/verify'
theme='primary'
>
{intl.formatMessage(messages.register)}
</Button>
)}
</HStack>
2020-04-25 15:26:47 -07:00
</div>
2022-03-21 11:09:01 -07:00
<Form className='hidden xl:flex space-x-2 items-center' onSubmit={handleSubmit}>
<Input
required
value={email}
onChange={(event) => setEmail(event.target.value)}
type='text'
placeholder={intl.formatMessage(messages.emailAddress)}
className='max-w-[200px]'
/>
<Input
required
value={password}
onChange={(event) => setPassword(event.target.value)}
type='password'
placeholder={intl.formatMessage(messages.password)}
className='max-w-[200px]'
/>
<Link to='/reset-password'>
<Tooltip text='Forgot password?'>
<IconButton
src={require('@tabler/icons/icons/help.svg')}
className='bg-transparent text-gray-400 hover:text-gray-700 cursor-pointer'
iconClassName='w-5 h-5'
/>
</Tooltip>
</Link>
<Button
theme='primary'
type='submit'
disabled={isLoading}
>
{intl.formatMessage(messages.login)}
</Button>
</Form>
2020-04-25 15:26:47 -07:00
</div>
</div>
</nav>
2022-03-21 11:09:01 -07:00
</header>
);
};
2020-04-25 15:26:47 -07:00
2022-03-21 11:09:01 -07:00
export default Header;