Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3129 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3127 stevensc 1
import React, { useState } from 'react'
3128 stevensc 2
import { useNavigate } from 'react-router-dom'
3150 stevensc 3
import { useDispatch } from 'react-redux'
3127 stevensc 4
import { Typography } from '@mui/material'
5
 
6
import { usePurposes } from '@hooks'
3150 stevensc 7
import { deletePurpose } from '@services/habits/purposes'
8
import { addNotification } from '@store/notification/notification.actions'
3127 stevensc 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 PurposeItem({
3128 stevensc 15
  purpose: { id, name, description, actions }
3127 stevensc 16
}) {
17
  const [show, setShow] = useState(false)
18
  const navigate = useNavigate()
3150 stevensc 19
  const dispatch = useDispatch()
3127 stevensc 20
 
3150 stevensc 21
  const { removePurpose, getPurposeById } = usePurposes()
22
  const currentPurpose = getPurposeById(id)
3127 stevensc 23
 
24
  const toggleConfirmModal = () => setShow(!show)
25
 
3150 stevensc 26
  const handleDelete = async () => {
27
    try {
28
      const response = await deletePurpose(currentPurpose.actions.link_delete)
29
      dispatch(addNotification({ style: 'success', msg: response }))
30
      removePurpose(id)
31
    } catch (error) {
32
      dispatch(addNotification({ style: 'danger', msg: error.message }))
33
    }
34
  }
35
 
3127 stevensc 36
  return (
3129 stevensc 37
    <>
38
      <Widget>
39
        <Widget.Header
40
          title={name}
41
          renderAction={() => (
42
            <Options>
43
              {actions.link_edit && (
44
                <Options.Item onClick={() => navigate(`edit/${id}`)}>
45
                  Editar
46
                </Options.Item>
47
              )}
48
              {actions.link_delete && (
3127 stevensc 49
                <Options.Item onClick={toggleConfirmModal}>Borrar</Options.Item>
3129 stevensc 50
              )}
51
            </Options>
52
          )}
53
        />
54
 
55
        <Widget.Body>
56
          <Typography>{description}</Typography>
57
        </Widget.Body>
58
      </Widget>
59
      <ConfirmModal
60
        show={show}
61
        onClose={toggleConfirmModal}
62
        title='Borrar'
63
        message='¿Estás seguro de que quieres borrar este propósito?'
3150 stevensc 64
        onAccept={handleDelete}
3127 stevensc 65
      />
3129 stevensc 66
    </>
3127 stevensc 67
  )
68
}