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