Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3371 | | 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 } from 'react-router-dom';
3
import { useDispatch } from 'react-redux';
4
 
5
import { useFetch, useGoals } from '@hooks';
6
import { saveGoal } 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
 
12
export default function CreateGoalPage() {
13
  const navigate = useNavigate();
14
  const dispatch = useDispatch();
15
 
16
  const { addUrl, addGoal } = useGoals();
17
  const { data: habits } = useFetch(addUrl);
18
  const habitsValues = Object.entries(habits).map(([value, name]) => ({
19
    value,
20
    name
21
  }));
22
 
23
  const onSubmit = async (goal) => {
24
    try {
25
      const response = await saveGoal(addUrl, goal);
26
      addGoal(response.data);
27
      dispatch(addNotification({ style: 'success', msg: response.message }));
28
      navigate('/habits/goals');
29
    } catch (error) {
30
      dispatch(addNotification({ style: 'danger', msg: error.message }));
31
    }
32
  };
33
 
34
  return (
35
    <>
36
      <PageHeader title='Crear una nueva Meta' goBack />
37
      <GoalForm onSubmit={onSubmit} habits={habitsValues} />
38
    </>
39
  );
40
}