Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 1976 | Rev 3047 | 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) => {
1979 stevensc 25
    const formData = new FormData()
26
    Object.entries(data).map(([key, value]) => formData.append(key, value))
27
 
1849 stevensc 28
    axios
1979 stevensc 29
      .post(`/profile/my-profiles/location/${uuid}`, formData)
1849 stevensc 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
 
1853 stevensc 44
        setAddress(data.formatted_address)
1849 stevensc 45
        toggleModal()
46
      })
47
      .catch((error) => {
48
        console.error(error)
49
        dispatch(addNotification({ style: 'danger', msg: error.message }))
50
      })
51
  }
52
 
53
  useEffect(() => {
54
    setAddress(defaultAddress)
55
  }, [defaultAddress])
56
 
57
  return (
58
    <>
59
      <ProfileWidget
60
        title={labels.location}
61
        action={
62
          edit && (
63
            <IconButton onClick={toggleModal}>
64
              <Edit />
65
            </IconButton>
66
          )
67
        }
68
      >
69
        <Typography variant='body1'>{address}</Typography>
70
      </ProfileWidget>
71
 
72
      <LocationModal
73
        show={showModal}
74
        onClose={toggleModal}
1852 stevensc 75
        onConfirm={handleEditLocation}
1849 stevensc 76
      />
77
    </>
78
  )
79
}
80
 
81
export default LocationCard