import React from 'react'
import { useDispatch } from 'react-redux'
import { updateCategorySetting } from '../reducers/category-settings'

const SelectCategoriesItem = ({item}) => {
  const elementId = 'dreamfox-field-'+item.id
  const dispatch = useDispatch()
  const handleDaysChanged = (e) => {
    dispatch(updateCategorySetting({id: item.id, data: {days: parseInt(e.target.value)}}))
  }
  const handleCheckClick = e => {
    dispatch(updateCategorySetting({id: item.id, data: {selected: e.target.checked}}))
  }
  return (
    <div className="dreamfox-select-categories-item">
      <div className="dreamfox-select-categories-item__check">
        <input
          className="dreamfox-select-categories-item__input"
          type="checkbox"
          checked={item.selected}
          id={elementId}
          onClick={handleCheckClick}
          onChange={handleCheckClick}
        />
        <label className="dreamfox-select-categories-item__label" htmlFor={elementId}>{item.name}</label>
      </div>
      <div>
        <input type="number" className="dreamfox-select-categories-item__number" value={item.days} onChange={handleDaysChanged} />
      </div>
    </div>
  )
}

export default SelectCategoriesItem
