<reponame>RubyvanderHolst/interface-dnabarcoder<filename>static/js/trees.js<gh_stars>0
document.addEventListener("DOMContentLoaded", function(){
    let task_id = document.getElementById('task_id').value
    get_data()
    function get_data() {
        $.ajax({
            url: "tree/" + task_id,
            type: "GET",
            dataType: "json",
            success: (data) => {
                document.getElementById('task_status').innerHTML = data.state.toLowerCase();
                if (data.state === 'SUCCESS') {
                    document.getElementById('spinner-box').classList.add('d-none');

                    if (data.no_trees.length > 0) {
                        let warning_text = `<b>Warning!</b> No trees could be produced for the following sequence IDs: `
                        for (const id of data.no_trees) {
                            warning_text += id + ', '
                        }
                        warning_text = warning_text.substring(0, warning_text.length - 2)
                        warning_text += `<br>This is probably due to a lack of 
                            similar reference sequences`

                        document.getElementById('warning').classList.remove('d-none');
                        document.getElementById('warning').innerHTML = warning_text;
                    }

                    if (data.download_file !== null) {
                        document.getElementById('download_button').classList.remove('d-none');
                        document.getElementById('download_button').href = data.download_file;
                    }

                } else if (data.state === 'PENDING') {
                    setTimeout(function(){
                        get_data()
                    }, 10**3) // reload after 1 second
                }
            },
            error: (error) => {
                console.log(error)
            }
        })
    }
})