Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3374 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 3374 Rev 3719
Línea 1... Línea 1...
1
import React from 'react'
1
import React from 'react';
2
import { useParams, useNavigate } from 'react-router-dom'
2
import { useParams, useNavigate } from 'react-router-dom';
3
import { useDispatch } from 'react-redux'
3
import { useDispatch } from 'react-redux';
4
 
4
 
5
import { useValues } from '@hooks'
5
import { useValues } from '@hooks';
6
import { editValue } from '@services/habits/values'
6
import { editValue } from '@services/habits/values';
7
import { addNotification } from '@store/notification/notification.actions'
7
import { addNotification } from '@store/notification/notification.actions';
8
 
8
 
9
import PageHeader from '@components/common/page-header'
9
import PageHeader from '@components/common/page-header';
10
import ValueForm from '@components/habits/values/value-form'
10
import ValueForm from '@components/habits/values/value-form';
11
 
11
 
12
export default function EditValuePage() {
12
export default function EditValuePage() {
13
  const { id } = useParams()
13
  const { id } = useParams();
14
  const navigate = useNavigate()
14
  const navigate = useNavigate();
15
  const dispatch = useDispatch()
15
  const dispatch = useDispatch();
16
 
16
 
17
  const { updateValue, getValueById } = useValues()
17
  const { updateValue, getValueById } = useValues();
18
  const currentValue = getValueById(id)
18
  const currentValue = getValueById(id);
19
 
19
 
20
  const onSubmit = async (value) => {
20
  const onSubmit = async (value) => {
21
    try {
21
    try {
22
      const response = await editValue(currentValue.actions.link_edit, value)
22
      const response = await editValue(currentValue.actions.link_edit, value);
23
      dispatch(addNotification({ style: 'success', msg: response.message }))
23
      dispatch(addNotification({ style: 'success', msg: response.message }));
24
      updateValue(response.data)
24
      updateValue(response.data);
25
      navigate('/habits/values')
25
      navigate('/habits/values');
26
    } catch (error) {
26
    } catch (error) {
27
      dispatch(addNotification({ style: 'danger', msg: error.message }))
27
      dispatch(addNotification({ style: 'danger', msg: error.message }));
28
    }
28
    }
29
  }
29
  };
30
 
30
 
31
  return (
31
  return (
32
    <>
32
    <>
33
      <PageHeader title='Edita tú valor' goBack />
33
      <PageHeader title='Edita tú valor' goBack />
34
      <ValueForm
34
      <ValueForm
35
        onSubmit={onSubmit}
35
        onSubmit={onSubmit}
36
        defaultValues={{
36
        defaultValues={{
37
          name: '',
37
          name: '',
38
          description: ''
38
          description: ''
39
        }}
39
        }}
40
        values={{
40
        values={{
41
          name: currentValue?.name,
41
          name: currentValue?.name,
42
          description: currentValue?.description
42
          description: currentValue?.description
43
        }}
43
        }}
44
      />
44
      />
45
    </>
45
    </>
46
  )
46
  );
47
}
47
}