import React, { FC } from 'react'
import { useSelector, useDispatch } from 'react-redux'
import { useTheme } from '../../hooks'
import { setFieldValue } from '../../actions/forms'
import { getFieldValue } from '../../selectors/forms'
import { AppState } from '../../types'

interface Props {
    name: string
}

const CheckboxField: FC<Props> = ({ name, children }) => {
    const theme = useTheme()
    const value = useSelector<AppState, boolean>(state => getFieldValue<boolean>(state, name, false))
    const dispatch = useDispatch()

    return (
        <label className="checkbox">
            <input type="checkbox" checked={value} onChange={e => dispatch(setFieldValue(name, e.target.checked))} style={{ color: theme.primary, border: 'none' }} />
            &nbsp;&nbsp;
            <span style={{ color: theme.text }}>{children}</span>
        </label>
    )
}

export default CheckboxField
