info_perso.js 3.96 KB
Newer Older
Administrator committed
1 2 3 4 5
try {
    let y_sel = $('[name="yyyy"]'),
        m_sel = $('[name="mm"]'),
        j_sel = $('[name="jj"]'),
        birthdate = $('[name="birthdate"]'),
6
        save_btn = $('#save');
Administrator committed
7 8 9

    var make_save_button_active = function() {
        if (! save_btn.hasClass('btn--primary')) {
10 11
            save_btn.addClass('btn--primary');
            save_btn.css({'cursor':'pointer'});
Administrator committed
12
        }
13
    };
Administrator committed
14 15

    var update_birthdate = function() {
16 17 18 19 20 21 22 23 24 25
        birthdate.val(y_sel.val() + '-' + m_sel.val() + '-' + j_sel.val());
        make_save_button_active();
    };

    var init_birthdate_selects = function() {
        let [
            y,
            m,
            d
        ] = birthdate.val().split('-');
Administrator committed
26

27 28
        var now = new Date();
        var end_year = new Date(now.setYear(now.getFullYear() - 15)).getFullYear();
Administrator committed
29 30

        for (var i=100; i>0; i--) {
31
            let opt = $('<option>').val(end_year-i)
32 33 34 35
                .text(end_year-i);

            if (end_year-i == y) opt.prop('selected', true);
            opt.appendTo(y_sel);
Administrator committed
36 37
        }
        for (var k=1; k<=12; k++) {
38
            let mth = k.pad(2);
39

40
            let opt = $('<option>').val(mth)
41 42 43 44
                .text(mth);

            if (m == mth) opt.prop('selected', true);
            opt.appendTo(m_sel);
Administrator committed
45 46
        }
        for (var l=1; l<=31; l++) {
47
            let day = l.pad(2);
48

49
            let opt = $('<option>').val(day)
50 51 52 53
                .text(day);

            if (d == day) opt.prop('selected', true);
            opt.appendTo(j_sel);
Administrator committed
54
        }
55 56 57 58
        y_sel.change(update_birthdate);
        m_sel.change(update_birthdate);
        j_sel.change(update_birthdate);
    };
Administrator committed
59 60 61

    var save_data = function() {
        //Transmit only what has been changed
62
        var changed = {};
Administrator committed
63 64

        for (attr in original) {
65 66
            var current_val = $('[name="' + attr + '"]').val();

Administrator committed
67
            if (attr == 'sex') {
68
                current_val = $('[name="' + attr + '"]:checked').val();
Administrator committed
69 70 71
            }
            if (current_val.trim() != original[attr]) {
                if (! (original[attr] == 'False' && current_val.length ==0))
72
                    changed[attr] = current_val.trim();
Administrator committed
73 74 75 76 77 78 79
            }
        }
        // console.log(changed)
        if (Object.keys(changed).length > 0) {
            if ('firstname' in changed || 'lastname' in changed) {
                changed['name'] = $('[name="firstname"]').val()
                                  + name_sep
80
                                  + $('[name="lastname"]').val();
Administrator committed
81
            }
82
            delete changed['email'];
Administrator committed
83 84 85 86 87 88 89
            post_form(
                '/website/update_info_perso',
                changed,
                function(err, result) {
                    if (typeof result.res.process != "undefined" &&
                        typeof result.res.process.update != "undefined" &&
                        result.res.process.update == true) {
90 91 92 93 94 95 96
                        save_btn.removeClass('btn--primary');
                        save_btn.css({'cursor':'default'});
                        for (attr in changed) {
                            if (attr != 'name')
                                original[attr] = changed[attr];
                        }
                        alert('Modifications enregistrées !');
Administrator committed
97
                    } else {
98
                        alert('Une erreur est intervenue pendant l\'enregistrement');
Administrator committed
99 100 101
                    }

                }
102
            );
Administrator committed
103
        } else {
104
            alert('Aucune modification significative détectée');
Administrator committed
105 106
        }

107 108
    };

Administrator committed
109
    if (typeof original.sex != "undefined") {
110
        $('input[name="sex"][value="' + original.sex + '"]').prop('checked', true);
Administrator committed
111 112
    }

113
    init_birthdate_selects();
114
    $('input').change(make_save_button_active);
Administrator committed
115 116 117 118 119

    save_btn.click(function() {
        if ($(this).hasClass('btn--primary') && is_time_to('save_perso_data')) {
            save_data();
        }
120
    });
Administrator committed
121

122 123 124
} catch (error) {
    err_obj = {msg: error.name + ' : ' + error.message, ctx: 'info_perso'};
    report_JS_error(error, 'website');
125
}