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 { useNavigate, useParams } from 'react-router-dom';
3
import { useDispatch } from 'react-redux';
4
 
5
import { useFetch, useGoals } from '@hooks';
6
import { editGoal } from '@services/habits/goals';
7
import { addNotification } from '@store/notification/notification.actions';
8
 
9
import PageHeader from '@components/common/page-header';
10
import GoalForm from '@components/habits/goals/goal-form';
11
import LoadingWrapper from '@components/common/loading-wrapper';
12
 
13
export default function EditGoalPage() {
14
  const { id } = useParams();
15
  const navigate = useNavigate();
16
  const dispatch = useDispatch();
17
 
18
  const { addUrl, updateGoal, getGoalById } = useGoals();
19
  const currentGoal = getGoalById(id);
20
 
21
  const { data: goalValues, isLoading } = useFetch(currentGoal?.actions.link_edit);
22
  const { data: habits } = useFetch(addUrl);
23
  const habitsValues = Object.entries(habits).map(([value, name]) => ({
24
    value,
25
    name
26
  }));
27
 
28
  const onSubmit = async (goal) => {
29
    try {
30
      const response = await editGoal(currentGoal?.actions.link_edit, goal);
31
      updateGoal(response.data);
32
      dispatch(addNotification({ style: 'success', msg: response.message }));
33
      navigate('/habits/goals');
34
    } catch (error) {
35
      dispatch(addNotification({ style: 'danger', msg: error.message }));
36
    }
37
  };
38
 
39
  return (
40
    <>
41
      <PageHeader title='Editar Meta' goBack />
42
      <LoadingWrapper loading={isLoading}>
43
        <GoalForm
44
          onSubmit={onSubmit}
45
          habits={habitsValues}
46
          values={{
47
            ...goalValues,
48
            start_date: new Date(goalValues.start_date + 'T00:00:00'),
49
            end_date: new Date(goalValues.end_date + 'T00:00:00')
50
          }}
51
        />
52
      </LoadingWrapper>
53
    </>
54
  );
55
}