members-space-my-shifts.js 11.1 KB
Newer Older
1 2 3 4
var history_table = null;

const history_items_limit = 10;

5 6 7
/**
 * Load the partner points history
 */
François committed
8
function load_partner_history(offset = 0) {
9 10 11
    return new Promise((resolve) => {
        $.ajax({
            type: 'GET',
12
            url: "/members_space/get_shifts_history",
13
            data: {
14
                partner_id: partner_data.concerned_partner_id,
15
                verif_token: partner_data.verif_token,
16
                limit: history_items_limit,
Damien Moulard committed
17
                offset: offset
18 19 20 21 22
            },
            dataType:"json",
            traditional: true,
            contentType: "application/json; charset=utf-8",
            success: function(data) {
23 24
                formatted_data = prepare_server_data(data.data);
                resolve(formatted_data);
25 26
            },
            error: function(data) {
27
                err = {msg: "erreur serveur lors de la récupération de l'historique", ctx: 'load_partner_history'};
28 29 30
                if (typeof data.responseJSON != 'undefined' && typeof data.responseJSON.error != 'undefined') {
                    err.msg += ' : ' + data.responseJSON.error;
                }
31
                report_JS_error(err, 'members_space.my_shifts');
32 33 34

                closeModal();
                // TODO Notify
35
                alert('Erreur lors de la récupération de votre historique.');
36 37 38 39 40
            }
        });
    });
}

41 42
/**
 * Format history data to insert in the table
François committed
43 44
 *
 * @param {Array} data
45 46 47 48
 * @returns formated data array
 */
function prepare_server_data(data) {
    res = [];
49

50
    for (history_item of data) {
Damien Moulard committed
51
        if (history_item.is_amnesty !== undefined) {
52
            let shift_datetime = new Date(history_item.date_begin);
François committed
53
            let str_shift_datetime = `${("0" + shift_datetime.getDate()).slice(-2)}/${("0" + (shift_datetime.getMonth() + 1)).slice(-2)}/${shift_datetime.getFullYear()}`;
54

François committed
55
            history_item.shift_name = `${history_item.shift_name} du ${str_shift_datetime}`;
Damien Moulard committed
56 57 58 59
        } else {
            history_item.shift_name = (history_item.shift_id === false) ? '' : history_item.shift_id[1];
            if (history_item.name === "Services des comités") {
                let shift_datetime = new Date(history_item.date_begin);
François committed
60 61 62

                let str_shift_datetime = `${("0" + shift_datetime.getDate()).slice(-2)}/${("0" + (shift_datetime.getMonth() + 1)).slice(-2)}/${shift_datetime.getFullYear()}`;

Damien Moulard committed
63
                str_shift_datetime = str_shift_datetime + " " + shift_datetime.toLocaleTimeString("fr-fr", time_options);
François committed
64 65

                history_item.shift_name = `Services des comités ${str_shift_datetime}`;
Damien Moulard committed
66
            }
67 68
        }

Etienne Freiss committed
69 70
        if (history_item.associate_registered == false || history_item.associate_registered == undefined) {
            history_item.associate_registered = "";
Damien Moulard committed
71
        } else {
Etienne Freiss committed
72 73 74 75 76 77 78 79 80
            if (partner_data.associated_partner_id != "False") {
                if (history_item.associate_registered==="partner") {
                    history_item.associate_registered = partner_data.name;
                } else if (history_item.associate_registered==="associate") {
                    history_item.associate_registered = partner_data.associated_partner_name;
                } else if (history_item.associate_registered==="both") {
                    history_item.associate_registered = "Les deux";
                } else {
                    history_item.associate_registered = "";
Etienne Freiss committed
81
                }
Etienne Freiss committed
82 83 84 85 86 87 88 89 90
            } else if (partner_data.parent_id != "False") {
                if (history_item.associate_registered==="partner") {
                    history_item.associate_registered = partner_data.parent_name;
                } else if (history_item.associate_registered==="associate") {
                    history_item.associate_registered = partner_data.name;
                } else if (history_item.associate_registered==="both") {
                    history_item.associate_registered = "Les deux";
                } else {
                    history_item.associate_registered = "";
Etienne Freiss committed
91 92 93
                }
            }
        }
94 95
        history_item.details = '';
        if (history_item.state === 'excused' || history_item.state === 'absent') {
Félicie committed
96
            history_item.details = "Absent.e";
97
        } else if (history_item.state === 'done' && history_item.is_late != false) {
Félicie committed
98
            history_item.details = "Présent.e (En Retard)";
99
        } else if (history_item.state === 'done') {
Félicie committed
100
            history_item.details = "Présent.e";
101 102
        } else if (history_item.state === 'cancel') {
            history_item.details = "Annulé";
103 104
        }
    }
105 106

    return data;
107 108
}

109
/**
François committed
110
 * Init the History section: display the history table
111 112 113 114 115
 */
