3719 |
stevensc |
1 |
import React, { useState } from 'react';
|
|
|
2 |
import { useNavigate } from 'react-router-dom';
|
|
|
3 |
import { useDispatch } from 'react-redux';
|
|
|
4 |
import { Typography } from '@mui/material';
|
|
|
5 |
|
|
|
6 |
import { useParadigms } from '@hooks';
|
|
|
7 |
import { deleteParadigm } from '@services/habits/paradigms';
|
|
|
8 |
import { addNotification } from '@store/notification/notification.actions';
|
|
|
9 |
|
|
|
10 |
import Widget from '@components/UI/Widget';
|
|
|
11 |
import Options from '@components/UI/Option';
|
|
|
12 |
import ConfirmModal from '@components/modals/ConfirmModal';
|
|
|
13 |
|
|
|
14 |
export default function ParadigmItem({ paradigm: { id, name, description, actions } }) {
|
|
|
15 |
const [show, setShow] = useState(false);
|
|
|
16 |
const navigate = useNavigate();
|
|
|
17 |
const dispatch = useDispatch();
|
|
|
18 |
|
|
|
19 |
const { removeParadigm, getParadigmById } = useParadigms();
|
|
|
20 |
const currentParadigm = getParadigmById(id);
|
|
|
21 |
|
|
|
22 |
const toggleConfirmModal = () => setShow(!show);
|
|
|
23 |
|
|
|
24 |
const handleDelete = async () => {
|
|
|
25 |
try {
|
|
|
26 |
const response = await deleteParadigm(currentParadigm.actions.link_delete);
|
|
|
27 |
dispatch(addNotification({ style: 'success', msg: response }));
|
|
|
28 |
removeParadigm(id);
|
|
|
29 |
} catch (error) {
|
|
|
30 |
dispatch(addNotification({ style: 'danger', msg: error.message }));
|
|
|
31 |
}
|
|
|
32 |
};
|
|
|
33 |
|
|
|
34 |
return (
|
|
|
35 |
<>
|
|
|
36 |
<Widget>
|
|
|
37 |
<Widget.Header
|
|
|
38 |
title={name}
|
|
|
39 |
renderAction={() => (
|
|
|
40 |
<Options>
|
|
|
41 |
{actions.link_edit && (
|
|
|
42 |
<Options.Item onClick={() => navigate(`edit/${id}`)}>Editar</Options.Item>
|
|
|
43 |
)}
|
|
|
44 |
{actions.link_delete && (
|
|
|
45 |
<Options.Item onClick={toggleConfirmModal}>Borrar</Options.Item>
|
|
|
46 |
)}
|
|
|
47 |
</Options>
|
|
|
48 |
)}
|
|
|
49 |
/>
|
|
|
50 |
|
|
|
51 |
<Widget.Body>
|
|
|
52 |
<Typography>{description}</Typography>
|
|
|
53 |
</Widget.Body>
|
|
|
54 |
</Widget>
|
|
|
55 |
<ConfirmModal
|
|
|
56 |
show={show}
|
|
|
57 |
onClose={toggleConfirmModal}
|
|
|
58 |
title='Borrar'
|
|
|
59 |
message='¿Estás seguro de que quieres borrar este paradigma?'
|
|
|
60 |
onAccept={handleDelete}
|
|
|
61 |
/>
|
|
|
62 |
</>
|
|
|
63 |
);
|
|
|
64 |
}
|