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

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