All files / app/web-ui/scripts state-scripts.ts

100% Statements 157/157
100% Branches 1/1
100% Functions 1/1
100% Lines 157/157

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 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 1571x 1x 1x 1x 1x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x
/**
 * This script exposes the triggerSync() and triggerReauth() functions.
 * Additionally the refreshState() function will be executed through an interval.
 */
export const stateViewScript = (basePath: string) => `
async function triggerSync() {
    const response = await fetch("${basePath}/api/sync", { method: "POST" });
    if (!response.ok) {
        alert("Unable to trigger sync: " + response.statusText);
        return;
    }
    await refreshState()
}
async function triggerReauth() {
    const response = await fetch("${basePath}/api/reauthenticate", { method: "POST" });
    if (!response.ok) {
        alert("Unable to trigger re-authentication: " + response.statusText);
        return;
    }
    await refreshState()
}
 
async function refreshState() {
    const state = await fetchState()
    resetState()
    updateState(state)
}
 
async function fetchState() {
    try{
        const fetchedState = await fetch("${basePath}/api/state", { 
            headers: {
                "Accept": "application/json"
            }
        })
 
        if(!fetchedState.ok) {
            throw new Error('Response not ok!')
        }
        
        return fetchedState.json();
    } catch (err) {
 
        console.log(err)
        return {
            state: 'ready',
            prevError: {
                message: 'Connection lost, please refresh!',
                code: 'CLIENT_ERR-CONNECTION_LOST'
            },
            timestamp: Date.now()
        };
    }
}
 
function setStateText(text) {
    document.querySelector("#state-text").innerHTML = text
}
 
function setProgress(progress) {
    if(progress && progress >= 0) {
        document.getElementById('progress-container').style.display = "block";
        document.getElementById('progress-bar').style.width = progress + '%';
    } else {
        document.getElementById('progress-container').style.display = "none";
        document.getElementById('progress-bar').style.width = '0%';
    }
}
 
function enableSymbol(symbolName) {
    document.querySelector("#" + symbolName + "-symbol").style.display = "block";
}
 
/**
 * This function resets the state and hides all dynamic elements
 */
function resetState() {
    setStateText('...')
    setProgress()
    document.querySelectorAll(".state-symbol").forEach((el) => {
        el.style.display = "none";
    });
    document.querySelectorAll(".hidden-when-not-ready").forEach((el) => {
        el.style.display = "none";
    });
}
 
/**
 * This function fetches the current app state and applies changes to the view
 * @param state - expects the json object form the API or undefined (will reload page on undefined)
 */
function updateState(state) {
    if(!state) {
        window.location.reload();
    }
 
    if(state.nextSync) {
        document.querySelector("#next-sync-time").innerHTML = formatDate(state.nextSync);
    }
 
    switch (state.state) {
        case 'ready':
            // Increase time between refresh while application is idle
            setTimeout(() => refreshState(), 5000);
 
            document.querySelectorAll(".hidden-when-not-ready").forEach((el) => {
                el.style.display = "block";
            });
 
            // If there was an error reported, show it
            if(state.prevError) {
                setStateText("Last " + (state.prevTrigger ?? "operation") + " failed at<br/>" +
                    formatDate(state.timestamp) + 
                    "<br/><br/>" +
                    "<span style='color: red; font-weight: bold'>" +
                        state.prevError.message +
                    "</span>"
                )
                enableSymbol('error')
                return
            } 
 
            enableSymbol('ok')
 
            if(!state.prevTrigger) {
                setStateText("Application ready")
                return
            }
 
            setStateText("Last " + (state.prevTrigger ?? "operation") + " successful at<br/>" +
                formatDate(state.timestamp) 
            )
            return;
        case 'blocked': 
            navigate('${basePath}/submit-mfa')
            return;
        case 'running':
            // Decrease time between refresh while application is running
            setTimeout(() => refreshState(), 500);
 
            setStateText(state.progressMsg ?? 'Syncing...')
            setProgress(state.progress)
            enableSymbol('running')
 
            return;
        default:
            setTimeout(() => refreshState(), 5000);
 
            setStateText('Unknown')
            enableSymbol('unknown')
            return;
    }
}
 
// Kick off function to update state
setTimeout(() => refreshState(), 0);
`