Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 1853 | Rev 1979 | 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
    axios
1976 stevensc 26
      .post(`/profile/my-profiles/location/${uuid}`, data)
1849 stevensc 27
      .then((response) => {
28
        const { data, success } = response.data
29
 
30
        if (!success) {
31
          const resError =
32
            typeof data === 'string'
33
              ? data
34
              : Object.entries(data)
35
                  .map(([key, value]) => `${key}: ${value}`)
36
                  .join(', ')
37
 
38
          throw new Error(resError)
39
        }
40
 
1853 stevensc 41
        setAddress(data.formatted_address)
1849 stevensc 42
        toggleModal()
43
      })
44
      .catch((error) => {
45
        console.error(error)
46
        dispatch(addNotification({ style: 'danger', msg: error.message }))
47
      })
48
  }
49
 
50
  useEffect(() => {
51
    setAddress(defaultAddress)
52
  }, [defaultAddress])
53
 
54
  return (
55
    <>
56
      <ProfileWidget
57
        title={labels.location}
58
        action={
59
          edit && (
60
            <IconButton onClick={toggleModal}>
61
              <Edit />
62
            </IconButton>
63
          )
64
        }
65
      >
66
        <Typography variant='body1'>{address}</Typography>
67
      </ProfileWidget>
68
 
69
      <LocationModal
70
        show={showModal}
71
        onClose={toggleModal}
1852 stevensc 72
        onConfirm={handleEditLocation}
1849 stevensc 73
      />
74
    </>
75
  )
76
}
77
 
78
export default LocationCard