Proyectos de Subversion LeadersLinked - SPA

Rev

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

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

import { useValues } from '@hooks'
import { editValue } from '@services/habits/values'
import { addNotification } from '@store/notification/notification.actions'

import PageHeader from '@components/common/page-header'
import ValueForm from '@components/habits/values/value-form'

export default function EditValuePage() {
  const { id } = useParams()
  const navigate = useNavigate()
  const dispatch = useDispatch()

  const { updateValue, getValueById } = useValues()
  const currentValue = getValueById(+id)

  const onSubmit = async (value) => {
    try {
      const response = await editValue(currentValue.actions.link_edit, value)
      dispatch(addNotification({ style: 'success', msg: response.message }))
      updateValue(response.data)
      navigate('/habits/values')
    } catch (error) {
      dispatch(addNotification({ style: 'danger', msg: error.message }))
    }
  }

  return (
    <>
      <PageHeader title='Edita tú valor' goBack />
      <ValueForm
        onSubmit={onSubmit}
        defaultValues={{
          name: '',
          description: ''
        }}
        values={{
          name: currentValue?.name,
          description: currentValue?.description
        }}
      />
    </>
  )
}