Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 1979 | Rev 3694 | 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
 
3047 stevensc 6
import { axios } from '@utils'
7
import { addNotification } from '@store/notification/notification.actions'
1849 stevensc 8
 
3047 stevensc 9
import Widget from '@components/UI/Widget'
1849 stevensc 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
        dispatch(addNotification({ style: 'danger', msg: error.message }))
49
      })
50
  }
51
 
52
  useEffect(() => {
53
    setAddress(defaultAddress)
54
  }, [defaultAddress])
55
 
56
  return (
57
    <>
3047 stevensc 58
      <Widget>
59
        <Widget.Header
60
          title={labels.location}
61
          renderAction={() => {
62
            if (!edit) return
63
            return (
64
              <IconButton onClick={toggleModal}>
65
                <Edit />
66
              </IconButton>
67
            )
68
          }}
69
        />
1849 stevensc 70
 
3047 stevensc 71
        <Widget.Body>
72
          <Typography variant='body1'>{address}</Typography>
73
        </Widget.Body>
74
      </Widget>
75
 
1849 stevensc 76
      <LocationModal
77
        show={showModal}
78
        onClose={toggleModal}
1852 stevensc 79
        onConfirm={handleEditLocation}
1849 stevensc 80
      />
81
    </>
82
  )
83
}
84
 
85
export default LocationCard