Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3371 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

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