Proyectos de Subversion LeadersLinked - SPA

Rev

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

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