summaryrefslogtreecommitdiff
path: root/StoneIsland/platforms/android/assets/www/js/lib/account/ProfileView.js
blob: 1bbfff2255c12f6ca1bf9bd49c24c344c26231e0 (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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
var ProfileView = FormView.extend({
  
  el: "#profile",
  
  events: {
    "click .privacy-msg": "privacy_link",
  },
  
  action: sdk.account.update,
  
  initialize: function(){
    this.$form = this.$(".form")
    this.$msg = this.$(".msg")
    this.scroller = ScrollFactory('#profile', app.iscroll_options)
  },

  show: function(){
    if (! auth.logged_in()) { return app.router.go("intro") }
    if (! navigator.onLine) {
      app.closed.showElement()
      app.closed.setMessage("PLEASE GO ONLINE TO<br>EDIT YOUR PROFILE.", "")
      return
    }
    if (! auth.user) return
    app.account.consent.check()
    app.footer.show("SAVE")
    document.body.className = "profile"
    if (auth.user.BirthDay.match(/T/)) {
      auth.user.BirthDay = auth.user.BirthDay.split("T")[0]
    }
    if (auth.user.BirthDay === '1900-01-01') {
      auth.user.BirthDay = ''
    }
    if (auth.user.name && ! auth.user.Name) {
      auth.user.Name = auth.user.name
    }
    if (auth.user.surname && ! auth.user.Surname) {
      auth.user.Surname = auth.user.surname
    }
    if (auth.user.email && ! auth.user.Email) {
      auth.user.Email = auth.user.email
    }
    console.log(auth.user)
    this.preload(auth.user)
    this.deferScrollToTop()
  },

  validate_presence: {
    "Name": "Please enter your first name.",
    "Surname": "Please enter your last name.",
    "Email": "Please enter a valid email address.",
    // "BirthDay": "Please enter your birthday.",
  },
  
  validate_fields: function(data, errors){
    if (! data.Email.match("@")) { errors.push([ "Email", "Email address is not valid." ]) }
    if (! data.CurrentPassword && (data.NewPassword || data.Email !== auth.user.Email)) { errors.push([ "CurrentPassword", "Please enter your current password." ]) }
    if (data.CurrentPassword && ! data.NewPassword) { errors.push([ "NewPassword", "Please enter your new password." ]) }
    if (data.NewPassword && data.NewPassword.length < 7) { errors.push([ "CurrentPassword", "New password must be 7 characters or more." ]) }
    var now = new Date ()
    var year = now.getFullYear()
    if (! data.BirthDay) data.BirthDay = year + '-01-01'
    if (data.BirthDay.split('-')[0] || '2018')
    var birthday = new Date (data.BirthDay)
    if (isNaN(birthday) || (now - birthday) / (365*24*60*60*1000) < 18) {
      errors.push(['BirthDay', 'You must be 18 or older to use the Stone Island app.'])
    }
    // if (data.Gender === "NONE") { errors.push([ "Gender", "Please supply your gender." ]) }
  },

  finalize: function(data){
    if (data.CurrentPassword && (data.NewPassword || data.Email !== auth.user.Email)) {
      data.NewPassword = data.NewPassword || data.CurrentPassword
      data.NewEmail = data.NewEmail || auth.user.Email
      
      sdk.account.update_mail_and_password({
        data: {
          Password: data.CurrentPassword,
          NewPassword: data.NewPassword || data.CurrentPassword,
          Email: auth.user,
          NewEmail: data.NewEmail || auth.user.Email,
        },
        success: function(){ console.log("updated password") },
        error: function(){ console.log("error updating password") },
      })
    }

    var submissible_data = _.pick(data, "Name Surname BirthDay YooxLetter".split(" "))
    submissible_data.Gender = "U"
//     submissible_data.idUser = auth.user_id
//     submissible_data.AccessToken = auth.access_token
//     submissible_data.Premium = "false"
//     submissible_data.LanguageId = ""
//     submissible_data.SiteCode = "STONEISLAND_US"
//     submissible_data.FuriganaName = ""
//     submissible_data.FuriganaSurname = ""
//     submissible_data.UserPromocode = ""
    submissible_data.BirthDay += "T00:00:00Z"
    submissible_data.YooxLetter = this.$("[name=YooxLetter]").prop("checked")
    submissible_data.DataProfiling = this.$("[name=DataProfiling]").prop("checked")

    console.log(data.DataProfiling, submissible_data.DataProfiling)
    console.log(submissible_data)
    
    return submissible_data
  },
  
  privacy_link: function(){
    // rewrite app.privacy instance temporarily
    app.privacy.back = function(){
      app.router.go("account/profile")
    }
    app.privacy.hide = function(){
      app.privacy.back = app.privacy.hide = null
    }
    app.router.go("page/privacy")
  },

  success: function(data){
  },
  
  error: function(data){
  },
  
  cancel: function(){
    app.router.go("intro")
  },

})