Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3394 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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 { useHabits } from '@hooks';
7
import { deleteHabit } from '@services/habits/habits';
8
import { INTELLIGENCES } from '@constants/habits';
9
import { addNotification } from '@store/notification/notification.actions';
10
 
11
import Widget from '@components/UI/Widget';
12
import Options from '@components/UI/Option';
13
import ConfirmModal from '@components/modals/ConfirmModal';
14
 
15
export default function HabitItem({
16
  habit: {
17
    id,
18
    name = '',
19
    description = '',
20
    intelligence = '',
21
    actions = {
22
      link_edit: '',
23
      link_delete: ''
24
    }
25
  }
26
}) {
27
  const [show, setShow] = useState(false);
28
  const navigate = useNavigate();
29
  const dispatch = useDispatch();
30
 
31
  const { removeHabit, getHabitById } = useHabits();
32
  const currentParadigm = getHabitById(id);
33
  const intelligenceLabel = INTELLIGENCES.find((i) => i.value === intelligence);
34
 
35
  const toggleConfirmModal = () => setShow(!show);
36
 
37
  const handleDelete = async () => {
38
    try {
39
      const response = await deleteHabit(currentParadigm.actions.link_delete);
40
      dispatch(addNotification({ style: 'success', msg: response }));
41
      removeHabit(id);
42
    } catch (error) {
43
      dispatch(addNotification({ style: 'danger', msg: error.message }));
44
    }
45
  };
46
 
47
  return (
48
    <>
49
      <Widget>
50
        <Widget.Header
51
          title={name}
52
          subheader={intelligenceLabel?.name ?? ''}
53
          renderAction={() => (
54
            <Options>
55
              {actions.link_edit && (
56
                <Options.Item onClick={() => navigate(`edit/${id}`)}>Editar</Options.Item>
57
              )}
58
              {actions.link_delete && (
59
                <Options.Item onClick={toggleConfirmModal}>Borrar</Options.Item>
60
              )}
61
            </Options>
62
          )}
63
        />
64
        <Widget.Body>
65
          <Typography>{description}</Typography>
66
        </Widget.Body>
67
      </Widget>
68
      <ConfirmModal
69
        show={show}
70
        onClose={toggleConfirmModal}
71
        title='Borrar'
72
        message='¿Estás seguro de que quieres borrar este hábito o competencia?'
73
        onAccept={handleDelete}
74
      />
75
    </>
76
  );
77
}