Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3270 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

import React from 'react'
import { useNavigate } from 'react-router-dom'
import { useDispatch } from 'react-redux'

import { useFetch, useGoals } from '@hooks'
import { saveGoal } from '@services/habits/goals'
import { addNotification } from '@store/notification/notification.actions'

import PageHeader from '@components/common/page-header'
import GoalForm from '@components/habits/goals/goal-form'

export default function CreateGoalPage() {
  const navigate = useNavigate()
  const dispatch = useDispatch()

  const { addUrl, addGoal } = useGoals()
  const { data: habits } = useFetch(addUrl)
  const habitsValues = Object.entries(habits).map(([value, name]) => ({
    value,
    name
  }))

  const onSubmit = async (goal) => {
    try {
      const response = await saveGoal(addUrl, goal)
      addGoal(response.data)
      dispatch(addNotification({ style: 'success', msg: response.message }))
      navigate('/habits/goals')
    } catch (error) {
      dispatch(addNotification({ style: 'danger', msg: error.message }))
    }
  }

  return (
    <>
      <PageHeader title='Crear una nueva Meta' goBack />
      <GoalForm onSubmit={onSubmit} habits={habitsValues} />
    </>
  )
}