3719 |
stevensc |
1 |
import React, { useMemo, 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 { useHabitProgress } from '@hooks';
|
|
|
7 |
import { deleteProgress } from '@services/habits/habits';
|
|
|
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 ProgressItem({ progress }) {
|
|
|
15 |
const [show, setShow] = useState(false);
|
|
|
16 |
const navigate = useNavigate();
|
|
|
17 |
const dispatch = useDispatch();
|
|
|
18 |
|
|
|
19 |
const { id, date, quantitative_value, qualitative_description, actions } = progress;
|
|
|
20 |
|
|
|
21 |
const { removeItem, getItemById } = useHabitProgress();
|
|
|
22 |
const currentRegister = getItemById(id);
|
|
|
23 |
|
|
|
24 |
const formatDate = useMemo(() => {
|
|
|
25 |
const intlDate = new Intl.DateTimeFormat('es', {
|
|
|
26 |
dateStyle: 'full',
|
|
|
27 |
timeStyle: 'medium',
|
|
|
28 |
timeZone: 'America/Bogota'
|
|
|
29 |
}).format(new Date(date));
|
|
|
30 |
return intlDate;
|
|
|
31 |
}, [date]);
|
|
|
32 |
|
|
|
33 |
const toggleConfirmModal = () => setShow(!show);
|
|
|
34 |
|
|
|
35 |
const handleDelete = async () => {
|
|
|
36 |
try {
|
|
|
37 |
const response = await deleteProgress(currentRegister?.actions.link_delete);
|
|
|
38 |
dispatch(addNotification({ style: 'success', msg: response }));
|
|
|
39 |
removeItem(id);
|
|
|
40 |
} catch (error) {
|
|
|
41 |
dispatch(addNotification({ style: 'danger', msg: error.message }));
|
|
|
42 |
}
|
|
|
43 |
};
|
|
|
44 |
|
|
|
45 |
return (
|
|
|
46 |
<>
|
|
|
47 |
<Widget>
|
|
|
48 |
<Widget.Header
|
|
|
49 |
title={formatDate}
|
|
|
50 |
subheader={quantitative_value}
|
|
|
51 |
renderAction={() => (
|
|
|
52 |
<Options>
|
|
|
53 |
{actions.link_edit && (
|
|
|
54 |
<Options.Item onClick={() => navigate(`edit/${id}`)}>Editar</Options.Item>
|
|
|
55 |
)}
|
|
|
56 |
{actions.link_delete && (
|
|
|
57 |
<Options.Item onClick={toggleConfirmModal}>Borrar</Options.Item>
|
|
|
58 |
)}
|
|
|
59 |
</Options>
|
|
|
60 |
)}
|
|
|
61 |
/>
|
|
|
62 |
|
|
|
63 |
<Widget.Body>
|
|
|
64 |
<Typography>{qualitative_description}</Typography>
|
|
|
65 |
</Widget.Body>
|
|
|
66 |
</Widget>
|
|
|
67 |
<ConfirmModal
|
|
|
68 |
show={show}
|
|
|
69 |
title='Borrar'
|
|
|
70 |
message='¿Estás seguro de que deseas borrar este registro?'
|
|
|
71 |
onAccept={handleDelete}
|
|
|
72 |
onClose={toggleConfirmModal}
|
|
|
73 |
/>
|
|
|
74 |
</>
|
|
|
75 |
);
|
|
|
76 |
}
|