Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3242 | Rev 3394 | 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: {
3239 stevensc 17
    id = 1,
18
    name = 'Habito 1',
19
    description = 'Prueba',
20
    intelligence = 'physical',
21
    actions = {
22
      link_edit:
23
        'https://dev-services.leaderslinked.com/habits/skills/edit/da9210a1-c36b-4b51-b102-9f3bd1e33bc1',
24
      link_delete:
25
        'https://dev-services.leaderslinked.com/habits/skills/delete/da9210a1-c36b-4b51-b102-9f3bd1e33bc1'
3153 stevensc 26
    }
27
  }
28
}) {
3239 stevensc 29
  const [show, setShow] = useState(false)
30
  const navigate = useNavigate()
31
  const dispatch = useDispatch()
32
 
33
  const { removeHabit, getHabitById } = useHabits()
34
  const currentParadigm = getHabitById(id)
3242 stevensc 35
  const intelligenceLabel = INTELLIGENCES.find((i) => i.value === intelligence)
3239 stevensc 36
 
37
  const toggleConfirmModal = () => setShow(!show)
38
 
39
  const handleDelete = async () => {
40
    try {
41
      const response = await deleteHabit(currentParadigm.actions.link_delete)
42
      dispatch(addNotification({ style: 'success', msg: response }))
43
      removeHabit(id)
44
    } catch (error) {
45
      dispatch(addNotification({ style: 'danger', msg: error.message }))
46
    }
47
  }
48
 
3153 stevensc 49
  return (
3239 stevensc 50
    <>
51
      <Widget>
52
        <Widget.Header
53
          title={name}
3242 stevensc 54
          subheader={intelligenceLabel.name}
3239 stevensc 55
          renderAction={() => (
56
            <Options>
57
              {actions.link_edit && (
58
                <Options.Item onClick={() => navigate(`edit/${id}`)}>
59
                  Editar
60
                </Options.Item>
61
              )}
62
              {actions.link_delete && (
63
                <Options.Item onClick={toggleConfirmModal}>Borrar</Options.Item>
64
              )}
65
            </Options>
66
          )}
67
        />
68
        <Widget.Body>
69
          <Typography>{description}</Typography>
70
        </Widget.Body>
71
      </Widget>
72
      <ConfirmModal
73
        show={show}
74
        onClose={toggleConfirmModal}
75
        title='Borrar'
3262 stevensc 76
        message='¿Estás seguro de que quieres borrar este hábito o competencia?'
3239 stevensc 77
        onAccept={handleDelete}
78
      />
79
    </>
3153 stevensc 80
  )
81
}