Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
3239 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'
3153 stevensc 5
 
3239 stevensc 6
import { useHabits } from '@hooks'
7
import { deleteHabit } from '@services/habits/habits'
3242 stevensc 8
import { INTELLIGENCES } from '@constants/habits'
3239 stevensc 9
import { addNotification } from '@store/notification/notification.actions'
10
 
3153 stevensc 11
import Widget from '@components/UI/Widget'
3239 stevensc 12
import Options from '@components/UI/Option'
13
import ConfirmModal from '@components/modals/ConfirmModal'
3153 stevensc 14
 
15
export default function HabitItem({
16
  habit: {
3394 stevensc 17
    id,
18
    name = '',
19
    description = '',
20
    intelligence = '',
3239 stevensc 21
    actions = {
3394 stevensc 22
      link_edit: '',
23
      link_delete: ''
3153 stevensc 24
    }
25
  }
26
}) {
3239 stevensc 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)
3242 stevensc 33
  const intelligenceLabel = INTELLIGENCES.find((i) => i.value === intelligence)
3239 stevensc 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
 
3153 stevensc 47
  return (
3239 stevensc 48
    <>
49
      <Widget>
50
        <Widget.Header
51
          title={name}
3394 stevensc 52
          subheader={intelligenceLabel?.name ?? ''}
3239 stevensc 53
          renderAction={() => (
54
            <Options>
55
              {actions.link_edit && (
56
                <Options.Item onClick={() => navigate(`edit/${id}`)}>
57
                  Editar
58
                </Options.Item>
59
              )}
60
              {actions.link_delete && (
61
                <Options.Item onClick={toggleConfirmModal}>Borrar</Options.Item>
62
              )}
63
            </Options>
64
          )}
65
        />
66
        <Widget.Body>
67
          <Typography>{description}</Typography>
68
        </Widget.Body>
69
      </Widget>
70
      <ConfirmModal
71
        show={show}
72
        onClose={toggleConfirmModal}
73
        title='Borrar'
3262 stevensc 74
        message='¿Estás seguro de que quieres borrar este hábito o competencia?'
3239 stevensc 75
        onAccept={handleDelete}
76
      />
77
    </>
3153 stevensc 78
  )
79
}