Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3432 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3719 stevensc 1
import React from 'react';
2
import { useParams, useNavigate } from 'react-router-dom';
3
import { useDispatch } from 'react-redux';
4
 
5
import { useFetch, useHabits } from '@hooks';
6
import { editHabit } from '@services/habits/habits';
7
import { addNotification } from '@store/notification/notification.actions';
8
 
9
import PageHeader from '@components/common/page-header';
10
import LoadingWrapper from '@components/common/loading-wrapper';
11
import HabitForm from '@components/habits/habits/habit-form';
12
 
13
export default function EditHabitPage() {
14
  const { id } = useParams();
15
  const navigate = useNavigate();
16
  const dispatch = useDispatch();
17
 
18
  const { updateHabit, getHabitById } = useHabits();
19
  const currentHabit = getHabitById(id);
20
 
21
  const { data: habitValues, isLoading } = useFetch(currentHabit?.actions.link_edit);
22
 
23
  const onSubmit = async (habit) => {
24
    try {
25
      const response = await editHabit(currentHabit?.actions.link_edit, habit);
26
      dispatch(addNotification({ style: 'success', msg: response.message }));
27
      updateHabit(response.data);
28
      navigate('/habits/habits');
29
    } catch (error) {
30
      dispatch(addNotification({ style: 'danger', msg: error.message }));
31
    }
32
  };
33
 
34
  return (
35
    <>
36
      <PageHeader title='Editar hábito o competencia' goBack />
37
      <LoadingWrapper loading={isLoading}>
38
        <HabitForm onSubmit={onSubmit} values={habitValues} />
39
      </LoadingWrapper>
40
    </>
41
  );
42
}