Settings.vue 5.33 KB
Newer Older
1
<template>
Bat's avatar
Bat committed
2
  <div class="main pusher" v-title="'Account Settings'">
3
4
    <div class="ui vertical stripe segment">
      <div class="ui small text container">
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
        <h2 class="ui header">Account settings</h2>
        <form class="ui form" @submit.prevent="submitSettings()">
          <div v-if="settings.success" class="ui positive message">
            <div class="header">Settings updated</div>
          </div>
          <div v-if="settings.errors.length > 0" class="ui negative message">
            <div class="header">We cannot save your settings</div>
            <ul class="list">
              <li v-for="error in settings.errors">{{ error }}</li>
            </ul>
          </div>
          <div class="field" v-for="f in orderedSettingsFields">
            <label :for="f.id">{{ f.label }}</label>
            <p v-if="f.help">{{ f.help }}</p>
            <select v-if="f.type === 'dropdown'" class="ui dropdown" v-model="f.value">
              <option :value="c.value" v-for="c in f.choices">{{ c.label }}</option>
            </select>
          </div>
          <button :class="['ui', {'loading': isLoading}, 'button']" type="submit">Update settings</button>
        </form>
      </div>
      <div class="ui hidden divider"></div>
      <div class="ui small text container">
        <h2 class="ui header">Change my password</h2>
        <form class="ui form" @submit.prevent="submitPassword()">
          <div v-if="passwordError" class="ui negative message">
31
32
            <div class="header">Cannot change your password</div>
            <ul class="list">
33
              <li v-if="passwordError == 'invalid_credentials'">Please double-check your password is correct</li>
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
            </ul>
          </div>
          <div class="field">
            <label>Old password</label>
            <input
            required
            type="password"
            autofocus
            placeholder="Enter your old password"
            v-model="old_password">
          </div>
          <div class="field">
            <label>New password</label>
            <input
            required
            type="password"
            autofocus
            placeholder="Enter your new password"
            v-model="new_password">
          </div>
          <button :class="['ui', {'loading': isLoading}, 'button']" type="submit">Change password</button>
        </form>
      </div>
    </div>
  </div>
</template>

<script>
62
import $ from 'jquery'
Eliot Berriot's avatar
Eliot Berriot committed
63
import axios from 'axios'
64
65
66
67
import logger from '@/logging'

export default {
  data () {
68
    let d = {
69
70
71
72
      // We need to initialize the component with any
      // properties that will be used in it
      old_password: '',
      new_password: '',
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
      passwordError: '',
      isLoading: false,
      settings: {
        success: false,
        errors: [],
        order: ['privacy_level'],
        fields: {
          'privacy_level': {
            type: 'dropdown',
            initial: this.$store.state.auth.profile.privacy_level,
            label: 'Activity visibility',
            help: 'Determine the visibility level of your activity',
            choices: [
              {
                value: 'me',
                label: 'Nobody except me'
              },
              {
                value: 'instance',
                label: 'Everyone on this instance'
              }
            ]
          }
        }
      }
98
    }
99
100
101
102
103
104
105
    d.settings.order.forEach(id => {
      d.settings.fields[id].value = d.settings.fields[id].initial
    })
    return d
  },
  mounted () {
    $('select.dropdown').dropdown()
106
107
  },
  methods: {
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
    submitSettings () {
      this.settings.success = false
      this.settings.errors = []
      let self = this
      let payload = this.settingsValues
      let url = `users/users/${this.$store.state.auth.username}/`
      return axios.patch(url, payload).then(response => {
        logger.default.info('Updated settings successfully')
        self.settings.success = true
      }, error => {
        logger.default.error('Error while updating settings')
        self.isLoading = false
        self.settings.errors = error.backendErrors
      })
    },
    submitPassword () {
124
125
126
127
128
129
130
131
      var self = this
      self.isLoading = true
      this.error = ''
      var credentials = {
        old_password: this.old_password,
        new_password1: this.new_password,
        new_password2: this.new_password
      }
132
      let url = 'auth/registration/change-password/'
Eliot Berriot's avatar
Eliot Berriot committed
133
      return axios.post(url, credentials).then(response => {
134
        logger.default.info('Password successfully changed')
135
136
137
138
139
140
141
        self.$router.push({
          name: 'profile',
          params: {
            username: self.$store.state.auth.username
          }})
      }, error => {
        if (error.response.status === 400) {
142
          self.passwordError = 'invalid_credentials'
143
        } else {
144
          self.passwordError = 'unknown_error'
145
146
147
148
        }
        self.isLoading = false
      })
    }
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
  },
  computed: {
    orderedSettingsFields () {
      let self = this
      return this.settings.order.map(id => {
        return self.settings.fields[id]
      })
    },
    settingsValues () {
      let self = this
      let s = {}
      this.settings.order.forEach(setting => {
        let conf = self.settings.fields[setting]
        s[setting] = conf.value
      })
      return s
    }
166
167
168
169
170
171
172
173
  }

}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>