Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 1866 | Rev 1979 | 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 }) => {
26
    axios
1978 stevensc 27
      .post(`/profile/my-profiles/hobby-and-interest/${uuid}`, {
28
        'hobbies_and_interests[]': hobbies.map((hobbie) => hobbie.value)
29
      })
1854 stevensc 30
      .then(({ data: response }) => {
31
        const { data, success } = response
32
 
33
        if (!success) {
34
          const errorMessage =
35
            typeof data === 'string' ? data : data.hobbies_and_interests[0]
36
          throw new Error(errorMessage)
37
        }
38
 
39
        setHobbies(hobbies)
40
        toggleModal()
41
      })
42
      .catch((err) => {
43
        dispatch(addNotification({ style: 'danger', msg: err.message }))
44
      })
45
  }
46
 
47
  useEffect(() => {
1866 stevensc 48
    setHobbies(defaultHobbies)
1854 stevensc 49
  }, [defaultHobbies])
50
 
51
  return (
52
    <>
53
      <ProfileWidget
54
        title={labels.hobbies_and_interests}
55
        action={
56
          edit && (
57
            <IconButton onClick={toggleModal}>
58
              <Edit />
59
            </IconButton>
60
          )
61
        }
62
      >
63
        <TagsList tags={hobbies} />
64
      </ProfileWidget>
65
 
66
      <HobbiesModal
67
        show={showModal}
68
        onClose={toggleModal}
69
        onConfirm={handleEditHobbies}
70
        hobbies={hobbies}
71
      />
72
    </>
73
  )
74
}
75
 
76
export default HobbiesCard