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