summaryrefslogtreecommitdiff
path: root/static/js/ajax.js
blob: e31a0097d2a860e7b4b589e528ace6672ee3afdc (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
(function () {
    const btn = document.querySelector('#shortenButton');
    const inpUrl = document.querySelector('#url');
    const inpSlug = document.querySelector('#slug');

    function shorten (e) {
        if (!inpUrl.value) return;

        const req = new XMLHttpRequest();
        req.addEventListener('load', () => {
            const reply = JSON.parse(req.responseText);
            if (req.status != 201) {
                btn.innerText = reply.uiString || 'API Error';
                btn.classList.add('error');
                console.error(req.status, reply.errorMessage);
                return;
            }

            inpUrl.value = reply.data.shortUrl;
            inpSlug.value = '';
            btn.removeEventListener('click', shorten);
            btn.classList.remove('error');
            btn.innerText = 'Copy';
            btn.addEventListener('click', copyUrl);
            inpUrl.focus();
            inpUrl.select();
        });
        req.addEventListener('error', () => {
            btn.innerText = 'Connection Error';
            btn.classList.add('error');
        })

        if (inpSlug.value)
            req.open('PUT', '/'+encodeURIComponent(inpSlug.value));
        else
            req.open('POST', '/');
        req.setRequestHeader('Content-Type', 'application/json');
        req.send(JSON.stringify({url: encodeURI(inpUrl.value)}));
    }

    async function copyUrl(e) {
        await navigator.clipboard.writeText(encodeURI(inpUrl.value));
        btn.innerText = 'Copied!';
    }

    btn.addEventListener('click', shorten);
})();