function init_history() {
    $(".loading-history").hide();
    $("#history").show();
    if (partner_history.length === 0) {
François committed
116 117
        $("#history").empty()
            .text("Aucun historique... pour l'instant !");
118 119 120 121
    } else {
        history_table = $('#history_table').DataTable({
            data: partner_history,
            columns: [
122 123 124 125 126
                {
                    data: "date_begin",
                    title: "",
                    visible: false
                },
127 128
                {
                    data: "shift_name",
129
                    title: "<spans class='dt-body-center'>Service</span>",
Etienne Freiss committed
130
                    width: "50%",
131
                    orderable: false
132 133
                },
                {
134
                    data: "details",
135
                    title: "Détails",
136 137
                    className: "tablet-l desktop",
                    orderable: false
Etienne Freiss committed
138 139 140 141 142
                },
                {
                    data: "associate_registered",
                    title: "",
                    orderable: false
Damien Moulard committed
143
                }
144 145
            ],
            iDisplayLength: -1,
146 147 148 149 150 151
            order: [
                [
                    0,
                    "desc"
                ]
            ],
152 153 154 155 156 157
            language: {url : '/static/js/datatables/french.json'},
            dom: "t",
            responsive: true,
            createdRow: function(row) {
                for (var i = 0; i < row.cells.length; i++) {
                    const cell = $(row.cells[i]);
François committed
158

Félicie committed
159
                    if (cell.text() === "Présent.e") {
160
                        $(row).addClass('row_partner_ok');
161
                    } else if (cell.text().includes("Retard")) {
162
                        $(row).addClass('row_partner_late');
Félicie committed
163
                    } else if (cell.text() === "Absent.e") {
164
                        $(row).addClass('row_partner_absent');
Damien Moulard committed
165
                    } else if (cell.text().includes("Amnistie")) {
166
                        $(row).addClass('row_partner_amnistie');
167 168 169 170 171 172 173 174 175 176
                    }
                }
            }
        });
    }
}

/**
 * Init the Incoming shifts section: display them
 */
177
function init_incoming_shifts() {
178 179
    $(".loading-incoming-shifts").hide();
    $("#incoming_shifts").show();
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    if (partner_data.comite === "True") {
            let message = $('#comite_my_shifs_message').clone()
            message.find('[data-type="nb_of_shifs_state"] [data-type="shifts_nb"]').text(partner_data.final_ftop_point)
            if (Math.abs(partner_data.final_ftop_point) > 1) {
                message.find('[data-type="nb_of_shifs_state"] [data-type="service_txt"]').text("services")
            }
            // let's get next ftop shift (incoming_shifts is ordered)
            if (incoming_shifts.length > 0) {
                const next_shift = incoming_shifts[0]
                let ns_date = new Date(next_shift.date_begin)
                const date_options = {dateStyle: "short"}
                message.find('[data-type="next_ftop_shift_date"]').text(ns_date.toLocaleDateString('fr-FR', date_options))
            }
            
            $("#incoming_shifts_area").html(message)
195
    } else {
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
        if (incoming_shifts.length === 0) {
            $("#incoming_shifts").text("Aucun service à venir...");
        } else {
            $("#incoming_shifts").empty();

            for (shift of incoming_shifts) {
                let shift_line_template = prepare_shift_line_template(shift.date_begin);

                if (partner_data.associated_partner_id != "False") {
                    if (shift.associate_registered==="partner") {
                        shift_line_template.find(".shift_line_associate").text(' - '+partner_data.name+'');
                    } else if (shift.associate_registered==="associate") {
                        shift_line_template.find(".shift_line_associate").text(' - '+partner_data.associated_partner_name+'');
                    } else if (shift.associate_registered==="both") {
                        shift_line_template.find(".shift_line_associate").text(' - Les deux');
                    } else {
                        shift_line_template.find(".shift_line_associate").text('A définir');
                    }
                } else if (partner_data.parent_id != "False") {
                    if (shift.associate_registered==="partner") {
                        shift_line_template.find(".shift_line_associate").text(' - '+partner_data.parent_name+'');
                    } else if (shift.associate_registered==="associate") {
                        shift_line_template.find(".shift_line_associate").text(' - '+partner_data.name+'');
                    } else if (shift.associate_registered==="both") {
                        shift_line_template.find(".shift_line_associate").text(' - Les deux');
                    } else {
                        shift_line_template.find(".shift_line_associate").text('A définir');
                    }
Etienne Freiss committed
224
                }
Etienne Freiss committed
225

226 227
                $("#incoming_shifts").append(shift_line_template.html());
            }
228
        }
229
    }   
230 231 232 233 234 235
}

function init_my_shifts() {
    if (incoming_shifts !== null) {
        init_incoming_shifts();
    } else {
236
        load_partner_shifts(partner_data.concerned_partner_id)
237 238 239 240 241 242 243
            .then(init_incoming_shifts);
    }

    if (partner_history !== null) {
        init_history();
    } else {
        load_partner_history()
244 245 246
            .then((data) => {
                partner_history = data;

247 248 249
                for (d of data) {
                    d.create_date = Date.parse(d.create_date);
                }
250
                // Sort by date desc
251 252 253 254
                partner_history.sort((a, b) => b.create_date - a.create_date);
                if (partner_history.length>0 && partner_history[partner_history.length-1].is_amnesty != undefined) {
                    partner_history.pop();
                }
255 256 257

                init_history();
            });
258
    }
259 260 261 262 263 264 265 266 267

    $(".more_history_button").on("click", function() {
        // Hide button & display loading
        $('.more_history_button').hide();
        $('.loading-more-history').show();

        load_partner_history(partner_history.length)
            .then((data) => {
                partner_history = partner_history.concat(data);
François committed
268 269
                if (history_table) {
                    history_table.rows.add(data).draw(false);
270 271 272 273 274 275 276 277 278
                }

                $('.loading-more-history').hide();
                // Show "load more" if there is more to load
                if (data.length === history_items_limit) {
                    $('.more_history_button').show();
                }
            });
    });
Félicie committed
279
}