spacedeck_account.js 3.16 KB
Newer Older
mntmn's avatar
mntmn committed
1
2
3
4
5
6
7
8
9
10
/*
  SpacedeckAccount
  This module contains functions dealing with the spacedeck account.
*/

SpacedeckAccount = {
  data: {
    account_confirmed_sent: false,
    account_tab: 'invoices',
    password_change_error: null,
11
    feedback_text: "",
mntmn's avatar
mntmn committed
12
13
  },
  methods: {
14
    show_account: function() {
mntmn's avatar
mntmn committed
15
      this.activate_dropdown('account');
16
17
    },

mntmn's avatar
mntmn committed
18
    account_save_user_digest: function(val) {
19
20
      this.user.prefs_email_digest = val;
      this.save_user(function() {  
mntmn's avatar
mntmn committed
21
22
23
24
      });
    },

    account_save_user_notifications: function(val) {
25
26
      this.user.prefs_email_notifications = val;
      this.save_user(function() {
mntmn's avatar
mntmn committed
27
28
29
30
31
32
33
34
35
36
      });
    },

    save_user_email: function() {
      this.save_user(function() {
      }.bind(this));
    },

    save_user_language: function(lang) {
      localStorage.lang = lang;
37
38
39
      this.user.prefs_language = lang;
      this.save_user(function() {
        window._spacedeck_location_change = true;
Wolfgang Knopki's avatar
Wolfgang Knopki committed
40
        location.href=ENV.endpoint + "/spaces";
41
      }.bind(this));
mntmn's avatar
mntmn committed
42
43
44
45
46
47
48
49
50
51
    },

    save_user: function(on_success) {
      if (this.user.email_changed) {
        this.user.confirmed_at = null;
      }
      window._spacedeck_location_change = true;

      save_user(this.user, function(user) {
        if (on_success) on_success();
Wolfgang Knopki's avatar
Wolfgang Knopki committed
52
        else location.href=ENV.endpoint + "/spaces";
mntmn's avatar
mntmn committed
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

      }.bind(this), function(xhr){
        console.error(xhr)
      });
    },

    save_user_password: function(oldPass, newPass, newPassConfirm) {
      this.password_change_error = null;

      if (!oldPass) {
        this.password_change_error = "Current password required";
        return;
      }

      if (!newPass || !newPassConfirm) {
        this.password_change_error = "New password/password confirmation required";
        return;
      }

      if (newPass!=newPassConfirm) {
        this.password_change_error = "New Passwords do not match";
        return;
      }

      if (newPass.length < 6) {
        this.password_change_error = "New Password to short";
        return;
      }

      save_user_password(this.user, oldPass, newPass, function() {
        alert("OK. Password Changed.");
        this.password_change_current = "";
        this.password_change_new = "";
        this.password_change_new_confirmation = "";
      }.bind(this), function(xhr) {
        if (xhr.status == 403) {
          this.password_change_error = "Old Password not correct";
        } else {
          this.password_change_error = "Something went wrong. Please try again later.";
        }
      }.bind(this));
    },

    confirm_again: function() {
      resent_confirm_mail(this.user, function(re) {
        this.account_confirmed_sent = true;

        alert(__("confirm_again"));

      }.bind(this), function(xhr){
        console.error(xhr);
        alert("Something went wrong, please try again.");
      });
    },

    confirm_account: function(token) {
      confirm_user(this.user, token, function(re) {
        smoke.alert(__("confirmed"), function() {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
111
          this.redirect_to(ENV.prefix+"/spaces");
mntmn's avatar
mntmn committed
112
113
114
115
        }.bind(this));
      }.bind(this), function(xhr) {
        console.error(xhr);
        alert(xhr.responseText);
Wolfgang Knopki's avatar
Wolfgang Knopki committed
116
        this.redirect_to(ENV.prefix+"/spaces");
mntmn's avatar
mntmn committed
117
118
119
120
      }.bind(this));
    },
  }
}