Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3694 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

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