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