|
| 1 | +import Component from '@ember/component'; |
| 2 | +import { empty } from '@ember/object/computed'; |
| 3 | + |
| 4 | +export default Component.extend({ |
| 5 | + type: '', |
| 6 | + value: '', |
| 7 | + isEditing: false, |
| 8 | + user: null, |
| 9 | + disableSave: empty('user.email'), |
| 10 | + notValidEmail: false, |
| 11 | + prevEmail: '', |
| 12 | + emailIsNull: true, |
| 13 | + |
| 14 | + actions: { |
| 15 | + editEmail() { |
| 16 | + let email = this.get('value'); |
| 17 | + let isEmailNull = function(email) { |
| 18 | + return (email == null); |
| 19 | + }; |
| 20 | + |
| 21 | + this.set('emailIsNull', isEmailNull(email)); |
| 22 | + this.set('isEditing', true); |
| 23 | + this.set('prevEmail', this.get('value')); |
| 24 | + }, |
| 25 | + |
| 26 | + saveEmail() { |
| 27 | + let userEmail = this.get('value'); |
| 28 | + let user = this.get('user'); |
| 29 | + |
| 30 | + let emailIsProperFormat = function(userEmail) { |
| 31 | + let regExp = /^\S+@\S+\.\S+$/; |
| 32 | + return regExp.test(userEmail); |
| 33 | + }; |
| 34 | + |
| 35 | + if (!emailIsProperFormat(userEmail)) { |
| 36 | + this.set('notValidEmail', true); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + user.set('email', userEmail); |
| 41 | + user.save() |
| 42 | + .then(() => this.set('serverError', null)) |
| 43 | + .catch(err => { |
| 44 | + let msg; |
| 45 | + if (err.errors && err.errors[0] && err.errors[0].detail) { |
| 46 | + msg = `An error occurred while saving this email, ${err.errors[0].detail}`; |
| 47 | + } else { |
| 48 | + msg = 'An unknown error occurred while saving this email.'; |
| 49 | + } |
| 50 | + this.set('serverError', msg); |
| 51 | + }); |
| 52 | + |
| 53 | + this.set('isEditing', false); |
| 54 | + this.set('notValidEmail', false); |
| 55 | + }, |
| 56 | + |
| 57 | + cancelEdit() { |
| 58 | + this.set('isEditing', false); |
| 59 | + this.set('value', this.get('prevEmail')); |
| 60 | + } |
| 61 | + } |
| 62 | +}); |
0 commit comments