aboutsummaryrefslogtreecommitdiff
path: root/modern/src/common/components/AddressValue.js
blob: 767f516b55185aa3acc97d4a7c1d6993da79d2ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import React, { useEffect, useState } from 'react';
import { Link } from '@mui/material';
import { useTranslation } from './LocalizationProvider';
import { useCatch } from '../../reactHelper';

const AddressValue = ({ latitude, longitude, originalAddress }) => {
  const t = useTranslation();

  const [address, setAddress] = useState();

  useEffect(() => {
    setAddress(originalAddress);
  }, [originalAddress]);

  const showAddress = useCatch(async () => {
    const query = new URLSearchParams({ latitude, longitude });
    const response = await fetch(`/api/server/geocode?${query.toString()}`);
    if (response.ok) {
      setAddress(await response.text());
    } else {
      throw Error(await response.text());
    }
  });

  if (address) {
    return address;
  }
  return (<Link href="#" onClick={showAddress}>{t('sharedShowAddress')}</Link>);
};

export default AddressValue;