aboutsummaryrefslogtreecommitdiffstats
path: root/staging.yml
diff options
context:
space:
mode:
Diffstat (limited to 'staging.yml')
0 files changed, 0 insertions, 0 deletions
'>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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
<script>
  import Left from "$comp/Left.svelte";
  import Icon from "$comp/Icon.svelte";
  import { f, s, post, warning, sat, sats } from "$lib/utils";
  import { t } from "$lib/translations";

  export let amount,
    currency,
    fiat = !amount,
    rate,
    submit = undefined;

  export let amountFiat = 0;

  let arrow = "<";

  $: update(amount, amountFiat);
  let update = (a, f) => {
    if (fiat) {
      amount = Math.round(f / (rate / sats));
    } else {
      amountFiat = ((a * rate) / sats).toFixed(2);
    }
  };

  let loading = false;

  let symbol =
    {
      USD: "$",
      CAD: "$",
      AUD: "$",
      NZD: "$",
      MXN: "$",
      BRL: "R$",
      HKD: "$",
      TWD: "$",
      JPY: "¥",
      CNY: "¥",
      GBP: "£",
      EUR: "€",
      KRW: "₩",
    }[currency] || "";

  const numPad = ["1", "2", "3", "4", "5", "6", "7", "8", "9", ".", "0", "<"];

  const handleInput = (value) => {
    if (fiat) {
      if (amountFiat === 0 && value !== "." && value !== "<" && value !== "0") {
        amountFiat = value;
      } else if ((amountFiat === 0 || amountFiat === "0") && value === "0") {
        return;
      } else if (amountFiat === 0 && value === ".") {
        amountFiat = "0.";
      } else if (
        amountFiat !== 0 &&
        amountFiat.includes(".") &&
        value === "."
      ) {
        return;
      } else if (value === "<") {
        if (amountFiat !== 0) {
          amountFiat = amountFiat.slice(0, amountFiat.length - 1);
          if (amountFiat.length === 0) {
            amountFiat = 0;
            amount = 0;
          }
        }
      } else if (
        value !== "." &&
        value !== "<" &&
        parseInt(amountFiat + value) > rate
      ) {
        warning($t("user.receive.lessThan1BTCWarning"));
      } else if (amountFiat !== 0 && amountFiat.match(/\.../)) {
        return;
      } else {
        amountFiat = amountFiat + value;
      }
    } else {
      if (value === ".") {
        return;
      } else if (!amount && value !== "<" && value !== "0") {
        amount = parseInt(value);
      } else if (value === "<") {
        if (amount !== 0) {
          amount = Math.floor(amount / 10);
          if (amount.length === 0) {
            amount = 0;
            amountFiat = 0;
          }
        }
      } else if (value !== "<" && parseInt(amount + value) > sats) {
        warning($t("user.receive.lessThan1BTCWarning"));
      } else {
        console.log("CLICK", amount.toString(), value);
        amount = parseInt(amount.toString() + value);
        console.log("AMOUNT", amount);
      }
    }
  };

  $: html = fiat ? amountFiat : s(amount);

  let prev = "";

  let input = (e) => {
    if (prev === "0" && html !== "0.") {
      prev = "";
      html = html.replace("0", "");
    }

    let sel = getSelection();
    let i = sel.focusOffset;

    let clean = html
      .substr(0, 15)
      .replace(/[^0-9.,]+/g, "")
      .replace(".", "F")
      .replace(/\./g, "")
      .replace("F", ".");

    if (fiat) clean = clean.replace(",", "");
    if (clean !== html) {
      html = clean;
      i = html.length;
    }

    if (amount > sats && html.length > prev.length) {
      html = prev;
      warning($t("user.receive.lessThan1BTCWarning"));
      i--;
    }

    if (fiat) {
      amountFiat = html;
    } else {
      amount = parseInt(html.replace(/,/g, ""));
    }

    if (!amount) amount = null;

    setTimeout(() => {
      if (!fiat) {
        let p = prev.split(",")[0].length;
        if (html.length > prev.length && p === 3) i++;
        if (html.length < prev.length && p === 1 && i > 1) i--;
      }

      let node = e.target.childNodes[0];
      let range = document.createRange();

      if (node) {
        range.setStart(node, i);
        range.setEnd(node, i);

        let sel = getSelection();
        sel.removeAllRanges();
        sel.addRange(range);
      }

      prev = html.toString();
    }, 0);
  };

  let focus = (e) => {
    let range = document.createRange();
    range.selectNodeContents(e.target);
    let sel = getSelection();
    sel.removeAllRanges();
    sel.addRange(range);
  };

  let keydown = (e) =>
    e.key === "Enter" && (e.preventDefault() || submit.click());
</script>

<div class="flex justify-center items-center mb-3 px-3">
  <div class="space-y-5">
    <div class="text-center">
      <div
        class="text-5xl md:text-6xl font-semibold tracking-widest flex justify-center"
      >
        <div class="my-auto" class:text-5xl={!fiat}>
          {fiat ? symbol : "⚡️"}
        </div>
        <div
          contenteditable
          bind:innerHTML={html}
          on:focus={focus}
          on:input={input}
          on:keydown={keydown}
          class="outline-none my-auto"
        />
      </div>
      <div class="mt-2">
        <span class="text-secondary mr-1"
          >{fiat ? sat(amount) : f(amountFiat, currency)}</span
        >
        <button
          type="button"
          on:click={() => {
            if (fiat) {
              amount = parseInt((amountFiat / (rate / sats)).toFixed(0));
            } else {
              amountFiat =
                (amount * (rate / sats)).toFixed(2) > 0.0
                  ? (amount * (rate / sats)).toFixed(2)
                  : 0;
            }
            fiat = !fiat;
          }}><Icon icon="swap" style="inline hover:opacity-80" /></button
        >
      </div>
    </div>

    <div class="grid grid-cols-3 gap-2 w-[300px] mx-auto">
      {#each numPad as value}
        {#if value === arrow}
          <button
            type="button"
            class="bg-primary rounded-xl py-4 px-8 font-semibold active:bg-black active:text-white flex justify-center items-center hover:opacity-80"
            on:click={() => handleInput(value)}
          >
            <Left />
          </button>
        {:else}
          <button
            type="button"
            class="bg-primary rounded-xl py-4 px-8 font-semibold active:bg-black active:text-white hover:opacity-80"
            on:click={() => handleInput(value)}>{value}</button
          >
        {/if}
      {/each}
    </div>
  </div>
</div>