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

103 lines
4.0 KiB

import React, { FC, ReactNode } from 'react'
import { useSelector, useDispatch } from 'react-redux'
import classNames from 'classnames'
import zxcvbn from 'zxcvbn'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { IconDefinition } from '@fortawesome/fontawesome-common-types'
import { faKey, faExclamationTriangle, faCheckCircle } from '@fortawesome/free-solid-svg-icons'
import { setFieldValue } from 'src/actions/forms'
import { getFieldValue, getFieldNotification } from 'src/selectors/forms'
import { notificationTypeToClassName } from 'src/utils'
import { AppState, FormNotification, ClassDictionary } from 'src/types'
export interface Props {
placeholder?: string
userInputs?: string[]
showStrength?: boolean
}
const PasswordField: FC<Props> = ({
placeholder,
userInputs = [],
showStrength = true,
}) => {
const value = useSelector<AppState, string>(state => getFieldValue<string>(state, 'password', ''))
const notification = useSelector<AppState, FormNotification | undefined>(state => getFieldNotification(state, 'password'))
const dispatch = useDispatch()
const inputClassDictionary: ClassDictionary = { input: true }
const controlClassDictionary: ClassDictionary = { control: true, 'has-icons-left': true }
const helpClassDictionary: ClassDictionary = { help: true }
let icon: IconDefinition | undefined
let passwordMessage: ReactNode | undefined
if (value && showStrength) {
const { score } = zxcvbn(value, userInputs)
switch (score) {
case 0:
inputClassDictionary['is-danger'] = true
controlClassDictionary['has-icons-right'] = true
icon = faExclamationTriangle
passwordMessage = <span>Strength: <span className="has-text-danger">Unusable</span></span>
break
case 1:
inputClassDictionary['is-danger'] = true
passwordMessage = <span>Strength: <span className="has-text-danger">Not good</span></span>
break
case 2:
inputClassDictionary['is-warning'] = true
passwordMessage = <span>Strength: <span className="has-text-warning">OK</span></span>
break
case 3:
inputClassDictionary['is-success'] = true
passwordMessage = <span>Strength: <span className="has-text-success">Good!</span></span>
break
case 4:
inputClassDictionary['is-success'] = true
controlClassDictionary['has-icons-right'] = true
icon = faCheckCircle
passwordMessage = <span>Strength: <span className="has-text-success">LIT</span></span>
break
}
}
if (notification) {
const ncn = notificationTypeToClassName(notification.type)
helpClassDictionary[ncn] = true
inputClassDictionary[ncn] = true
}
const helpText = () => {
if (notification) return <p className={classNames(helpClassDictionary)}>{notification.message}</p>
if (passwordMessage) return <p className="help">{passwordMessage}</p>
}
return (
<div className="field">
<label className="label">Password</label>
<div className={classNames(controlClassDictionary)}>
<input
className={classNames(inputClassDictionary)}
type="password"
placeholder={placeholder}
value={value}
onChange={e => dispatch(setFieldValue('password', e.target.value))} />
<span className="icon is-small is-left">
<FontAwesomeIcon icon={faKey} />
</span>
{icon &&
<span className="icon is-small is-right">
<FontAwesomeIcon icon={icon} />
</span>
}
</div>
{helpText()}
</div>
)
}
export default PasswordField