[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.

96 lines
3.6 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. // login.tsx
  2. // Copyright (C) 2020 Dwayne Harris
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU General Public License for more details.
  11. // You should have received a copy of the GNU General Public License
  12. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. import React, { FC, useEffect } from 'react'
  14. import { useSelector, useDispatch } from 'react-redux'
  15. import { useHistory } from 'react-router'
  16. import { faIdCard } from '@fortawesome/free-solid-svg-icons'
  17. import { handleApiError } from '../../api/errors'
  18. import { authenticate } from '../../actions/authentication'
  19. import { showNotification } from '../../actions/notifications'
  20. import { getChecked, getAuthenticated } from '../../selectors/authentication'
  21. import { getFieldValue } from '../../selectors/forms'
  22. import { getIsFetching } from '../../selectors/requests'
  23. import { initForm, initField, setFieldNotification } from '../../actions/forms'
  24. import Title from '../../components/title'
  25. import Section from '../../components/section'
  26. import TextField from '../../components/controls/text-field'
  27. import PasswordField from '../../components/controls/password-field'
  28. import PrimaryButton from '../../components/controls/primary-button'
  29. import { AppState, RequestKey, NotificationType } from '../../types'
  30. const Login: FC = () => {
  31. const checked = useSelector(getChecked)
  32. const authenticated = useSelector(getAuthenticated)
  33. const name = useSelector<AppState, string>(state => getFieldValue(state, 'name', ''))
  34. const password = useSelector<AppState, string>(state => getFieldValue(state, 'password', ''))
  35. const authenticating = useSelector<AppState, boolean>(state => getIsFetching(state, RequestKey.Authenticate))
  36. const dispatch = useDispatch()
  37. const history = useHistory()
  38. useEffect(() => {
  39. if (checked && authenticated) history.push('/self')
  40. }, [checked, authenticated])
  41. useEffect(() => {
  42. dispatch(initForm())
  43. dispatch(initField('name', '', 'id'))
  44. dispatch(initField('password', ''))
  45. }, [])
  46. const handleAuthenticate = async () => {
  47. let invalid = false
  48. if (!name) {
  49. dispatch(setFieldNotification('name', NotificationType.Error, 'This is required'))
  50. invalid = true
  51. }
  52. if (!password) {
  53. dispatch(setFieldNotification('password', NotificationType.Error, 'This is required'))
  54. invalid = true
  55. }
  56. if (invalid) return
  57. try {
  58. const id = await dispatch(authenticate(name, password))
  59. dispatch(showNotification(NotificationType.Welcome, `Welcome back ${id}!`))
  60. history.push('/')
  61. } catch (err) {
  62. handleApiError(err, dispatch, history)
  63. }
  64. }
  65. return (
  66. <div>
  67. <Section>
  68. <Title>Log In to Flexor</Title>
  69. <TextField icon={faIdCard} name="name" label="Username" placeholder="Your Username/ID" />
  70. <PasswordField placeholder="Your password" showStrength={false} />
  71. <PrimaryButton text="Log In" onClick={() => handleAuthenticate()} loading={authenticating} />
  72. </Section>
  73. </div>
  74. )
  75. }
  76. export default Login