3719 |
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(currentParadigm.actions.link_edit, paradigm);
|
|
|
23 |
dispatch(addNotification({ style: 'success', msg: response.message }));
|
|
|
24 |
updateParadigm(response.data);
|
|
|
25 |
navigate('/habits/paradigms');
|
|
|
26 |
} catch (error) {
|
|
|
27 |
dispatch(addNotification({ style: 'danger', msg: error.message }));
|
|
|
28 |
}
|
|
|
29 |
};
|
|
|
30 |
|
|
|
31 |
return (
|
|
|
32 |
<>
|
|
|
33 |
<PageHeader title='Edita tú paradigma' goBack />
|
|
|
34 |
<ParadigmForm
|
|
|
35 |
onSubmit={onSubmit}
|
|
|
36 |
defaultValues={{
|
|
|
37 |
name: '',
|
|
|
38 |
description: ''
|
|
|
39 |
}}
|
|
|
40 |
values={{
|
|
|
41 |
name: currentParadigm?.name,
|
|
|
42 |
description: currentParadigm?.description
|
|
|
43 |
}}
|
|
|
44 |
/>
|
|
|
45 |
</>
|
|
|
46 |
);
|
|
|
47 |
}
|