Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
3270 stevensc 1
import React from 'react'
2
import { useParams, useNavigate } from 'react-router-dom'
3
import { useDispatch } from 'react-redux'
4
 
5
import { usePurposes } from '@hooks'
6
import { editPurpose } from '@services/habits/purposes'
7
import { addNotification } from '@store/notification/notification.actions'
8
 
9
import PageHeader from '@components/common/page-header'
10
import PurposeForm from '@components/habits/purposes/purpose-form'
11
 
12
export default function EditPurposePage() {
13
  const { id } = useParams()
14
  const navigate = useNavigate()
15
  const dispatch = useDispatch()
16
 
17
  const { updatePurpose, getPurposeById } = usePurposes()
3374 stevensc 18
  const currentPurpose = getPurposeById(id)
3270 stevensc 19
 
20
  const onSubmit = async (purpose) => {
21
    try {
22
      const response = await editPurpose(
23
        currentPurpose.actions.link_edit,
24
        purpose
25
      )
26
      updatePurpose(response.data)
3371 stevensc 27
      dispatch(addNotification({ style: 'success', msg: response.message }))
3270 stevensc 28
      navigate('/habits/purposes')
29
    } catch (error) {
30
      dispatch(addNotification({ style: 'danger', msg: error.message }))
31
    }
32
  }
33
 
34
  return (
35
    <>
36
      <PageHeader title='Edita tú propósito' goBack />
37
      <PurposeForm
38
        onSubmit={onSubmit}
39
        defaultValues={{
40
          name: '',
41
          description: ''
42
        }}
43
        values={{
44
          name: currentPurpose?.name,
45
          description: currentPurpose?.description
46
        }}
47
      />
48
    </>
49
  )
50
}