Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 1978 | Rev 3047 | Ir a la última revisión | | Comparar con el anterior | 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 = ({
1866 stevensc 14
  hobbies: defaultHobbies = [],
1854 stevensc 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 }) => {
1979 stevensc 26
    const formData = new FormData()
27
 
28
    hobbies.map((hobbie) =>
29
      formData.append('hobbies_and_interests[]', hobbie.value)
30
    )
31
 
1854 stevensc 32
    axios
1979 stevensc 33
      .post(`/profile/my-profiles/hobby-and-interest/${uuid}`, formData)
1854 stevensc 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(() => {
1866 stevensc 52
    setHobbies(defaultHobbies)
1854 stevensc 53
  }, [defaultHobbies])
54
 
55
  return (
56
    <>
57
      <ProfileWidget
58
        title={labels.hobbies_and_interests}
59
        action={
60
          edit && (
61
            <IconButton onClick={toggleModal}>
62
              <Edit />
63
            </IconButton>
64
          )
65
        }
66
      >
67
        <TagsList tags={hobbies} />
68
      </ProfileWidget>
69
 
70
      <HobbiesModal
71
        show={showModal}
72
        onClose={toggleModal}
73
        onConfirm={handleEditHobbies}
74
        hobbies={hobbies}
75
      />
76
    </>
77
  )
78
}
79
 
80
export default HobbiesCard