[ABANDONED] React/Redux front end for the Flexor social network.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.9 KiB

5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. import React, { FC, useEffect } from 'react'
  2. import { useSelector, useDispatch } from 'react-redux'
  3. import { useHistory } from 'react-router'
  4. import { faIdCard } from '@fortawesome/free-solid-svg-icons'
  5. import { handleApiError } from '../../api/errors'
  6. import { authenticate } from '../../actions/authentication'
  7. import { showNotification } from '../../actions/notifications'
  8. import { getChecked, getAuthenticated } from '../../selectors/authentication'
  9. import { getFieldValue } from '../../selectors/forms'
  10. import { getIsFetching } from '../../selectors/requests'
  11. import { initForm, initField, setFieldNotification } from '../../actions/forms'
  12. import Title from '../../components/title'
  13. import Section from '../../components/section'
  14. import TextField from '../../components/controls/text-field'
  15. import PasswordField from '../../components/controls/password-field'
  16. import PrimaryButton from '../../components/controls/primary-button'
  17. import { AppState, RequestKey, NotificationType } from '../../types'
  18. const Login: FC = () => {
  19. const checked = useSelector(getChecked)
  20. const authenticated = useSelector(getAuthenticated)
  21. const name = useSelector<AppState, string>(state => getFieldValue(state, 'name', ''))
  22. const password = useSelector<AppState, string>(state => getFieldValue(state, 'password', ''))
  23. const authenticating = useSelector<AppState, boolean>(state => getIsFetching(state, RequestKey.Authenticate))
  24. const dispatch = useDispatch()
  25. const history = useHistory()
  26. useEffect(() => {
  27. if (checked && authenticated) history.push('/self')
  28. }, [checked, authenticated])
  29. useEffect(() => {
  30. dispatch(initForm())
  31. dispatch(initField('name', '', 'id'))
  32. dispatch(initField('password', ''))
  33. }, [])
  34. const handleAuthenticate = async () => {
  35. let invalid = false
  36. if (!name) {
  37. dispatch(setFieldNotification('name', NotificationType.Error, 'This is required'))
  38. invalid = true
  39. }
  40. if (!password) {
  41. dispatch(setFieldNotification('password', NotificationType.Error, 'This is required'))
  42. invalid = true
  43. }
  44. if (invalid) return
  45. try {
  46. const id = await dispatch(authenticate(name, password))
  47. dispatch(showNotification(NotificationType.Welcome, `Welcome back ${id}!`))
  48. history.push('/')
  49. } catch (err) {
  50. handleApiError(err, dispatch, history)
  51. }
  52. }
  53. return (
  54. <div>
  55. <Section>
  56. <Title>Log In to Flexor</Title>
  57. <TextField icon={faIdCard} name="name" label="Username" placeholder="Your Username/ID" />
  58. <PasswordField placeholder="Your password" showStrength={false} />
  59. <PrimaryButton text="Log In" onClick={() => handleAuthenticate()} loading={authenticating} />
  60. </Section>
  61. </div>
  62. )
  63. }
  64. export default Login