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