Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
1856 stevensc 1
import React, { useEffect, useMemo, useState } from 'react'
1854 stevensc 2
import { useDispatch, useSelector } from 'react-redux'
3
import { IconButton } from '@mui/material'
4
import { Edit } from '@mui/icons-material'
5
 
1857 stevensc 6
import { formatObjectToArray } from '@app/utils'
7
import { updateSkills } from '@app/services/profiles'
1854 stevensc 8
import { addNotification } from '@app/redux/notification/notification.actions'
9
 
10
import TagsList from '@app/components/UI/TagsList'
11
import ProfileWidget from '../ProfileWidget'
12
import SkillsModal from './SkillsModal'
13
 
14
const SkillsCard = ({
15
  skills: defaultSkills = {},
16
  uuid = '',
17
  edit = false
18
}) => {
19
  const [skills, setSkills] = useState([])
20
  const [showModal, setShowModal] = useState(false)
21
  const labels = useSelector(({ intl }) => intl.labels)
22
  const dispatch = useDispatch()
1861 stevensc 23
  const usersSkills = useMemo(() => {
24
    console.log('on format skills')
25
    return formatObjectToArray(defaultSkills)
26
  }, [defaultSkills])
1854 stevensc 27
 
28
  const toggleModal = () => setShowModal(!showModal)
29
 
30
  const handleEditSkills = ({ skills }) => {
1857 stevensc 31
    updateSkills(uuid, skills)
1854 stevensc 32
      .then(({ data: response }) => {
33
        const { data, success } = response
34
        if (!success) {
35
          const errorMessage = typeof data === 'string' ? data : data.skills[0]
36
          throw new Error(errorMessage)
37
        }
38
 
39
        toggleModal()
40
        setSkills(skills)
41
      })
42
      .catch((error) => {
43
        dispatch(addNotification({ style: 'danger', msg: error.message }))
44
      })
45
  }
46
 
47
  useEffect(() => {
1861 stevensc 48
    console.log('on set skills')
49
    setSkills(usersSkills)
50
  }, [usersSkills])
1854 stevensc 51
 
52
  return (
53
    <>
54
      <ProfileWidget
55
        title={labels.skills}
56
        action={
57
          edit && (
58
            <IconButton onClick={toggleModal}>
59
              <Edit />
60
            </IconButton>
61
          )
62
        }
63
      >
64
        <TagsList tags={skills} />
65
      </ProfileWidget>
66
 
67
      <SkillsModal
68
        show={showModal}
69
        onClose={toggleModal}
70
        onConfirm={handleEditSkills}
71
        skills={skills}
72
      />
73
    </>
74
  )
75
}
76
 
77
export default SkillsCard