Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
1849 stevensc 1
import React, { useState, useEffect } from 'react'
2
import { useDispatch, useSelector } from 'react-redux'
3
import { IconButton, Typography } 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 LocationModal from './LocationModal'
11
 
12
const LocationCard = ({
13
  address: defaultAddress = '',
14
  uuid = '',
15
  edit = false
16
}) => {
17
  const [address, setAddress] = useState('')
18
  const [showModal, setShowModal] = useState(false)
19
  const labels = useSelector(({ intl }) => intl.labels)
20
  const dispatch = useDispatch()
21
 
22
  const toggleModal = () => setShowModal(!showModal)
23
 
24
  const handleEditLocation = (data) => {
25
    const formData = new FormData()
26
    Object.entries(data).map(([key, value]) => formData.append(key, value))
27
 
28
    axios
29
      .post(`/profile/my-profiles/location/${uuid}`, formData)
30
      .then((response) => {
31
        const { data, success } = response.data
32
 
33
        if (!success) {
34
          const resError =
35
            typeof data === 'string'
36
              ? data
37
              : Object.entries(data)
38
                  .map(([key, value]) => `${key}: ${value}`)
39
                  .join(', ')
40
 
41
          throw new Error(resError)
42
        }
43
 
44
        toggleModal()
45
      })
46
      .catch((error) => {
47
        console.error(error)
48
        dispatch(addNotification({ style: 'danger', msg: error.message }))
49
      })
50
  }
51
 
52
  useEffect(() => {
53
    setAddress(defaultAddress)
54
  }, [defaultAddress])
55
 
56
  return (
57
    <>
58
      <ProfileWidget
59
        title={labels.location}
60
        action={
61
          edit && (
62
            <IconButton onClick={toggleModal}>
63
              <Edit />
64
            </IconButton>
65
          )
66
        }
67
      >
68
        <Typography variant='body1'>{address}</Typography>
69
      </ProfileWidget>
70
 
71
      <LocationModal
72
        show={showModal}
73
        onClose={toggleModal}
1852 stevensc 74
        onConfirm={handleEditLocation}
1849 stevensc 75
      />
76
    </>
77
  )
78
}
79
 
80
export default LocationCard