File size: 2,691 Bytes
ca7bcbd
 
 
 
 
 
94367ea
ca7bcbd
 
e37d8b9
641c42f
ca7bcbd
641c42f
ca7bcbd
641c42f
e37d8b9
 
ca7bcbd
 
 
2fc40c6
ca7bcbd
94367ea
5485a06
ca7bcbd
 
2fc40c6
641c42f
94367ea
2fc40c6
94367ea
ca7bcbd
 
 
 
641c42f
ca7bcbd
 
 
 
641c42f
 
 
ca7bcbd
 
 
 
 
 
 
 
 
5485a06
ca7bcbd
 
 
 
 
94367ea
e37d8b9
ca7bcbd
94367ea
 
 
e37d8b9
ca7bcbd
830d5f1
ca7bcbd
e37d8b9
 
94367ea
 
ca7bcbd
 
94367ea
e37d8b9
641c42f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const amountInput = document.getElementById('amount');
const fromCurrency = document.getElementById('fromCurrency');
const toCurrency = document.getElementById('toCurrency');
const swapButton = document.getElementById('swapButton');
const convertButton = document.getElementById('convertButton');
const resultDiv = document.getElementById('result');

// Status konversi otomatis
let autoConvert = false;

// Tombol Convert pertama kali ditekan
convertButton.addEventListener('click', () => {
  autoConvert = true; // Aktifkan auto-convert
  convertCurrency(); // Lakukan konversi pertama kali
  hideConvertButton(); // Sembunyikan tombol Convert
});

// Swap mata uang
swapButton.addEventListener('click', async () => {
  const temp = fromCurrency.value;
  fromCurrency.value = toCurrency.value;
  toCurrency.value = temp;

  // Lakukan konversi langsung jika autoConvert aktif
  if (autoConvert) {
    convertCurrency();
  } else {
    clearResult(); // Hapus hasil jika autoConvert tidak aktif
  }
});

// Pantau perubahan input atau dropdown
[amountInput, fromCurrency, toCurrency].forEach((element) => {
  element.addEventListener('input', () => {
    if (autoConvert) {
      convertCurrency(); // Konversi otomatis
    }
  });

  element.addEventListener('change', () => {
    autoConvert = false; // Nonaktifkan auto-convert
    showConvertButton(); // Tampilkan tombol Convert kembali
    clearResult(); // Hapus hasil sebelumnya
  });
});

// Fungsi konversi
async function convertCurrency() {
  const amount = amountInput.value;
  const from = fromCurrency.value;
  const to = toCurrency.value;

  // Validasi input
  if (!amount || amount <= 0) {
    resultDiv.innerHTML = 'Please enter a valid amount.';
    return;
  }

  try {
    const apiKey = '3ebe2ccf9eeea2aaef280201';
    const url = `https://v6.exchangerate-api.com/v6/${apiKey}/latest/${from}`;
    const response = await fetch(url);
    const data = await response.json();

    if (data.result === 'success') {
      const rate = data.conversion_rates[to];
      const convertedAmount = (amount * rate).toFixed(2);
      resultDiv.innerHTML = `${amount} ${from} = ${convertedAmount} ${to}`;
    } else {
      resultDiv.innerHTML = `Error: ${data['error-type']}`;
    }
  } catch (error) {
    resultDiv.innerHTML = 'Error fetching conversion rate.';
    console.error(error);
  }
}

// Fungsi untuk menyembunyikan tombol Convert
function hideConvertButton() {
  convertButton.style.display = 'none';
}

// Fungsi untuk menampilkan tombol Convert
function showConvertButton() {
  convertButton.style.display = 'block';
}

// Fungsi untuk menghapus hasil konversi
function clearResult() {
  resultDiv.innerHTML = '';
}