Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 1866 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1854 stevensc 1
import React, { useState, useEffect } from 'react'
2
import { useDispatch, useSelector } from 'react-redux'
3
import { IconButton } from '@mui/material'
4
import { Edit } from '@mui/icons-material'
5
 
6
import { axios } from '@app/utils'
7
import { addNotification } from '@app/redux/notification/notification.actions'
8
 
9
import ProfileWidget from '../ProfileWidget'
10
import TagsList from '@app/components/UI/TagsList'
11
import HobbiesModal from './HobbiesModal'
12
 
13
const HobbiesCard = ({
14
  hobbies: defaultHobbies = {},
15
  uuid = '',
16
  edit = false
17
}) => {
18
  const [hobbies, setHobbies] = useState([])
19
  const [showModal, setShoModal] = useState(false)
20
  const labels = useSelector(({ intl }) => intl.labels)
21
  const dispatch = useDispatch()
22
 
23
  const toggleModal = () => setShoModal(!showModal)
24
 
25
  const handleEditHobbies = ({ hobbies }) => {
26
    const formData = new FormData()
27
 
28
    hobbies.map((hobbie) =>
29
      formData.append('hobbies_and_interests[]', hobbie.value)
30
    )
31
 
32
    axios
33
      .post(`/profile/my-profiles/hobby-and-interest/${uuid}`, formData)
34
      .then(({ data: response }) => {
35
        const { data, success } = response
36
 
37
        if (!success) {
38
          const errorMessage =
39
            typeof data === 'string' ? data : data.hobbies_and_interests[0]
40
          throw new Error(errorMessage)
41
        }
42
 
43
        setHobbies(hobbies)
44
        toggleModal()
45
      })
46
      .catch((err) => {
47
        dispatch(addNotification({ style: 'danger', msg: err.message }))
48
      })
49
  }
50
 
51
  useEffect(() => {
52
    const formattedhHobbies = Object.entries(defaultHobbies).map(
53
      ([key, value]) => ({
54
        name: value,
55
        value: key
56
      })
57
    )
58
    setHobbies(formattedhHobbies)
59
  }, [defaultHobbies])
60
 
61
  return (
62
    <>
63
      <ProfileWidget
64
        title={labels.hobbies_and_interests}
65
        action={
66
          edit && (
67
            <IconButton onClick={toggleModal}>
68
              <Edit />
69
            </IconButton>
70
          )
71
        }
72
      >
73
        <TagsList tags={hobbies} />
74
      </ProfileWidget>
75
 
76
      <HobbiesModal
77
        show={showModal}
78
        onClose={toggleModal}
79
        onConfirm={handleEditHobbies}
80
        hobbies={hobbies}
81
      />
82
    </>
83
  )
84
}
85
 
86
export default HobbiesCard