Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3374 | Ir a la última revisión | | 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 { useParadigms } from '@hooks'
6
import { editParadigm } from '@services/habits/paradigms'
7
import { addNotification } from '@store/notification/notification.actions'
8
 
9
import PageHeader from '@components/common/page-header'
10
import ParadigmForm from '@components/habits/paradigms/paradigm-form'
11
 
12
export default function EditParadigmPage() {
13
  const { id } = useParams()
14
  const navigate = useNavigate()
15
  const dispatch = useDispatch()
16
 
17
  const { updateParadigm, getParadigmById } = useParadigms()
18
  const currentParadigm = getParadigmById(+id)
19
 
20
  const onSubmit = async (paradigm) => {
21
    try {
22
      const response = await editParadigm(
23
        currentParadigm.actions.link_edit,
24
        paradigm
25
      )
26
      dispatch(addNotification({ style: 'success', msg: response.message }))
27
      updateParadigm(response.data)
28
      navigate('/habits/paradigms')
29
    } catch (error) {
30
      dispatch(addNotification({ style: 'danger', msg: error.message }))
31
    }
32
  }
33
 
34
  return (
35
    <>
36
      <PageHeader title='Edita tú paradigma' goBack />
37
      <ParadigmForm
38
        onSubmit={onSubmit}
39
        defaultValues={{
40
          name: '',
41
          description: ''
42
        }}
43
        values={{
44
          name: currentParadigm?.name,
45
          description: currentParadigm?.description
46
        }}
47
      />
48
    </>
49
  )
50
}