Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3244 | | 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 { useGoals } from '@hooks';
7
import { deleteGoal } from '@services/habits/goals';
8
import { addNotification } from '@store/notification/notification.actions';
9
 
10
import Widget from '@app/components/UI/Widget';
11
import Options from '@components/UI/Option';
12
import ConfirmModal from '@components/modals/ConfirmModal';
13
 
14
export default function GoalItem({
15
  goal: {
16
    id = 1,
17
    name = 'Test',
18
    description = 'Prueba',
19
    actions = {
20
      link_edit:
21
        'https://dev-services.leaderslinked.com/habits/goals/edit/cfede10c-f576-4a06-bbba-282ee88011fd',
22
      link_delete:
23
        'https://dev-services.leaderslinked.com/habits/goals/delete/cfede10c-f576-4a06-bbba-282ee88011fd'
24
    }
25
  }
26
}) {
27
  const [show, setShow] = useState(false);
28
  const navigate = useNavigate();
29
  const dispatch = useDispatch();
30
 
31
  const { deleteGoal: removeGoal, getGoalById } = useGoals();
32
  const currentGoal = getGoalById(id);
33
 
34
  const toggleConfirmModal = () => setShow(!show);
35
 
36
  const handleDelete = async () => {
37
    try {
38
      const response = await deleteGoal(currentGoal.actions.link_delete);
39
      dispatch(addNotification({ style: 'success', msg: response }));
40
      removeGoal(id);
41
    } catch (error) {
42
      dispatch(addNotification({ style: 'danger', msg: error.message }));
43
    }
44
  };
45
 
46
  return (
47
    <>
48
      <Widget>
49
        <Widget.Header
50
          title={name}
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
        <Widget.Body>
63
          <Typography>{description}</Typography>
64
        </Widget.Body>
65
      </Widget>
66
      <ConfirmModal
67
        show={show}
68
        onClose={toggleConfirmModal}
69
        title='Borrar'
70
        message='¿Estás seguro de que quieres borrar esta meta?'
71
        onAccept={handleDelete}
72
      />
73
    </>
74
  );
75
}