useStatus.js 677 B

12345678910111213141516171819202122232425262728293031
  1. import { useState } from "react";
  2. import { useDispatch } from "react-redux";
  3. const useStatus = (initValue, apiFnc, id, changeData) => {
  4. const dispatch = useDispatch();
  5. const [status, setStatus] = useState(initValue);
  6. const handleChangeStatus = (event) => {
  7. setStatus(event.target.value);
  8. }
  9. const handleClickStatusApply = async () => {
  10. await apiFnc({
  11. id,
  12. status
  13. }).then(() => {
  14. alert('저장 성공')
  15. dispatch(changeData({ id, key: 'status', value: status }))
  16. }).catch(() => {
  17. alert('저장 실패');
  18. })
  19. }
  20. return {
  21. status,
  22. handleChangeStatus,
  23. handleClickStatusApply
  24. }
  25. }
  26. export default useStatus;