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

99 lines
3.6 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
  1. import React, { FC, ReactNode } from 'react'
  2. import { useSelector, useDispatch } from 'react-redux'
  3. import zxcvbn from 'zxcvbn'
  4. import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
  5. import { faKey } from '@fortawesome/free-solid-svg-icons'
  6. import { useTheme } from '../../hooks'
  7. import { setFieldValue } from '../../actions/forms'
  8. import { getFieldValue, getFieldNotification } from '../../selectors/forms'
  9. import { AppState, FormNotification, NotificationType } from '../../types'
  10. import FieldLabel from '../../components/controls/field-label'
  11. import FormNotificationComponent from '../../components/form-notification'
  12. export interface Props {
  13. placeholder?: string
  14. userInputs?: string[]
  15. showStrength?: boolean
  16. }
  17. const PasswordField: FC<Props> = ({
  18. placeholder,
  19. userInputs = [],
  20. showStrength = true,
  21. }) => {
  22. const theme = useTheme()
  23. const value = useSelector<AppState, string>(state => getFieldValue<string>(state, 'password', ''))
  24. const notification = useSelector<AppState, FormNotification | undefined>(state => getFieldNotification(state, 'password'))
  25. const dispatch = useDispatch()
  26. let passwordMessage: ReactNode | undefined
  27. let successState = false
  28. let errorState = false
  29. if (value && showStrength) {
  30. const { score } = zxcvbn(value, userInputs)
  31. switch (score) {
  32. case 0:
  33. errorState = true
  34. passwordMessage = <span>Strength: <span style={{ color: theme.red }}>Unusable</span></span>
  35. break
  36. case 1:
  37. errorState = true
  38. passwordMessage = <span>Strength: <span style={{ color: theme.red }}>Not good</span></span>
  39. break
  40. case 2:
  41. passwordMessage = <span>Strength: <span>OK</span></span>
  42. break
  43. case 3:
  44. successState = true
  45. passwordMessage = <span>Strength: <span style={{ color: theme.green }}>Good!</span></span>
  46. break
  47. case 4:
  48. successState = true
  49. passwordMessage = <span>Strength: <span style={{ color: theme.green }}>LIT</span></span>
  50. break
  51. }
  52. }
  53. if (notification) {
  54. switch (notification.type) {
  55. case NotificationType.Success:
  56. errorState = false
  57. successState = true
  58. case NotificationType.Error:
  59. errorState = true
  60. successState = false
  61. }
  62. }
  63. const color = successState ? theme.green : errorState ? theme.red : theme.primary
  64. const helpText = () => {
  65. if (notification) return <FormNotificationComponent notification={notification} />
  66. if (passwordMessage) return <p className="help">{passwordMessage}</p>
  67. }
  68. return (
  69. <div className="field">
  70. <FieldLabel>Password</FieldLabel>
  71. <div className="control-container">
  72. <div className="control-icon" style={{ backgroundColor: color, color: theme.primaryAlternate }}>
  73. <FontAwesomeIcon icon={faKey} />
  74. </div>
  75. <div className="control">
  76. <input
  77. style={{ backgroundColor: theme.backgroundSecondary, borderColor: color, color: theme.text }}
  78. type="password"
  79. placeholder={placeholder}
  80. value={value}
  81. onChange={e => dispatch(setFieldValue('password', e.target.value))} />
  82. </div>
  83. </div>
  84. {helpText()}
  85. </div>
  86. )
  87. }
  88. export default PasswordField