import { useState } from "react";
import { useDispatch } from "react-redux";

const useStatus = (initValue, apiFnc, id, changeData) => {
  const dispatch = useDispatch();
  const [status, setStatus] = useState(initValue);

  const handleChangeStatus = (event) => {
    setStatus(event.target.value);
  }

  const handleClickStatusApply = async () => {
    await apiFnc({
      id,
      status
    }).then(() => {
      alert('저장 성공')
      dispatch(changeData({ id, key: 'status', value: status }))
    }).catch(() => {
      alert('저장 실패');
    })
  }

  return {
    status,
    handleChangeStatus,
    handleClickStatusApply
  }
}

export default useStatus;