Proyectos de Subversion LeadersLinked - SPA

Rev

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

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