summaryrefslogtreecommitdiff
path: root/day9/task5/static/js/main.js
blob: 2a4534124a6231cc7725b3bb8610d4f2e893a044 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
function onPageLoad() {
    request.post(`${HOST}/get`, renderTable, {'type': 'full'});

    let btn = document.getElementById('uploadFileBtn');
    btn.innerHTML = uploadFileHtml;
}

function renderTable(text) {
    let data = JSON.parse(text);

    let table = document.getElementById('table');

    while (table.firstChild) table.removeChild(table.firstChild);

    let tableHeaders = document.createElement('thead');
    let headerRow = document.createElement('tr');
    tableHeaders.appendChild(headerRow);
    data['headers'].forEach((field) => {
        let header = document.createElement('th');
        header.innerText = field;
        headerRow.appendChild(header);
    });
    headerRow.appendChild(document.createElement('th'));
    headerRow.appendChild(document.createElement('th'));

    let tableContent = document.createElement('tbody');
    data['content'].forEach((row) => {
        row.push(`<button value="${row[0]}" onclick="setupEditFormFields('${row[0]}')">&#9998</button>`);
        row.push(`<button value="${row[0]}" onclick="removeField('${row[0]}')">&#10006</button>`);

        let contentRow = document.createElement('tr');
        contentRow.setAttribute('id', `row_${row[0]}`);

        row.forEach((column, colIndex) => {
            let columnNode = document.createElement('td');

            if (colIndex !== 0 && colIndex < data['headers'].length) {
                let fieldId = `${data['headers'][colIndex]}-${row[0]}`;
                columnNode.classList.add('editableField');
                columnNode.onclick = () => { onFieldClick(fieldId) };
                columnNode.setAttribute('id', fieldId);
            }

            columnNode.innerHTML = column;
            contentRow.appendChild(columnNode);
        });

        tableContent.appendChild(contentRow);
    });

    table.appendChild(tableHeaders);
    table.appendChild(tableContent);
}

function setupEditFormFields(service_id) {
    showEditForm();
    request.post(`${HOST}/get`, (text) => {
        let row = JSON.parse(text);

        Object.keys(row).forEach((element) => {
            document.getElementsByName(element)[0].value = row[element];
        });

        let [sent_date, sent_time] = row['creation_request_sent_date'].split(' ');
        document.getElementsByName('creation_request_sent_date')[0].value = sent_date;
        document.getElementsByName('creation_request_sent_time')[0].value = sent_time;
    }, {
        'type': 'single_id', 'service_id': service_id
    });
}