add.js 813 B

123456789101112131415161718192021222324252627282930
  1. import { tickers, ticker, sel, graph, err } from "./App.vue";
  2. export function add(nameToAdd) {
  3. const newTicker = {
  4. name: nameToAdd,
  5. price: '-'
  6. };
  7. localStorage.setItem(
  8. 'cryptonomicon-list',
  9. JSON.stringify(tickers.value));
  10. if (tickers.value.find(n => n.name === newTicker.name) === undefined) {
  11. tickers.value.push(newTicker);
  12. ticker.value = '';
  13. setInterval(async () => {
  14. const f = await fetch(
  15. `https://min-api.cryptocompare.com/data/price?fsym=${newTicker.name}&tsyms=USD&api_key=ce3fd966e7a1d10d65f907b20bf000552158fd3ed1bd614110baa0ac6cb57a7e`
  16. );
  17. const data = await f.json();
  18. tickers.value.find(t => t.name === newTicker.name).price = data.USD > 1 ? data.USD.toFixed(2) :
  19. data.USD.toPrecision(2);
  20. if (sel.value?.name === newTicker.name) {
  21. graph.value.push(data.USD);
  22. }
  23. }, 3000);
  24. }
  25. else { err.value = 1; }
  26. }